From 1c5b0e46675ebc6f1110b24ac61b9c2a80a1dfce Mon Sep 17 00:00:00 2001 From: Martin Michelsen Date: Sat, 4 Jan 2025 19:00:42 -0800 Subject: [PATCH] make name-all-items more useful --- src/ItemNameIndex.hh | 3 +++ src/ItemParameterTable.cc | 32 ++++++++++++++++++++++++++++++++ src/ItemParameterTable.hh | 11 ++++++++--- src/Main.cc | 36 ++++++++++++++++++++++-------------- 4 files changed, 65 insertions(+), 17 deletions(-) diff --git a/src/ItemNameIndex.hh b/src/ItemNameIndex.hh index e424cf56..bf311cbb 100644 --- a/src/ItemNameIndex.hh +++ b/src/ItemNameIndex.hh @@ -35,6 +35,9 @@ public: return this->name_index; } + inline bool exists(const ItemData& item) const { + return this->primary_identifier_index.count(item.primary_identifier()); + } std::string describe_item(const ItemData& item, bool include_color_escapes = false) const; ItemData parse_item_description(const std::string& description) const; diff --git a/src/ItemParameterTable.cc b/src/ItemParameterTable.cc index 43d16e9d..25b1ea5e 100644 --- a/src/ItemParameterTable.cc +++ b/src/ItemParameterTable.cc @@ -739,6 +739,38 @@ pair ItemParameterTable::find_tool_by_id(uint32_t item_id) con } } +variant< + const ItemParameterTable::WeaponV4*, + const ItemParameterTable::ArmorOrShieldV4*, + const ItemParameterTable::UnitV4*, + const ItemParameterTable::MagV4*, + const ItemParameterTable::ToolV4*> +ItemParameterTable::definition_for_primary_identifier(uint32_t primary_identifier) const { + uint8_t data1_0 = (primary_identifier >> 24) & 0xFF; + uint8_t data1_1 = (primary_identifier >> 16) & 0xFF; + uint8_t data1_2 = (primary_identifier >> 8) & 0xFF; + switch (data1_0) { + case 0: + return &this->get_weapon(data1_1, data1_2); + case 1: + switch (data1_1) { + case 1: + case 2: + return &this->get_armor_or_shield(data1_1, data1_2); + case 3: + return &this->get_unit(data1_2); + default: + throw runtime_error("invalid primary identifier"); + } + case 2: + return &this->get_mag(data1_1); + case 3: + return &this->get_tool(data1_1, data1_2); + default: + throw runtime_error("invalid primary identifier"); + } +} + template float ItemParameterTable::get_sale_divisor_t(const OffsetsT* offsets, uint8_t data1_0, uint8_t data1_1) const { switch (data1_0) { diff --git a/src/ItemParameterTable.hh b/src/ItemParameterTable.hh index d273dad5..4c312868 100644 --- a/src/ItemParameterTable.hh +++ b/src/ItemParameterTable.hh @@ -2,16 +2,19 @@ #include +#include #include #include #include #include #include +#include #include #include "ItemData.hh" #include "Text.hh" #include "Types.hh" +#include "Version.hh" class ItemParameterTable { public: @@ -438,7 +441,6 @@ public: ItemParameterTable(std::shared_ptr data, Version version); ~ItemParameterTable() = default; - void print(FILE* stream) const; std::set compute_all_valid_primary_identifiers() const; size_t num_weapons_in_class(uint8_t data1_1) const; @@ -453,6 +455,9 @@ public: const ToolV4& get_tool(uint8_t data1_1, uint8_t data1_2) const; std::pair find_tool_by_id(uint32_t id) const; + std::variant + definition_for_primary_identifier(uint32_t primary_identifier) const; + float get_sale_divisor(uint8_t data1_0, uint8_t data1_1) const; const MagFeedResult& get_mag_feed_result(uint8_t table_index, uint8_t which) const; uint8_t get_item_stars(uint32_t id) const; @@ -484,7 +489,7 @@ public: size_t num_specials; size_t first_rare_mag_index; -private: +protected: struct TableOffsetsDCProtos { /* ## / NTE / 11/2000 */ /* 00 / 0013 / 0013 */ le_uint32_t unknown_a0; @@ -642,7 +647,7 @@ public: uint8_t get_evolution_number(uint8_t data1_1) const; -private: +protected: std::shared_ptr data; phosg::StringReader r; const TableOffsets* offsets; diff --git a/src/Main.cc b/src/Main.cc index 21af6826..334d6d56 100644 --- a/src/Main.cc +++ b/src/Main.cc @@ -2092,7 +2092,7 @@ Action a_name_all_items( if (args.get("list")) { for (uint32_t primary_identifier : all_primary_identifiers) { - fprintf(stderr, "%08" PRIX32 "\n", primary_identifier); + fprintf(stdout, "%08" PRIX32 "\n", primary_identifier); for (Version v : ALL_VERSIONS) { const auto& index = s->item_name_index_opt(v); if (index) { @@ -2101,41 +2101,49 @@ Action a_name_all_items( string name = index->describe_item(item); try { bool is_rare = pmt->is_item_rare(item); - fprintf(stderr, " %10s: %s %s\n", phosg::name_for_enum(v), is_rare ? "+++" : "---", name.c_str()); + fprintf(stdout, " %10s: %s %s\n", phosg::name_for_enum(v), is_rare ? "+++" : "---", name.c_str()); } catch (const out_of_range&) { - fprintf(stderr, " %10s: (missing)\n", phosg::name_for_enum(v)); + fprintf(stdout, " %10s: (missing)\n", phosg::name_for_enum(v)); } } } - fputc('\n', stderr); + fputc('\n', stdout); } } else { - fprintf(stderr, "IDENT :"); + bool separate_classes = args.get("separate-classes"); + + fprintf(stdout, "IDENT :"); for (Version v : ALL_VERSIONS) { const auto& index = s->item_name_index_opt(v); if (index) { - fprintf(stderr, " %30s ", phosg::name_for_enum(v)); + fprintf(stdout, " %30s ", phosg::name_for_enum(v)); } } - fputc('\n', stderr); + fputc('\n', stdout); + uint32_t prev_ident = 0; for (uint32_t primary_identifier : all_primary_identifiers) { - fprintf(stderr, "%08" PRIX32 ":", primary_identifier); + if (separate_classes & ((primary_identifier & 0xFFFF0000) != (prev_ident & 0xFFFF0000))) { + fputc('\n', stdout); + } + prev_ident = primary_identifier; + + fprintf(stdout, "%08" PRIX32 ":", primary_identifier); for (Version v : ALL_VERSIONS) { const auto& index = s->item_name_index_opt(v); if (index) { auto pmt = s->item_parameter_table(v); ItemData item = ItemData::from_primary_identifier(*s->item_stack_limits(v), primary_identifier); - string name = index->describe_item(item); - try { + if (index->exists(item)) { + string name = index->describe_item(item); bool is_rare = pmt->is_item_rare(item); - fprintf(stderr, " %30s%s", name.c_str(), is_rare ? " (*)" : " "); - } catch (const out_of_range&) { - fprintf(stderr, " "); + fprintf(stdout, " %30s%s", name.c_str(), is_rare ? " ***" : " ..."); + } else { + fprintf(stdout, " ------------------------------ ---"); } } } - fputc('\n', stderr); + fputc('\n', stdout); } } });