Inventory Weight

Started by Hellfire Dragon, August 26, 2009, 02:50:57 pm

Previous topic - Next topic

Hellfire Dragon

I need a script that will give each item in the player's inventory a set weight. The play can't pick up items if they are over the limit. How much the player can carry is based off their total STR stat.
I need this to be compatiable with Blizz ABS, picking items up on the map :P

Couldn't find this anywhere, i searches :P

G_G

Well I'll need some sort of equation on how to make the strength.

Max Amount of Weigth = average party str * 2

or something like that.

Hellfire Dragon

I only only want the leader's strenght stat, since I only have 1 person in the party most of time, if that makes it easier

G_G

Okay so if the strength is 20 the max weight is 20?

fugibo

August 26, 2009, 04:05:30 pm #4 Last Edit: August 26, 2009, 05:09:10 pm by Longfellow
This easiest method will be name-tagging, or checking each Item's name for "\w[<v>]" and setting weight to V.

Also, you'll only want to do checking when gain_item, gain_weapon, gain_armor, lose_item, lose_weapon, or lose_armor is called.

*makes a version*

EDIT:

Something like this should work, but you'll have to do some fiddling to get it to *really* work with BlizzABS/DBS (ie making items stay on the ground if they're too heavy to pick up, etc, and making an improved items menu, etc):

#==============================================================================
# * Game_System
#==============================================================================
class Game_System
attr_accessor :wcw_weighted_active, :wcw_weighted_factor
#----------------------------------------------------------------------------
# initialize
#----------------------------------------------------------------------------
alias wcw_weighted_initialize_later initialize
def initialize
@wcw_weighted_active = true
@wcw_weighted_factor = 20
wcw_weighted_initialize_later
end
end

#==============================================================================
# ** WcW_Weighted
#==============================================================================
module WcW_Weighted
#----------------------------------------------------------------------------
# wcw_weighted_weight
#----------------------------------------------------------------------------
  def wcw_weighted_weight
  @name.scan /\\[[0-9]*?\]/
return $1 ? $1 : -1
end
#----------------------------------------------------------------------------
# wcw_weighted_remove_tag
#----------------------------------------------------------------------------
def wcw_weighted_remove_tag(str)
return str.gsub(/\w\[[0-9]*?\]/, '')
end
end

#==============================================================================
# * RPG::Item
#==============================================================================
class RPG::Item
include WcW_Weighted
#----------------------------------------------------------------------------
# name
#----------------------------------------------------------------------------
def name
return wcw_weighted_remove_tag @name
end
end

#==============================================================================
# * RPG::Weapon
#==============================================================================
class RPG::Weapon
include WcW_Weighted
#----------------------------------------------------------------------------
# name
#----------------------------------------------------------------------------
def name
return wcw_weighted_remove_tag @name
end
end

#==============================================================================
# * RPG::Armor
#==============================================================================
class RPG::Armor
include WcW_Weighted
#----------------------------------------------------------------------------
# name
#----------------------------------------------------------------------------
def name
return wcw_weighted_remove_tag @name
end
end

#==============================================================================
# * Game_Actor
#==============================================================================
class Game_Actor
#----------------------------------------------------------------------------
# wcw_weighted_max_weight?
#----------------------------------------------------------------------------
def wcw_weighted_max_weight?
return base_str / $game_system.wcw_weighted_factor
end
end

