Skip to content

Commit

Permalink
Make Process::step unsigned
Browse files Browse the repository at this point in the history
Avoid having to deal with UB while relying on signed integer overflow, add new value Processors::restart to be used in place of the -1 when "looping" a process (a value that when incremented by 1 will properly overflow to 0)
  • Loading branch information
edo9300 committed Feb 11, 2024
1 parent a50505e commit 62c2c5d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
4 changes: 2 additions & 2 deletions operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1560,7 +1560,7 @@ bool field::process(Processors::SelfDestroy& arg) {
destroy(pcard, nullptr, REASON_EFFECT, PLAYER_SELFDES);
}
core.self_destroy_set.erase(it);
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
case 1: {
Expand All @@ -1587,7 +1587,7 @@ bool field::process(Processors::SelfToGrave& arg) {
send_to(pcard, nullptr, REASON_EFFECT, PLAYER_NONE, PLAYER_NONE, LOCATION_GRAVE, 0, POS_FACEUP);
}
core.self_tograve_set.erase(it);
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
case 1: {
Expand Down
60 changes: 30 additions & 30 deletions processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ bool field::process(Processors::PhaseEvent& arg) {
else {
arg.priority_passed = true;
arg.is_opponent = !arg.is_opponent;
arg.step = -1;
arg.step = Processors::restart;
}
return FALSE;
}
Expand Down Expand Up @@ -513,13 +513,13 @@ bool field::process(Processors::PhaseEvent& arg) {
for(auto& ch : core.current_chain)
ch.triggering_effect->get_handler()->set_status(STATUS_CHAINING, FALSE);
emplace_process<Processors::SolveChain>(false, false, false);
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
case 4: {
adjust_instant();
emplace_process<Processors::PointEvent>(false, false, false);
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
case 20: {
Expand Down Expand Up @@ -556,7 +556,7 @@ bool field::process(Processors::PhaseEvent& arg) {
case 22: {
core.hand_adjusted = true;
emplace_process<Processors::PointEvent>(false, false, false);
arg.step = -1;
arg.step = Processors::restart;
arg.is_opponent = false;
arg.priority_passed = false;
return FALSE;
Expand Down Expand Up @@ -897,12 +897,12 @@ bool field::process(Processors::PointEvent& arg) {
if(!core.select_chains.empty())
emplace_process<Processors::SelectChain>(check_player, core.spe_effect[check_player], false);
else
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
case 33: {
if(returns.at<int32_t>(0) == -1) {
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
auto newchain = std::next(core.select_chains.begin(), returns.at<int32_t>(0));
Expand Down Expand Up @@ -953,7 +953,7 @@ bool field::process(Processors::QuickEffect& arg) {
if(returns.at<int32_t>(0) == -1) {
if(core.quick_f_chain.size()) {
arg.is_opponent = true;
arg.step = -1;
arg.step = Processors::restart;
} else if(core.new_chains.size()) {
emplace_process<Processors::AddChain>();
emplace_process<Processors::QuickEffect>(false, 1 - core.new_chains.back().triggering_player);
Expand All @@ -974,7 +974,7 @@ bool field::process(Processors::QuickEffect& arg) {
peffect->dec_count(tp);
core.new_chains.splice(core.new_chains.end(), core.select_chains, newchain);
core.quick_f_chain.erase(peffect);
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
case 2: {
Expand Down Expand Up @@ -1433,7 +1433,7 @@ bool field::process(Processors::IdleCommand& arg) {
nil_event.event_code = EVENT_FREE_CHAIN;
if(core.set_forced_attack) {
core.set_forced_attack = false;
arg.step = -1;
arg.step = Processors::restart;
emplace_process<Processors::ForcedBattle>();
return FALSE;
}
Expand Down Expand Up @@ -1613,7 +1613,7 @@ bool field::process(Processors::IdleCommand& arg) {
arg.step = 8;
return FALSE;
} else if (ctype == 8) {
arg.step = -1;
arg.step = Processors::restart;
shuffle(infos.turn_player, LOCATION_HAND);
infos.can_shuffle = false;
return FALSE;
Expand Down Expand Up @@ -1644,27 +1644,27 @@ bool field::process(Processors::IdleCommand& arg) {
for(auto& ch : core.current_chain)
ch.triggering_effect->get_handler()->set_status(STATUS_CHAINING, FALSE);
emplace_process<Processors::SolveChain>(false, false, false);
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
case 3: {
adjust_instant();
emplace_process<Processors::PointEvent>(false, false, false);
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
case 5: {
card* target = core.summonable_cards[returns.at<int32_t>(0) >> 16];
core.summon_cancelable = TRUE;
summon(infos.turn_player, target, nullptr, FALSE, 0);
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
case 6: {
card* target = core.spsummonable_cards[returns.at<int32_t>(0) >> 16];
core.summon_cancelable = TRUE;
special_summon_rule(infos.turn_player, target, 0);
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
case 7: {
Expand Down Expand Up @@ -1692,20 +1692,20 @@ bool field::process(Processors::IdleCommand& arg) {
} else
emplace_process<Processors::FlipSummon>(target->current.controler, target);
target->set_status(STATUS_FORM_CHANGED, TRUE);
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
case 8: {
card* target = core.msetable_cards[returns.at<int32_t>(0) >> 16];
core.summon_cancelable = TRUE;
mset(target->current.controler, target, nullptr, FALSE, 0);
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
case 9: {
card* target = core.ssetable_cards[returns.at<int32_t>(0) >> 16];
emplace_process<Processors::SpellSet>(target->current.controler, target->current.controler, target, nullptr);
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
case 10: {
Expand All @@ -1717,7 +1717,7 @@ bool field::process(Processors::IdleCommand& arg) {
for(auto& ch : core.current_chain)
ch.triggering_effect->get_handler()->set_status(STATUS_CHAINING, FALSE);
emplace_process<Processors::SolveChain>(false, false, false);
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
reset_phase(infos.phase);
Expand Down Expand Up @@ -1754,7 +1754,7 @@ bool field::process(Processors::IdleCommand& arg) {
emplace_process<Processors::PointEvent>(false, false, false);
}
target->set_status(STATUS_FORM_CHANGED, TRUE);
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
}
Expand Down Expand Up @@ -1945,7 +1945,7 @@ bool field::process(Processors::BattleCommand& arg) {
for(auto& ch : core.current_chain)
ch.triggering_effect->get_handler()->set_status(STATUS_CHAINING, FALSE);
emplace_process<Processors::SolveChain>(false, false, false);
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
case 3: {
Expand All @@ -1961,7 +1961,7 @@ bool field::process(Processors::BattleCommand& arg) {
returns.set<int32_t>(0, 2);
arg.step = 0;
} else
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
if(core.forced_attack)
Expand Down Expand Up @@ -2060,7 +2060,7 @@ bool field::process(Processors::BattleCommand& arg) {
returns.set<int32_t>(0, 2);
arg.step = 0;
} else
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
if(returns.at<int32_t>(0) == -2)
Expand All @@ -2087,7 +2087,7 @@ bool field::process(Processors::BattleCommand& arg) {
returns.set<int32_t>(0, 2);
arg.step = 0;
} else
arg.step = -1;
arg.step = Processors::restart;
}
return FALSE;
}
Expand Down Expand Up @@ -2202,7 +2202,7 @@ bool field::process(Processors::BattleCommand& arg) {
returns.set<int32_t>(0, 2);
arg.step = 0;
} else
arg.step = -1;
arg.step = Processors::restart;
reset_phase(PHASE_DAMAGE);
adjust_all();
return FALSE;
Expand All @@ -2220,7 +2220,7 @@ bool field::process(Processors::BattleCommand& arg) {
case 15: {
adjust_instant();
emplace_process<Processors::PointEvent>(false, false, false);
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
// EFFECT_MUST_ATTACK_MONSTER where an effect affects more than 1 monster
Expand Down Expand Up @@ -2690,7 +2690,7 @@ bool field::process(Processors::BattleCommand& arg) {
returns.set<int32_t>(0, 2);
arg.step = 0;
} else
arg.step = -1;
arg.step = Processors::restart;
infos.phase = PHASE_BATTLE_STEP;
pduel->new_message(MSG_DAMAGE_STEP_END);
reset_phase(PHASE_DAMAGE);
Expand All @@ -2708,7 +2708,7 @@ bool field::process(Processors::BattleCommand& arg) {
ch.triggering_effect->get_handler()->set_status(STATUS_CHAINING, FALSE);
emplace_process<Processors::SolveChain>(false, false, false);
if(!core.forced_attack)
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
reset_phase(PHASE_BATTLE_STEP);
Expand Down Expand Up @@ -3599,7 +3599,7 @@ bool field::process(Processors::Turn& arg) {
core.new_ochain.clear();
core.quick_f_chain.clear();
core.delayed_quick_tmp.clear();
arg.step = -1;
arg.step = Processors::restart;
arg.turn_player = 1 - arg.turn_player;
return FALSE;
}
Expand Down Expand Up @@ -4316,7 +4316,7 @@ bool field::process(Processors::SolveChain& arg) {
core.subunits.push_back(*std::exchange(core.reserved, std::nullopt));
core.summoning_card = nullptr;
core.summoning_proc_group_type = 0;
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
case 11: {
Expand Down Expand Up @@ -4984,7 +4984,7 @@ bool field::process(Processors::Adjust& arg) {
}
case 16: {
if(core.re_adjust) {
arg.step = -1;
arg.step = Processors::restart;
return FALSE;
}
if(core.shuffle_hand_check[0])
Expand Down
3 changes: 2 additions & 1 deletion processor_unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
#include "containers_fwd.h"

namespace P {
inline constexpr auto restart = static_cast<uint16_t>(~(uint16_t()));
template<bool NA>
struct Process {
static constexpr auto needs_answer = NA;
Process(const Process&) = delete; // non construction-copyable
Process& operator=(const Process&) = delete; // non copyable
Process(Process&&) = default; // construction-movable
Process& operator=(Process&&) = default; // movable
int16_t step;
uint16_t step;
protected:
explicit Process(uint16_t step_) : step(step_) {}
};
Expand Down

0 comments on commit 62c2c5d

Please sign in to comment.