hey guys, im kind today and I will share with you how to solve the problem with the corpses/items delays:
@ForlornSkald @dorphs
The problem was not the decay itself, its the duration of the decay, after lot of hours i managed to solve it..
Summary of changes:
Advanced the iterator after using erase to remove items.
Changed the for loop to a while loop to have more control over the iterator advancement.
Properly handled moving items between buckets.
Added iterator advancement when the item was not deleted or moved.
These changes combined ensure that the process of decaying and relocating items across buckets is efficient and does not cause issues with iterators or items falling outside the loop.
The problem is in game.cpp replace this: checkDecay to this one and compile:
void Game::checkDecay()
{
g_scheduler.addEvent(createSchedulerTask(EVENT_DECAYINTERVAL, std::bind(&Game::checkDecay, this)));
size_t bucket = (lastBucket + 1) % EVENT_DECAY_BUCKETS;
auto& currentBucket = decayItems[bucket];
std::vector<Item*> itemsToRemove;
auto it = currentBucket.begin(), end = currentBucket.end();
while (it != end) {
Item* item = *it;
if (!item->canDecay()) {
item->setDecaying(DECAYING_FALSE);
ReleaseItem(item);
it = currentBucket.erase(it); // Eliminar y avanzar el iterador
continue;
}
int32_t duration = item->getDuration();
int32_t decreaseTime = std::min<int32_t>(EVENT_DECAYINTERVAL * EVENT_DECAY_BUCKETS, duration);
duration -= decreaseTime;
item->decreaseDuration(decreaseTime);
if (duration <= 0) {
it = currentBucket.erase(it); // Eliminar y avanzar el iterador
internalDecayItem(item);
ReleaseItem(item);
}
else if (duration < EVENT_DECAYINTERVAL * EVENT_DECAY_BUCKETS) {
it = currentBucket.erase(it); // Eliminar y avanzar el iterador
size_t newBucket = (bucket + ((duration + EVENT_DECAYINTERVAL / 2) / 1000)) % EVENT_DECAY_BUCKETS;
if (newBucket == bucket) {
internalDecayItem(item);
ReleaseItem(item);
}
else {
decayItems[newBucket].push_back(item); // Mover el item a otro bucket
}
}
else {
++it; // Avanzar el iterador cuando no se elimina ni mueve el ítem
}
}
lastBucket = bucket;
cleanup();
}