From 873feaee2b04e1d27a48253b5ee616ced0521a82 Mon Sep 17 00:00:00 2001 From: Japa Illo Date: Tue, 7 Feb 2017 15:57:35 +0530 Subject: [PATCH] Added a function to the creature renamer to save a graphics pack file to set graphics for all the generated creatures. --- plugins/generated-creature-renamer.cpp | 177 +++++++++++++++++-------- 1 file changed, 121 insertions(+), 56 deletions(-) diff --git a/plugins/generated-creature-renamer.cpp b/plugins/generated-creature-renamer.cpp index e6cf8723f..5eb030d44 100644 --- a/plugins/generated-creature-renamer.cpp +++ b/plugins/generated-creature-renamer.cpp @@ -8,6 +8,7 @@ #include "df/creature_raw.h" #include "df/caste_raw.h" #include "modules/World.h" +#include "MemAccess.h" //#include "df/world.h" @@ -25,17 +26,17 @@ DFhackCExport command_result plugin_init(color_ostream &out, std::vector descriptorCount = std::vector(descriptors.size()); + + auto version = World::GetPersistentData("AlreadyRenamedCreatures"); + if (version.isValid() && version.ival(1) >= RENAMER_VERSION) { - case DFHack::SC_WORLD_LOADED: - CoreSuspender suspend; + return CR_OK; + } - std::vector descriptorCount = std::vector(descriptors.size()); + int creatureCount = 0; - auto version = World::GetPersistentData("AlreadyRenamedCreatures"); - if (version.isValid() && version.ival(1) >= RENAMER_VERSION) - { - return CR_OK; - } + for (int i = 0; i < world->raws.creatures.all.size(); i++) + { + auto creatureRaw = world->raws.creatures.all[i]; + if (!creatureRaw->flags.is_set(df::enums::creature_raw_flags::GENERATED)) + continue; + size_t minPos = std::string::npos; + size_t foundIndex = -1; + size_t prefixIndex = -1; - for (int i = 0; i < world->raws.creatures.all.size(); i++) + for (rsize_t j = 0; j < prefixes.size(); j++) { - auto creatureRaw = world->raws.creatures.all[i]; - if (!creatureRaw->flags.is_set(df::enums::creature_raw_flags::GENERATED)) - continue; - size_t minPos = 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) { - if (creatureRaw->creature_id.compare(0, prefixes[j].length(), prefixes[j]) == 0) - { - prefixIndex = j; - } + prefixIndex = j; } + } - if (prefixIndex < 0) - continue; //unrecognized generaed type. + if (prefixIndex < 0) + continue; //unrecognized generaed type. - for (size_t j = 0; j < descriptors.size(); j++) + for (size_t j = 0; j < descriptors.size(); j++) + { + size_t pos = creatureRaw->caste[0]->description.find(" " + descriptors[j]); + if (pos < minPos) { - size_t pos = creatureRaw->caste[0]->description.find(" " + descriptors[j]); - if (pos < minPos) - { - minPos = pos; - foundIndex = j; - } + minPos = pos; + foundIndex = j; } + } - if (foundIndex < 0) - continue; //can't find a match. + if (foundIndex < 0) + continue; //can't find a match. - auto descriptor = descriptors[foundIndex]; + auto descriptor = descriptors[foundIndex]; - for (int j = 0; j < descriptor.size(); j++) - { - if (descriptor[j] == ' ') - descriptor[j] = '_'; - else - descriptor[j] = toupper(descriptor[j]); - } + for (int j = 0; j < descriptor.size(); j++) + { + if (descriptor[j] == ' ') + descriptor[j] = '_'; + else + descriptor[j] = toupper(descriptor[j]); + } - auto prefix = prefixes[prefixIndex]; - if (prefix[prefix.length() - 1] != '_') - prefix.append("_"); + auto prefix = prefixes[prefixIndex]; + if (prefix[prefix.length() - 1] != '_') + prefix.append("_"); - creatureRaw->creature_id = prefixes[prefixIndex] + descriptor; + creatureRaw->creature_id = prefixes[prefixIndex] + descriptor; - if (descriptorCount[foundIndex] > 0) - { - creatureRaw->creature_id.append("_" + std::to_string(descriptorCount[foundIndex])); - } - descriptorCount[foundIndex]++; + if (descriptorCount[foundIndex] > 0) + { + creatureRaw->creature_id.append("_" + std::to_string(descriptorCount[foundIndex])); } - version = World::AddPersistentData("AlreadyRenamedCreatures"); - version.ival(1) = RENAMER_VERSION; - break; + descriptorCount[foundIndex]++; + creatureCount++; } + version = World::AddPersistentData("AlreadyRenamedCreatures"); + version.ival(1) = RENAMER_VERSION; + + out << "Renamed " << creatureCount << " generated creatures to have sensible names." << endl; + + return CR_OK; } @@ -200,10 +207,68 @@ command_result list_creatures(color_ostream &out, std::vector & pa } out.print("\n"); } + return CR_OK; } command_result save_generated_raw(color_ostream &out, std::vector & parameters) { +#ifdef LINUX_BUILD + std::string pathSep = "/"; +#else + std::string pathSep = "\\"; +#endif + int pageWidth = 16; + int pageHeight = (descriptors.size() / pageWidth) + ((descriptors.size() % pageWidth > 0) ? 1 : 0); + int tileWidth = 24; + int tileHeight = 24; + std::string fileName = "graphics_procedural_creatures"; + std::string pageName = "PROCEDURAL_FRIENDLY"; + int repeats = 128; + + std::ofstream outputFile(fileName + ".txt", std::ios::out | std::ios::trunc); + + outputFile << fileName << endl << endl; + + outputFile << "[OBJECT:GRAPHICS]" << endl << endl; + + outputFile << "[TILE_PAGE:" << pageName << "]" << endl; + outputFile << " [FILE:procedural_friendly.png]" << endl; + outputFile << " [TILE_DIM:" << tileWidth << ":" << tileHeight << "]" << endl; + outputFile << " [PAGE_DIM:" << pageWidth << ":" << pageHeight << "]" << endl << endl; + + for (size_t descIndex = 0; descIndex < descriptors.size(); descIndex++) + { + for (size_t prefIndex = 0; prefIndex < prefixes.size(); prefIndex++) + { + for (size_t rep = 0; rep < repeats; rep++) + { + auto descriptor = descriptors[descIndex]; + + for (int j = 0; j < descriptor.size(); j++) + { + if (descriptor[j] == ' ') + descriptor[j] = '_'; + else + descriptor[j] = toupper(descriptor[j]); + } + + auto prefix = prefixes[prefIndex]; + if (prefix[prefix.length() - 1] != '_') + prefix.append("_"); + + std::string token = prefix + descriptor; + if (rep > 0) + token.append("_" + std::to_string(rep)); + + outputFile << "[CREATURE_GRAPHICS:" << token << "]" << endl; + outputFile << " [DEFAULT:" << pageName << ":" << descIndex % pageWidth << ":" << descIndex / pageWidth << ":ADD_COLOR]" << endl; + } + outputFile << endl; + } + outputFile << endl; + } + + outputFile.close(); return CR_OK; }