aboutsummaryrefslogtreecommitdiff
path: root/go/game/ai.go
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2023-02-25 23:08:47 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2025-08-20 15:57:06 +0200
commit3d36d1d0254b8aeecc889a2a49cbcced458cbe9c (patch)
tree38dd0851d8eaa6535d05b97f29f65fc35655a5ef /go/game/ai.go
parent19fb1a7e5c5883f49e944b6499fa1ced207a3e39 (diff)
downloadmuhqs-game-3d36d1d0254b8aeecc889a2a49cbcced458cbe9c.tar.gz
muhqs-game-3d36d1d0254b8aeecc889a2a49cbcced458cbe9c.zip
intermediate commit
* Implement move artifact action * Fix widget update memory leak * update the highlights not during on each frame * do not update the unchanged label of a button * Allow to exit the game by pressing 'q' * Implement some cards from the magic set * Improve permanent formatting * Some tweaks to UnitAI code
Diffstat (limited to 'go/game/ai.go')
-rw-r--r--go/game/ai.go62
1 files changed, 53 insertions, 9 deletions
diff --git a/go/game/ai.go b/go/game/ai.go
index 31a16363..94a9f7ec 100644
--- a/go/game/ai.go
+++ b/go/game/ai.go
@@ -15,6 +15,7 @@ type UnitAI struct {
s *State
u *Unit
actions chan Action
+ target *Target
syncGameState *sync.WaitGroup
}
@@ -44,6 +45,7 @@ func (ai *UnitAI) promptAction() {
func (ai *UnitAI) Execute(f func(*UnitAI)) {
defer close(ai.actions)
if ai.awaitGameStateSync() {
+ // TODO: Investigate hangs when the AI did not issue any actions
f(ai)
}
}
@@ -57,7 +59,12 @@ func NewUnitAI(s *State, u *Unit) *UnitAI {
c := make(chan Action)
wg := new(sync.WaitGroup)
wg.Add(1)
- ai := &UnitAI{s, u, c, wg}
+ ai := &UnitAI{
+ s: s,
+ u: u,
+ actions: c,
+ syncGameState: wg,
+ }
switch aiDesc {
case "aggressive":
@@ -72,6 +79,9 @@ func NewUnitAI(s *State, u *Unit) *UnitAI {
}
go ai.Execute(func(ai *UnitAI) { WanderingAI(ai, x) })
case "target-oriented":
+ aiDesc := u.Card().getCanonicalValues("ai")[0]
+ targetDesc := aiDesc[len("target-oriented")+1:]
+ ai.target = newTarget(s, newTargetDesc(targetDesc), nil)
go ai.Execute(TargetOrientedAI)
}
@@ -120,6 +130,36 @@ func (m *Map) generateMapGraphFor(u *Unit) *dijkstra.Graph {
return graph
}
+// func (m *Map) generateMovementRangeGraphFor(u *Unit) *dijkstra.Graph {
+// origin := TileOrContainingPermTile(u).Position
+// graph := dijkstra.NewGraph()
+// for _, t := range tilesInRangeFromOrigin(m, origin, u.Movement.Range, true) {
+// graph.AddMappedVertex(t.Position.String())
+// }
+
+// for _, t := range TilesInRange(m, u, u.Movement.Range) {
+// tId := t.Position.String()
+// for _, neighbour := range tilesInRangeFromOrigin(m, t.Position, 1, true) {
+// if !IsPositionInRange(neighbour.Position, origin, u.Movement.Range) {
+// continue
+// }
+
+// if u.IsAvailableTile(neighbour) {
+// cost := 2
+// if t.OnDiagonal(neighbour) {
+// cost = 3
+// }
+// log.Printf("add %v -> %v: %d", tId, neighbour.Position.String(), int64(cost))
+// err := graph.AddMappedArc(tId, neighbour.Position.String(), int64(cost))
+// if err != nil {
+// log.Panicf("Failed to add mapped arc: %s", err)
+// }
+// }
+// }
+// }
+// return graph
+// }
+
func findPathTo(graph *dijkstra.Graph, u *Unit, pos Position) (dijkstra.BestPath, error) {
srcId, err := graph.GetMapping(TileOrContainingPermTile(u).Position.String())
if err != nil {
@@ -304,8 +344,8 @@ out:
// }
// 3.
- if ai.u.AvailAttackActions > 0 {
- attackAttackableEnemyPerm(ai)
+ if a := attackAttackableEnemyPerm(ai); a != nil {
+ ai.actions <- a
}
}
@@ -346,12 +386,16 @@ func TargetOrientedAI(ai *UnitAI) {
// 1.
if ai.u.HasFullAction() {
fullAction(ai)
+ return
}
- // if ai.t != nil {
- // // TODO: path finding again
- // attackAttackableEnemyPerm(ai)
- // } else {
- WanderingAI(ai, 3)
- // }
+ options := ai.target.Options()
+ if len(options) > 0 {
+ // TODO: move towards nearest target
+ if a := attackAttackableEnemyPerm(ai); a != nil {
+ ai.actions <- a
+ }
+ } else {
+ WanderingAI(ai, 3)
+ }
}