DEV-765 pipeline refactor (#64)
Refactor rulesets into smaller composable operations In order to mix up the functionality from different rulesets like Solo, Royale, etc. the code in these classes needs to be broken up into small functions that can be composed in a pipeline to make a custom game mode.
This commit is contained in:
parent
5e629e9e93
commit
397d925110
25 changed files with 1475 additions and 222 deletions
42
royale.go
42
royale.go
|
|
@ -13,7 +13,7 @@ type RoyaleRuleset struct {
|
|||
ShrinkEveryNTurns int32
|
||||
}
|
||||
|
||||
func (r *RoyaleRuleset) Name() string { return "royale" }
|
||||
func (r *RoyaleRuleset) Name() string { return GameTypeRoyale }
|
||||
|
||||
func (r *RoyaleRuleset) CreateNextBoardState(prevState *BoardState, moves []SnakeMove) (*BoardState, error) {
|
||||
if r.StandardRuleset.HazardDamagePerTurn < 1 {
|
||||
|
|
@ -26,7 +26,7 @@ func (r *RoyaleRuleset) CreateNextBoardState(prevState *BoardState, moves []Snak
|
|||
}
|
||||
|
||||
// Royale's only job is now to populate the hazards for next turn - StandardRuleset takes care of applying hazard damage.
|
||||
err = r.populateHazards(nextBoardState, prevState.Turn+1)
|
||||
err = r.populateHazards(nextBoardState)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -34,20 +34,28 @@ func (r *RoyaleRuleset) CreateNextBoardState(prevState *BoardState, moves []Snak
|
|||
return nextBoardState, nil
|
||||
}
|
||||
|
||||
func (r *RoyaleRuleset) populateHazards(b *BoardState, turn int32) error {
|
||||
func (r *RoyaleRuleset) populateHazards(b *BoardState) error {
|
||||
_, err := r.callStageFunc(PopulateHazardsRoyale, b, []SnakeMove{})
|
||||
return err
|
||||
}
|
||||
|
||||
func PopulateHazardsRoyale(b *BoardState, settings Settings, moves []SnakeMove) (bool, error) {
|
||||
b.Hazards = []Point{}
|
||||
|
||||
if r.ShrinkEveryNTurns < 1 {
|
||||
return errors.New("royale game can't shrink more frequently than every turn")
|
||||
// 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 {
|
||||
return false, errors.New("royale game can't shrink more frequently than every turn")
|
||||
}
|
||||
|
||||
if turn < r.ShrinkEveryNTurns {
|
||||
return nil
|
||||
if turn < settings.RoyaleSettings.ShrinkEveryNTurns {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
randGenerator := rand.New(rand.NewSource(r.Seed))
|
||||
randGenerator := rand.New(rand.NewSource(settings.RoyaleSettings.seed))
|
||||
|
||||
numShrinks := turn / r.ShrinkEveryNTurns
|
||||
numShrinks := turn / settings.RoyaleSettings.ShrinkEveryNTurns
|
||||
minX, maxX := int32(0), b.Width-1
|
||||
minY, maxY := int32(0), b.Height-1
|
||||
for i := int32(0); i < numShrinks; i++ {
|
||||
|
|
@ -71,5 +79,19 @@ func (r *RoyaleRuleset) populateHazards(b *BoardState, turn int32) error {
|
|||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (r RoyaleRuleset) Settings() Settings {
|
||||
s := r.StandardRuleset.Settings()
|
||||
s.RoyaleSettings = RoyaleSettings{
|
||||
seed: r.Seed,
|
||||
ShrinkEveryNTurns: r.ShrinkEveryNTurns,
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Adaptor for integrating stages into RoyaleRuleset
|
||||
func (r *RoyaleRuleset) callStageFunc(stage StageFunc, boardState *BoardState, moves []SnakeMove) (bool, error) {
|
||||
return stage(boardState, r.Settings(), moves)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue