Lua function to return readable weather?

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
Whicker
Posts: 664
Joined: Tue Jun 19, 2018 9:54 pm

Lua function to return readable weather?

Post by Whicker »

has anyone posted something where you could pass in the current weather and get back a readable weather report? with what each number translates to? like if rain is 20 it would say `moderate rain`.

I want to say I saw something about this a while ago, but I think maybe it was more instructions on how it could be done rather than the actual code to do it.

The game has this already, but I see no access to it via lua.
Rory Noonan
Posts: 2418
Joined: Thu Dec 18, 2014 1:53 am
Location: Brooklyn, NY

RE: Lua function to return readable weather?

Post by Rory Noonan »

Here's an example from one of my scripts:
function DTG(TimeVar)
if TimeVar == nil then
TimeVar = ScenEdit_CurrentTime()
end
local msgtime = os.date("!%d%H%M" .. "Z" .. " " .. "%b %y", TimeVar)
local msgtime = string.upper(msgtime)
return msgtime
end

function Round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end

function ACP126(rec_station,snd_station,precedence,from,to,classification,body)
--rec_station --4 letter code (e.g. YDCX)
--snd_station --4 letter code +/- NR 3 number (e.g. YBDN NR 270)
--precedence --Flash (Z), Immediate (O), Priority (P), Routine (R), Flash Override (Y)
local dtg = DTG()
--from --e.g. MET FLT OPS
--to --e.g. SSN 21 SEAWOLF
--classification --Unclass +/- SBU / FOUO / NOFORN (Restricted), Confidential, Secret, Top Secret
--body
_,gr = body:gsub("%S+","")
local sig_string = string.upper('<P><FONT face=Consolas>'..rec_station..' <BR>'..
'DE '..snd_station..' <BR>'..
precedence..' '..dtg..' <BR>'..
'fm '..from..' <BR>'..
'to '..to..' <BR>'..
'wd gr'..gr..' <BR>'..
'bt <BR>'..
classification..' <BR>'..
body..' <BR>'..
'bt <BR>'..
'nnnn </P>')
return sig_string
end

function WeatherReport(outlook)
if outlook == nil then outlook = 'next forecast at '..DTG(ScenEdit_CurrentTime()+21600) end
--Generate special message to player
local weather = ScenEdit_GetWeather() --Get new weather parameters
local temp, cloud, rain, sea = weather.temp, weather.undercloud, weather.rainfall, weather.seastate

local f_temp = Round((temp*1.8) + 32,0) --Convert to Fahrenheit

--create rain/precipitation descriptor (based on in-game descriptions)
if rain == 0 then precipdesc = 'nil'
elseif rain < 5 then precipdesc = 'very light'
elseif rain < 11 then precipdesc = 'light'
elseif rain < 20 then precipdesc = 'moderate'
elseif rain < 30 then precipdesc = 'heavy'
elseif rain < 40 then precipdesc = 'very heavy'
else precipdesc = 'extreme'
end

--create cloud descriptor (based on in-game descriptions)
if cloud == 0 then clouddesc = 'clear skies'
elseif cloud < 0.2 then clouddesc = 'light low clouds'
elseif cloud < 0.3 then clouddesc = 'light middle clouds'
elseif cloud < 0.4 then clouddesc = 'light high clouds'
elseif cloud < 0.5 then clouddesc = 'moderate low clouds'
elseif cloud < 0.6 then clouddesc = 'moderate middle clouds'
elseif cloud < 0.7 then clouddesc = 'moderate high clouds'
elseif cloud < 0.8 then clouddesc = 'moderate middle clouds & light high clouds'
elseif cloud < 0.9 then clouddesc = 'solid middle clouds & moderate high clouds'
elseif cloud < 1.0 then clouddesc = 'thin fog & solid cloud cover'
else clouddesc = 'thick fog & solid cloud cover'
end

--rec_station,snd_station,precedence,from,to,classification,body
local wx_time = DTG()
local wx_report = ACP126('NEWC','JOCMETOPS','r','HQJOC METEOROLOGICAL OFFICE','FFG 06 NEWCASTLE <BR> INFO ALL STATIONS','unclass','WX REPORT ' .. wx_time .. ' - EAST COAST AND BASS STRAIT <BR>AVERAGE TEMP '.. temp ..'°C / '..f_temp..'°F <BR> SEA STATE '.. sea ..' <BR>'..precipdesc..' PRECIPITATION <BR>'..clouddesc..'<BR>'..outlook)
ScenEdit_SpecialMessage('playerside',wx_report)
end

WeatherReport()

It's long but will return a properly formatted weather report in ACP126 signal format. Also note the DTG function (similar to what you were looking at yesterday).
Image
Whicker
Posts: 664
Joined: Tue Jun 19, 2018 9:54 pm

RE: Lua function to return readable weather?

Post by Whicker »

Ask and yee shall receive. Doesn't always work that way, but sometimes it does.

That's awesome, thanks.
Whicker
Posts: 664
Joined: Tue Jun 19, 2018 9:54 pm

RE: Lua function to return readable weather?

Post by Whicker »

BASS STRAIT

I think that code came from simex18? what happened to that? I quite liked that one. Lua in there is impressive.
Craigkn
Posts: 192
Joined: Thu Jan 27, 2022 12:06 am
Location: Central Maryland

Re: Lua function to return readable weather?

Post by Craigkn »

I wanted to bump this thread from 2018 - this was exactly what I was looking for to create a weather report. This should be stickied imo.
Post Reply

Return to “Lua Legion”