Warcraft III: MapsModelsSkinsIconsSpellsToolsTutorials
WC3 JASS: Code SnippetsJASS and vJASS Spells and SystemsJass Tutorials
Chat @Discord

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

Author Topic: Random Iteration  (Read 4898 times)

Rating
Average Score
« Created: February 28, 2018, 12:59:36 PM by moyack »

  • Newbie - level 1
  • *
  • Posts: 13
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • WC3 Spells: 0
  • Reputation: 0
  • Just a Man in the Back
    • View Profile
Random Iteration
« on: September 04, 2016, 09:16:22 AM »
Category: Execution
Language: vJASS
This snippet is a replacement to the BJ function GetRandomSubGroup with the advantage of being able to directly enumerate random number of units in a group instead of creating another subgroup of an already existing group. Notice that this is not the same as GroupEnumUnitsInRangeCounted in which case the enumerated units are not random but is based on which units are picked first.


Code: jass  [Select]
  1. library RandomIteration
  2.  
  3.     globals
  4.         private group tempGroup = CreateGroup()
  5.         private integer unitCount
  6.     endglobals
  7.  
  8.     private function EnumUnits takes nothing returns boolean
  9.         call GroupAddUnit(tempGroup, GetFilterUnit())
  10.         set unitCount = unitCount + 1
  11.         return false
  12.     endfunction
  13.  
  14.     function EnumRandomUnitsInRangeCounted takes group g, real x, real y, real radius, integer limit returns nothing
  15.         local real chance
  16.         local unit u
  17.         call GroupEnumUnitsInRange(g, x, y, radius, Filter(function EnumUnits))
  18.         set chance = I2R(limit)/I2R(unitCount)
  19.         loop
  20.             set u = FirstOfGroup(tempGroup)
  21.             exitwhen u == null or limit == 0
  22.             call GroupRemoveUnit(tempGroup, u)
  23.             if GetRandomReal(0, 1) <= chance then
  24.                 call GroupAddUnit(g, u)
  25.                 set limit = limit - 1
  26.             endif
  27.         endloop
  28.         set u = null
  29.     endfunction
  30.  
  31.     function EnumRandomUnitsInRectCounted takes group g, rect r, integer limit returns nothing
  32.         local real chance
  33.         local unit u
  34.         call GroupEnumUnitsInRect(g, r, Filter(function EnumUnits))
  35.         set chance = I2R(limit)/I2R(unitCount)
  36.         loop
  37.             set u = FirstOfGroup(tempGroup)
  38.             exitwhen u == null or limit == 0
  39.             call GroupRemoveUnit(tempGroup, u)
  40.             if GetRandomReal(0, 1) <= chance then
  41.                 call GroupAddUnit(g, u)
  42.                 set limit = limit - 1
  43.             endif
  44.         endloop
  45.         set u = null
  46.     endfunction
  47.  
  48. endlibrary
« Last Edit: December 19, 2017, 10:58:51 PM by moyack »

  • Site Owner
  • Administrator
  • Starter - level 4
  • *
  • Posts: 958
  • WC3 Models: 59
  • WC3 Tutorials: 13
  • WC3 Tools: 11
  • WC3 Maps: 12
  • WC3 Skins: 6
  • WC3 Icons: 2
  • WC3 Spells: 15
  • Reputation: 1153
  • Site Admin - I love fix things
    • View Profile
    • Blizzard Modding Information Center
Re: Random Iteration
« Reply #1 on: September 04, 2016, 10:22:47 AM »
I just did some small aesthetics for your post. Interesting snippet :)

We can give you full hosting for your projects, a complete page!!

A custom altered melee map where you can play Naga and Demons. Check it out!!
Use Dropbox...

 

Random weather script

Started by moyackBoard Codes & Snippets

Replies: 0
Views: 182
Last post February 18, 2012, 05:59:47 AM
by moyack
Blizzard Modding Information Center Wacraft III Modding Information Center WC3JASS.com - The JASS Vault Chronicles of Darkness - A Warcraft III mod Jetcraft - A Starcraft II mod Troll Smash - A Warcraft III Arena Starcraft II Modding Information Center
  Mod DB - Change the Game Power of Corruption - A Warcraft III altered melee map Chaos Realm - The world of Game modders and wc3 addicts Follow us on Facebook!!