示例#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")
}
示例#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
}
示例#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))
}
示例#4
0
func (self *comm) complete() bool {
	r, _ := self.acked_send(link.NewMessage(link.Commands["Complete"]))
	return r
}
示例#5
0
func (self *comm) ack_migrate() {
	self.send(link.NewMessage(link.Commands["Ack"], link.Commands["Migrate"]))
}
示例#6
0
func (self *comm) Collect() bool {
	r, _ := self.acked_send(link.NewMessage(link.Commands["Collect"]))
	return r
}
示例#7
0
func (self *comm) PrevResult() bool {
	self.send(link.NewMessage(link.Commands["PrevResult"]))
	self.recv()
	return false
}
示例#8
0
func (self *AgentProxy) start_turn() bool {
	return self.acked_send(link.NewMessage(link.Commands["Start"], self.state.Turn))
}
示例#9
0
func (self *comm) ack_start() {
	self.send(link.NewMessage(link.Commands["Ack"], link.Commands["Start"]))
}
示例#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
}
示例#11
0
func (self *comm) Look() link.Vision {
	self.send(link.NewMessage(link.Commands["Look"]))
	self.recv()
	return nil
}
示例#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
}
示例#13
0
func (self *AgentProxy) ack_cmd(cmd link.Command) {
	self.send(link.NewMessage(link.Commands["Ack"], cmd))
}
示例#14
0
func (self *AgentProxy) Stop() {
	self.send(link.NewMessage(link.Commands["Exit"]))
}
示例#15
0
func (self *AgentProxy) nak_cmd(cmd link.Command) {
	self.snd <- *link.NewMessage(link.Commands["Nak"], cmd)
}
示例#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
}
示例#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
}
示例#18
0
func (self *comm) complete() bool {
	//     fmt.Println("started complete")
	return self.acked_send(link.NewMessage(link.Commands["Complete"]))
}
示例#19
0
func (self *AgentProxy) MigrateTo(address string) {
	self.acked_send(link.NewMessage(link.Commands["Migrate"], agent.NewMigrate([]byte(address))))
}