DEV-1761: New rules API (#118)
* DEV-1761: Clean up Ruleset interface (#115) * remove legacy ruleset types and simplify ruleset interface * remove unnecessary settings argument from Ruleset interface * decouple rules.Settings from client API and store settings as strings * DEV 1761: Add new BoardState and Point fields (#117) * add Point.TTL, Point.Value, GameState and PointState to BoardState * allow maps to access BoardState.GameState,PointState * add PreUpdateBoard and refactor snail_mode with it * fix bug where an extra turn was printed to the console * fix formatting * fix lint errors Co-authored-by: JonathanArns <jonathan.arns@googlemail.com>
This commit is contained in:
parent
639362ef46
commit
82e1999126
50 changed files with 1349 additions and 1610 deletions
41
royale.go
41
royale.go
|
|
@ -14,26 +14,6 @@ var royaleRulesetStages = []string{
|
|||
StageSpawnHazardsShrinkMap,
|
||||
}
|
||||
|
||||
type RoyaleRuleset struct {
|
||||
StandardRuleset
|
||||
|
||||
ShrinkEveryNTurns int
|
||||
}
|
||||
|
||||
func (r *RoyaleRuleset) Name() string { return GameTypeRoyale }
|
||||
|
||||
func (r RoyaleRuleset) Execute(bs *BoardState, s Settings, sm []SnakeMove) (bool, *BoardState, error) {
|
||||
return NewPipeline(royaleRulesetStages...).Execute(bs, s, sm)
|
||||
}
|
||||
|
||||
func (r *RoyaleRuleset) CreateNextBoardState(prevState *BoardState, moves []SnakeMove) (*BoardState, error) {
|
||||
if r.StandardRuleset.HazardDamagePerTurn < 1 {
|
||||
return nil, errors.New("royale damage per turn must be greater than zero")
|
||||
}
|
||||
_, nextState, err := r.Execute(prevState, r.Settings(), moves)
|
||||
return nextState, err
|
||||
}
|
||||
|
||||
func PopulateHazardsRoyale(b *BoardState, settings Settings, moves []SnakeMove) (bool, error) {
|
||||
if IsInitialization(b, settings, moves) {
|
||||
return false, nil
|
||||
|
|
@ -43,17 +23,18 @@ func PopulateHazardsRoyale(b *BoardState, settings Settings, moves []SnakeMove)
|
|||
// Royale uses the current turn to generate hazards, not the previous turn that's in the board state
|
||||
turn := b.Turn + 1
|
||||
|
||||
if settings.RoyaleSettings.ShrinkEveryNTurns < 1 {
|
||||
shrinkEveryNTurns := settings.Int(ParamShrinkEveryNTurns, 0)
|
||||
if shrinkEveryNTurns < 1 {
|
||||
return false, errors.New("royale game can't shrink more frequently than every turn")
|
||||
}
|
||||
|
||||
if turn < settings.RoyaleSettings.ShrinkEveryNTurns {
|
||||
if turn < shrinkEveryNTurns {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
randGenerator := settings.GetRand(0)
|
||||
|
||||
numShrinks := turn / settings.RoyaleSettings.ShrinkEveryNTurns
|
||||
numShrinks := turn / shrinkEveryNTurns
|
||||
minX, maxX := 0, b.Width-1
|
||||
minY, maxY := 0, b.Height-1
|
||||
for i := 0; i < numShrinks; i++ {
|
||||
|
|
@ -72,22 +53,10 @@ func PopulateHazardsRoyale(b *BoardState, settings Settings, moves []SnakeMove)
|
|||
for x := 0; x < b.Width; x++ {
|
||||
for y := 0; y < b.Height; y++ {
|
||||
if x < minX || x > maxX || y < minY || y > maxY {
|
||||
b.Hazards = append(b.Hazards, Point{x, y})
|
||||
b.Hazards = append(b.Hazards, Point{X: x, Y: y})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (r *RoyaleRuleset) IsGameOver(b *BoardState) (bool, error) {
|
||||
return GameOverStandard(b, r.Settings(), nil)
|
||||
}
|
||||
|
||||
func (r RoyaleRuleset) Settings() Settings {
|
||||
s := r.StandardRuleset.Settings()
|
||||
s.RoyaleSettings = RoyaleSettings{
|
||||
ShrinkEveryNTurns: r.ShrinkEveryNTurns,
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue