Warcraft 3 documentation
vJASS & Zinc Documentation
For the latest documentation about how it works vJASS and Zinc language layers for Warcraft III, please follow these links:
Jasshelper documentation - Zinc documentation - WC3 Optimizer documentation

Random weather script No New Posts Codes & Snippets

Started by
moyack

0 Members and 1 Guest are viewing this topic.

Rating

Average Score - 5 / 5

« Created: January 02, 2018, 08:05:56 PM by moyack »

Random weather script
on: February 18, 2012, 05:59:47 AM
Category: Effect
Language: vJASS

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
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
  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
Code: jass
  1. // Random weather changer, by moyack. 2008.
  2. library RandomWeather initializer init
  3. // Configuration section
  4. globals
  5.     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. endglobals
  8. // End configuration section
  9. globals
  10.     // Weather Type Constants
  11.     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. endglobals
  35.  
  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.     integer id
  41.     real weight
  42.     real chance
  43.    
  44.     private method onDestroy takes nothing returns nothing
  45.         call RemoveWeatherEffect(.w)
  46.     endmethod
  47.    
  48.     static method Clean takes nothing returns nothing
  49.         local integer i = 1
  50.         local Weather W
  51.         loop
  52.             exitwhen i > Weather.index
  53.             set W = Weather(i)
  54.             call W.destroy()
  55.             set i = i+1
  56.         endloop
  57.         set Weather.index = 0
  58.     endmethod
  59.    
  60.     static method Add takes integer id, real w returns nothing
  61.         local integer i = 1
  62.         local Weather W = Weather.allocate()
  63.         set W.id = id
  64.         set W.weight = w
  65.         set W.w = AddWeatherEffect(Weather.R, id)
  66.         call EnableWeatherEffect(W.w, false)
  67.         set Weather.total = Weather.total + w
  68.         set Weather.index = integer(W)
  69.         loop
  70.             exitwhen i > Weather.index
  71.             set W = Weather(i)
  72.             set W.chance = W.weight / Weather.total
  73.             set i = i+1
  74.         endloop
  75.     endmethod
  76.    
  77.     static method Start takes nothing returns nothing
  78.         local integer i = 1
  79.         local real r = GetRandomReal(0,1)
  80.         local real s = 0.
  81.         local Weather W
  82.         loop
  83.             exitwhen i > Weather.index
  84.             set W = Weather(i)
  85.             exitwhen s <= r and r < s+W.chance
  86.             set s = s+W.chance
  87.             set i = i+1
  88.         endloop
  89.         call EnableWeatherEffect(W.w, true)
  90.     endmethod
  91.    
  92.     static method Stop takes nothing returns nothing
  93.         local integer i = 1
  94.         local Weather W
  95.         loop
  96.             exitwhen i > Weather.index
  97.             set W = Weather(i)
  98.             call EnableWeatherEffect(W.w, false)
  99.             set i = i+1
  100.         endloop
  101.     endmethod
  102.    
  103.     private static method onInit takes nothing returns nothing
  104.         set Weather.R = GetWorldBounds()
  105.     endmethod
  106. endstruct
  107.  
  108. private function Loop takes nothing returns nothing
  109.     local timer t = GetExpiredTimer()
  110.     set IsWeather = not IsWeather
  111.     if IsWeather then
  112.         call Weather.Start()
  113.     else
  114.         call Weather.Stop()
  115.     endif
  116.     call PauseTimer(t)
  117.     call TimerStart(t, GetRandomReal(MinDur, MaxDur), false, function Loop)
  118.     set t = null
  119. endfunction
  120.  
  121. private function init takes nothing returns nothing
  122.     call TimerStart(CreateTimer(), GetRandomReal(MinDur, MaxDur), false, function Loop)
  123. endfunction
  124. // ==============
  125. // user functions
  126. // ==============
  127. function SetWeatherWeight takes integer wid, real w returns nothing
  128.     call Weather.Add(wid, w)
  129. endfunction
  130.  
  131. function CleanWeatherWeights takes nothing returns nothing
  132.     call Weather.Clean()
  133. endfunction
  134.  
  135. endlibrary


Minimalistic version. Only manages one type of weather
Code: jass
  1. // Random weather changer, by moyack. 2008.
  2. scope RandomWeather initializer init
  3.  
  4. globals
  5.     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. endglobals
  11.  
  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. endfunction
  20.  
  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. endfunction
  25.  
  26. endscope


Weather RawCodes
Code: jass
  1. globals
  2.     // Weather Type Constants
  3.     constant integer Ashenvale_Rain_Heavy    = 'RAhr'
  4.     constant integer Ashenvale_Rain_Light    = 'RAlr'
  5.     constant integer Dalaran_Shield          = 'MEds'
  6.     constant integer Dungeon_Blue_Fog_Heavy  = 'FDbh'
  7.     constant integer Dungeon_Blue_Fog_Light  = 'FDbl'
  8.     constant integer Dungeon_Green_Fog_Heavy = 'FDgh'
  9.     constant integer Dungeon_Green_Fog_Light = 'FDgl'
  10.     constant integer Dungeon_Red_Fog_Heavy   = 'FDrh'
  11.     constant integer Dungeon_Red_Fog_Light   = 'FDrl'
  12.     constant integer Dungeon_White_Fog_Heavy = 'FDwh'
  13.     constant integer Dungeon_White_Fog_Light = 'FDwl'
  14.     constant integer Lordaeron_Rain_Heavy    = 'RLhr'
  15.     constant integer Lordaeron_Rain_Light    = 'RLlr'
  16.     constant integer Northrend_Blizzard      = 'SNbs'
  17.     constant integer Northrend_Snow_Heavy    = 'SNhs'
  18.     constant integer Northrend_Snow_Light    = 'SNls'
  19.     constant integer Outland_Wind_Heavy      = 'WOcw'
  20.     constant integer Outland_Wind_Light      = 'WOlw'
  21.     constant integer Rays_Of_Light           = 'LRaa'
  22.     constant integer Rays_Of_Moonlight       = 'LRma'
  23.     constant integer Wind_Heavy              = 'WNcw'
  24.     // End Weather type Constants...
  25. endglobals
« Last Edit: December 21, 2017, 12:05:33 PM by moyack »



 

Started by moyack

Replies: 0
Views: 1612
Codes & Snippets

Started by moyack

Replies: 40
Views: 58668
Jass Theory & Questions

Started by moyack

Replies: 1
Views: 7099
JetScript

Started by moyack

Replies: 5
Views: 10223
JetScript
Vivir aprendiendo.co - A place for learning stuff, in Spanish   Chaos Realm - The world of Game modders and wc3 addicts   Diplo, a gaming community   Power of Corruption, an altered melee featuring Naga and Demon. Play it now!!!   WC3JASS.com - The JASS Vault + vJASS and Zinc   Jetcraft - A Starcraft II mod   WormTastic Clan (wTc)   Warcraft RESOURCES Reforged: Modelos, mapas, proyectos y mas...