$imported = {} if $imported.nil?
$imported['cshop'] = true
puts 'Load: Shop script by Clyve'
module CSHOP
module REGEXP
ITEM_REQ = /<require ([IWA])(\d+), (\d+)>/i
end
end
class RPG::ShopSales
#------------------------
attr_accessor :item_sales
attr_accessor :weapon_sales
attr_accessor :armor_sales
#------------------------
def initialize
# arrys that track how many each item has been sold. index = item id
@item_sales = []
@weapon_sales = []
@armor_sales = []
end
end
class Scene_Shop < Scene_MenuBase
alias :shopsales_prepare :prepare
def prepare(goods, purchase_only)
shopsales_prepare(goods, purchase_only)
add_available_goods
end
alias :shopsales_start :start
def start
shopsales_start
@message_window = Window_Message.new
end
def add_available_goods
# CHECK NOTETAGS AND WHETHER $data_shopsales's DATA IS ENOUGH
# FOR THE ITEMS TO BE AVAILABLE OR NOT, THEN ADD THE ITEM TO @goods
# IF ITS AVAILABLE.
# @goods is an array with three values
# @goods[0] is either 0, 1, or 2.
# 0 is Items ($data_items)
# 1 is Weapons ($data_weapons)
# 2 is Armors ($data_armors)
# @goods[1] is the index of the item.
# @goods[2] is a different price than the one in the database, it needs to
# be set to 0 for it to use the price in the database
for i in 1...$data_items.size
if !$data_items[i].sell_req.empty?
include = true
for req in $data_items[i].sell_req
if req[0] == 0
if $data_shopsales.item_sales[req[1]] < req[2]
include = false
end
elsif req[0] == 1
if $data_shopsales.weapon_sales[req[1]] < req[2]
include = false
end
elsif req[0] == 2
if $data_shopsales.armor_sales[req[1]] < req[2]
include = false
end
end
end
@goods.push([0, i, 0]) if include == true
end
# This is where you'll use @goods[0]
end
for i in 1...$data_weapons.size
if !$data_weapons[i].sell_req.empty?
include = true
for req in $data_weapons[i].sell_req
if req[0] == 0
if $data_shopsales.item_sales[req[1]] < req[2]
include = false
end
elsif req[0] == 1
if $data_shopsales.weapon_sales[req[1]] < req[2]
include = false
end
elsif req[0] == 2
if $data_shopsales.armor_sales[req[1]] < req[2]
include = false
end
end
end
@goods.push([1, i, 0]) if include == true
end
# This is where you'll use @goods[1]
end
for i in 1...$data_armors.size
if !$data_armors[i].sell_req.empty?
include = true
for req in $data_armors[i].sell_req
if req[0] == 0
if $data_shopsales.item_sales[req[1]] < req[2]
include = false
end
elsif req[0] == 1
if $data_shopsales.weapon_sales[req[1]] < req[2]
include = false
end
elsif req[0] == 2
if $data_shopsales.armor_sales[req[1]] < req[2]
include = false
end
end
end
@goods.push([2, i, 0]) if include == true
end
# This is where you'll use @goods[2]
end
end
# Increase Items of Type Sold
alias :shopsales_do_sell :do_sell
def do_sell(number)
shopsales_do_sell(number)
added_items = []
case item_type
when 0
$data_shopsales.item_sales[@item.id] += number
# This is where you'll use @goods[0]
when 1
$data_shopsales.weapon_sales[@item.id] += number
# This is where you'll use @goods[1]
when 2
$data_shopsales.armor_sales[@item.id] += number
# This is where you'll use @goods[2]
end
for i in 1...$data_items.size
if !$data_items[i].sell_req.empty?
include = true
for req in $data_items[i].sell_req
if req[0] == 0
if $data_shopsales.item_sales[req[1]] < req[2]
include = false
end
elsif req[0] == 1
if $data_shopsales.weapon_sales[req[1]] < req[2]
include = false
end
elsif req[0] == 2
if $data_shopsales.armor_sales[req[1]] < req[2]
include = false
end
end
end
if include == true && !@goods.include?([0, i, 0])
@goods.push([0, i, 0])
added_items.push($data_items[i].name)
end
end
end
for i in 1...$data_weapons.size
if !$data_weapons[i].sell_req.empty?
include = true
for req in $data_weapons[i].sell_req
if req[0] == 0
if $data_shopsales.item_sales[req[1]] < req[2]
include = false
end
elsif req[0] == 1
if $data_shopsales.weapon_sales[req[1]] < req[2]
include = false
end
elsif req[0] == 2
if $data_shopsales.armor_sales[req[1]] < req[2]
include = false
end
end
end
if include == true && !@goods.include?([1, i, 0])
@goods.push([1, i, 0])
added_items.push($data_weapons[i].name)
end
end
end
for i in 1...$data_armors.size
if !$data_armors[i].sell_req.empty?
include = true
for req in $data_armors[i].sell_req
if req[0] == 0
if $data_shopsales.item_sales[req[1]] < req[2]
include = false
end
elsif req[0] == 1
if $data_shopsales.weapon_sales[req[1]] < req[2]
include = false
end
elsif req[0] == 2
if $data_shopsales.armor_sales[req[1]] < req[2]
include = false
end
end
end
if include == true && !@goods.include?([2, i, 0])
@goods.push([2, i, 0])
added_items.push($data_armors[i].name)
end
end
end
if !added_items.empty?
$game_message.add("Thanks! I can now sell you the following items:")
added_items.each_with_index do |item, i|
if i < added_items.size - 1
$game_message.add("#{item}, ")
else
$game_message.add("#{item}")
end
end
@message_window.update
end
end
# Checks Item Type of @item
def item_type
if @item.is_a?(RPG::Item)
return 0
elsif @item.is_a?(RPG::Weapon)
return 1
elsif @item.is_a?(RPG::Armor)
return 2
end
end
end
module DataManager
# Initialize Shop Sales data
class <<self; alias shopsales_load_database load_database; end
def self.load_database
shopsales_load_database
$data_shopsales = RPG::ShopSales.new
load_item_notetags
init_shopsales_data
end
def self.load_item_notetags
groups = [$data_items, $data_weapons, $data_armors]
for group in groups
for obj in group
next if obj.nil?
obj.load_item_notetags
end
end
puts "Read: Item Requirements Notetags"
end
def self.init_shopsales_data
for index in 0..$data_items.size
$data_shopsales.item_sales[index] = 0
end
for index in 0..$data_weapons.size
$data_shopsales.weapon_sales[index] = 0
end
for index in 0..$data_armors.size
$data_shopsales.armor_sales[index] = 0
end
end
# Dump Shop Sales data into save file
class <<self; alias shopsales_make_save_contents make_save_contents; end
def self.make_save_contents
shopsales_make_save_contents
contents[:shopsales] = $data_shopsales
contents
end
# Load Shop Sales data from save file
class <<self; alias shopsales_extract_save_contents extract_save_contents; end
def self.extract_save_contents(contents)
shopsales_extract_save_contents(contents)
if contents[:shopsales]
$data_shopsales = contents[:shopsales]
else
$data_shopsales = RPG::ShopSales.new
init_shopsales_data
end
end
end
class RPG::BaseItem
attr_reader :sell_req
def load_item_notetags
@sell_req = []
@note.split(/[\r\n]+/).each do |line|
case line
when CSHOP::REGEXP::ITEM_REQ
case $1
when "I"
@sell_req.push([0, $2.to_i, $3.to_i])
when "W"
@sell_req.push([1, $2.to_i, $3.to_i])
when "A"
@sell_req.push([2, $2.to_i, $3.to_i])
end
end
end
end
end