14 years
edited 8 years
Random weather script
By moyack. 2008.
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
1 function StartWeather takes nothing returns nothing // This function can run at map init...
2 call CleanWeatherWeights() // Prepares and clean the Weather script...
3 call SetWeatherWeight(Northrend_Blizzard, 3.) // adds a weather type and its weight.
4 call SetWeatherWeight(Rays_Of_Light, 5.)
5 call SetWeatherWeight(Wind_Heavy, 8.)
6 // if a weather has a higher weight, it will have more chances to happen than others.7 // this value is arbitrary...8 endfunction
If you need to change the weather weights, or types, you must clean them with CleanWeatherWeights() and then reset the values with SetWeatherWeight(...) command.
Random Weather Script - Standard version
1 // Random weather changer, by moyack. 2008.2 library RandomWeather initializer init
3 // Configuration section4 globals5 private constant real MinDur = 5. // minimum time that we have to wait between weathers
6 private constant real MaxDur = 15. // maximum time that we have to wait between weathers
7 endglobals8 // End configuration section9 globals10 // Weather Type Constants11 constant integer Ashenvale_Rain_Heavy = 'RAhr'
12 constant integer Ashenvale_Rain_Light = 'RAlr'
13 constant integer Dalaran_Shield = 'MEds'
14 constant integer Dungeon_Blue_Fog_Heavy = 'FDbh'
15 constant integer Dungeon_Blue_Fog_Light = 'FDbl'
16 constant integer Dungeon_Green_Fog_Heavy = 'FDgh'
17 constant integer Dungeon_Green_Fog_Light = 'FDgl'
18 constant integer Dungeon_Red_Fog_Heavy = 'FDrh'
19 constant integer Dungeon_Red_Fog_Light = 'FDrl'
20 constant integer Dungeon_White_Fog_Heavy = 'FDwh'
21 constant integer Dungeon_White_Fog_Light = 'FDwl'
22 constant integer Lordaeron_Rain_Heavy = 'RLhr'
23 constant integer Lordaeron_Rain_Light = 'RLlr'
24 constant integer Northrend_Blizzard = 'SNbs'
25 constant integer Northrend_Snow_Heavy = 'SNhs'
26 constant integer Northrend_Snow_Light = 'SNls'
27 constant integer Outland_Wind_Heavy = 'WOcw'
28 constant integer Outland_Wind_Light = 'WOlw'
29 constant integer Rays_Of_Light = 'LRaa'
30 constant integer Rays_Of_Moonlight = 'LRma'
31 constant integer Wind_Heavy = 'WNcw'
32 // End Weather type Constants...33 private boolean IsWeather = false
34 endglobals35 36 private struct Weather // this is the struct that manages all the weather behavior
37 private static rect R
38 private static real total = 0.
39 private static integer index = 0
40 weathereffect w
41 integer id
42 real weight
43 real chance
44
45 private method onDestroy takes nothing returns nothing
46 call RemoveWeatherEffect(.w)
47 endmethod48
49 static method Clean takes nothing returns nothing
50 local integer i = 1
51 local Weather W52 loop53 exitwhen i > Weather.index
54 set W = Weather(i)
55 call W.destroy()
56 set i = i+1
57 endloop58 set Weather.index = 0
59 endmethod60
61 static method Add takes integer id, real w returns nothing
62 local integer i = 1
63 local Weather W = Weather.allocate()
64 set W.id = id
65 set W.weight = w
66 set W.w = AddWeatherEffect(Weather.R, id)
67 call EnableWeatherEffect(W.w, false)
68 set Weather.total = Weather.total + w
69 set Weather.index = integer(W)
70 loop71 exitwhen i > Weather.index
72 set W = Weather(i)
73 set W.chance = W.weight / Weather.total
74 set i = i+1
75 endloop76 endmethod77
78 static method Start takes nothing returns nothing
79 local integer i = 1
80 local real r = GetRandomReal(0,1)
81 local real s = 0.
82 local Weather W83 loop84 exitwhen i > Weather.index
85 set W = Weather(i)
86 exitwhen s <= r and r < s+W.chance
87 set s = s+W.chance
88 set i = i+1
89 endloop90 call EnableWeatherEffect(W.w, true)
91 endmethod92
93 static method Stop takes nothing returns nothing
94 local integer i = 1
95 local Weather W96 loop97 exitwhen i > Weather.index
98 set W = Weather(i)
99 call EnableWeatherEffect(W.w, false)
100 set i = i+1
101 endloop102 endmethod103
104 private static method onInit takes nothing returns nothing
105 set Weather.R = GetWorldBounds()
106 endmethod107 endstruct108 109 private function Loop takes nothing returns nothing
110 local timer t = GetExpiredTimer()
111 set IsWeather = not IsWeather
112 if IsWeather then
113 call Weather.Start()
114 else115 call Weather.Stop()
116 endif117 call PauseTimer(t)
118 call TimerStart(t, GetRandomReal(MinDur, MaxDur), false, function Loop)
119 set t = null
120 endfunction121 122 private function init takes nothing returns nothing
123 call TimerStart(CreateTimer(), GetRandomReal(MinDur, MaxDur), false, function Loop)
124 endfunction125 // ==============126 // user functions127 // ==============128 function SetWeatherWeight takes integer wid, real w returns nothing
129 call Weather.Add(wid, w)
130 endfunction131 132 function CleanWeatherWeights takes nothing returns nothing
133 call Weather.Clean()
134 endfunction135 136 endlibrary
[/btable]Minimalistic version. Only manages one type of weather
1 // Random weather changer, by moyack. 2008.2 scope RandomWeather initializer init
3 4 globals5 private constant integer Rain = 'RAhr' //Set the weather type that you want to use...
6 private constant real MinDur = 15. // minimum time that we have to wait between weathers
7 private constant real MaxDur = 35. // maximum time that we have to wait between weathers
8 private weathereffect W
9 private boolean IsRain = false
10 endglobals11 12 private function Loop takes nothing returns nothing
13 local timer t = GetExpiredTimer()
14 set IsRain = not IsRain
15 call EnableWeatherEffect(W, IsRain)
16 call PauseTimer(t)
17 call TimerStart(t, GetRandomReal(MinDur, MaxDur), false, function Loop)
18 set t = null
19 endfunction20 21 private function init takes nothing returns nothing
22 set W = AddWeatherEffect(GetWorldBounds(), Rain)
23 call TimerStart(CreateTimer(), GetRandomReal(MinDur, MaxDur), false, function Loop)
24 endfunction25 26 endscope
Weather RawCodes
1 2 globals3 // Weather Type Constants4 constant integer Ashenvale_Rain_Heavy = 'RAhr'
5 constant integer Ashenvale_Rain_Light = 'RAlr'
6 constant integer Dalaran_Shield = 'MEds'
7 constant integer Dungeon_Blue_Fog_Heavy = 'FDbh'
8 constant integer Dungeon_Blue_Fog_Light = 'FDbl'
9 constant integer Dungeon_Green_Fog_Heavy = 'FDgh'
10 constant integer Dungeon_Green_Fog_Light = 'FDgl'
11 constant integer Dungeon_Red_Fog_Heavy = 'FDrh'
12 constant integer Dungeon_Red_Fog_Light = 'FDrl'
13 constant integer Dungeon_White_Fog_Heavy = 'FDwh'
14 constant integer Dungeon_White_Fog_Light = 'FDwl'
15 constant integer Lordaeron_Rain_Heavy = 'RLhr'
16 constant integer Lordaeron_Rain_Light = 'RLlr'
17 constant integer Northrend_Blizzard = 'SNbs'
18 constant integer Northrend_Snow_Heavy = 'SNhs'
19 constant integer Northrend_Snow_Light = 'SNls'
20 constant integer Outland_Wind_Heavy = 'WOcw'
21 constant integer Outland_Wind_Light = 'WOlw'
22 constant integer Rays_Of_Light = 'LRaa'
23 constant integer Rays_Of_Moonlight = 'LRma'
24 constant integer Wind_Heavy = 'WNcw'
25 // End Weather type Constants...26 endglobals