• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Trigger] Random Region Setting

Status
Not open for further replies.
Level 5
Joined
Apr 29, 2015
Messages
143
Hello, does anyone know a way to configure random regions?
My map has 3 region for creep spawning and needs a trigger which switches region time by time, to diversify unit spawning from place to place.

Like, there's 3 set of unit, one is tank, one is siege and one is hybrid, each type of them spawns in 3 different regions but I cannot just use "(Random integer number between 1 and 3)" since it is possible for 2 type of units to spawn in a same region.

I literally have no idea how to configure this and I would appreciate it alot if anyone knows how to configure it. :)
 
Level 12
Joined
Feb 22, 2010
Messages
1,115
There are multiple ways, you can do something like this if you only have 3 regions.If you have more it is better to use more technical solution.


  • Spawn Trigger 1
  • Set Region[1] = region1
  • Set Region[2] = region2
  • Set Region[3] = region3
  • Set RegionUsed[1] = false
  • Set RegionUsed[2] = false
  • Set RegionUsed[3] = false
  • Set RandomInt = (random integer number between 1 and 3)
  • Set RegionUsed[RandomInt] = true
  • Unit - Create your TANK unit at Region[RandomInt]
  • Trigger - Run Trigger 2
  • Trigger 2
  • Set RandomInt = (random integer number between 1 and 3)
  • if (RegionUsed[RandomInt]) is equal to true
    • Trigger - Run Trigger 2
  • else
    • Set RegionUsed[RandomInt] = true
    • Unit - Create your SIEGE unit at Region[RandomInt]
    • Trigger - Run Trigger 3
You figure out trigger 3, it looks like trigger 2.
 
Level 5
Joined
Apr 29, 2015
Messages
143
There are multiple ways, you can do something like this if you only have 3 regions.If you have more it is better to use more technical solution.


  • Spawn Trigger 1
  • Set Region[1] = region1
  • Set Region[2] = region2
  • Set Region[3] = region3
  • Set RegionUsed[1] = false
  • Set RegionUsed[2] = false
  • Set RegionUsed[3] = false
  • Set RandomInt = (random integer number between 1 and 3)
  • Set RegionUsed[RandomInt] = true
  • Unit - Create your TANK unit at Region[RandomInt]
  • Trigger - Run Trigger 2
  • Trigger 2
  • Set RandomInt = (random integer number between 1 and 3)
  • if (RegionUsed[RandomInt]) is equal to true
    • Trigger - Run Trigger 2
  • else
    • Set RegionUsed[RandomInt] = true
    • Unit - Create your SIEGE unit at Region[RandomInt]
    • Trigger - Run Trigger 3
You figure out trigger 3, it looks like trigger 2.

Hm, just wondering why do we have to re-run trigger 2 on the trigger 2 itself?

And if you could make the trigger 3 then I would appreciate it alot. :)
 
Level 25
Joined
Sep 26, 2009
Messages
2,373
Unused regions have boolean value (RegionUsed) set to false, used regions have that value set to true.
In trigger 2 he chooses random number as an index for the RegionUsed array. Then he checks if the boolean value of the array at that index is equal to true. If the value is True, it means that region is already used, but he wants unused regions. So if the value is equal to true, he re-runs trigger 2 until he gets random unused region (until he gets an index for the array which contains False at that index).

I would use swapping method here tho. Meaning that I have an array of... say 10 regions called simply Regions[] - I will keep the number of regions in an integer variable called MaxIndex, so MaxIndex now contains value 10. I will choose random number between 1 and MaxIndex (in this case, it would be between 1 and 10). Once I chose a number, I would do my stuff with the region Region[chosen_index] and then I will swap that region with the last unused region - which is region indexed at MaxIndex (10) and decrease value of MaxIndex by 1. I would repeat this process each time I chose random region. What I would get is region array Region[] which contains unused regions until MaxIndex value and used region after MaxIndex value.

The advantage of this approach is that you always choose one of the available regions, while Ceday's approach will create an overhead the more used regions he has - imagine you have for example 10 regions. It's easy to choose the first few regions without any problem, but imagine you have for example 9 out of 10 used regions and the trigger he posted would have to randomly select the last region - that means he would have a 10% chance he chose that region, meaning that trigger would run way too many times.

