Fields of Mistria — Vanilla GML Reference FoM v1.0.8

Weather.gml

#macro WEATHER_PROTOTYPES global.__weather_prototypes
WEATHER_PROTOTYPES = undefined;

#macro WEATHER_COUNTS global.__weather_counts
WEATHER_COUNTS = undefined;

function load_weather() {
    var collection = array_create(Weather.LEN, undefined);
    var weather_data = fiddle_get("weather");
    var weather_default = weather_data[$ "default"];
    for (var i = 0; i < Weather.LEN; i++) {
        var weather = clone_value(weather_default);
        patch_object(weather_data[$ weather_to_string(i)], weather);
        filter_toml_nulls(weather);

        var music = array_create(Season.LEN, undefined);
        if weather.music != undefined {
            var default_track = is_string(weather.music) ? weather.music : undefined;
            music[Season.Spring] = weather.music[$ "spring"] ?? default_track;
            music[Season.Summer] = weather.music[$ "summer"] ?? default_track;
            music[Season.Fall] = weather.music[$ "fall"] ?? default_track;
            music[Season.Winter] = weather.music[$ "winter"] ?? default_track;
        }
        weather.music = music;

        collection[i] = weather;
    }
    return collection;
}

function load_weather_counts() {
    var output = array_create_ext(Season.LEN, function() {
        return array_create(ForecastGroup.LEN, undefined);
    });

    for (var season = 0; season < Season.LEN; season++) {
        output[season][ForecastGroup.Inclement] = fiddle_deserialize_vec2(fiddle_get(format("weather/seasonal_counts/{Season}/inclement", season)));
        output[season][ForecastGroup.Special] = fiddle_deserialize_vec2(fiddle_get(format("weather/seasonal_counts/{Season}/special", season)));
    }

    return output;
}

#macro WEATHER global.__weather
WEATHER = undefined;

