LCOV - code coverage report
Current view: top level - systems - weather.go Coverage Total Hit
Test: coverage.lcov Lines: 90.5 % 42 38
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package systems
       2              : 
       3              : import (
       4              :         "math/rand/v2"
       5              : 
       6              :         "github.com/kijimaD/ruins/internal/gamelog"
       7              :         w "github.com/kijimaD/ruins/internal/world"
       8              :         "github.com/kijimaD/ruins/internal/world/query"
       9              : )
      10              : 
      11              : // weatherSpellStreamKey はスペル長の乱数を次の天候の抽選と別系列にするための定数。RunSeed と XOR して
      12              : // 系列を分けるだけで、値そのものに意味はない。ストリームが分かれれば何でもよい非ゼロの任意値。
      13              : const weatherSpellStreamKey uint64 = 0x5715_9EA7_4E12_3D5B
      14              : 
      15              : // WeatherSystem は世界全体の天候スペルを進めるシステム。スペルが尽きたら次の天候を引き直す。
      16              : type WeatherSystem struct{}
      17              : 
      18              : // String はシステム名を返す。
      19              : func (sys *WeatherSystem) String() string {
      20            1 :         return "WeatherSystem"
      21            1 : }
      22              : 
      23              : // Update は現在のスペルが尽きていれば次の天候へ遷移する。まだ続くなら何もしない。
      24              : func (sys *WeatherSystem) Update(world w.World) error {
      25            1 :         gt := query.GetGameTime(world)
      26            1 :         weather := query.GetWeather(world)
      27            1 :         if gt.TotalTurns < weather.UntilTurn {
      28            1 :                 return nil
      29            1 :         }
      30              : 
      31              :         // 共有 RNG を消費すると下流の敵AIや loot の乱数までずれるので、世界生成の RunSeed とターンから
      32              :         // 専用ストリームを引く。RunSeed はテストやリプレイで固定されるが Config.Seed は都度乱数になる
      33            1 :         band := query.GetSeamlessBand(world)
      34            1 :         if band == nil {
      35            0 :                 return nil
      36            0 :         }
      37            1 :         season := gt.GetSeason()
      38            1 :         northDepth := playerNorthDepth(world)
      39            1 :         // 次の天候とスペル長で別系列の乱数を使う。1つの rng を順に消費すると NextWeather の抽選回数に
      40              :         // RollSpellTurns の入力が依存して決定論が崩れるため、系列を分ける
      41            1 :         turn := uint64(gt.TotalTurns)
      42            1 :         rngNext := rand.New(rand.NewPCG(band.RunSeed, turn))
      43            1 :         rngSpell := rand.New(rand.NewPCG(band.RunSeed^weatherSpellStreamKey, turn))
      44            1 : 
      45            1 :         defs := world.Resources.WeatherDefs
      46            1 :         prev := weather.Current
      47            1 :         next := query.NextWeather(defs, prev, season, northDepth, rngNext)
      48            1 :         weather.Current = next
      49            1 :         weather.UntilTurn = gt.TotalTurns + query.RollSpellTurns(defs, next, season, northDepth, rngSpell)
      50            1 : 
      51            1 :         if next != prev {
      52            1 :                 name := query.WeatherName(world, next)
      53            1 :                 gamelog.New(query.GetGameLog(world)).
      54            1 :                         Markup(gamelog.Tag("system", query.T(world, "The weather changed to %s.", name))).
      55            1 :                         Log()
      56            1 :                 // 視程が動いたら、移動を待たずその場で視界を再計算させる
      57            1 :                 if defs.Kinds[next].Effect.VisionDelta != defs.Kinds[prev].Effect.VisionDelta {
      58            0 :                         query.GetVisionState(world).RequestUpdate()
      59            0 :                 }
      60              :         }
      61            1 :         return nil
      62              : }
      63              : 
      64              : // playerNorthDepth はプレイヤーの北への奥行きを返す。プレイヤーや座標が無ければ0で穏やかに倒す。
      65              : func playerNorthDepth(world w.World) int {
      66            1 :         player, err := query.GetPlayerEntity(world)
      67            1 :         if err != nil {
      68            1 :                 return 0
      69            1 :         }
      70            1 :         if !world.Components.GridElement.Has(player) {
      71            1 :                 return 0
      72            1 :         }
      73            1 :         return query.NorthDepthChunks(world, world.Components.GridElement.Get(player).Y)
      74              : }
        

Generated by: LCOV version 2.0-1