Exemple #1
0
func (self *comm) Energy() cagent.Energy {
	if ok, args := self.acked_send(link.NewMessage(link.Commands["Energy"])); ok {
		if len(args) == 2 {
			return cagent.MakeEnergy(args[1])
		}
	}
	panic("didn't get an energy")
}
Exemple #2
0
func (self *comm) Listen(freq uint8) []byte {
	m := link.NewMessage(link.Commands["Listen"], newListen(freq))
	if ok, args := self.acked_send(m); ok {
		if len(args) == 2 {
			return args[1]
		}
	}
	panic("didn't get an energy")
	self.recv()
	return nil
}
Exemple #3
0
func (self *AgentProxy) ack_cmd(cmd link.Command) {
	self.snd <- *link.NewMessage(link.Commands["Ack"], cmd)
	//     self.log.Println(link.NewMessage(link.Commands["Ack"], cmd))
}
Exemple #4
0
func (self *comm) complete() bool {
	r, _ := self.acked_send(link.NewMessage(link.Commands["Complete"]))
	return r
}
Exemple #5
0
func (self *comm) ack_migrate() {
	self.send(link.NewMessage(link.Commands["Ack"], link.Commands["Migrate"]))
}
Exemple #6
0
func (self *comm) Collect() bool {
	r, _ := self.acked_send(link.NewMessage(link.Commands["Collect"]))
	return r
}
Exemple #7
0
func (self *comm) PrevResult() bool {
	self.send(link.NewMessage(link.Commands["PrevResult"]))
	self.recv()
	return false
}
Exemple #8
0
func (self *AgentProxy) start_turn() bool {
	return self.acked_send(link.NewMessage(link.Commands["Start"], self.state.Turn))
}
Exemple #9
0
func (self *comm) ack_start() {
	self.send(link.NewMessage(link.Commands["Ack"], link.Commands["Start"]))
}
Exemple #10
0
func (self *comm) Broadcast(freq uint8, msg []byte) bool {
	r, _ := self.acked_send(link.NewMessage(link.Commands["Broadcast"], newBroadcast(freq, msg)))
	return r
}
Exemple #11
0
func (self *comm) Look() link.Vision {
	self.send(link.NewMessage(link.Commands["Look"]))
	self.recv()
	return nil
}
Exemple #12
0
func (self *AgentProxy) Turn() bool {
	type handler (func(*link.Message) bool)

	check_args := func(count int, args link.Arguments) bool {
		if len(args) == count {
			return true
		}
		self.log.Println("Error : Wrong number of arguments recieved")
		return false
	}

	argnum := func(count int, f handler) handler {
		return func(msg *link.Message) bool {
			if check_args(count, msg.Args) {
				return f(msg)
			}
			return false
		}
	}

	var handlers = map[link.Command]handler{
		link.Commands["Complete"]: argnum(0, func(msg *link.Message) bool {
			self.ack_cmd(msg.Cmd)
			return true
		}),
		link.Commands["Move"]: argnum(1, func(msg *link.Message) bool {
			mv := geo.MakePoint(msg.Args[0])
			if self.state.Mv(mv) {
				self.ack_cmd(msg.Cmd)
			} else {
				self.nak_cmd(msg.Cmd)
			}
			return false
		}),
		link.Commands["Look"]: argnum(0, func(msg *link.Message) bool {
			self.send(link.NewMessage(link.Commands["Ack"], msg.Cmd, nil))
			return false
		}),
		link.Commands["Listen"]: argnum(1, func(msg *link.Message) bool {
			freq := agent.MakeListen(msg.Args[0]).Listen()
			heard := self.game.Listen(self.state.Position, freq)
			self.send(link.NewMessage(link.Commands["Ack"], msg.Cmd, heard))
			return false
		}),
		link.Commands["Broadcast"]: argnum(1, func(msg *link.Message) bool {
			freq, pkt := agent.MakeBroadcast(msg.Args[0]).Message()
			if self.state.Broadcast(freq, pkt) {
				self.ack_cmd(msg.Cmd)
			} else {
				self.nak_cmd(msg.Cmd)
			}
			return false
		}),
		link.Commands["Collect"]: argnum(0, func(msg *link.Message) bool {
			if self.state.Collect() {
				self.ack_cmd(msg.Cmd)
			} else {
				self.nak_cmd(msg.Cmd)
			}
			return false
		}),
		link.Commands["Energy"]: argnum(0, func(msg *link.Message) bool {
			self.send(link.NewMessage(link.Commands["Ack"], msg.Cmd, self.state.Energy))
			return false
		}),
		link.Commands["PrevResult"]: argnum(0, func(msg *link.Message) bool {
			//                 self.send(link.NewMessage(link.Commands["Ack"], msg.Cmd, self.state.PrevResult()))
			return false
		}),
	}

	handle := func(msg *link.Message) bool {
		if f, ok := handlers[msg.Cmd]; ok {
			return f(msg)
		}
		panic(fmt.Sprintf("Command %s not found.", msg.Cmd))
	}

	complete := make(chan bool)
	if self.game == nil {
		panic("(agentproxy) self.game == nil")
	}
	self.state.NewMove()
	if !self.state.Alive {
		return false
	}
	//     self.log.Println("Starting Turn", self.state.Turn)
	//     self.log.Println(self.state)
	if !self.start_turn() {
		return false
	}
	go func(done chan<- bool) {
		for {
			if ok, msg := self.recv(); ok {
				if handle(msg) {
					done <- true
					break
				}
			} else {
				done <- false
				break
			}
		}
		return
	}(complete)
	c := <-complete
	//     self.log.Println("Ending Turn", self.state.Turn)
	return c
}
Exemple #13
0
func (self *AgentProxy) ack_cmd(cmd link.Command) {
	self.send(link.NewMessage(link.Commands["Ack"], cmd))
}
Exemple #14
0
func (self *AgentProxy) Stop() {
	self.send(link.NewMessage(link.Commands["Exit"]))
}
Exemple #15
0
func (self *AgentProxy) nak_cmd(cmd link.Command) {
	self.snd <- *link.NewMessage(link.Commands["Nak"], cmd)
}
Exemple #16
0
func (self *comm) Move(move link.Move) bool {
	c := self.acked_send(link.NewMessage(link.Commands["Move"], move))
	//     fmt.Println("completed move")
	return c
}
Exemple #17
0
func (self *comm) Move(x, y int) bool {
	c, _ := self.acked_send(link.NewMessage(link.Commands["Move"], geo.NewPoint(x, y)))
	return c
}
Exemple #18
0
func (self *comm) complete() bool {
	//     fmt.Println("started complete")
	return self.acked_send(link.NewMessage(link.Commands["Complete"]))
}
Exemple #19
0
func (self *AgentProxy) MigrateTo(address string) {
	self.acked_send(link.NewMessage(link.Commands["Migrate"], agent.NewMigrate([]byte(address))))
}