function WeatherManager() constructor {
    self.forecast = undefined;
    self.weather = undefined;
    self.atmosphere = undefined;
    self.under_spell = false;
    self.spell_timer = undefined;

    function on_new_day() {
        self.under_spell = false;
        self.spell_timer = undefined;
        //
        //
        //
        //
        //
        //
        if CALENDAR.time == ARI.wedding_date  {
            self.forecast[CALENDAR.day()] = matches(CALENDAR.season(), Season.Spring, Season.Fall)
                ? Weather.Special
                : Weather.Calm;
        }
        self.set_weather(self.forecast[CALENDAR.day()]);
    }

    function is_inclement() {
        return matches(self.weather, Weather.Inclement, Weather.HeavyInclement);
    }

    function stop_atmosphere_sounds() {
        if self.atmosphere != undefined && self.atmosphere[$ "sound_instances"] != undefined {
            apply_func(self.atmosphere.sound_instances, function(v) {
                v.force_stop();
            });
        }
    }

    function set_atmosphere(atmosphere_id) {
        self.stop_atmosphere_sounds();

        if self.atmosphere != undefined {
            free_particle_renderables(self.atmosphere[$ "particles"]);
            free_particle_renderables(self.atmosphere[$ "clouds"]);
        }

        if atmosphere_id == undefined {
            self.atmosphere = undefined;
            return;
        }
        var func = atmosphere_function(atmosphere_id);
        self.atmosphere = func();
    }

	function set_weather(weather, override_atmosphere) {
        self.weather = weather;
		var atmosphere = override_atmosphere == undefined
            ? weather_to_atmosphere(weather, CALENDAR.time)
            : override_atmosphere;

        if matches(weather, Weather.Inclement, Weather.HeavyInclement) {
            if !self.under_spell {
                water_all(GRIDS[LocationId.Farm]);
            } else {
                water_all(GRID);
            }
        }

        self.set_atmosphere(atmosphere);
	}

	function on_room_start() {
		if self.under_spell {
            self.under_spell = false;
            self.spell_timer = undefined;
            self.set_weather(self.forecast[CALENDAR.day()]);
		}

        if self.atmosphere != undefined && self.atmosphere[$ "on_room_start"] != undefined {
            self.atmosphere.on_room_start();
        }
	}

	function step() {
    	if self.under_spell && self.spell_timer != undefined && self.spell_timer <= 0 {
            self.under_spell = false;
            self.spell_timer = undefined;
            self.set_weather(self.forecast[CALENDAR.day()]);
        }

        var location_can_draw = LOCATIONS[CURRENT_LOCATION_ID].outdoor
            || (self.under_spell && is_rain_spell_target(CURRENT_LOCATION_ID))

        if self.atmosphere == undefined || location_can_draw == false {
            return;
        }

        //
        if self.atmosphere.id != Atmosphere.HighClouds {
            update_atmosphere_particles(self.atmosphere);
        }

        if CURRENT_LOCATION_ID != LocationId.DeepWoods
            && (self.atmosphere.id == Atmosphere.HighClouds
            || self.atmosphere.id == Atmosphere.Petals
            || self.atmosphere.id == Atmosphere.Leaves)
        {
            for (var i = 0; i < array_length(self.atmosphere.clouds); i++) {
                var part = self.atmosphere.clouds[i];
                var renderable = part.renderable;

                var right = room_width() * (1.0 / dcos(part.angle));
                var bottom = room_height() * (1.0 / dsin(part.angle));

                var next_x = renderable.x + lengthdir_x(part.speed, part.angle);
                var next_y = renderable.y + lengthdir_y(part.speed, part.angle);

                var wrapped = next_x < -150 || next_x >= right || next_y < -100 || next_y >= bottom;

                renderable.x = wrap(next_x, right, -150);
                renderable.y = wrap(next_y, bottom, -100);

                //
                if wrapped {
                    renderable.teleport();
                }
            }
        }

        if matches(self.atmosphere.id, Atmosphere.Rain, Atmosphere.Storm) {
            var modifier = 1;
            if self.spell_timer != undefined {
                modifier = min(self.spell_timer / fiddle_get("spells/summon_rain/indoor_fade_length"), 1);
                self.spell_timer -= 1;
            }

            var droplets_needed = floor((array_length(self.atmosphere.particles) / 12) * modifier);
            if SETTINGS.get("weather_strength") == "low" || MIST.is_running() {
                droplets_needed /= 2;
            }
            if CURRENT_LOCATION_ID == LocationId.Summit || SETTINGS.get("weather_strength") == "none" {
                droplets_needed = 0;
            }

            while instance_number(obj_rain_droplet) < droplets_needed {
                instance_create_depth(0, 0, 0, obj_rain_droplet);
            }
            while instance_number(obj_rain_droplet) > droplets_needed {
                var inst = instance_find(obj_rain_droplet, irandom(instance_number(obj_rain_droplet) - 1));
                instance_destroy(inst);
            }

            if self.atmosphere.lightning != undefined && !MIST.running && !is_dungeon_room(room()) {
                self.atmosphere.lightning.timer -= 1;
                if self.atmosphere.lightning.timer <= 0 {
                    if !LOCATIONS[CURRENT_LOCATION_ID].outdoor {
                        TANGO.play("Ambience/IndoorThunder");
                    } else if irandom(1) == 0 {
                        TANGO.play("Ambience/OutdoorThunderFar");
                    } else {
                        TANGO.play("Ambience/OutdoorThunderClose");
                        self.atmosphere.lightning.current = 30;
                        self.atmosphere.lightning.power = random_range(0.75, 1.0);

                        var xx = irandom_range(CAMERA.cam_pos.x - 100, CAMERA.right() + 100);
                        var yy = irandom_range(CAMERA.cam_pos.y - 100, CAMERA.bottom());
                        instance_create_depth(xx, yy, floor(-yy), obj_lightning);
                    }

                    self.atmosphere.lightning.timer = irandom_range(
                        self.atmosphere.lightning.timer_min,
                        self.atmosphere.lightning.timer_max,
                    );
                }
            }

            //
            for (var i = 0; i < array_length(self.atmosphere.clouds); i++) {
                var part = self.atmosphere.clouds[i];
                var renderable = part.renderable;

                var right = room_width() * (1.0 / dcos(part.angle));

                var next_x = renderable.x + lengthdir_x(part.speed, part.angle);

                renderable.x = wrap(next_x, room_width() + 100.0, -100);

                if abs(renderable.x - next_x) > 100.0 {
                    renderable.teleport();
                }
            }
        }
	}

    function on_draw(frame_alpha) {
        var location_can_draw = LOCATIONS[CURRENT_LOCATION_ID].outdoor
            || (self.under_spell && is_rain_spell_target(CURRENT_LOCATION_ID))

        if self.atmosphere == undefined || location_can_draw == false {
            return;
        }

        switch self.atmosphere.id {
            case Atmosphere.Rain:
            case Atmosphere.Storm:
                gpu_set_depth_test(cmpfunc_always, false);

                //
                gpu_set_stencil_operation(StencilOperation.Replace);
                gpu_set_stencil_test(cmpfunc_greater, stencil_increment());

                gpu_set_color_write(false);
                for (var i = 0; i < array_length(self.atmosphere.clouds); i++) {
                    self.atmosphere.clouds[i].renderable.draw(c_white, 1, frame_alpha);
                }
                gpu_set_color_write(true);

                draw_sprite_ext(
                    spr_pixel,
                    0,
                    CAMERA.internal_cam_pos.x - CAMERA.x_buffer,
                    CAMERA.internal_cam_pos.y - CAMERA.y_buffer,
                    CAMERA.view_width + (CAMERA.x_buffer * 2),
                    CAMERA.view_height + (CAMERA.y_buffer * 2),
                    0,
                    make_color_rgb(12, 30, 30),
                    0.1,
                );
                gpu_disable_stencil();

                var modifier = 1;
                if self.spell_timer != undefined {
                    modifier = min(self.spell_timer / fiddle_get("spells/summon_rain/indoor_fade_length"), 1);
                }

                var xx = CAMERA.internal_cam_pos.x;
                var yy = CAMERA.internal_cam_pos.y;

                //
                draw_sprite_ext(spr_pixel, 0, xx, yy, CAMERA.view_width, CAMERA.view_height, 0, make_color_rgb(12, 100, 85), 0.40 * modifier);

                gpu_set_blendmode_ext(bm_src_alpha, bm_one);
                draw_atmosphere_particles(self.atmosphere, frame_alpha, modifier, make_color_rgb(69, 144, 142));

                if self.atmosphere.lightning != undefined && !MIST.running && !is_dungeon_room(room()) {
                    if self.atmosphere.lightning.current > 0 {
                        var alpha = SETTINGS.get("screen_flash")
                            ? (min(self.atmosphere.lightning.current, 20) / 30) * self.atmosphere.lightning.power
                            : 0;

                        if alpha != 0 {
                            draw_sprite_ext(spr_pixel, 0, xx, yy, CAMERA.view_width, CAMERA.view_height, 0, c_white, alpha);
                        }

                        self.atmosphere.lightning.current -= 1;
                    }
                }
                gpu_set_blendmode_ext(bm_src_alpha, bm_inv_src_alpha);
                gpu_set_depth_test(cmpfunc_always, true);
                break;
            case Atmosphere.HighClouds:
                if CURRENT_LOCATION_ID == LocationId.DeepWoods {
                    return;
                }

                var fade_in_start = fiddle_get("weather/high_cloud/fade_in_start");
                fade_in_start = hours(fade_in_start[0]) + minutes(fade_in_start[1]);

                var fade_in_end = fiddle_get("weather/high_cloud/fade_in_end");
                fade_in_end = hours(fade_in_end[0]) + minutes(fade_in_end[1]);

                var fade_out_start = fiddle_get("weather/high_cloud/fade_out_start");
                fade_out_start = hours(fade_out_start[0]) + minutes(fade_out_start[1]);

                var fade_out_end = fiddle_get("weather/high_cloud/fade_out_end");
                fade_out_end = hours(fade_out_end[0]) + minutes(fade_out_end[1]);

                var scalar = 1;
                if CLOCK.time < fade_in_end {
                    scalar = inverse_lerp(CLOCK.time, fade_in_end, fade_in_start);
                } else if CLOCK.time >= fade_out_start {
                    scalar = 1.0 - inverse_lerp(CLOCK.time, fade_out_end, fade_out_start);
                }
                var opacity = fiddle_get("weather/high_cloud/opacity") * clamp(scalar, 0, 1);
                if opacity < 0.001 {
                    return;
                }

                gpu_set_depth_test(cmpfunc_always, false);
                gpu_set_stencil_operation(StencilOperation.Replace);
                gpu_set_stencil_test(cmpfunc_greater, stencil_increment());

                var shadow_color = make_color_rgb(
                    round(255 * POST_PROCESS.shadow_multiply_color[0] * opacity),
                    round(255 * POST_PROCESS.shadow_multiply_color[1] * opacity),
                    round(255 * POST_PROCESS.shadow_multiply_color[2] * opacity),
                );
                //
                for (var i = 0; i < array_length(self.atmosphere.clouds); i++) {
                    self.atmosphere.clouds[i].renderable.draw(shadow_color, opacity, frame_alpha);
                }
                gpu_disable_stencil();
                gpu_set_depth_test(cmpfunc_always, true);
                break;
            case Atmosphere.Snow:
            case Atmosphere.Blizzard:
                gpu_set_depth_test(cmpfunc_always, false);
                var modifier = 1;
                if self.spell_timer != undefined {
                    modifier = min(self.spell_timer / fiddle_get("spells/summon_rain/indoor_fade_length"), 1);
                }

                var xx = CAMERA.internal_cam_pos.x;
                var yy = CAMERA.internal_cam_pos.y;

                var base = self.atmosphere.id == Atmosphere.Snow ? 0.2 : 0.25;

                //
                draw_sprite_ext(spr_pixel, 0, xx, yy, CAMERA.view_width, CAMERA.view_height, 0, make_color_rgb(12, 100, 85), base * modifier);
                draw_atmosphere_particles(self.atmosphere, frame_alpha);
                gpu_set_depth_test(cmpfunc_always, true);
                break;
            default:
                draw_atmosphere_particles(self.atmosphere, frame_alpha);
                break;
        }
    }

    function serialize() {
        return {
            forecast: array_map(self.forecast, weather_to_string),
        }
    }

    function deserialize(struct) {
        self.forecast = array_map(struct.forecast, string_to_weather);
    }

}

