Exemplo n.º 1
0
// ToWire converts the board to the wire representation with respect to the
// given faction (since the wire factions are us vs. them).
func (b *Board) ToWire(out botapi.Board, faction int) error {
	out.SetWidth(uint16(b.Size.X))
	out.SetHeight(uint16(b.Size.Y))
	out.SetRound(int32(b.Round))

	robots, err := botapi.NewRobot_List(out.Segment(), int32(len(b.Locs)))
	if err != nil {
		return err
	}
	if err = out.SetRobots(robots); err != nil {
		return err
	}

	n := 0
	for loc, r := range b.Locs {
		outr := robots.At(n)
		outr.SetId(uint32(r.ID))
		outr.SetX(uint16(loc.X))
		outr.SetY(uint16(loc.Y))
		outr.SetHealth(int16(r.Health))
		if r.Faction == faction {
			outr.SetFaction(botapi.Faction_mine)
		} else {
			outr.SetFaction(botapi.Faction_opponent)
		}
		n++
	}
	return nil
}
Exemplo n.º 2
0
// boardFromWire converts the wire representation to the board
func boardFromWire(wire botapi.Board) (*Board, error) {
	b := EmptyBoard(BoardConfig{
		Size: Loc{X: int(wire.Width()), Y: int(wire.Height())},
	})
	b.Round = int(wire.Round())

	bots, err := wire.Robots()
	if err != nil {
		return b, err
	}

	for i := 0; i < bots.Len(); i++ {
		bot := bots.At(i)
		loc := Loc{
			X: int(bot.X()),
			Y: int(bot.Y()),
		}
		b.Locs[loc] = robotFromWire(bot)
	}

	return b, nil
}
Exemplo n.º 3
0
func convertBoard(wire botapi.Board) (b *Board, playerBots []*Robot, err error) {
	w, h := int(wire.Width()), int(wire.Height())
	cells := make([]*Robot, w*h)
	cols := make([][]*Robot, w)
	for x := range cols {
		cols[x] = cells[x*h : (x+1)*h]
	}
	robots, err := wire.Robots()
	if err != nil {
		return nil, nil, err
	}
	playerBots = make([]*Robot, 0, robots.Len())
	for i, n := 0, robots.Len(); i < n; i++ {
		r := robots.At(i)
		l := Loc{X: int(r.X()), Y: int(r.Y())}
		rr := &Robot{
			ID:     r.Id(),
			Loc:    l,
			Health: int(r.Health()),
		}
		switch r.Faction() {
		case botapi.Faction_mine:
			rr.Faction = MyFaction
			playerBots = append(playerBots, rr)
		case botapi.Faction_opponent:
			fallthrough
		default:
			rr.Faction = OpponentFaction
		}
		cols[l.X][l.Y] = rr
	}
	return &Board{
		Size:  Loc{w, h},
		Round: int(wire.Round()),
		Cells: cols,
	}, playerBots, nil
}