Szyu

StateConditions

Jul 26th, 2015
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.48 KB | None | 0 0
  1. #==============================================================================
  2. # ** StateConditions
  3. #
  4. # * Author:   Szyu
  5. # * Version:  1.3
  6. # * Pastebin: http://adf.ly/4920670/stateconditions
  7. # * Description:
  8. #     Adds the possibilty to invoke state features only when specific state
  9. #     conditions are met.
  10. #
  11. #
  12. # * How To Use:
  13. #   "<state_condition: id>" in states notes where id represents the state id
  14. #
  15. # * Changelog
  16. #   - Fixed a major bug which causes state conditions not to work as intended
  17. #   - Added new function: States with conditions are removed,
  18. #     if a condition state is removed
  19. #   - Added independent state. If a state condition is independent, the state
  20. #     will stay applied even if the condition state is removed
  21. #
  22. #==============================================================================
  23. $imported = {} if $imported.nil?
  24. $imported["Szyu_StateConditions"] = true
  25. #==============================================================================
  26. # ** Game_Battler
  27. #==============================================================================
  28. class Game_Battler < Game_BattlerBase
  29.   alias szstco_state_addable? state_addable?
  30.   alias szstci_erase_state erase_state
  31.   #--------------------------------------------------------------------------
  32.   # * Determine if States Are Addable
  33.   #--------------------------------------------------------------------------
  34.   def state_addable?(state_id)
  35.     res = szstco_state_addable?(state_id)
  36.     res && state_conditions_met(state_id)
  37.   end
  38.   #--------------------------------------------------------------------------
  39.   # * Erase States
  40.   #--------------------------------------------------------------------------
  41.   def erase_state(state_id)
  42.     szstci_erase_state(state_id)
  43.     @states.each do |sid|
  44.       conditions = $data_states[sid].state_conditions
  45.       if conditions
  46.         conditions.each do |sc|
  47.           if !sc[1] && !state?(sc[0])
  48.             @states.delete(sid)
  49.             @state_turns.delete(sid)
  50.             @state_steps.delete(sid)
  51.             break
  52.           end
  53.         end
  54.       end
  55.     end
  56.   end
  57.   #--------------------------------------------------------------------------
  58.   # * Determine if StateConditions are met
  59.   #--------------------------------------------------------------------------
  60.   def state_conditions_met(state_id)
  61.     conditions = $data_states[state_id].state_conditions
  62.     return true unless conditions
  63.     conditions.each do |sc|
  64.       return false if !state?(sc[0])
  65.     end
  66.     return true
  67.   end
  68. end
  69.  
  70. #==============================================================================
  71. # ** RPG::State
  72. #==============================================================================
  73. class RPG::State < RPG::BaseItem
  74.   #--------------------------------------------------------------------------
  75.   # * state_conditions
  76.   #--------------------------------------------------------------------------
  77.   def state_conditions
  78.     conditions = []
  79.     self.note.downcase.each_line do |line|
  80.       sc = scan_state_condition(line)
  81.       conditions << sc if sc
  82.     end
  83.     return conditions.size > 0 ? conditions : nil
  84.   end
  85.   #--------------------------------------------------------------------------
  86.   # * scan_state_condition
  87.   #--------------------------------------------------------------------------
  88.   def scan_state_condition(line)
  89.     return nil unless line =~ /<state_condition:\s*(\d+)\s*(?:,\s*(independent)\s*)?>/i
  90.     con = [$1.to_i, $2 != nil]
  91.   end
  92. end
Add Comment
Please, Sign In to add comment