shadowm

Unit swapping algorithm in case I ever want to fix it again

Feb 11th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. [event]
  2. name=player hits blood core
  3. first_time_only=no
  4.  
  5. {BUG_ON ({VARIABLE_NUMERICAL_NOT_EQUALS boss_stage 2}) ("Blood Core handler running not on boss stage 2 ($boss_stage|)")}
  6.  
  7. #
  8. # Shuffle Blood Cores around whenever possible.
  9. #
  10.  
  11. [store_unit]
  12. [filter]
  13. type=Blood Core
  14. [filter_location]
  15. find_in=core_locs
  16. [/filter_location]
  17. [/filter]
  18. variable=shuffle_stack
  19. kill=yes
  20. [/store_unit]
  21.  
  22. #
  23. # This shuffling algorithm should work in every case regardless of the amount
  24. # of units stored. If there's a single unit stored, it is unstored back with
  25. # minimal overhead (*) below. If there are no units stored, the loop never
  26. # runs, and the bugcheck path is not hit.
  27. #
  28.  
  29. {REVERSE_FOREACH shuffle_stack k}
  30.  
  31. # k is the position of the primary unit to swap, set by the foreach cycle.
  32. # r is the position of teh secondary unit to swap, determined by the RNG.
  33. {VARIABLE r -1}
  34.  
  35. [if]
  36. {VARIABLE_NUMERICAL_GREATER_THAN k 0}
  37. [then]
  38. {VARIABLE_RANDOM r "0..$($k - 1)"}
  39.  
  40. {LOG_ATS_DBG ("SWAP: $k <-> $r <$shuffle_stack.length>")}
  41.  
  42. {VARIABLE swap.x $shuffle_stack[$r].x}
  43. {VARIABLE swap.y $shuffle_stack[$r].y}
  44.  
  45. {LOG_ATS_DBG ("SWAP: ($shuffle_stack[$k].x,$shuffle_stack[$k].y) <-> ($swap.x,$swap.y)")}
  46.  
  47. {VARIABLE shuffle_stack[$r].x $shuffle_stack[$k].x}
  48. {VARIABLE shuffle_stack[$r].y $shuffle_stack[$k].y}
  49.  
  50. {VARIABLE shuffle_stack[$k].x $swap.x}
  51. {VARIABLE shuffle_stack[$k].y $swap.y}
  52.  
  53. {LOG_ATS_DBG ("SWAP: ($shuffle_stack[$r].x,$shuffle_stack[$r].y) ($shuffle_stack[$k].x,$shuffle_stack[$k].y)")}
  54.  
  55. [unstore_unit]
  56. variable="shuffle_stack[$r]"
  57. find_vacant=yes
  58. check_passability=no
  59. [/unstore_unit]
  60.  
  61. {CLEAR_VARIABLE swap}
  62. [/then]
  63. [/if]
  64.  
  65. # (*)
  66.  
  67. [unstore_unit]
  68. variable="shuffle_stack[$k]"
  69. find_vacant=yes
  70. [/unstore_unit]
  71.  
  72. [if]
  73. {VARIABLE_NUMERICAL_GREATER_THAN r -1}
  74. [then]
  75. [floating_text]
  76. x=$shuffle_stack[$k].x,$shuffle_stack[$r].y
  77. y=$shuffle_stack[$k].y,$shuffle_stack[$r].y
  78. text="<span color='#FF7F00'>!</span>" # wmllint: ignore
  79. [/floating_text]
  80.  
  81. {CLEAR_VARIABLE shuffle_stack[$r]}
  82.  
  83. {VARIABLE_DEC k}
  84. [/then]
  85. [/if]
  86.  
  87. {CLEAR_VARIABLE r,shuffle_stack[$k]}
  88.  
  89. {REVERSE_NEXT k}
  90.  
  91. # We shouldn't hit this unless the shuffling algorithm is broken.
  92. {BUG_ON ({VARIABLE_NUMERICAL_GREATER_THAN shuffle_stack.length 0}) ("Unit shuffling algorithm postcondition broken")}
  93. [/event]
Advertisement
Add Comment
Please, Sign In to add comment