#============================================================================== # LonaCraft - 50_Tiers.rb # What it does: # * Quality tiers for crafted gear. Items have no per-instance data in this # engine (inventory = count per item id, equip slot = item id), so each # tier is its own item type: tier 1 "Common" is the original item, tiers # 2-5 are clones generated at load and named "_LC". # * CRAFTABLE is chosen at load from $data_weapons / $data_armors (game + # RolePlayS + anything loaded before LonaCraft): equipment whose pot_data # has Wood, Metal, Cloth or Leather, that is not a key item, not bondage / # hair / debug, not Cocona's, has a price, and whose name has none of # Cuff, Collar, Chain, Rope, Bondage, Slave. The mod's own weapons # (LonaCraft::WEAPON_NAMES, 0_Core.rb: the Hatchet) are left out: the # Hatchet is a weak tool and always comes as the plain item. # * Each variant is a deep copy of the base RPG object (same icon, etype, # wtype/atype, skills, portrait layer, weight, pot tags) rebuilt through the # game's own ItemConfigs#load_lona_effect_from_hash with: # price x PRICE_MULT, primary stat x STAT_MULT, fixed bonus lines # (BONUSES), and private text keys for name and description. # Its portrait layer (LonaStat) is built explicitly with createLonaState, # as Game_Actor#init_lonaStat_weapons/armors (25_Game_Actor.rb:205-224) # would; change_equip reads it at 25_Game_Actor.rb:490-491. # * Variants are appended with id = table length (same as RolePlayS, # RolePlayS\Scripts\0_CORE.rb:337-347) and registered in $data_ItemName. # Each also gets a copy of its base's ground-drop EventLib event, so it # can be dropped and picked up like the base. # * Text: every menu, popup and storage screen reads names and descriptions # as $game_text[item.name] / $game_text[item.description] # (e.g. 191_menu_07items.rb, 188_menu_06equips.rb, 203_Storage Boxes.rb, # 53_Window_Base.rb, 57_Window_Help.rb and the RolePlayS copies), and # $game_text is a Text (121_Dialog_Control_System.rb:98 Text#[]). Text#[] # is aliased here, the lowest point all of them share, to answer the # variants' private keys "LCTier:name:" / "LCTier:desc:" # with " ()" (shape from Tiers:Name/format) and " + a coloured tier # line with the item's ATK/DEF and HP/STA/SPD bonuses". The craftable # bases' own name/description keys (BASE_KEYS, only keys no other item # uses) get the same treatment as "Common"; only the returned text # changes, never item_name or the key. The part "LCTier" is never registered with $game_text, so if # the alias were missing the game would return the key, not a msgbox. # Language switches rebuild $game_text from the same class, so the alias # keeps working. #============================================================================== module LonaCraft module Tiers # [tier, last-resort name, text colour index for \C[n]]. The names are keyed # (Tiers.txt Tier/name1..5, ENG fallback in 15_Text.rb); these English words # only show when $game_text is not up yet or Text/ENG cannot be read. # Colours read from Graphics/System/Window.png (the RolePlayS copy has the # same palette); this skin differs from default RPG Maker: # 0 white, 1 red-orange, 2 green (42,255,0), 3 blue (66,101,255), # 4 cyan, 5 magenta (255,0,255), 6 yellow (255,255,0). TIERS = [ [1, "Common", 0], [2, "Uncommon", 2], [3, "Rare", 3], [4, "Epic", 5], [5, "Legendary", 6] ] MAX_TIER = 5 PRICE_MULT = [1, 1.5, 2.5, 4, 7] STAT_MULT = [1.0, 1.1, 1.2, 1.35, 1.5] # Legendary move_speed bonus. The base walk speed is 4.3 and RGSS moves # 2**speed/256 tiles per frame, so +1 would double the speed per piece # (and stack over 8 armour slots). Gear in the game changes it by 0.03-0.1. MOVE_SPEED_BONUS = 0.1 # Fixed bonuses per tier: [stat, attribute_type, amount]. # sta/health use BTmax like the game's own gear (NunMidExtra.json). # atk_plus/def_plus are "reality" stats (94_Game_Actor_Stats.rb:275-276, # type 1): reset_definition skips them (:195-204), so BTmax on them would # do nothing; the game's gear uses "max" for them, and so do these. # They are rebuilt from defaults on every refresh (reset_reality_stats # :40-48), so "max" does not accumulate. Never "current" (it re-applies # on every refresh, 25_Game_Actor.rb:1406). :primary = atk_plus or def_plus. BONUSES = { 1 => [], 2 => [["sta", "BTmax", 5]], 3 => [["sta", "BTmax", 5], ["health", "BTmax", 5]], 4 => [["sta", "BTmax", 10], ["health", "BTmax", 5], [:primary, "max", 1]], 5 => [["sta", "BTmax", 15], ["health", "BTmax", 10], [:primary, "max", 2], ["move_speed", "max", MOVE_SPEED_BONUS]] } # attribute types whose "+" entries count as the primary stat and get scaled SCALED_TYPES = ["max", "Tmax", "BTmax"] CRAFT_TAGS = ["Wood", "Metal", "Cloth", "Leather"] EXCLUDE_NAME = /Cuff|Collar|Chain|Rope|Bondage|Slave|Cocona/ EXCLUDE_TAGS = ["Key", "Bondage", "Hair", "Debug"] VARIANT_SUFFIX = "_LC" VARIANT_RE = /_LC([2-9])\z/ TEXT_PREFIX = "LCTier:" NAME_PREFIX = "LCTier:name:" DESC_PREFIX = "LCTier:desc:" TEXT_FILE = "LonaCraft:Tiers:" NAME_FORMAT = "%s (%s)" # last-resort shape of Tiers:Name/format, item then tier CRAFTABLE = [] # base item_names, filled by generate_variants CRAFTABLE_SET = {} # base item_name => true VARIANTS = {} # base item_name => {tier => variant item_name} BASE_OF = {} # variant item_name => [base item_name, tier] STAT_INFO = {} # base or variant item_name => {stat => net value} for the tier line # Text key of a craftable base => [base item_name, :name or :desc]. Only keys # used by that one item, so no other item's text changes. BASE_KEYS = {} # Stats shown on the tier line, in the order of the game's own equipment # descriptions (ATK, DEF, then HP, STA, SPD; DataEquip.txt FootmanMid). # [stat, label key under Tiers.txt Bonus/, shown as a total (true) or signed] SHOWN_STATS = [ ["atk_plus", "atk", true], ["def_plus", "def", true], ["health", "health", false], ["sta", "sta", false], ["move_speed", "move_speed", false] ] # attribute types summed for the shown totals ("current"/"min" are not # equipped bonuses; "*" and "/" adjustors are skipped) SHOWN_TYPES = ["max", "Tmax", "BTmax"] #---------------------------------------------------------------------- # Public API #---------------------------------------------------------------------- # 1..5; 1 for anything that is not a variant (base items included) def self.tier_of(item_or_name) data = BASE_OF[name_of(item_or_name)] data ? data[1] : 1 end # The base item_name of a variant, or the name itself def self.base_of(item_or_name) name = name_of(item_or_name) data = BASE_OF[name] data ? data[0] : name end def self.variant?(item_or_name) BASE_OF.has_key?(name_of(item_or_name)) end # item_name of +base_name+ at +tier+. Tier 1 (and any base that has no # variants) returns base_name itself. def self.variant(base_name, tier) t = tier.to_i return base_name if t <= 1 tiers = VARIANTS[base_name] return base_name if tiers.nil? t = MAX_TIER if t > MAX_TIER tiers[t] || base_name end def self.craftable?(base_name) CRAFTABLE_SET.has_key?(base_name.to_s) end # Display name of the tier in the current language def self.tier_name(tier) t = clamp_tier(tier) key = "#{TEXT_FILE}Tier/name#{t}" res = $game_text ? $game_text[key] : nil return TIERS[t - 1][1] if res.nil? || res == key || res.to_s.empty? res end # Colour index for \C[n] def self.tier_color(tier) TIERS[clamp_tier(tier) - 1][2] end #---------------------------------------------------------------------- # Generation (called from LonaCraft.load_database, after all items exist) #---------------------------------------------------------------------- def self.generate_variants CRAFTABLE.clear CRAFTABLE_SET.clear VARIANTS.clear BASE_OF.clear STAT_INFO.clear BASE_KEYS.clear begin bases = [] [$data_weapons, $data_armors].each{|table| table.each{|item| next unless craftable_base_item?(item) next if CRAFTABLE_SET.has_key?(item.item_name) CRAFTABLE_SET[item.item_name] = true bases << item } } count = 0 bases.each{|base| made = false 2.upto(MAX_TIER){|tier| begin made = true if make_variant(base, tier) rescue => e p "LonaCraft Tiers: #{base.item_name} tier #{tier} failed #{e.message}" end } if made CRAFTABLE << base.item_name count += 1 else CRAFTABLE_SET.delete(base.item_name) end } p "LonaCraft Tiers: #{count} craftable items, #{BASE_OF.size} variants" p "LonaCraft Tiers craftable: #{CRAFTABLE.join(", ")}" build_base_keys(bases) rescue => e p "LonaCraft Tiers error #{e.message}" msgbox("#{LonaCraft::TextFallback.get("Info", "Err/build_tiers", "LonaCraft failed to build quality tiers.")}\n#{e.message}\n#{e.backtrace.join("\n")}") end CRAFTABLE end def self.craftable_base_item?(item) return false if item.nil? return false unless item.is_a?(RPG::Weapon) || item.is_a?(RPG::Armor) name = item.item_name.to_s return false if name.empty? return false if name =~ VARIANT_RE return false unless $data_ItemName[name].equal?(item) # skip duplicate / shadowed entries return false if name =~ EXCLUDE_NAME return false if defined?(LonaCraft::WEAPON_NAMES) && LonaCraft::WEAPON_NAMES.include?(name) return false if item.key_item? return false if EXCLUDE_TAGS.include?(item.type_tag.to_s) || EXCLUDE_TAGS.include?(item.type.to_s) return false if item.price.to_i <= 0 pot = item.pot_data return false unless pot.is_a?(Hash) return false unless CRAFT_TAGS.any?{|tag| pot[tag].to_f > 0 } return false unless item.instance_variable_get(:@item_data).is_a?(Hash) true end def self.make_variant(base, tier) name = "#{base.item_name}#{VARIANT_SUFFIX}#{tier}" return true if BASE_OF.has_key?(name) table = base.is_a?(RPG::Weapon) ? $data_weapons : $data_armors obj = clone_base(base) return false if obj.nil? data = obj.instance_variable_get(:@item_data) return false unless data.is_a?(Hash) old_changes = data["attribute_change"].is_a?(Array) ? data["attribute_change"] : [] primary = primary_stat(base, old_changes) STAT_INFO[base.item_name] ||= stat_totals(old_changes) # before scaling = the base's own values changes = [] old_changes.each{|eff| next unless eff.is_a?(Hash) if eff["attribute_name"] == primary && eff["adjustor"] == "+" && SCALED_TYPES.include?(eff["attribute_type"]) eff["adjustment"] = (eff["adjustment"].to_f * STAT_MULT[tier - 1]).round(2) end changes << eff } BONUSES[tier].each{|stat, type, amount| stat = primary if stat == :primary changes << {"attribute_name" => stat, "attribute_type" => type, "adjustor" => "+", "adjustment" => amount} } info = stat_totals(changes) data["item_name"] = name data["price"] = (base.price.to_f * PRICE_MULT[tier - 1]).round data["weight"] = base.weight.to_f data["icon_index"] = base.icon_index data["attribute_change"] = changes flags = data["textFlag"].is_a?(Hash) ? data["textFlag"] : {} flags["name"] = "#{NAME_PREFIX}#{name}" flags["description"] = "#{DESC_PREFIX}#{name}" data["textFlag"] = flags obj.id = table.length begin obj.load_lona_effect_from_hash(data) # registers $data_ItemName[name], sets price/texts/effects rescue => e $data_ItemName.delete(name) if $data_ItemName[name].equal?(obj) raise e end table << obj # appended only once fully built, so a failure leaves no half item obj.instance_variable_set(:@item_data, data) # RolePlayS's load_lona_effect_from_hash alias resets @optional from textFlag; # keep the base's optional text (requirements line) if the variant lost it. if $imported && $imported[:RolePlayS] && obj.instance_variable_get(:@optional).nil? obj.instance_variable_set(:@optional, base.instance_variable_get(:@optional)) end build_portrait(obj) register_drop_event(base.item_name, name) VARIANTS[base.item_name] ||= {} VARIANTS[base.item_name][tier] = name BASE_OF[name] = [base.item_name, tier] STAT_INFO[name] = info true end # Deep copy of the base; falls back to a fresh object of the same class # with a copy of the base's merged JSON data. def self.clone_base(base) obj = nil begin obj = Marshal.load(Marshal.dump(base)) rescue => e p "LonaCraft Tiers: deep copy of #{base.item_name} failed (#{e.message}), rebuilding it" obj = nil end if obj.nil? src = base.instance_variable_get(:@item_data) return nil unless src.is_a?(Hash) obj = base.class.new obj.note = base.note.to_s obj.load_notetags_aee obj.instance_variable_set(:@item_data, Marshal.load(Marshal.dump(src))) end obj.configured = true # lona_effect getter returns [] for unconfigured items (ItemConfig.rb:353-360) obj.instance_variable_set(:@lonaStat, nil) obj end # Portrait/CHS layer, built the way init_lonaStat_weapons/armors does # (25_Game_Actor.rb:205-224): json hash first, else the note. def self.build_portrait(obj) stat_hash = obj.lonaStat_json_hash if stat_hash.nil? note = obj.note.to_s return if note.empty? stat_hash = Note.get_data(note) end return unless stat_hash.is_a?(Hash) obj.createLonaState(stat_hash) rescue => e p "LonaCraft Tiers: portrait layer for #{obj.item_name} failed #{e.message}" end # Items dropped on the ground become EventLib events named after their # item_name (Game_Temp#call_item_drop -> Game_Map#ask_summon_event, which # shows "event not found" and loses the item if there is none, # 31_Game_Map.rb:1207-1209). Clone the base item's drop event and point # its common_pickup_item("") at the variant, as 0_Core.rb does for # the materials. def self.register_drop_event(base_name, variant_name) return if $data_EventLib.nil? || $data_EventLib[variant_name] src = $data_EventLib[base_name] return if src.nil? ev = Marshal.load(Marshal.dump(src[1])) ev.name = variant_name replaced = false ev.pages.each{|page| page.list.each{|cmd| cmd.parameters.each_with_index{|prm, i| next unless prm.is_a?(String) next unless prm.include?("common_pickup_item") fixed = prm.gsub("\"#{base_name}\"", "\"#{variant_name}\"").gsub("'#{base_name}'", "'#{variant_name}'") next if fixed == prm cmd.parameters[i] = fixed replaced = true } } } if replaced $data_EventLib[variant_name] = [src[0], ev] else p "LonaCraft Tiers: could not retarget the drop event for #{variant_name}" end rescue => e p "LonaCraft Tiers: drop event for #{variant_name} failed #{e.message}" end # atk_plus for weapons, def_plus for armour; the other one if the item # only carries that (e.g. a shield in the weapon table with def_plus only). def self.primary_stat(item, changes) first, second = item.is_a?(RPG::Weapon) ? ["atk_plus", "def_plus"] : ["def_plus", "atk_plus"] return first if primary_total(changes, first) > 0 return second if primary_total(changes, second) > 0 first end def self.primary_total(changes, stat) total = 0.0 changes.each{|eff| next unless eff.is_a?(Hash) next unless eff["attribute_name"] == stat && eff["adjustor"] == "+" next unless SCALED_TYPES.include?(eff["attribute_type"]) total += eff["adjustment"].to_f } total end # Net value per shown stat: sum of "+" minus "-" entries of the types in # SHOWN_TYPES ({"atk_plus" => 2.4, "sta" => 5, ...}). def self.stat_totals(changes) out = {} SHOWN_STATS.each{|stat, label, total| out[stat] = 0.0 } changes.each{|eff| next unless eff.is_a?(Hash) stat = eff["attribute_name"] next unless out.has_key?(stat) next unless SHOWN_TYPES.include?(eff["attribute_type"]) case eff["adjustor"] when "+" then out[stat] += eff["adjustment"].to_f when "-" then out[stat] -= eff["adjustment"].to_f end } out.keys.each{|stat| out[stat] = out[stat].round(2) } out end # Name/description keys of the craftable bases (tier 1, Common). A key is # taken only if no other item (weapon, armour or item) uses it, so the # "(Common)" text never reaches another item. Only $game_text[key] changes; # item_name, the key itself and every lookup by item_name stay as they are. def self.build_base_keys(bases) users = {} [$data_items, $data_weapons, $data_armors].each{|table| table.each{|item| next if item.nil? next if BASE_OF.has_key?(item.item_name.to_s) # variants have their own keys [item.name, item.description].each{|key| next unless key.is_a?(String) && !key.empty? users[key] ||= {} users[key][item.item_name.to_s] = true } } } bases.each{|base| next unless CRAFTABLE_SET.has_key?(base.item_name) [[base.name, :name], [base.description, :desc]].each{|key, kind| next unless key.is_a?(String) && !key.empty? next if key.start_with?(TEXT_PREFIX) next unless users[key] && users[key].size == 1 BASE_KEYS[key] = [base.item_name, kind] } } end #---------------------------------------------------------------------- # Text (called from the Text#[] alias below) #---------------------------------------------------------------------- def self.text_for(key) if key.start_with?(NAME_PREFIX) return variant_name_text(key[NAME_PREFIX.length..-1]) elsif key.start_with?(DESC_PREFIX) return variant_desc_text(key[DESC_PREFIX.length..-1]) end nil rescue => e p "LonaCraft Tiers text error #{key} #{e.message}" nil end # +raw+ = the game's own text for a craftable base's key def self.base_text(key, raw) data = BASE_KEYS[key] return nil if data.nil? if data[1] == :name name_with_tier(raw, 1) else with_tier_line(raw, data[0], 1) end rescue => e p "LonaCraft Tiers text error #{key} #{e.message}" nil end # The game's text without the tier additions def self.raw_text(key) return "" if $game_text.nil? if $game_text.respond_to?(:lonacraft_tier_text_orig) $game_text.lonacraft_tier_text_orig(key) else $game_text[key] end end # Displayed name of an item in a tier: the text Tiers:Name/format with the # first %s replaced by the item name and the second by the tier name # (English "%s (%s)"), so a language can reorder or reword it. Every # craftable shows its tier, Common included. Falls back to the English shape # when the key is missing or does not hold exactly two %s. def self.name_with_tier(item_name, tier) fmt = $game_text ? text("Name/format").to_s : "" fmt = NAME_FORMAT if fmt.empty? || fmt == "#{TEXT_FILE}Name/format" || fmt.scan("%s").size != 2 n = 0 fmt.gsub("%s"){ (n += 1) == 1 ? item_name.to_s : tier_name(tier).to_s } end def self.variant_name_text(variant) data = BASE_OF[variant] return nil if data.nil? base = $data_ItemName[data[0]] return nil if base.nil? name_with_tier(raw_text(base.name), data[1]) end def self.variant_desc_text(variant) data = BASE_OF[variant] return nil if data.nil? base = $data_ItemName[data[0]] return nil if base.nil? with_tier_line(raw_text(base.description), variant, data[1]) end # Description + one line in the style of the game's equipment texts: # "\C[tier colour]Rare \C[2]ATK 2.4, HP +5, STA +5\n", increases green (2), # decreases yellow (6) as in DataEquip.txt ("\C[2]Increases DEF, ATK. # \C[6]Decreases Weak,Sexy."). ATK/DEF are the item's full values; HP, STA, # SPD are the item's net bonuses. Stats at 0 are left out. def self.with_tier_line(desc, item_name, tier) desc = desc.to_s desc += "\\n" unless desc.empty? || desc.end_with?("\\n") line = "\\C[#{tier_color(tier)}]#{text("Desc/header").gsub("%s", tier_name(tier))}" totals = STAT_INFO[item_name] || {} parts = [] SHOWN_STATS.each{|stat, label, total| value = totals[stat].to_f next if value == 0 color = value > 0 ? 2 : 6 shown = total ? format_number(value) : signed_number(value) parts << "\\C[#{color}]#{text("Bonus/#{label}")} #{shown}" } line += " " + parts.join(", ") unless parts.empty? desc + line + "\\n" end def self.signed_number(value) value.to_f > 0 ? "+#{format_number(value)}" : format_number(value) end def self.text(key) full = "#{TEXT_FILE}#{key}" res = $game_text[full] res.nil? ? full : res.to_s end def self.format_number(value) v = value.to_f v == v.round ? v.round.to_s : v.round(2).to_s end def self.clamp_tier(tier) t = tier.to_i t = 1 if t < 1 t = MAX_TIER if t > MAX_TIER t end def self.name_of(item_or_name) return item_or_name if item_or_name.is_a?(String) return "" if item_or_name.nil? return item_or_name.item_name.to_s if item_or_name.respond_to?(:item_name) item_or_name.to_s end end end class Text unless method_defined?(:lonacraft_tier_text_orig) alias_method :lonacraft_tier_text_orig, :[] # The tier texts look up other texts ($game_text[...] for tier names and # labels). Those inner lookups never need a tier answer, so while one is # being built (Tiers @text_busy) every key goes straight to the inner # chain (15_Text.rb fallback, then the game): no path can recurse. def [](key, noError = false) if key.is_a?(String) && !LonaCraft::Tiers.instance_variable_get(:@text_busy) if key.start_with?(LonaCraft::Tiers::TEXT_PREFIX) res = nil begin LonaCraft::Tiers.instance_variable_set(:@text_busy, true) res = LonaCraft::Tiers.text_for(key) ensure LonaCraft::Tiers.instance_variable_set(:@text_busy, false) end return res if res elsif LonaCraft::Tiers::BASE_KEYS.has_key?(key) raw = lonacraft_tier_text_orig(key, noError) res = nil begin LonaCraft::Tiers.instance_variable_set(:@text_busy, true) res = LonaCraft::Tiers.base_text(key, raw) ensure LonaCraft::Tiers.instance_variable_set(:@text_busy, false) end return res ? res : raw end end lonacraft_tier_text_orig(key, noError) end end end