Skip to content

Commit 1b99aff

Browse files
committed
DynRPG Easyrpg_raw - simplify commands
removed repeated code, and overcomplex logic.
1 parent 5fd9447 commit 1b99aff

File tree

2 files changed

+42
-75
lines changed

2 files changed

+42
-75
lines changed

src/constants.h

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18+
#include "utils.h"
1819
#include <iostream>
1920
#include <map>
2021
#include <string>
@@ -1178,7 +1179,6 @@ class Constants {
11781179

11791180
};
11801181
EventCode = {
1181-
// AJUDAS
11821182
{"END", 10},
11831183
{"CallCommonEvent", 1005},
11841184
{"ForceFlee", 1006},
@@ -1336,33 +1336,21 @@ class Constants {
13361336
};
13371337
};
13381338

1339-
std::string toLower(const std::string& str) {
1340-
std::string result = str;
1341-
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
1342-
return result;
1343-
};
1344-
13451339
// Function to return contents from either DestinyScript or EventCode
13461340
std::unordered_map<std::string, int>& MapCollection(const std::string& collectionName) {
1347-
std::string lowercaseCollectionName = toLower(collectionName);
1341+
if ( Utils::StrICmp(collectionName, "destinyscript") == 0 ) return DestinyScript;
1342+
else if ( Utils::StrICmp(collectionName, "eventcode") == 0 ) return EventCode;
13481343

1349-
if (lowercaseCollectionName == "destinyscript") return DestinyScript;
1350-
else if (lowercaseCollectionName == "eventcode") return EventCode;
13511344
else return EventCode;
1352-
13531345
};
13541346

13551347
// Function to retrieve a value from mapCollection ignoring case
13561348
std::string get(const std::string& mapName, const std::string& key) {
13571349
std::unordered_map<std::string, int>& selectedMap = MapCollection(mapName);
1358-
std::string lowercaseKey = toLower(key);
1359-
1360-
for (auto it = selectedMap.begin(); it != selectedMap.end(); ++it) {
1361-
std::string lowercaseMapKey = toLower(it->first);
13621350

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

1366-
return "0"; // Key not found
1354+
return key; // Key not found
13671355
}
13681356
};

src/dynrpg_easyrpg.cpp

Lines changed: 36 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -88,80 +88,59 @@ static bool EasyAdd(dyn_arg_list args) {
8888

8989
return true;
9090
}
91-
9291
bool DynRpg::EasyRpgPlugin::EasyRaw(dyn_arg_list args, Game_Interpreter* interpreter) {
93-
if (!interpreter) {
94-
return true;
95-
}
96-
97-
auto func = "easyrpg_raw";
98-
bool okay = false;
99-
100-
lcf::rpg::EventCommand cmd;
101-
std::vector<int32_t> output_args;
92+
if (!interpreter) return true;
10293

10394
if (args.empty()) {
10495
Output::Warning("easyrpg_raw: Command too short");
10596
return true;
10697
}
10798

108-
Constants Constants;
109-
std::string keyToPrint = "ENABLE";
110-
111-
//Output::Warning("Key {}, value {}", keyToPrint, Constants.get("DestinyScript",keyToPrint));
112-
113-
auto evt = args[0];
114-
if (evt.find("@") == 0) {
115-
evt = evt.substr(1);
116-
evt = Constants.get("EventCode", evt);
99+
Constants constList;
117100

118-
cmd.code = stoi(evt);
119-
okay = bool(cmd.code);
120-
} else
121-
std::tie(cmd.code) = DynRpg::ParseArgs<int>(func, args, &okay);
122-
123-
if (!okay) {
124-
Output::Warning("EasyRpgPlugin - Unknown Input: {}",args[0]);
125-
return true;
126-
}
101+
const std::string func = "easyrpg_raw";
102+
bool okay = false;
103+
int codeArgIndex = 0;
104+
int stringArgIndex = 1;
105+
bool endOfLine = false;
127106

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

132-
if (!okay) {
133-
return true;
111+
for (size_t i = 0; i < args.size(); ++i) {
112+
if (i == args.size() - 1) {
113+
if (args[i].back() == ';') args[i] = args[i].substr(0, args[i].length() - 1);
114+
endOfLine = true;
134115
}
135116

136-
for (size_t i = 2; i < args.size(); ++i) {
137-
138-
139-
auto currArg = args[i];
140-
141-
if (currArg.find("@") == 0) {
142-
currArg = currArg.substr(1);
143-
currArg = Constants.get("DestinyScript", currArg);
144-
145-
auto int_arg = stoi(currArg);
146-
okay = true;
147-
output_args.push_back(int_arg);
148-
}
149-
else {
150-
auto [int_arg] = DynRpg::ParseArgs<int>(func, args.subspan(i), &okay);
151-
output_args.push_back(int_arg);
152-
}
117+
// TODO: Implement multi-line command interpretation split by ';'.
153118

154-
155-
if (!okay) {
156-
return true;
157-
}
119+
if (i == codeArgIndex) {
120+
if (args[i].front() == '@') args[i] = constList.get("EventCode", args[i].substr(1));
121+
std::tie(cmd.code) = DynRpg::ParseArgs<int>(func, args, &okay);
122+
}
123+
else if (i == stringArgIndex) {
124+
auto [stringArg] = DynRpg::ParseArgs<std::string>(func, args.subspan(i), &okay);
125+
cmd.string = lcf::DBString(stringArg);
126+
}
127+
else {
128+
if (args[i].front() == '@') args[i] = constList.get("DestinyScript", args[i].substr(1));
129+
auto [intArg] = DynRpg::ParseArgs<int>(func, args.subspan(i), &okay);
130+
outputArgs.push_back(intArg);
158131
}
159-
}
160132

161-
cmd.parameters = lcf::DBArray<int32_t>(output_args.begin(), output_args.end());
133+
if (endOfLine) {
134+
codeArgIndex = i + 1;
135+
stringArgIndex = i + 2;
136+
cmd.parameters = lcf::DBArray<int32_t>(outputArgs.begin(), outputArgs.end());
137+
cmdList.push_back(cmd);
138+
}
162139

163-
interpreter->Push({ cmd }, 0, false);
140+
if (!okay) return true;
141+
}
164142

143+
interpreter->Push(cmdList, 0, false);
165144
return true;
166145
}
167146

0 commit comments

Comments
 (0)