|
68 | 68 | #include "baseui.h"
|
69 | 69 | #include "algo.h"
|
70 | 70 | #include "rand.h"
|
71 |
| -#include <variant> |
72 | 71 |
|
73 | 72 | enum BranchSubcommand {
|
74 | 73 | eOptionBranchElse = 1
|
@@ -5110,47 +5109,148 @@ bool Game_Interpreter::CommandManiacCallCommand(lcf::rpg::EventCommand const& co
|
5110 | 5109 |
|
5111 | 5110 | return true;
|
5112 | 5111 | }
|
5113 |
| -bool Game_Interpreter::CommandGetVarByName(lcf::rpg::EventCommand const& com) { |
5114 |
| - auto& targetData = lcf::Data::variables; |
| 5112 | +bool Game_Interpreter::CommandGetIdByName(lcf::rpg::EventCommand const& com) { |
| 5113 | + const int dataType = ValueOrVariable(com.parameters[0], com.parameters[1]); |
5115 | 5114 |
|
5116 |
| - int stringVarParam = 6; // string var is detected at com.parameters[n] |
5117 |
| - const std::string targetName = ToString(CommandStringOrVariable(com, stringVarParam, stringVarParam + 1)); |
5118 |
| - if (targetName.empty()) { |
5119 |
| - Output::Warning("The variable name to search is empty."); |
5120 |
| - return true; |
5121 |
| - } |
5122 |
| - |
5123 |
| - const int outputVar = ValueOrVariable(com.parameters[0], com.parameters[1]); |
| 5115 | + const int outputVar = ValueOrVariable(com.parameters[2], com.parameters[3]); |
5124 | 5116 | if (outputVar <= 0) {
|
5125 | 5117 | Output::Warning("This command requires a valid variable.");
|
5126 | 5118 | return true;
|
5127 | 5119 | }
|
5128 | 5120 |
|
5129 |
| - int rangeMin = ValueOrVariable(com.parameters[2], com.parameters[3]); |
5130 |
| - int rangeMax = ValueOrVariable(com.parameters[4], com.parameters[5]); |
| 5121 | + int rangeMin = ValueOrVariable(com.parameters[4], com.parameters[5]); |
| 5122 | + int rangeMax = ValueOrVariable(com.parameters[6], com.parameters[7]); |
5131 | 5123 |
|
5132 |
| - if (rangeMin <= 0) rangeMin = 1; |
5133 |
| - if (rangeMin > static_cast<int>(targetData.size())) rangeMin = static_cast<int>(targetData.size()); |
5134 |
| - if (rangeMax <= 0 || rangeMax >= static_cast<int>(targetData.size())) rangeMax = static_cast<int>(targetData.size()); |
| 5124 | + const std::string targetName = ToString(CommandStringOrVariable(com, 8, 9)); |
| 5125 | + if (targetName.empty()) { |
| 5126 | + Output::Warning("The name to search is empty."); |
| 5127 | + return true; |
| 5128 | + } |
5135 | 5129 |
|
5136 |
| - if (rangeMax < rangeMin) std::swap(rangeMin, rangeMax); |
| 5130 | + auto GetName = [](const auto& obj) -> std::string { |
| 5131 | + using T = std::decay_t<decltype(obj)>; |
5137 | 5132 |
|
5138 |
| - auto it = std::find_if(targetData.begin() + rangeMin - 1, targetData.begin() + rangeMax, |
5139 |
| - [&](const auto& entry) { return entry.name == targetName; }); |
| 5133 | + if constexpr (std::is_same_v<T, Game_Event>) { |
| 5134 | + return ToString(obj.GetName()); |
| 5135 | + } |
| 5136 | + else if constexpr (std::is_same_v<T, Game_Actor*>) { |
| 5137 | + return ToString(obj->GetName()); |
| 5138 | + } |
| 5139 | + else { |
| 5140 | + return ToString(obj.name); |
| 5141 | + } |
| 5142 | + }; |
5140 | 5143 |
|
5141 |
| - if (it != targetData.end()) { |
5142 |
| - const int id = std::distance(targetData.begin(), it) + 1; |
| 5144 | + auto find_by_name = [&](const auto& vec) -> int { |
| 5145 | + if (rangeMin <= 0) rangeMin = 1; |
| 5146 | + if (rangeMin > static_cast<int>(vec.size())) rangeMin = static_cast<int>(vec.size()); |
5143 | 5147 |
|
5144 |
| - if (id >= rangeMin && id <= rangeMax) { |
5145 |
| - Output::Debug(" Variable \"{}\" found at ID: {}. Storing ID in variable {}.", targetName, id, outputVar); |
| 5148 | + if (rangeMax <= 0 || rangeMax >= static_cast<int>(vec.size())) rangeMax = static_cast<int>(vec.size()); |
| 5149 | + if (rangeMax < rangeMin) std::swap(rangeMin, rangeMax); |
5146 | 5150 |
|
5147 |
| - Main_Data::game_variables->Set(outputVar, id); |
5148 |
| - Game_Map::SetNeedRefresh(true); |
5149 |
| - return true; |
| 5151 | + for (int i = rangeMin - 1; i < rangeMax; ++i) { |
| 5152 | + if (GetName(vec[i]) == targetName) { |
| 5153 | + return i + 1; |
| 5154 | + } |
5150 | 5155 | }
|
| 5156 | + return 0; |
| 5157 | + }; |
| 5158 | + |
| 5159 | + std::string typeName = "Unknown"; |
| 5160 | + int id = 0; |
| 5161 | + switch (dataType) { |
| 5162 | + case 0: |
| 5163 | + typeName = "Actor"; |
| 5164 | + id = find_by_name(lcf::Data::actors); |
| 5165 | + break; |
| 5166 | + case 1: |
| 5167 | + typeName = "Skill"; |
| 5168 | + id = find_by_name(lcf::Data::skills); |
| 5169 | + break; |
| 5170 | + case 2: |
| 5171 | + typeName = "Item"; |
| 5172 | + id = find_by_name(lcf::Data::items); |
| 5173 | + break; |
| 5174 | + case 3: |
| 5175 | + typeName = "Enemy"; |
| 5176 | + id = find_by_name(lcf::Data::enemies); |
| 5177 | + break; |
| 5178 | + case 4: |
| 5179 | + typeName = "Troop"; |
| 5180 | + id = find_by_name(lcf::Data::troops); |
| 5181 | + break; |
| 5182 | + case 5: |
| 5183 | + typeName = "Terrain"; |
| 5184 | + id = find_by_name(lcf::Data::terrains); |
| 5185 | + break; |
| 5186 | + case 6: |
| 5187 | + typeName = "Attribute"; |
| 5188 | + id = find_by_name(lcf::Data::attributes); |
| 5189 | + break; |
| 5190 | + case 7: |
| 5191 | + typeName = "State"; |
| 5192 | + id = find_by_name(lcf::Data::states); |
| 5193 | + break; |
| 5194 | + case 8: |
| 5195 | + typeName = "Animation"; |
| 5196 | + id = find_by_name(lcf::Data::animations); |
| 5197 | + break; |
| 5198 | + case 9: |
| 5199 | + typeName = "Tileset"; |
| 5200 | + id = find_by_name(lcf::Data::chipsets); |
| 5201 | + break; |
| 5202 | + case 10: |
| 5203 | + typeName = "Switch"; |
| 5204 | + id = find_by_name(lcf::Data::switches); |
| 5205 | + break; |
| 5206 | + case 11: |
| 5207 | + typeName = "Variable"; |
| 5208 | + id = find_by_name(lcf::Data::variables); |
| 5209 | + break; |
| 5210 | + case 12: |
| 5211 | + typeName = "String Variable"; |
| 5212 | + Output::Warning("{} names are not supported yet.", typeName); // FIXME: .t[a].name? - String variables don't support name yet |
| 5213 | + return true; |
| 5214 | + // Main_Data::game_strings->GetLcfData(); |
| 5215 | + // break; |
| 5216 | + case 13: |
| 5217 | + typeName = "Common Event"; |
| 5218 | + id = find_by_name(lcf::Data::commonevents); |
| 5219 | + break; |
| 5220 | + case 14: |
| 5221 | + typeName = "Class"; |
| 5222 | + id = find_by_name(lcf::Data::classes); |
| 5223 | + break; |
| 5224 | + case 15: |
| 5225 | + typeName = "Battler Animation"; |
| 5226 | + id = find_by_name(lcf::Data::battleranimations); |
| 5227 | + break; |
| 5228 | + case 16: |
| 5229 | + typeName = "Map"; |
| 5230 | + id = find_by_name(lcf::Data::treemap.maps); |
| 5231 | + break; |
| 5232 | + case 17: |
| 5233 | + typeName = "Map Event"; |
| 5234 | + id = find_by_name(Game_Map::GetEvents()); |
| 5235 | + break; |
| 5236 | + case 18: |
| 5237 | + typeName = "Party Member"; |
| 5238 | + id = find_by_name(Main_Data::game_party.get()->GetActors()); |
| 5239 | + break; |
| 5240 | + default: |
| 5241 | + Output::Warning("Unsupported data type: {}({})", typeName, dataType); |
| 5242 | + return true; |
| 5243 | + } |
| 5244 | + |
| 5245 | + if (id > 0) { |
| 5246 | + Output::Debug(" {} \"{}\" found at ID: {}. Storing ID in variable {}.", typeName, targetName, id, outputVar); |
| 5247 | + Main_Data::game_variables->Set(outputVar, id); |
| 5248 | + Game_Map::SetNeedRefresh(true); |
| 5249 | + } |
| 5250 | + else { |
| 5251 | + Output::Warning("{} \"{}\" not found within the range {} to {}.", typeName, targetName, rangeMin, rangeMax); |
5151 | 5252 | }
|
5152 | 5253 |
|
5153 |
| - Output::Warning("Variable \"{}\" not found within the range of variables {} to {}.", targetName, rangeMin, rangeMax); |
5154 | 5254 | return true;
|
5155 | 5255 | }
|
5156 | 5256 |
|
|
0 commit comments