Choose random actors from a weighted list...
playincorporated
Member, PRO Posts: 18
I've been trying to come up with an elegant way of doing this using GS ... Any ideas?
The problem:
* Every 5 seconds a wave of attackers proceeds to engage the game player.
* The number of attackers per wave is random (from 1 to a maximum of 5).
* The attackers come from a list of actors, 20 in total (EG bear, dog, cat, ex-wife etc.).
* It is OK if a wave of attackers contain duplicates of the same actor (IE 2 dogs and 1 cat).
* Each actor is weighted so as to influence its inclusion in a wave (IE dogs (weight 10) should appear twice as often as cats (weight 5)).
My initial spec (not tested yet in GS) is as follows:
Timer: Every 5 seconds
Set Attribute: game.wave_population = random(1,5) # we can have from 1 to 5 attackers per wave
Rule: if game.wave_population >= 1
Set Attribute: game.attacker_1 = random(1,100) # 100 equals the total weight of all actors added up
Rule: if game.attacker_1 = 1
Spawn Actor: the actor least likely to be seen
Rule: if game.attacker_1 > 1 && game.attacker_1 <= 11
Spawn Actor: the actor likely to be seen 10 times more frequently than the actor least likely to be seen
.. continue until all weights have been tested
Rule: if game.wave_population >= 2
Set Attribute: game.attacker_2 = random(1,100) # 100 equals the total weight of all actors added up
.. test all weights as above
.. repeat again for rules game.wave_population >= 3, >= 4, = 5
Seems kind of clunky. And like I said, I haven't tested it so I may not get the result I'm looking for which is:
* a random number of attackers per wave where we see some actors less frequently than others, a frequency which we can control and tweak using some sort of weighting system.
Hopefully someone already has an elegant solution that they are willing to share, thereby demonstrating their awesome and freakishly large intelligence
The problem:
* Every 5 seconds a wave of attackers proceeds to engage the game player.
* The number of attackers per wave is random (from 1 to a maximum of 5).
* The attackers come from a list of actors, 20 in total (EG bear, dog, cat, ex-wife etc.).
* It is OK if a wave of attackers contain duplicates of the same actor (IE 2 dogs and 1 cat).
* Each actor is weighted so as to influence its inclusion in a wave (IE dogs (weight 10) should appear twice as often as cats (weight 5)).
My initial spec (not tested yet in GS) is as follows:
Timer: Every 5 seconds
Set Attribute: game.wave_population = random(1,5) # we can have from 1 to 5 attackers per wave
Rule: if game.wave_population >= 1
Set Attribute: game.attacker_1 = random(1,100) # 100 equals the total weight of all actors added up
Rule: if game.attacker_1 = 1
Spawn Actor: the actor least likely to be seen
Rule: if game.attacker_1 > 1 && game.attacker_1 <= 11
Spawn Actor: the actor likely to be seen 10 times more frequently than the actor least likely to be seen
.. continue until all weights have been tested
Rule: if game.wave_population >= 2
Set Attribute: game.attacker_2 = random(1,100) # 100 equals the total weight of all actors added up
.. test all weights as above
.. repeat again for rules game.wave_population >= 3, >= 4, = 5
Seems kind of clunky. And like I said, I haven't tested it so I may not get the result I'm looking for which is:
* a random number of attackers per wave where we see some actors less frequently than others, a frequency which we can control and tweak using some sort of weighting system.
Hopefully someone already has an elegant solution that they are willing to share, thereby demonstrating their awesome and freakishly large intelligence
Comments
I'll let you know if I come up with a different solution as it's something I've thought about before...
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
Cat
Cookie Monster
Cookie Monster
Dog
Dog
Dog
Bear
Bear
Bear
Bear
Ex-wife
Ex-wife
Ex-wife
Ex-wife
Ex-wife
Ex-wife's lawyer
Ex-wife's lawyer
Ex-wife's lawyer
Ex-wife's lawyer
Ex-wife's lawyer
Ex-wife's lawyer
Lady Gaga
Lady Gaga
Lady Gaga
Lady Gaga
Lady Gaga
Lady Gaga
Lady Gaga
. . . . etc
And then randomly select between 1 and 5 of these values - with the weighting achieved by the number of times something appears in the table - example: 'Bear' is twice as likely to be selected than 'Cookie Monster' is.
"* Each actor is weighted so as to influence its inclusion in a wave (IE dogs (weight 10) should appear twice as often as cats (weight 5))."
'Should' might be the wrong word here (if I understand what it is you're trying to achieve?), if this is a random selection then although dogs are twice as likely to turn up in the selection you couldn't really say they 'should' turn up twice as often! I mean if you get 5 cats and no dogs then the process has still worked correctly.
Unless you always want twice as many dogs as cats regardless of the overall selection! i.e. If dogs as well as cats are selected as two of the 5 attackers (from the list of 20) then their weighting will mean you must have twice as many dogs as cats ?
@tatiang Thanks for taking the time to reply. I am using the rules so that I can record data to one of 5 actor variables. Also I was not certain how to spawn actors via a loop (EG for game.wave_population times, spawn actor)
Thanks for your help everyone!
You say that you don't know how to spawn actors using variables. What do you mean by that?
You can take a look at my Study of Loops demo to see various methods for looping using a counter: http://forums.gamesalad.com/discussion/comment/381275/#Comment_381275
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
For @socks solution I imagined (without the variable ability) I would have to test each result like so:
Rule: is database result == dog
Spawn Actor: Dog in pulldown
Otherwise: Rule: is database result == cat
with a nesting set of rules to test for each of the twenty actors... maybe I'm not understanding @socks solution or the nature of database/Spawn Actor operations sufficiently to see the solution
Also, since I can't store the results of the database operations in an array (as far as I know) I would have to perform these tests each time.
It would ideal to say something like:
select as results from the database limit game.wave_population
foreach result in results
spawn actor "result"
But I realize that GS isn't PHP.
Maybe I can pull the actor data (image, damage, points, size, bonuses etc.) from the database and then spawn a generic actor, changing all the pertinent details of the individual instance of the actor on the fly? I will have to study GS a little further...
Thanks for all the help. The GS community is strong!
It IS possible to pull all relevant data from a table and apply it to a generic actor. It's not easy. I have some demos that will help you get there... If I forget to post them tonight just @notify me.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User