Update the rover list to a list of pointers

This commit is contained in:
Marc Di Luzio 2020-07-22 19:25:32 +01:00
parent 447dbe3582
commit 9e42764398
2 changed files with 5 additions and 14 deletions

View file

@ -62,8 +62,8 @@ type Rover struct {
} }
// DefaultRover returns a default rover object with default settings // DefaultRover returns a default rover object with default settings
func DefaultRover() Rover { func DefaultRover() *Rover {
return Rover{ return &Rover{
Range: 4, Range: 4,
Integrity: 10, Integrity: 10,
MaximumIntegrity: 10, MaximumIntegrity: 10,

View file

@ -22,7 +22,7 @@ type World struct {
CurrentTicks int `json:"current-ticks"` CurrentTicks int `json:"current-ticks"`
// Rovers is a id->data map of all the rovers in the game // Rovers is a id->data map of all the rovers in the game
Rovers map[string]Rover `json:"rovers"` Rovers map[string]*Rover `json:"rovers"`
// Atlas represends the world map of chunks and tiles // Atlas represends the world map of chunks and tiles
Atlas Atlas `json:"atlas"` Atlas Atlas `json:"atlas"`
@ -41,7 +41,7 @@ type World struct {
// NewWorld creates a new world object // NewWorld creates a new world object
func NewWorld(chunkSize int) *World { func NewWorld(chunkSize int) *World {
return &World{ return &World{
Rovers: make(map[string]Rover), Rovers: make(map[string]*Rover),
CommandQueue: make(map[string]CommandStream), CommandQueue: make(map[string]CommandStream),
CommandIncoming: make(map[string]CommandStream), CommandIncoming: make(map[string]CommandStream),
Atlas: NewChunkAtlas(chunkSize), Atlas: NewChunkAtlas(chunkSize),
@ -94,7 +94,7 @@ func (w *World) GetRover(rover string) (Rover, error) {
if !ok { if !ok {
return Rover{}, fmt.Errorf("Failed to find rover with name: %s", rover) return Rover{}, fmt.Errorf("Failed to find rover with name: %s", rover)
} }
return i, nil return *i, nil
} }
// RoverRecharge charges up a rover // RoverRecharge charges up a rover
@ -117,7 +117,6 @@ func (w *World) RoverRecharge(rover string) (int, error) {
i.Charge++ i.Charge++
i.AddLogEntryf("recharged to %d", i.Charge) i.AddLogEntryf("recharged to %d", i.Charge)
} }
w.Rovers[rover] = i
return i.Charge, nil return i.Charge, nil
} }
@ -152,7 +151,6 @@ func (w *World) RoverBroadcast(rover string, message []byte) (err error) {
} }
i.AddLogEntryf("broadcasted %s", string(message)) i.AddLogEntryf("broadcasted %s", string(message))
w.Rovers[rover] = i
return return
} }
@ -193,7 +191,6 @@ func (w *World) SetRoverPosition(rover string, pos maths.Vector) error {
} }
i.Pos = pos i.Pos = pos
w.Rovers[rover] = i
return nil return nil
} }
@ -230,7 +227,6 @@ func (w *World) WarpRover(rover string, pos maths.Vector) error {
} }
i.Pos = pos i.Pos = pos
w.Rovers[rover] = i
return nil return nil
} }
@ -259,7 +255,6 @@ func (w *World) MoveRover(rover string, b roveapi.Bearing) (maths.Vector, error)
i.AddLogEntryf("moved %s to %+v", b.String(), newPos) i.AddLogEntryf("moved %s to %+v", b.String(), newPos)
// Perform the move // Perform the move
i.Pos = newPos i.Pos = newPos
w.Rovers[rover] = i
} else { } else {
// If it is a blocking tile, reduce the rover integrity // If it is a blocking tile, reduce the rover integrity
i.AddLogEntryf("tried to move %s to %+v", b.String(), newPos) i.AddLogEntryf("tried to move %s to %+v", b.String(), newPos)
@ -267,8 +262,6 @@ func (w *World) MoveRover(rover string, b roveapi.Bearing) (maths.Vector, error)
i.AddLogEntryf("had a collision, new integrity %d", i.Integrity) i.AddLogEntryf("had a collision, new integrity %d", i.Integrity)
if i.Integrity == 0 { if i.Integrity == 0 {
// TODO: The rover needs to be left dormant with the player // TODO: The rover needs to be left dormant with the player
} else {
w.Rovers[rover] = i
} }
} }
@ -303,7 +296,6 @@ func (w *World) RoverStash(rover string) (roveapi.Object, error) {
r.AddLogEntryf("stashed %c", obj.Type) r.AddLogEntryf("stashed %c", obj.Type)
r.Inventory = append(r.Inventory, obj) r.Inventory = append(r.Inventory, obj)
w.Rovers[rover] = r
w.Atlas.SetObject(r.Pos, Object{Type: roveapi.Object_ObjectUnknown}) w.Atlas.SetObject(r.Pos, Object{Type: roveapi.Object_ObjectUnknown})
return obj.Type, nil return obj.Type, nil
} }
@ -343,7 +335,6 @@ func (w *World) RoverTurn(rover string, bearing roveapi.Bearing) (roveapi.Bearin
// Set the new bearing // Set the new bearing
r.Bearing = bearing r.Bearing = bearing
w.Rovers[rover] = r
return r.Bearing, nil return r.Bearing, nil
} }