Weather: rain, storms, snow and fog

Since API 4 a script mod can change the hunt's weather: rain around the hunter, thunderstorms with lightning and thunder, snow (in Dinosaur Hunter too), fog and a cloud cover over the sky. Presets fade all of it in and out together with the map's own weather (the fog, the sky, sun and shadow colours of world.weather), so a hunt can turn stormy over a minute and clear up again.

on("hunt_start", function()
  weather.set("overcast")                                    -- at once
  timer.after(30, function() weather.set("storm", 60) end)   -- the storm rolls in over a minute
  timer.after(180, function() weather.set("clear", 90) end)  -- and goes again
end)

Nothing changes in the game until a mod calls weather.*: without such a mod there is no rain, no sound, no extra drawing, and the game plays and looks exactly as it does without mods.

Presets

weather.set(preset, seconds) fades to a preset in seconds (0 or none: at once). Every preset is made from the map's own weather, so a night map stays a night map and Ice Age's cold light stays cold:

Preset What it is
map the map's own weather, as it was before the first weather.* call on this map (Ice Age: with its snow)
clear the same, without rain, snow or lightning
overcast grey clouds over the sky, a softer, dimmer sun, the fog a little closer
fog thick fog from the hunter on (fogStart 0), the sky the fog's colour
rain rain (0.7), a dark grey sky, a dim, wet light, closer fog, fast clouds
storm heavy rain, lightning (6 strikes a minute), a dark sky and closer fog
snow snowfall (0.6), a white-grey sky with a cold tint
blizzard thick snow (1) and close fog

weather.presets() lists them, weather.current() tells where the weather is and where it is going:

local w = weather.current()
print(w.preset, w.fade, w.rain, w.target.rain)   -- storm 31.5 0.62 1

One value at a time

weather.rain, weather.snow, weather.lightning and weather.cover read a value without an argument and fade to one with it; nil puts the map's own back (none, or Ice Age's snow):

weather.rain(1, 20)       -- heavier rain over 20 seconds
weather.lightning(12)     -- twelve strikes a minute, at once
weather.snow(nil, 30)     -- the snow stops over half a minute
print(weather.rain())     -- the rain now (during a fade: on its way)
Value Range What it does
rain 0-1 rain streaks around the hunter, slanted by the wind (world.wind), splashes on the ground and the water near him, the rain's sound; on a bright sky it brings a darker, greyer, wetter light and clouds of its own
snow 0-1 snowflakes around the hunter (1 = 2048 flakes, as Ice Age's snow_density), drifting with the wind and lying on the ground for two seconds
lightning 0-60 random strikes per minute
cover 0-1 clouds over the whole sky in the sky's colour (1: the sky is the fog's colour)

A table sets several values at once. Without preset it changes only the values it names (the rest goes on as it goes); with one, it is that preset with these values changed. Colours are Colors ("#405848", {r =, g =, b =}):

weather.set({preset = "rain", rain = 0.4, skyColor = "#405848", fogEnd = 8000}, 20)  -- a green, misty rain
weather.set({lightning = 3}, 10)                                                     -- only the lightning

