Advertisement
Fomar0153

Fomar0153 - CBS Add-On For YF Engine Ace

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