138 lines
5.1 KiB
Ruby
138 lines
5.1 KiB
Ruby
# encoding: UTF-8
|
|
# LonaCraft 1.1.2 / NoerHome coexistence: three independent house offers.
|
|
# Only the NoerHome bank's main "Buy house" choice is intercepted. We never
|
|
# replace NoerHome's script, payment, online save, or private-house state.
|
|
module LonaCraft
|
|
module Homes
|
|
BANK_HOUSE_IDS = ["noer_shack", "noer_stonehouse"]
|
|
|
|
# NoerHome tracks its own private property in story stats, including
|
|
# online updates in NoerHomeEstate. Never put it in lonacraft_data.
|
|
def self.noerhome_private_bought?
|
|
return true if defined?(NoerHomeEstate) && NoerHomeEstate.respond_to?(:house_bought?) && NoerHomeEstate.house_bought?
|
|
defined?($story_stats) && $story_stats && $story_stats["NoerHouseBought"].to_i != 0
|
|
rescue StandardError
|
|
false
|
|
end
|
|
|
|
def self.noerhome_buy_label
|
|
return "" unless defined?($noer_home_config) && $noer_home_config
|
|
cfg = $noer_home_config["noer_home"]
|
|
return "" unless cfg
|
|
res = cfg["resourse_name"].to_s
|
|
res = "NoerHome" if res.empty?
|
|
$game_text["#{res}:NoerHome:Bank/Buy"].to_s
|
|
rescue StandardError
|
|
""
|
|
end
|
|
|
|
def self.noerhome_private_price
|
|
cfg = $noer_home_config["noer_home"]
|
|
cfg["price"].to_i
|
|
rescue StandardError
|
|
0
|
|
end
|
|
|
|
# Only recognize NoerHome's actual bank menu (contains its Buy label
|
|
# AND the game's two regular bank options). This also works if NoerHome
|
|
# loads after LonaCraft and sets the script redirect later.
|
|
def self.noerhome_bank_info(dialog)
|
|
return nil unless enabled? && noerhome_active? && dialog.is_a?(String)
|
|
match = /\\optB\[([^\]]*)\]/.match(dialog)
|
|
return nil unless match
|
|
items = match[1].split(",").map{|v| v.strip}.reject{|v| v.empty?}
|
|
buy = noerhome_buy_label
|
|
barter = $game_text["commonNPC:commonNPC/Barter"].to_s
|
|
storage = $game_text["commonNPC:commonNPC/Storage"].to_s
|
|
return nil if buy.empty? || !items.include?(barter) || !items.include?(storage)
|
|
idx = items.index(buy)
|
|
return nil if idx.nil?
|
|
[idx, items, match]
|
|
rescue StandardError => error
|
|
p "LonaCraft NoerHome bank detection: #{error.class}: #{error.message}"
|
|
nil
|
|
end
|
|
|
|
def self.bank_available_houses
|
|
result = []
|
|
result << ["private", noerhome_private_price] unless noerhome_private_bought?
|
|
BANK_HOUSE_IDS.each do |id|
|
|
loc = location(id)
|
|
result << [id, loc[:price].to_i] if loc && available?(loc) && !owned?(id)
|
|
end
|
|
result
|
|
end
|
|
|
|
def self.bank_house_label(id, price)
|
|
name = if id == "private"
|
|
text("bank/private_house")
|
|
else
|
|
text("#{id}/name")
|
|
end
|
|
clean_label("#{name} — #{price} #{text('common/gold')}")
|
|
end
|
|
|
|
def self.bank_back_label
|
|
clean_label(text("bank/back"))
|
|
end
|
|
end
|
|
end
|
|
|
|
class Game_Interpreter
|
|
unless method_defined?(:show_npc_info_before_lc_noerhome_three_houses)
|
|
alias_method :show_npc_info_before_lc_noerhome_three_houses, :show_npc_info
|
|
def show_npc_info(*args)
|
|
info = args.length >= 3 ? LonaCraft::Homes.noerhome_bank_info(args[2]) : nil
|
|
return show_npc_info_before_lc_noerhome_three_houses(*args) unless info
|
|
|
|
buy_index, main_labels, match = info
|
|
homes = LonaCraft::Homes
|
|
offers = homes.bank_available_houses
|
|
|
|
# After buying all three, also hide "Buy house" in the main bank
|
|
# menu. Translate the resulting index back to the NoerHome script's
|
|
# original cmd_sheet, preserving Barter, Storage and other mod actions.
|
|
if offers.empty?
|
|
text = args[2]
|
|
start = match.begin(1)
|
|
finish = match.end(1)
|
|
labels = main_labels.dup
|
|
labels.delete_at(buy_index)
|
|
reduced = text[0...start] + labels.join(",") + "," + text[finish..-1]
|
|
actual_args = args.dup
|
|
actual_args[2] = reduced
|
|
result = show_npc_info_before_lc_noerhome_three_houses(*actual_args)
|
|
pick = $game_temp.choice
|
|
if pick.is_a?(Integer) && pick >= buy_index && pick < labels.length
|
|
$game_temp.choice = pick + 1
|
|
end
|
|
return result
|
|
end
|
|
|
|
result = show_npc_info_before_lc_noerhome_three_houses(*args)
|
|
return result unless $game_temp.choice == buy_index
|
|
|
|
# Submenu: private NoerHome, LonaCraft shack, LonaCraft stone house.
|
|
# Paid/bought places disappear each time it is opened.
|
|
# Choosing private returns the original index so NoerHome executes
|
|
# its untouched purchase confirmation, charging, and cloud sync.
|
|
$game_temp.choice = -1
|
|
labels = [homes.bank_back_label] + offers.map{|id, cost| homes.bank_house_label(id, cost)}
|
|
call_msg("\\optB[#{labels.join(',')}]")
|
|
sub_pick = $game_temp.choice
|
|
$game_temp.choice = -1
|
|
return result unless sub_pick.is_a?(Integer) && sub_pick > 0 && sub_pick <= offers.length
|
|
|
|
chosen_id = offers[sub_pick - 1][0]
|
|
if chosen_id == "private"
|
|
# Recheck the status after an asynchronous menu/UI wait.
|
|
$game_temp.choice = buy_index unless homes.noerhome_private_bought?
|
|
else
|
|
# The buy() API rechecks ownership and gold, preventing double pay.
|
|
lonacraft_home_purchase(chosen_id)
|
|
end
|
|
result
|
|
end
|
|
end
|
|
end
|