Advertisement
LiTTleDRAgo

[RGSS] Heretic's Unlimited Battle Page Conditions (edited)

Aug 18th, 2014
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 15.08 KB | None | 0 0
  1. #=============================================================================
  2. #
  3. #           Unlimited Battle Page Conditions
  4. #           Authors: Heretic (edited by LiTTleDRAgo)
  5. #           Version 1.0
  6. #           Monday, January 20th, 2014
  7. #
  8. #-----------------------------------------------------------------------------
  9. #
  10. #  ---   OVERVIEW   ---
  11. #
  12. #  This script will allow you to add an Unlimited Number of Page Conditions
  13. #  for Battle Events by using Comments as a Condition on each Battle Page
  14. #
  15. #  ---   IMPORTANT!   ---
  16. #
  17. #  The Conditions that you put in to the Comment MUST be able to evaluate
  18. #  to true / false!
  19. #
  20. #  What this means is that a Condition of var = 1 will run, but not evaluate
  21. #  to true, so it wont ever be recognized as a valid condition.  By placing
  22. #  a Condition of "if var == 1" will return true, which makes it valid when
  23. #  the proper conditions are met.
  24. #
  25. #  ---   USAGE   ---
  26. #
  27. #  Create a Condition by making a Comment ANYWHERE on a Battle Event Page
  28. #  with "condition:" followed by your Scripted Condition in the Comment.
  29. #
  30. #  Example #1:  
  31. #
  32. #  @>Comment:  condition:  if $foo == $bar
  33. #
  34. #
  35. #  Comment Conditions will run Eval for ALL of the contents for each Comment
  36. #  all at once.  This allows you to have a Multi Line Condition.
  37. #
  38. #  Example #2:  
  39. #
  40. #  @>Comment:  condition:  if ($foo == $bar and
  41. #                             $lorem == $ipsum)
  42. #
  43. #
  44. #  Comments run as Scripts just like a Script Event, so you can put anything
  45. #  into the Script.  Loops, Iterations, Create Variables, Self Switches, etc.
  46. #
  47. #  Example #3  
  48. #
  49. #  @>Comment:  condition:  result = false
  50. #                          for actor in $game_party.actors
  51. #                            if actor.weapon_id == 12
  52. #                              result = true
  53. #                            end
  54. #                          end
  55. #                          return true if result == true
  56. #
  57. #
  58. #  Each Comment Condition you add will add another Condition to check before
  59. #  the Battle Event Page can be run.  This allows you to add as many Conditions
  60. #  as you want.  THERE IS NO LIMIT.  Each Comment Condition is considered as
  61. #  its own Condition.
  62. #
  63. #  Example #4:  
  64. #
  65. #  @>Comment: condition: $game_party.actors[0].hp == $game_party.actors[0].maxhp &&
  66. #  @>Comment: $game_party.actors[0].id == 2
  67. #
  68. #  In the above example, BOTH Conditions MUST BE MET for the Battle Event Page
  69. #  to be considered Valid and thus run.
  70. #
  71. #
  72. #  You can use OR Statements inside of a single Comment Condition allowing for
  73. #  greater flexibility of Battle Page Conditions.
  74. #
  75. #  Examle #5:
  76. #
  77. #  @>Comment: condition: if $foo == $bar OR $lorem == $ipsum
  78. #
  79. #
  80. #  ANY Sript Logic can be used provided it fits in the comment.  Its all
  81. #  evaluated the same way.
  82. #
  83. #  ---   BATTLE EVENTING   ---
  84. #
  85. #  Battle Eventing has a Menu for a few things already.  Such as checking to
  86. #  make sure a certain Switch is on before running the Battle Event Page.  You
  87. #  can now use a Comment Condition to check if a certain Switch is OFF before
  88. #  running a Battle Event Page.
  89. #
  90. #  Battle Event also uses what is called SPAN.
  91. #
  92. #  SPAN controls when and how many times to run a Battle Event Page.
  93. #
  94. #  Span:   Battle - Page runs ONCE per the entire Battle
  95. #  Turn:   Page runs ONCE per Turn, even if conditions are still met.
  96. #  Moment: Runs when Conditions are met, and continues to run as long as
  97. #          those conditions are met.
  98. #
  99. #  Comment Conditions are EXTREMELY USEFUL for allowing a Span Moment Page
  100. #  to STOP RUNNING.  It allows you to run the Page once.  Like a specific
  101. #  enemy has a State of "Immune to Fire", run some dialogue once, then
  102. #  use some variable to set that the page has run already.  You could use
  103. #  a Switch, or even any other variable like @this_page_ran = true, and set
  104. #  that as a Condition.
  105. #
  106. #  Example #6
  107. #
  108. #  @>Comment:  condition:  if $game_troop.enemies[0].states.include?(12)
  109. #                            @this_new_var_for_page_ran = true
  110. #                          else
  111. #                            unless @this_new_var_for_page_ran
  112. #                              @this_new_var_for_page_ran = false
  113. #                            end
  114. #                          end
  115. #  @>Conditional Branch:  Script: @this_new_var_for_page_ran != true
  116. #    @>Text:  "Don't cast Fire on him!  He is now Immune to Fire!"
  117. #  @>End Branch
  118. #
  119. #  ---   COMMAND LIST   ---
  120. #
  121. #  I'd like to make this as easy as possible for people to use.  So to do that
  122. #  these list of Commands should make adding Comment Conditions easier.
  123. #
  124. #  Example of Command:
  125. #
  126. #  - item_in_inventory?(item_id)
  127. #  
  128. #  @>Comment  condition: # If Party has a Potion in their Inventory
  129. #                        if item_in_inventory(1)
  130. #
  131. #
  132. #  Command List
  133. #  - item_in_inventory?(item_id)
  134. #
  135. #-----------------------------------------------------------------------------
  136. #==============================================================================
  137. # ** Scene_Battle
  138. #------------------------------------------------------------------------------
  139. #  This class performs battle screen processing.
  140. #==============================================================================
  141. class Scene_Battle
  142.   #--------------------------------------------------------------------------
  143.   # * Battle Event Setup
  144.   #
  145.   #   - Replacement for default XP Battle System setup_battle_event
  146.   #   - Any Script Conflicts would most likely come from this method
  147.   #--------------------------------------------------------------------------
  148.   def setup_battle_event
  149.     # If battle event is running
  150.     return if $game_system.battle_interpreter.running?
  151.     # Search for all battle event pages
  152.     for index in 0...$data_troops[@troop_id].pages.size
  153.       # Get event pages
  154.       page = $data_troops[@troop_id].pages[index]
  155.       # Make event conditions possible for reference with c
  156.       c = page.condition
  157.      
  158.       # This code is mostly default with the addition of "page_condition_valid"
  159.       # Go to next page if no conditions are appointed      
  160.       unless c.turn_valid || c.enemy_valid || c.actor_valid || c.switch_valid ||
  161.         page.cond_eval_valid
  162.         next
  163.       end
  164.      
  165.       # Default code
  166.       # Go to next page if action has been completed
  167.       next if $game_temp.battle_event_flags[index]
  168.       # Confirm turn conditions
  169.       if c.turn_valid
  170.         n, a, b = $game_temp.battle_turn, c.turn_a, c.turn_b
  171.         m = (n < 1 || n < a || n % b != a % b)
  172.         next if (b == 0 && n != a) || (b > 0 && m)
  173.       end
  174.       # Confirm enemy conditions
  175.       if c.enemy_valid
  176.         enemy = $game_troop.enemies[c.enemy_index]
  177.         next if enemy == nil or enemy.hp * 100.0 / enemy.maxhp > c.enemy_hp
  178.       end
  179.       # Confirm actor conditions
  180.       if c.actor_valid
  181.         actor = $game_actors[c.actor_id]
  182.         next if actor == nil or actor.hp * 100.0 / actor.maxhp > c.actor_hp
  183.       end
  184.       # Confirm switch conditions
  185.       if c.switch_valid
  186.         next unless $game_switches[c.switch_id]
  187.       end
  188.      
  189.       # This small block is also all that was added to the Default code
  190.       # Confirm Page Condition (if Additional Page Condition exists)
  191.       if (cond = page.cond_eval_valid)
  192.         # If running the extra Page Condition returns false (which is default)
  193.         next unless eval(cond)
  194.       end
  195.      
  196.       # The rest of the code in this method is Default
  197.       # Set up event
  198.       $game_system.battle_interpreter.setup(page.list, 0)
  199.       # If this page span is [battle] or [turn]
  200.       # Set action completed flag
  201.       $game_temp.battle_event_flags[index] = true if page.span <= 1
  202.       return
  203.     end
  204.   end
  205.   #--------------------------------------------------------------------------#
  206.   # **  Custom Commands for Easy Checks for Conditional Switches             #
  207.   #                                                                          #
  208.   #             ALL METHODS MUST RETURN TRUE OR FALSE                        #
  209.   #--------------------------------------------------------------------------#
  210.  
  211.   #--------------------------------------------------------------------------
  212.   # * Item In Inventory
  213.   #     item_id : Item Id from Database
  214.   #
  215.   #   Example: 1 is a Potion
  216.   #            # If Party has a Potion in the Inventory
  217.   #            item_in_inventory?(1)
  218.   #
  219.   #            2 is a High Potion
  220.   #            # If Party has a High Potion in the Inventory
  221.   #            item_in_inventory?(2)
  222.   #
  223.   #  This is an Example Script.  It could be accomplished by copying and
  224.   #  pasting the one "return true if ..." line of code.  I provided this
  225.   #  because it makes it easy for us to script easier condition checks
  226.   #  for others that may use this script.
  227.   #--------------------------------------------------------------------------
  228.   def item_in_inventory?(item_id)
  229.     # If Item is in Inventory, return True
  230.     return true if $game_party.item_number(item_id) > 0
  231.     # Default
  232.     return false
  233.   end
  234. end
  235.  
  236. #==============================================================================
  237. # ** Game_Enemy
  238. #------------------------------------------------------------------------------
  239. #  This class handles enemies. It's used within the Game_Troop class
  240. #  ($game_troop).
  241. #==============================================================================
  242. class Game_Enemy
  243.   #--------------------------------------------------------------------------
  244.   # * Public Instance Variables
  245.   #--------------------------------------------------------------------------
  246.   attr_accessor :escaped                   # escaped flag
  247.   #--------------------------------------------------------------------------
  248.   # * Alias Listing
  249.   #--------------------------------------------------------------------------
  250.   $@ || alias_method(:escaped_flag_addition, :escape)
  251.   #--------------------------------------------------------------------------
  252.   # * Escape
  253.   #--------------------------------------------------------------------------
  254.   define_method(:escape){|*s| (@escaped = true) && escaped_flag_addition(*s)}
  255. end
  256.  
  257. #==============================================================================
  258. # ** Scene_Battle
  259. #------------------------------------------------------------------------------
  260. #  This class performs battle screen processing.
  261. #==============================================================================
  262. class Scene_Battle
  263.   #--------------------------------------------------------------------------
  264.   # * Alias Listing
  265.   #--------------------------------------------------------------------------
  266.   $@ || alias_method(:escaped_judge, :judge)
  267.   #--------------------------------------------------------------------------
  268.   # * Shortcut
  269.   #--------------------------------------------------------------------------
  270.   define_method(:all_visible_defeated?) { visible_enemies.empty?   }
  271.   define_method(:all_hidden_defeated?)  { unescaped_enemies.empty? }
  272.   #--------------------------------------------------------------------------
  273.   # * Frame Update (main phase step 1 : action preparation)
  274.   #--------------------------------------------------------------------------
  275.   def judge(*args)
  276.     if all_visible_defeated? #&& !all_hidden_defeated?
  277.       setup_battle_event
  278.       return false if $game_system.battle_interpreter.running?
  279.     end
  280.     escaped_judge(*args)
  281.   end
  282.   #--------------------------------------------------------------------------
  283.   # * unescaped_enemies
  284.   #--------------------------------------------------------------------------
  285.   def unescaped_enemies
  286.     $game_troop.enemies.select {|e| !e.dead? && e.hidden && !e.escaped }
  287.   end
  288.   #--------------------------------------------------------------------------
  289.   # * visible_enemies
  290.   #--------------------------------------------------------------------------
  291.   def visible_enemies
  292.     $game_troop.enemies.select {|e| !e.dead? && !e.hidden }
  293.   end  
  294. end
  295.  
  296.  
  297. #==============================================================================
  298. # ** Interpreter
  299. #------------------------------------------------------------------------------
  300. #  This interpreter runs event commands. This class is used within the
  301. #  Game_System class and the Game_Event class.
  302. #==============================================================================
  303. class Interpreter
  304.   #--------------------------------------------------------------------------
  305.   # * Alias Listing
  306.   #--------------------------------------------------------------------------
  307.   $@ || alias_method(:escaped_command_335, :command_335)
  308.   #--------------------------------------------------------------------------
  309.   # * Enemy Appearance
  310.   #--------------------------------------------------------------------------
  311.   def command_335(*args)
  312.     enemy = $game_troop.enemies[@parameters[0]]
  313.     enemy && enemy.escaped = false
  314.     escaped_command_335(*args)
  315.   end
  316. end
  317.  
  318. #==============================================================================
  319. # ** RPG::Troop::Page
  320. #------------------------------------------------------------------------------
  321. #  
  322. #==============================================================================
  323. class RPG::Troop::Page
  324.   #--------------------------------------------------------------------------
  325.   # * cond_eval_valid
  326.   #--------------------------------------------------------------------------
  327.   def cond_eval_valid
  328.     @cond_eval.nil? ? @cond_eval ||= get_cond_eval : @cond_eval
  329.   end
  330.   #--------------------------------------------------------------------------
  331.   # * Eval Page Conditions
  332.   #   - Finds ALL Page Conditions in Comments, then Evaluates them
  333.   #
  334.   #   NOTE: Conditions MUST be able to return a Value
  335.   #         $foo = "Bar" will not return anything, thus evals to False
  336.   #         if $foo == "Bar" will return either True or False
  337.   #
  338.   #   This code will check to enusre ALL of the Comment Conditions on a
  339.   #   Battle Event Page are met.  If you have Two Conditions, both must
  340.   #   eval to True for an Event Page to be considered "valid".  Each Comment
  341.   #   Condition is considered as a Separate Condition.  Everything in the
  342.   #   Comment Box is evaluated all at once, so you can create temporary
  343.   #   variables, Loops, OR statements, etc as long as it is all contained
  344.   #   in ONE Comment.  IE "if $foo == $bar OR actor_id == 2".  Complex
  345.   #   scripts may adversely affect performance.
  346.   #
  347.   #   This is NEW CODE and should not cause any Script Conflicts.
  348.   #
  349.   #--------------------------------------------------------------------------
  350.   def get_cond_eval
  351.     for i in 0...list.size
  352.       # If Command is First Line of a Comment
  353.       next unless list[i].code == 108
  354.       # Check each Commment if it has "condition:" as String
  355.       condition = $1 if list[i].parameters[0][/^condition:\s*(.*)/i]
  356.       # If Battle Condition in a Comment
  357.       next unless condition
  358.       # Check for more lines in the Comment to Eval all at once
  359.       while (list[i + 1] and [108,408].include?(list[i + 1].code))
  360.         # Add Next Line of Comment (code 408) to Condition to eval
  361.         condition += "\n" + list[i += 1].parameters[0]
  362.       end
  363.       return condition
  364.     end
  365.     return false
  366.   end
  367. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement