Skip to content

Commit

Permalink
Merge branch 'master' into patch-uint8
Browse files Browse the repository at this point in the history
  • Loading branch information
salix5 authored Jun 18, 2024
2 parents d08afe0 + c606699 commit 730106f
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 109 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
11 changes: 8 additions & 3 deletions effectset.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ bool effect_sort_id(const effect* e1, const effect* e2);

struct effect_set {
void add_item(effect* peffect) {
if(count >= 64) return;
if (count >= 64)
return;
container[count++] = peffect;
}
void remove_item(int index) {
if(index >= count)
if (index < 0 || index >= count)
return;
if(index == count - 1) {
--count;
Expand All @@ -45,9 +46,11 @@ struct effect_set {
std::sort(container.begin(), container.begin() + count, effect_sort_id);
}
effect* const& get_last() const {
assert(count);
return container[count - 1];
}
effect*& get_last() {
assert(count);
return container[count - 1];
}
effect* const& operator[] (int index) const {
Expand All @@ -72,7 +75,7 @@ struct effect_set_v {
container.push_back(peffect);
}
void remove_item(int index) {
if(index >= (int)container.size())
if (index < 0 || index >= (int)container.size())
return;
container.erase(container.begin() + index);
}
Expand All @@ -89,9 +92,11 @@ struct effect_set_v {
std::sort(container.begin(), container.begin() + count, effect_sort_id);
}
effect* const& get_last() const {
assert(container.size());
return container.back();
}
effect*& get_last() {
assert(container.size());
return container.back();
}
effect* const& operator[] (int index) const {
Expand Down
64 changes: 32 additions & 32 deletions field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,19 +613,19 @@ int32 field::is_location_useable(uint32 playerid, uint32 location, uint32 sequen
return TRUE;
}
/**
* Return usable count in zone of playerid's MZONE or SZONE(0~4) when uplayer moves pcard to playerid's field (can be negative).
* for LOCATION_MZONE, "usable" means not used, not disabled, satisfying EFFECT_MUST_USE_MZONE, satisfying EFFECT_MAX_MZONE
* for LOCATION_SZONE, "usable" means not used, not disabled, satisfying EFFECT_MAX_SZONE
* Return usable count in `zone` when `uplayer` moves `pcard` to the MZONE or SZONE(0~4) of `playerid` (can be negative).
* For LOCATION_MZONE, "usable" means not used, not disabled, satisfying EFFECT_MUST_USE_MZONE, satisfying EFFECT_MAX_MZONE.
* For LOCATION_SZONE, "usable" means not used, not disabled, satisfying EFFECT_MAX_SZONE.
*
* @param pcard the card about to move
* @param playerid the target player
* @param location LOCATION_MZONE or LOCATION_SZONE
* @param uplayer the request player, PLAYER_NONE means ignoring EFFECT_MUST_USE_MZONE of uplayer, ignoring EFFECT_MAX_MZONE, EFFECT_MAX_SZONE of playerid
* @param reason location reason
* @param zone specified zones, 0xff by default
* @param list storing unavailable or unspecified zones
* @param pcard the card about to move
* @param playerid target player
* @param location LOCATION_MZONE or LOCATION_SZONE
* @param uplayer request player, PLAYER_NONE means ignoring EFFECT_MUST_USE_MZONE of uplayer, ignoring EFFECT_MAX_MZONE, EFFECT_MAX_SZONE of playerid
* @param reason location reason
* @param zone specified zones, default: 0xff
* @param list storing unavailable or unspecified zones
*
* @return usable count in zone of playerid's MZONE or SZONE(0~4) (can be negative)
* @return usable count in the MZONE or SZONE(0~4) of `playerid` (can be negative)
*/
int32 field::get_useable_count(card* pcard, uint8 playerid, uint8 location, uint8 uplayer, uint32 reason, uint32 zone, uint32* list) {
if(location == LOCATION_MZONE && pcard && pcard->current.location == LOCATION_EXTRA)
Expand All @@ -634,8 +634,8 @@ int32 field::get_useable_count(card* pcard, uint8 playerid, uint8 location, uint
return get_useable_count_other(pcard, playerid, location, uplayer, reason, zone, list);
}
/**
* @param pcard the card about to move from Extra Deck (nullptr means any card in Extra Deck)
* @return usable count in zone of playerid's MZONE for pcard
* @param pcard the card about to move from Extra Deck (nullptr means any card in Extra Deck)
* @return usable count in the MZONE of `playerid`
*/
int32 field::get_useable_count_fromex(card* pcard, uint8 playerid, uint8 uplayer, uint32 zone, uint32* list) {
bool use_temp_card = false;
Expand All @@ -654,8 +654,8 @@ int32 field::get_useable_count_fromex(card* pcard, uint8 playerid, uint8 uplayer
return useable_count;
}
/**
* @return the number of available grids in zone of playerid's MZONE for pcard sp_summoned by playerid
* for LOCATION_MZONE, "available" means not used, not disabled, satisfying EFFECT_MUST_USE_MZONE
* @return the number of available slots in `zone` when `pcard` is sp_summoned to the MZONE of `playerid`
* For LOCATION_MZONE, "available" means not used, not disabled, satisfying EFFECT_MUST_USE_MZONE.
*/
int32 field::get_spsummonable_count(card* pcard, uint8 playerid, uint32 zone, uint32* list) {
if(pcard->current.location == LOCATION_EXTRA)
Expand All @@ -664,8 +664,8 @@ int32 field::get_spsummonable_count(card* pcard, uint8 playerid, uint32 zone, ui
return get_tofield_count(pcard, playerid, LOCATION_MZONE, playerid, LOCATION_REASON_TOFIELD, zone, list);
}
/**
* @param pcard the card about to move from Extra Deck (nullptr means any card in Extra Deck)
* @return the number of available grids in zone of playerid's MZONE for pcard
* @param pcard the card about to move from Extra Deck (nullptr means any card in Extra Deck)
* @return the number of available slots in `zone` when `pcard` is sp_summoned to the MZONE of `playerid` by `uplayer`
*/
int32 field::get_spsummonable_count_fromex(card* pcard, uint8 playerid, uint8 uplayer, uint32 zone, uint32* list) {
bool use_temp_card = false;
Expand All @@ -684,7 +684,7 @@ int32 field::get_spsummonable_count_fromex(card* pcard, uint8 playerid, uint8 up
return spsummonable_count;
}
/**
* @return usable count in zone of Main MZONE or SZONE(0~4)
* @return usable count in `zone` of Main MZONE or SZONE(0~4)
*/
int32 field::get_useable_count_other(card* pcard, uint8 playerid, uint8 location, uint8 uplayer, uint32 reason, uint32 zone, uint32* list) {
int32 count = get_tofield_count(pcard, playerid, location, uplayer, reason, zone, list);
Expand All @@ -698,7 +698,7 @@ int32 field::get_useable_count_other(card* pcard, uint8 playerid, uint8 location
return count;
}
/**
* @return the number of available grids in zone of Main MZONE or SZONE(0~4)
* @return the number of available slots in `zone` of Main MZONE or SZONE(0~4)
* for LOCATION_MZONE, "available" means not used, not disabled, satisfying EFFECT_MUST_USE_MZONE
* for LOCATION_SZONE, "available" means not used, not disabled
*/
Expand Down Expand Up @@ -754,11 +754,11 @@ int32 field::get_spsummonable_count_fromex_rule4(card* pcard, uint8 playerid, ui
return count;
}
/**
* @param playerid the target player
* @param uplayer the request player, PLAYER_NONE means ignoring EFFECT_MAX_MZONE
* @param reason location reason
* @param playerid target player
* @param uplayer request player, PLAYER_NONE means ignoring EFFECT_MAX_MZONE
* @param reason location reason
*
* @return the remaining count in playerid's MZONE after applying EFFECT_MAX_MZONE (can be negative).
* @return the remaining count in the MZONE of `playerid` after applying EFFECT_MAX_MZONE (can be negative).
*/
int32 field::get_mzone_limit(uint8 playerid, uint8 uplayer, uint32 reason) {
uint32 used_flag = player[playerid].used_location;
Expand Down Expand Up @@ -787,11 +787,11 @@ int32 field::get_mzone_limit(uint8 playerid, uint8 uplayer, uint32 reason) {
return limit;
}
/**
* @param playerid the target player
* @param uplayer the request player, PLAYER_NONE means ignoring EFFECT_MAX_SZONE
* @param reason location reason
* @param playerid target player
* @param uplayer request player, PLAYER_NONE means ignoring EFFECT_MAX_SZONE
* @param reason location reason
*
* @return the remaining count in playerid's SZONE(0~4) after applying EFFECT_MAX_SZONE.
* @return the remaining count in the SZONE(0~4) of `playerid` after applying EFFECT_MAX_SZONE.
*/
int32 field::get_szone_limit(uint8 playerid, uint8 uplayer, uint32 reason) {
uint32 used_flag = player[playerid].used_location;
Expand Down Expand Up @@ -835,11 +835,11 @@ uint32 field::get_rule_zone_fromex(int32 playerid, card* pcard) {
}
}
/**
* @param playerid the target player
* @param uplayer the request player, PLAYER_NONE means ignoring EFFECT_MUST_USE_MZONE of uplayer
* @param reason location reason
* @param pcard the card about to move
* @param flag storing the zones in MZONE blocked by EFFECT_MUST_USE_MZONE
* @param playerid target player
* @param uplayer request player, PLAYER_NONE means ignoring EFFECT_MUST_USE_MZONE of uplayer
* @param reason location reason
* @param pcard the card about to move
* @param flag storing the zones in MZONE blocked by EFFECT_MUST_USE_MZONE
*/
void field::filter_must_use_mzone(uint8 playerid, uint8 uplayer, uint32 reason, card* pcard, uint32* flag) {
effect_set eset;
Expand Down
Loading

0 comments on commit 730106f

Please sign in to comment.