Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
florianessl committed May 29, 2024
1 parent 98d6317 commit 9482e5a
Show file tree
Hide file tree
Showing 7 changed files with 444 additions and 254 deletions.
94 changes: 50 additions & 44 deletions src/game_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -857,11 +857,11 @@ bool Game_Interpreter::ExecuteCommand(lcf::rpg::EventCommand const& com) {
return CommandControlScopedTypeOptions(com);
#else
case Cmd::ConditionalBranchEx:
return CommandConditionalBranchEx(com);
return CommandEasyRpgConditionalBranchEx(com);
case Cmd::ControlSwitchesEx:
return CommandControlSwitchesEx(com);
return CommandEasyRpgControlSwitchesEx(com);
case Cmd::ControlVarsEx:
return CommandControlVariablesEx(com);
return CommandEasyRpgControlVariablesEx(com);
case Cmd::ControlScopedSwitches:
return CommandControlScopedSwitches(com);
case Cmd::ControlScopedVars:
Expand All @@ -876,47 +876,6 @@ bool Game_Interpreter::ExecuteCommand(lcf::rpg::EventCommand const& com) {
}
}

void Game_Interpreter::CarryOverFrameScopedVariables(bool is_pop) {
auto apply_carry_flags = [](std::vector<uint32_t>& flags, auto apply) {
for (int i = 0; i < flags.size(); i++) {
int mask = flags[i];
if (mask == 0)
continue;
for (int b = 0; b < 32; b++) {
if ((mask & (1 << b)) > 0) {
apply((i * 32) + 1 + b);
}
}
}
};

assert(_state.stack.size() > 1);

#ifndef SCOPEDVARS_LIBLCF_STUB
if (is_pop) {
auto& frame_curr = _state.stack[_state.stack.size() - 1];
auto& frame_prev = _state.stack[_state.stack.size() - 2];

apply_carry_flags(frame_curr.easyrpg_frame_switches_carry_flags_out, [&frame_curr, &frame_prev](int id) {
frame_prev.easyrpg_frame_switches[i] = frame_curr.easyrpg_frame_switches[i];
});
apply_carry_flags(frame_curr.easyrpg_frame_variables_carry_flags_out, [&frame_curr, &frame_prev](int id) {
frame_prev.easyrpg_frame_variables[i] = frame_curr.easyrpg_frame_variables[i];
});
} else {
auto& frame_curr = _state.stack[_state.stack.size() - 2];
auto& frame_new = _state.stack[_state.stack.size() - 1];

apply_carry_flags(frame_curr.easyrpg_frame_switches_carry_flags_in, [&frame_curr, &frame_new](int id) {
frame_new.easyrpg_frame_switches[i] = frame_curr.easyrpg_frame_switches[i];
});
apply_carry_flags(frame_curr.easyrpg_frame_variables_carry_flags_in, [&frame_curr, &frame_new](int id) {
frame_new.easyrpg_frame_variables[i] = frame_curr.easyrpg_frame_variables[i];
});
}
#endif
}

bool Game_Interpreter::OnFinishStackFrame() {
auto& frame = GetFrame();

Expand Down Expand Up @@ -956,6 +915,53 @@ bool Game_Interpreter::OnFinishStackFrame() {
return !is_base_frame;
}

void Game_Interpreter::CarryOverFrameScopedVariables(bool is_pop) {
auto apply_carry_flags = [](auto& from, auto& to, std::vector<uint32_t>& flags) {
for (int i = 0; i < flags.size(); i++) {
int mask = flags[i];
if (mask == 0)
continue;
for (int b = 0; b < 32; b++) {
if ((mask & (1 << b)) > 0) {
int id = (i * 32) + 1 + b;
to.resize(id, 0);
to[id] = from[id];
}
}
}
};

assert(_state.stack.size() > 1);

#ifndef SCOPEDVARS_LIBLCF_STUB
if (is_pop) {
auto& frame_curr = _state.stack[_state.stack.size() - 1];
auto& frame_prev = _state.stack[_state.stack.size() - 2];

apply_carry_flags(
frame_curr.easyrpg_frame_switches,
frame_prev.easyrpg_frame_switches,
frame_curr.easyrpg_frame_switches_carry_flags_out);
apply_carry_flags(
frame_curr.easyrpg_frame_variables,
frame_prev.easyrpg_frame_variables,
frame_curr.easyrpg_frame_variables_carry_flags_out);
} else {
auto& frame_curr = _state.stack[_state.stack.size() - 2];
auto& frame_new = _state.stack[_state.stack.size() - 1];

apply_carry_flags(
frame_curr.easyrpg_frame_switches,
frame_new.easyrpg_frame_switches,
frame_curr.easyrpg_frame_switches_carry_flags_out);
apply_carry_flags(
frame_curr.easyrpg_frame_variables,
frame_new.easyrpg_frame_variables,
frame_curr.easyrpg_frame_variables_carry_flags_out);
}
#endif
}

std::vector<std::string> Game_Interpreter::GetChoices(int max_num_choices) {
const auto& frame = GetFrame();
const auto& list = frame.commands;
Expand Down
14 changes: 7 additions & 7 deletions src/game_scoped_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ SCOPED_IMPL V Game_DataStorage_TPL::PerformOperation(const int id, const V value
storage.prepare(id, id);

if constexpr (std::is_same<V, bool>::value) {
bool v = storage.vector_ref()[id - 1];
bool v = storage[id];
AssignOp(v, op(v, value));
storage.vector_ref()[id - 1] = v;
storage[id] = v;
return v;
} else {
V& v = storage[id];
Expand Down Expand Up @@ -101,9 +101,9 @@ SCOPED_IMPL V Game_DataStorage_TPL::PerformOperation(const int id, const V value
storage.flags[id - 1] |= ScopedDataStorage_t::eFlag_ValueDefined;

if constexpr (std::is_same<V, bool>::value) {
bool v = storage.vector_ref()[id - 1];
bool v = storage[id];
AssignOp(v, op(v, value));
storage.vector_ref()[id - 1] = v;
storage[id] = v;
return v;
} else {
V& v = storage[id];
Expand Down Expand Up @@ -152,7 +152,7 @@ SCOPED_IMPL void Game_DataStorage_TPL::PrepareRange(const int first_id, const in

for (int i = prepare_first_id; i <= prepare_last_id; i++) {
storage.flags[i - 1] = scopedInitFlags(S, i);
storage.vector_ref()[i - 1] = scopedGetDefaultValue(S, i);
storage[i] = scopedGetDefaultValue(S, i);
}
}
}
Expand Down Expand Up @@ -474,7 +474,7 @@ void Game_DataStorage_TPL::scopedClearValue(int id, int map_id, int event_id) {
}

storage.flags[id - 1] &= ~ScopedDataStorage_t::eFlag_ValueDefined;
storage.vector_ref()[id - 1] = _defaultValue;
storage[id] = _defaultValue;
}

Game_DataStorage_Declare_TPL
Expand All @@ -492,7 +492,7 @@ void Game_DataStorage_TPL::scopedResetTemporaryData(int map_id, int event_id) {
continue;
if ((it->second & ScopedDataStorage_t::eFlag_AutoReset) == 0)
continue;
storage.vector_ref()[it->first] = scopedGetDefaultValue(S, it->first);
storage[it->first + 1] = scopedGetDefaultValue(S, it->first);
}
}

Expand Down
Loading

0 comments on commit 9482e5a

Please sign in to comment.