#macro ATMOS_PADDING 50

function create_particle_renderable(sprite, index, xx, yy) {
    var renderable = Game.create_renderable(sprite);
    renderable.visible = false;
    renderable.speed = 0.0;
    renderable.animation_frame = index;
    renderable.x = xx;
    renderable.y = yy;
    renderable.teleport();

    return renderable;
}

//
function free_particle_renderables(particles) {
    if particles == undefined {
        return;
    }

    for (var i = 0; i < array_length(particles); i++) {
        particles[i].renderable.free();
    }
}

function atmosphere_function(atmosphere) {
    switch atmosphere {
        case Atmosphere.HighClouds: return high_clouds;
        case Atmosphere.Rain: return rain;
        case Atmosphere.Storm: return function() {
            return rain(true);
        };
        case Atmosphere.Petals: return petals;
        case Atmosphere.Leaves: return leaves;
        case Atmosphere.Snow: return snow;
        case Atmosphere.Blizzard: return function() {
            return snow(true);
        };
        default: impossible("Unexpected Atmosphere: {Atmosphere}", atmosphere);
    }
}

function rain(is_storm=false) {
    var range = Vec2(20, 40);
    if SETTINGS.get("weather_strength") == "none" {
        return undefined;
    } else if SETTINGS.get("weather_strength") == "low" || MIST.is_running() {
        range = Vec2(50, 80);
    }
    var points = poisson_points(
        CAMERA.view_width + ATMOS_PADDING * 2,
        CAMERA.view_height + ATMOS_PADDING * 2,
        is_storm ? range.x : range.y,
    );

    var particles = apply_func(
        points,
        function(v) {
            var index = irandom(sprite_get_number(spr_rain_drop) - 1);

            return {
                angle: random_range(300, 320),
                speed: irandom_range(3, 6),
                anim_speed: 0,
                index,
                renderable: create_particle_renderable(
                    spr_rain_drop,
                    index,
                    v[0] + CAMERA.left(),
                    v[1] + CAMERA.top(),
                ),
            };
        }
    );

    var atmos =  {
        id: is_storm ? Atmosphere.Storm : Atmosphere.Rain,
        particles,
        sound_instances: [],
        lightning: !is_storm ? undefined : {
            current: 0,
            power: 0,
            timer: irandom_range(600, 1200),
            timer_max: 60 * 20,
            timer_min: 60 * 10,
        },
    };

    atmos.on_room_start = method(atmos, function() {
        for (var i = 0; i < array_length(self.sound_instances); i++) {
            self.sound_instances[i].force_stop();
        }
        self.sound_instances = [];
        var is_outdoor = LOCATIONS[CURRENT_LOCATION_ID].outdoor;
        if is_outdoor || WEATHER.under_spell {
            array_push(self.sound_instances, new SoundInstanceController(TANGO.play("Ambience/MildRain")));
            if self.id == Atmosphere.Storm {
                array_push(self.sound_instances, new SoundInstanceController(TANGO.play("Ambience/HardRainBase")));
            }
        } else if !is_dungeon_room(room()) && CURRENT_LOCATION_ID != LocationId.MinesEntry {
            array_push(self.sound_instances, new SoundInstanceController(TANGO.play("Ambience/IndoorRain")));
        }

        var points = poisson_points(
            room_width(),
            room_height(),
            200,
        );

        free_particle_renderables(self[$ "clouds"]);
        self.clouds = apply_func(
            points,
            function(v) {
                return {
                    angle: 0,
                    speed: 0.05,
                    renderable: create_particle_renderable(
                        spr_sky_holes,
                        irandom(sprite_get_number(spr_sky_holes) - 1),
                        v[0],
                        v[1],
                    ),
                }
            }
        );
    });

    atmos.on_room_start();

    return atmos;
}

