Choose random actors from a weighted list...

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 :)





Comments

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    I think you have it set up correctly but I don't understand why you have separate rules for game.wave_population > 1 and game.wave_population > 2, etc. Wouldn't game.wave_population > 1 be enough? Are you nesting each rule? I would think that if, for example, game.wave_population = 4, you would just loop and do your set of weighted conditions four times.

    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

  • SocksSocks London, UK.Member Posts: 12,822
    edited January 2014
    A quick a dirty way would be to simply enter 100 values into a table . . .


    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.

  • SocksSocks London, UK.Member Posts: 12,822
    edited January 2014
    P.S . . .

    "* 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 ?
  • playincorporatedplayincorporated Member, PRO Posts: 18
    @socks Thanks for your reply. I thought about the database approach but I don't know of a way to spawn actors using variables. As for the frequency of appearance and the word "should", I probably should ;) have said "the dog (10) *should (over time)* appear twice as often as the cat (5)" hopefully that is a better way of phrasing it. Thanks for your help!

    @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!
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    I like @socks solution better than trying to code so many rule conditions for weighting.

    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

  • playincorporatedplayincorporated Member, PRO Posts: 18
    @tatiang -- thanks for the link to the loops file. I will study it tonight, very helpful indeed. As for spawning actors using variables: as I understand the Spawn Actor behaviour it only allows you to choose an actor from a pulldown list as opposed to using a variable to store the actor you wish to spawn and then using that variable in the Spawn Actor behaviour.

    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!
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    You're spot on in your analysis of how this will have to work.

    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

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited January 2014
    Here's something I just threw together. It doesn't loop and it doesn't calculate a certain number of actors for a wave... it just randomly spawns actors based on weighted table data per @Socks and then assigns them unique table data based on their type.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    It actually doesn't need game attributes at all. I originally made four animal actors before I remembered that you wanted a generic actor. I can optimize it a bit and post an update later.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • playincorporatedplayincorporated Member, PRO Posts: 18
    @tatiang thanks for the help! Totally generous, totally unexpected. Just "awesomed" all your posts... thanks!
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    That could be overboard. ;) You're very welcome and I'll get that optimized version to you in a second.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    Here's the next one.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • playincorporatedplayincorporated Member, PRO Posts: 18
    Thanks again @tatiang -- I'll post a file of the final solution once I finish it.
Sign In or Register to comment.