| 90 | |
| 91 | |
| 92 | DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) |
| 93 | { |
| 94 | if (!is_enabled) |
| 95 | return CR_OK; |
| 96 | |
| 97 | if (event != DFHack::SC_WORLD_LOADED) |
| 98 | return CR_OK; |
| 99 | |
| 100 | std::vector<int> descriptorCount = std::vector<int>(descriptors.size()); |
| 101 | |
| 102 | auto version = World::GetPersistentData("AlreadyRenamedCreatures"); |
| 103 | if (version.isValid() && version.ival(1) >= RENAMER_VERSION) |
| 104 | { |
| 105 | return CR_OK; |
| 106 | } |
| 107 | |
| 108 | int creatureCount = 0; |
| 109 | |
| 110 | for (size_t i = 0; i < world->raws.creatures.all.size(); i++) |
| 111 | { |
| 112 | auto creatureRaw = world->raws.creatures.all[i]; |
| 113 | if (!creatureRaw->flags.is_set(df::enums::creature_raw_flags::GENERATED)) |
| 114 | continue; |
| 115 | size_t minPos = std::string::npos; |
| 116 | size_t foundIndex = -1; |
| 117 | size_t prefixIndex = -1; |
| 118 | |
| 119 | for (size_t j = 0; j < prefixes.size(); j++) |
| 120 | { |
| 121 | if (creatureRaw->creature_id.compare(0, prefixes[j].length(), prefixes[j]) == 0) |
| 122 | { |
| 123 | prefixIndex = j; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (prefixIndex < 0) |
| 128 | continue; //unrecognized generaed type. |
| 129 | |
| 130 | for (size_t j = 0; j < descriptors.size(); j++) |
| 131 | { |
| 132 | size_t pos = creatureRaw->caste[0]->description.find(" " + descriptors[j]); |
| 133 | if (pos < minPos) |
| 134 | { |
| 135 | minPos = pos; |
| 136 | foundIndex = j; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if (foundIndex < 0) |
| 141 | continue; //can't find a match. |
| 142 | |
| 143 | auto descriptor = descriptors[foundIndex]; |
| 144 | |
| 145 | for (size_t j = 0; j < descriptor.size(); j++) |
| 146 | { |
| 147 | if (descriptor[j] == ' ') |
| 148 | descriptor[j] = '_'; |
| 149 | else |