aboutsummaryrefslogtreecommitdiff
path: root/go/game
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2023-02-17 00:29:26 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2025-01-27 16:43:50 +0100
commit65aa5f53d54b4590b98a559fccfb1cc7c7644187 (patch)
tree8b258625df3211a93efba1db889bd371e2f1d0ae /go/game
parent1c4651a7bba1e2b23a182152dc96902e9fb6868a (diff)
downloadmuhqs-game-65aa5f53d54b4590b98a559fccfb1cc7c7644187.tar.gz
muhqs-game-65aa5f53d54b4590b98a559fccfb1cc7c7644187.zip
implement unit and artifact switch action
Simplify the Choice Widget and make it a TextBox variant. Drive-by fixes: * ui.Collection: prioritize newer widgets during active widget selection * game.FullAction: fix incorrect cost function. Full actions cost all available actions. Also fix the cost check
Diffstat (limited to 'go/game')
-rw-r--r--go/game/action.go82
-rw-r--r--go/game/card.go9
-rw-r--r--go/game/permanent.go9
-rw-r--r--go/game/state.go11
-rw-r--r--go/game/unit.go13
5 files changed, 96 insertions, 28 deletions
diff --git a/go/game/action.go b/go/game/action.go
index 77e9743f..1e25cd67 100644
--- a/go/game/action.go
+++ b/go/game/action.go
@@ -161,13 +161,7 @@ func NewMoveAction(u *Unit) *MoveAction {
u.move(s, tile)
}
- a.costFunc = func(*State) bool {
- if u.AvailMoveActions < 1 {
- return false
- }
- u.AvailMoveActions--
- return true
- }
+ a.costFunc = genMoveActionCost(u)
return a
}
@@ -182,6 +176,36 @@ func (a *MoveAction) String() string {
return fmt.Sprintf("%s -> %v", u.FmtWController(), t.Position)
}
+func genBaseActionCost(u *Unit, attack, move int) ActionCostFunc {
+ return func(*State) bool {
+ if u.AvailAttackActions < attack || u.AvailMoveActions < move {
+ return false
+ }
+ u.AvailAttackActions -= attack
+ u.AvailMoveActions -= move
+ return true
+ }
+}
+
+func genAttackActionCost(u *Unit) ActionCostFunc {
+ return genBaseActionCost(u, 1, 0)
+}
+
+func genMoveActionCost(u *Unit) ActionCostFunc {
+ return genBaseActionCost(u, 0, 1)
+}
+
+func genFullActionCost(u *Unit) ActionCostFunc {
+ return func(*State) bool {
+ if u.AvailAttackActions < 1 || u.AvailMoveActions < 1 {
+ return false
+ }
+ u.AvailAttackActions = 0
+ u.AvailMoveActions = 0
+ return true
+ }
+}
+
type AttackAction struct {
ActionBase
}
@@ -200,13 +224,7 @@ func NewAttackAction(u *Unit) *AttackAction {
s.Fight(u, a.Target().sel[0].(Permanent))
}
- a.costFunc = func(*State) bool {
- if u.AvailAttackActions < 1 {
- return false
- }
- u.AvailAttackActions--
- return true
- }
+ a.costFunc = genAttackActionCost(u)
return a
}
@@ -221,6 +239,33 @@ func (a *AttackAction) String() string {
return fmt.Sprintf("%s x %v", u.FmtWController(), target.Tile().Position)
}
+type ArtifactSwitchAction struct {
+ ActionBase
+ Artifact Permanent
+}
+
+func newArtifactSwitchAction(u *Unit, artifact Permanent) *ArtifactSwitchAction {
+ a := &ArtifactSwitchAction{
+ ActionBase{
+ source: u,
+ Card: u.Card(),
+ },
+ artifact,
+ }
+
+ a.resolveFunc = func(s *State) {
+ s.switchPermanents(a.Source().(*Unit), a.Artifact)
+ }
+
+ a.costFunc = genMoveActionCost(u)
+
+ return a
+}
+
+func (a *ArtifactSwitchAction) String() string {
+ return fmt.Sprintf("%s <-> %v", a.Source().(*Unit).FmtWController(), a.Artifact)
+}
+
type FullAction struct {
ActionBase
desc string
@@ -242,14 +287,7 @@ func NewFullAction(u *Unit, proto ActionFuncPrototype, desc string) *FullAction
rf(s)
}
- a.costFunc = func(*State) bool {
- if u.AvailAttackActions < 1 && u.AvailMoveActions < 1 {
- return false
- }
- u.AvailAttackActions--
- u.AvailMoveActions--
- return true
- }
+ a.costFunc = genFullActionCost(u)
return a
}
diff --git a/go/game/card.go b/go/game/card.go
index f305634b..d9d2c81b 100644
--- a/go/game/card.go
+++ b/go/game/card.go
@@ -80,6 +80,15 @@ func (ct CardType) IsPermanent() bool {
}
}
+func (ct CardType) IsArtifact() bool {
+ switch ct {
+ case CardTypes.Artifact, CardTypes.Equipment, CardTypes.Potion:
+ return true
+ default:
+ return false
+ }
+}
+
type cardImplementation interface {
spawnTiles(*State, *Player) []*Tile
fullActions(*Unit) []*FullAction
diff --git a/go/game/permanent.go b/go/game/permanent.go
index db2dcaff..cd5cbfe6 100644
--- a/go/game/permanent.go
+++ b/go/game/permanent.go
@@ -203,6 +203,15 @@ func enterTile(p Permanent, t *Tile) {
p.Card().Impl.onEntering(t)
}
+func enterTileOrPile(p Permanent, t *Tile) {
+ if t.Permanent == nil {
+ enterTile(p, t)
+ } else {
+ AddPermanentToPile(t.Permanent, p)
+ p.SetTile(nil)
+ }
+}
+
func leaveTile(p Permanent) {
if p.Tile() != nil {
p.Card().Impl.onLeaving(p.Tile())
diff --git a/go/game/state.go b/go/game/state.go
index 7fa6e1bf..4af56b10 100644
--- a/go/game/state.go
+++ b/go/game/state.go
@@ -362,6 +362,17 @@ func (s *State) MoveUnit(u *Unit, t *Tile) {
u.move(s, t)
}
+func (s *State) switchPermanents(p1 Permanent, p2 Permanent) {
+ t1 := TileOrContainingPermTile(p1)
+ t2 := TileOrContainingPermTile(p2)
+
+ leaveTile(p1)
+ leaveTile(p2)
+
+ enterTileOrPile(p1, t2)
+ enterTileOrPile(p2, t1)
+}
+
func (s *State) FilterUnits(filter func(*Unit) bool) []*Unit {
units := []*Unit{}
for _, u := range s.Units {
diff --git a/go/game/unit.go b/go/game/unit.go
index 51383b77..bc9cad3f 100644
--- a/go/game/unit.go
+++ b/go/game/unit.go
@@ -99,12 +99,7 @@ func (u *Unit) move(s *State, tile *Tile) {
leaveTile(u)
}
- if tile.Permanent == nil {
- enterTile(u, tile)
- } else { // crew permanent at tile
- AddPermanentToPile(tile.Permanent, u)
- u.tile = nil
- }
+ enterTileOrPile(u, tile)
}
func (u *Unit) MoveRangeTiles(m *Map) []*Tile {
@@ -200,6 +195,12 @@ func (u *Unit) AvailSlowActions() (actions []Action) {
for _, p := range u.FullActions {
actions = append(actions, p)
}
+
+ for _, t := range TilesInRange(m, u, 1) {
+ if t.Permanent != nil && t.Permanent.Card().Type.IsArtifact() {
+ actions = append(actions, newArtifactSwitchAction(u, t.Permanent))
+ }
+ }
}
return actions