Ok i have a ported over trigger from warcraft3 that randomizes heroes, removes each one from the unit array as they are made so there are no doubles
It works perfectly in SC2 also, there are never doubles of heroes, and each person always get a different hero
However, the assignment item that is matched with the hero, very rarely may be different for some reason
For example, Marine hero is "unit(1)", and Marine hero should always have "item(1)" given to that hero, but sometimes he gets another Item from the array
_______
Rndp1, Rndp2, etc are global integers
N is a global integer
Hero is a unit array
playunit is a unit array
Items is a unit array
___________________
Variable - Set Rndp1 = (Random integer between 1 and N)
Unit - Create 1 Hero[Rndp1] for player 1 at (Center of Region 001) facing 270.0 degrees (No Options)
Variable - Set playunit[1] = (Last created unit)
Unit - Create one Items[Rndp1] item in the inventory of playunit[1]
Variable - Set Hero[Rndp1] = Hero[N]
Variable - Set N = (N - 1)
___________________
0.3 seconds later in another trigger...................
Variable - Set Rndp2 = (Random integer between 1 and N)
Unit - Create 1 Hero[Rndp2] for player 2 at (Center of Region 002) facing 270.0 degrees (No Options)
Variable - Set playunit[2] = (Last created unit)
Unit - Create one Items[Rndp2] item in the inventory of playunit[2]
Variable - Set Hero[Rndp2] = Hero[N]
Variable - Set N = (N - 1)
___________________
After 0.3 seconds, that another trigger for player 2, fires, then player 3 after another 0.3 seconds etc all the way to player 12
All the heroes are created perfectly, never doubles and always different
But the item assigned, doesnt always match up, perhaps maybe 1/10 times there is a non matching hero and item
I am not convinced that the above code does a complete shuffle of integers.
I recommend you randomize the array before creating the heroes. You can use Fisher-Yates shuffle algorithm for this purpose, which should be implemented somewhat like this:
EventsWhateverLocalVariablesn=10<Integer>ShuffledArray=0<Integer[n]>j=0<Integer>i=0<Integer>swap=0<Integer>ConditionsActions-- Initialize the array (set to straight forward array [1, 2, 3, ... , n])General-Foreachintegerifrom1tonwithincrement1,do(Actions)ActionsVariable-SetShuffledArray[i]=i-- Randomize arrayGeneral-Foreachintegerifromnto1withincrement-1,do(Actions)ActionsVariable-Setj=(Randomintegerbetween1andi)Variable-Setswap=ShuffledArray[j]Variable-SetShuffledArray[j]=ShuffledArray[i]Variable-SetShuffledArray[i]=swap-- A randomized array is now stored in ShuffledArray
The result will be as in this example:
ShuffledArray[1] = 2
ShuffledArray[2] = 10
ShuffledArray[3] = 6
ShuffledArray[4] = 7
ShuffledArray[5] = 4
ShuffledArray[6] = 1
ShuffledArray[7] = 9
ShuffledArray[8] = 5
ShuffledArray[9] = 3
ShuffledArray[10] = 8
If you implement this as a function you can change the variable n to a parameter and return the ShuffledArray as a comma separated string (the editor cannot return arrays, do'h). You will have to split the text string into an integer array again in the receiving trigger, so I'm not sure much is gained from creating a function for this.
Also, the 0.3 wait time between the creation of each hero made me curious. Why is this time needed?
Lastly, make sure your item array points to the correct items ;)
Ok i have a ported over trigger from warcraft3 that randomizes heroes, removes each one from the unit array as they are made so there are no doubles
It works perfectly in SC2 also, there are never doubles of heroes, and each person always get a different hero
However, the assignment item that is matched with the hero, very rarely may be different for some reason
For example, Marine hero is "unit(1)", and Marine hero should always have "item(1)" given to that hero, but sometimes he gets another Item from the array
_______
Rndp1, Rndp2, etc are global integers
N is a global integer
Hero is a unit array
playunit is a unit array
Items is a unit array
___________________
Variable - Set Rndp1 = (Random integer between 1 and N)
Unit - Create 1 Hero[Rndp1] for player 1 at (Center of Region 001) facing 270.0 degrees (No Options)
Variable - Set playunit[1] = (Last created unit)
Unit - Create one Items[Rndp1] item in the inventory of playunit[1]
Variable - Set Hero[Rndp1] = Hero[N]
Variable - Set N = (N - 1)
___________________
0.3 seconds later in another trigger...................
Variable - Set Rndp2 = (Random integer between 1 and N)
Unit - Create 1 Hero[Rndp2] for player 2 at (Center of Region 002) facing 270.0 degrees (No Options)
Variable - Set playunit[2] = (Last created unit)
Unit - Create one Items[Rndp2] item in the inventory of playunit[2]
Variable - Set Hero[Rndp2] = Hero[N]
Variable - Set N = (N - 1)
___________________
After 0.3 seconds, that another trigger for player 2, fires, then player 3 after another 0.3 seconds etc all the way to player 12
All the heroes are created perfectly, never doubles and always different
But the item assigned, doesnt always match up, perhaps maybe 1/10 times there is a non matching hero and item
Anyone know why?
I am not convinced that the above code does a complete shuffle of integers.
I recommend you randomize the array before creating the heroes. You can use Fisher-Yates shuffle algorithm for this purpose, which should be implemented somewhat like this:
The result will be as in this example:
If you implement this as a function you can change the variable n to a parameter and return the ShuffledArray as a comma separated string (the editor cannot return arrays, do'h). You will have to split the text string into an integer array again in the receiving trigger, so I'm not sure much is gained from creating a function for this.
Also, the 0.3 wait time between the creation of each hero made me curious. Why is this time needed?
Lastly, make sure your item array points to the correct items ;)
- Kafoso