From f979afb58c1aa0bc83df96176e25b8d1a1833086 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Thu, 24 Jul 2025 13:21:17 -0400 Subject: fix Shy AI not issuing full actions --- go/game/ai.go | 13 +++---------- go/game/ai_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 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") + } +} -- cgit v1.2.3