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

Random Spawn Area Selection

Status
Not open for further replies.
Level 7
Joined
Sep 19, 2012
Messages
204
Hey guys...

im trying to create a trigger that randomly selects a spawn area for every Player, but doesnt put 2 players in the same spot.
For example i have an arena with 8 slots and i want my 6 players to start on one of these 8 locations (random) so you dont know where your enemy is, but i dont want two people spawning on the same location.

I tried different things for at least 4 hours now and i still couldnt figure it out. Any ideas?

greetings
CodeBlack
 
Level 11
Joined
Jun 2, 2004
Messages
849
The simplest way is to generate a random number between 1 and 8 for each player. If there are two or more players with the same number, reroll or do a simple algorithm (like adding +1 and using modulo 8 repeatedly) until they all have unique numbers.

GUI doesn't have a "while" function unfortunately but you can fudge it by using a regular loop between 0 and 0 and decrementing the counter every time you want it to loop again. This will let you loop infinitely until you're satisfied with the result (then letting the counter hit 1 and terminate the loop).
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,198
The simplest way is to generate a random number between 1 and 8 for each player. If there are two or more players with the same number, reroll or do a simple algorithm (like adding +1 and using modulo 8 repeatedly) until they all have unique numbers.
Why not give them truly random start locations instead?

Simplest way is to turn an array into an unordered list of all spawn locations. This is done by placing the spawn locations into the array starting at index 0, then index 1 etc. For each player pick an inclusive random number between 0 and {number of elements in the list - 1}, assign that spawn location to that player, replace that spawn location in the list with the element at the end of the list and then decrement the number of elements in the list by 1. As long as the list has more elements than players it will assign every player a truly random spawn location, or at least as random as computers allow.

One can think of the approach like a raffle. All the tickets (spawn locations) are thrown into a hat (array) and then tickets are repeatedly removed from the hat at random (picking random element between 0 and last element) and given to people (players).
 
Level 7
Joined
Sep 19, 2012
Messages
204
Why not give them truly random start locations instead?

Simplest way is to turn an array into an unordered list of all spawn locations. This is done by placing the spawn locations into the array starting at index 0, then index 1 etc. For each player pick an inclusive random number between 0 and {number of elements in the list - 1}, assign that spawn location to that player, replace that spawn location in the list with the element at the end of the list and then decrement the number of elements in the list by 1. As long as the list has more elements than players it will assign every player a truly random spawn location, or at least as random as computers allow.

One can think of the approach like a raffle. All the tickets (spawn locations) are thrown into a hat (array) and then tickets are repeatedly removed from the hat at random (picking random element between 0 and last element) and given to people (players).

And as always i was thinking way too complicated! Thank you very much! +rep
  • Battle Setup
    • Events
      • Time - Battle_Timer expires
    • Conditions
    • Actions
      • Set temp_integer = 8
      • Set RandomArea[1] = FFA Pit1 <gen>
      • Set RandomArea[2] = FFA Pit2 <gen>
      • Set RandomArea[3] = FFA Pit3 <gen>
      • Set RandomArea[4] = FFA Pit4 <gen>
      • Set RandomArea[5] = FFA Pit5 <gen>
      • Set RandomArea[6] = FFA Pit6 <gen>
      • Set RandomArea[7] = FFA Pit7 <gen>
      • Set RandomArea[8] = FFA Pit8 <gen>
      • For each (Integer A) from 1 to 6, do (Actions)
        • Loop - Actions
          • Set temp_randomnr = (Random integer number between 1 and temp_integer)
          • Set SpawnArea[(Integer A)] = RandomArea[temp_randomnr]
          • Set RandomArea[temp_randomnr] = RandomArea[temp_integer]
          • Set temp_integer = (temp_integer - 1)
          • Unit - Create 1 Footman for (Player((Integer A))) at (Center of SpawnArea[(Integer A)]) facing Default building facing degrees
 
Status
Not open for further replies.
Top