Line data Source code
1 : package query
2 :
3 : import (
4 : "errors"
5 : "math"
6 :
7 : gc "github.com/kijimaD/ruins/internal/components"
8 : "github.com/kijimaD/ruins/internal/consts"
9 : "github.com/kijimaD/ruins/internal/geometry"
10 : "github.com/kijimaD/ruins/internal/rawschema"
11 : w "github.com/kijimaD/ruins/internal/world"
12 : "github.com/mlange-42/ark/ecs"
13 : )
14 :
15 : // 温度閾値の定数
16 : const (
17 : // ComfortableTempLower は快適温度の下限
18 : ComfortableTempLower = 11
19 : // ComfortableTempUpper は快適温度の上限
20 : ComfortableTempUpper = 30
21 : )
22 :
23 : // Insulation は部位ごとの断熱値
24 : type Insulation struct {
25 : Cold int // 耐寒。快適温度の下限を下げる
26 : Heat int // 耐暑。快適温度の上限を上げる
27 : }
28 :
29 : // comfortableRange は断熱値から快適温度範囲を計算する
30 : func comfortableRange(insulation Insulation) (lower, upper int) {
31 1 : return ComfortableTempLower - insulation.Cold, ComfortableTempUpper + insulation.Heat
32 1 : }
33 :
34 : // sleepTemperatureMargin は入眠可能な温度を快適帯からどれだけ広げるか。
35 : // 快適でなくとも多少の寒暖なら眠れる。値は実プレイで調整する
36 : const sleepTemperatureMargin = 5
37 :
38 : // SleepableTemperatureRange は装備断熱込みの快適帯を margin ぶん広げた入眠可能な温度帯を返す。
39 : // この帯を外れると寒すぎ暑すぎで眠れず、火を焚くか屋内に入る必要がある
40 : func SleepableTemperatureRange(world w.World, entity ecs.Entity) (lower, upper int) {
41 1 : cl, cu := comfortableRange(CalculateEquippedInsulation(world, entity))
42 1 : return cl - sleepTemperatureMargin, cu + sleepTemperatureMargin
43 1 : }
44 :
45 : // stageBaseTemperature はステージの基本気温を返す。値はステージ生成時に StageField.BaseTemp へ
46 : // 確定済みで、ここは読むだけ。StageField 未確定なら基本気温0として扱う。
47 : // dungeon 登録表を引かないことで query から dungeon への依存を断つ
48 : func stageBaseTemperature(world w.World) int {
49 1 : if field := GetCurrentStageField(world); field != nil {
50 1 : return field.BaseTemp
51 1 : }
52 1 : return 0
53 : }
54 :
55 : // AmbientTemperatureAt はタイルの周囲気温を返す。ステージの基本気温、囲われに応じて
56 : // 受け方を変えた世界温度、タイルの加算℃、熱源の押し上げの4項の和になる。
57 : // 緯度勾配は引数 y のタイル固有の緯度で測るので、同じ帯でも北のタイルほど寒い。
58 : func AmbientTemperatureAt(world w.World, x, y consts.Tile) (int, error) {
59 1 : if GetDungeon(world) == nil {
60 1 : return 0, errors.New("dungeon resource is not set")
61 1 : }
62 1 : baseTemp := stageBaseTemperature(world)
63 1 :
64 1 : gt := GetGameTime(world)
65 1 : // 屋外の世界温度。その日の季節と緯度の分に、時間帯と天候を足す。天候の寒さも worldTemp の一項なので
66 : // 屋内は ShelteredWorldTemp で緩和される。末尾で引くと屋内外が同じだけ寒くなってしまう
67 1 : worldTemp := climateWorldTemperatureAt(world, y) + world.Resources.CalendarDefs.TimesOfDay[gt.GetTimeOfDay()].TempModifier +
68 1 : world.Resources.WeatherDefs.Kinds[GetWeather(world).Current].Effect.TempModifier
69 1 : shelter, tileModifier := TileEnvironmentAt(world, x, y)
70 1 :
71 1 : return baseTemp +
72 1 : world.Resources.TileEnvDefs.ShelteredWorldTemp(shelter, worldTemp) +
73 1 : tileModifier +
74 1 : ambientHeatAt(world, x, y), nil
75 : }
76 :
77 : // ClimateTemperatureAt は屋外のその日の平均気温を返す。ステージの基本気温と季節と緯度の和で、
78 : // 時間帯・天候・屋内外・熱源を含まないので1日の中では変わらない
79 : func ClimateTemperatureAt(world w.World, y consts.Tile) int {
80 1 : return stageBaseTemperature(world) + climateWorldTemperatureAt(world, y)
81 1 : }
82 :
83 : // climateWorldTemperatureAt は世界温度のうち、その日の中で変わらない季節と緯度の分を返す
84 : func climateWorldTemperatureAt(world w.World, y consts.Tile) int {
85 1 : return world.Resources.CalendarDefs.SeasonalTemperatureForDay(GetGameTime(world).GetDayNumber()) - latitudeCold(world, y)
86 1 : }
87 :
88 : // 雪がまだらに積もる気温の幅。その日の平均気温が snowStartTemperature で積もり始め、
89 : // snowFullTemperature で屋根の無い土の地面の全てを覆う
90 : const (
91 : snowStartTemperature = 5
92 : snowFullTemperature = -30
93 : )
94 :
95 : // SnowFraction はその日の平均気温 temp から、雪に覆われる地面のマスの割合を 0 から 1 で返す
96 : func SnowFraction(temp int) float64 {
97 1 : switch {
98 : case temp >= snowStartTemperature:
99 1 : return 0
100 : case temp <= snowFullTemperature:
101 1 : return 1
102 : }
103 1 : return float64(snowStartTemperature-temp) / float64(snowStartTemperature-snowFullTemperature)
104 : }
105 :
106 : // SnowFractionAt は帯ローカル座標 y の行で、雪に覆われる地面のマスの割合を返す
107 : func SnowFractionAt(world w.World, y consts.Tile) float64 {
108 1 : return SnowFraction(ClimateTemperatureAt(world, y))
109 1 : }
110 :
111 : // 雪の積もり方のむら。snowPatchSize マスほどの大きさでなめらかに変わる値を主にし、マスごとの揺らぎを
112 : // snowJitterWeight だけ混ぜる。雪は塊で積もり始め、寒くなるほど塊が広がってつながる
113 : const (
114 : snowPatchSize = 6
115 : snowJitterWeight = 0.2
116 : )
117 :
118 : // SnowRankAt は帯ローカル座標 (x, y) のマスが雪に覆われる順位を 0 以上 1 未満で返す。割合がこれを上回れば覆われる。
119 : // 帯がずれても同じ場所で同じ値になるよう絶対座標から決める
120 : func SnowRankAt(world w.World, x, y consts.Tile) float64 {
121 1 : absY := int(y)
122 1 : if sb := GetSeamlessBand(world); sb != nil {
123 1 : absY = int(sb.LocalToAbsY(y))
124 1 : }
125 1 : ax := int(x)
126 1 : gx, gy := consts.FloorDiv(ax, snowPatchSize), consts.FloorDiv(absY, snowPatchSize)
127 1 : fx := smoothstep(float64(ax-gx*snowPatchSize) / snowPatchSize)
128 1 : fy := smoothstep(float64(absY-gy*snowPatchSize) / snowPatchSize)
129 1 : top := lerp(unitHash(gx, gy), unitHash(gx+1, gy), fx)
130 1 : bottom := lerp(unitHash(gx, gy+1), unitHash(gx+1, gy+1), fx)
131 1 : patch := lerp(top, bottom, fy)
132 1 : return (1-snowJitterWeight)*patch + snowJitterWeight*unitHash(ax*snowPatchSize+1, absY*snowPatchSize+1)
133 : }
134 :
135 : // unitHash は格子点 (x, y) から 0 以上 1 未満の決定的な値を返す
136 : func unitHash(x, y int) float64 {
137 1 : h := consts.Mix64(uint64(int64(x))*0x9E3779B97F4A7C15 + uint64(int64(y))*0xC2B2AE3D27D4EB4F)
138 1 : return float64(h>>11) / (1 << 53)
139 1 : }
140 :
141 1 : func smoothstep(t float64) float64 { return t * t * (3 - 2*t) }
142 :
143 1 : func lerp(a, b, t float64) float64 { return a + (b-a)*t }
144 :
145 : // snowEdgeWidth は雪の塊の縁で、量が0から1まで変わる順位の幅。縁ほど薄く積もり、境目がぼける
146 : const snowEdgeWidth = 0.25
147 :
148 : // SnowAmount は雪の割合 fraction とマスの順位 rank から積もる量を 0 から 1 で返す。割合と順位の差から決め、
149 : // 塊の内側ほど濃く縁ほど薄い。割合が1なら全てのマスが1になるよう、割合を縁の幅ぶん引き伸ばす
150 : func SnowAmount(fraction, rank float64) float64 {
151 1 : if fraction <= 0 {
152 1 : return 0
153 1 : }
154 1 : return math.Min(math.Max((fraction*(1+snowEdgeWidth)-rank)/snowEdgeWidth, 0), 1)
155 : }
156 :
157 : // 奥地ほど寒い緯度勾配のパラメータ。奥へ進んだチャンク距離が増えるほど世界温度を下げる。値は実プレイで調整する。
158 : const (
159 : // latitudeColdPerChunk は1チャンク奥へ進むごとに下がる℃
160 : latitudeColdPerChunk = 1
161 : // latitudeColdMax は緯度勾配で下げる℃の上限。これ以上奥へ進んでも寒くならない
162 : latitudeColdMax = 40
163 : )
164 :
165 : // NorthDepthChunks は帯ローカル座標 y が湧き位置から北へ何チャンク進んだかを返す。
166 : // 起点は初期帯の中央行 SpawnChunkY で、開始地点を穏やかに保つ。手前や帯を持たないステージでは0。
167 : // 緯度勾配と HUD の奥地表示がこの1関数を共有する。絶対軸 Y で測るので帯シフトをまたいでも連続。
168 : func NorthDepthChunks(world w.World, y consts.Tile) int {
169 1 : sb := GetSeamlessBand(world)
170 1 : // ChunkH<=0 はゼロ除算を、Rows<=0 は起点計算を壊す。未初期化の退化帯を計算より前に弾く
171 1 : if sb == nil || sb.ChunkH <= 0 || sb.Rows <= 0 {
172 1 : return 0
173 1 : }
174 : // 絶対 Y は北側で負になりうるので consts.FloorDiv でチャンク境界を連続させる
175 1 : currentChunkY := consts.FloorDiv(int(sb.LocalToAbsY(y)), int(sb.ChunkH))
176 1 : if depth := int(sb.SpawnChunkY()) - currentChunkY; depth > 0 {
177 1 : return depth
178 1 : }
179 1 : return 0
180 : }
181 :
182 : // latitudeCold は帯ローカル座標 y の緯度勾配の寒さ、すなわち世界温度から差し引く℃を返す。
183 : // 基礎的な寒さはステージの基本気温が担い、緯度勾配は北ほど寒い加算分だけを表す。
184 : func latitudeCold(world w.World, y consts.Tile) int {
185 1 : return latitudeColdForDepth(NorthDepthChunks(world, y))
186 1 : }
187 :
188 : // latitudeColdForDepth は湧き位置から奥へ進んだチャンク距離から差し引く℃を返す純粋計算。
189 : // 距離に比例して単調増加し、latitudeColdMax で頭打ちになる。起点手前や負の距離では0。
190 : func latitudeColdForDepth(chunksDeep int) int {
191 1 : if chunksDeep <= 0 {
192 1 : return 0
193 1 : }
194 1 : if cold := chunksDeep * latitudeColdPerChunk; cold < latitudeColdMax {
195 1 : return cold
196 1 : }
197 1 : return latitudeColdMax
198 : }
199 :
200 : // ambientHeatPerWarmth は熱源の暖かさ1あたり環境気温へ押し上げる℃。
201 : // 体温タイマーへの直接回復とは別の効きで、火を焚けば周囲気温そのものが上がる。
202 : // 状態は持たず、毎回そのターンの熱源から平衡値を出す。値は実プレイで調整する。
203 : // 焚き火 warmth 0.75 の半径内で +23℃ になり、春の夜が火のそばで快適帯に入る
204 : const ambientHeatPerWarmth = 30
205 :
206 : // ambientHeatAt はタイル座標に届く熱源の環境気温への押し上げ℃を返す
207 : func ambientHeatAt(world w.World, x, y consts.Tile) int {
208 1 : return int(math.Round(HeatSourceWarmthAt(world, x, y) * ambientHeatPerWarmth))
209 1 : }
210 :
211 : // CalculateEquippedInsulation はエンティティの装備から全身の断熱値を計算する。
212 : // 各装備部位の断熱値を合算して返す。
213 : func CalculateEquippedInsulation(world w.World, owner ecs.Entity) Insulation {
214 1 : var total Insulation
215 1 :
216 1 : equipQuery := ecs.NewFilter2[gc.LocationEquipped, gc.Wearable](world.ECS).Query()
217 1 : for equipQuery.Next() {
218 1 : item := equipQuery.Entity()
219 1 : equipped := world.Components.LocationEquipped.Get(item)
220 1 : if equipped.Owner != owner {
221 1 : continue
222 : }
223 :
224 1 : wearable := world.Components.Wearable.Get(item)
225 1 : total.Cold += wearable.InsulationCold
226 1 : total.Heat += wearable.InsulationHeat
227 : }
228 :
229 1 : return total
230 : }
231 :
232 : // TileEnvironmentAt は指定座標のタイル環境を返す。囲われの緩和度と、水・植生の加算℃
233 : func TileEnvironmentAt(world w.World, x, y consts.Tile) (rawschema.Shelter, int) {
234 1 : shelter := rawschema.Outdoor
235 1 : var modifier int
236 1 : tileTempQuery := ActiveFilter2[gc.GridElement, gc.TileEnvironment](world).Query()
237 1 : for tileTempQuery.Next() {
238 1 : entity := tileTempQuery.Entity()
239 1 : grid := world.Components.GridElement.Get(entity)
240 1 : if grid.X == x && grid.Y == y {
241 1 : tileTemp := world.Components.TileEnvironment.Get(entity)
242 1 : shelter = tileTemp.Shelter
243 1 : modifier = world.Resources.TileEnvDefs.TempModifier(*tileTemp)
244 1 : tileTempQuery.Close()
245 1 : break
246 : }
247 : }
248 1 : return shelter, modifier
249 : }
250 :
251 : // HeatSourceWarmthAt はタイル座標に届く全熱源の暖かさ合計を返す。
252 : // 各熱源は半径内で距離によらず一律に効き、半径外は効かない。複数の熱源は加算する。
253 : func HeatSourceWarmthAt(world w.World, x, y consts.Tile) float64 {
254 1 : at := consts.Coord[consts.Tile]{X: x, Y: y}
255 1 : var warmth float64
256 1 : heatQuery := ActiveFilter2[gc.HeatSource, gc.GridElement](world).Query()
257 1 : defer heatQuery.Close()
258 1 : for heatQuery.Next() {
259 1 : entity := heatQuery.Entity()
260 1 : src := world.Components.HeatSource.Get(entity)
261 1 : grid := world.Components.GridElement.Get(entity)
262 1 : if geometry.ChebyshevDistance(at, grid.Coord) <= int(src.Radius) {
263 1 : warmth += src.Warmth
264 1 : }
265 : }
266 1 : return warmth
267 : }
|