aboutsummaryrefslogtreecommitdiff
path: root/go/game/ai.go
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2024-12-27 19:10:16 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2025-01-27 16:43:58 +0100
commit660c175c66c5ba6af55c1c86a59d49736c191115 (patch)
tree67aa741775f5a5b20be67d5ea2d23df76c7ad66e /go/game/ai.go
parent026ad913e78551716ac14648288eecd159685020 (diff)
downloadmuhqs-game-660c175c66c5ba6af55c1c86a59d49736c191115.tar.gz
muhqs-game-660c175c66c5ba6af55c1c86a59d49736c191115.zip
finish the shy AI
Diffstat (limited to 'go/game/ai.go')
-rw-r--r--go/game/ai.go63
1 files changed, 54 insertions, 9 deletions
diff --git a/go/game/ai.go b/go/game/ai.go
index ec535182..415f01e0 100644
--- a/go/game/ai.go
+++ b/go/game/ai.go
@@ -258,6 +258,43 @@ func moveToRandomTile(ai *UnitAI) Action {
return a
}
+func moveAwayFromEnemies(ai *UnitAI) Action {
+ if ai.u.AvailMoveActions == 0 {
+ return nil
+ }
+
+ graph := ai.s.Map().generateMapGraphFor(ai.u)
+ var farest *Tile
+ var farestEnemyDistance int64
+
+ for _, availableTile := range ai.u.MoveRangeTiles(ai.s.Map()) {
+ var nearestEnemy int64 = -1
+
+ for _, enemyUnit := range ai.s.EnemyUnits(ai.u.Controller()) {
+ p, err := findPathTo(graph, enemyUnit, availableTile.Position)
+ if err != nil {
+ continue
+ }
+ if nearestEnemy == -1 || p.Distance < nearestEnemy {
+ nearestEnemy = p.Distance
+ }
+ }
+
+ if farest == nil || farestEnemyDistance < nearestEnemy {
+ farest = availableTile
+ farestEnemyDistance = nearestEnemy
+ }
+ }
+
+ if farest == nil {
+ return nil
+ }
+
+ a := NewMoveAction(ai.u)
+ a.Target().AddSelection(farest)
+ return a
+}
+
func actionFromPaths(ai *UnitAI, graph *dijkstra.Graph, paths []*dijkstra.BestPath) Action {
nearest := 0
for i, p := range paths {
@@ -332,7 +369,7 @@ func attackAttackableEnemyPerm(ai *UnitAI) Action {
return a
}
-func fullAction(ai *UnitAI) {
+func fullAction(ai *UnitAI) Action {
fullActions := ai.u.FullActions
idx := 0
if len(fullActions) > 1 {
@@ -340,10 +377,14 @@ func fullAction(ai *UnitAI) {
}
a := fullActions[idx]
- err := selectRandomTargets(ai.s.Rand, a.Targets())
- if err == nil {
- ai.actions <- a
+ if a.Targets() != nil && a.Targets().RequireSelection() {
+ err := selectRandomTargets(ai.s.Rand, a.Targets())
+ if err == nil {
+ return nil
+ }
}
+
+ return a
}
// 1. If enemy Unit in attack Range then
@@ -399,13 +440,17 @@ out:
}
}
if !inEnemyAttackRange && ai.u.HasFullAction() {
- fullAction(ai)
+ ai.actions <- fullAction(ai)
+ return
}
// 2.
- // TODO: implement path finding
- // if u.AvailMoveActions > 0 {
- // }
+ if ai.u.AvailMoveActions > 0 {
+ a := moveAwayFromEnemies(ai)
+ if !ai.PostAndContinue(a) {
+ return
+ }
+ }
// 3.
if a := attackAttackableEnemyPerm(ai); a != nil {
@@ -452,7 +497,7 @@ func WanderingAI(ai *UnitAI, x int) {
func TargetOrientedAI(ai *UnitAI) {
// 1.
if ai.u.HasFullAction() {
- fullAction(ai)
+ ai.actions <- fullAction(ai)
return
}