[Trigger] Random Array Shuffle [SOLVED]

Level 30
Joined
Feb 18, 2014
Messages
3,623
Hey everyone! Over the past few days I've been working on a puzzle system and I am struggling to get it to work properly.

I want a special effect (e.g: thunder clap) to appear on 9 units (e.g: Circle of Power) "randomly" and without "reappearing" twice on the same unit. After that, I want to store the exact order in which the special effect appeared on those units. Finally, I want to check whether the player selected those units based on the order the special effect appeared on them.

I've learned how to loop a random integer through an array without duplicating numbers using the Fisher-Yates Shuffle algorithm thanks to ChatGPT, but I'm not sure how to implement this algorithm in this puzzle and I also don't know exactly how to check the order the player selected the units. Here are my triggers:
  • Memory Init
    • Events
      • Unit - A unit enters MemoryPuzzleTripwire <gen>
    • Conditions
      • (Owner of (Triggering unit)) Equal to Player 1 (Red)
    • Actions
      • Trigger - Turn off (This trigger)
      • Set MemoryUnitArray[1] = Circle of Power 0337 <gen>
      • Set MemoryUnitArray[2] = Circle of Power 0338 <gen>
      • Set MemoryUnitArray[3] = Circle of Power 0339 <gen>
      • Set MemoryUnitArray[4] = Circle of Power 0340 <gen>
      • Set MemoryUnitArray[5] = Circle of Power 0341 <gen>
      • Set MemoryUnitArray[6] = Circle of Power 0342 <gen>
      • Set MemoryUnitArray[7] = Circle of Power 0343 <gen>
      • Set MemoryUnitArray[8] = Circle of Power 0344 <gen>
      • Set MemoryUnitArray[9] = Circle of Power 0345 <gen>
      • For each (Integer MemoryInteger) from 1 to 9, do (Actions)
        • Loop - Actions
          • Unit Group - Add MemoryUnitArray[MemoryInteger] to MemorySelection
          • Set MemoryShuffleArray[MemoryInteger] = MemoryUnitArray[MemoryInteger]
      • Visibility - Create an initially Enabled visibility modifier for Player 1 (Red) emitting Visibility across MemoryPuzzleVM <gen>
      • Set PuzzleVM = (Last created visibility modifier)
      • Wait 2.00 seconds
      • Trigger - Run Memory Random <gen> (checking conditions)
  • Memory Random
    • Events
    • Conditions
    • Actions
      • Cinematic - Disable user control for (All players)
      • -------- Shuffle --------
      • For each (Integer MemoryInteger) from 1 to 9, do (Actions)
        • Loop - Actions
          • Set MemoryShuffleArray[MemoryInteger] = MemoryUnitArray[MemoryInteger]
          • Set MemoryTempInteger = (Random integer number between 1 and 9)
          • Set MemoryTempUnit = MemoryShuffleArray[MemoryInteger]
          • Set MemoryShuffleArray[MemoryInteger] = MemoryShuffleArray[MemoryTempInteger]
          • Set MemoryShuffleArray[MemoryTempInteger] = MemoryTempUnit
      • -------- Special Effect --------
      • For each (Integer MemoryInteger) from 1 to 9, do (Actions)
        • Loop - Actions
          • Wait 1.00 seconds
          • Set TempPoint = (Position of MemoryShuffleArray[MemoryInteger])
          • Special Effect - Create a special effect at TempPoint using Abilities\Spells\Items\AIam\AIamTarget.mdl
          • Special Effect - Destroy (Last created special effect)
          • Custom script: call RemoveLocation (udg_TempPoint)
          • Set MemoryEffectOrder[MemoryInteger] = MemoryShuffleArray[MemoryInteger]
      • Trigger - Turn on Memory Select <gen>
      • Cinematic - Enable user control for (All players)
  • Memory Select
    • Events
      • Player - Player 1 (Red) Selects a unit
    • Conditions
      • ((Triggering unit) is in MemorySelection) Equal to True
    • Actions
      • Set MemorySelectionOrder[MemorySelectionIndex] = (Triggering unit)
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • MemorySelectionOrder[MemorySelectionIndex] Equal to MemoryEffectOrder[MemorySelectionIndex]
        • Then - Actions
          • Set MemorySelectionIndex = (MemorySelectionIndex + 1)
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • MemorySelectionIndex Equal to 9
            • Then - Actions
              • Trigger - Turn off (This trigger)
              • Quest - Display to (All players) the Quest Completed message: |cffffcc00SUCCESS!|r
            • Else - Actions
              • Trigger - Turn off (This trigger)
              • Quest - Display to (All players) the Quest Failed message: |cffffcc00FAILED!|r
              • Wait Campaign quest delay seconds
              • Trigger - Run Memory Random <gen> (checking conditions)
        • Else - Actions
