• 🏆 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!

Portal Trigger

Level 5
Joined
Apr 22, 2022
Messages
55
Hey yall, got a portal trigger which works but I was hoping to make it a bit better. Currently the trigger will randomly choose one of the variables to spawn a portal. I was hoping to add a check to see if a portal is already there, causing it to try spawning another portal elsewhere. If all 4 portals are already spawned I'd like the trigger to do nothing.
1702244476742.png
 
Level 25
Joined
Sep 26, 2009
Messages
2,383
You have a lot of unnecessary actions in your trigger. The only thing that is different in each If/Then/Else is only the region, everything else is the same.
So the first thing to do is to optimize the entire thing by just setting the Point variable in each If/Then/Else and execute all other actions outside those ITE blocks.
Also, this:
  • Player Group - Pick every player in (All players) and do (Actions)
    • Loop - Actions
      • Quest - Create Campaign ...
      • Unit - Grant shared vision...
      • Custom script: call RemoveLocation(...
should be like this:
  • Player Group - Pick every player in (All players) and do (Actions)
    • Loop - Actions
      • Quest - Create Campaign ...
      • Unit - Grant shared vision...
  • Custom script: call RemoveLocation(...
To further improve this, create a region array variable, on map initialization initialize the array with your regions and in Portals trigger use the random number as the index in array. Something like:
  • Map Ini
    • Events
      • Map Initialization
    • Conditions
    • Actions
      • Set Regions[1] = Portal Left 1 <gen>
      • Set Regions[2] = Portal Left 2 <gen>
      • Set Regions[3] = Portal Right 1 <gen>
      • Set Regions[4] = Portal Right 2 <gen>
  • // In Portals trigger
  • Set choice = (Random integer number between 1 and 4)
  • Set Point = (Center of Regions[choice])
  • ...
I see you also change facing and perhaps even the text message. You can use real array and string array to store angle and text for each choice.
I have recently posted an explanation in another thread about how to pick region randomly without picking same region again. See [Solved] - Random Hero Trigger Leak
 
Level 5
Joined
Apr 22, 2022
Messages
55
Thanks for the advice! I'll correct it to make it a bit smoother but I apologize, I don't think I follow your post you linked lol.
 
Level 25
Joined
Sep 26, 2009
Messages
2,383
The algorithm in the picture describes how to pick a region without picking same region multiple times. It achieves that using:
  • region array containing all possible regions
  • integer variable keeping track of number of unpicked regions
  • region variable which holds the randomly selected region
The algorithm can be described as:
  1. Let variable maxIndex represent the number of unpicked regions. At the start of the game maxIndex will be equal to number of regions in regions array
  2. Let num be random number between 1 and maxIndex
  3. Set region variable reg to region at num position in the regions array (i.e. regions[num])
  4. reg is your randomly picked region
To prevent same region from being picked again, the algorithm takes the following steps:
  1. switch position of region at num position with region at maxIndex position (=last unpicked region) in the regions array.
  2. decrease maxIndex by one, since you have one less region that was not yet picked
As an example, if you had 20 unpicked regions, then maxIndex would be 20. If the random number num was 6, then your randomly picked region would be region[6], which would be for example "Forest Area 1" region.
Now you swap region[6] with whatever is at region[maxIndex] = region[20]. Let's say that is region "Lake Area 3".
Finally, you decrease the maxIndex by one.
So you got from:
JASS:
maxIndex = 20
region[6] = "Forest Area 1"
region[20] = "Lake Area 3"
to
JASS:
maxIndex = 19
region[6] = "Lake Area 3"
region[20] = "Forest Area 1"
Because maxIndex is now 19, then the next time the trigger is executed, it will pick random number between 1 and 19, not 20. Because the random number won't ever be 20, it will never pick "Forest Area 1" again.
On the other hand, if the random number was 6 again, then neither that would be a problem, because previously region[6] was "Forest Area 1", but now region[6] is "Lake Area 3"
 
Top