function high_clouds() {
    if SETTINGS.get("weather_strength") == "none" {
        return undefined;
    }

    var atmos =  {
        id: Atmosphere.HighClouds,
    };

    atmos.on_room_start = method(atmos, function() {
        random_set_seed(CALENDAR.time);

        var range = fiddle_get("weather/high_cloud/range");

        if SETTINGS.get("weather_strength") == "low" {
            range[0] = round(range[0] * 0.5);
            range[1] = round(range[1] * 0.5);
        }

        var angle_range = fiddle_get("weather/high_cloud/angle");
        var speed_range = fiddle_get("weather/high_cloud/speed");

        var wrapper = {
            my_angle: irandom_range(angle_range[0], angle_range[1]),
            my_speed: random_range(speed_range[0], speed_range[1]),
        }

        var points = poisson_points(
            room_width() * 2,
            room_height() * 2,
            irandom_range(range[0], range[1])
        );

        free_particle_renderables(self[$ "clouds"]);
        self.clouds = apply_func(
            points,
            method(wrapper, function(v) {
                return {
                    angle: self.my_angle,
                    speed: self.my_speed,
                    renderable: create_particle_renderable(
                        spr_cloud_silhouette,
                        irandom(sprite_get_number(spr_cloud_silhouette) - 1),
                        v[0],
                        v[1],
                    ),
                }
            }),
        );

        randomize();
    });
    atmos.on_room_start();

    return atmos;
}

