aboutsummaryrefslogtreecommitdiff
path: root/go/game/state.go
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2023-02-01 18:46:32 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2025-01-27 16:43:45 +0100
commit21a87ffccae9950580a240180fd3703768ef450e (patch)
tree1cb83289216bd1e213d417812b4e09001ca89c09 /go/game/state.go
parent3fd15c9b83fff074b588ff9688f9e49aab38cd32 (diff)
downloadmuhqs-game-21a87ffccae9950580a240180fd3703768ef450e.tar.gz
muhqs-game-21a87ffccae9950580a240180fd3703768ef450e.zip
intermediate commit
Diffstat (limited to 'go/game/state.go')
-rw-r--r--go/game/state.go70
1 files changed, 38 insertions, 32 deletions
diff --git a/go/game/state.go b/go/game/state.go
index 816fc0d0..1d4b644f 100644
--- a/go/game/state.go
+++ b/go/game/state.go
@@ -102,11 +102,10 @@ func (s *State) IsValidPlay(a *PlayAction) error {
}
if a.Card.IsPermanent() {
- pos := a.Targets.(Position)
- spawn := s.Map.TileAt(pos)
- if !slices.Contains(s.AvailableSpawnTiles(a.Source.(*Player), a.Card), spawn) {
+ spawn := a.Targets().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",
- pos.String(), a.Card.Name)
+ spawn.Position.String(), a.Card.Name)
}
}
@@ -118,8 +117,8 @@ func (s *State) IsValidMove(a *MoveAction) error {
return fmt.Errorf("Move actions can only be declared during one's actions phase")
}
- u := a.Source.(*Unit)
- tile := a.Targets.(*Tile)
+ u := a.Source().(*Unit)
+ tile := a.Targets().sel[0].(*Tile)
if !slices.Contains(u.MoveRangeTiles(s.Map), tile) {
return fmt.Errorf("Unit %s at %s can not move to %s",
@@ -139,16 +138,12 @@ func (s *State) IsValidAttack(a *AttackAction) error {
return fmt.Errorf("Attack actions can only be declared during one's actions phase")
}
- unit := a.Source.(Unit)
+ unit := a.Source().(Unit)
if unit.Attack == INVALID_ATTACK() {
return fmt.Errorf("%s can not attack", unit.card.Name)
}
- target := a.Targets.(Permanent)
- if !target.Attackable() {
- return fmt.Errorf("Target %s is not attackable", target.Tile().Position.String())
- }
-
+ target := a.Targets().sel[0].(Permanent)
if !IsPositionInRange(unit.tile.Position, target.Tile().Position, unit.Attack.Range) {
return fmt.Errorf("Attack target on %s is not in attack range %d of %s on %s",
target.Tile().Position.String(), unit.Attack.Range, unit.card.Name,
@@ -158,7 +153,7 @@ func (s *State) IsValidAttack(a *AttackAction) error {
return nil
}
-func (s *State) IsValidBuy(a *BuyAction) error {
+func (s *State) isValidBuy(a *BuyAction) error {
if s.Map.HasStores() {
var storeTiles []*Tile
for pos, store := range s.Map.Stores {
@@ -189,8 +184,12 @@ func (s *State) IsValidBuy(a *BuyAction) error {
return nil
}
-func (s *State) ValidateAction(a Action) error {
- var err error = nil
+func (s *State) ValidateAction(a Action) (err error) {
+ err = a.CheckTargets(s)
+ if err != nil {
+ return
+ }
+
switch a := a.(type) {
case *PlayAction:
err = s.IsValidPlay(a)
@@ -199,9 +198,9 @@ func (s *State) ValidateAction(a Action) error {
case *AttackAction:
err = s.IsValidAttack(a)
case *BuyAction:
- err = s.IsValidBuy(a)
}
- return err
+
+ return
}
func (s *State) PushAction(a Action) {
@@ -214,18 +213,23 @@ func (s *State) PushAction(a Action) {
func (s *State) DeclareAction(a Action) {
var err error = nil
- switch a := a.(type) {
- case *BuyAction:
- if s.ActivePhase != Phases.BuyPhase {
- err = fmt.Errorf("Cards can only be bought during one's buy phase")
- }
- if !a.card.IsBuyable() {
- err = fmt.Errorf("Card %s is not buyable", a.card.Name)
- }
- case *FreeAction:
- default:
- if !s.Stack.IsEmpty() {
- err = fmt.Errorf("Slow action can not be declared while other actions are on the stack")
+
+ err = a.CheckTargets(s)
+
+ if err != nil {
+ switch a := a.(type) {
+ case *BuyAction:
+ if s.ActivePhase != Phases.BuyPhase {
+ err = fmt.Errorf("Cards can only be bought during one's buy phase")
+ }
+ if !a.card.IsBuyable() {
+ err = fmt.Errorf("Card %s is not buyable", a.card.Name)
+ }
+ case *FreeAction:
+ default:
+ if !s.Stack.IsEmpty() {
+ err = fmt.Errorf("Slow action can not be declared while other actions are on the stack")
+ }
}
}
@@ -367,12 +371,14 @@ func (s *State) EnemyUnits(player *Player) []*Unit {
})
}
-func (s *State) ResolvePlay(p *Player, c *Card, targets interface{}) {
+func (s *State) resolvePlay(p *Player, c *Card, targets *Targets) {
switch c.Type {
case CardTypes.Unit:
- s.AddNewUnit(c, targets.(Position), p)
+ tile := targets.sel[0].(*Tile)
+ s.AddNewUnit(c, tile.Position, p)
case CardTypes.Artifact:
- s.AddNewArtifact(c, targets.(Position), p)
+ tile := targets.sel[0].(*Tile)
+ s.AddNewArtifact(c, tile.Position, p)
default:
log.Printf("Resolving unhandled card type: %s\n", c.Type.String())
}