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-- The climate: how likely each kind of weather is in each season, and how
3-- hard the wind blows. A remix can make its own.
4
5local world = require("@potluck/world@0.23")
6
7local climate = {}
8
9climate.SEASONS = { "spring", "summer", "autumn", "winter" }
10
11-- each season's odds for each kind of weather, as a share of its total
12climate.ODDS = {
13 spring = { clear = 3, cloudy = 3, rain = 3, storm = 0.6, fog = 1 },
14 summer = { clear = 6, cloudy = 2, rain = 1.5, storm = 1, fog = 0.3 },
15 autumn = { clear = 2.5, cloudy = 3, rain = 3, storm = 0.7, fog = 2 },
16 winter = { clear = 3, cloudy = 3, snow = 4, fog = 1.5 },
17} :: { [string]: { [string]: number } }
18
19-- how hard the wind blows in each kind, blocks a second: the least and the most
20climate.WIND = {
21 clear = { 0, 2 },
22 cloudy = { 1, 4 },
23 rain = { 2, 5 },
24 storm = { 6, 11 },
25 snow = { 1, 4 },
26 fog = { 0, 1 },
27} :: { [string]: { number } }
28
29-- A kind of weather for a season, by its odds: no storms when `storms` is
30-- false, and what falls in winter is snow, rain the rest of the year.
31function climate.pick(season: string, storms: boolean): string
32 local odds = climate.ODDS[season] or climate.ODDS.spring
33 -- in a set order, so a world's weather goes the same way from a seed
34 local kinds = {}
35 for kind in odds do
36 if kind ~= "storm" or storms then
37 table.insert(kinds, kind)
38 end
39 end
40 table.sort(kinds)
41 local total = 0
42 for _, kind in kinds do
43 total += odds[kind]
44 end
45 local roll = world.random() * total
46 local kind = kinds[#kinds] or "clear"
47 for _, k in kinds do
48 roll -= odds[k]
49 if roll <= 0 then
50 kind = k
51 break
52 end
53 end
54 if season == "winter" and kind == "rain" then
55 return "snow"
56 elseif season ~= "winter" and kind == "snow" then
57 return "rain"
58 end
59 return kind
60end
61
62-- The wind for a kind of weather: blocks a second along x and z, any way.
63function climate.wind(kind: string): { number }
64 local w = climate.WIND[kind] or climate.WIND.clear
65 local speed = world.random(w[1], w[2])
66 local turn = world.random() * 2 * math.pi
67 return { math.cos(turn) * speed, math.sin(turn) * speed }
68end
69
70return climate