diff options
Diffstat (limited to 'go/game/state.go')
| -rw-r--r-- | go/game/state.go | 84 |
1 files changed, 50 insertions, 34 deletions
diff --git a/go/game/state.go b/go/game/state.go index 604e4530..87e1ee3d 100644 --- a/go/game/state.go +++ b/go/game/state.go @@ -12,20 +12,21 @@ import ( ) type State struct { - Decks []*Deck - Stores []*Store - Exile *PileOfCardsBase - Map *Map - Stack *Stack - Hands []*Hand - DiscardPiles []*DiscardPile - Players []*Player - ActivePlayer *Player - ActivePhase PhaseType - Permanents []Permanent - Units []*Unit - Rand *rand.Rand - EotEffects []effect + Decks []*Deck + Stores []*Store + Exile *PileOfCardsBase + Map *Map + Stack *Stack + Hands []*Hand + DiscardPiles []*DiscardPile + Players []*Player + ActivePlayer *Player + ActivePhase PhaseType + Permanents []Permanent + Units []*Unit + Rand *rand.Rand + EotEffects []effect + outstandingEquipment []*Card } func NewState() *State { @@ -114,6 +115,11 @@ func (s *State) IsValidPlay(a *PlayAction) error { } if a.Card.IsPermanent() { + // Equipments may be played not on a spawn tile but equiped to the next played unit + if a.Card.Type == CardTypes.Equipment && len(a.Target().sel) == 0 { + return nil + } + spawn := a.Target().sel[0].(*Tile) if !slices.Contains(s.AvailableSpawnTiles(a.Source().(*Player), a.Card), spawn) { return fmt.Errorf("Spawn %v is not a valid spawn tile for %s", @@ -354,7 +360,7 @@ func (s *State) broadcastNotification(n PlayerNotification) { } } -func (s *State) AddPermanent(perm Permanent) { +func (s *State) addPermanent(perm Permanent) { s.Permanents = append(s.Permanents, perm) switch p := perm.(type) { case *Unit: @@ -366,26 +372,22 @@ func (s *State) AddPermanent(perm Permanent) { } } -func (s *State) AddNewUnit(card *Card, pos Position, owner *Player) { - s.AddPermanent(NewUnit(card, s.Map.TileAt(pos), owner)) -} - -func (s *State) AddNewArtifact(card *Card, pos Position, owner *Player) { - s.AddPermanent(NewArtifact(card, s.Map.TileAt(pos), owner)) +func (s *State) addNewUnit(card *Card, pos Position, owner *Player) *Unit { + u := NewUnit(card, s.Map.TileAt(pos), owner) + s.addPermanent(u) + return u } -func (s *State) addNewEquipment(card *Card, pos Position, owner *Player) { - s.AddPermanent(newEquipment(card, s.Map.TileAt(pos), owner)) +func (s *State) addNewArtifact(card *Card, pos Position, owner *Player) *Artifact { + a := NewArtifact(card, s.Map.TileAt(pos), owner) + s.addPermanent(a) + return a } -func (s *State) AddNewPermFromPath(cardPath string, pos Position, owner *Player) { - card := NewCard(cardPath) - switch card.Type { - case CardTypes.Unit: - s.AddNewUnit(card, pos, owner) - case CardTypes.Artifact: - s.AddNewArtifact(card, pos, owner) - } +func (s *State) addNewEquipment(card *Card, pos Position, owner *Player) *Equipment { + e := newEquipment(card, s.Map.TileAt(pos), owner) + s.addPermanent(e) + return e } func (s *State) removePermanent(p Permanent) { @@ -470,12 +472,21 @@ func (s *State) resolvePlay(p *Player, c *Card, targets *Targets) { switch c.Type { case CardTypes.Unit: tile := targets.ts[0].sel[0].(*Tile) - s.AddNewUnit(c, tile.Position, p) + u := s.addNewUnit(c, tile.Position, p) + if len(s.outstandingEquipment) > 0 { + for _, e := range s.outstandingEquipment { + u.equip(newEquipment(e, nil, p)) + } + s.outstandingEquipment = nil + } case CardTypes.Artifact: tile := targets.ts[0].sel[0].(*Tile) - s.AddNewArtifact(c, tile.Position, p) + s.addNewArtifact(c, tile.Position, p) case CardTypes.Equipment: - if tile, ok := targets.ts[0].sel[0].(*Tile); ok { + if len(targets.ts[0].sel) == 0 { + s.outstandingEquipment = append(s.outstandingEquipment, c) + } else { + tile, _ := targets.ts[0].sel[0].(*Tile) s.addNewEquipment(c, tile.Position, p) } case CardTypes.Spell: @@ -584,6 +595,11 @@ func (s *State) addEotEffect(e effect) { } func (s *State) endOfTurn() { + // Move all outstanding equipment cards to the discard pile + for _, e := range s.outstandingEquipment { + s.ActivePlayer.DiscardPile.AddCard(e) + } + for _, e := range s.EotEffects { e.end(s) } |
