Random weather script

Scripts

14 years
edited 8 years
Random weather script
By moyack. 2008.

I did this as a request: a code that changes randomly the weather from normal to a rainy day. Due to its configurability and easy to use (just copy it to your map and it will work) I'll post it to the public. Now the current version allows multiple types of weathers and with a few functions you can achieve very nice weather effect transitions.


How the script works?

The script initializes a timer with a random duration (configurable via globals), and it will change from no weather to a weather environment. if you use the minimalistic version, it will turn on and off in a random way the specified weather. If you use the Standard version, it will do the same but with a set of weathers configured previously. this preconfiguration can be done at map init or at game time... there's no limitation.


Credits and Acknowledgments

Greetings to Syntic_Arrow for doing the request.
Credits to Ammorth for the suggestion of weights to implement randomness
Gives credits when you use it...


How to install:


  • Create a new trigger
  • Call it whatever you want
  • Convert it to custom text
  • Delete all the content of that trigger
  • Paste this trigger code and voila!!!


Standard version
[btable]The standard version offers the possibility to setup the kind of weathers you want to see in your map and their chances to appear. To start a set of weathers, you just have to add a code like this:

Sample code
Code (jass) Select
function StartWeather takes nothing returns nothing // This function can run at map init...
    call CleanWeatherWeights() // Prepares and clean the Weather script...
    call SetWeatherWeight(Northrend_Blizzard, 3.) // adds a weather type and its weight.
    call SetWeatherWeight(Rays_Of_Light, 5.)
    call SetWeatherWeight(Wind_Heavy, 8.)
    // if a weather has a higher weight, it will have more chances to happen than others.
    // this value is arbitrary...
endfunction


If you need to change the weather weights, or types, you must clean them with [lcode=jass]CleanWeatherWeights()[/lcode] and then reset the values with [lcode=jass]SetWeatherWeight(...)[/lcode] command.

Random Weather Script - Standard version
Code (jass) Select
// Random weather changer, by moyack. 2008.
library RandomWeather initializer init
// Configuration section
globals
    private constant real    MinDur = 5. // minimum time that we have to wait between weathers
    private constant real    MaxDur = 15. // maximum time that we have to wait between weathers
endglobals
// End configuration section
globals
    // Weather Type Constants
    constant integer Ashenvale_Rain_Heavy    = 'RAhr'
    constant integer Ashenvale_Rain_Light    = 'RAlr'
    constant integer Dalaran_Shield          = 'MEds'
    constant integer Dungeon_Blue_Fog_Heavy  = 'FDbh'
    constant integer Dungeon_Blue_Fog_Light  = 'FDbl'
    constant integer Dungeon_Green_Fog_Heavy = 'FDgh'
    constant integer Dungeon_Green_Fog_Light = 'FDgl'
    constant integer Dungeon_Red_Fog_Heavy   = 'FDrh'
    constant integer Dungeon_Red_Fog_Light   = 'FDrl'
    constant integer Dungeon_White_Fog_Heavy = 'FDwh'
    constant integer Dungeon_White_Fog_Light = 'FDwl'
    constant integer Lordaeron_Rain_Heavy    = 'RLhr'
    constant integer Lordaeron_Rain_Light    = 'RLlr'
    constant integer Northrend_Blizzard      = 'SNbs'
    constant integer Northrend_Snow_Heavy    = 'SNhs'
    constant integer Northrend_Snow_Light    = 'SNls'
    constant integer Outland_Wind_Heavy      = 'WOcw'
    constant integer Outland_Wind_Light      = 'WOlw'
    constant integer Rays_Of_Light           = 'LRaa'
    constant integer Rays_Of_Moonlight       = 'LRma'
    constant integer Wind_Heavy              = 'WNcw'
    // End Weather type Constants...
    private boolean IsWeather = false
endglobals

