From 91106aec09fc81576f2bd8e322b471b31695cd9d Mon Sep 17 00:00:00 2001 From: Chris Hoefgen <53871533+chris-bsnake@users.noreply.github.com> Date: Mon, 8 Aug 2022 14:57:34 -0700 Subject: [PATCH] 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 --- maps/healing_pools.go | 14 +++++++++++++- maps/healing_pools_test.go | 13 ++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/maps/healing_pools.go b/maps/healing_pools.go index 703dff2..f530630 100644 --- a/maps/healing_pools.go +++ b/maps/healing_pools.go @@ -1,6 +1,8 @@ package maps import ( + "math/rand" + "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 { - 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{ diff --git a/maps/healing_pools_test.go b/maps/healing_pools_test.go index 9a856a1..d807c32 100644 --- a/maps/healing_pools_test.go +++ b/maps/healing_pools_test.go @@ -41,8 +41,9 @@ func TestHealingPoolsMap(t *testing.T) { m := maps.HealingPoolsMap{} state := rules.NewBoardState(tc.boardSize, tc.boardSize) 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) require.Empty(t, state.Hazards) err := m.SetupBoard(state, settings, editor) @@ -53,6 +54,16 @@ func TestHealingPoolsMap(t *testing.T) { for _, p := range state.Hazards { 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)) }) } }