Skip to content

Commit

Permalink
Restructure the processor handling to use a variant object (#149)
Browse files Browse the repository at this point in the history
Rewrite the whole ``process`` function of the processor to use instead tag dispatching on variant objects.
Each single processor message got rewritten as a struct, inheriting from a base templated Process struct, containing only the required data for that process, rather than using the generic structure that ended up needing to spam bitwise operations to pack as many arguments in a byte as possible. This makes things easier to follow and streamlines the addition of new processes:
 - Define its struct with the parameters it expects by also providing a constructor
   - If the process requires an answer, inherit it from ``Process<true>``, if it requires no answer, inherit it from ``Process<false>``
 - Add such struct to the ``processors`` variant type
 - Declare and implement its corresponding ``bool process(Processors::NewProcess& arg);`` function
  • Loading branch information
edo9300 authored Jan 8, 2024
1 parent 35f83fa commit e97a1c2
Show file tree
Hide file tree
Showing 13 changed files with 2,583 additions and 2,335 deletions.
1 change: 1 addition & 0 deletions Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ LOCAL_SRC_FILES := card.cpp \
operations.cpp \
playerop.cpp \
processor.cpp \
processor_visit.cpp \
scriptlib.cpp

LOCAL_CFLAGS := -pedantic -Wextra -fvisibility=hidden -DOCGCORE_EXPORT_FUNCTIONS
Expand Down
6 changes: 3 additions & 3 deletions field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2079,7 +2079,7 @@ void field::adjust_self_destroy_set() {
}
std::sort(uniq_set.begin(), uniq_set.end(), [](card* lhs, card* rhs) { return lhs->fieldid < rhs->fieldid; });
for(auto& pcard : uniq_set) {
add_process(PROCESSOR_SELF_DESTROY, 0, 0, 0, p, 0, 0, 0, pcard);
emplace_process<Processors::SelfDestroyUnique>(pcard, p);
core.unique_destroy_set.insert(pcard);
}
p = 1 - p;
Expand Down Expand Up @@ -2110,9 +2110,9 @@ void field::adjust_self_destroy_set() {
}
}
if(!core.self_destroy_set.empty())
add_process(PROCESSOR_SELF_DESTROY, 10, 0, 0, 0, 0);
emplace_process<Processors::SelfDestroy>();
if(!core.self_tograve_set.empty())
add_process(PROCESSOR_SELF_DESTROY, 20, 0, 0, 0, 0);
emplace_process<Processors::SelfToGrave>();
}
void field::erase_grant_effect(effect* peffect) {
auto eit = effects.grant_effect.find(peffect);
Expand Down
379 changes: 139 additions & 240 deletions field.h

Large diffs are not rendered by default.

82 changes: 41 additions & 41 deletions libduel.cpp

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions libgroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ LUA_FUNCTION(FilterSelect) {
if(pduel->lua->check_matching(pcard, findex, extraargs))
pduel->game_field->core.select_cards.push_back(pcard);
}
pduel->game_field->add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, playerid + (cancelable << 16), min + (max << 16));
pduel->game_field->emplace_process<Processors::SelectCard>(playerid, cancelable, min, max);
return push_return_cards(L, cancelable);
}
LUA_FUNCTION(Select) {
Expand All @@ -270,7 +270,7 @@ LUA_FUNCTION(Select) {
auto min = lua_get<uint16_t>(L, 3);
auto max = lua_get<uint16_t>(L, 4);
pduel->game_field->core.select_cards.assign(cset.begin(), cset.end());
pduel->game_field->add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, playerid + (cancelable << 16), min + (max << 16));
pduel->game_field->emplace_process<Processors::SelectCard>(playerid, cancelable, min, max);
return push_return_cards(L, cancelable);
}
LUA_FUNCTION(SelectUnselect) {
Expand Down Expand Up @@ -307,7 +307,7 @@ LUA_FUNCTION(SelectUnselect) {
pduel->game_field->core.unselect_cards.assign(pgroup2->container.begin(), pgroup2->container.end());
else
pduel->game_field->core.unselect_cards.clear();
pduel->game_field->add_process(PROCESSOR_SELECT_UNSELECT_CARD, 0, 0, 0, playerid + (cancelable << 16), min + (max << 16), finishable);
pduel->game_field->emplace_process<Processors::SelectUnselectCard>(playerid, cancelable, min, max, finishable);
return yieldk({
if(pduel->game_field->return_cards.canceled)
lua_pushnil(L);
Expand Down Expand Up @@ -427,7 +427,7 @@ LUA_FUNCTION(SelectWithSumEqual) {
interpreter::pushobject(L, empty_group);
return 1;
}
pduel->game_field->add_process(PROCESSOR_SELECT_SUM, 0, 0, 0, acc, playerid + (min << 16) + (max << 24));
pduel->game_field->emplace_process<Processors::SelectSum>(playerid, acc, min, max);
return yieldk({
group* pgroup = pduel->new_group(pduel->game_field->return_cards.list);
pduel->game_field->core.must_select_cards.clear();
Expand Down Expand Up @@ -481,7 +481,7 @@ LUA_FUNCTION(SelectWithSumGreater) {
interpreter::pushobject(L, empty_group);
return 1;
}
pduel->game_field->add_process(PROCESSOR_SELECT_SUM, 0, 0, 0, acc, playerid);
pduel->game_field->emplace_process<Processors::SelectSum>(playerid, acc, 0, 0);
return yieldk({
group* pgroup = pduel->new_group(pduel->game_field->return_cards.list);
pduel->game_field->core.must_select_cards.clear();
Expand Down
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ ocgcore_src = files([
'operations.cpp',
'playerop.cpp',
'processor.cpp',
'processor_visit.cpp',
'scriptlib.cpp',
])

Expand Down
6 changes: 3 additions & 3 deletions ocgapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,17 @@ OCGAPI void OCG_DuelNewCard(OCG_Duel ocg_duel, OCG_NewCardInfo info) {

OCGAPI void OCG_StartDuel(OCG_Duel ocg_duel) {
auto* pduel = static_cast<duel*>(ocg_duel);
pduel->game_field->add_process(PROCESSOR_STARTUP, 0, 0, 0, 0, 0);
pduel->game_field->emplace_process<Processors::Startup>();
}

OCGAPI int OCG_DuelProcess(OCG_Duel ocg_duel) {
auto* pduel = static_cast<duel*>(ocg_duel);
pduel->buff.clear();
auto flag = 0;
auto flag = OCG_DUEL_STATUS_END;
do {
flag = pduel->game_field->process();
pduel->generate_buffer();
} while(pduel->buff.size() == 0 && flag == PROCESSOR_FLAG_CONTINUE);
} while(pduel->buff.size() == 0 && flag == OCG_DUEL_STATUS_CONTINUE);
return flag;
}

Expand Down
1,543 changes: 803 additions & 740 deletions operations.cpp

Large diffs are not rendered by default.

Loading

0 comments on commit e97a1c2

Please sign in to comment.