Run random action. And make it possible to remove one of the options?

Discussion in 'Starcraft 2 (SC2) Editor Help' started by X-maul, Dec 16, 2012.

  1. X-maul

    X-maul AKA: Demtrod

    Ratings:
    +200 / 0 / -0
    I want to make a selection system, where I can choose from A, B, C and D. If I choose A, A will be disabled for all players, this is pretty simple as it is right now. But I also want to make it possible to pick a random, so if I pick A and another player selects random, he will get a random from A, B, C and D. But A is no longer an option.

    How can I do this?
    To clarify even more; a system that takes a random from X amount of options, with the option to remove one of the options from the random group.
     
  2. Siretu

    Siretu Starcraft 2 Editor Moderator Staff Member

    Ratings:
    +295 / 0 / -0
    I think it's hard but definitely not impossible to do it in a proper way.

    The way I'd do it is probably easier though.

    Save the removed options somehow. Maybe a boolean array. So if removedOptions[0] is true, option A is removed. When you random an option, have a while-loop that keeps randoming a new number as long as removedOptions[randNumber] is true.

    Basically, make it random a new option until it finds one that isn't removed.

    You might need a check to check if they're all removed so you don't get stuck in an infinite loop if it's possible to remove all options.
     
    • Like Like x 1
  3. X-maul

    X-maul AKA: Demtrod

    Ratings:
    +200 / 0 / -0
    Yeah, I thought of keeping randomizing untill I found something which wasnt disabled :) But I just wanted to know if there was a more efficient way to do it :)
    But thanks anyway Siretu :)

    It's for a hero selection system, so I'll need more hero options than the max amount of players ;)
     
  4. Phubar

    Phubar Well-Known Member

    Ratings:
    +30 / 0 / -0
    Tomorrow i will post the trigger :) plz take a look to my suggestion :)
     
  5. X-maul

    X-maul AKA: Demtrod

    Ratings:
    +200 / 0 / -0
    I already have it planned out (not done yet though), but please give it a go :)
     
  6. Phubar

    Phubar Well-Known Member

    Ratings:
    +30 / 0 / -0
    Let me understand, you have many heros available (how many?) and only four players that have to choose between them, right?
     
  7. Phubar

    Phubar Well-Known Member

    Ratings:
    +30 / 0 / -0
    I wrote this with openoffice writer, not with the sc2 editor so it's not a real trigger but a list of instructions as near to the editor actions as posible.

    I assumed you have 9 heroes in your pool.

    You need GLOBAL VARIABLES (with starting values)


    MaxRdm=9 (integer)
    SelectedType=NoUnitType (unit type)
    HeroeType[9]= (array unit type)
    HeroP1=NoUnit (unit)
    HeroP2=NoUnit (unit)
    HeroP3=NoUnit (unit)
    HeroP4=NoUnit (unit)

    You have to start filling the array with a unit type foe each index.


    Code:
    TRIGGER "Click button" (one trigger for each player)
     
    Event
        Player1 click on a button
     
    Action
        case "buttonRaynor"
            Create 1 RaynorType for Player1
            Set HeroP1= last created unit
        case "buttonZeratul"
            Create 1 ZeratulType for Player1
            Set HeroP1= last created unit
        case "buttonKerrighan"
            Create 1 KerrighanType for Player1
            Set HeroP1= last created unit
        ...
        ...
        case "buttonRandom"
            Execute trigger "Random choose" wait the end
            Create 1 SelectedType for Player1
            Set HeroP1= last created unit
            Set SelectedType=NoUnitType

    Code:
    TRIGGER "Random choose"
     
    Event
        none
     
    Local variables
        IndexRdm=0 (integer)
        Counter=0 (integer)
     
    Actions
        Set IndexRdm= random integer between 1 and MaxRdm
        Set SelectedType= HeroeType[IndexRdm]
        For Counter=  IndexRdm to MaxRdm do
            Set HeroeType[Counter]=HeroeType[counter+1]
        Set MaxRdm=MaxRdm-1

    This will work i hope.. cannot check it :(
    The only problem is when a player clicks on "random" button before the instance of the previous "random choose" trigger is not finished... you have to add some "conditions" like

    Code:
    TRIGGER "Click button" (one trigger for each player)
     
    Add actions at beginning
     
    Set EnableP2=F (boolean)
    Set EnableP3=F (boolean)
    Set EnableP4=F (boolean)
     
    an then this action after case "buttonRandom"
     
    wait until EnableP1=T
     
    And the actions at the end
     
    Set EnableP2=T (boolean)
    Set EnableP3=T (boolean)
    Set EnableP4=T (boolean)

    Of couyrse you need four boolean global variables EnableP, EnableP2, EnableP3, EnableP4.
     
  8. Dave312

    Dave312 Censored for your safe viewing

    Ratings:
    +269 / 0 / -0
    The easy way to do it is to create a local array where the maximum length is the maximum number of options. Then cycle through each option and if the option is available, add it to your local array. Then just get a random index from your local array.
     
    • Like Like x 1
  9. Phubar

    Phubar Well-Known Member

    Ratings:
    +30 / 0 / -0
    Dave you are right, my trigger does this by deleting the choosen option and sliding "down" the next ones. Then it "cuts" the array setting the maximum index 1 unit smaller.

    The trigger that handles the random choice is the second one, the frist is for the selection of specific hero or random selection.

    The third part of code is only an add in order to avoid that the random trigger is executed more than one instance at the time.
     
  10. Phubar

    Phubar Well-Known Member

    Ratings:
    +30 / 0 / -0
    Sorry Dave was totally right!

    My trigger works only if all players choose random... if a palyer chooses a specific hero it's hard to delete it's option in the array and modify the array in order to prevent that a random selecion cauld choose the same hero too.
     
  11. X-maul

    X-maul AKA: Demtrod

    Ratings:
    +200 / 0 / -0
    Yeah, you're most likely right Dave312. But using this method wont make it random? As far as I can see, the method Phubar suggests is flawed; If Player 1 and 2 selects an option, now these are the available options;
    Option 1
    Option 2
    Option 3
    Option 4
    Option 5
    Option 6
    Option 7
    Option 8
    Now player 3 selects random, how will your system check if it's available?

    I think I'll use Siretu's suggestion, as I belive it's the easiest way, the most efficient and the most random.

    EDIT: I made a custom Action, othervise I would'nt have been able to determine which player the trigger was run for, so I made this;
    Code:
    Random Hero
        Options: Action
        Return Type: (None)
        Parameters
            Player = 0 <Integer>
        Grammar Text: Create a random Hero for (Player)
        Hint Text: (None)
        Custom Script Code
        Local Variables
            Random = (Random integer between 0 and 12) <Integer>
            ActionDone = False <Boolean>
        Actions
            General - While (Conditions) are true, do (Actions)
                Conditions
                    ActionDone == False
                Actions
                    General - If (Conditions) then do (Actions) else do (Actions)
                        If
                            Hero Is Available[Random] == True
                        Then
                            Unit - Create 1 HeroTypes[Random] for player Player at (Start location of player Player) facing Energy Fountain Center (No Options)
                            Variable - Set ActionDone = True
                            Variable - Set Hero Is Available[Random] = False
                            General - Skip remaining actions
                        Else
                            Variable - Set Random = (Random integer between 0 and 12)
    
    EDIT 2: The action does not work? How come?
    (I'm just running the action 8 times each
    EDIT 3: The action works? After I shut down my computer and turned it on again I inserted some debug messages to troubleshoot, and then it worked on the first try.
     
  12. Dave312

    Dave312 Censored for your safe viewing

    Ratings:
    +269 / 0 / -0
    Why wouldn't the value be random? You're just getting a random option from the available options. It would not be biased against any particular option as every option has an equal chance of being selected. This is the method I am using in my map and I haven't had any problems with it.

    It looks like it should work. Did you remember to set the starting values of your Hero Is Available variable to true?
     
  13. X-maul

    X-maul AKA: Demtrod

    Ratings:
    +200 / 0 / -0
    I asume I do not fully understand the system you are suggesting then, could you give me a trigger example? (nothing complex, just an example)
    For some reason it does now, I dont know why it didnt before? But now it works. I'm still very eager to see how your method would work?
     
  14. Dave312

    Dave312 Censored for your safe viewing

    Ratings:
    +269 / 0 / -0
    Nope it will not.

    Trigger:
    • Random Hero
      • Options: Action
      • Return Type: (None)
      • Parameters
        • Player = 0 <Integer>
      • Grammar Text: Random Hero(Player)
      • Hint Text: (None)
      • Custom Script Code
      • Local Variables
        • AvailableList = 0 <Integer[12]>
        • i = 0 <Integer>
        • Count = 0 <Integer>
      • Actions
        • General - For each integer i from 0 to 12 with increment 1, do (Actions)
          • Actions
            • General - If (Conditions) then do (Actions) else do (Actions)
              • If
                • Hero Is Available[i] == True
              • Then
                • ------- Add Hero to Available List
                • Variable - Set AvailableList[Count] = i
                • Variable - Modify Count: + 1
              • Else
        • ------- Get a random Hero
        • Variable - Set i = (Random integer between 0 and (Count - 1))
        • Variable - Set Hero Is Available[AvailableList[i]] = False
        • Unit - Create 1 HeroTypes[AvailableList[i]] for player Player at (Start location of player Player facing Energy Fountain Center (No Options)
     
    • Like Like x 1
  15. X-maul

    X-maul AKA: Demtrod

    Ratings:
    +200 / 0 / -0
    Haha, for some reason I coult NOT imagine this in my head before now :rolleyes: thank you Dave312 :) I'll use this instead, as it seems to be easier for the computer to handle, than the continues randomizing.

    NOTE: I cant quite see why you have to find a random integer between 0 and (Count - 1)?
    (Why the -1?)
     

Share This Page