function petals() {
    var r = 100;
    if SETTINGS.get("weather_strength") == "none" {
        return undefined;
    } else if SETTINGS.get("weather_strength") == "low" || MIST.is_running() {
        r = 200;
    }
    var points = poisson_points(
        CAMERA.view_width + ATMOS_PADDING * 2,
        CAMERA.view_height + ATMOS_PADDING * 2,
        r,
    );

    var particles = apply_func(
        points,
        function(v) {
            var index = irandom(sprite_get_number(spr_part_petal) - 1);

            return {
                angle: random_range(300, 320),
                speed: 0.3,
                index,
                anim_speed: 0.16,
                renderable: create_particle_renderable(
                    spr_part_petal,
                    index,
                    v[0] + CAMERA.left(),
                    v[1] + CAMERA.top(),
                ),
            };
        }
    );

    var cloud_atmos = high_clouds();

    var output = {
        id: Atmosphere.Petals,
        clouds: cloud_atmos.clouds,
        particles,
    };
    output.on_room_start = method(output, cloud_atmos.on_room_start);

    return output;
}

function leaves() {
    var r = 100;
    if SETTINGS.get("weather_strength") == "none" {
        return undefined;
    } else if SETTINGS.get("weather_strength") == "low" || MIST.is_running() {
        r = 200;
    }
    var points = poisson_points(
        CAMERA.view_width + ATMOS_PADDING * 2,
        CAMERA.view_height + ATMOS_PADDING * 2,
        r,
    );

    var particles = apply_func(
        points,
        function(v) {
            var sprite = string_to_asset(format("spr_part_leaffall_multi_spin_v{}", irandom_range(1, 6)));
            var index = irandom(sprite_get_number(spr_part_leaffall_multi_spin_v1) - 1);

            return {
                angle: random_range(300, 320),
                index,
                speed: 0.3,
                anim_speed: 0.16,
                renderable: create_particle_renderable(
                    sprite,
                    index,
                    v[0] + CAMERA.left(),
                    v[1] + CAMERA.top(),
                ),
            };
        }
    );

    var cloud_atmos = high_clouds();

    var output = {
        id: Atmosphere.Leaves,
        clouds: cloud_atmos.clouds,
        particles,
    };
    output.on_room_start = method(output, cloud_atmos.on_room_start);

    return output;
}

