• 🏆 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 hero unit to pick

Status
Not open for further replies.
Level 1
Joined
Jun 13, 2008
Messages
132
Hi there, I need your help to make my "flag unit" have between three heroes to choose from, I've already stored each hero with integers, Heros[1] = Warden, Heros[2] = King, and so on. I don't want two players to have the option to choose the same hero, if a hero is available to be chosen for a player, others cannot have the same hero in the flag to be chosen. Can you help me? thanks in advance.
 
Init
  • Set Hero[1] = Hero1
  • Set Hero[2] = Hero2
  • Set Hero[3] = Hero3
  • Set MaxIndex = 3

Pick
  • Set Index = (RandomInteger from 1 to MaxIndex)
  • Unit - Create Hero[Index] for Player
  • Set Hero[Index] = Hero[MaxIndex]
  • Set MaxIndex = MaxIndex - 1

Note that this will work only once because it will modify the default list.
If you want to use it multiple times some help variables can be used.
 
Level 1
Joined
Jun 13, 2008
Messages
132
Init
  • Set Hero[1] = Hero1
  • Set Hero[2] = Hero2
  • Set Hero[3] = Hero3
  • Set MaxIndex = 3

Pick
  • Set Index = (RandomInteger from 1 to MaxIndex)
  • Unit - Create Hero[Index] for Player
  • Set Hero[Index] = Hero[MaxIndex]
  • Set MaxIndex = MaxIndex - 1

Note that this will work only once because it will modify the default list.
If you want to use it multiple times some help variables can be used.

I'm trying to do what you suggested me with a few extra things, for instance, I have now my Heroes var renamed to HeroesStrength, other with HeroesAgility and other with HeroesIntelligence, extra integers too, HeroesStrengthInteger, HeroesAgilityInteger, HeroesIntelligenceInteger, Instead of your "Index" I'm using "TempInteger", it works, now instead of creating the unit I'm adding it to the flag via add unit to stock to last created unit, flag is in a loop and it is created for every player, so I think it works, to further test it I need to create enough heroes, but thanks for your help in advance.
 
Level 1
Joined
Jun 13, 2008
Messages
132
Init
  • Set Hero[1] = Hero1
  • Set Hero[2] = Hero2
  • Set Hero[3] = Hero3
  • Set MaxIndex = 3

Pick
  • Set Index = (RandomInteger from 1 to MaxIndex)
  • Unit - Create Hero[Index] for Player
  • Set Hero[Index] = Hero[MaxIndex]
  • Set MaxIndex = MaxIndex - 1

Note that this will work only once because it will modify the default list.
If you want to use it multiple times some help variables can be used.

Your system works but, I've seen players who had the possibility to pick the same hero other player had, although I don't have enough heroes to draft for 10 players, but I thought that when all the heroes are set to each player, if the pool becomes zero, the rest of the players wouldn't get the option to pick any hero at all. What can I do?
 
Level 12
Joined
Jan 2, 2016
Messages
973
Your system works but, I've seen players who had the possibility to pick the same hero other player had, although I don't have enough heroes to draft for 10 players, but I thought that when all the heroes are set to each player, if the pool becomes zero, the rest of the players wouldn't get the option to pick any hero at all. What can I do?

Well, it should work if you have enough heroes for each player.
But no... when you are out of heroes - your random number will be from 1 to 0 and will always be 1, so the rest of the players will be getting the hero who has been randomed last.

You could add a condition to the trigger "MaxIndex greater than or equal to 1" if you don't want the rest of the players to get heroes who have already been picked.
 
Level 15
Joined
Aug 14, 2007
Messages
936
It is a memory allocation game, you need the standard hero memory pool for unit type. Secondly when you random, scroll through all existing hero already picked and exclude them in the selection. Next, randomize this pool and pick the hero for the player. You don't have to worry about duplicated because it was handled at the excluding process.
 

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,197
It is a memory allocation game, you need the standard hero memory pool for unit type. Secondly when you random, scroll through all existing hero already picked and exclude them in the selection. Next, randomize this pool and pick the hero for the player. You don't have to worry about duplicated because it was handled at the excluding process.
Or more efficiently you remove selections from the list of possible selections after a selection is made. This is what IcemanBo gave. Filtering a collection like that usually has the time complexity of O(n) where n is the number of elements in the collection. Although trivial in this application, doing so for a bigger list or doing it often might become a problem.

That said the filter could do more functionality such as removing hero categories instead of individual hero types.
 
Status
Not open for further replies.
Top