summaryrefslogtreecommitdiff
path: root/src/factories/sf64/ScriptFactory.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/factories/sf64/ScriptFactory.cpp')
-rw-r--r--src/factories/sf64/ScriptFactory.cpp155
1 files changed, 61 insertions, 94 deletions
diff --git a/src/factories/sf64/ScriptFactory.cpp b/src/factories/sf64/ScriptFactory.cpp
index 64c6a31..9251cf2 100644
--- a/src/factories/sf64/ScriptFactory.cpp
+++ b/src/factories/sf64/ScriptFactory.cpp
@@ -4,14 +4,10 @@
#include "Companion.h"
#include "utils/Decompressor.h"
#include "utils/TorchUtils.h"
-#include "regex.h"
+#include <regex>
#include "storm/SWrapper.h"
-#define CLAMP_MAX(val, max) (((val) < (max)) ? (val) : (max))
-#define MIN(a, b) (((a) < (b)) ? (a) : (b))
-#define VALUE_TO_ENUM(val, enumname, fallback) (Companion::Instance->GetEnumFromValue(enumname, val).value_or("/*" + std::string(fallback) + " */ " + std::to_string(val)));
-
SF64::ScriptData::ScriptData(std::vector<uint32_t> ptrs, std::vector<uint16_t> cmds, std::map<uint32_t, int> sizeMap, uint32_t ptrsStart, uint32_t cmdsStart): mPtrs(ptrs), mCmds(cmds), mSizeMap(sizeMap), mPtrsStart(ptrsStart), mCmdsStart(cmdsStart) {
}
@@ -20,7 +16,7 @@ ExportResult SF64::ScriptHeaderExporter::Export(std::ostream &write, std::shared
const auto symbol = GetSafeNode(node, "symbol", entryName);
if(Companion::Instance->IsOTRMode()){
- write << "static const char " << symbol << "[] = \"__OTR__" << (*replacement) << "\";\n\n";
+ write << "static const ALIGN_ASSET(2) char " << symbol << "[] = \"__OTR__" << (*replacement) << "\";\n\n";
return std::nullopt;
}
@@ -34,21 +30,20 @@ std::string MakeScriptCmd(uint16_t s1, uint16_t s2) {
std::ostringstream cmd;
std::ostringstream comment;
-
+ auto enumName = Companion::Instance->GetEnumFromValue("EventOpcode", opcode).value_or("/* EVOP_UNK */ " + std::to_string(opcode));
switch (opcode) {
case 0:
case 1: {
auto f3 = arg1 & 0x7F;
- // auto zm = (arg1 >> 7) & 3;
- // auto zmode = Companion::Instance->GetEnumFromValue("EventModeZ", zm).value_or("/* EVOP_UNK */ " + std::to_string(zm));
- auto zmode = VALUE_TO_ENUM((arg1 >> 7) & 3, "EventModeZ", "EVOP_UNK");
-
- cmd << "EVENT_SET_" << (opcode ? "ACCEL" : "SPEED") << "(" << std::dec << f3 << ", " << zmode << ", " << s2;
+ auto zm = (arg1 >> 7) & 3;
+ auto zmode = Companion::Instance->GetEnumFromValue("EventModeZ", zm).value_or("/* EVOP_UNK */ " + std::to_string(zm));
+
+ cmd << "EVENT_SET_" << (opcode ? "ACCEL" : "SPEED") << "(" << std::dec << f3 << ", " << zmode << ", " << s2 << "),";
comment << " // wait " << s2 << " frames";
} break;
case 2:
- cmd << "EVENT_SET_BASE_ZVEL(" << std::dec << s2;
+ cmd << "EVENT_SET_BASE_ZVEL(" << std::dec << s2 << "),";
break;
case 9:
case 10:
@@ -60,124 +55,97 @@ std::string MakeScriptCmd(uint16_t s1, uint16_t s2) {
case 19:
case 20:
case 21: {
- auto rotcmd = VALUE_TO_ENUM(opcode, "EventOpcode", "EVOP_UNK");
- cmd << rotcmd.replace(0, 4, "EVENT") << "(" << std::dec << s2 << ", " << std::fixed << std::setprecision(1) << arg1 / 10.0f;
+ auto rotcmd = enumName.replace(0, 4, "EVENT");
+ cmd << rotcmd << "(" << std::dec << s2 << ", " << std::fixed << std::setprecision(1) << arg1 / 10.0f << "),";
if(opcode < 16 && arg1 != 0) {
comment << " // wait " << 10 * s2 / arg1 << " frames";
}
} break;
case 24:
- cmd << "EVENT_SET_ROTATE(";
+ cmd << "EVENT_SET_ROTATE(),";
break;
case 25:
- cmd << "EVENT_STOP_ROTATE(";
- break;
- case 44:
- cmd << "EVENT_CHASE_TARGET(" << std::dec << arg1 << ", " << s2;
+ cmd << "EVENT_STOP_ROTATE(),";
break;
- case 45: {
- auto teamId = VALUE_TO_ENUM(arg1, "TeamId", "TEAMID_UNK");
- cmd << "EVENT_SET_TARGET(" << teamId << ", " << std::dec << s2;
- } break;
case 56:
- cmd << "EVENT_SET_CALL(" << std::dec << s2 << ", " << arg1;
+ cmd << "EVENT_SET_CALL(" << std::dec << s2 << ", " << arg1 << "),";
+ break;
+ case 57:
+ cmd << "EVENT_RESTORE_TEAM(" << std::dec << s2 << "),";
break;
- case 57: {
- auto teamId = VALUE_TO_ENUM(s2, "TeamId", "TEAMID_UNK");
- cmd << "EVENT_RESTORE_TEAM(" << teamId;
- } break;
- case 58:
+ case 58: {
+ auto sfxIndex = Companion::Instance->GetEnumFromValue("EventSfx", s2).value_or("/* EVSFX_UNK */ " + std::to_string(s2));
+ cmd << "EVENT_PLAY_SFX(" << sfxIndex << "),";
+ } break;
case 59: {
- // auto sfxIndex = Companion::Instance->GetEnumFromValue("EventSfx", s2).value_or("/* EVSFX_UNK */ " + std::to_string(s2));
- auto sfxIndex = VALUE_TO_ENUM(s2, "EventSfx", "EVSFX_UNK");
- cmd << "EVENT_" << ((opcode == 58) ? "PLAY" : "STOP") << "_SFX(" << sfxIndex;
+ auto sfxIndex = Companion::Instance->GetEnumFromValue("EventSfx", s2).value_or("/* EVSFX_UNK */ " + std::to_string(s2));
+ cmd << "EVENT_STOP_SFX(" << sfxIndex << "),";
} break;
- // case 59: {
- // auto sfxIndex = Companion::Instance->GetEnumFromValue("EventSfx", s2).value_or("/* EVSFX_UNK */ " + std::to_string(s2));
- // cmd << "EVENT_STOP_SFX(" << sfxIndex;
- // } break;
- case 96:
- cmd << ((s2 == 0) ? "EVENT_CLEAR_TRIGGER(" : "EVENT_SET_TRIGGER(");
- if(s2 != 0) {
- auto condition = VALUE_TO_ENUM(MIN(s2, 100), "EventCondition", "EVC_UNK");
- cmd << condition << ((s2 < 100) ? "" : " + " + std::to_string(s2 - 100)) << ", ";
+ case 96:
+ if(s2 == 0) {
+ cmd << "EVENT_CLEAR_TRIGGER(" << std::dec << arg1 << "),";
+ } else if(s2 >= 100) {
+ auto condition = Companion::Instance->GetEnumFromValue("EventCondition", 100).value_or("/* EVC_UNK */ " + std::to_string(s2));
+ cmd << "EVENT_SET_TRIGGER(" << condition << " + " << std::dec << (s2 - 100) << ", " << std::dec << arg1 << "),";
+ } else {
+ auto condition = Companion::Instance->GetEnumFromValue("EventCondition", s2).value_or("/* EVC_UNK */ " + std::to_string(s2));
+ cmd << "EVENT_SET_TRIGGER(" << condition << ", " << std::dec << arg1 << "),";
}
- cmd << ((arg1 < 200) ? "" : "EVENT_AI_CHANGE + ") << std::dec << ((arg1 < 200) ? arg1 : arg1 - 200);
break;
case 104: {
- // auto actorinfo = Companion::Instance->GetEnumFromValue("EventActorInfo", s2).value_or("/* EINFO_UNK */ " + std::to_string(s2));
- auto actorInfo = VALUE_TO_ENUM(s2, "EventActorInfo", "EINFO_UNK");
- cmd << "EVENT_INIT_ACTOR(" << actorInfo << ", " << std::dec << arg1;
- } break;
- case 105: {
- auto teamId = VALUE_TO_ENUM(s2, "TeamId", "TEAMID_UNK");
- cmd << "EVENT_SET_TEAM_ID(" << teamId;
+ auto actorinfo = Companion::Instance->GetEnumFromValue("EventActorInfo", s2).value_or("/* EINFO_UNK */ " + std::to_string(s2));
+ cmd << "EVENT_INIT_ACTOR(" << actorinfo << ", " << std::dec << arg1 << "),";
} break;
case 112:{
- // auto actiontype = Companion::Instance->GetEnumFromValue("EventAction", s2).value_or("/* EVACT_UNK */ " + std::to_string(s2));
- auto actiontype = VALUE_TO_ENUM(s2, "EventAction", "EVACT_UNK");
- cmd << "EVENT_SET_ACTION(" << actiontype;
+ auto actiontype = Companion::Instance->GetEnumFromValue("EventAction", s2).value_or("/* EVACT_UNK */ " + std::to_string(s2));
+ cmd << "EVENT_SET_ACTION(" << actiontype << "),";
if((s2 == 14 || s2 == 15)) {
comment << " // wait 1 frame";
}
} break;
case 113:
- cmd << "EVENT_ADD_TO_GROUP(" << std::dec << s2 << ", " << arg1;
+ cmd << "EVENT_ADD_TO_GROUP(" << std::dec << s2 << ", " << arg1 << "),";
break;
case 116: {
- // auto itemtype = Companion::Instance->GetEnumFromValue("ItemDrop", s2).value_or("/* DROP_UNK */ " + std::to_string(s2));
- auto itemType = VALUE_TO_ENUM(s2, "ItemDrop", "DROP_UNK");
- cmd << "EVENT_DROP_ITEM(" << itemType;
+ auto itemtype = Companion::Instance->GetEnumFromValue("ItemDrop", s2).value_or("/* DROP_UNK */ " + std::to_string(s2));
+ cmd << "EVENT_DROP_ITEM(" << itemtype << "),";
} break;
- case 118:
- cmd << "EVENT_SET_REVERB(" << std::dec << s2;
+ case 118:
+ cmd << "EVENT_SET_REVERB(" << std::dec << s2 << "),";
break;
case 119: {
- // auto groundtype = Companion::Instance->GetEnumFromValue("GroundType", s2).value_or("/* GROUNDTYPE_UNK */ " + std::to_string(s2));
- auto groundtype = VALUE_TO_ENUM(s2, "GroundType", "GROUNDTYPE_UNK");
- cmd << "EVENT_SET_GROUND(" << groundtype;
+ auto groundtype = Companion::Instance->GetEnumFromValue("GroundType", s2).value_or("/* GROUNDTYPE_UNK */ " + std::to_string(s2));
+ cmd << "EVENT_SET_GROUND(" << groundtype << "),";
} break;
case 120: {
- // auto rcidName = Companion::Instance->GetEnumFromValue("RadioCharacterId", arg1).value_or("/* RCID_UNK */ " + std::to_string(arg1));
- auto rcidName = VALUE_TO_ENUM(arg1, "RadioCharacterId", "RCID_UNK");
- cmd << "EVENT_PLAY_MSG(" << rcidName << ", " << std::dec << s2;
- } break;
- case 121: {
- auto teamId = VALUE_TO_ENUM(s2, "TeamId", "TEAMID_UNK");
- cmd << "EVENT_DAMAGE_TEAM(" << teamId << ", " << std::dec << arg1;
+ auto rcidName = Companion::Instance->GetEnumFromValue("RadioCharacterId", arg1).value_or("/* RCID_UNK */ " + std::to_string(arg1));
+ cmd << "EVENT_PLAY_MSG(" << rcidName << ", " << std::dec << std::setw(5) << s2 << "),";
} break;
- case 122:
- cmd << "EVENT_STOP_BGM(";
+ case 122:
+ cmd << "EVENT_STOP_BGM(),";
break;
case 124: {
- // auto color = Companion::Instance->GetEnumFromValue("TexLineColor", s2).value_or("/* TXLC_UNK */ " + std::to_string(s2));
- auto color = VALUE_TO_ENUM(s2, "TexLineColor", "TXLC_UNK");
- cmd << "EVENT_MAKE_TEXLINE(" << color;
+ auto color = Companion::Instance->GetEnumFromValue("TexLineColor", s2).value_or("/* TXLC_UNK */ " + std::to_string(s2));
+ cmd << "EVENT_MAKE_TEXLINE(" << color << "),";
} break;
case 125:
- cmd << "EVENT_STOP_TEXLINE(";
+ cmd << "EVENT_STOP_TEXLINE(),";
break;
- case 126:
- cmd << ((s2 == 0) ? "EVENT_GOTO(" : "EVENT_LOOP(");
- if(s2 != 0) {
- cmd << std::dec << s2 << ", ";
- }
- cmd << ((arg1 < 200) ? "" : "EVENT_AI_CHANGE + ") << std::dec << ((arg1 < 200) ? arg1 : arg1 - 200);
+ case 126:
+ cmd << "EVENT_LOOP(" << std::dec << arg1 << ", " << s2 << "),";
break;
case 127:
- cmd << "EVENT_STOP_SCRIPT(";
+ cmd << "EVENT_STOP_SCRIPT(),";
comment << "// wait";
break;
- default: {
- // auto opcodeName = Companion::Instance->GetEnumFromValue("EventOpcode", opcode).value_or("/* EVOP_UNK */ " + std::to_string(opcode));
- auto opcodeName = VALUE_TO_ENUM(opcode, "EventOpcode", "EVOP_UNK");
- cmd << "EVENT_CMD(" << opcodeName << ", " << std::dec << arg1 << ", " << s2;
+ default:
+ cmd << "EVENT_CMD(" << enumName << ", " << std::dec << arg1 << ", " << s2 << "),";
if(opcode >= 40 && opcode <= 48) {
comment << " // wait";
}
- } break;
+ break;
}
- cmd << "), " << comment.str();
+ cmd << comment.str();
return cmd.str();
}
@@ -210,7 +178,7 @@ ExportResult SF64::ScriptCodeExporter::Export(std::ostream &write, std::shared_p
// }
write << MakeScriptCmd(script->mCmds[cmdIndex], script->mCmds[cmdIndex + 1]);
}
-
+
write << "\n};\n";
cmdOff += 4 * cmdCount;
@@ -255,8 +223,6 @@ ExportResult SF64::ScriptBinaryExporter::Export(std::ostream &write, std::shared
for (auto ptr : sortedPtrs) {
auto wrapper = Companion::Instance->GetCurrentWrapper();
std::ostringstream stream;
- stream.str("");
- stream.clear();
auto cmdWriter = LUS::BinaryWriter();
WriteHeader(cmdWriter, LUS::ResourceType::ScriptCmd, 0);
@@ -283,9 +249,10 @@ ExportResult SF64::ScriptBinaryExporter::Export(std::ostream &write, std::shared
// Export Script
WriteHeader(scriptWriter, LUS::ResourceType::Script, 0);
- scriptWriter.Write((uint32_t) script->mPtrs.size());
- for (auto &ptr : script->mPtrs) {
- scriptWriter.Write(ptrMap.at(ptr));
+ auto count = script->mPtrs.size();
+ scriptWriter.Write((uint32_t) count);
+ for (size_t i = 0; i < script->mPtrs.size(); i++) {
+ scriptWriter.Write(ptrMap.at(script->mPtrs.at(i)));
}
scriptWriter.Finish(write);