-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather_api.go
More file actions
100 lines (94 loc) · 3.1 KB
/
weather_api.go
File metadata and controls
100 lines (94 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package weather
import (
"github.com/GoMudEngine/GoMud/modules/weather/engine"
"github.com/GoMudEngine/GoMud/modules/weather/sim"
)
// registerExports exposes the module API to other modules and JS scripts via
// plugin.ExportFunction (spec §9.7). All exports guard simReady so callers
// during boot (or in degraded mode) get empty-but-valid answers.
// Mutating exports rely on the engine invoking exported functions on the
// MainWorker goroutine (the same single-goroutine guarantee as commands and
// events).
func (m *weatherModule) registerExports() {
m.plug.ExportFunction(`GetWeather`, m.exportGetWeather)
m.plug.ExportFunction(`GetFronts`, m.exportGetFronts)
m.plug.ExportFunction(`SpawnFront`, m.exportSpawnFront)
m.plug.ExportFunction(`GetSeason`, m.exportGetSeason)
}
// exportGetSeason reports a zone's current season: {"track": string,
// "season": string, "blend": float64}. Empty strings when seasons are off,
// the zone is unknown, or its biome is unbound.
func (m *weatherModule) exportGetSeason(zone string) map[string]any {
out := map[string]any{"track": "", "season": "", "blend": 0.0}
if !m.simReady || !m.seasonsOn {
return out
}
canonical, ok := m.graph.FindZone(zone)
if !ok {
return out
}
zs, ok := m.zoneSeasons[canonical]
if !ok {
return out
}
out["track"] = zs.Track
out["season"] = zs.Season
out["blend"] = zs.Blend
return out
}
// exportGetWeather reports a zone's current weather: {"type": string,
// "intensity": float64}. type is "" when the sim isn't running or the zone is
// unknown; intensity is the strongest effective front projection (0 for calm).
func (m *weatherModule) exportGetWeather(zone string) map[string]any {
out := map[string]any{"type": "", "intensity": 0.0}
if !m.simReady {
return out
}
canonical, ok := m.graph.FindZone(zone)
if !ok {
return out
}
w := m.state.Weather[canonical]
if w == "" {
w = sim.Clear
}
out["type"] = string(w)
if covers := sim.Covering(m.graph, m.state.Fronts, m.simCfg, canonical); len(covers) > 0 {
out["intensity"] = covers[0].Effective
}
return out
}
// exportGetFronts lists active fronts as plain maps (id, type, zone,
// intensity, moisture, age).
func (m *weatherModule) exportGetFronts() []map[string]any {
if !m.simReady {
return nil
}
out := make([]map[string]any, 0, len(m.state.Fronts))
for _, f := range m.state.Fronts {
out = append(out, map[string]any{
"id": uint64(f.Id), "type": string(f.Type), "zone": f.Zone,
"intensity": f.Intensity, "moisture": f.Moisture, "age": f.Age,
})
}
return out
}
// exportSpawnFront programmatically spawns a front (e.g. a quest summoning a
// storm). Returns false when the sim isn't running or the zone is unknown.
func (m *weatherModule) exportSpawnFront(wtype string, zone string, intensity float64) bool {
if !m.simReady {
return false
}
canonical, ok := m.graph.FindZone(zone)
if !ok {
return false
}
next, _, ok := sim.ForceSpawn(m.state, m.graph, m.simCfg, sim.WeatherType(wtype), canonical, intensity, sim.Clock{Round: engine.CurrentRound()})
if !ok {
return false
}
m.state = next
engine.Reconcile(m.state.Weather)
m.persistState()
return true
}