2020-07-21 14:58:56 -07:00
|
|
|
package rules
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SquadRuleset struct {
|
|
|
|
|
StandardRuleset
|
|
|
|
|
|
|
|
|
|
SquadMap map[string]string
|
|
|
|
|
|
|
|
|
|
// These are intentionally designed so that they default to a standard game.
|
|
|
|
|
AllowBodyCollisions bool
|
|
|
|
|
SharedElimination bool
|
|
|
|
|
SharedHealth bool
|
|
|
|
|
SharedLength bool
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-16 16:58:05 -07:00
|
|
|
func (r *SquadRuleset) Name() string { return GameTypeSquad }
|
2021-07-02 20:00:19 -07:00
|
|
|
|
2020-07-21 14:58:56 -07:00
|
|
|
func (r *SquadRuleset) CreateNextBoardState(prevState *BoardState, moves []SnakeMove) (*BoardState, error) {
|
|
|
|
|
nextBoardState, err := r.StandardRuleset.CreateNextBoardState(prevState, moves)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = r.resurrectSquadBodyCollisions(nextBoardState)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = r.shareSquadAttributes(nextBoardState)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nextBoardState, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-16 16:58:05 -07:00
|
|
|
func areSnakesOnSameSquad(squadMap map[string]string, snake *Snake, other *Snake) bool {
|
|
|
|
|
return areSnakeIDsOnSameSquad(squadMap, snake.ID, other.ID)
|
2020-07-21 14:58:56 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-16 16:58:05 -07:00
|
|
|
func areSnakeIDsOnSameSquad(squadMap map[string]string, snakeID string, otherID string) bool {
|
|
|
|
|
return squadMap[snakeID] == squadMap[otherID]
|
2020-07-21 14:58:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *SquadRuleset) resurrectSquadBodyCollisions(b *BoardState) error {
|
2022-03-16 16:58:05 -07:00
|
|
|
_, err := r.callStageFunc(ResurrectSnakesSquad, b, []SnakeMove{})
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ResurrectSnakesSquad(b *BoardState, settings Settings, moves []SnakeMove) (bool, error) {
|
|
|
|
|
if !settings.SquadSettings.AllowBodyCollisions {
|
|
|
|
|
return false, nil
|
2020-07-21 14:58:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := 0; i < len(b.Snakes); i++ {
|
|
|
|
|
snake := &b.Snakes[i]
|
|
|
|
|
if snake.EliminatedCause == EliminatedByCollision {
|
|
|
|
|
if snake.EliminatedBy == "" {
|
2022-03-16 16:58:05 -07:00
|
|
|
return false, errors.New("snake eliminated by collision and eliminatedby is not set")
|
2020-07-21 14:58:56 -07:00
|
|
|
}
|
2022-03-16 16:58:05 -07:00
|
|
|
if snake.ID != snake.EliminatedBy && areSnakeIDsOnSameSquad(settings.SquadSettings.squadMap, snake.ID, snake.EliminatedBy) {
|
2020-07-21 14:58:56 -07:00
|
|
|
snake.EliminatedCause = NotEliminated
|
|
|
|
|
snake.EliminatedBy = ""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-16 16:58:05 -07:00
|
|
|
return false, nil
|
2020-07-21 14:58:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *SquadRuleset) shareSquadAttributes(b *BoardState) error {
|
2022-03-16 16:58:05 -07:00
|
|
|
_, err := r.callStageFunc(ShareAttributesSquad, b, []SnakeMove{})
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ShareAttributesSquad(b *BoardState, settings Settings, moves []SnakeMove) (bool, error) {
|
|
|
|
|
squadSettings := settings.SquadSettings
|
|
|
|
|
|
|
|
|
|
if !(squadSettings.SharedElimination || squadSettings.SharedLength || squadSettings.SharedHealth) {
|
|
|
|
|
return false, nil
|
2020-07-21 14:58:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := 0; i < len(b.Snakes); i++ {
|
|
|
|
|
snake := &b.Snakes[i]
|
|
|
|
|
if snake.EliminatedCause != NotEliminated {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for j := 0; j < len(b.Snakes); j++ {
|
|
|
|
|
other := &b.Snakes[j]
|
2022-03-16 16:58:05 -07:00
|
|
|
if areSnakesOnSameSquad(squadSettings.squadMap, snake, other) {
|
|
|
|
|
if squadSettings.SharedHealth {
|
2020-07-21 14:58:56 -07:00
|
|
|
if snake.Health < other.Health {
|
|
|
|
|
snake.Health = other.Health
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-16 16:58:05 -07:00
|
|
|
if squadSettings.SharedLength {
|
2020-07-21 14:58:56 -07:00
|
|
|
if len(snake.Body) == 0 || len(other.Body) == 0 {
|
2022-03-16 16:58:05 -07:00
|
|
|
return false, errors.New("found snake of zero length")
|
2020-07-21 14:58:56 -07:00
|
|
|
}
|
|
|
|
|
for len(snake.Body) < len(other.Body) {
|
2022-03-16 16:58:05 -07:00
|
|
|
growSnake(snake)
|
2020-07-21 14:58:56 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-03-16 16:58:05 -07:00
|
|
|
if squadSettings.SharedElimination {
|
2020-07-21 14:58:56 -07:00
|
|
|
if snake.EliminatedCause == NotEliminated && other.EliminatedCause != NotEliminated {
|
|
|
|
|
snake.EliminatedCause = EliminatedBySquad
|
|
|
|
|
// We intentionally do not set snake.EliminatedBy because there might be multiple culprits.
|
|
|
|
|
snake.EliminatedBy = ""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-16 16:58:05 -07:00
|
|
|
return false, nil
|
2020-07-21 14:58:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *SquadRuleset) IsGameOver(b *BoardState) (bool, error) {
|
2022-03-16 16:58:05 -07:00
|
|
|
return r.callStageFunc(GameOverSquad, b, []SnakeMove{})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GameOverSquad(b *BoardState, settings Settings, moves []SnakeMove) (bool, error) {
|
2020-07-21 14:58:56 -07:00
|
|
|
snakesRemaining := []*Snake{}
|
|
|
|
|
for i := 0; i < len(b.Snakes); i++ {
|
|
|
|
|
if b.Snakes[i].EliminatedCause == NotEliminated {
|
|
|
|
|
snakesRemaining = append(snakesRemaining, &b.Snakes[i])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := 0; i < len(snakesRemaining); i++ {
|
2022-03-16 16:58:05 -07:00
|
|
|
if !areSnakesOnSameSquad(settings.SquadSettings.squadMap, snakesRemaining[i], snakesRemaining[0]) {
|
2020-07-21 14:58:56 -07:00
|
|
|
// There are multiple squads remaining
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// no snakes or single squad remaining
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
2022-03-16 16:58:05 -07:00
|
|
|
|
|
|
|
|
func (r SquadRuleset) Settings() Settings {
|
|
|
|
|
s := r.StandardRuleset.Settings()
|
|
|
|
|
s.SquadSettings = SquadSettings{
|
|
|
|
|
squadMap: r.SquadMap,
|
|
|
|
|
AllowBodyCollisions: r.AllowBodyCollisions,
|
|
|
|
|
SharedElimination: r.SharedElimination,
|
|
|
|
|
SharedHealth: r.SharedHealth,
|
|
|
|
|
SharedLength: r.SharedLength,
|
|
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Adaptor for integrating stages into SquadRuleset
|
|
|
|
|
func (r *SquadRuleset) callStageFunc(stage StageFunc, boardState *BoardState, moves []SnakeMove) (bool, error) {
|
|
|
|
|
return stage(boardState, r.Settings(), moves)
|
|
|
|
|
}
|