Skip to content

Commit

Permalink
Refactor: Moved a few other helper functions from Game_Interpreter to…
Browse files Browse the repository at this point in the history
… Game_Interpreter_Shared
  • Loading branch information
florianessl committed May 30, 2024
1 parent 407342d commit 90499eb
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 78 deletions.
67 changes: 0 additions & 67 deletions src/game_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,56 +566,6 @@ void Game_Interpreter::SkipToNextConditional(std::initializer_list<Cmd> codes, i
}
}

int Game_Interpreter::DecodeInt(lcf::DBArray<int32_t>::const_iterator& it) {
int value = 0;

for (;;) {
int x = *it++;
value <<= 7;
value |= x & 0x7F;
if (!(x & 0x80))
break;
}

return value;
}

const std::string Game_Interpreter::DecodeString(lcf::DBArray<int32_t>::const_iterator& it) {
std::ostringstream out;
int len = DecodeInt(it);

for (int i = 0; i < len; i++)
out << (char)*it++;

std::string result = lcf::ReaderUtil::Recode(out.str(), Player::encoding);

return result;
}

lcf::rpg::MoveCommand Game_Interpreter::DecodeMove(lcf::DBArray<int32_t>::const_iterator& it) {
lcf::rpg::MoveCommand cmd;
cmd.command_id = *it++;

switch (cmd.command_id) {
case 32: // Switch ON
case 33: // Switch OFF
cmd.parameter_a = DecodeInt(it);
break;
case 34: // Change Graphic
cmd.parameter_string = lcf::DBString(DecodeString(it));
cmd.parameter_a = DecodeInt(it);
break;
case 35: // Play Sound Effect
cmd.parameter_string = lcf::DBString(DecodeString(it));
cmd.parameter_a = DecodeInt(it);
cmd.parameter_b = DecodeInt(it);
cmd.parameter_c = DecodeInt(it);
break;
}

return cmd;
}