function snow(is_blizzard=false) {
    var range = Vec2(20, 40);
    if SETTINGS.get("weather_strength") == "none" {
        return undefined;
    } else if SETTINGS.get("weather_strength") == "low" || MIST.is_running() {
        range = Vec2(50, 80);
    }
    var points = poisson_points(
        CAMERA.view_width + ATMOS_PADDING * 2,
        CAMERA.view_height + ATMOS_PADDING * 2,
        is_blizzard ? range.x : range.y,
    );

    var particles = [];
    for (var i = 0; i < array_length(points); i++) {
        var p = points[i];
        var index = irandom(sprite_get_number(spr_part_snow) - 1);

        array_push(particles, {
            angle: random_range(300, 320),
            speed: is_blizzard ? random_range(0.5, 2) : random_range(0.1, 0.3),
            index,
            anim_speed: 0,
            renderable: create_particle_renderable(
                spr_part_snow,
                index,
                p[0] + CAMERA.left(),
                p[1] + CAMERA.top(),
            ),
        });
    }

    return {
        id: is_blizzard ? Atmosphere.Blizzard : Atmosphere.Snow,
        particles,
    };
}

function update_atmosphere_particles(atmos) {
    var left = CAMERA.left() - ATMOS_PADDING;
    var top = CAMERA.top() - ATMOS_PADDING;
    var right = CAMERA.right() + ATMOS_PADDING;
    var bottom = CAMERA.bottom() + ATMOS_PADDING;

    for (var i = 0; i < array_length(atmos.particles); i++) {
        var part = atmos.particles[i];
        var renderable = part.renderable;

        var next_x = renderable.x + lengthdir_x(part.speed, part.angle);
        var next_y = renderable.y + lengthdir_y(part.speed, part.angle);

        var wrapped = next_x < left || next_x >= right || next_y < top || next_y >= bottom;

        renderable.x = wrap(next_x, right, left);
        renderable.y = wrap(next_y, bottom, top);

        //
        if wrapped {
            renderable.teleport();
        }

        part.index += part.anim_speed;
        renderable.animation_frame = part.index;
    }
}

function draw_atmosphere_particles(atmos, frame_alpha, alpha=1, color=c_white) {
    for (var i = 0; i < array_length(atmos.particles); i++) {
        atmos.particles[i].renderable.draw(color, alpha, frame_alpha);
    }
}

//
enum ForecastGroup {
    Calm,
    Inclement,
    Special,
    LEN
}

//
//
enum Weather {
    Calm,
    Inclement,
    HeavyInclement,
    Special,
    LEN,
}

//
enum Atmosphere {
    HighClouds,
    Rain,
    Storm,
    Petals,
    Leaves,
    Snow,
    Blizzard
}

//
function weather_tomorrow() {
    var next_day = CALENDAR.day() + 1;
    if next_day >= array_length(WEATHER.forecast) {
        return Weather.Calm;
    } else {
        return WEATHER.forecast[next_day];
    }
}

function weather_to_forecast_group(weather) {
    switch weather {
        case Weather.Calm:
            return ForecastGroup.Calm;
        case Weather.Inclement:
        case Weather.HeavyInclement:
            return ForecastGroup.Inclement;
        case Weather.Special:
            return ForecastGroup.Special;
        default: impossible();
    }
}

//
function ui_icon_for_weather(weather, season) {
    switch weather {
        case Weather.Calm: return spr_ui_hud_info_backplate_weather_icon_sunny;
        case Weather.Special: return season == Season.Spring
            ? spr_ui_hud_info_backplate_weather_icon_petals
            : spr_ui_hud_info_backplate_weather_icon_leaves;
        case Weather.Inclement: return season == Season.Winter
            ? spr_ui_hud_info_backplate_weather_icon_snow
            : spr_ui_hud_info_backplate_weather_icon_rain;
        case Weather.HeavyInclement: return season == Season.Winter
            ? spr_ui_hud_info_backplate_weather_icon_blizzard
            : spr_ui_hud_info_backplate_weather_icon_storm;
        default: impossible("Unexpected Weather: {}", weather);
    }
}