The fields of a table and of weather.current(): fogStart, fogEnd, skyColor, sunColor, shadowColor, skyPlaneColor, cloudsSpeed (the map's, as in world.weather), cover, rain, snow, lightning.

Lightning and thunder

A strike lights up the sky and the land for a moment (the sky, sun and shadow colours flash: creatures, the hunter and the ground with them), draws a bolt from the clouds to the ground and sends its thunder after distance / 128 / 343 seconds: sound travels 343 m a second and 128 units are a metre. Near strikes crack, far ones rumble.

local x, _, z = player.pos()
local fx, fz = player.forward()
local s = weather.strike(x + fx * 60000, z + fz * 60000)   -- 470 m ahead
print(s.distance, s.delay)                                  -- 60000 1.37

on("lightning", function(e)
  if e.distance < 20000 then game.message("That was close!") end
  -- e.cancelled = true: no flash, no bolt, no thunder
end)

weather.strike() without a place picks one around the hunter, as weather.lightning's random strikes do: mostly a few hundred metres to two kilometres away, now and then close. Strikes far away are drawn on the sky in their direction, so hills and trees in front of them still hide them. Lightning does not hurt or scare anything by itself; a mod can do that in its lightning handler.

Sounds

The rain's sound grows and fades with the rain (quieter under water, silent while the game is paused); thunder comes with each strike. The game ships no weather sounds: they are made when the game runs. To use your own, put a rain.wav (it loops) and a thunder.wav into your mod's folder: the game finds them by name, as every file of a mod (Mod folders).

What lasts how long

The weather is a definition, like world.weather (Game data):

weather.* works in a hunt only (outside a hunt weather.set raises an error and weather.current() is nil).

With world.weather

Both change the same fields. world.weather reads what is drawn: the weather's values with the rain's light and a lightning flash on top; weather.current() reads the weather's own values. A field written through world.weather is the weather's from then on: a fade of that field ends there, and the next weather.set starts from it. In Ice Age world.weather.snow is the same as weather.snow(); in Dinosaur Hunter it is now the weather's snowfall too (it was nil before API 4).

Multiplayer

The weather is each player's own, as world.weather: a mod that wants it the same for everyone sends its presets with net and calls weather.set on each side. Random lightning strikes in different places for each player.

Example: Storm Chaser

The hunt's weather changes by itself: clouds gather, a thunderstorm rolls in over a minute and clears again, then fog (Ice Age: snow and a blizzard). F3 (on a phone: WEATHER) switches to the next weather, F4 (STRIKE) calls lightning ahead, F5 (AUTO) stops and restarts the changes.

-- Storm Chaser: the hunt's weather changes by itself. Clouds gather, a thunderstorm rolls in over a minute with rain and
-- lightning, then it clears; later fog, or snow (Ice Age: snow and a blizzard, then a storm). F3 switches to the next
-- weather (phone: WEATHER), F4 calls a lightning strike somewhere ahead (STRIKE), F5 stops and restarts the changes
-- (AUTO). Lightning close by gets a message.
-- Shows (API 4): weather.set with fades, weather.current, weather.strike, the lightning event; keys with touch buttons;
-- the map's own weather back by itself when the mod is switched off.

local IA = game.id() == "iceage"

-- the program: each step fades to a preset in `fade` seconds and keeps it `hold` seconds
local PROGRAM = IA and {
  {preset = "map", fade = 0, hold = 30},
  {preset = "overcast", fade = 30, hold = 30},
  {preset = "snow", fade = 40, hold = 60},
  {preset = "blizzard", fade = 30, hold = 40},
  {preset = "overcast", fade = 40, hold = 30},
  {preset = "storm", fade = 60, hold = 90},
  {preset = "clear", fade = 60, hold = 90},
} or {
  {preset = "map", fade = 0, hold = 30},
  {preset = "overcast", fade = 30, hold = 30},
  {preset = "storm", fade = 60, hold = 90},
  {preset = "rain", fade = 30, hold = 60},
  {preset = "clear", fade = 60, hold = 90},
  {preset = "fog", fade = 40, hold = 60},
  {preset = "clear", fade = 40, hold = 90},
}
-- F3 goes through these
local CYCLE = {"clear", "overcast", "rain", "storm", "fog", "snow", "blizzard", "map"}

local step, auto, timerId = 0, true, nil
local strikes, closest = 0, nil

local function say(text, color) game.message(text, 4, color or "#c8d8ff") end

local function nextStep()
  timerId = nil
  if not auto or not game.inHunt() then return end
  step = step % #PROGRAM + 1
  local s = PROGRAM[step]
  weather.set(s.preset, s.fade)
  if s.fade > 0 then say("The weather turns: " .. s.preset .. " in " .. s.fade .. " s") end
  timerId = timer.after(s.fade + s.hold, nextStep)
end

local function startProgram()
  if timerId then timer.cancel(timerId) end
  step = 0
  nextStep()
end

on("hunt_start", function()
  strikes, closest = 0, nil
  auto = true
  startProgram()
end)

-- F3: the next weather of the list, over 8 seconds (the program pauses)
keys.bind("F3", function()
  if not game.inHunt() then return end
  auto = false
  if timerId then timer.cancel(timerId); timerId = nil end
  local now = weather.current().preset
  local i = 0
  for k, name in ipairs(CYCLE) do if name == now then i = k end end
  local preset = CYCLE[i % #CYCLE + 1]
  weather.set(preset, 8)
  say("Weather: " .. preset .. " (F5: back to the automatic changes)")
end, {touch = {label = "WEATHER"}})

-- F4: a strike half a kilometre to two kilometres ahead
keys.bind("F4", function()
  if not game.inHunt() then return end
  local x, _, z = player.pos()
  local fx, fz = player.forward()
  local d = math.random(64000, 256000)
  local side = math.random(-30000, 30000)
  weather.strike(x + fx * d - fz * side, z + fz * d + fx * side)
end, {touch = {label = "STRIKE"}})

-- F5: the automatic changes off / on again (from the start of the program)
keys.bind("F5", function()
  if not game.inHunt() then return end
  auto = not auto
  if auto then
    startProgram()
    say("Automatic weather on")
  else
    if timerId then timer.cancel(timerId); timerId = nil end
    say("Automatic weather off: it stays " .. weather.current().preset)
  end
end, {touch = {label = "AUTO"}})

-- every strike, the game's random ones and F4's: a message when it is close (distances: 128 units = 1 m)
on("lightning", function(e)
  strikes = strikes + 1
  local metres = math.floor(e.distance / 128)
  if not closest or metres < closest then closest = metres end
  if metres < 400 then
    say(string.format("Lightning %d m away! Thunder in %.1f s", metres, e.delay), "#fff0a0")
  end
end)

on("hunt_end", function()
  if timerId then timer.cancel(timerId); timerId = nil end
  if strikes > 0 then print(string.format("%d strikes this hunt, the closest %d m away", strikes, closest)) end
end)

Performance

Rain is up to 3000 streaks and 768 splashes, snow up to 2048 flakes, all computed on the CPU and drawn with the game's own particle shader in a few draw calls. It costs about as much as Ice Age's snow; on slow phones keep rain below 1.