// Execute Command.
bool Game_Interpreter::ExecuteCommand() {
auto& frame = GetFrame();
Expand Down Expand Up @@ -5056,23 +5006,6 @@ bool Game_Interpreter::IsWaitingForWaitCommand() const {
return (_state.wait_time > 0) || _state.wait_key_enter;
}

bool Game_Interpreter::ManiacCheckContinueLoop(int val, int val2, int type, int op) const {
switch (type) {
case 0: // Infinite loop
return true;
case 1: // X times
case 2: // Count up
return val <= val2;
case 3: // Count down
return val >= val2;
case 4: // While
case 5: // Do While
return CheckOperator(val, val2, op);
default:
return false;
}
}

int Game_Interpreter::ManiacBitmask(int value, int mask) const {
if (Player::IsPatchManiac()) {
return value & mask;
Expand Down
5 changes: 0 additions & 5 deletions src/game_interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,6 @@ class Game_Interpreter : public Game_BaseInterpreterContext
bool CommandManiacCallCommand(lcf::rpg::EventCommand const& com);
bool CommandEasyRpgSetInterpreterFlag(lcf::rpg::EventCommand const& com);

int DecodeInt(lcf::DBArray<int32_t>::const_iterator& it);
const std::string DecodeString(lcf::DBArray<int32_t>::const_iterator& it);
lcf::rpg::MoveCommand DecodeMove(lcf::DBArray<int32_t>::const_iterator& it);

void SetSubcommandIndex(int indent, int idx);
uint8_t& ReserveSubcommandIndex(int indent);
int GetSubcommandIndex(int indent) const;
Expand Down Expand Up @@ -329,7 +325,6 @@ class Game_Interpreter : public Game_BaseInterpreterContext
void toSave(lcf::rpg::SaveEventExecState& save) const;
};

bool ManiacCheckContinueLoop(int val, int val2, int type, int op) const;
int ManiacBitmask(int value, int mask) const;

lcf::rpg::SaveEventExecState _state;
Expand Down
52 changes: 52 additions & 0 deletions src/game_interpreter_shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <cmath>
#include <cstdint>
#include <lcf/rpg/savepartylocation.h>
#include <lcf/reader_util.h>

using Main_Data::game_switches, Main_Data::game_variables, Main_Data::game_strings;

Expand Down Expand Up @@ -185,3 +186,54 @@ StringView Game_Interpreter_Shared::CommandStringOrVariableBitfield(lcf::rpg::Ev

return com.string;
}


int Game_Interpreter_Shared::DecodeInt(lcf::DBArray<int32_t>::const_iterator& it) {
int value = 0;

for (;;) {
int x = *it++;
value <<= 7;
value |= x & 0x7F;
if (!(x & 0x80))
break;
}

return value;
}

const std::string Game_Interpreter_Shared::DecodeString(lcf::DBArray<int32_t>::const_iterator& it) {
std::ostringstream out;
int len = DecodeInt(it);

for (int i = 0; i < len; i++)
out << (char)*it++;

std::string result = lcf::ReaderUtil::Recode(out.str(), Player::encoding);

return result;
}

lcf::rpg::MoveCommand Game_Interpreter_Shared::DecodeMove(lcf::DBArray<int32_t>::const_iterator& it) {
lcf::rpg::MoveCommand cmd;
cmd.command_id = *it++;

switch (cmd.command_id) {
case 32: // Switch ON
case 33: // Switch OFF
cmd.parameter_a = DecodeInt(it);
break;
case 34: // Change Graphic
cmd.parameter_string = lcf::DBString(DecodeString(it));
cmd.parameter_a = DecodeInt(it);
break;
case 35: // Play Sound Effect
cmd.parameter_string = lcf::DBString(DecodeString(it));
cmd.parameter_a = DecodeInt(it);
cmd.parameter_b = DecodeInt(it);
cmd.parameter_c = DecodeInt(it);
break;
}

return cmd;
}
36 changes: 30 additions & 6 deletions src/game_interpreter_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define EP_GAME_INTERPRETER_SHARED

#include <lcf/rpg/eventcommand.h>
#include <lcf/rpg/movecommand.h>
#include <lcf/rpg/saveeventexecframe.h>
#include <string_view.h>

Expand Down Expand Up @@ -60,18 +61,18 @@ namespace Game_Interpreter_Shared {
* Indicates how the target of an interpreter operation (lvalue) should be evaluated.
*/
enum TargetEvalMode : std::int8_t { // 4 bits
eTargetEval_Single = 0, // v[x]
eTargetEval_Range = 1, // v[x...y]
eTargetEval_IndirectSingle = 2, // v[v[x]]
eTargetEval_IndirectRange = 3, // v[v[x]...v[y]] (ManiacPatch)
eTargetEval_Expression = 4 // ManiacPatch expression
eTargetEval_Single = 0, // v[x]
eTargetEval_Range = 1, // v[x...y]
eTargetEval_IndirectSingle = 2, // v[v[x]]
eTargetEval_IndirectRange = 3, // v[v[x]...v[y]] (ManiacPatch)
eTargetEval_Expression = 4 // ManiacPatch expression
};

/*
* Indicates how an operand of an interpreter operation (rvalue) should be evaluted.
*/
enum ValueEvalMode : std::int8_t { // 4 bits
eValueEval_Constant = 0, // Constant value is given
eValueEval_Constant = 0, // Constant value is given
eValueEval_Variable = 1,
eValueEval_VariableIndirect = 2,
eValueEval_Switch = 3,
Expand Down Expand Up @@ -123,6 +124,12 @@ namespace Game_Interpreter_Shared {
StringView CommandStringOrVariableBitfield(lcf::rpg::EventCommand const& com, int mode_idx, int shift, int val_idx);

bool CheckOperator(int val, int val2, int op);

int DecodeInt(lcf::DBArray<int32_t>::const_iterator& it);
const std::string DecodeString(lcf::DBArray<int32_t>::const_iterator& it);
lcf::rpg::MoveCommand DecodeMove(lcf::DBArray<int32_t>::const_iterator& it);

bool ManiacCheckContinueLoop(int val, int val2, int type, int op);
}

inline bool Game_Interpreter_Shared::CheckOperator(int val, int val2, int op) {
Expand All @@ -144,6 +151,23 @@ inline bool Game_Interpreter_Shared::CheckOperator(int val, int val2, int op) {
}
}

inline bool Game_Interpreter_Shared::ManiacCheckContinueLoop(int val, int val2, int type, int op) {
switch (type) {
case 0: // Infinite loop
return true;
case 1: // X times
case 2: // Count up
return val <= val2;
case 3: // Count down
return val >= val2;
case 4: // While
case 5: // Do While
return CheckOperator(val, val2, op);
default:
return false;
}
}

//explicit declarations for target evaluation logic shared between ControlSwitches/ControlVariables/ControlStrings
template bool Game_BaseInterpreterContext::DecodeTargetEvaluationMode<true, false, false, false, false>(lcf::rpg::EventCommand const&, int&, int&) const;
template bool Game_BaseInterpreterContext::DecodeTargetEvaluationMode<true, true, true, false, false>(lcf::rpg::EventCommand const&, int&, int&) const;
Expand Down

0 comments on commit 90499eb

Please sign in to comment.