LCOV - code coverage report
Current view: top level - world/lifecycle - facility.go Coverage Total Hit
Test: coverage.lcov Lines: 81.6 % 49 40
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package lifecycle
       2              : 
       3              : import (
       4              :         "fmt"
       5              : 
       6              :         gc "github.com/kijimaD/ruins/internal/components"
       7              :         "github.com/kijimaD/ruins/internal/consts"
       8              :         w "github.com/kijimaD/ruins/internal/world"
       9              :         "github.com/kijimaD/ruins/internal/world/query"
      10              :         "github.com/mlange-42/ark/ecs"
      11              : )
      12              : 
      13              : // FacilityCellFree は展開空間のマスが設備を装着できる空きかを返す。壁・キャラ・prop・アイテムが
      14              : // 1つでもあれば偽。キューブ自身のマスは装着先にしない。メニューを開いたプレイヤーのマスは障害物として
      15              : // 見ない。deploySpaceFree の判定を1マスに絞ったもので、装着 UI のマス表示と PlaceFacility の
      16              : // 最終ガードが同じ判定を共有する。
      17              : func FacilityCellFree(world w.World, cube ecs.Entity, coord consts.Coord[consts.Tile]) bool {
      18            1 :         if world.Components.GridElement.Get(cube).Coord == coord {
      19            0 :                 return false
      20            0 :         }
      21              :         // 空間索引が未構築なら nil。その間は空きなし扱いで装着を拒否し、安全側に倒す
      22            1 :         si := query.GetSpatialIndex(world)
      23            1 :         if si == nil {
      24            0 :                 return false
      25            0 :         }
      26            1 :         if si.IsBlockPass(coord) {
      27            0 :                 return false
      28            0 :         }
      29              :         // 取得失敗時は InvalidEntity になるが実キャラと一致しないので、誰も除外しないだけで安全側に倒れる
      30            1 :         player, _ := query.GetPlayerEntity(world)
      31            1 :         if e, ok := si.CharacterAt(coord); ok && e != player {
      32            0 :                 return false
      33            0 :         }
      34              :         // 空間索引は prop・アイテムを持たないので LocationOnField を走査する
      35            1 :         q := query.ActiveFilter1[gc.LocationOnField](world).Query()
      36            1 :         defer q.Close()
      37            1 :         for q.Next() {
      38            1 :                 e := q.Entity()
      39            1 :                 if e == cube || !world.Components.GridElement.Has(e) {
      40            1 :                         continue
      41              :                 }
      42            1 :                 if world.Components.GridElement.Get(e).Coord != coord {
      43            0 :                         continue
      44              :                 }
      45            1 :                 if world.Components.Prop.Has(e) || world.Components.Item.Has(e) {
      46            1 :                         return false
      47            1 :                 }
      48              :         }
      49            1 :         return true
      50              : }
      51              : 
      52              : // PlaceFacility は装着アイテムをキューブの展開空間の coord へ装着する。実体は削除せず、Item を外し Prop を
      53              : // 付けてロケーションをフィールドへ移す。マスが空いていなければ装着せず error を返す。
      54              : func PlaceFacility(world w.World, cube ecs.Entity, item ecs.Entity, coord consts.Coord[consts.Tile]) (ecs.Entity, error) {
      55            1 :         if !world.Components.Deployable.Has(item) {
      56            1 :                 return gc.InvalidEntity, fmt.Errorf("place facility: item is not deployable")
      57            1 :         }
      58              :         // 装着は設備画面がバックパックの装着アイテムから呼ぶ。他ロケーションからの経路は現状無いが、
      59              :         // 前提を握りつぶさず error にして、将来の経路追加で総重量の再計算が漏れるのを早期に検知する
      60            1 :         if !world.Components.LocationInBackpack.Has(item) {
      61            1 :                 return gc.InvalidEntity, fmt.Errorf("place facility: item is not in backpack")
      62            1 :         }
      63            1 :         if !FacilityCellFree(world, cube, coord) {
      64            1 :                 return gc.InvalidEntity, fmt.Errorf("place facility: cell %s is occupied", coord)
      65            1 :         }
      66              :         // 前の所有者を渡して総重量の再計算を促す
      67            1 :         owner := world.Components.LocationInBackpack.Get(item).Owner
      68            1 :         ensureRemoved(world.Components.Item, item)
      69            1 :         ensureMarker(world, world.Components.Prop, item, &gc.Prop{})
      70            1 :         MoveMembersToField(world, []ecs.Entity{item}, coord, owner)
      71            1 :         return item, nil
      72              : }
      73              : 
      74              : // RemoveFacility は装着した設備をプレイヤーのバックパックへ戻す。実体は削除せず、Prop を外し Item を付けて
      75              : // 移す。収納に中身が残るときは孤児化を避けるため戻さず error を返す。
      76              : func RemoveFacility(world w.World, facility ecs.Entity, player ecs.Entity) error {
      77            1 :         if !world.Components.Deployable.Has(facility) {
      78            1 :                 return fmt.Errorf("remove facility: entity is not deployable")
      79            1 :         }
      80            1 :         if len(query.GetStorageItems(world, facility)) > 0 {
      81            1 :                 return fmt.Errorf("remove facility: storage is not empty")
      82            1 :         }
      83            1 :         ensureRemoved(world.Components.Prop, facility)
      84            1 :         ensureMarker(world, world.Components.Item, facility, &gc.Item{})
      85            1 :         return MoveToBackpack(world, facility, player)
      86              : }
        

Generated by: LCOV version 2.0-1