#==============================================================================
# * Game_Party
#==============================================================================
class Game_Party
attr_accessor :wcw_weighted_total_weight
#----------------------------------------------------------------------------
# initialize
#----------------------------------------------------------------------------
alias wcw_weighted_initialize_later initialize
def initialize
@wcw_weighted_total_weight = 0
end
#----------------------------------------------------------------------------
# gain_item(id)
#----------------------------------------------------------------------------
alias wcw_weighted_gain_item_later gain_item
def gain_item(id)
if (not $game_system.wcw_weighted_active) || ($data_items[id].wcw_weighted_weight ==
-1) || (@wcw_weighted_total_weight + $data_items[id].wcw_weighted_weight <
@actors[0].wcw_weighted_max_weight)
wcw_weighted_gain_item_later(id)
wcw_weighted_total_weight += $data_item[id].wcw_weighted_weight
end
end
#----------------------------------------------------------------------------
# gain_weapon(id)
#----------------------------------------------------------------------------
alias wcw_weighted_gain_weapon_later gain_weapon
def gain_weapon(id)
if (not $game_system.wcw_weighted_active) || ($data_weapons[id].wcw_weighted_weight ==
-1) || (@wcw_weighted_total_weight + $data_weapons[id].wcw_weighted_weight <
@actors[0].wcw_weighted_max_weight)
wcw_weighted_gain_weapon_later(id)
wcw_weighted_total_weight += $data_weapons[id].wcw_weighted_weight
end
end
#----------------------------------------------------------------------------
# gain_armor(id)
#----------------------------------------------------------------------------
alias wcw_weighted_gain_armor_later gain_armor
def gain_armor(id)
if (not $game_system.wcw_weighted_active) || ($data_armors[id].wcw_weighted_weight ==
-1) || (@wcw_weighted_total_weight + $data_armors[id].wcw_weighted_weight <
@actors[0].wcw_weighted_max_weight)
wcw_weighted_gain_armor_later(id)
wcw_weighted_total_weight += $data_armors[id].wcw_weighted_weight
end
end
#----------------------------------------------------------------------------
# lose_item(id)
#----------------------------------------------------------------------------
alias wcw_weighted_lose_item_later lose_item
def lose_item(id)
@wcw_weighted_total_weight -= $data_items[id].wcw_weighted_weight if $game_system.wcw_weighted_active
wcw_weighted_lose_item_later(id)
end
#----------------------------------------------------------------------------
# lose_weapon(id)
#----------------------------------------------------------------------------
alias wcw_weighted_lose_weapon_later lose_weapon
def lose_weapon(id)
@wcw_weighted_total_weight -= $data_weapons[id].wcw_weighted_weight if $game_system.wcw_weighted_active
wcw_weighted_lose_weapon_later(id)
end
#----------------------------------------------------------------------------
# lose_armor(id)
#----------------------------------------------------------------------------
alias wcw_weighted_lose_armor_later lose_armor
def lose_armor(id)
@wcw_weighted_total_weight -= $data_armors[id].wcw_weighted_weight if $game_system.wcw_weighted_active
wcw_weighted_lose_armor_later(id)
end
end


To give an item weight, simply add "\w[<weight>]" in its name in the editor.
To change the number of strength it takes to increase maximum weight by 1, change either $game_system.wcw_weighted_factor (in-game) or the default value in the script (search for @wcw_weighted_factor =).
To activate/deactivate the system, use $game_system.wcw_weighted_active = <true/false>.

Blizzard

Either that or a case-when configuration method.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Hellfire Dragon

August 27, 2009, 03:12:44 pm #6 Last Edit: August 27, 2009, 03:18:43 pm by Hellfire Dragon
Nice Lf :D ty, so if I wanted to display the inventory's total weight, what could I add to a scene?

fugibo

August 27, 2009, 08:34:45 pm #7 Last Edit: August 27, 2009, 08:41:37 pm by Longfellow
Quote from: Hellfire Dragon on August 27, 2009, 03:12:44 pm
Nice Lf :D ty, so if I wanted to display the inventory's total weight, what could I add to a scene?


To the window where you want to add it:
self.contents.draw_text(<x>, <y>, <w>, <h>, "Total Weight: #{$game_party.wcw_weighted_total_weight}")


EDIT:
Here's some of the useful APIs for my script:

- To get an the weight of an item, weapon, or armor, just use
<object>.wcw_weighted_weight

If it returns -1, then it's not "weighted," which means that no weight is set in the database. Use this how you want to; I made it -1 so it differentiates unset objects from objects weighing 0 (both act as if they weigh nothing, though).

- To get the maximum amount of weight an actor can carry, use
<actor>.wcw_weighted_max_weight


- The current weight of the inventory is stored in
$game_party.wcw_weighted_total_weight


Also, there are two bugs in my script that need to be fixed (I don't have time right now): First, changing the lead actor of the party will cause bugs with the weight (he'll be able to hold too many objects). Handling for this needs to be added to the corresponding code. Second, if the lead actor's strength is reduced, the same problem occurs. This will also need a bit of tinkering (you'll have to open a scene to remove items until the weight is small enough, or something like that).

Hellfire Dragon

August 28, 2009, 02:42:23 pm #8 Last Edit: August 28, 2009, 02:48:33 pm by Hellfire Dragon
Okay, thanks Lf ;)

EDIT: syntaxt Error on line 51

fugibo

Quote from: Hellfire Dragon on August 28, 2009, 02:42:23 pm
Okay, thanks Lf ;)

EDIT: syntaxt Error on line 51


You copied it wrong, line 51 is
#==============================================================================

Hellfire Dragon

Okay now there's a syntax error on line 25 :P

fugibo

August 30, 2009, 02:07:43 pm #11 Last Edit: August 30, 2009, 02:09:22 pm by Longfellow
Ick, I accidentally typed "[" instead of "w."

Here's the fixed version:

#==============================================================================
# * Game_System
#==============================================================================
class Game_System
 attr_accessor :wcw_weighted_active, :wcw_weighted_factor
#----------------------------------------------------------------------------
# initialize
#----------------------------------------------------------------------------
alias wcw_weighted_initialize_later initialize
def initialize
@wcw_weighted_active = true
@wcw_weighted_factor = 20
wcw_weighted_initialize_later
end
end

