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 seasons' own tests: `potluck-server --test=weather`. |
| 3 | |
| 4 | local test = require("@potluck/test@0.8") |
| 5 | local world = require("@potluck/world@0.23") |
| 6 | local sky = require("@potluck/sky@0.1") |
| 7 | local store = require("@potluck/store") |
| 8 | local climate = require("./climate") |
| 9 | |
| 10 | -- the sun goes around once: past midnight |
| 11 | local function a_day_passes() |
| 12 | test.daytime(0.9) |
| 13 | test.wait(1.2) |
| 14 | test.daytime(0.05) |
| 15 | test.wait(1.2) |
| 16 | end |
| 17 | |
| 18 | test.case("a week makes a season, and four of them a year", function() |
| 19 | local name, progress = sky.season() |
| 20 | test.same(name, "spring", "a new world starts in spring") |
| 21 | test.check(progress > 0.25 and progress < 0.45, "after the thaw: " .. progress) |
| 22 | for _ = 1, 3 do |
| 23 | a_day_passes() |
| 24 | end |
| 25 | name, progress = sky.season() |
| 26 | test.same(name, "spring", "still spring after three days") |
| 27 | test.check(progress > 0.7 and progress < 0.9, "and most of the way through it: " .. progress) |
| 28 | for _ = 1, 4 do |
| 29 | a_day_passes() |
| 30 | end |
| 31 | test.same((sky.season()), "summer", "a week on, summer") |
| 32 | for _ = 1, 14 do |
| 33 | a_day_passes() |
| 34 | end |
| 35 | test.same((sky.season()), "winter", "then autumn, then winter") |
| 36 | for _ = 1, 7 do |
| 37 | a_day_passes() |
| 38 | end |
| 39 | test.same((sky.season()), "spring", "and around to spring again") |
| 40 | test.same(store.get("days"), 30, "the days are kept with the world") |
| 41 | end) |
| 42 | |
| 43 | test.case("it snows in winter, never rains, and summer is mostly fine", function() |
| 44 | local seen: { [string]: { [string]: number } } = {} |
| 45 | for _, season in climate.SEASONS do |
| 46 | local counts: { [string]: number } = {} |
| 47 | for _ = 1, 400 do |
| 48 | local kind = climate.pick(season, true) |
| 49 | counts[kind] = (counts[kind] or 0) + 1 |
| 50 | end |
| 51 | seen[season] = counts |
| 52 | end |
| 53 | test.same(seen.winter.rain, nil, "no rain in winter") |
| 54 | test.same(seen.winter.storm, nil, "nor storms") |
| 55 | test.check((seen.winter.snow or 0) > 100, "but snow: " .. tostring(seen.winter.snow)) |
| 56 | test.same(seen.summer.snow, nil, "no snow in summer") |
| 57 | test.check((seen.summer.clear or 0) > 200, "summer's mostly clear: " .. tostring(seen.summer.clear)) |
| 58 | test.check((seen.autumn.fog or 0) > (seen.summer.fog or 0) * 2, "autumn's foggier than summer") |
| 59 | for _ = 1, 200 do |
| 60 | test.check(climate.pick("summer", false) ~= "storm", "no storms when they're off") |
| 61 | end |
| 62 | local w = climate.wind("storm") |
| 63 | local speed = math.sqrt(w[1] * w[1] + w[2] * w[2]) |
| 64 | test.check(speed >= 6 and speed <= 11, "a storm's wind is strong: " .. speed) |
| 65 | end) |
| 66 | |
| 67 | test.case("a storm throws lightning near players, at the top of what's there", function() |
| 68 | local p = test.player("Ada") |
| 69 | local struck: { vector } = {} |
| 70 | sky.on("strike", function(at: vector) |
| 71 | table.insert(struck, at) |
| 72 | end) |
| 73 | sky.set_weather("storm", { strength = 1, over = 0 }) |
| 74 | test.same(sky.weather().kind, "storm") |
| 75 | test.wait(25) |
| 76 | test.check(#struck >= 1, "lightning struck: " .. #struck) |
| 77 | for _, at in struck do |
| 78 | local d = at - p.position |
| 79 | test.check(math.sqrt(d.x * d.x + d.z * d.z) <= 50, "near the player") |
| 80 | test.same(world.block(at), "Air", "just above what it hit") |
| 81 | test.check(world.block(at - vector.create(0, 1, 0)) ~= "Air", "on top of something") |
| 82 | end |
| 83 | -- and none once it clears |
| 84 | sky.set_weather("clear", { over = 0 }) |
| 85 | test.wait(1) |
| 86 | local before = #struck |
| 87 | test.wait(25) |
| 88 | test.same(#struck, before, "no lightning without a storm") |
| 89 | end) |
| 90 | |
| 91 | test.case("lightning is rate-limited, and every creation hears where it struck", function() |
| 92 | local heard = 0 |
| 93 | sky.on("strike", function() |
| 94 | heard += 1 |
| 95 | end) |
| 96 | local made = 0 |
| 97 | for _ = 1, 6 do |
| 98 | if sky.strike(vector.create(10, 60, 10)) then |
| 99 | made += 1 |
| 100 | end |
| 101 | end |
| 102 | test.same(made, 3, "three at once") |
| 103 | test.wait(0.3) |
| 104 | test.same(heard, 3, "and each one heard") |
| 105 | test.wait(1.2) |
| 106 | test.check(sky.strike(vector.create(10, 60, 10)), "another a second later") |
| 107 | -- and what's said about the weather is checked |
| 108 | test.check(not pcall(function() |
| 109 | sky.set_weather("sleet" :: any) |
| 110 | end), "there's no sleet") |
| 111 | test.check(not pcall(function() |
| 112 | sky.set_weather("rain", { strength = 2 }) |
| 113 | end), "strength is 0 to 1") |
| 114 | sky.set_weather("rain", { strength = 0.4, wind = vector.create(3, 0, 4) }) |
| 115 | local now = sky.weather() |
| 116 | test.same(now.kind, "rain") |
| 117 | test.check(math.abs(now.strength - 0.4) < 1e-6, "as heavy as it said") |
| 118 | test.check(math.abs(now.wind.x - 3) < 1e-6 and math.abs(now.wind.z - 4) < 1e-6 and now.wind.y == 0, "the wind as it said") |
| 119 | sky.set_season("autumn", 0.5) |
| 120 | local name, progress = sky.season() |
| 121 | test.same(name, "autumn") |
| 122 | test.same(progress, 0.5) |
| 123 | end) |