Line data Source code
1 : package aiinput
2 :
3 : import (
4 : "fmt"
5 : "math/rand/v2"
6 :
7 : "github.com/kijimaD/ruins/internal/activity"
8 : gc "github.com/kijimaD/ruins/internal/components"
9 : "github.com/kijimaD/ruins/internal/consts"
10 : "github.com/kijimaD/ruins/internal/geometry"
11 : "github.com/kijimaD/ruins/internal/logger"
12 : w "github.com/kijimaD/ruins/internal/world"
13 :
14 : "github.com/mlange-42/ark/ecs"
15 : )
16 :
17 : // Planner はエンティティの行動計画を担うインターフェースを表す。
18 : // 各ターンのAPループ内で呼ばれ、次のアクションを返す。nil を返すとそのターンの行動を終える
19 : type Planner interface {
20 : Plan(world w.World, entity ecs.Entity) *gc.Activity
21 : }
22 :
23 : // maxActivitiesPerTurn は1ターン中に実行可能なアクティビティの上限を表す
24 : const maxActivitiesPerTurn = 10
25 :
26 : // runAPLoop はAPが残っている限りPlannerのアクションを連続実行する
27 : func runAPLoop(world w.World, entity ecs.Entity, planner Planner, log *logger.Logger) error {
28 1 : executed := 0
29 1 :
30 1 : for executed < maxActivitiesPerTurn {
31 1 : if world.Components.Dead.Has(entity) {
32 0 : log.Debug("entity is dead, aborting", "entity", entity)
33 0 : break
34 : }
35 :
36 1 : comp := planner.Plan(world, entity)
37 1 : if comp == nil {
38 0 : break
39 : }
40 :
41 1 : b, err := activity.GetBehavior(comp.BehaviorName)
42 1 : if err != nil {
43 0 : return err
44 0 : }
45 1 : actionCost := b.Info().ActionPointCost
46 1 : tbComp := world.Components.TurnBased.Get(entity)
47 1 : if tbComp == nil || tbComp.AP.Current < actionCost {
48 1 : log.Debug("insufficient AP", "entity", entity, "activity", comp.BehaviorName, "cost", actionCost)
49 1 : break
50 : }
51 :
52 1 : result, err := activity.Execute(comp, entity, world)
53 1 : if err != nil {
54 0 : return fmt.Errorf("entity %v %s: %w", entity, comp.BehaviorName, err)
55 0 : }
56 :
57 1 : log.Debug("action executed", "entity", entity, "activity", comp.BehaviorName, "success", result.Success)
58 1 : executed++
59 1 :
60 1 : if !result.Success {
61 0 : break
62 : }
63 : }
64 1 : return nil
65 : }
66 :
67 : // eightDirections は隣接8方向の座標差分を定義する
68 : var eightDirections = []consts.Coord[consts.Tile]{
69 : {X: -1, Y: -1}, {X: 0, Y: -1}, {X: 1, Y: -1},
70 : {X: -1, Y: 0}, {X: 1, Y: 0},
71 : {X: -1, Y: 1}, {X: 0, Y: 1}, {X: 1, Y: 1},
72 : }
73 :
74 : // calculateMoveCandidates はターゲットに向かう移動候補を計算する
75 : func calculateMoveCandidates(delta consts.Coord[consts.Tile]) []consts.Coord[consts.Tile] {
76 1 : var candidates []consts.Coord[consts.Tile]
77 1 : dx, dy := delta.X, delta.Y
78 1 :
79 1 : switch {
80 : case dx != 0 && dy != 0:
81 1 : moveX := consts.Tile(1)
82 1 : if dx < 0 {
83 1 : moveX = -1
84 1 : }
85 1 : moveY := consts.Tile(1)
86 1 : if dy < 0 {
87 1 : moveY = -1
88 1 : }
89 1 : candidates = append(candidates, consts.Coord[consts.Tile]{X: moveX, Y: moveY})
90 1 :
91 1 : if geometry.Abs(dx) > geometry.Abs(dy) {
92 1 : candidates = append(candidates, consts.Coord[consts.Tile]{X: moveX, Y: 0})
93 1 : candidates = append(candidates, consts.Coord[consts.Tile]{X: 0, Y: moveY})
94 1 : } else {
95 1 : candidates = append(candidates, consts.Coord[consts.Tile]{X: 0, Y: moveY})
96 1 : candidates = append(candidates, consts.Coord[consts.Tile]{X: moveX, Y: 0})
97 1 : }
98 : case dx != 0:
99 1 : moveX := consts.Tile(1)
100 1 : if dx < 0 {
101 1 : moveX = -1
102 1 : }
103 1 : candidates = append(candidates, consts.Coord[consts.Tile]{X: moveX, Y: 0})
104 1 : candidates = append(candidates, consts.Coord[consts.Tile]{X: 0, Y: 1})
105 1 : candidates = append(candidates, consts.Coord[consts.Tile]{X: 0, Y: -1})
106 : case dy != 0:
107 1 : moveY := consts.Tile(1)
108 1 : if dy < 0 {
109 0 : moveY = -1
110 0 : }
111 1 : candidates = append(candidates, consts.Coord[consts.Tile]{X: 0, Y: moveY})
112 1 : candidates = append(candidates, consts.Coord[consts.Tile]{X: 1, Y: 0})
113 1 : candidates = append(candidates, consts.Coord[consts.Tile]{X: -1, Y: 0})
114 : }
115 :
116 1 : return candidates
117 : }
118 :
119 : // tryMoveCandidates は移動候補を順に試行し、最初に移動可能な方向へ移動するアクションを返す
120 : func tryMoveCandidates(world w.World, entity ecs.Entity, from *gc.GridElement, candidates []consts.Coord[consts.Tile]) (*gc.Activity, bool) {
121 1 : fromPos := from.Coord
122 1 : for _, c := range candidates {
123 1 : dest := fromPos.Add(c)
124 1 : if activity.CanMoveTo(world, dest, fromPos, entity) {
125 1 : return moveAction(dest), true
126 1 : }
127 : }
128 1 : return nil, false
129 : }
130 :
131 : // moveAction は指定座標への移動アクションを生成する
132 : func moveAction(dest consts.Coord[consts.Tile]) *gc.Activity {
133 1 : return activity.NewMoveActivity(gc.GridElement{Coord: dest})
134 1 : }
135 :
136 : // waitAction は待機アクションを生成する
137 : func waitAction() *gc.Activity {
138 1 : return activity.NewWaitActivity(1)
139 1 : }
140 :
141 : // shuffledEightDirections は8方向をシャッフルして返す
142 : func shuffledEightDirections(rng *rand.Rand) []consts.Coord[consts.Tile] {
143 1 : shuffled := make([]consts.Coord[consts.Tile], len(eightDirections))
144 1 : copy(shuffled, eightDirections)
145 1 : for i := len(shuffled) - 1; i > 0; i-- {
146 1 : j := rng.IntN(i + 1)
147 1 : shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
148 1 : }
149 1 : return shuffled
150 : }
151 :
152 : // isAdjacent は2つのタイルが隣接しているかを判定する
153 : func isAdjacent(a, b *gc.GridElement) bool {
154 1 : return geometry.IsAdjacent(a.Coord, b.Coord)
155 1 : }
|