LCOV - code coverage report
Current view: top level - overworld - district_jump.go Coverage Total Hit
Test: coverage.lcov Lines: 87.1 % 62 54
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package overworld
       2              : 
       3              : import (
       4              :         "errors"
       5              :         "fmt"
       6              : 
       7              :         gc "github.com/kijimaD/ruins/internal/components"
       8              :         "github.com/kijimaD/ruins/internal/consts"
       9              :         "github.com/kijimaD/ruins/internal/rawschema"
      10              :         w "github.com/kijimaD/ruins/internal/world"
      11              :         "github.com/kijimaD/ruins/internal/world/lifecycle"
      12              :         "github.com/kijimaD/ruins/internal/world/query"
      13              :         "github.com/mlange-42/ark/ecs"
      14              : )
      15              : 
      16              : var (
      17              :         // errDistrictNotFound は raw に無い区域の種類を指定したときに返す
      18              :         errDistrictNotFound = errors.New("district not found")
      19              :         // errNoDistrictNorth は北に区域が見つからないときに返す
      20              :         errNoDistrictNorth = errors.New("no district to the north")
      21              : )
      22              : 
      23              : // districtJumpSearch は北の区域を探すリージョンの数の上限
      24              : const districtJumpSearch = 100
      25              : 
      26              : // JumpToDistrict はプレイヤーの北で一番近い種類 id の区域まで帯を進め、プレイヤーを区域の南の端の
      27              : // 真ん中の列のすぐ外へ、キューブをその隣へ置く。シフトは南端の行を捨てるので、シフトのたびにプレイヤーと
      28              : // キューブを1行北へ動かして中央の行に留める
      29              : func (dr *Driver) JumpToDistrict(world w.World, id string) error {
      30            1 :         raws := world.Resources.RawMaster
      31            1 :         var target *district
      32            1 :         for _, d := range districtsFrom(raws) {
      33            1 :                 if d.row.Id == id {
      34            1 :                         target = &d
      35            1 :                         break
      36              :                 }
      37              :         }
      38            1 :         if target == nil {
      39            1 :                 return fmt.Errorf("%w: %q", errDistrictNotFound, id)
      40            1 :         }
      41              : 
      42            1 :         player, err := query.GetPlayerEntity(world)
      43            1 :         if err != nil {
      44            0 :                 return err
      45            0 :         }
      46            1 :         sb := query.GetSeamlessBand(world)
      47            1 :         chunkH := sb.ChunkH
      48            1 :         playerRow := sb.AbsChunkRow(world.Components.GridElement.Get(player).Y)
      49            1 : 
      50            1 :         r, ok := nearestDistrictNorth(raws, *target, sb.RunSeed, playerRow, sb.Cols, sb.SpawnChunkY())
      51            1 :         if !ok {
      52            0 :                 return fmt.Errorf("%w: %q", errNoDistrictNorth, id)
      53            0 :         }
      54              : 
      55            1 :         for range playerRow - (r.anchor.Y + r.h) {
      56            1 :                 world.Components.GridElement.Get(player).Y -= chunkH
      57            1 :                 cubes := query.ActiveFilter2[gc.GridElement, gc.Cube](world).Query()
      58            1 :                 for cubes.Next() {
      59            1 :                         world.Components.GridElement.Get(cubes.Entity()).Y -= chunkH
      60            1 :                 }
      61            1 :                 if err := dr.band.ShiftNorth(world, dr.gen); err != nil {
      62            0 :                         return err
      63            0 :                 }
      64              :         }
      65            1 :         sb.NorthIndex = dr.band.NorthIndex()
      66            1 :         query.InvalidateSpatialIndex(world)
      67            1 : 
      68            1 :         rowTop := world.Components.GridElement.Get(player).Y / chunkH * chunkH
      69            1 :         gate := consts.Coord[consts.Tile]{X: (r.anchor.X + r.w/2).Tiles(sb.ChunkW) + urbanStreetW/2, Y: rowTop}
      70            1 :         spawn := walkableSpawnNear(world, gate, nil)
      71            1 :         if err := lifecycle.MovePlayerToPosition(world, spawn); err != nil {
      72            0 :                 return err
      73            0 :         }
      74              :         // 置物や落ちている物の上に置くと、キューブに塞がれて触れなくなる
      75            1 :         taken := map[consts.Coord[consts.Tile]]bool{spawn: true}
      76            1 :         onField := query.ActiveFilter2[gc.GridElement, gc.LocationOnField](world).Without(ecs.C[gc.Cube]()).Query()
      77            1 :         for onField.Next() {
      78            1 :                 taken[world.Components.GridElement.Get(onField.Entity()).Coord] = true
      79            1 :         }
      80            1 :         cubes := query.ActiveFilter2[gc.GridElement, gc.Cube](world).Query()
      81            1 :         for cubes.Next() {
      82            1 :                 pos := walkableSpawnNear(world, spawn.Add(consts.Coord[consts.Tile]{X: 2}), taken)
      83            1 :                 taken[pos] = true
      84            1 :                 world.Components.GridElement.Get(cubes.Entity()).Coord = pos
      85            1 :         }
      86            1 :         query.InvalidateSpatialIndex(world)
      87            1 :         query.GetVisionState(world).RequestUpdate()
      88            1 :         return nil
      89              : }
      90              : 
      91              : // nearestDistrictNorth は絶対チャンク行 playerRow より北にある種類 d の区域のうち、一番近いものを返す。
      92              : // 種類の違う区域との重なりで出ない区域は飛ばす
      93              : func nearestDistrictNorth(raws rawschema.Raws, d district, runSeed uint64, playerRow, cols, spawnRow consts.Chunk) (districtRegion, bool) {
      94            1 :         start := consts.FloorDiv(playerRow, consts.Chunk(d.row.Spacing))
      95            1 :         for i := range consts.Chunk(districtJumpSearch) {
      96            1 :                 r := d.regionIn(runSeed, start-i, cols)
      97            1 :                 if r.anchor.Y+r.h >= playerRow {
      98            1 :                         continue
      99              :                 }
     100            1 :                 if got, ok := districtAt(raws, runSeed, r.anchor, cols, spawnRow); ok && got == r {
     101            1 :                         return r, true
     102            1 :                 }
     103              :         }
     104            1 :         return districtRegion{}, false
     105              : }
        

Generated by: LCOV version 2.0-1