aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2025-07-24 13:21:17 -0400
committerFlorian Fischer <florian.fischer@muhq.space>2025-07-28 18:03:10 +0200
commitf979afb58c1aa0bc83df96176e25b8d1a1833086 (patch)
tree7342b3e450e6fdf02bb899f89936bd4bdb38bfa2
parent749dc58ced17662d9f6dd40bb44d315d12d3d48b (diff)
downloadmuhqs-game-f979afb58c1aa0bc83df96176e25b8d1a1833086.tar.gz
muhqs-game-f979afb58c1aa0bc83df96176e25b8d1a1833086.zip
fix Shy AI not issuing full actions
-rw-r--r--go/game/ai.go13
-rw-r--r--go/game/ai_test.go43
2 files changed, 46 insertions, 10 deletions
diff --git a/go/game/ai.go b/go/game/ai.go
index d2caa008..faf8f11b 100644
--- a/go/game/ai.go
+++ b/go/game/ai.go
@@ -118,10 +118,7 @@ func selectRandomTargets(rand *rand.Rand, targets *Targets) error {
return fmt.Errorf("No possible selection")
}
- idx := 0
- if len(options) > 1 {
- idx = rand.Intn(len(options) - 1)
- }
+ idx := rand.Intn(len(options))
_ = t.AddSelection(options[idx])
}
}
@@ -431,15 +428,11 @@ func attackAttackableEnemyPerm(ai *UnitAI) Action {
func fullAction(ai *UnitAI) Action {
fullActions := ai.u.FullActions
- idx := 0
- if len(fullActions) > 1 {
- idx = ai.s.Rand.Intn(len(fullActions) - 1)
- }
- a := fullActions[idx]
+ a := fullActions[ai.s.Rand.Intn(len(fullActions))]
if a.Targets() != nil && a.Targets().RequireSelection() {
err := selectRandomTargets(ai.s.Rand, a.Targets())
- if err == nil {
+ if err != nil {
return nil
}
}
diff --git a/go/game/ai_test.go b/go/game/ai_test.go
index 60ab1082..9c90c729 100644
--- a/go/game/ai_test.go
+++ b/go/game/ai_test.go
@@ -272,3 +272,46 @@ symbols:
t.Fatal("ShyAI did not close its channel")
}
}
+
+func TestUnthreatendShyUnitAI(t *testing.T) {
+ mapDef := `map: |1-
+ HST
+ H H
+ F W
+symbols:
+ T: tower
+ H: house
+ F: farm
+ S: street
+ W: deep water
+`
+ s := NewLocalState()
+
+ m, _ := readMap(strings.NewReader(mapDef))
+ s.SetMap(m)
+
+ p := s.AddNewPlayer("p", NewDeck())
+
+ r := s.addNewUnit(NewCard("base/recruiter"), Position{1, 1}, p)
+ ai := NewUnitAI(s, r)
+ if ai == nil {
+ t.Fatal("ai is nil")
+ }
+
+ ai.promptAction()
+ a, more := ai.NextAction()
+ if a == nil {
+ t.Fatal("Nil action received from shy AI")
+ }
+ if _, ok := a.(*FullAction); !ok {
+ t.Fatal("Not FullAction received from uncontested recruiter")
+ }
+ if !more {
+ t.Fatal("Ai action channel already closed")
+ }
+
+ _, more = ai.NextAction()
+ if more {
+ t.Fatal("AI did not finish after issuing a FullAction")
+ }
+}