Make repair require rover parts

This commit is contained in:
Marc Di Luzio 2020-07-24 19:08:39 +01:00
parent ce6e10afbb
commit 2f1ccdfdb9
6 changed files with 230 additions and 282 deletions

View file

@ -99,10 +99,10 @@ func TestCommand_Repair(t *testing.T) {
assert.Equal(t, info.MaximumIntegrity-1, info.Integrity)
// Stash a repair object
w.Atlas.SetObject(info.Pos, Object{Type: roveapi.Object_RockSmall})
w.Atlas.SetObject(info.Pos, Object{Type: roveapi.Object_RoverParts})
obj, err := w.RoverStash(name)
assert.NoError(t, err)
assert.Equal(t, roveapi.Object_RockSmall, obj)
assert.Equal(t, roveapi.Object_RoverParts, obj)
// Enqueue the repair and tick
err = w.Enqueue(name, &roveapi.Command{Command: roveapi.CommandType_repair})

View file

@ -33,6 +33,7 @@ func (o *Object) IsBlocking() bool {
func (o *Object) IsStashable() bool {
var stashable = [...]roveapi.Object{
roveapi.Object_RockSmall,
roveapi.Object_RoverParts,
}
for _, t := range stashable {

View file

@ -352,11 +352,24 @@ func (w *World) RoverRepair(rover string) (int, error) {
return 0, fmt.Errorf("no rover matching id")
}
// Consume an inventory item to repair if possible
if len(r.Inventory) > 0 && r.Integrity < r.MaximumIntegrity {
r.Inventory = r.Inventory[:len(r.Inventory)-1]
r.Integrity = r.Integrity + 1
r.AddLogEntryf("repaired self to %d", r.Integrity)
// Can't repair past max
if r.Integrity >= r.MaximumIntegrity {
return r.Integrity, nil
}
// Find rover parts in inventory
for i, o := range r.Inventory {
if o.Type == roveapi.Object_RoverParts {
// Copy-erase from slice
r.Inventory[i] = r.Inventory[len(r.Inventory)-1]
r.Inventory = r.Inventory[:len(r.Inventory)-1]
// Repair
r.Integrity = r.Integrity + 1
r.AddLogEntryf("repaired self to %d", r.Integrity)
break
}
}
return r.Integrity, nil

View file

@ -248,10 +248,10 @@ func TestWorld_RoverRepair(t *testing.T) {
assert.NoError(t, err, "couldn't get rover info")
// Pick up something to repair with
world.Atlas.SetObject(pos, Object{Type: roveapi.Object_RockSmall})
world.Atlas.SetObject(pos, Object{Type: roveapi.Object_RoverParts})
o, err := world.RoverStash(a)
assert.NoError(t, err, "Failed to stash")
assert.Equal(t, roveapi.Object_RockSmall, o, "Failed to get correct object")
assert.Equal(t, roveapi.Object_RoverParts, o, "Failed to get correct object")
world.Atlas.SetObject(maths.Vector{X: 0.0, Y: 1.0}, Object{Type: roveapi.Object_RockLarge})
@ -273,10 +273,10 @@ func TestWorld_RoverRepair(t *testing.T) {
assert.Contains(t, newinfo.Logs[len(newinfo.Logs)-1].Text, "repair", "Rover logs should contain the repair")
// Check again that it can't repair past the max
world.Atlas.SetObject(pos, Object{Type: roveapi.Object_RockSmall})
world.Atlas.SetObject(pos, Object{Type: roveapi.Object_RoverParts})
o, err = world.RoverStash(a)
assert.NoError(t, err, "Failed to stash")
assert.Equal(t, roveapi.Object_RockSmall, o, "Failed to get correct object")
assert.Equal(t, roveapi.Object_RoverParts, o, "Failed to get correct object")
err = world.ExecuteCommand(&roveapi.Command{Command: roveapi.CommandType_repair}, a)
assert.NoError(t, err, "Failed to repair rover")