[VX] Item Cap

Started by AngryPacman, January 03, 2012, 09:06:43 am

Previous topic - Next topic

AngryPacman

Item Cap
Authors: Pacman
Version: 2.3
Type: Individual Item Cap System
Key Term: Player / Party / Troop Add-on



Introduction

There are quite a few scripts that serve a similar purpose to this one, but to my knowledge this is the only one that does this specifically: gives each item, weapon or armor it's own possession cap, that is displayed in shop, equip and item screens. You can also set (and change in-game) the maximum amount of gold attainable. You can also now set certain items to not have their cap drawn in the windows.


Features


  • Notebox tag for setting individual item caps on each item

  • Notebox tag for not drawing the item's cap in windows

  • Altering limit on gold, also changeable in-game

  • Displays item cap in item (battle), equip and shop scenes

  • You can set which types of items (items, weapons and armors) have their cap displayed in the inventory scenes




Screenshots


Screenshot of rgangsta's CyberDrive.



Demo

Not necessary.


Script

Spoiler: ShowHide

#===============================================================================
#
# Item Cap - Pacman (29/12/2011)
# Version 2.3
# Written for rgangsta (http://www.omega-dev.net/forums/showthread.php?tid=1280)
# Suggestion by Scalinger2
# Use the tag <item cap: x> to set the cap of an item to number x.
# Use the tax \EXEMPT_CAP to make the windows not draw the cap at all in the
# item and shop scenes.
# Set the default maximum gold in the module below.
# Set the default caps of items (when not set through notebox) in the module.
# Change the TEXT if you want for some reason.
# You can change the maximum amount of gold with the script call:
# $game_system.gold_max = x
# Where x is the maximum amount of gold.
# Set which items can have their item cap drawn in the below module. The
# available options are:
#   :items
#   :weapons
#   :armors
# New in 2.1: no longer crashes upon entering equip scene.
# New in 2.2: no longer crashes upon entering shop scene.
# New in 2.3: draws item name more efficiently.
#
#===============================================================================
#
# CONFIGURATION
#
#===============================================================================

module ITEM_CAP
  GOLD_MAX = 40000    # Maximum amount of gold
  DEFAULT_LIMIT = 50  # Default item cap (if not set through notebox)
  TEXT = ": %s / %s"    # Text to display the cap
  SHOW_CAP = [        # Show cap of which items?
    :items,           # Items
    :weapons,         # Weapons
    :armors,          # Armors
  ]
end

#===============================================================================
#
# NO EDITING >:D
#
#===============================================================================

#==============================================================================
# ** RPG::BaseItem
#------------------------------------------------------------------------------
# Superclass of Skill, Item, Weapon, and Armor.
#==============================================================================

class RPG::BaseItem
  #--------------------------------------------------------------------------
  # * Maximum possession of item
  #--------------------------------------------------------------------------
  def item_cap
    return if self.is_a?(RPG::Skill)  # Only items, weapons and armors
    return @item_cap if @item_cap != nil  # Don't perform if already set
    @item_cap = ITEM_CAP::DEFAULT_LIMIT
    self.note.split(/[\r\n]+/).each { |line|
      case line
      when /<(?:ITEM_CAP|item cap):[ ](\d+)>/i
        @item_cap = $1.to_i
      end
    }
    return @item_cap
  end
  #--------------------------------------------------------------------------
  # * Don't draw the item's cap
  #--------------------------------------------------------------------------
  def exempt_cap?
    return if self.is_a?(RPG::Skill)
    return @exempt if @exempt != nil
    @exempt = false
    self.note.split(/[\r\n]+/).each { |line|
      case line
      when /\\EXEMPT_CAP/i
        @exempt = true
      end
    }
    return @exempt
  end
end

#==============================================================================
# ** Game_System
#------------------------------------------------------------------------------
#  This class handles system-related data. Also manages vehicles and BGM, etc.
# The instance of this class is referenced by $game_system.
#==============================================================================

class Game_System
  #--------------------------------------------------------------------------
  # Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor :gold_cap
  #--------------------------------------------------------------------------
  # alias listing
  #--------------------------------------------------------------------------
  alias item_max_initialize initialize
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    item_max_initialize
    @gold_cap = ITEM_CAP::GOLD_MAX
  end
