ForeverZer0

[RMXP] Item Weight

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