Line data Source code
1 : package query
2 :
3 : import (
4 : "math/rand/v2"
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 : )
10 :
11 : // GetWeather はシングルトンから世界全体の天候を取得する。
12 : func GetWeather(world w.World) *gc.Weather {
13 1 : return GetSingleton[gc.Weather](world, world.Components.Weather)
14 1 : }
15 :
16 : // placeSeverityCoef は奥地1チャンクあたり厳しい天候の重みをどれだけ伸ばすか。値は暫定。
17 : const placeSeverityCoef = 0.05
18 :
19 : // weatherStayWeight は遷移の粘り。同じ天候を引き継ぐ重み。1より大きいほど regime が続く。値は暫定。
20 : const weatherStayWeight = 2.5
21 :
22 : // stickiness は前の天候から次への遷移の粘りを返す。同一は重く、離れるほど 1/距離 で細る。段階を踏む
23 : // 移ろいを表す。距離は WeatherKind の並び順に密結合するので、並びを変えると遷移の起きやすさも変わる。
24 : func stickiness(current, next gc.WeatherKind) float64 {
25 1 : if current == next {
26 1 : return weatherStayWeight
27 1 : }
28 1 : d := int(current) - int(next)
29 1 : if d < 0 {
30 1 : d = -d
31 1 : }
32 1 : return 1.0 / float64(d)
33 : }
34 :
35 : // placeFactor は奥地ほど厳しい天候を厚くする係数を返す。severity0 の晴れ・曇り・雨は奥行きに反応せず、
36 : // 雪・吹雪・寒波だけが northDepth に応じて伸びる。北へ進むほど荒天が主役になる。
37 : func placeFactor(defs gc.WeatherDefs, northDepth int, next gc.WeatherKind) float64 {
38 1 : if northDepth < 0 {
39 1 : northDepth = 0
40 1 : }
41 1 : return 1.0 + float64(defs.Kinds[next].Severity)*float64(northDepth)*placeSeverityCoef
42 : }
43 :
44 : // NextWeather は現在の天候から次の天候を1つ引く。粘り・季節親和・場所寄せの積で重み付けする。
45 : // 季節親和は defs の天候ごとの季節の重み。northDepth はプレイヤーの北への奥行き。奥ほど厳しい天候へ寄る。
46 : func NextWeather(defs gc.WeatherDefs, current gc.WeatherKind, season gc.Season, northDepth int, rng *rand.Rand) gc.WeatherKind {
47 1 : var weights [gc.NumWeatherKind]float64
48 1 : for next := range gc.NumWeatherKind {
49 1 : kind := gc.WeatherKind(next)
50 1 : weights[next] = stickiness(current, kind) *
51 1 : defs.Kinds[kind].SeasonWeights[season] *
52 1 : placeFactor(defs, northDepth, kind)
53 1 : }
54 2 : i, ok := consts.Pick(rng, weights[:], func(w float64) float64 { return w })
55 1 : if !ok {
56 1 : return current
57 1 : }
58 1 : return gc.WeatherKind(i)
59 : }
60 :
61 : // harshSpellStretchPerChunk は奥地1チャンクあたり厳しい天候のスペルをどれだけ伸ばすか。値は暫定。
62 : const harshSpellStretchPerChunk = 0.02
63 :
64 : // RollSpellTurns は天候 kind のスペルが続くターン数を引く。天候種で基準長が変わり、冬・奥地では
65 : // 厳しい天候が長引く。下限+乱数で裾を作る。
66 : func RollSpellTurns(defs gc.WeatherDefs, kind gc.WeatherKind, season gc.Season, northDepth int, rng *rand.Rand) consts.Turn {
67 1 : if northDepth < 0 {
68 1 : northDepth = 0
69 1 : }
70 1 : def := defs.Kinds[kind]
71 1 : lo, hi := def.SpellTurnsMin, def.SpellTurnsMax
72 1 : turns := lo
73 1 : if hi > lo {
74 1 : turns += rng.IntN(hi - lo)
75 1 : }
76 : // 厳しい天候は冬と奥地で長引く。severity0 の晴れ・曇り・雨は伸ばさない
77 1 : if def.Severity > 0 {
78 1 : stretch := 1.0 + float64(northDepth)*harshSpellStretchPerChunk
79 1 : if season == gc.SeasonWinter {
80 1 : stretch += 0.5
81 1 : }
82 1 : turns = int(float64(turns) * stretch)
83 : }
84 1 : return consts.Turn(turns)
85 : }
|