Files
LonaRPG-Online-Games/ModScripts/_Mods/LonaCraft/Scripts/20_Station.rb
T

173 lines
6.3 KiB
Ruby
Raw Normal View History

2026-10-29 21:59:06 +04:00
#==============================================================================
# LonaCraft - 20_Station.rb
# What it does:
# * The dark pots (inn pots, inn stoves, the placeable PlayerPot) are the
# game's own again: LonaCraft no longer redirects
# Data/HCGframes/Command_DarkPot.rb, so they only cook. Crafting by hand
# is the main menu's job (station :hand, 55_Recipes.rb).
# * lonacraft_run_original_pot runs that pot script (vanilla, or another
# mod's copy through the game's redirect table $mod_load_script,
# 102_ScriptImport.rb:63) for the Cook action of the Stove and the
# Cooking Fire furniture (90_Furniture.rb).
# * Salvage lives at the Carpentry Bench, Anvil and Tailor's Table
# (Scripts/90_Furniture.rb calls lonacraft_salvage_menu(station)). Each of
# those station kinds has its own storage box (SALVAGE_BOXES, shared by
# every bench of that kind, town or home). "Break it down" breaks only the
# gear whose dominant tag belongs to that station (Stations.for_item:
# Wood -> carpentry, Metal -> anvil, Cloth/Leather -> tailor) into
# materials, using ONLY the salvage recipes (game's
# darkPot_translate_recipe, per item), capped at a quarter of the item's
# price in material value. Other gear stays in the box.
#==============================================================================
module LonaCraft
POT_SCRIPT = "Data/HCGframes/Command_DarkPot.rb"
# Salvage box per station kind. The game uses map ids (< 1000) and
# 65533-65537; placed chests use 66000 and up (90_Furniture.rb).
SALVAGE_BOXES = {:carpentry => 65901, :anvil => 65902, :tailor => 65903}
def self.salvage_station?(station)
SALVAGE_BOXES.has_key?(station)
end
# Opens (and names) the station's salvage box; returns its id.
def self.salvage_box_id(station)
id = SALVAGE_BOXES[station]
$game_boxes.box(id)
$game_boxes.setup(id, $game_text["LonaCraft:Craft:Station/#{station}"], 0)
id
end
# Materials one unit of +item+ breaks into: {item_name => amount}.
# Only equipment with pot tags; key items are never salvaged.
def self.salvage_yield(interpreter, item)
out = {}
return out unless item.is_a?(RPG::EquipItem)
return out if item.key_item?
return out unless item.pot_data.is_a?(Hash) && !item.pot_data.empty?
tags = Hash.new(0)
item.pot_data.each{|tag, value| tags[tag] += value }
got = Hash.new(0)
salvage_recipes.each{|recipe|
tags, got = interpreter.darkPot_translate_recipe(recipe, tags, got)
}
got.each{|name, amount| out[name] = amount.to_i if amount.to_i > 0 }
cap_salvage_value(item, out)
end
# Salvage never returns more than SALVAGE_VALUE_SHARE of the item's price
# in material value (count x the material's own price). Yields are cut one
# unit at a time: Nails first, then the material worth the most in this
# yield, then the next, until the value is at or under the cap. An item
# whose cap is below one unit of every material it yields gives nothing
# (it stays in the pot; lonacraft_salvage_run reports "nothing" if no item
# in the pot gave anything).
SALVAGE_VALUE_SHARE = 0.25
def self.salvage_value(out)
total = 0.0
out.each{|name, amount|
obj = $data_ItemName[name]
next if obj.nil?
total += amount.to_i * obj.price.to_f
}
total
end
def self.cap_salvage_value(item, out)
return out if out.empty?
# box keys can be stale save copies: use this session's item for the price
src = item.respond_to?(:item_name) ? $data_ItemName[item.item_name.to_s] : nil
src = item if src.nil?
cap = src.price.to_f * SALVAGE_VALUE_SHARE
nails = MATERIAL["Nails"]
order = []
order << nails if out.has_key?(nails)
rest = out.keys.reject{|name| name == nails }
rest = rest.sort_by{|name|
obj = $data_ItemName[name]
-(out[name].to_i * (obj ? obj.price.to_f : 0.0))
}
order.concat(rest)
order.each{|name|
guard = 0 # one unit per pass, plus a hard cap
while out[name].to_i > 0 && salvage_value(out) > cap && (guard += 1) <= 100000
out[name] = out[name].to_i - 1
end
out.delete(name) if out[name].to_i <= 0
}
out
end
# Add +amount+ of +obj+ to a storage box hash. Box keys are RPG objects and
# may be save-file copies, so match by item_name before adding a new key.
def self.box_add(box, obj, amount)
key = box.keys.find{|k| k.equal?(obj) }
key ||= box.keys.find{|k| k.class == obj.class && k.respond_to?(:item_name) && k.item_name == obj.item_name }
key ||= obj
box[key] = box[key].to_i + amount
end
end
class Game_Interpreter
# The dark pot's cooking (Stove and Cooking Fire "Cook"): the game's
# Command_DarkPot.rb, or another mod's copy (load_script follows
# $mod_load_script itself). It ends the event with its own eventPlayEnd.
def lonacraft_run_original_pot
load_script(LonaCraft::POT_SCRIPT)
end
# station: :carpentry, :anvil or :tailor
def lonacraft_salvage_menu(station)
return unless LonaCraft.salvage_station?(station)
call_msg("LonaCraft:Station:Salvage/begin_#{station}")
tmpPick = $game_temp.choice
$game_temp.choice = -1
case tmpPick
when 1 # Add/Remove equipment
box_id = LonaCraft.salvage_box_id(station)
SceneManager.goto(Scene_ItemStorage)
SceneManager.scene.prepare(box_id)
when 2 # Break it down
lonacraft_salvage_run(station)
end
end
def lonacraft_salvage_run(station)
actor = $game_player.actor
if actor.sta <= 0 # same rule as cooking
SndLib.sys_buzzer
$game_map.popup(0,"QuickMsg:Lona/sta_too_low#{rand(2)}",0,0)
return
end
box_id = LonaCraft.salvage_box_id(station)
box = $game_boxes.box(box_id)
results = Hash.new(0)
used = 0
box.keys.each{|item|
count = box[item].to_i
next if count < 1
# only gear whose dominant tag belongs to this station
next unless LonaCraft::Stations.for_item(item) == station
out = LonaCraft.salvage_yield(self, item)
next if out.empty?
out.each{|name, amount| results[name] += amount * count }
used += count
box.delete(item)
}
if used < 1
SndLib.sys_buzzer
call_msg("LonaCraft:Station:Salvage/nothing_#{station}")
return
end
actor.sta -= used # 1 STA per item broken down, as cooking costs 1 per item
results.each{|name, amount|
obj = $data_ItemName[name]
next if obj.nil? || amount < 1
LonaCraft.box_add(box, obj, amount)
}
SndLib.WoodenBuild
call_msg("LonaCraft:Station:Salvage/working")
SceneManager.goto(Scene_ItemStorage)
SceneManager.scene.prepare(box_id)
end
end