ForeverZer0

[RMXP] Item Weight

Apr 15th, 2012
141
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
  2. # Item Weight
  3. # Author: ForeverZer0
  4. # Date: 4.15.2012
  5. # Version: 1.0
  6. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
  7. #                           VERSION HISTORY
  8. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
  9. # v.1.0
  10. #   - Original release
  11. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
  12. #
  13. # Explanation:
  14. #   This system provides your game with an inventory "weight" feature. Every
  15. #   item and piece of equipment can be configured with an individual weight that
  16. #   provides a different dynamic to collecting items, since the player will have
  17. #   to manage their inventories instead of just collecting everything in sight.
  18. #   Settings for can be made for managing it on a "per actor basis", and various
  19. #   states can be applied when a character/party is "overweight".
  20. #
  21. # Features:
  22. #   - States can be applied to the whole party or individual actor basis
  23. #   - Every item, weapon, and armor can have a unique weight
  24. #   - Unlimited number of states
  25. #   - Can us a dynamic weight capacity that grows with actor power, or static
  26. #     capacity that you can control yourself via simple script calls
  27. #   - Automcatic setting of game variables that are equal to the party's current
  28. #     weight and total capacity
  29. #   - Configurable modifier that allows for defining the rate of dynamic weight
  30. #     capacity based off actor strength
  31. #   - Automatic adding of weight to item descriptions if desired so that the
  32. #     player is always aware of each item's weight
  33. #   - Configuration for allowing or not items to be picked up by party or
  34. #     equipped by actors if weight would exceed capacity
  35. #    
  36. # Instructions:
  37. #   - Place script below default scripts, and above "Main"
  38. #   - See below and find the "Weight" module. There are a few simple settings
  39. #     that can be made with detailed descriptions of each
  40. #   - Configure item weights in the RPG::Item, RPG::Weapon, and RPG::Armor
  41. #     classes found below
  42. #   - When using STATIC_CAPACITY and you would like to change an actor's
  43. #     carrying capacity, use these script calls:
  44. #
  45. #         $game_party.actors[ACTOR_INDEX].weight_capacity = VALUE
  46. #                         OR
  47. #         $game_actors[ACTOR_ID].weight_capacity = VALUE
  48. #
  49. #
  50. #
  51. # Compatibility:
  52. #   - Custom CMS's (specifically the equip part of it), could possibly cause
  53. #     problems, and would require minor edits to ensure that items that are too
  54. #     heavy could not be equipped when OVERWEIGHT_ACTOR is false.
  55. #
  56. # Credits/Thanks:
  57. #   - ForeverZer0, for the script
  58. #   - Bigfoot, for requesting it
  59. #
  60. # Author's Notes:
  61. #   - Please report any bugs/issues to www.chaos-project.com or via email:
  62. #     foreverzer0@chaos-project.com
  63. #
  64. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
  65. #                          BEGIN CONFIGURATION
  66. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
  67.  
  68. #===============================================================================
  69. # Weight
  70. #-------------------------------------------------------------------------------
  71. # Module for configuring various weight settings
  72. #===============================================================================
  73.  
  74. module Weight
  75.  
  76.   # Set to true/false if actor weight capacity grows with the actor's
  77.   # strength, or remains a fixed number (can be changed via script call)
  78.   DYNAMIC_CAPACITY = false
  79.  
  80.   # Define the modifier for the DYNAMIC_CAPACITY. This value will be multiplied
  81.   # by the actor's strength to determine their carrying capacity. Ignore this
  82.   # setting if DYNAMIC_CAPACITY is false.
  83.   DYNAMIC_MODIFIER = 1.5
  84.  
  85.   # Define the static carrying capacity that actors begin with. This value can
  86.   # only be changed via script call, and will not grow with the actor's power.
  87.   # Ignore this setting if DYNAMIC_CAPACITY is true.
  88.   STATIC_CAPACITY = 4.0
  89.  
  90.   # Include the IDs of any states that will be applied when
  91.   #cweight exceeds maximum capacity.
  92.   OVERWEIGHT_STATES = [2, 3]
  93.  
  94.   # Set to true/false if individual actors can become overweight, or if it is
  95.   # only on a "whole party" basis. The entire party will still be afflicted with
  96.   # states if the total weight exceeds the party's capacity. In other words,
  97.   # define if actor's can equip items that are too heavy for them.
  98.   OVERWEIGHT_ACTOR = true
  99.  
  100.   # Determines if items/equipment can still be gained when current weight would
  101.   # exceed total carrying capacity after picking it up. The maximum number that
  102.   # can be added will still be received if this is false and receiving multiple
  103.   # quantities. In other words, can items still be picked up that are too
  104.   # heavy for the party.
  105.   OVERWEIGHT_PARTY = false
  106.  
  107.   # Set the ID of the variable that will be equal to the party's current weight
  108.   CURRENT_VARIABLE = 10
  109.  
  110.   # Set the ID of the variable that will be equal to the party's total capacity
  111.   CAPACITY_VARIABLE = 11
  112.  
  113.   # Set to true/false if item weights should automatically be added to the
  114.   # description for help menu's, etc.
  115.   WEIGHT_DESCRIPTION = true
  116.  
  117.   # Determines the modification of the item's description. Ignore if
  118.   # WEIGHT_DESCRIPTION is false.
  119.   # EXAMPLE: "[#{weight} lbs] #{text}"
  120.   def self.description(text, weight)
  121.     return "#{text} (#{weight} kg)"
  122.   end
  123. end
  124.  
  125. #===============================================================================
  126. # * RPG::Item
  127. #===============================================================================
  128. module RPG
  129.   class Item
  130.     #---------------------------------------------------------------------------
  131.     # * weight
  132.     #     Define the weights of items (when ITEM_ID then WEIGHT)
  133.     #---------------------------------------------------------------------------
  134.     def weight
  135.       return case @id
  136.       when 1 then 0.5
  137.       when 2 then 0.75
  138.       when 3 then 1.0
  139.       when 4 then 1.0
  140.       when 5 then 1.0
  141.       when 6 then 1.0
  142.       when 7 then 1.0
  143.       when 8 then 1.0
  144.       when 9 then 1.0
  145.       when 10 then 1.0
  146.       else 1.0
  147.       end
  148.     end
  149.     #---------------------------------------------------------------------------
  150.     # * description
  151.     #     Returns the modified description when WEIGHT_DESCRIPTION is true
  152.     #---------------------------------------------------------------------------
  153.     def description
  154.       return !Weight::WEIGHT_DESCRIPTION ? @description :
  155.         Weight.description(@description, weight)  
  156.     end
  157.   end
  158. #===============================================================================
  159. # * RPG::Weapon
  160. #===============================================================================
  161.   class Weapon
  162.     #---------------------------------------------------------------------------
  163.     # * weight
  164.     #     Define the weights of weapons (when WEAPON_ID then WEIGHT)
  165.     #---------------------------------------------------------------------------
  166.     def weight
  167.       return case @id
  168.       when 1 then 1.0
  169.       when 2 then 1.0
  170.       when 3 then 1.0
  171.       when 4 then 1.0
  172.       when 5 then 1.0
  173.       when 6 then 1.0
  174.       when 7 then 1.0
  175.       when 8 then 1.0
  176.       when 9 then 1.0
  177.       when 10 then 1.0
  178.       else 1.0
  179.       end
  180.     end
  181.     #---------------------------------------------------------------------------
  182.     # * description
  183.     #     Returns the modified description when WEIGHT_DESCRIPTION is true
  184.     #---------------------------------------------------------------------------
  185.     def description
  186.       return !Weight::WEIGHT_DESCRIPTION ? @description :
  187.         Weight.description(@description, weight)  
  188.     end
  189.   end
  190. #===============================================================================
  191. # * RPG::Armor
  192. #===============================================================================
  193.   class Armor
  194.     #---------------------------------------------------------------------------
  195.     # * weight
  196.     #     Define the weights of armors (when ARMOR_ID then WEIGHT)
  197.     #---------------------------------------------------------------------------
  198.     def weight
  199.       return case @id
  200.       when 1 then 1.0
  201.       when 2 then 1.0
  202.       when 3 then 1.0
  203.       when 4 then 1.0
  204.       when 5 then 1.0
  205.       when 6 then 1.0
  206.       when 7 then 1.0
  207.       when 8 then 1.0
  208.       when 9 then 1.0
  209.       when 10 then 1.0
  210.       when 29 then 2.0
  211.       else 1.0
  212.       end
  213.     end
  214.     #---------------------------------------------------------------------------
  215.     # * description
  216.     #     Returns the modified description when WEIGHT_DESCRIPTION is true
  217.     #---------------------------------------------------------------------------
  218.     def description
  219.       return !Weight::WEIGHT_DESCRIPTION ? @description :
  220.         Weight.description(@description, weight)  
  221.     end
  222.   end
  223. end
  224.  
  225. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
  226. #                            END CONFIGURATION
  227. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
  228.  
  229. #===============================================================================
  230. # * Game_Actor
  231. #===============================================================================
  232.  
  233. class Game_Actor
  234.   #-----------------------------------------------------------------------------
  235.   # * initailize
  236.   #     Aliased method to add capacity variable to Game_Actor, which is used
  237.   #     when using STATIC_CAPACITY.
  238.   #-----------------------------------------------------------------------------
  239.   alias inventory_weight_init initialize
  240.   def initialize(actor_id)
  241.     inventory_weight_init(actor_id)
  242.     @capacity = Weight::STATIC_CAPACITY
  243.   end
  244.   #-----------------------------------------------------------------------------
  245.   # * weight
  246.   #     Returns the total weight of all equipped items the actor has
  247.   #-----------------------------------------------------------------------------
  248.   def weight
  249.     weight = 0
  250.     equipment = [
  251.       $data_weapons[@weapon_id],
  252.       $data_armors[@armor1_id],
  253.       $data_armors[@armor2_id],
  254.       $data_armors[@armor3_id],
  255.       $data_armors[@armor4_id]
  256.     ]
  257.     equipment.each {|item| weight += (item != nil ? item.weight : 0) }
  258.     return weight
  259.   end
  260.   #-----------------------------------------------------------------------------
  261.   # * weight_capacity=
  262.   #     Sets the carrying capacity of the actor when using STATIC_CAPACITY
  263.   #-----------------------------------------------------------------------------
  264.   def weight_capacity=(capacity)
  265.     @capacity = capacity
  266.   end
  267.   #-----------------------------------------------------------------------------
  268.   # * weight_capacity
  269.   #     Returns the amount of weight the actor can carry
  270.   #-----------------------------------------------------------------------------
  271.   def weight_capacity
  272.     if Weight::DYNAMIC_CAPACITY
  273.       return base_str * Weight::DYNAMIC_MODIFIER
  274.     else
  275.       return @capacity
  276.     end
  277.   end
  278.   #-----------------------------------------------------------------------------
  279.   # * within_capacity?
  280.   #     Factors in weight when determining if piece of equipment is equippable
  281.   #     or not.
  282.   #-----------------------------------------------------------------------------
  283.   def equippable_weight?(item)
  284.     return true if item == nil || Weight::OVERWEIGHT_ACTOR
  285.     actor_weight = self.weight + item.weight
  286.     if item.is_a?(RPG::Weapon)
  287.       weapon = $data_weapons[@weapon_id]
  288.       actor_weight -= (weapon == nil ? 0 : weapon.weight)
  289.     elsif item.is_a?(RPG::Armor)
  290.       case item.kind
  291.       when 0 # Armor
  292.         armor = $data_armors[@armor1_id]
  293.       when 1 # Shield
  294.         armor = $data_armors[@armor2_id]
  295.       when 2 # Helmet
  296.         armor = $data_armors[@armor3_id]
  297.       when 3 # Accessory
  298.         armor = $data_armors[@armor4_id]
  299.       end
  300.       actor_weight -= (armor == nil ? 0 : armor.weight)
  301.     end
  302.     return actor_weight <= weight_capacity
  303.   end
  304.   #-----------------------------------------------------------------------------
  305.   # * equip
  306.   #     Refreshes party weight and states after item change
  307.   #-----------------------------------------------------------------------------
  308.   alias item_weight_equip equip
  309.   def equip(kind, id)
  310.     item = kind == 0 ? $data_weapons[id] : $data_armors[id]
  311.     if equippable_weight?(item)
  312.       item_weight_equip(kind, id)
  313.       $game_party.refresh_weight
  314.     end
  315.   end
  316. end
  317.  
  318. #===============================================================================
  319. # * Game_Party
  320. #===============================================================================
  321.  
  322. class Game_Party
  323.   #-----------------------------------------------------------------------------
  324.   # * weight_capacity
  325.   #     Returns the cumulative weight capacity of all current party members
  326.   #-----------------------------------------------------------------------------
  327.   def weight_capacity
  328.     weight = 0
  329.     @actors.each {|actor| weight += actor.weight_capacity }
  330.     return weight
  331.   end
  332.   #-----------------------------------------------------------------------------
  333.   # * actor_weight
  334.   #     Returns the total weight of all equipment worn by party members
  335.   #-----------------------------------------------------------------------------
  336.   def actor_weight
  337.     weight = 0
  338.     @actors.each {|actor| weight += actor.weight }
  339.     return weight
  340.   end
  341.   #-----------------------------------------------------------------------------
  342.   # * item_weight
  343.   #     Returns the total weight of all items in inventory
  344.   #-----------------------------------------------------------------------------
  345.   def item_weight
  346.     weight = 0
  347.     @items.each_pair {|id, qty| weight += $data_items[id].weight * qty }
  348.     return weight
  349.   end
  350.   #-----------------------------------------------------------------------------
  351.   # * weapon_weight
  352.   #     Returns the total weight of all weapons in inventory
  353.   #-----------------------------------------------------------------------------
  354.   def weapon_weight
  355.     weight = 0
  356.     @weapons.each_pair {|id, qty| weight += $data_weapons[id].weight * qty }
  357.     return weight
  358.   end
  359.   #-----------------------------------------------------------------------------
  360.   # * armor_weight
  361.   #     Returns the total weight of all armors in inventory
  362.   #-----------------------------------------------------------------------------
  363.   def armor_weight
  364.     weight = 0
  365.     @armors.each_pair {|id, qty| weight += $data_armors[id].weight * qty }
  366.     return weight
  367.   end
  368.   #-----------------------------------------------------------------------------
  369.   # * total_weight
  370.   #     Returns the total weight of all inventory and equipment that is
  371.   #     equipped by the party members
  372.   #-----------------------------------------------------------------------------
  373.   def total_weight
  374.     return actor_weight + item_weight + weapon_weight + armor_weight
  375.   end
  376.   #-----------------------------------------------------------------------------
  377.   # * gain_item
  378.   #     Aliased method to determine if party can add an item to inventory when
  379.   #     ALLOW_OVERWHEIGHT is set to false. The maximum quantity that can fit
  380.   #     will be added out of the total quanity if capacity would be exceeded.
  381.   #-----------------------------------------------------------------------------
  382.   alias item_weight_gain_item gain_item
  383.   def gain_item(id, quantity)
  384.     if Weight::OVERWEIGHT_PARTY
  385.       item_weight_gain_item(id, quantity)
  386.     else
  387.       item = $data_items[id]
  388.       weight = item == nil ? 0 : item.weight
  389.       if !enough_capacity?(weight * quantity)
  390.         allowed = (weight_capacity - total_weight) / weight
  391.         item_weight_gain_item(id, allowed.floor)
  392.       else
  393.         item_weight_gain_item(id, quantity)
  394.       end
  395.     end
  396.     refresh_weight
  397.   end
  398.   #-----------------------------------------------------------------------------
  399.   # * gain_weapon
  400.   #     Aliased method to determine if party can add a weapon to inventory when
  401.   #     ALLOW_OVERWHEIGHT is set to false. The maximum quantity that can fit
  402.   #     will be added out of the total quanity if capacity would be exceeded.
  403.   #-----------------------------------------------------------------------------
  404.   alias item_weight_gain_weapon gain_weapon
  405.   def gain_weapon(id, quantity)
  406.     if Weight::OVERWEIGHT_PARTY
  407.       item_weight_gain_weapon(id, quantity)
  408.     else
  409.       weapon = $data_weapons[id]
  410.       weight = weapon == nil ? 0 : weapon.weight
  411.       if !enough_capacity?(weight * quantity)
  412.         allowed = (weight_capacity - total_weight) / weight
  413.         item_weight_gain_weapon(id, allowed.floor)
  414.       else
  415.         item_weight_gain_weapon(id, quantity)
  416.       end
  417.     end
  418.     refresh_weight
  419.   end
  420.   #-----------------------------------------------------------------------------
  421.   # * gain_armor
  422.   #     Aliased method to determine if party can add an armor to inventory when
  423.   #     ALLOW_OVERWHEIGHT is set to false. The maximum quantity that can fit
  424.   #     will be added out of the total quanity if capacity would be exceeded.
  425.   #-----------------------------------------------------------------------------
  426.   alias item_weight_gain_armor gain_armor
  427.   def gain_armor(id, quantity)
  428.     if Weight::OVERWEIGHT_PARTY
  429.       item_weight_gain_armor(id, quantity)
  430.     else
  431.       armor = $data_armors[id]
  432.       weight = armor == nil ? 0 : armor.weight
  433.       if !enough_capacity?(weight * quantity)
  434.         allowed = (weight_capacity - total_weight) / weight
  435.         item_weight_gain_armor(id, allowed.floor)
  436.       else
  437.         item_weight_gain_armor(id, quantity)
  438.       end
  439.     end
  440.     refresh_weight
  441.   end  
  442.   #-----------------------------------------------------------------------------
  443.   # * add_actor
  444.   #     Refreshes party weight and states when actor is added to party
  445.   #-----------------------------------------------------------------------------
  446.   alias item_weight_add_actor add_actor
  447.   def add_actor(actor_id)
  448.     item_weight_add_actor(actor_id)
  449.     refresh_weight
  450.   end
  451.   #-----------------------------------------------------------------------------
  452.   # * remove_actor
  453.   #     Refreshes party weight and states when actor is removed from party
  454.   #-----------------------------------------------------------------------------
  455.   alias item_weight_remove_actor remove_actor
  456.   def remove_actor(actor_id)
  457.     item_weight_remove_actor(actor_id)
  458.     refresh_weight
  459.   end
  460.   #-----------------------------------------------------------------------------
  461.   # * enough_capacity?
  462.   #     Returns true/false if given weight would not exceed capacity
  463.   #-----------------------------------------------------------------------------
  464.   def enough_capacity?(weight)
  465.     return (total_weight + weight) <= weight_capacity
  466.   end
  467.   #-----------------------------------------------------------------------------
  468.   # * refresh_weight
  469.   #     Refreshes the weight variables and adds/removes weight states
  470.   #-----------------------------------------------------------------------------
  471.   def refresh_weight
  472.     total, capacity = total_weight, weight_capacity
  473.     $game_variables[Weight::CURRENT_VARIABLE] = total
  474.     $game_variables[Weight::CAPACITY_VARIABLE] = capacity
  475.     # Add/remove states to party as a whole
  476.     if Weight::OVERWEIGHT_PARTY && total > capacity
  477.       @actors.each {|actor|
  478.         Weight::OVERWEIGHT_STATES.each {|state_id| actor.add_state(state_id) }
  479.       }
  480.     # Add/remove individual states for actors
  481.     elsif Weight::OVERWEIGHT_ACTOR
  482.       @actors.each {|actor|
  483.         if actor.weight > actor.weight_capacity
  484.           Weight::OVERWEIGHT_STATES.each {|state_id| actor.add_state(state_id) }
  485.         else
  486.           Weight::OVERWEIGHT_STATES.each {|state_id| actor.remove_state(state_id) }
  487.         end
  488.       }
  489.     else
  490.       @actors.each {|actor|
  491.         Weight::OVERWEIGHT_STATES.each {|state_id| actor.remove_state(state_id) }
  492.       }
  493.     end
  494.   end
  495.   #-----------------------------------------------------------------------------
  496.   # * setup_starting_members
  497.   #     Alias method to set the game variables at the beginning of the game
  498.   #-----------------------------------------------------------------------------
  499.   alias item_weight_setup_starting_members setup_starting_members
  500.   def setup_starting_members
  501.     item_weight_setup_starting_members
  502.     refresh_weight
  503.   end
  504. end
  505.  
  506. #===============================================================================
  507. # * Scene_Equip
  508. #-------------------------------------------------------------------------------
  509. # This needs to be here more or less just to what can only be considered either
  510. # bad coding or a bug in RMXP. The "equip" method of Game_Actor is constantly
  511. # called whenever an item is highlighted in Scene_Equip, and this is kind of
  512. # a little bypass for that.
  513. #===============================================================================
  514.  
  515. class Scene_Equip
  516.   #-----------------------------------------------------------------------------
  517.   # * update_item
  518.   #     Cancels equipping item if configured to do so and item would put
  519.   #     actor overweight
  520.   #-----------------------------------------------------------------------------
  521.   alias item_weight_update_item update_item
  522.   def update_item
  523.     if Input.trigger?(Input::C)
  524.       item = @item_window.item
  525.       unless @actor.equippable_weight?(item)
  526.         $game_system.se_play($data_system.buzzer_se)
  527.         return
  528.       end
  529.     end
  530.     item_weight_update_item
  531.   end
  532. end
RAW Paste Data