From f9b296884c728ea416a799505e4d30d1e7ee2ce1 Mon Sep 17 00:00:00 2001 From: Japa Illo Date: Tue, 7 Feb 2017 11:01:42 +0530 Subject: [PATCH] Made the creature renamer work more than once on the same save, and changed the format of the resulting names. --- plugins/generated-creature-renamer.cpp | 51 +++++++++++++++++++------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/plugins/generated-creature-renamer.cpp b/plugins/generated-creature-renamer.cpp index 1d0b98f58..9463773b2 100644 --- a/plugins/generated-creature-renamer.cpp +++ b/plugins/generated-creature-renamer.cpp @@ -16,7 +16,7 @@ using namespace DFHack; DFHACK_PLUGIN("generated-creature-renamer"); REQUIRE_GLOBAL(world); -#define RENAMER_VERSION 1 +#define RENAMER_VERSION 2 command_result list_creatures(color_ostream &out, std::vector & parameters); @@ -78,6 +78,14 @@ std::vector descriptors = { "cat", "ass", "elk" }; +std::vector prefixes = { + "FORGOTTEN_BEAST_", + "TITAN_", + "DEMON_", + "NIGHT_CREATURE_", + "HF" +}; + DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) { @@ -102,7 +110,19 @@ DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_chan if (!creatureRaw->flags.is_set(df::enums::creature_raw_flags::GENERATED)) continue; size_t minPos = std::string::npos; - size_t foundIndex = std::string::npos; + size_t foundIndex = -1; + size_t prefixIndex = -1; + + for (rsize_t j = 0; j < prefixes.size(); j++) + { + if (creatureRaw->creature_id.compare(0, prefixes[j].length(), prefixes[j]) == 0) + { + prefixIndex = j; + } + } + + if (prefixIndex < 0) + continue; //unrecognized generaed type. for (size_t j = 0; j < descriptors.size(); j++) { @@ -114,21 +134,26 @@ DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_chan } } - if (foundIndex < std::string::npos) - { - size_t digitPos = creatureRaw->creature_id.find_first_of("0123456789"); - if (digitPos > creatureRaw->creature_id.length()) - digitPos = creatureRaw->creature_id.length(); + if (foundIndex < 0) + continue; //can't find a match. - creatureRaw->creature_id.replace(digitPos, std::string::npos, descriptors[foundIndex]); + auto descriptor = descriptors[foundIndex]; - if (descriptorCount[foundIndex] > 0) - { - creatureRaw->creature_id.append(std::to_string(descriptorCount[foundIndex])); - } + for (int j = 0; j < descriptor.size(); j++) + { + if (descriptor[j] == ' ') + descriptor[j] = '_'; + else + descriptor[j] = toupper(descriptor[j]); + } + + creatureRaw->creature_id = prefixes[prefixIndex] + descriptor; - descriptorCount[foundIndex]++; + if (descriptorCount[foundIndex] > 0) + { + creatureRaw->creature_id.append("_" + std::to_string(descriptorCount[foundIndex])); } + descriptorCount[foundIndex]++; } version = World::AddPersistentData("AlreadyRenamedCreatures"); version.ival(1) = RENAMER_VERSION;