Advertisement
Guest User

Untitled

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