Weather
version 3/080914 by Ish McGravin
Version 3/080914 of the Weather by Ish McGravin begins here.
"Simulates a changing date, day/night cycle, randomized pseudo-weather, and semi-accurate moon phase. Read the documentation carefully."
[variables!]
Year, Month, Day, DTemp, NTemp, Weather, LunD, LunG, MoonN are number variables.
WDesc, MoonP are text variables.
[defaults!]
Time of day is 12:01 AM.
Year is 1900.
Month is 2.
Day is 1.
MoonP is "The new moon is dark and leaves the land in shadow.".
MoonN is 0.
[fancy table!]
[as per documentation, these numbers are based on the population center of the United States]
[note that month 1 is December instead of month 12, and other months are one greater]
Table of Months
| Name | Days | Sunrise | Sunset | Av Temp |
| "December" | 31 | 8:15 AM | 5:59 PM | 33 |
| "January" | 31 | 8:22 AM | 6:11 PM | 29 |
| "February" | 28 | 7:57 AM | 6:46 PM | 34 |
| "March" | 31 | 7:19 AM | 7:14 PM | 44 |
| "April" | 30 | 6:32 AM | 7:43 PM | 55 |
| "May" | 31 | 5:57 AM | 8:10 PM | 64 |
| "June" | 30 | 5:44 AM | 8:31 PM | 73 |
| "July" | 31 | 5:56 AM | 8:29 PM | 78 |
| "August" | 31 | 6:22 AM | 8:00 PM | 77 |
| "September" | 30 | 6:49 AM | 7:15 PM | 67 |
| "October" | 31 | 7:15 AM | 6:30 PM | 56 |
| "November" | 30 | 7:47 AM | 5:55 PM | 45 |
[set up a few basics when the game starts]
When play begins:
change DTemp to Av Temp in row Month of Table of Months + a random number from -7 to 7;
change NTemp to DTemp - 7 + a random number from -3 to 2;
change Weather to a random number from 1 to 10;
if Weather is less than 4 begin;
change WDesc to "sunny";
otherwise if Weather is less than 6;
change WDesc to "partly cloudy";
otherwise if Weather is less than 9;
change WDesc to "cloudy";
otherwise;
if DTemp is less than 33 begin;
change WDesc to "snowing";
otherwise;
change WDesc to "raining";
end if;
end if;
[conditionals]
[nighttime - an often-used one]
To decide whether it is night:
if the time of day is before Sunrise in row Month of Table of Months then decide yes;
if the time of day is after Sunset in row Month of Table of Months then decide yes;
decide no.
[snow is on the ground if the temperature is below 33; this could be made much more robust]
To decide whether snow is on the ground:
if it is night begin;
if NTemp is less than 33 then decide yes;
otherwise if Dtemp is less than 33;
decide yes;
end if;
decide no.
[the four seasons]
To decide whether it is winter: if month is less than 4 then decide yes; decide no.
To decide whether it is spring: if month is less than 4 then decide no; if month is greater than 6 then decide no; decide yes.
To decide whether it is summer: if month is less than 7 then decide no; if month is greater than 9 then decide no; decide yes;
To decide whether it is fall: if month is greater than 9 then decide yes; decide no.
[every turn recalculations]
Every turn:
[daily recalculations]
if time of day is 12:00 AM begin;
change Day to Day + 1;
[day temperature is the average monthly temp +- 7 degrees]
change DTemp to Av Temp in row Month of Table of Months + a random number from -7 to 7;
[night temperature is modified from day temperature]
change NTemp to DTemp - 7 + a random number from -3 to 2;
if Day is greater than Days in row Month of Table of Months begin;
change Month to Month + 1;
change Day to 1;
end if;
if Month is 1 begin;
if Day is 1 then change Year to Year + 1;
end if;
if Month is greater than 12, change Month to 1;
[the moon phase calculator - complex, so be careful with it!]
change LunD to Day;
if Month is 1 begin;
change LunD to LunD + 334;
otherwise;
repeat with X running from 2 to Month - 1 begin;
change LunD to LunD + Days in row X of Table of Months;
end repeat;
end if;
change LunG to Year - 1900;
change LunG to remainder after dividing LunG by 19;
change LunG to 11 * LunG + 29;
change LunG to remainder after dividing LunG by 30;
if LunG is 24 then change LunG to LunG + 1;
if LunG is 25 then change LunG to LunG + 1;
change MoonN to LunG + LunD;
change MoonN to MoonN * 6 + 5;
change MoonN to remainder after dividing MoonN by 177;
change MoonN to MoonN / 22;
if MoonN is 8 then change MoonN to 0;
[end of the moon phase calculator]
if MoonN is 0 begin;
change MoonP to "The new moon is dark and leaves the land in shadow.";
otherwise if MoonN is 1;
change MoonP to "The crescent moon casts a dim glow.";
otherwise if MoonN is 2;
change MoonP to "The first quarter moon smiles down on the land.";
otherwise if MoonN is 3;
change MoonP to "The waxing gibbous moon brightens the sky.";
otherwise if MoonN is 4;
change MoonP to "The full moon is almost dazzlingly bright and lights up the land.";
otherwise if MoonN is 5;
change MoonP to "The waning gibbous moon brightens the sky.";
otherwise if MoonN is 6;
change MoonP to "The last quarter moon creates eerie shadows on the land.";
otherwise;
change MoonP to "The crescent moon casts a dim glow.";
end if;
end if;
[every hour recalculations]
if the minutes part of the time of day is 00 begin;
[modify the weather value by probability: -2,-1,-1,0,0,0,+1,+1,+2 ]
change Weather to Weather + a random number between -1 and 1 + a random number between -1 and 1;
[make sure weather value is within the range 1 to 10 inclusive]
if Weather is greater than 10 then change Weather to 10;
if Weather is less than 1 then change Weather to 1;
[describe the weather]
if Weather is less than 4 begin;
if it is night begin;
change WDesc to "starry";
otherwise;
change WDesc to "sunny";
end if;
otherwise if Weather is less than 6;
change WDesc to "partly cloudy";
otherwise if Weather is less than 9;
change WDesc to "cloudy";
otherwise;
if it is night begin;
if NTemp is less than 33 begin;
change WDesc to "snowing";
otherwise if DTemp is less than 33;
change WDesc to "snowing";
end if;
otherwise;
change WDesc to "raining";
end if;
end if;
end if.
Weather ends here.