end

#==============================================================================
# ** Game_Party
#------------------------------------------------------------------------------
#  This class handles the party. It includes information on amount of gold
# and items. The instance of this class is referenced by $game_party.
#==============================================================================

class Game_Party < Game_Unit
  #--------------------------------------------------------------------------
  # alias listing
  #--------------------------------------------------------------------------
  alias item_max_gain_gold gain_gold
  alias item_max_gain_item gain_item
  #--------------------------------------------------------------------------
  # * Gain Gold (or lose)
  #     n : amount of gold
  #--------------------------------------------------------------------------
  def gain_gold(n)
    new_gold = item_max_gain_gold(n)
    @gold = [new_gold, $game_system.gold_cap].min
  end
  #--------------------------------------------------------------------------
  # * Gain Items (or lose)
  #     item          : Item
  #     n             : Number
  #     include_equip : Include equipped items
  #--------------------------------------------------------------------------
  def gain_item(item, n, include_equip = false)
    unless $scene.is_a?(Scene_Equip)
      number = item_number(item)
      cap = item.item_cap
      case item
      when RPG::Item
        @items[item.id] = [[number + n, 0].max, cap].min
      when RPG::Weapon
        @weapons[item.id] = [[number + n, 0].max, cap].min
      when RPG::Armor
        @armors[item.id] = [[number + n, 0].max, cap].min
      end
      n += number
      if include_equip and n < 0
        for actor in members
          while n < 0 and actor.equips.include?(item)
            actor.discard_equip(item)
            n += 1
          end
        end
      end
    else
      item_max_gain_item(item, n, include_equip)
    end
  end
end

#==============================================================================
# ** Window_Base
#------------------------------------------------------------------------------
#  This is a superclass of all windows in the game.
#==============================================================================

class Window_Base
  #--------------------------------------------------------------------------
  # * Draw Item Name
  #     item    : Item (skill, weapon, armor are also possible)
  #     x       : draw spot x-coordinate
  #     y       : draw spot y-coordinate
  #     enabled : Enabled flag. When false, draw semi-transparently.
  #     ctext   : Text to display the cap.
  #--------------------------------------------------------------------------
  def draw_item_name(item, x, y, enabled = true, ctext = "")
    if item != nil
      w = self.contents.text_size(ctext).width - 48
      draw_icon(item.icon_index, x, y, enabled)
      self.contents.font.color = normal_color
      self.contents.font.color.alpha = enabled ? 255 : 128
      self.contents.draw_text(x + 24, y, 172 - w, WLH, item.name)
    end
  end
end

#==============================================================================
# ** Window_Item
#------------------------------------------------------------------------------
#  This window displays a list of inventory items for the item screen, etc.
#==============================================================================

class Window_Item < Window_Selectable
  #--------------------------------------------------------------------------
  # alias listing
  #--------------------------------------------------------------------------
  alias item_cap_draw_item draw_item
  #--------------------------------------------------------------------------
  # * Draw Item
  #     index : item number
  #--------------------------------------------------------------------------
  def draw_item(index)
    if draw_cap?(index)
      rect = item_rect(index)
      self.contents.clear_rect(rect)
      item = @data[index]
      if item != nil
        number1 = $game_party.item_number(item); number2 = item.item_cap
        enabled = enable?(item)
        rect.width -= 4
        ctext = sprintf(ITEM_CAP::TEXT, number1, number2)
        draw_item_name(item, rect.x, rect.y, enabled, ctext)
        self.contents.draw_text(rect, ctext, 2)
      end
    else
      item_cap_draw_item(index)
    end
  end
  #--------------------------------------------------------------------------
  # * Determine whether or not to draw cap
  #     item : item to be checked
  #--------------------------------------------------------------------------
  def draw_cap?(index)
    check = ITEM_CAP::SHOW_CAP
    item = @data[index]
    return true if item == nil and $scene.is_a?(Scene_Equip)
    return false if item.exempt_cap?
    return true if check.include?(:items) and item.is_a?(RPG::Item)
    return true if check.include?(:weapons) and item.is_a?(RPG::Weapon)
    return true if check.include?(:armors) and item.is_a?(RPG::Armor)
    return false
  end
