Line data Source code
1 : package aiinput
2 :
3 : import (
4 : "fmt"
5 : "math/rand/v2"
6 :
7 : gc "github.com/kijimaD/ruins/internal/components"
8 : "github.com/kijimaD/ruins/internal/consts"
9 : "github.com/kijimaD/ruins/internal/geometry"
10 : "github.com/kijimaD/ruins/internal/logger"
11 : "github.com/kijimaD/ruins/internal/rawschema"
12 : w "github.com/kijimaD/ruins/internal/world"
13 : "github.com/kijimaD/ruins/internal/world/query"
14 :
15 : "github.com/mlange-42/ark/ecs"
16 : )
17 :
18 : // activationMargin は視界半径に加える余白タイル数。
19 : // 圏外の敵が視界に入るのと同じターンに反応できるよう、視界より一回り広く処理する
20 : const activationMargin = 2
21 :
22 : // activationRadius は SoloAI を距離カリングせず処理するチェビシェフ半径(タイル)。
23 : // この半径を超えた非交戦 SoloAI は行動計画をスキップする(アクティベーション半径)
24 : const activationRadius = int(consts.VisionRadiusTiles) + activationMargin
25 :
26 : // Processor はAIエンティティの行動処理を管理する。
27 : // SoloAI エンティティを soloPlanner で処理する
28 : type Processor struct {
29 : logger *logger.Logger
30 : planner Planner
31 : }
32 :
33 : // NewProcessor は新しいProcessorを作成する。
34 : // rngはゲーム全体のseedから派生した乱数生成器を渡す
35 : func NewProcessor(rng *rand.Rand) *Processor {
36 1 : return &Processor{
37 1 : logger: logger.New(logger.CategoryTurn),
38 1 : planner: newSoloPlanner(rand.New(rand.NewPCG(rng.Uint64(), rng.Uint64()))),
39 1 : }
40 : }
41 :
42 : // ProcessAll は全 SoloAI エンティティを soloPlanner で処理する。
43 : func (p *Processor) ProcessAll(world w.World) error {
44 : // 退避中ステージのAIはターンを取らない
45 1 : var targets []ecs.Entity
46 1 : soloQuery := query.ActiveFilter2[gc.SoloAI, gc.GridElement](world).Query()
47 1 : for soloQuery.Next() {
48 1 : targets = append(targets, soloQuery.Entity())
49 1 : }
50 : // クエリ反復を終えてからカリングする。反復中に GetPlayerEntity の別クエリを張らない
51 1 : culled, err := cullDistantSolo(world, targets)
52 1 : if err != nil {
53 0 : return err
54 0 : }
55 1 : targets = culled
56 1 :
57 1 : for _, entity := range targets {
58 1 : if world.Components.Dead.Has(entity) {
59 0 : continue
60 : }
61 1 : if err := runAPLoop(world, entity, p.planner, p.logger); err != nil {
62 0 : return err
63 0 : }
64 : }
65 :
66 1 : return nil
67 : }
68 :
69 : // cullDistantSolo は遠方の非交戦 SoloAI を処理対象から除外する。
70 : // 交戦中は視界外でも対象を追い続ける設計のため、距離に関わらず残す。随伴中のキューブも、遅れて
71 : // 圏外へ出ても追い続けるので残す。
72 : func cullDistantSolo(world w.World, targets []ecs.Entity) ([]ecs.Entity, error) {
73 1 : playerEntity, err := query.GetPlayerEntity(world)
74 1 : if err != nil {
75 1 : return nil, fmt.Errorf("culling requires a player: %w", err)
76 1 : }
77 1 : if !world.Components.GridElement.Has(playerEntity) {
78 0 : return nil, fmt.Errorf("player has no position")
79 0 : }
80 1 : playerGrid := world.Components.GridElement.Get(playerEntity)
81 1 :
82 1 : kept := make([]ecs.Entity, 0, len(targets))
83 1 : for _, entity := range targets {
84 1 : solo := world.Components.SoloAI.Get(entity)
85 1 : if solo != nil && !isActiveCombatState(solo.SubState) && solo.Movement != rawschema.Follow {
86 1 : grid := world.Components.GridElement.Get(entity)
87 1 : if geometry.ChebyshevDistance(playerGrid.Coord, grid.Coord) > activationRadius {
88 : // 圏外の待機・徘徊敵はスキップ。画面外で観測不能なため凍結してよい
89 1 : continue
90 : }
91 : }
92 1 : kept = append(kept, entity)
93 : }
94 1 : return kept, nil
95 : }
96 :
97 : // isActiveCombatState は交戦中(追跡・逃亡)状態かを返す。
98 : // 交戦中の敵は視界外でも追い続ける設計のため距離カリングの対象外とする
99 : func isActiveCombatState(s gc.AIStateSubState) bool {
100 1 : return s == gc.AIStateChasing || s == gc.AIStateFleeing
101 1 : }
|