LCOV - code coverage report
Current view: top level - states - action_handlers.go Coverage Total Hit
Test: coverage.lcov Lines: 86.9 % 222 193
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package states
       2              : 
       3              : import (
       4              :         "github.com/kijimaD/ruins/internal/activity"
       5              :         gc "github.com/kijimaD/ruins/internal/components"
       6              :         "github.com/kijimaD/ruins/internal/consts"
       7              :         es "github.com/kijimaD/ruins/internal/engine/states"
       8              :         "github.com/kijimaD/ruins/internal/gamelog"
       9              :         "github.com/kijimaD/ruins/internal/geometry"
      10              :         w "github.com/kijimaD/ruins/internal/world"
      11              : 
      12              :         "github.com/kijimaD/ruins/internal/world/query"
      13              :         "github.com/mlange-42/ark/ecs"
      14              : )
      15              : 
      16              : // InteractionAction はインタラクション可能なアクション情報
      17              : type InteractionAction struct {
      18              :         Label       string             // 表示ラベル
      19              :         Target      ecs.Entity         // ターゲットエンティティ
      20              :         Interaction gc.InteractionKind // 実行するインタラクション
      21              : }
      22              : 
      23              : // GetInteractionActions はプレイヤー周辺の実行可能なアクションを取得する
      24              : func GetInteractionActions(world w.World) []InteractionAction {
      25            1 :         playerEntity, err := query.GetPlayerEntity(world)
      26            1 :         if err != nil {
      27            0 :                 return nil
      28            0 :         }
      29              : 
      30            1 :         if !world.Components.GridElement.Has(playerEntity) {
      31            0 :                 return nil
      32            0 :         }
      33              : 
      34            1 :         gridElement := world.Components.GridElement.Get(playerEntity)
      35            1 : 
      36            1 :         var actions []InteractionAction
      37            1 : 
      38              :         // インタラクティブな相互作用を全て取得してアクションを生成
      39            1 :         interactableEntities := activity.GetAllInteractiveInteractablesInRange(world, gridElement)
      40            1 :         for _, interactableEntity := range interactableEntities {
      41            1 :                 if !world.Components.GridElement.Has(interactableEntity) {
      42            0 :                         continue
      43              :                 }
      44            1 :                 if !world.Components.Interactable.Has(interactableEntity) {
      45            0 :                         continue
      46              :                 }
      47              : 
      48            1 :                 interactable := world.Components.Interactable.Get(interactableEntity)
      49            1 :                 interactableGrid := world.Components.GridElement.Get(interactableEntity)
      50            1 :                 // 実体単位でなく相互作用単位で発動範囲を絞る。隣接に来た実体でも、直上でしか発動しない
      51              :                 // 相互作用、例えばポータルは隣接メニューへ出さない。範囲判定は
      52              :                 // GetAllInteractiveInteractablesInRange と同じ IsInActivationRange を使う
      53            1 :                 var inRange []gc.InteractionKind
      54            1 :                 for _, interaction := range interactable.Interactions {
      55            1 :                         config := interaction.Config()
      56            1 :                         if (config.ActivationWay == gc.ActivationWayManual || config.ActivationWay == gc.ActivationWayOnCollision) &&
      57            1 :                                 query.IsInActivationRange(gridElement, interactableGrid, config.ActivationRange) {
      58            1 :                                 inRange = append(inRange, interaction)
      59            1 :                         }
      60              :                 }
      61            1 :                 if len(inRange) > 0 {
      62            1 :                         dirLabel := query.T(world, activity.GetDirectionLabel(gridElement, interactableGrid))
      63            1 :                         actions = append(actions, getInteractionActions(world, &gc.Interactable{Interactions: inRange}, interactableEntity, dirLabel)...)
      64            1 :                 }
      65              :         }
      66              : 
      67            1 :         actions = appendIgniteActions(world, playerEntity, gridElement, actions)
      68            1 :         return appendFeedFuelActions(world, gridElement, actions)
      69              : }
      70              : 
      71              : // appendFeedFuelActions は隣接する、燃えている火へ給油するアクションを足す。
      72              : // 対象は Burning を持つ火だけ。着火した火は必ず Burning を持つので、これが火であることの判定を兼ねる。
      73              : // 燃料の有無に関わらず出し、残ターン数を確認しつつ、燃料を持っていればくべられるようにする。
      74              : func appendFeedFuelActions(world w.World, playerGrid *gc.GridElement, actions []InteractionAction) []InteractionAction {
      75            1 :         fireQuery := query.ActiveFilter2[gc.Burning, gc.GridElement](world).Query()
      76            1 :         for fireQuery.Next() {
      77            1 :                 fire := fireQuery.Entity()
      78            1 :                 grid := world.Components.GridElement.Get(fire)
      79            1 :                 if !geometry.IsAdjacent(playerGrid.Coord, grid.Coord) {
      80            1 :                         continue
      81              :                 }
      82            1 :                 dirLabel := query.T(world, activity.GetDirectionLabel(playerGrid, grid))
      83            1 :                 actions = append(actions, InteractionAction{
      84            1 :                         Label:       query.T(world, "Feed fire (%s)", dirLabel),
      85            1 :                         Target:      fire,
      86            1 :                         Interaction: gc.InteractionFeedFuel,
      87            1 :                 })
      88              :         }
      89            1 :         return actions
      90              : }
      91              : 
      92              : // appendIgniteActions は火種を持つとき、隣接タイルの燃焼物へ着火するアクションをタイルごとに1つ足す。
      93              : // 足元は自分が燃えるため対象にしない。同じタイルに複数の燃料があっても着火は1タイル1回にまとめる。
      94              : // 着火の可否は火種の所持だけで判定する。火種を消費するかは別の関心で、ここでは扱わない。
      95              : func appendIgniteActions(world w.World, player ecs.Entity, playerGrid *gc.GridElement, actions []InteractionAction) []InteractionAction {
      96            1 :         if !query.HoldsFireStarter(world, player) {
      97            1 :                 return actions
      98            1 :         }
      99              :         // タイルごとに代表の燃料を1つ選ぶ。map の走査順に依存しないよう出現順を別に記録する
     100            1 :         repByTile := map[consts.Coord[consts.Tile]]ecs.Entity{}
     101            1 :         var order []consts.Coord[consts.Tile]
     102            1 :         // 地面の燃焼物を隣接タイルで絞るので GridElement も条件に入れる。フィルタで保証されるので Get は安全
     103            1 :         fuelQuery := query.ActiveFilter3[gc.Material, gc.LocationOnField, gc.GridElement](world).Query()
     104            1 :         for fuelQuery.Next() {
     105            1 :                 entity := fuelQuery.Entity()
     106            1 :                 if !query.IsCombustible(world, entity) {
     107            0 :                         continue
     108              :                 }
     109            1 :                 coord := world.Components.GridElement.Get(entity).Coord
     110            1 :                 if !geometry.IsAdjacent(playerGrid.Coord, coord) {
     111            1 :                         continue
     112              :                 }
     113            1 :                 if _, seen := repByTile[coord]; !seen {
     114            1 :                         repByTile[coord] = entity
     115            1 :                         order = append(order, coord)
     116            1 :                 }
     117              :         }
     118            1 :         for _, coord := range order {
     119            1 :                 rep := repByTile[coord]
     120            1 :                 grid := world.Components.GridElement.Get(rep)
     121            1 :                 dirLabel := query.T(world, activity.GetDirectionLabel(playerGrid, grid))
     122            1 :                 actions = append(actions, InteractionAction{
     123            1 :                         Label:       query.T(world, "Light fire (%s)", dirLabel),
     124            1 :                         Target:      rep,
     125            1 :                         Interaction: gc.InteractionIgnite,
     126            1 :                 })
     127            1 :         }
     128            1 :         return actions
     129              : }
     130              : 
     131              : // GetSameTileManualActions はプレイヤー直上のManual発動アクションを全て取得する
     132              : func GetSameTileManualActions(world w.World) []InteractionAction {
     133            1 :         playerEntity, err := query.GetPlayerEntity(world)
     134            1 :         if err != nil {
     135            1 :                 return nil
     136            1 :         }
     137            1 :         if !world.Components.GridElement.Has(playerEntity) {
     138            0 :                 return nil
     139            0 :         }
     140            1 :         playerGrid := world.Components.GridElement.Get(playerEntity)
     141            1 : 
     142            1 :         var actions []InteractionAction
     143            1 :         sameTileQuery := query.ActiveFilter2[gc.GridElement, gc.Interactable](world).Query()
     144            1 :         for sameTileQuery.Next() {
     145            1 :                 entity := sameTileQuery.Entity()
     146            1 :                 if world.Components.Dead.Has(entity) {
     147            0 :                         continue
     148              :                 }
     149            1 :                 ge := world.Components.GridElement.Get(entity)
     150            1 :                 if ge.X != playerGrid.X || ge.Y != playerGrid.Y {
     151            1 :                         continue
     152              :                 }
     153            1 :                 interactable := world.Components.Interactable.Get(entity)
     154            1 :                 // Manual+SameTileのインタラクションのみフィルタする
     155            1 :                 var filtered []gc.InteractionKind
     156            1 :                 for _, interaction := range interactable.Interactions {
     157            1 :                         config := interaction.Config()
     158            1 :                         if config.ActivationRange == gc.ActivationRangeSameTile && config.ActivationWay == gc.ActivationWayManual {
     159            1 :                                 filtered = append(filtered, interaction)
     160            1 :                         }
     161              :                 }
     162            1 :                 if len(filtered) > 0 {
     163            1 :                         actions = append(actions, getInteractionActions(world, &gc.Interactable{Interactions: filtered}, entity, query.T(world, "directly above"))...)
     164            1 :                 }
     165              :         }
     166              : 
     167            1 :         return actions
     168              : }
     169              : 
     170              : // startPickup は足元と周囲8タイルから拾う。アイテム1つだけならそのまま拾い、それ以外は拾うメニューを開く
     171              : func startPickup(world w.World) (es.Transition[w.World], error) {
     172            1 :         actions := nearbyPickupActions(world)
     173            1 :         switch {
     174              :         case len(actions) == 0:
     175            1 :                 gamelog.New(query.GetGameLog(world)).
     176            1 :                         Markup(query.T(world, "There is nothing to pick up nearby.")).
     177            1 :                         Log()
     178            1 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
     179              :         case len(actions) == 1 && actions[0].Interaction == gc.InteractionItem:
     180            1 :                 player, err := query.GetPlayerEntity(world)
     181            1 :                 if err != nil {
     182            0 :                         return es.Transition[w.World]{Type: es.TransNone}, err
     183            0 :                 }
     184            1 :                 _, err = activity.ExecuteInteraction(player, actions[0].Target, actions[0].Interaction, world)
     185            1 :                 return es.Transition[w.World]{Type: es.TransNone}, err
     186              :         }
     187            1 :         return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{
     188            1 :                 func() (es.State[w.World], error) {
     189            0 :                         return NewChoiceMenu(pickupChoices), nil
     190            0 :                 },
     191              :         }}, nil
     192              : }
     193              : 
     194              : // pickupChoices は拾うメニューの選択肢を返す。収納を選ぶとメニューを閉じ、まだ探していなければ探し始める
     195              : func pickupChoices(world w.World) (string, []Choice) {
     196            1 :         actions := nearbyPickupActions(world)
     197            1 :         choices := make([]Choice, 0, len(actions))
     198            1 :         for _, a := range actions {
     199            1 :                 choices = append(choices, Choice{Label: a.Label, Run: func(world w.World) (es.Transition[w.World], error) {
     200            1 :                         player, err := query.GetPlayerEntity(world)
     201            1 :                         if err != nil {
     202            0 :                                 return es.Transition[w.World]{}, err
     203            0 :                         }
     204            1 :                         result, err := activity.ExecuteInteraction(player, a.Target, a.Interaction, world)
     205            1 :                         if err != nil {
     206            0 :                                 return es.Transition[w.World]{}, err
     207            0 :                         }
     208            1 :                         if a.Interaction == gc.InteractionStorage {
     209              :                                 // 探し始めたか開いたなら閉じ、開く要求が拾うメニューごと開き直す。敵が近くて探せなければ一覧に残す
     210            1 :                                 if result != nil && result.Success {
     211            1 :                                         return es.Transition[w.World]{Type: es.TransPop}, nil
     212            1 :                                 }
     213            1 :                                 return es.Transition[w.World]{Type: es.TransNone}, nil
     214              :                         }
     215            1 :                         if len(nearbyPickupActions(world)) == 0 {
     216            1 :                                 return es.Transition[w.World]{Type: es.TransPop}, nil
     217            1 :                         }
     218            1 :                         return es.Transition[w.World]{Type: es.TransNone}, nil
     219              :                 }})
     220              :         }
     221            1 :         return "", choices
     222              : }
     223              : 
     224              : // nearbyPickupActions は足元と周囲8タイルの収納と、タイルごとにスタックで束ねた床のアイテムを行にする
     225              : func nearbyPickupActions(world w.World) []InteractionAction {
     226            1 :         player, err := query.GetPlayerEntity(world)
     227            1 :         if err != nil || !world.Components.GridElement.Has(player) {
     228            0 :                 return nil
     229            0 :         }
     230            1 :         playerGrid := world.Components.GridElement.Get(player)
     231            1 : 
     232            1 :         var actions []InteractionAction
     233            1 :         for dy := consts.Tile(-1); dy <= 1; dy++ {
     234            1 :                 for dx := consts.Tile(-1); dx <= 1; dx++ {
     235            1 :                         c := consts.Coord[consts.Tile]{X: playerGrid.X + dx, Y: playerGrid.Y + dy}
     236            1 :                         dirLabel := query.T(world, activity.GetDirectionLabel(playerGrid, &gc.GridElement{Coord: c}))
     237            1 :                         if storage, ok := interactableAt(world, c, gc.InteractionStorage); ok {
     238            1 :                                 actions = append(actions, InteractionAction{
     239            1 :                                         Label:       storageLabel(world, storage, dirLabel),
     240            1 :                                         Target:      storage,
     241            1 :                                         Interaction: gc.InteractionStorage,
     242            1 :                                 })
     243            1 :                         }
     244            1 :                         for _, stack := range query.GroupStacks(world, query.PickablesAt(world, c)) {
     245            1 :                                 actions = append(actions, InteractionAction{
     246            1 :                                         Label:       query.T(world, "Pick up %s (%s)", query.FormatNameCount(query.GetEntityName(stack.Rep, world), stack.Count), dirLabel),
     247            1 :                                         Target:      stack.Rep,
     248            1 :                                         Interaction: gc.InteractionItem,
     249            1 :                                 })
     250            1 :                         }
     251              :                 }
     252              :         }
     253            1 :         return actions
     254              : }
     255              : 
     256              : // storageLabel は収納の行の文言を返す。まだ探していない収納、空の収納、中身のある収納を見分けさせる
     257              : func storageLabel(world w.World, storage ecs.Entity, dirLabel string) string {
     258            1 :         name := query.GetEntityName(storage, world)
     259            1 :         switch {
     260              :         case world.Components.Unsearched.Has(storage):
     261            1 :                 return query.T(world, "Search %s (%s)", name, dirLabel)
     262              :         case !query.HasStorageItems(world, storage):
     263            1 :                 return query.T(world, "Open %s (empty) (%s)", name, dirLabel)
     264              :         default:
     265            1 :                 return query.T(world, "Open %s (%s)", name, dirLabel)
     266              :         }
     267              : }
     268              : 
     269              : // getInteractionActions はInteractableに対応するアクションを取得する
     270              : func getInteractionActions(world w.World, interactable *gc.Interactable, interactableEntity ecs.Entity, dirLabel string) []InteractionAction {
     271            1 :         var result []InteractionAction
     272            1 : 
     273            1 :         for _, interaction := range interactable.Interactions {
     274            1 :                 switch interaction {
     275              :                 case gc.InteractionDoor:
     276            1 :                         if world.Components.Door.Has(interactableEntity) {
     277            1 :                                 door := world.Components.Door.Get(interactableEntity)
     278            1 :                                 var label string
     279            1 :                                 if door.IsOpen {
     280            0 :                                         label = query.T(world, "Close (%s)", dirLabel)
     281            0 :                                 } else {
     282            1 :                                         label = query.T(world, "Open (%s)", dirLabel)
     283            1 :                                 }
     284            1 :                                 result = append(result, InteractionAction{
     285            1 :                                         Label:       label,
     286            1 :                                         Target:      interactableEntity,
     287            1 :                                         Interaction: interaction,
     288            1 :                                 })
     289              :                         }
     290              :                 case gc.InteractionTalk:
     291            1 :                         if world.Components.Name.Has(interactableEntity) {
     292            1 :                                 result = append(result, InteractionAction{
     293            1 :                                         Label:       query.T(world, "Talk (%s)", query.GetEntityName(interactableEntity, world)),
     294            1 :                                         Target:      interactableEntity,
     295            1 :                                         Interaction: interaction,
     296            1 :                                 })
     297            1 :                         }
     298              :                 case gc.InteractionPortalNext:
     299            1 :                         result = append(result, InteractionAction{
     300            1 :                                 Label:       query.T(world, "Warp (next floor)"),
     301            1 :                                 Target:      interactableEntity,
     302            1 :                                 Interaction: interaction,
     303            1 :                         })
     304              :                 case gc.InteractionPortalPrev:
     305            1 :                         result = append(result, InteractionAction{
     306            1 :                                 Label:       query.T(world, "Warp (previous floor)"),
     307            1 :                                 Target:      interactableEntity,
     308            1 :                                 Interaction: interaction,
     309            1 :                         })
     310              :                 case gc.InteractionDungeonEnter:
     311            0 :                         result = append(result, InteractionAction{
     312            0 :                                 Label:       query.T(world, "Enter ruins"),
     313            0 :                                 Target:      interactableEntity,
     314            0 :                                 Interaction: interaction,
     315            0 :                         })
     316              :                 case gc.InteractionOpenCubeMenu:
     317            1 :                         result = append(result, InteractionAction{
     318            1 :                                 Label:       query.T(world, "Open cube menu"),
     319            1 :                                 Target:      interactableEntity,
     320            1 :                                 Interaction: interaction,
     321            1 :                         })
     322            1 :                 case gc.InteractionItem, gc.InteractionMelee, gc.InteractionStorage, gc.InteractionSwap:
     323              :                         // アクションメニューに出さない種類。default を置かず exhaustive に全種別を
     324              :                         // 明示させ、新しい InteractionKind の対応漏れを lint で検知する
     325            0 :                 case gc.InteractionIgnite:
     326              :                         // 着火はエンティティの Interactable でなく appendIgniteActions が隣接タイル走査で
     327              :                         // タイル単位に組む。ここには来ないが exhaustive のため case を明示する
     328            0 :                 case gc.InteractionFeedFuel:
     329              :                         // 給油も Interactable でなく appendFeedFuelActions が隣接の火を走査して組む。
     330              :                         // ここには来ないが exhaustive のため case を明示する
     331              :                 }
     332              :         }
     333              : 
     334            1 :         return result
     335              : }
        

Generated by: LCOV version 2.0-1