private struct Weather // this is the struct that manages all the weather behavior
    private static rect R
    private static real total = 0.
    private static integer index = 0
    weathereffect w
    integer id
    real weight
    real chance
   
    private method onDestroy takes nothing returns nothing
        call RemoveWeatherEffect(.w)
    endmethod
   
    static method Clean takes nothing returns nothing
        local integer i = 1
        local Weather W
        loop
            exitwhen i > Weather.index
            set W = Weather(i)
            call W.destroy()
            set i = i+1
        endloop
        set Weather.index = 0
    endmethod
   
    static method Add takes integer id, real w returns nothing
        local integer i = 1
        local Weather W = Weather.allocate()
        set W.id = id
        set W.weight = w
        set W.w = AddWeatherEffect(Weather.R, id)
        call EnableWeatherEffect(W.w, false)
        set Weather.total = Weather.total + w
        set Weather.index = integer(W)
        loop
            exitwhen i > Weather.index
            set W = Weather(i)
            set W.chance = W.weight / Weather.total
            set i = i+1
        endloop
    endmethod
   
    static method Start takes nothing returns nothing
        local integer i = 1
        local real r = GetRandomReal(0,1)
        local real s = 0.
        local Weather W
        loop
            exitwhen i > Weather.index
            set W = Weather(i)
            exitwhen s <= r and r < s+W.chance
            set s = s+W.chance
            set i = i+1
        endloop
        call EnableWeatherEffect(W.w, true)
    endmethod
   
    static method Stop takes nothing returns nothing
        local integer i = 1
        local Weather W
        loop
            exitwhen i > Weather.index
            set W = Weather(i)
            call EnableWeatherEffect(W.w, false)
            set i = i+1
        endloop
    endmethod
   
    private static method onInit takes nothing returns nothing
        set Weather.R = GetWorldBounds()
    endmethod
endstruct

private function Loop takes nothing returns nothing
    local timer t = GetExpiredTimer()
    set IsWeather = not IsWeather
    if IsWeather then
        call Weather.Start()
    else
        call Weather.Stop()
    endif
    call PauseTimer(t)
    call TimerStart(t, GetRandomReal(MinDur, MaxDur), false, function Loop)
    set t = null
endfunction

private function init takes nothing returns nothing
    call TimerStart(CreateTimer(), GetRandomReal(MinDur, MaxDur), false, function Loop)
endfunction
// ==============
// user functions
// ==============
function SetWeatherWeight takes integer wid, real w returns nothing
    call Weather.Add(wid, w)
endfunction

function CleanWeatherWeights takes nothing returns nothing
    call Weather.Clean()
endfunction

endlibrary
[/btable]


Minimalistic version. Only manages one type of weather
Code (jass) Select
// Random weather changer, by moyack. 2008.
scope RandomWeather initializer init

globals
    private constant integer Rain = 'RAhr' //Set the weather type that you want to use...
    private constant real    MinDur = 15. // minimum time that we have to wait between weathers
    private constant real    MaxDur = 35. // maximum time that we have to wait between weathers
    private weathereffect W
    private boolean IsRain = false
endglobals

private function Loop takes nothing returns nothing
    local timer t = GetExpiredTimer()
    set IsRain = not IsRain
    call EnableWeatherEffect(W, IsRain)
    call PauseTimer(t)
    call TimerStart(t, GetRandomReal(MinDur, MaxDur), false, function Loop)
    set t = null
endfunction

private function init takes nothing returns nothing
    set W = AddWeatherEffect(GetWorldBounds(), Rain)
    call TimerStart(CreateTimer(), GetRandomReal(MinDur, MaxDur), false, function Loop)
endfunction

endscope


Weather RawCodes
Code (jass) Select

globals
    // Weather Type Constants
    constant integer Ashenvale_Rain_Heavy    = 'RAhr'
    constant integer Ashenvale_Rain_Light    = 'RAlr'
    constant integer Dalaran_Shield          = 'MEds'
    constant integer Dungeon_Blue_Fog_Heavy  = 'FDbh'
    constant integer Dungeon_Blue_Fog_Light  = 'FDbl'
    constant integer Dungeon_Green_Fog_Heavy = 'FDgh'
    constant integer Dungeon_Green_Fog_Light = 'FDgl'
    constant integer Dungeon_Red_Fog_Heavy   = 'FDrh'
    constant integer Dungeon_Red_Fog_Light   = 'FDrl'
    constant integer Dungeon_White_Fog_Heavy = 'FDwh'
    constant integer Dungeon_White_Fog_Light = 'FDwl'
    constant integer Lordaeron_Rain_Heavy    = 'RLhr'
    constant integer Lordaeron_Rain_Light    = 'RLlr'
    constant integer Northrend_Blizzard      = 'SNbs'
    constant integer Northrend_Snow_Heavy    = 'SNhs'
    constant integer Northrend_Snow_Light    = 'SNls'
    constant integer Outland_Wind_Heavy      = 'WOcw'
    constant integer Outland_Wind_Light      = 'WOlw'
    constant integer Rays_Of_Light           = 'LRaa'
    constant integer Rays_Of_Moonlight       = 'LRma'
    constant integer Wind_Heavy              = 'WNcw'
    // End Weather type Constants...
endglobals