Skip to content

Commit

Permalink
DynRPG Easyrpg_raw - simplify commands
Browse files Browse the repository at this point in the history
removed repeated code, and overcomplex logic.
  • Loading branch information
jetrotal committed Nov 6, 2023
1 parent 5fd9447 commit 5660b7f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 75 deletions.
24 changes: 6 additions & 18 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/

#include "utils.h"
#include <iostream>
#include <map>
#include <string>
Expand Down Expand Up @@ -1178,7 +1179,6 @@ class Constants {

};
EventCode = {
// AJUDAS
{"END", 10},
{"CallCommonEvent", 1005},
{"ForceFlee", 1006},
Expand Down Expand Up @@ -1336,33 +1336,21 @@ class Constants {
};
};

std::string toLower(const std::string& str) {
std::string result = str;
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
return result;
};

// Function to return contents from either DestinyScript or EventCode
std::unordered_map<std::string, int>& MapCollection(const std::string& collectionName) {
std::string lowercaseCollectionName = toLower(collectionName);
if ( Utils::StrICmp(collectionName, "destinyscript") == 0 ) return DestinyScript;
else if ( Utils::StrICmp(collectionName, "eventcode") == 0 ) return EventCode;

if (lowercaseCollectionName == "destinyscript") return DestinyScript;
else if (lowercaseCollectionName == "eventcode") return EventCode;
else return EventCode;

};

// Function to retrieve a value from mapCollection ignoring case
std::string get(const std::string& mapName, const std::string& key) {
std::unordered_map<std::string, int>& selectedMap = MapCollection(mapName);
std::string lowercaseKey = toLower(key);

for (auto it = selectedMap.begin(); it != selectedMap.end(); ++it) {
std::string lowercaseMapKey = toLower(it->first);

if (lowercaseMapKey == lowercaseKey) return std::to_string(it->second);
}
for (auto it = selectedMap.begin(); it != selectedMap.end(); ++it)
if ( Utils::StrICmp(it->first, key) == 0 ) return std::to_string(it->second);

return "0"; // Key not found
return key; // Key not found
}
};
93 changes: 36 additions & 57 deletions src/dynrpg_easyrpg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,80 +88,59 @@ static bool EasyAdd(dyn_arg_list args) {

return true;
}

bool DynRpg::EasyRpgPlugin::EasyRaw(dyn_arg_list args, Game_Interpreter* interpreter) {
if (!interpreter) {
return true;
}

auto func = "easyrpg_raw";
bool okay = false;

lcf::rpg::EventCommand cmd;
std::vector<int32_t> output_args;
if (!interpreter) return true;

if (args.empty()) {
Output::Warning("easyrpg_raw: Command too short");
return true;
}

Constants Constants;
std::string keyToPrint = "ENABLE";

//Output::Warning("Key {}, value {}", keyToPrint, Constants.get("DestinyScript",keyToPrint));

auto evt = args[0];
if (evt.find("@") == 0) {
evt = evt.substr(1);
evt = Constants.get("EventCode", evt);
Constants constList;

cmd.code = stoi(evt);
okay = bool(cmd.code);
} else
std::tie(cmd.code) = DynRpg::ParseArgs<int>(func, args, &okay);

if (!okay) {
Output::Warning("EasyRpgPlugin - Unknown Input: {}",args[0]);
return true;
}
const std::string func = "easyrpg_raw";
bool okay = false;
int codeArgIndex = 0;
int stringArgIndex = 1;
bool endOfLine = false;

if (args.size() >= 2) {
auto [string_arg] = DynRpg::ParseArgs<std::string>(func, args.subspan(1), &okay);
cmd.string = lcf::DBString(string_arg);
lcf::rpg::EventCommand cmd;
std::vector<int32_t> outputArgs;
std::vector<lcf::rpg::EventCommand> cmdList;

if (!okay) {
return true;
for (size_t i = 0; i < args.size(); ++i) {
if (i == args.size() - 1) {
if (args[i].back() == ';') args[i] = args[i].substr(0, args[i].length() - 1);
endOfLine = true;
}

for (size_t i = 2; i < args.size(); ++i) {


auto currArg = args[i];

if (currArg.find("@") == 0) {
currArg = currArg.substr(1);
currArg = Constants.get("DestinyScript", currArg);

auto int_arg = stoi(currArg);
okay = true;
output_args.push_back(int_arg);
}
else {
auto [int_arg] = DynRpg::ParseArgs<int>(func, args.subspan(i), &okay);
output_args.push_back(int_arg);
}
// TODO: Implement multi-line command interpretation split by ';'.


if (!okay) {
return true;
}
if (i == codeArgIndex) {
if (args[i].front() == '@') args[i] = constList.get("EventCode", args[i].substr(1));
std::tie(cmd.code) = DynRpg::ParseArgs<int>(func, args, &okay);
}
else if (i == stringArgIndex) {
auto [stringArg] = DynRpg::ParseArgs<std::string>(func, args.subspan(i), &okay);
cmd.string = lcf::DBString(stringArg);
}
else {
if (args[i].front() == '@') args[i] = constList.get("DestinyScript", args[i].substr(1));
auto [intArg] = DynRpg::ParseArgs<int>(func, args.subspan(i), &okay);
outputArgs.push_back(intArg);
}
}

cmd.parameters = lcf::DBArray<int32_t>(output_args.begin(), output_args.end());
if (endOfLine) {
codeArgIndex = i + 1;
stringArgIndex = i + 2;
cmd.parameters = lcf::DBArray<int32_t>(outputArgs.begin(), outputArgs.end());
cmdList.push_back(cmd);
}

interpreter->Push({ cmd }, 0, false);
if (!okay) return true;
}

interpreter->Push(cmdList, 0, false);
return true;
}

Expand Down

0 comments on commit 5660b7f

Please sign in to comment.