Sign in
mod

Weather and seasons

by Potluck · version 1 · Checked for all ages

Rain, storms, snow, fog and wind, and four seasons of a week each. Summer is mostly fine, autumn wet and foggy, and in winter it snows. Crops grow fast in spring and summer and stop outdoors in winter (a greenhouse keeps them growing), the leaves turn in autumn, rain waters crops and puts out fires, storms strike tall trees, and winter nights are cold without wool, leather or a fire. Knobs set the length of a season or hold the weather.

1--!strict
2-- Weather and the seasons (@potluck/sky). The days are counted as the sun
3-- goes around (world.daytime), and every few days make a season: spring,
4-- summer, autumn and winter in turn. The weather comes in spells of a few
5-- minutes, picked by the season: summer is mostly clear, autumn wet and
6-- foggy, and in winter it snows instead of raining. Storms throw lightning
7-- at the tallest trees near players (sky.strike), which Fire can catch.
8-- The game draws all of it on every game in the world, and plays the rain
9-- and the thunder; Farming, Fire and Armor read the season and the
10-- weather for themselves. What day it is and the spell are kept with the
11-- world (store), so a server's year goes on after a restart.
12
13local world = require("@potluck/world@0.33")
14local sky = require("@potluck/sky@0.1")
15local tweak = require("@potluck/tweak")
16local store = require("@potluck/store")
17
18local knobs = tweak {
19 days = { 7, min = 1, max = 30, label = "Days in a season", order = 1 },
20 seasons = { true, label = "Seasons go by", order = 2 },
21 changes = { true, label = "Weather changes", order = 3 },
22 storms = { true, label = "Storms and lightning", order = 4 },
23 minutes = { 5, min = 1, max = 30, label = "Minutes a spell of weather lasts, about", order = 5 },
24}
25
26local climate = require("./climate")
27local SEASONS = climate.SEASONS
28
29-- the days gone by, and the time of day last time it looked: a new world
30-- starts a little way into spring (two days of seven), after the thaw, so
31-- it isn't under melting snow
32local days: number = store.get("days") or math.floor(math.max(1, math.floor(knobs.days)) * 0.3)
33local last = world.daytime()
34
35-- the spell of weather now: its kind, how heavy, the wind, and the seconds
36-- of it left
37type Spell = { kind: string, strength: number, wind: { number }, left: number }
38local spell: Spell = store.get("spell") or { kind = "clear", strength = 0, wind = { 0, 0 }, left = 60 }
39
40local function season(): (string, number)
41 local per = math.max(1, math.floor(knobs.days))
42 local into = days % (per * 4)
43 local name = SEASONS[math.floor(into / per) + 1] or "spring"
44 -- how far through it, with today's part of a day
45 local progress = math.clamp(((into % per) + last) / per, 0, 1)
46 return name, progress
47end
48
49local function save()
50 store.set("days", days)
51 store.set("spell", spell)
52end
53
54-- Says the weather now, coming in over `over` seconds.
55local function show(over: number)
56 sky.set_weather(spell.kind, { strength = spell.strength, wind = vector.create(spell.wind[1], 0, spell.wind[2]), over = over })
57end
58
59-- A new spell for the season, a few minutes long.
60local function next_spell()
61 local name = season()
62 local kind = if knobs.changes then climate.pick(name, knobs.storms) else "clear"
63 spell = {
64 kind = kind,
65 strength = if kind == "clear" then 0 else world.random(0.3, 1),
66 wind = climate.wind(kind),
67 left = knobs.minutes * 60 * world.random(0.6, 1.5),
68 }
69 show(30)
70 save()
71end
72
73-- At once, as the world starts: the season and the weather it was in.
74sky.set_season(season())
75show(0)
76
77-- The days, and the season they make.
78local said = ""
79world.every(1, function()
80 local now = world.daytime()
81 if now < last - 0.5 then
82 -- past midnight
83 if knobs.seasons then
84 days += 1
85 end
86 save()
87 end
88 last = now
89 local name, progress = season()
90 -- a word to the season every few seconds of its progress, so it isn't
91 -- sent to every game each second
92 local say = name .. string.format("%.3f", progress)
93 if say ~= said then
94 said = say
95 sky.set_season(name, progress)
96 end
97 spell.left -= 1
98 if spell.left <= 0 then
99 next_spell()
100 end
101end)
102
103-- The tallest thing near `at`, of a dozen places tried around it: a tree's
104-- crown, a tower's top. Where lightning goes, just above it.
105local function tallest(at: vector): vector?
106 local best: vector? = nil
107 for _ = 1, 12 do
108 local turn = world.random() * 2 * math.pi
109 local far = world.random(8, 48)
110 local x, z = math.floor(at.x + math.cos(turn) * far), math.floor(at.z + math.sin(turn) * far)
111 local y = world.top(x, z)
112 -- leaves aren't solid to world.top: climb through a tree's crown
113 local up = y + 1
114 while up <= y + 12 and world.block(vector.create(x, up, z)) ~= "Air" do
115 up += 1
116 end
117 -- (deep water, or a column nobody's near enough to have)
118 local open = world.block(vector.create(x, up, z)) == "Air" and world.block(vector.create(x, up - 1, z)) ~= "Air"
119 if open and (best == nil or up > (best :: vector).y) then
120 best = vector.create(x + 0.5, up, z + 0.5)
121 end
122 end
123 return best
124end
125
126-- Lightning in a storm, every so often, near someone to see it: in any
127-- storm, whoever's creation brought it.
128local function storm_again()
129 local now = sky.weather()
130 local wait = if now.kind == "storm" then world.random(6, 20) / math.max(0.3, now.strength) else 5
131 world.after(wait, function()
132 if sky.weather().kind == "storm" and knobs.storms then
133 local players = world.players()
134 if #players > 0 then
135 local p = players[math.floor(world.random(1, #players + 0.999))]
136 -- the Long Dusk's sky stays calm
137 local at = if world.place(p.position) == "dusk" then nil else tallest(p.position)
138 if at then
139 sky.strike(at)
140 end
141 end
142 end
143 storm_again()
144 end)
145end
146storm_again()