#==============================================================================
# ** WcW_Weighted
#==============================================================================
module WcW_Weighted
#----------------------------------------------------------------------------
# wcw_weighted_weight
#----------------------------------------------------------------------------
 def wcw_weighted_weight
  return @name.scan(/\\w\[[0-9]*?\]/i)[0]
end
#----------------------------------------------------------------------------
# wcw_weighted_remove_tag
#----------------------------------------------------------------------------
def wcw_weighted_remove_tag(str)
return str.gsub(/\w\[[0-9]*?\]/, '')
end
end

#==============================================================================
# * RPG::Item
#==============================================================================
class RPG::Item
include WcW_Weighted
#----------------------------------------------------------------------------
# name
#----------------------------------------------------------------------------
def name
return wcw_weighted_remove_tag @name
end
end

#==============================================================================
# * RPG::Weapon
#==============================================================================
class RPG::Weapon
include WcW_Weighted
#----------------------------------------------------------------------------
# name
#----------------------------------------------------------------------------
def name
return wcw_weighted_remove_tag @name
end
end

#==============================================================================
# * RPG::Armor
#==============================================================================
class RPG::Armor
include WcW_Weighted
#----------------------------------------------------------------------------
# name
#----------------------------------------------------------------------------
def name
return wcw_weighted_remove_tag @name
end
end

#==============================================================================
# * Game_Actor
#==============================================================================
class Game_Actor
#----------------------------------------------------------------------------
# wcw_weighted_max_weight?
#----------------------------------------------------------------------------
def wcw_weighted_max_weight?
return base_str / $game_system.wcw_weighted_factor
end
end

#==============================================================================
# * Game_Party
#==============================================================================
class Game_Party
  attr_accessor :wcw_weighted_total_weight
  #----------------------------------------------------------------------------
  # initialize
  #----------------------------------------------------------------------------
  alias wcw_weighted_initialize_later initialize
  def initialize
    @wcw_weighted_total_weight = 0
    wcw_weighted_initialize_later
  end
  #----------------------------------------------------------------------------
  # gain_item(id)
  #----------------------------------------------------------------------------
  alias wcw_weighted_gain_item_later gain_item
  def gain_item(id)
if (not $game_system.wcw_weighted_active) || ($data_items[id].wcw_weighted_weight ==
-1) || (@wcw_weighted_total_weight + $data_items[id].wcw_weighted_weight <
@actors[0].wcw_weighted_max_weight)
wcw_weighted_gain_item_later(id)
wcw_weighted_total_weight += $data_item[id].wcw_weighted_weight
end
end
#----------------------------------------------------------------------------
# gain_weapon(id)
#----------------------------------------------------------------------------
alias wcw_weighted_gain_weapon_later gain_weapon
def gain_weapon(id)
if (not $game_system.wcw_weighted_active) || ($data_weapons[id].wcw_weighted_weight ==
-1) || (@wcw_weighted_total_weight + $data_weapons[id].wcw_weighted_weight <
@actors[0].wcw_weighted_max_weight)
wcw_weighted_gain_weapon_later(id)
wcw_weighted_total_weight += $data_weapons[id].wcw_weighted_weight
end
end
#----------------------------------------------------------------------------
# gain_armor(id)
#----------------------------------------------------------------------------
alias wcw_weighted_gain_armor_later gain_armor
def gain_armor(id)
if (not $game_system.wcw_weighted_active) || ($data_armors[id].wcw_weighted_weight ==
-1) || (@wcw_weighted_total_weight + $data_armors[id].wcw_weighted_weight <
@actors[0].wcw_weighted_max_weight)
wcw_weighted_gain_armor_later(id)
wcw_weighted_total_weight += $data_armors[id].wcw_weighted_weight
end
end
#----------------------------------------------------------------------------
# lose_item(id)
#----------------------------------------------------------------------------
alias wcw_weighted_lose_item_later lose_item
def lose_item(id)
@wcw_weighted_total_weight -= $data_items[id].wcw_weighted_weight if $game_system.wcw_weighted_active
wcw_weighted_lose_item_later(id)
end
#----------------------------------------------------------------------------
# lose_weapon(id)
#----------------------------------------------------------------------------
alias wcw_weighted_lose_weapon_later lose_weapon
def lose_weapon(id)
@wcw_weighted_total_weight -= $data_weapons[id].wcw_weighted_weight if $game_system.wcw_weighted_active
wcw_weighted_lose_weapon_later(id)
end
#----------------------------------------------------------------------------
# lose_armor(id)
#----------------------------------------------------------------------------
alias wcw_weighted_lose_armor_later lose_armor
def lose_armor(id)
@wcw_weighted_total_weight -= $data_armors[id].wcw_weighted_weight if $game_system.wcw_weighted_active
wcw_weighted_lose_armor_later(id)
end
end