Add decay logic to healing pools (#97)

* adding logic to remove healing pools periodically to prevent extended length games

* Fix for the case where ShrinkEveryNTurns is not set
This commit is contained in:
Chris Hoefgen 2022-08-08 14:57:34 -07:00 committed by GitHub
parent 215a0ea998
commit 91106aec09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 2 deletions

View file

@ -1,6 +1,8 @@
package maps package maps
import ( import (
"math/rand"
"github.com/BattlesnakeOfficial/rules" "github.com/BattlesnakeOfficial/rules"
) )
@ -48,7 +50,17 @@ func (m HealingPoolsMap) SetupBoard(initialBoardState *rules.BoardState, setting
} }
func (m HealingPoolsMap) UpdateBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error { func (m HealingPoolsMap) UpdateBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error {
return StandardMap{}.UpdateBoard(lastBoardState, settings, editor) if err := (StandardMap{}).UpdateBoard(lastBoardState, settings, editor); err != nil {
return err
}
if lastBoardState.Turn > 0 && settings.RoyaleSettings.ShrinkEveryNTurns > 0 && len(lastBoardState.Hazards) > 0 && lastBoardState.Turn%settings.RoyaleSettings.ShrinkEveryNTurns == 0 {
// Attempt to remove a healing pool every ShrinkEveryNTurns until there are none remaining
i := rand.Intn(len(lastBoardState.Hazards))
editor.RemoveHazard(lastBoardState.Hazards[i])
}
return nil
} }
var poolLocationOptions = map[rules.Point][][]rules.Point{ var poolLocationOptions = map[rules.Point][][]rules.Point{

View file

@ -41,8 +41,9 @@ func TestHealingPoolsMap(t *testing.T) {
m := maps.HealingPoolsMap{} m := maps.HealingPoolsMap{}
state := rules.NewBoardState(tc.boardSize, tc.boardSize) state := rules.NewBoardState(tc.boardSize, tc.boardSize)
settings := rules.Settings{} settings := rules.Settings{}
settings.RoyaleSettings.ShrinkEveryNTurns = 10
// ensure the ring of hazards is added to the board at setup // ensure the hazards are added to the board at setup
editor := maps.NewBoardStateEditor(state) editor := maps.NewBoardStateEditor(state)
require.Empty(t, state.Hazards) require.Empty(t, state.Hazards)
err := m.SetupBoard(state, settings, editor) err := m.SetupBoard(state, settings, editor)
@ -53,6 +54,16 @@ func TestHealingPoolsMap(t *testing.T) {
for _, p := range state.Hazards { for _, p := range state.Hazards {
require.Contains(t, tc.allowableHazards, p) require.Contains(t, tc.allowableHazards, p)
} }
// ensure the hazards are removed
totalTurns := settings.RoyaleSettings.ShrinkEveryNTurns*tc.expectedHazards + 1
for i := 0; i < totalTurns; i++ {
state.Turn = i
err = m.UpdateBoard(state, settings, editor)
require.NoError(t, err)
}
require.Equal(t, 0, len(state.Hazards))
}) })
} }
} }