Advertisement
Fomar0153

Fomar0153 - CBS Add-On For YF Engine Ace 1.2

Mar 22nd, 2012
11,330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 19.55 KB | None | 0 0
  1. =begin
  2. YanFly Compatible Customisable ATB/Stamina Based Battle System Script
  3. by Fomar0153
  4. Version 1.2
  5. ----------------------
  6. Notes
  7. ----------------------
  8. Requires Yanfly Engine Ace - Ace Battle Engine
  9. Customises the battle system to be similar to
  10. ATB or Stamina based battle systems.
  11. ----------------------
  12. Instructions
  13. ----------------------
  14. Edit variables in CBS to suit your needs.
  15. The guard status should be set to 2~2 turns.
  16. ----------------------
  17. Change Log
  18. ----------------------
  19. 1.0 -> 1.1 Added CTB related options
  20.            Added the ability to pass a turn
  21.            Added options for turn functionality to be based on time
  22.            or number of actions
  23.            Added the ability to change the bar colours based on states
  24. 1.1 -> 1.2 Fixed a bug when escaping using CTB mode
  25. ----------------------
  26. Known bugs
  27. ----------------------
  28. None
  29. =end
  30.  
  31. $imported = {} if $imported.nil?
  32. $imported["Fomar0153-CBS"] = true
  33.  
  34. module CBS
  35.  
  36.   MAX_STAMINA = 1000
  37.   RESET_STAMINA = true
  38.  
  39.   # If ATB is set to false then the bars won't appear and
  40.   # the pauses where the bars would be filling up are removed
  41.   # effectively turning this into a CTB system
  42.   ATB = true
  43.   SAMINA_GAUGE_NAME = "ATB"
  44.   ENABLE_PASSING = true
  45.   PASSING_COST = 200
  46.  
  47.   # If TURN_LENGTH is set to 0 then a turn length will be
  48.   # decided on number of actions
  49.   # TURN_LENGTH is number of seconds per turn
  50.   TURN_LENGTH = 4
  51.  
  52.   ESCAPE_COST = 500
  53.   # If reset stamina is set to true then all characters
  54.   # will start with a random amount of stamina capped at
  55.   # the percentage you set.
  56.   # If reset stamina is set to false then this just
  57.   # affects enemies.
  58.   STAMINA_START_PERCENT = 20
  59.  
  60.   # Default skill cost
  61.   # If you want to customise skill costs do it like this
  62.   # SKILL_COST[skill_id] = cost
  63.   SKILL_COST = []
  64.   SKILL_COST[0] = 1000
  65.   # Attack
  66.   SKILL_COST[1] = 1000
  67.   # Guard
  68.   SKILL_COST[2] = 500
  69.   ITEM_COST = 1000
  70.  
  71.   # If you prefer to have states handle agility buffs then set STATES_HANDLE_AGI to true
  72.   STATES_HANDLE_AGI = false
  73.   # In the following section mult means the amount you multiply stamina gains by
  74.   # if STATES_HANDLE_AGI is set to true then it is only used to determine bar color
  75.   # with debuffs taking precedence over buffs
  76.   STAMINA_STATES = []
  77.   # Default colour
  78.   STAMINA_STATES[0] = [1,31,32]
  79.   # in the form
  80.   # STAMINA_STATES[STATE_ID] = [MULT,FILL_COLOUR,EMPTY_COLOR]
  81.   # e.g. Haste
  82.   STAMINA_STATES[10] = [2,4,32]
  83.   # e.g. Stop  
  84.   STAMINA_STATES[11] = [0,8,32]
  85.   # e.g. Slow  
  86.   STAMINA_STATES[12] = [0.5,8,32]
  87.  
  88.   #--------------------------------------------------------------------------
  89.   # ● New Method stamina_gain
  90.   #--------------------------------------------------------------------------
  91.   def self.stamina_gain(battler)
  92.     return ((2 + [0, battler.agi / 10].max) * self.stamina_mult(battler)).to_i
  93.   end
  94.   #--------------------------------------------------------------------------
  95.   # ● New Method stamina_gain
  96.   #--------------------------------------------------------------------------
  97.   def self.stamina_mult(battler)
  98.     return 1 if STATES_HANDLE_AGI
  99.     mult = STAMINA_STATES[0][0]
  100.     for state in battler.states
  101.       unless STAMINA_STATES[state.id].nil?
  102.         mult *= STAMINA_STATES[state.id][0]
  103.       end
  104.     end
  105.     return mult
  106.   end
  107.   #--------------------------------------------------------------------------
  108.   # ● New Method stamina_gain
  109.   #--------------------------------------------------------------------------
  110.   def self.stamina_colors(battler)
  111.     colors = STAMINA_STATES[0]
  112.     for state in battler.states
  113.       unless STAMINA_STATES[state.id].nil?
  114.         if STAMINA_STATES[state.id][0] < colors[0] or colors[0] == 1
  115.           colors = STAMINA_STATES[state.id]
  116.         end
  117.       end
  118.     end
  119.     return colors
  120.   end
  121.   #--------------------------------------------------------------------------
  122.   # ● New Method stamina_start
  123.   #--------------------------------------------------------------------------
  124.   def self.stamina_start(battler)
  125.     battler.stamina = rand(MAX_STAMINA * STAMINA_START_PERCENT / 100)
  126.   end
  127. end
  128.  
  129. class Game_BattlerBase
  130.   #--------------------------------------------------------------------------
  131.   # ● New attr_accessor
  132.   #--------------------------------------------------------------------------
  133.   attr_accessor :stamina
  134.   #--------------------------------------------------------------------------
  135.   # ● Aliases initialize
  136.   #--------------------------------------------------------------------------
  137.   alias yf_fomar_cbs_initialize initialize
  138.   def initialize
  139.     yf_fomar_cbs_initialize
  140.     @stamina = 0
  141.   end
  142.   #--------------------------------------------------------------------------
  143.   # ● New Method stamina_rate
  144.   #--------------------------------------------------------------------------
  145.   def stamina_rate
  146.     @stamina.to_f / CBS::MAX_STAMINA
  147.   end
  148.   #--------------------------------------------------------------------------
  149.   # ● New Method stamina_rate
  150.   #--------------------------------------------------------------------------
  151.   def stamina_gain
  152.     return if not movable?
  153.     @stamina = [CBS::MAX_STAMINA, @stamina + CBS.stamina_gain(self)].min
  154.   end
  155.   #--------------------------------------------------------------------------
  156.   # ● New Method stamina_color
  157.   #--------------------------------------------------------------------------
  158.   def stamina_color
  159.     for state in @states
  160.       unless CBS::STAMINA_STATES[state].nil?
  161.         return STAMINA_STATES[state]
  162.       end
  163.     end
  164.     return STAMINA_STATES[0]
  165.   end
  166. end
  167.  
  168. class Scene_Battle < Scene_Base
  169.   #--------------------------------------------------------------------------
  170.   # ● Rewrote update
  171.   #--------------------------------------------------------------------------
  172.   def update
  173.     super
  174.     if (CBS::ENABLE_PASSING and @actor_command_window.active) and Input.press?(:A)
  175.       command_pass
  176.     end
  177.     if BattleManager.in_turn? and !inputting?
  178.       while @subject.nil? and !CBS::ATB
  179.         process_stamina
  180.       end
  181.       if CBS::ATB
  182.         process_stamina
  183.       end
  184.       process_event
  185.       process_action
  186.     end
  187.     BattleManager.judge_win_loss
  188.   end
  189.   #--------------------------------------------------------------------------
  190.   # ● Rewrote Method update_info_viewport
  191.   #--------------------------------------------------------------------------
  192.   def update_info_viewport
  193.     move_info_viewport(0)   if @party_command_window.active
  194.     move_info_viewport(128) if @actor_command_window.active
  195.     move_info_viewport(64)  if BattleManager.in_turn? and !inputting?
  196.   end
  197.   #--------------------------------------------------------------------------
  198.   # ● New Method inputting?
  199.   #--------------------------------------------------------------------------
  200.   def inputting?
  201.     return @actor_command_window.active || @skill_window.active ||
  202.       @item_window.active || @actor_window.active || @enemy_window.active
  203.   end
  204.   #--------------------------------------------------------------------------
  205.   # ● New Method process_stamina
  206.   #--------------------------------------------------------------------------
  207.   def process_stamina
  208.     @actor_command_window.close
  209.     return if @subject
  210.     BattleManager.advance_turn
  211.     all_battle_members.each do |battler|
  212.       battler.stamina_gain
  213.     end
  214.     @status_window.refresh_stamina
  215.     if @status_window.close?
  216.       @status_window.open
  217.      end
  218.     if BattleManager.escaping?
  219.       $game_party.battle_members.each do |battler|
  220.         if battler.stamina < CBS::MAX_STAMINA
  221.           $game_troop.members.each do |enemy|
  222.             if enemy.stamina == CBS::MAX_STAMINA
  223.               enemy.make_actions
  224.               @subject = enemy
  225.             end
  226.           end
  227.           return
  228.         end
  229.       end
  230.       unless BattleManager.process_escape
  231.         $game_party.battle_members.each do |actor|
  232.           actor.stamina -= CBS::ESCAPE_COST
  233.         end
  234.         BattleManager.set_escaping(false) unless CBS::ATB
  235.       end
  236.     end
  237.     all_battle_members.each do |battler|
  238.       if battler.stamina == CBS::MAX_STAMINA
  239.         battler.make_actions
  240.         @subject = battler
  241.         if @subject.inputable? and battler.is_a?(Game_Actor)
  242.           @actor_command_window.setup(@subject)
  243.           @status_window.index = @subject.index
  244.           BattleManager.set_actor(battler)
  245.         end
  246.         return
  247.       end
  248.     end
  249.   end
  250.   #--------------------------------------------------------------------------
  251.   # ● Rewrote start_party_command_selection Yanfly version
  252.   #--------------------------------------------------------------------------
  253.   def start_party_command_selection
  254.     unless scene_changing?
  255.       refresh_status
  256.       @status_window.unselect
  257.       @status_window.open
  258.       if BattleManager.input_start
  259.         @actor_command_window.close
  260.         @party_command_window.setup
  261.       else
  262.         @party_command_window.deactivate
  263.         turn_start
  264.       end
  265.     end
  266.   end
  267.   #--------------------------------------------------------------------------
  268.   # ● Rewrote start_actor_command_selection
  269.   #--------------------------------------------------------------------------
  270.   def start_actor_command_selection
  271.     @party_command_window.close
  272.     BattleManager.set_escaping(false)
  273.     turn_start
  274.   end
  275.   #--------------------------------------------------------------------------
  276.   # ● Rewrote prior_command Yanfly version
  277.   #--------------------------------------------------------------------------
  278.   def prior_command
  279.     redraw_current_status
  280.     start_party_command_selection
  281.   end
  282.   #--------------------------------------------------------------------------
  283.   # ● Rewrote process_action
  284.   #--------------------------------------------------------------------------
  285.   def process_action
  286.     return if scene_changing?
  287.     if !@subject || !@subject.current_action
  288.       @subject = BattleManager.next_subject
  289.     end
  290.     if Input.trigger?(:B) and (@subject == nil)
  291.       start_party_command_selection
  292.     end
  293.     return unless @subject
  294.     if @subject.current_action
  295.       @subject.current_action.prepare
  296.       if @subject.current_action.valid?
  297.         @status_window.open
  298.         execute_action
  299.       end
  300.       @subject.remove_current_action
  301.       refresh_status
  302.       @log_window.display_auto_affected_status(@subject)
  303.       @log_window.wait_and_clear
  304.     end
  305.     process_action_end unless @subject.current_action
  306.   end
  307.   #--------------------------------------------------------------------------
  308.   # ● Aliases use_item
  309.   #--------------------------------------------------------------------------
  310.   alias cbs_use_item use_item
  311.   def use_item
  312.     cbs_use_item
  313.     @subject.stamina_loss
  314.   end
  315.   #--------------------------------------------------------------------------
  316.   # ● Rewrote turn_end
  317.   #--------------------------------------------------------------------------
  318.   def turn_end
  319.     all_battle_members.each do |battler|
  320.       battler.on_turn_end
  321.       refresh_status
  322.       @log_window.display_auto_affected_status(battler)
  323.       @log_window.wait_and_clear
  324.     end
  325.     #BattleManager.turn_end
  326.     #process_event
  327.     #start_party_command_selection
  328.   end
  329.   #--------------------------------------------------------------------------
  330.   # ● Rewrote command_fight
  331.   #--------------------------------------------------------------------------
  332.   def command_fight
  333.     BattleManager.next_command
  334.     start_actor_command_selection
  335.   end
  336.   #--------------------------------------------------------------------------
  337.   # ● Rewrote command_escape
  338.   #--------------------------------------------------------------------------
  339.   def command_escape
  340.     @party_command_window.close
  341.     BattleManager.set_escaping(true)
  342.     turn_start
  343.   end
  344.   #--------------------------------------------------------------------------
  345.   # ● New method command_pass
  346.   #--------------------------------------------------------------------------
  347.   def command_pass
  348.     BattleManager.actor.stamina -= CBS::PASSING_COST
  349.     BattleManager.clear_actor
  350.     @subject = nil
  351.     turn_start
  352.     @actor_command_window.active = false
  353.     @actor_command_window.close
  354.   end
  355.   #--------------------------------------------------------------------------
  356.   # ● Destroyed next_command
  357.   #--------------------------------------------------------------------------
  358.   def next_command
  359.     @status_window.show
  360.     @actor_command_window.show
  361.     @status_aid_window.hide
  362.   end
  363. end
  364.  
  365.  
  366.  
  367.  
  368. module BattleManager
  369.   #--------------------------------------------------------------------------
  370.   # ● Rewrote setup
  371.   #--------------------------------------------------------------------------
  372.   def self.setup(troop_id, can_escape = true, can_lose = false)
  373.     init_members
  374.     $game_troop.setup(troop_id)
  375.     @can_escape = can_escape
  376.     @can_lose = can_lose
  377.     make_escape_ratio
  378.     @escaping = false
  379.     @turn_counter = 0
  380.     @actions_per_turn = $game_party.members.size + $game_troop.members.size
  381.     ($game_party.members + $game_troop.members).each do |battler|
  382.       if battler.is_a?(Game_Enemy) or CBS::RESET_STAMINA
  383.         CBS.stamina_start(battler)
  384.       end
  385.     end
  386.   end
  387.   #--------------------------------------------------------------------------
  388.   # ● New Method set_escaping
  389.   #--------------------------------------------------------------------------
  390.   def self.set_escaping(escaping)
  391.     @escaping = escaping
  392.   end
  393.   #--------------------------------------------------------------------------
  394.   # ● New Method escaping?
  395.   #--------------------------------------------------------------------------
  396.   def self.escaping?
  397.     return @escaping
  398.   end
  399.   #--------------------------------------------------------------------------
  400.   # ● Rewrote turn_start Yanfly version
  401.   #--------------------------------------------------------------------------
  402.   def self.turn_start
  403.     @phase = :turn
  404.     clear_actor
  405.     $game_troop.increase_turn if $game_troop.turn_count == 0
  406.     @performed_battlers = []
  407.   end
  408.   #--------------------------------------------------------------------------
  409.   # ● New Method set_actor
  410.   #--------------------------------------------------------------------------
  411.   def self.set_actor(actor)
  412.     @actor_index = actor.index
  413.   end
  414.   #--------------------------------------------------------------------------
  415.   # ● New Increase action counter
  416.   #--------------------------------------------------------------------------
  417.   def self.add_action
  418.     return if @actions_per_turn.nil?
  419.     @turn_counter += 1
  420.     if @turn_counter == @actions_per_turn and CBS::TURN_LENGTH == 0
  421.       $game_troop.increase_turn
  422.       SceneManager.scene.turn_end
  423.       @turn_counter = 0
  424.     end
  425.   end
  426.   #--------------------------------------------------------------------------
  427.   # ● New Method advance_turn
  428.   #--------------------------------------------------------------------------
  429.   def self.advance_turn
  430.     return if CBS::TURN_LENGTH == 0
  431.     @turn_counter += 1
  432.     if @turn_counter == 60 * CBS::TURN_LENGTH
  433.       $game_troop.increase_turn
  434.       SceneManager.scene.turn_end
  435.       @turn_counter = 0
  436.     end
  437.   end
  438. end
  439.  
  440. class Game_Battler < Game_BattlerBase
  441.   #--------------------------------------------------------------------------
  442.   # ● Rewrote on_turn_end
  443.   #--------------------------------------------------------------------------
  444.   def on_turn_end
  445.     @result.clear
  446.     regenerate_all
  447.     update_state_turns
  448.     update_buff_turns
  449.     remove_states_auto(2)
  450.   end
  451.   #--------------------------------------------------------------------------
  452.   # ● New Method on_turn_end
  453.   #--------------------------------------------------------------------------
  454.   def stamina_loss
  455.     if self.actor?
  456.       @stamina -= input.stamina_cost
  457.     else
  458.       @stamina -= @actions[0].stamina_cost
  459.     end
  460.     BattleManager.add_action
  461.   end
  462. end
  463.  
  464. class Game_Actor < Game_Battler
  465.   #--------------------------------------------------------------------------
  466.   # ● Rewrote input
  467.   #--------------------------------------------------------------------------
  468.   def input
  469.     if @actions[@action_input_index] == nil
  470.       @actions[@action_input_index] = Game_Action.new(self)
  471.     end
  472.     return @actions[@action_input_index]
  473.   end
  474. end
  475.  
  476. class Game_Action
  477.   #--------------------------------------------------------------------------
  478.   # ● New Method stamina_cost
  479.   #--------------------------------------------------------------------------
  480.   def stamina_cost
  481.     if @item.is_skill?
  482.       return CBS::SKILL_COST[item.id] if CBS::SKILL_COST[item.id]
  483.       return CBS::SKILL_COST[0]
  484.     end
  485.     return CBS::ITEM_COST if @item.is_item?
  486.     return CBS::MAX_STAMINA
  487.   end
  488. end
  489.  
  490. class Window_BattleStatus < Window_Selectable
  491.   #--------------------------------------------------------------------------
  492.   # Aliases method: draw_item yanfly version
  493.   #--------------------------------------------------------------------------
  494.   alias prefomar_draw_item draw_item
  495.   def draw_item(index)
  496.     unless CBS::ATB
  497.       prefomar_draw_item(index)
  498.       return
  499.     end
  500.     return if index.nil?
  501.     clear_item(index)
  502.     actor = battle_members[index]
  503.     rect = item_rect(index)
  504.     return if actor.nil?
  505.     draw_actor_face(actor, rect.x+2, rect.y+2, actor.alive?)
  506.     draw_actor_name(actor, rect.x, rect.y, rect.width-8)
  507.     draw_actor_action(actor, rect.x, rect.y)
  508.     draw_actor_icons(actor, rect.x, line_height*1, rect.width)
  509.     gx = YEA::BATTLE::BATTLESTATUS_HPGAUGE_Y_PLUS
  510.     contents.font.size = YEA::BATTLE::BATTLESTATUS_TEXT_FONT_SIZE
  511.     draw_actor_hp(actor, rect.x+2, line_height*2, rect.width-4)
  512.     if draw_tp?(actor) && draw_mp?(actor)
  513.       dw = rect.width/2-2
  514.       dw += 1 if $imported["YEA-CoreEngine"] && YEA::CORE::GAUGE_OUTLINE
  515.       draw_actor_tp(actor, rect.x+2, line_height*2+gx, dw)
  516.       dw = rect.width - rect.width/2 - 2
  517.       draw_actor_mp(actor, rect.x+rect.width/2, line_height*2+gx, dw)
  518.     elsif draw_tp?(actor) && !draw_mp?(actor)
  519.       draw_actor_tp(actor, rect.x+2, line_height*2+gx, rect.width-4)
  520.     else
  521.       draw_actor_mp(actor, rect.x+2, line_height*2+gx, rect.width-4)
  522.     end
  523.     draw_actor_stamina(actor, rect.x+2, line_height*3, rect.width-4)
  524.   end
  525.   #--------------------------------------------------------------------------
  526.   # overwrite method: draw_item yanfly version
  527.   #--------------------------------------------------------------------------
  528.   def draw_item_stamina(index)
  529.     return if index.nil?
  530.     actor = battle_members[index]
  531.     rect = item_rect(index)
  532.     return if actor.nil?
  533.     gx = YEA::BATTLE::BATTLESTATUS_HPGAUGE_Y_PLUS
  534.     contents.font.size = YEA::BATTLE::BATTLESTATUS_TEXT_FONT_SIZE
  535.     draw_actor_stamina(actor, rect.x+2, line_height*3, rect.width-4)
  536.   end
  537.   #--------------------------------------------------------------------------
  538.   # new method: refresh_stamina
  539.   #--------------------------------------------------------------------------
  540.   def refresh_stamina
  541.     return unless CBS::ATB
  542.     item_max.times {|i| draw_item_stamina(i) }
  543.   end
  544.   #--------------------------------------------------------------------------
  545.   # new method: draw_actor_stamina
  546.   #--------------------------------------------------------------------------
  547.   def draw_actor_stamina(actor, dx, dy, width = 124)
  548.     draw_gauge(dx, dy, width, actor.stamina_rate, text_color(CBS.stamina_colors(actor)[2]),text_color(CBS.stamina_colors(actor)[1]))
  549.     change_color(system_color)
  550.     cy = (Font.default_size - contents.font.size) / 2 + 1
  551.     draw_text(dx+2, dy+cy, 30, line_height, CBS::SAMINA_GAUGE_NAME)
  552.   end
  553. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement