From cd5acdb07f498be317dc3e7bccbf78cc5882dc0f Mon Sep 17 00:00:00 2001 From: Overlord Date: Sat, 29 Aug 2026 17:09:16 +0200 Subject: [PATCH] add weather channel functionality for configurable forecasts and shutoff announcements --- .../lua/server/AEBSMisc/AEBSMisc_Options.lua | 21 ++ .../AEBSMisc/AEBSMisc_WeatherChannel.lua | 317 ++++++++++++++++++ 2 files changed, 338 insertions(+) create mode 100644 Contents/mods/AEBSMisc/common/media/lua/server/AEBSMisc/AEBSMisc_Options.lua create mode 100644 Contents/mods/AEBSMisc/common/media/lua/server/AEBSMisc/AEBSMisc_WeatherChannel.lua diff --git a/Contents/mods/AEBSMisc/common/media/lua/server/AEBSMisc/AEBSMisc_Options.lua b/Contents/mods/AEBSMisc/common/media/lua/server/AEBSMisc/AEBSMisc_Options.lua new file mode 100644 index 0000000..d52ecc8 --- /dev/null +++ b/Contents/mods/AEBSMisc/common/media/lua/server/AEBSMisc/AEBSMisc_Options.lua @@ -0,0 +1,21 @@ +AEBSMisc_Options = {} + +---Mirrors the defaults declared in sandbox-options.txt. +local DEFAULTS = { + Units = 1, -- 1 = Metric, 2 = Imperial, 3 = Both + AnnouncePowerShutoff = true, + AnnounceWaterShutoff = true, +} + +---@param name string an AEBSMisc.* sandbox option name, without the "AEBSMisc." prefix +---@return any +function AEBSMisc_Options.get(name) + local sv = SandboxVars and SandboxVars.AEBSMisc + local value = sv and sv[name] + + if value == nil then + return DEFAULTS[name] + end + + return value +end diff --git a/Contents/mods/AEBSMisc/common/media/lua/server/AEBSMisc/AEBSMisc_WeatherChannel.lua b/Contents/mods/AEBSMisc/common/media/lua/server/AEBSMisc/AEBSMisc_WeatherChannel.lua new file mode 100644 index 0000000..e486452 --- /dev/null +++ b/Contents/mods/AEBSMisc/common/media/lua/server/AEBSMisc/AEBSMisc_WeatherChannel.lua @@ -0,0 +1,317 @@ +---@diagnostic disable: param-type-mismatch +require("AEBSMisc.AEBSMisc_Options") + +--- Forecast types accepted by WeatherChannel.GetForecastString, named +--- so call sites don't have to carry the vanilla magic numbers. +local FORECAST = { + TEMPERATURE = 1, + WIND = 2, + FOG = 3, + WEATHER_START = 4, + WEATHER_END = 5, +} + +-- Hardcoded rather than routed through getText(): +-- GameServer.java:562 calls Translator.loadFiles() (which merges each +-- mod's Translate/*.json into the running translation tables) BEFORE +-- GameServer.java:607 even reads ServerOptions.mods into the active +-- mod list, so a dedicated server merges mod translations before it +-- knows which mods are active, and never repeats the merge afterward. + +local WATER_SHUTOFF_LINES = { + "Knox County Water Authority: Pressure irregularities reported across the grid.", + "Knox County Water Authority: Pumping stations offline. Contamination risk.", + "Knox County Water Authority: Water supply terminated.", +}; + +local POWER_SHUTOFF_LINES = { + getText("AEBS_Power_1"), + getText("AEBS_Power_2"), + getText("AEBS_Power_3"), +}; + +-- + +---@param _val number +---@return string +local function formatRounded(_val) + return tostring(ISDebugUtils.roundNum(_val, 2)); +end + +---@param _val number +---@return string +local function formatWholeNumber(_val) + return tostring(ISDebugUtils.roundNum(_val, 0)); +end + +--- Renders a Celsius value per the AEBSMisc.Units sandbox option: +--- Metric ("20.4 °C"), Imperial ("68.7 °F"), or Both ("20.4 °C (68.7 °F)"). +--- @param _celsius number +--- @return string +local function formatTemperature(_celsius) + local mode = AEBSMisc_Options.get("Units"); + local c = tostring(ISDebugUtils.roundNum(_celsius, 1)) .. " " .. Temperature.getCelsiusPostfix(); + + if mode == 1 then return c; end + + local f = tostring( + ISDebugUtils.roundNum(Temperature.CelsiusToFahrenheit(_celsius), 1) + ) .. " " .. Temperature.getFahrenheitPostfix(); + + if mode == 2 then + return f; + else + return c .. " (" .. f .. ")"; + end +end + +--- Renders a wind-power value per the AEBSMisc.Units sandbox option: +--- Metric ("14 km/h"), Imperial ("9 mph"), or Both ("14 km/h (9 MpH)"). +--- @param _windPower number +--- @return string +local function formatWindSpeed(_windPower) + local mode = AEBSMisc_Options.get("Units"); + local kph = formatRounded(ClimateManager.ToKph(_windPower)) .. getText("AEBS_KpH"); + + if (mode == 1) then return kph; end + + local mph = formatRounded(ClimateManager.ToMph(_windPower)) .. getText("AEBS_MpH"); + + if mode == 2 then + return mph; + else + return kph .. " (" .. mph .. ")"; + end +end + +--- Adds the three escalating radio lines (early warning -> critical -> shutoff) +--- for a utility's shutoff timeline, based on in-game days remaining until +--- _shutDay. Shared by AddPowerNotice and AddWaterNotice. +--- @param _lines string[] exactly 3 lines: early warning, critical, final shutoff +--- @param _shutDay number sandbox day the shutoff occurs +--- @param _c table color {r, g, b} +--- @param _bc RadioBroadCast +--- @param _force boolean|nil add all three lines regardless of the current day +local function addShutoffNotice(_lines, _shutDay, _c, _bc, _force) + local day = getGameTime():getWorldAgeHours() / 24 + (getSandboxOptions():getTimeSinceApo() - 1) * 30; + + if _force or (day >= _shutDay-2 and day < _shutDay-1) then + _bc:AddRadioLine( RadioLine.new(_lines[1], _c.r, _c.g, _c.b) ); + end + if _force or (day >= _shutDay-1 and day < _shutDay) then + _bc:AddRadioLine( RadioLine.new(_lines[2], _c.r, _c.g, _c.b) ); + end + if _force or (day >= _shutDay) then + _bc:AddRadioLine( RadioLine.new(_lines[3], _c.r, _c.g, _c.b) ); + end +end + +--- Vanilla override: adds the escalating Knox Power Grid warning lines +--- (fluctuations -> failing -> blackout) based on days until the sandbox +--- electricity-shutoff day, gated by the AnnouncePowerShutoff sandbox option. +--- @param _c table color {r, g, b} +--- @param _bc RadioBroadCast +--- @param _force boolean|nil add all three lines regardless of the current day +function WeatherChannel.AddPowerNotice(_c, _bc, _force) + if not AEBSMisc_Options.get("AnnouncePowerShutoff") then return; end + + addShutoffNotice(POWER_SHUTOFF_LINES, getSandboxOptions():getElecShutModifier(), _c, _bc, _force); +end + +---New: mirrors AddPowerNotice but for the sandbox water-shutoff day, +---gated by the AnnounceWaterShutoff sandbox option. +---@param _c table color {r, g, b} +---@param _bc RadioBroadCast +---@param _force boolean|nil add all three lines regardless of the current day +function WeatherChannel.AddWaterNotice(_c, _bc, _force) + if not AEBSMisc_Options.get("AnnounceWaterShutoff") then return; end + + addShutoffNotice(WATER_SHUTOFF_LINES, getSandboxOptions():getWaterShutModifier(), _c, _bc, _force); +end + +---@param _forecast ClimateForecaster.DayForecast +---@return string +local function buildTemperatureForecast(_forecast) + local v = _forecast:getTemperature(); + local mean, min, max = v:getTotalMean(), v:getTotalMin(), v:getTotalMax(); + local humidity = formatWholeNumber(_forecast:getHumidity():getTotalMean()*100); + + return " " .. getText("AEBS_temperature", formatTemperature(mean), formatTemperature(min), formatTemperature(max), humidity); +end + +---@param meanWindPower number +---@return string +local function describeWindStrength(meanWindPower) + if meanWindPower > 0.75 then + return getText("AEBS_wind_4") + elseif meanWindPower > 0.5 then + return getText("AEBS_wind_3") + elseif meanWindPower > 0.25 then + return getText("AEBS_wind_2") + else + return getText("AEBS_wind_1") + end +end + +---@param _forecast ClimateForecaster.DayForecast +---@return string +local function describeCloudiness(_forecast) + local cloudiness = _forecast:getCloudiness(); + local mean, max = cloudiness:getTotalMean(), cloudiness:getTotalMax(); + local description = getText("AEBS_clouds_0"); + + if mean > 0.7 then + description = getText("AEBS_clouds_2"); + elseif mean > 0.4 then + description = getText("AEBS_clouds_1"); + if max > 0.7 then + description = description .. " " .. getText("AEBS_clouds_4"); + end + else + if max > 0.7 then + description = description .. " " .. getText("AEBS_clouds_4"); + elseif max > 0.4 then + description = description .. " " .. getText("AEBS_clouds_3"); + end + end + + return description; +end + +---@param _forecast ClimateForecaster.DayForecast +---@return string +local function buildWindForecast(_forecast) + local v = _forecast:getWindPower(); + local meanPower, maxPower = v:getTotalMean(), v:getTotalMax(); + local direction = _forecast:getMeanWindAngleString(); + local directionText = getText("AEBS_zone_name_" .. direction:lower()); + + return getText("AEBS_wind_0", describeWindStrength(meanPower), directionText, formatWindSpeed(maxPower), describeCloudiness(_forecast)); +end + +---@param _forecast ClimateForecaster.DayForecast +---@return string +local function buildFogForecast(_forecast) + local v = _forecast:getFogStrength(); + if v == 1 then + return getText("AEBS_fog_2"); + elseif v > 0.75 then + return getText("AEBS_fog_1"); + else + return getText("AEBS_fog_0"); + end +end + +---@param _forecast ClimateForecaster.DayForecast +---@return string +local function describePredictedWeatherEvents(_forecast) + local t = {}; + + if _forecast:isHasHeavyRain() then + table.insert(t, getText("AEBS_weather_heavy_rain")); + end + if _forecast:isHasStorm() then + table.insert(t, getText("AEBS_weather_storm")); + end + if _forecast:isHasTropicalStorm() then + table.insert(t, getText("AEBS_weather_tropical")); + end + if _forecast:isHasBlizzard() then + table.insert(t, getText("AEBS_weather_blizzard")); + end + + if #t > 0 then + if #t == 1 then + return getText("AEBS_weather_predicted") .. t[1]; + else + local s = getText("AEBS_weather_predicted"); + for k, v in ipairs(t) do + if k <# t then + s = s .. v .. (v ~= #t - 1 and ", " or ""); + else + s = s .. getText("AEBS_weather_and_a") .. v .. "..."; + end + end + + return s; + end + else + return getText("AEBS_weather_light_moderate"); + end +end + +---@param _type integer FORECAST.WEATHER_START or FORECAST.WEATHER_END +---@param _forecast ClimateForecaster.DayForecast +---@return string +local function buildWeatherEventForecast(_type, _forecast) + local s; + if _type == FORECAST.WEATHER_START then + s = getText("AEBS_weather_0_a", WeatherChannel.GetDaySegmentForHour(_forecast:getWeatherStartTime())); + else + local endTime = _forecast:getWeatherEndTime(); + if endTime >= 22 then + s = getText("AEBS_weather_0_b"); + else + s = getText("AEBS_weather_0_c", WeatherChannel.GetDaySegmentForHour(endTime)); + end + end + + s = s .. describePredictedWeatherEvents(_forecast); + + if _forecast:isChanceOnSnow() then + s = s..getText("AEBS_weather_snowfall"); + end + + return s; +end + +---Vanilla override: only the temperature and wind-speed builders differ, +---rendering via the AEBSMisc.Units sandbox option instead of the client-only +---getOptionDisplayAsCelsius(). +---@param _forecast ClimateForecaster.DayForecast +---@return string +function WeatherChannel.GetForecastString(_type, _forecast) + if _type == FORECAST.TEMPERATURE then + return buildTemperatureForecast(_forecast); + elseif _type == FORECAST.WIND then + return buildWindForecast(_forecast); + elseif _type == FORECAST.FOG then + return buildFogForecast(_forecast); + elseif _type == FORECAST.WEATHER_START or _type == FORECAST.WEATHER_END then + return buildWeatherEventForecast(_type, _forecast); + else + return ""; + end +end + +---Vanilla override: identical structure to the original, +---with an added AddWaterNotice call alongside AddPowerNotice. +---@param _gametime GameTime +---@param _bc RadioBroadCast +function WeatherChannel.FillBroadcast(_gametime, _bc) + local hour = _gametime:getHour(); + local c = { r = 1.0, g = 1.0, b = 1.0 }; + + _bc:AddRadioLine(RadioLine.new(getText("AEBS_Intro"), c.r, c.g, c.b)); + + WeatherChannel.AddFuzz(c, _bc); + + ---@diagnostic disable-next-line: missing-parameter + WeatherChannel.AddPowerNotice(c, _bc); + WeatherChannel.AddWaterNotice(c, _bc); + + WeatherChannel.GetRandomString(c, _bc, 100); + WeatherChannel.AddFuzz(c, _bc); + + WeatherChannel.AddForecasting(c, _bc, hour); + WeatherChannel.AddFuzz(c, _bc); + + WeatherChannel.GetRandomString(c, _bc, 100); + + if getGameTime():getNightsSurvived() == getGameTime():getHelicopterDay1() then + WeatherChannel.AddFuzz(c, _bc, 6); + _bc:AddRadioLine(RadioLine.new(getText("AEBS_Choppah"), c.r, c.g, c.b)); + end + + WeatherChannel.AddFuzz(c, _bc); +end