Skip to content

Commit

Permalink
change counter_map to std::map<uint16, uint16> (#597)
Browse files Browse the repository at this point in the history
* change counter_map to std::map<uint16, uint16>

Now temporary/permanent counter is determinded by counter type.

* change returns.svalue to uint16

* add const declaration

* fix add_counter(), remove_counter()

* remove counter when RESET_DISABLE
  • Loading branch information
salix5 authored Jun 18, 2024
1 parent cd23edd commit 301ae50
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 73 deletions.
98 changes: 42 additions & 56 deletions card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ int32 card::get_infos(byte* buf, uint32 query_flag, int32 use_cache) {
if(query_flag & QUERY_COUNTERS) {
buffer_write<int32_t>(p, (int32_t)counters.size());
for (const auto& cmit : counters) {
int32 tdata = cmit.first + ((cmit.second[0] + cmit.second[1]) << 16);
buffer_write<int32_t>(p, tdata);
uint32 tdata = cmit.first + ((uint32)cmit.second << 16);
buffer_write<uint32_t>(p, tdata);
}
}
if (query_flag & QUERY_OWNER) {
Expand Down Expand Up @@ -1998,15 +1998,15 @@ void card::remove_effect(effect* peffect, effect_container::iterator it) {
pduel->game_field->effects.cheff.erase(peffect);
if(peffect->is_flag(EFFECT_FLAG_COUNT_LIMIT))
pduel->game_field->effects.rechargeable.erase(peffect);
if(((peffect->code & 0xf0000) == EFFECT_COUNTER_PERMIT) && (peffect->type & EFFECT_TYPE_SINGLE)) {
if(peffect->get_code_type() == CODE_COUNTER && (peffect->code & 0xf0000) == EFFECT_COUNTER_PERMIT && (peffect->type & EFFECT_TYPE_SINGLE)) {
auto cmit = counters.find(peffect->code & 0xffff);
if(cmit != counters.end()) {
pduel->write_buffer8(MSG_REMOVE_COUNTER);
pduel->write_buffer16(cmit->first);
pduel->write_buffer8(current.controler);
pduel->write_buffer8(current.location);
pduel->write_buffer8(current.sequence);
pduel->write_buffer16(cmit->second[0] + cmit->second[1]);
pduel->write_buffer16(cmit->second);
counters.erase(cmit);
}
}
Expand Down Expand Up @@ -2156,21 +2156,17 @@ void card::reset(uint32 id, uint32 reset_type) {
}
if(id & RESET_DISABLE) {
for(auto cmit = counters.begin(); cmit != counters.end();) {
if(cmit->second[1] > 0) {
pduel->write_buffer8(MSG_REMOVE_COUNTER);
pduel->write_buffer16(cmit->first);
pduel->write_buffer8(current.controler);
pduel->write_buffer8(current.location);
pduel->write_buffer8(current.sequence);
pduel->write_buffer16(cmit->second[1]);
cmit->second[1] = 0;
if(cmit->second[0] == 0)
cmit = counters.erase(cmit);
else
++cmit;
}
else
if ((uint32)cmit->first & COUNTER_WITHOUT_PERMIT) {
++cmit;
continue;
}
pduel->write_buffer8(MSG_REMOVE_COUNTER);
pduel->write_buffer16(cmit->first);
pduel->write_buffer8(current.controler);
pduel->write_buffer8(current.location);
pduel->write_buffer8(current.sequence);
pduel->write_buffer16(cmit->second);
cmit = counters.erase(cmit);
}
}
if(id & RESET_TURN_SET) {
Expand Down Expand Up @@ -2355,35 +2351,28 @@ int32 card::destination_redirect(uint8 destination, uint32 reason) {
}
return 0;
}
// cmit->second[0]: permanent
// cmit->second[1]: reset while negated
int32 card::add_counter(uint8 playerid, uint16 countertype, uint16 count, uint8 singly) {
if(!is_can_add_counter(playerid, countertype, count, singly, 0))
return FALSE;
uint16 cttype = countertype;
auto pr = counters.emplace(cttype, counter_map::mapped_type());
auto pr = counters.emplace(cttype, 0);
auto cmit = pr.first;
if(pr.second) {
cmit->second[0] = 0;
cmit->second[1] = 0;
}
uint16 pcount = count;
int32 pcount = count;
if(singly) {
effect_set eset;
uint16 limit = 0;
int32 limit = 0;
filter_effect(EFFECT_COUNTER_LIMIT + cttype, &eset);
for(int32 i = 0; i < eset.size(); ++i)
limit = eset[i]->get_value();
if (eset.size())
limit = eset.get_last()->get_value();
if(limit) {
uint16 mcount = limit - get_counter(cttype);
if(pcount > mcount)
int32 mcount = limit - get_counter(cttype);
if (mcount < 0)
mcount = 0;
if (pcount > mcount)
pcount = mcount;
}
}
if(countertype & COUNTER_WITHOUT_PERMIT)
cmit->second[0] += pcount;
else
cmit->second[1] += pcount;
cmit->second += pcount;
pduel->write_buffer8(MSG_ADD_COUNTER);
pduel->write_buffer16(cttype);
pduel->write_buffer8(current.controler);
Expand All @@ -2398,22 +2387,20 @@ int32 card::remove_counter(uint16 countertype, uint16 count) {
auto cmit = counters.find(countertype);
if(cmit == counters.end())
return FALSE;
if(cmit->second[1] <= count) {
uint16 remains = count;
remains -= cmit->second[1];
cmit->second[1] = 0;
if(cmit->second[0] <= remains)
counters.erase(cmit);
else cmit->second[0] -= remains;
} else {
cmit->second[1] -= count;
int32 remove_count = count;
if (cmit->second <= count) {
remove_count = cmit->second;
counters.erase(cmit);
}
else {
cmit->second -= count;
}
pduel->write_buffer8(MSG_REMOVE_COUNTER);
pduel->write_buffer16(countertype);
pduel->write_buffer8(current.controler);
pduel->write_buffer8(current.location);
pduel->write_buffer8(current.sequence);
pduel->write_buffer16(count);
pduel->write_buffer16(remove_count);
return TRUE;
}
// return: the player can put a counter on this or not
Expand All @@ -2423,9 +2410,9 @@ int32 card::is_can_add_counter(uint8 playerid, uint16 countertype, uint16 count,
return FALSE;
if (!loc && (!(current.location & LOCATION_ONFIELD) || !is_position(POS_FACEUP)))
return FALSE;
uint32 check = countertype & COUNTER_WITHOUT_PERMIT;
uint32 check = (uint32)countertype & COUNTER_WITHOUT_PERMIT;
if(!check) {
filter_effect(EFFECT_COUNTER_PERMIT + (countertype & 0xffff), &eset);
filter_effect(EFFECT_COUNTER_PERMIT + countertype, &eset);
for(int32 i = 0; i < eset.size(); ++i) {
uint32 prange = eset[i]->range;
if(loc)
Expand All @@ -2439,15 +2426,14 @@ int32 card::is_can_add_counter(uint8 playerid, uint16 countertype, uint16 count,
}
if(!check)
return FALSE;
uint16 cttype = countertype;
int32 limit = -1;
int32 cur = 0;
auto cmit = counters.find(cttype);
if(cmit != counters.end())
cur = cmit->second[0] + cmit->second[1];
filter_effect(EFFECT_COUNTER_LIMIT + cttype, &eset);
for(int32 i = 0; i < eset.size(); ++i)
limit = eset[i]->get_value();
auto cmit = counters.find(countertype);
if (cmit != counters.end())
cur = cmit->second;
filter_effect(EFFECT_COUNTER_LIMIT + countertype, &eset);
if (eset.size())
limit = eset.get_last()->get_value();
if(limit > 0 && (cur + (singly ? 1 : count) > limit))
return FALSE;
return TRUE;
Expand All @@ -2458,7 +2444,7 @@ int32 card::is_can_have_counter(uint16 countertype) {
if (countertype & COUNTER_WITHOUT_PERMIT)
return FALSE;
else {
filter_self_effect(EFFECT_COUNTER_PERMIT + (countertype & 0xffff), &eset);
filter_self_effect(EFFECT_COUNTER_PERMIT + countertype, &eset);
if (current.is_location(LOCATION_ONFIELD)) {
for (int32 i = 0; i < eset.size(); ++i) {
if (eset[i]->is_single_ready())
Expand All @@ -2477,7 +2463,7 @@ int32 card::get_counter(uint16 countertype) {
auto cmit = counters.find(countertype);
if(cmit == counters.end())
return 0;
return cmit->second[0] + cmit->second[1];
return cmit->second;
}
void card::set_material(card_set* materials) {
if(!materials) {
Expand Down
2 changes: 1 addition & 1 deletion card.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class card {
using effect_indexer = std::unordered_map<effect*, effect_container::iterator>;
using effect_relation = std::unordered_set<std::pair<effect*, uint16>, effect_relation_hash>;
using relation_map = std::unordered_map<card*, uint32>;
using counter_map = std::map<uint16, std::array<uint16, 2>>;
using counter_map = std::map<uint16, uint16>;
using effect_count = std::map<uint32, int32>;
class attacker_map : public std::unordered_map<uint16, std::pair<card*, uint32>> {
public:
Expand Down
2 changes: 1 addition & 1 deletion effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ uint32 effect::get_active_type() {
} else
return owner->get_type();
}
int32 effect::get_code_type() {
int32 effect::get_code_type() const {
// start from the highest bit
if (code & 0xf0000000)
return CODE_CUSTOM;
Expand Down
2 changes: 1 addition & 1 deletion effect.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class effect {
void set_activate_location();
void set_active_type();
uint32 get_active_type();
int32 get_code_type();
int32 get_code_type() const;

bool is_flag(effect_flag flag) const {
return !!(this->flag[0] & flag);
Expand Down
2 changes: 1 addition & 1 deletion field.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ constexpr int SIZE_IVALUE = SIZE_RETURN_VALUE / 4;
constexpr int SIZE_LVALUE = SIZE_RETURN_VALUE / 8;
union return_value {
int8 bvalue[SIZE_RETURN_VALUE];
int16 svalue[SIZE_SVALUE];
uint16 svalue[SIZE_SVALUE];
int32 ivalue[SIZE_IVALUE];
int64 lvalue[SIZE_LVALUE];
};
Expand Down
2 changes: 1 addition & 1 deletion libcard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2879,7 +2879,7 @@ int32 scriptlib::card_remove_counter(lua_State *L) {
pduel->write_buffer8(pcard->current.controler);
pduel->write_buffer8(pcard->current.location);
pduel->write_buffer8(pcard->current.sequence);
pduel->write_buffer16(cmit.second[0] + cmit.second[1]);
pduel->write_buffer16(cmit.second);
}
pcard->counters.clear();
return 0;
Expand Down
11 changes: 2 additions & 9 deletions libdebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,9 @@ int32 scriptlib::debug_pre_add_counter(lua_State *L) {
uint32 countertype = (uint32)lua_tointeger(L, 2);
uint32 count = (uint32)lua_tointeger(L, 3);
uint16 cttype = countertype;
auto pr = pcard->counters.emplace(cttype, card::counter_map::mapped_type());
auto pr = pcard->counters.emplace(cttype, 0);
auto cmit = pr.first;
if(pr.second) {
cmit->second[0] = 0;
cmit->second[1] = 0;
}
if(countertype & COUNTER_WITHOUT_PERMIT)
cmit->second[0] += count;
else
cmit->second[1] += count;
cmit->second += count;
return 0;
}
int32 scriptlib::debug_reload_field_begin(lua_State *L) {
Expand Down
4 changes: 2 additions & 2 deletions operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ int32 field::remove_counter(uint16 step, uint32 reason, card* pcard, uint8 rplay
core.select_effects.clear();
if((pcard && pcard->get_counter(countertype) >= count) || (!pcard && get_field_counter(rplayer, s, o, countertype))) {
core.select_options.push_back(10);
core.select_effects.push_back(0);
core.select_effects.push_back(nullptr);
}
auto pr = effects.continuous_effect.equal_range(EFFECT_RCOUNTER_REPLACE + countertype);
tevent e;
Expand All @@ -733,7 +733,7 @@ int32 field::remove_counter(uint16 step, uint32 reason, card* pcard, uint8 rplay
return TRUE;
if(core.select_options.size() == 1)
returns.ivalue[0] = 0;
else if(core.select_effects[0] == 0 && core.select_effects.size() == 2)
else if(core.select_effects[0] == nullptr && core.select_effects.size() == 2)
add_process(PROCESSOR_SELECT_EFFECTYN, 0, 0, (group*)core.select_effects[1]->handler, rplayer, 220);
else
add_process(PROCESSOR_SELECT_OPTION, 0, 0, 0, rplayer, 0);
Expand Down
2 changes: 1 addition & 1 deletion playerop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ int32 field::select_counter(uint16 step, uint8 playerid, uint16 countertype, uin
}
return FALSE;
} else {
uint16 ct = 0;
int32 ct = 0;
for(int32 i = 0; i < (int32)core.select_cards.size(); ++i) {
if(core.select_cards[i]->get_counter(countertype) < returns.svalue[i]) {
pduel->write_buffer8(MSG_RETRY);
Expand Down

0 comments on commit 301ae50

Please sign in to comment.