Skip to content

Commit

Permalink
Fix EFFECT_COUNTER_LIMIT handling
Browse files Browse the repository at this point in the history
In case of multiple of those effects being applied, the value of the latest applied was being used, rather than the smallest of them all
  • Loading branch information
edo9300 committed Feb 14, 2024
1 parent 62c2c5d commit 935d17c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
22 changes: 12 additions & 10 deletions card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <algorithm> //std::sort
#include <algorithm> //std::sort, std::min
#include <cstring> //std::memcpy
#include <utility> //std::pair, std::make_pair, std::swap
#include <set>
Expand Down Expand Up @@ -2302,7 +2302,7 @@ int32_t card::destination_redirect(uint8_t destination, uint32_t /*reason*/) {
}
// cmit->second[0]: permanent
// cmit->second[1]: reset while negated
int32_t card::add_counter(uint8_t playerid, uint16_t countertype, uint16_t count, uint8_t singly) {
int32_t card::add_counter(uint8_t playerid, uint16_t countertype, uint16_t count, bool singly) {
if(!is_can_add_counter(playerid, countertype, count, singly, 0))
return FALSE;
uint16_t cttype = countertype & ~COUNTER_NEED_ENABLE;
Expand All @@ -2315,12 +2315,12 @@ int32_t card::add_counter(uint8_t playerid, uint16_t countertype, uint16_t count
uint16_t pcount = count;
if(singly) {
effect_set eset;
uint16_t limit = 0;
auto limit = ~uint16_t{} + 1;
filter_effect(EFFECT_COUNTER_LIMIT + cttype, &eset);
for(const auto& peffect : eset)
limit = peffect->get_value();
if(limit) {
uint16_t mcount = limit - get_counter(cttype);
limit = std::min<int>(static_cast<uint16_t>(peffect->get_value()), limit);
if(limit != (~uint16_t{} + 1)) {
uint16_t mcount = static_cast<uint16_t>(limit) - get_counter(cttype);
if(pcount > mcount)
pcount = mcount;
}
Expand Down Expand Up @@ -2361,7 +2361,7 @@ int32_t card::remove_counter(uint16_t countertype, uint16_t count) {
message->write<uint16_t>(count);
return TRUE;
}
int32_t card::is_can_add_counter(uint8_t playerid, uint16_t countertype, uint16_t count, uint8_t singly, uint32_t loc) {
int32_t card::is_can_add_counter(uint8_t playerid, uint16_t countertype, uint16_t count, bool singly, uint32_t loc) {
effect_set eset;
if(count > 0) {
if(!pduel->game_field->is_player_can_place_counter(playerid, this, countertype, count))
Expand Down Expand Up @@ -2396,15 +2396,17 @@ int32_t card::is_can_add_counter(uint8_t playerid, uint16_t countertype, uint16_
if(!check)
return FALSE;
uint16_t cttype = countertype & ~COUNTER_NEED_ENABLE;
int32_t limit = -1;
auto limit = ~uint16_t{} + 1;
int32_t 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(const auto& peffect : eset)
limit = peffect->get_value();
if(limit > 0 && (cur + (singly ? 1 : count) > limit))
limit = std::min<int>(static_cast<uint16_t>(peffect->get_value()), limit);
if(singly)
count = 1;
if((limit != (~uint16_t{} + 1)) && (cur + count > limit))
return FALSE;
return TRUE;
}
Expand Down
4 changes: 2 additions & 2 deletions card.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ class card : public lua_obj_helper<LuaParam::CARD> {
void release_relation(effect* peffect);
int32_t leave_field_redirect(uint32_t reason);
int32_t destination_redirect(uint8_t destination, uint32_t reason);
int32_t add_counter(uint8_t playerid, uint16_t countertype, uint16_t count, uint8_t singly);
int32_t add_counter(uint8_t playerid, uint16_t countertype, uint16_t count, bool singly);
int32_t remove_counter(uint16_t countertype, uint16_t count);
int32_t is_can_add_counter(uint8_t playerid, uint16_t countertype, uint16_t count, uint8_t singly, uint32_t loc);
int32_t is_can_add_counter(uint8_t playerid, uint16_t countertype, uint16_t count, bool singly, uint32_t loc);
int32_t get_counter(uint16_t countertype);
void set_material(card_set materials);
void add_card_target(card* pcard);
Expand Down

0 comments on commit 935d17c

Please sign in to comment.