end

#==============================================================================
# ** Window_ShopBuy
#------------------------------------------------------------------------------
#  This window displays buyable goods on the shop screen.
#==============================================================================

class Window_ShopBuy < Window_Selectable
  #--------------------------------------------------------------------------
  # * Draw Item
  #     index : item number
  #--------------------------------------------------------------------------
  def draw_item(index)
    item = @data[index]
    number = $game_party.item_number(item)
    enabled = (item.price <= $game_party.gold and number < item.item_cap)
    rect = item_rect(index)
    self.contents.clear_rect(rect)
    draw_item_name(item, rect.x, rect.y, enabled)
    rect.width -= 4
    self.contents.draw_text(rect, item.price, 2)
  end
end

#==============================================================================
# ** Window_ShopStatus
#------------------------------------------------------------------------------
#  This window displays number of items in possession and the actor's equipment
# on the shop screen.
#==============================================================================

class Window_ShopStatus < Window_Base
  #--------------------------------------------------------------------------
  # Alias listing
  #--------------------------------------------------------------------------
  alias item_cap_refresh refresh
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    if draw_cap?(@item)
      self.contents.clear
      if @item != nil
        number = $game_party.item_number(@item); cap = @item.item_cap
        text = sprintf(ITEM_CAP::TEXT, number, cap)
        self.contents.font.color = system_color
        self.contents.draw_text(4, 0, 200, WLH, Vocab::Possession)
        self.contents.font.color = normal_color
        self.contents.draw_text(4, 0, 200, WLH, text, 2)
        for actor in $game_party.members
          x = 4
          y = WLH * (2 + actor.index * 2)
          draw_actor_parameter_change(actor, x, y)
        end
      end
    else
      item_cap_refresh
    end
  end
  #--------------------------------------------------------------------------
  # * Determine whether or not to draw cap
  #     item : item to be checked
  #--------------------------------------------------------------------------
  def draw_cap?(index)
    check = ITEM_CAP::SHOW_CAP
    item = @item
    return true unless item
    return false if item.exempt_cap?
    return true if check.include?(:items) and item.is_a?(RPG::Item)
    return true if check.include?(:weapons) and item.is_a?(RPG::Weapon)
    return true if check.include?(:armors) and item.is_a?(RPG::Armor)
    return false
  end
end

#==============================================================================
# ** Scene_Shop
#------------------------------------------------------------------------------
#  This class performs shop screen processing.
#==============================================================================

class Scene_Shop < Scene_Base
  #--------------------------------------------------------------------------
  # * Update Buy Item Selection
  #--------------------------------------------------------------------------
  def update_buy_selection
    @status_window.item = @buy_window.item
    if Input.trigger?(Input::B)
      Sound.play_cancel
      @command_window.active = true
      @dummy_window.visible = true
      @buy_window.active = false
      @buy_window.visible = false
      @status_window.visible = false
      @status_window.item = nil
      @help_window.set_text("")
      return
    end
    if Input.trigger?(Input::C)
      @item = @buy_window.item
      number = $game_party.item_number(@item)
      if @item == nil or @item.price > $game_party.gold or
       number == @item.item_cap
        Sound.play_buzzer
      else
        Sound.play_decision
        max = @item.price == 0 ? @item.item_cap : $game_party.gold / @item.price
        max = [max, @item.item_cap - number].min
        @buy_window.active = false
        @buy_window.visible = false
        @number_window.set(@item, max, @item.price)
        @number_window.active = true
        @number_window.visible = true
      end
    end
  end
end

#===============================================================================
#
# END OF SCRIPT
#
#===============================================================================



Instructions

Paste near the top of your custom script list, but still below Materials. I'll be directing you to the instructions included in the script, conveniently situated next to the applicable configuration options.


Compatibility

I overwrote some methods, but I don't know if that was avoidable. Paste at the top of your script list for improved compatibility. If you discover an issue, post it here or at RMRK.


Credits and Thanks


  • Rgangsta, for the request

  • Xzygon for reporting a kind of bug

  • Scalinger2, for suggesting something

  • Mog, I used his similar XP script as guidance




Author's Notes

Ham?
G_G's a silly boy.