Here are the triggers in question
  • Map Ini
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Set MaxIndex = 4 //represents the maximum index for the Region array
      • Set Region[1] = *some region*
      • Set Region[2] = *some region*
      • Set Region[3] = *some region*
      • Set Region[4] = *some region*
  • Choose region
    • Events
    • Conditions
    • Actions
      • Set num = (Random integer number between 1 and MaxIndex)
      • Set reg = Region[num]
      • Set loc = (Center of reg)
      • Unit - Create 1 Footman for Player 1 (Red) at loc facing Default building facing degrees
      • Custom script: call RemoveLocation(udg_loc)
      • -------- The code below swaps Region[num] with Region[maxIndex] and decreases maxIndex --------
      • Set Region[num] = Region[MaxIndex]
      • Set Region[MaxIndex] = reg
      • Set MaxIndex = (MaxIndex - 1)
 
Level 5
Joined
Apr 29, 2015
Messages
143
Ok, the trigger now worked, thanks to Ceday (and Nichilus).
But I have to make a another trigger though, one for spawning and three for region randomization.

:)
 
Level 15
Joined
Jan 27, 2007
Messages
948
Unused regions have boolean value (RegionUsed) set to false, used regions have that value set to true.
In trigger 2 he chooses random number as an index for the RegionUsed array. Then he checks if the boolean value of the array at that index is equal to true. If the value is True, it means that region is already used, but he wants unused regions. So if the value is equal to true, he re-runs trigger 2 until he gets random unused region (until he gets an index for the array which contains False at that index).

I would use swapping method here tho. Meaning that I have an array of... say 10 regions called simply Regions[] - I will keep the number of regions in an integer variable called MaxIndex, so MaxIndex now contains value 10. I will choose random number between 1 and MaxIndex (in this case, it would be between 1 and 10). Once I chose a number, I would do my stuff with the region Region[chosen_index] and then I will swap that region with the last unused region - which is region indexed at MaxIndex (10) and decrease value of MaxIndex by 1. I would repeat this process each time I chose random region. What I would get is region array Region[] which contains unused regions until MaxIndex value and used region after MaxIndex value.

The advantage of this approach is that you always choose one of the available regions, while Ceday's approach will create an overhead the more used regions he has - imagine you have for example 10 regions. It's easy to choose the first few regions without any problem, but imagine you have for example 9 out of 10 used regions and the trigger he posted would have to randomly select the last region - that means he would have a 10% chance he chose that region, meaning that trigger would run way too many times.

Here are the triggers in question
  • Map Ini
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Set MaxIndex = 4 //represents the maximum index for the Region array
      • Set Region[1] = *some region*
      • Set Region[2] = *some region*
      • Set Region[3] = *some region*
      • Set Region[4] = *some region*
  • Choose region
    • Events
    • Conditions
    • Actions
      • Set num = (Random integer number between 1 and MaxIndex)
      • Set reg = Region[num]
      • Set loc = (Center of reg)
      • Unit - Create 1 Footman for Player 1 (Red) at loc facing Default building facing degrees
      • Custom script: call RemoveLocation(udg_loc)
      • -------- The code below swaps Region[num] with Region[maxIndex] and decreases maxIndex --------
      • Set Region[num] = Region[MaxIndex]
      • Set Region[MaxIndex] = reg
      • Set MaxIndex = (MaxIndex - 1)

im sorry to revive an old thread, but instead of making a new one, i'd rather revive this one.

I'm having the exact same trouble but this does not fix the problem.

when you set the max index -1 it CAN still randomly pick an already picked number. I mean: If you have a range of numbers 1-5, and your random number is 2, and you lower by -1, you CAN still get number 2 and have 2 spawned units together. Because the range will be 1-4, including 2. Do you get me?
 
Level 25
Joined
Sep 26, 2009
Messages
2,373
No, what you write is incorrect.
When you pick a region form the array, you SWAP it with the LAST region in the array and THEN decrease the index by 1. Because of the swap and decreased index, you no longer choose the same region.

I already made a pic for someone about this that explains it, thank god I didn't get rid of it.

Check the hidden tab.
attachment.php
 

Attachments

  • wc3.png
    wc3.png
    44.5 KB · Views: 161
Status
Not open for further replies.
Top