From d76655a521807f0aa6fae52de0a231a8531c660b Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Tue, 21 Feb 2023 09:03:32 +0100 Subject: validate equip and switch actions --- go/game/state.go | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/go/game/state.go b/go/game/state.go index d98595b4..347b5406 100644 --- a/go/game/state.go +++ b/go/game/state.go @@ -107,7 +107,7 @@ func (s *State) stateBasedActions() { func (s *State) IsValidPlay(a *PlayAction) error { if s.ActivePhase != Phases.ActionPhase { - log.Panicf("Play actions can only be declared during one's actions phase") + return fmt.Errorf("Play actions can only be declared during one's actions phase") } if a.Card.IsPermanent() { @@ -198,6 +198,40 @@ func (s *State) isValidBuy(a *BuyAction) error { return nil } +func (s *State) IsValidEquip(a *EquipAction) error { + if s.ActivePhase != Phases.ActionPhase { + return fmt.Errorf("Play actions can only be declared during one's actions phase") + } + + uTile := TileOrContainingPermTile(a.Source().(*Unit)) + eTile := TileOrContainingPermTile(a.equipment) + if !IsPositionInRange(uTile, eTile, 1) { + return fmt.Errorf("Can only equip adjacent equipments") + } + + return nil +} + +func (s *State) IsValidArtifactSwitch(a *EquipAction) error { + if s.ActivePhase != Phases.ActionPhase { + return fmt.Errorf("Play actions can only be declared during one's actions phase") + } + + u := a.Source().(*Unit) + uTile := TileOrContainingPermTile(u) + a := a.Artifact + aTile := TileOrContainingPermTile(a) + if !IsPositionInRange(uTile, aTile, 1) { + return fmt.Errorf("Can only switch position with adjacent equipments") + } + + if a.Controller() != u.Controller() && a.Solid > 0 { + return fmt.Errorf("Can only switch with solid artifacts you control") + } + + return nil +} + func (s *State) ValidateAction(a Action) (err error) { err = a.CheckTargets(s) if err != nil { @@ -211,6 +245,10 @@ func (s *State) ValidateAction(a Action) (err error) { err = s.IsValidMove(a) case *AttackAction: err = s.IsValidAttack(a) + case *EquipAction: + err = s.IsValidEquip(a) + case *ArtifactSwitchAction: + err = s.IsValidArtifactSwitch(a) case *BuyAction: } -- cgit v1.2.3