function weather_to_atmosphere(weather, time) {
    var is_winter = get_seasons(time) == Season.Winter;
    var is_fall = get_seasons(time) == Season.Fall;
    switch weather {
        case Weather.Calm: return Atmosphere.HighClouds;
        case Weather.Inclement: return is_winter ? Atmosphere.Snow : Atmosphere.Rain;
        case Weather.HeavyInclement: return is_winter ? Atmosphere.Blizzard : Atmosphere.Storm;
        case Weather.Special: return is_fall ? Atmosphere.Leaves : Atmosphere.Petals;
        default: impossible("Unexpected Weather: {Weather}", weather);
    }
}

function generate_forecast(season) {
    var forecast = array_create(28, Weather.Calm);
    var taken = array_create(28, false);
    var already_given = array_create(ForecastGroup.LEN, false);

    //
    taken[0] = true;

    //
    if CALENDAR.year() == 0 && season == Season.Spring {
        var first_week_of_weather = fiddle_get("misc/first_week_of_weather");

        for (var i = 0; i < 7; i++) {
            forecast[i] = string_to_weather(first_week_of_weather[i]);
            taken[i] = true;
            already_given[weather_to_forecast_group(forecast[i])] += 1;
        }
    }

    //
    for (var i = 0; i < FestivalId.LEN; i++) {
        var f = FESTIVALS[i];
        if f.prototype.date.season == season {
            forecast[f.prototype.date.day - 1] = f.prototype.forced_weather;
            taken[f.prototype.date.day - 1] = true;
            already_given[weather_to_forecast_group(forecast[f.prototype.date.day - 1])] += 1;
        }
    }

    //
    for (var i = 1; i < ForecastGroup.LEN; i++) {
        var amt = irandom_range(WEATHER_COUNTS[season][i].x, WEATHER_COUNTS[season][i].y) - already_given[i];
        if amt <= 0 {
            continue;
        }

        //
        repeat amt {
            //
            //
            //
            var offset = irandom_range(0, 27);
            var success = false;
            var signum = choose(-1, 1);
            for (var base_day = 0; base_day < 28; base_day++) {
                var j = abs((offset + signum * base_day) % 28);

                //
                //
                if taken[j]
                    || (i != ForecastGroup.Special && matches(j % Day.LEN, Day.Friday, Day.Saturday))
                {
                    continue;
                }

                var forecast_output;
                switch i {
                    case ForecastGroup.Inclement:
                        var below = try_index_array(forecast, j - 1);
                        var above = try_index_array(forecast, j + 1);

                        if below == Weather.HeavyInclement
                            || below == Weather.Inclement
                            || above == Weather.HeavyInclement
                            || above == Weather.Inclement
                        {
                            continue;
                        }

                        if chance_percent(60) {
                            forecast_output = Weather.HeavyInclement;
                        } else {
                            forecast_output = Weather.Inclement;
                        }
                        break;
                    case ForecastGroup.Special:
                        forecast_output = Weather.Special;

                        if try_index_array(forecast, j - 1) == Weather.Special
                            || try_index_array(forecast, j + 1) == Weather.Special
                        {
                            continue;
                        }

                        break;
                    default: impossible();
                }

                forecast[j] = forecast_output;
                taken[j] = true;
                success = true;
                break;
            }

            if success == false {
                error("we couldn't make as many `{ForecastGroup}` as we wanted to ({})", i, amt);
            }
        }
    }

    return forecast;
}

//
//
function is_rain_spell_target(location_id) {
    switch location_id {
        case LocationId.Dungeon:
        case LocationId.SmallGreenhouse:
        case LocationId.LargeGreenhouse:
            return true;
        default: return LOCATIONS[location_id].outdoor;
    }
}

//
function forecast_generation_test() {
    var _wm = new WeatherManager();
    for (var i = 0; i < Season.LEN; i++) {
        repeat 100 {
            generate_forecast(i);
        }
    }
}