aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2023-02-04 13:09:57 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2025-08-20 15:46:39 +0200
commitdb60306a942874062bf7a706aca6d31e15f6fef9 (patch)
treea1a1df2baa4a75c1ab7114216f2090cb08debc0e
parenta907c8794f572a5b330bea1f9143887c8f2a4c4a (diff)
downloadmuhqs-game-db60306a942874062bf7a706aca6d31e15f6fef9.tar.gz
muhqs-game-db60306a942874062bf7a706aca6d31e15f6fef9.zip
introduce state-based action
State-based action are executed anytime a action resolved to handle events caused by the game state. Currently state-based actions only destroy unit with more damage than health.
-rw-r--r--go/game/stack.go7
-rw-r--r--go/game/state.go20
2 files changed, 15 insertions, 12 deletions
diff --git a/go/game/stack.go b/go/game/stack.go
index 9132ae13..cf0622ce 100644
--- a/go/game/stack.go
+++ b/go/game/stack.go
@@ -17,11 +17,11 @@ func (s *Stack) IsEmpty() bool {
return len(s.Actions) == 0
}
-func (s *Stack) Push(a Action) {
+func (s *Stack) push(a Action) {
s.Actions = append(s.Actions, a)
}
-func (s *Stack) Pop() {
+func (s *Stack) pop() {
l := len(s.Actions)
if l == 0 {
log.Fatalf("Can not pop from empty stack")
@@ -41,6 +41,7 @@ func (s *Stack) Pop() {
func (s *Stack) Resolve() {
for !s.IsEmpty() {
s.gameState.AllPassing(false)
- s.Pop()
+ s.pop()
+ s.gameState.stateBasedActions()
}
}
diff --git a/go/game/state.go b/go/game/state.go
index 15b6b226..45c8bba5 100644
--- a/go/game/state.go
+++ b/go/game/state.go
@@ -96,6 +96,14 @@ func (s *State) Loop() []*Player {
return winners
}
+func (s *State) stateBasedActions() {
+ for _, u := range s.Units {
+ if u.IsDestroyed() {
+ s.DestroyPermanent(u)
+ }
+ }
+}
+
func (s *State) IsValidPlay(a *PlayAction) error {
if s.ActivePhase != Phases.ActionPhase {
log.Fatalf("Play actions can only be declared during one's actions phase")
@@ -203,12 +211,12 @@ func (s *State) ValidateAction(a Action) (err error) {
return
}
-func (s *State) PushAction(a Action) {
+func (s *State) pushAction(a Action) {
if !a.PayCosts(s) {
log.Fatalf("Cost for %s could not be paid", a)
}
- s.Stack.Push(a)
+ s.Stack.push(a)
}
func (s *State) DeclareAction(a Action) {
@@ -234,7 +242,7 @@ func (s *State) DeclareAction(a Action) {
}
if err == nil {
- s.PushAction(a)
+ s.pushAction(a)
}
s.broadcastNotification(NewDeclaredActionNotification(a, err))
@@ -337,12 +345,6 @@ func (s *State) DestroyPermanent(p Permanent) {
func (s *State) Fight(p1, p2 Permanent) {
p1.Fight(p2)
p2.Fight(p1)
- if p1.IsDestroyed() {
- s.DestroyPermanent(p1)
- }
- if p2.IsDestroyed() {
- s.DestroyPermanent(p2)
- }
}
func (s *State) MoveUnit(u *Unit, t *Tile) {