Variables:
  • MemoryInteger = Integer (1)
  • MemoryEffectOrder = Unit (Array)
  • MemorySelection = Unit Group
  • MemorySelectionIndex = Integer (1)
  • MemorySelectionOrder = Unit (Array)
  • MemoryShuffleArray = Unit (Array)
  • MemoryTempInteger = Integer
  • MemoryTempUnit = Unit
  • MemoryUnitArray = Unit (Array)
The special effect still appear on the same unit twice and when I select a unit I get an instant failed message. Does anyone know what I did wrong here? Thanks.
 
Level 30
Joined
Feb 18, 2014
Messages
3,623
Okay, I finally got it working! :grin:
  • Memory Init
    • Events
      • Unit - A unit enters MemoryPuzzleTripwire <gen>
    • Conditions
      • (Owner of (Triggering unit)) Equal to Player 1 (Red)
    • Actions
      • Trigger - Turn off (This trigger)
      • Set MemoryUnitArray[1] = Circle of Power 0337 <gen>
      • Set MemoryUnitArray[2] = Circle of Power 0338 <gen>
      • Set MemoryUnitArray[3] = Circle of Power 0339 <gen>
      • Set MemoryUnitArray[4] = Circle of Power 0340 <gen>
      • Set MemoryUnitArray[5] = Circle of Power 0341 <gen>
      • Set MemoryUnitArray[6] = Circle of Power 0342 <gen>
      • Set MemoryUnitArray[7] = Circle of Power 0343 <gen>
      • Set MemoryUnitArray[8] = Circle of Power 0344 <gen>
      • Set MemoryUnitArray[9] = Circle of Power 0345 <gen>
      • For each (Integer A) from 1 to 9, do (Actions)
        • Loop - Actions
          • Unit Group - Add MemoryUnitArray[Integer A] to MemorySelection
          • Set MemoryShuffleArray[Integer A] = MemoryUnitArray[Integer A]
      • Visibility - Create an initially Enabled visibility modifier for Player 1 (Red) emitting Visibility across MemoryPuzzleVM <gen>
      • Set PuzzleVM = (Last created visibility modifier)
      • Wait 2.00 seconds
      • Trigger - Run Memory Random <gen> (checking conditions)
  • Memory Random
    • Events
    • Conditions
    • Actions
      • Cinematic - Disable user control for (All players)
      • -------- Shuffle --------
      • For each (Integer A) from 1 to 9, do (Actions)
        • Loop - Actions
          • Set MemoryTempInteger = (Random integer number between (Integer A) and 9)
          • Set MemoryTempUnit = MemoryShuffleArray[(Integer A)]
          • Set MemoryShuffleArray[(Integer A)] = MemoryShuffleArray[MemoryTempInteger]
          • Set MemoryShuffleArray[MemoryTempInteger] = MemoryTempUnit
          • Wait 1.00 seconds
          • Set TempPoint = (Position of MemoryShuffleArray[(Integer A)])
          • Special Effect - Create a special effect at TempPoint using Abilities\Spells\Demon\DarkPortal\DarkPortalTarget.mdl
          • Special Effect - Destroy (Last created special effect)
          • Custom script - call RemoveLocation (udg_TempPoint)
          • Set MemoryUnitEffect[(Integer A)] = MemoryShuffleArray[(Integer A)]
      • -------- Selection --------
      • Trigger - Turn on Memory Select <gen>
      • Cinematic - Enable user control for (All players)
  • Memory Select
    • Events
      • Player - Player 1 (Red) Selects a unit
    • Conditions
      • ((Triggering unit) is in MemorySelectGroup) Equal to True
    • Actions
      • Set MemorySelectCount = (MemorySelectCount + 1)
      • Set MemorySelectOrder[MemorySelectCount] = (Triggering unit)
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • MemorySelectOrder[MemorySelectCount] Equal to MemoryUnitEffect[MemorySelectCount]
        • Then - Actions
          • Set MemoryIndex = (MemoryIndex + 1)
        • Else - Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • MemorySelectCount Equal to 9
        • Then - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • MemoryIndex Equal to 9
            • Then - Actions
              • Trigger - Turn off (This trigger)
              • Game - Display to (All players) the text: SUCCESS!
            • Else - Actions
              • Trigger - Turn off (This trigger)
              • Game - Display to (All players) the text: FAILED!
              • Set MemorySelectCount = 0
              • Set MemoryIndex = 0
        • Else - Actions
 
Top