1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-21 07:09:42 +00:00

Fix Lua remove interacting with restocking items

This commit is contained in:
uramer 2023-11-25 17:39:00 +01:00
parent 47d176e6ed
commit 1841341da2

View file

@ -417,20 +417,23 @@ namespace MWLua
using DelayedRemovalFn = std::function<void(MWWorld::Ptr)>; using DelayedRemovalFn = std::function<void(MWWorld::Ptr)>;
auto removeFn = [](const MWWorld::Ptr ptr, int countToRemove) -> std::optional<DelayedRemovalFn> { auto removeFn = [](const MWWorld::Ptr ptr, int countToRemove) -> std::optional<DelayedRemovalFn> {
int currentCount = ptr.getRefData().getCount(); int rawCount = ptr.getRefData().getCount(false);
int currentCount = std::abs(rawCount);
int signedCountToRemove = (rawCount < 0 ? -1 : 1) * countToRemove;
if (countToRemove <= 0 || countToRemove > currentCount) if (countToRemove <= 0 || countToRemove > currentCount)
throw std::runtime_error("Can't remove " + std::to_string(countToRemove) + " of " throw std::runtime_error("Can't remove " + std::to_string(countToRemove) + " of "
+ std::to_string(currentCount) + " items"); + std::to_string(currentCount) + " items");
ptr.getRefData().setCount(currentCount - countToRemove); // Immediately change count ptr.getRefData().setCount(rawCount - signedCountToRemove); // Immediately change count
if (!ptr.getContainerStore() && currentCount > countToRemove) if (!ptr.getContainerStore() && currentCount > countToRemove)
return std::nullopt; return std::nullopt;
// Delayed action to trigger side effects // Delayed action to trigger side effects
return [countToRemove](MWWorld::Ptr ptr) { return [signedCountToRemove](MWWorld::Ptr ptr) {
// Restore the original count // Restore the original count
ptr.getRefData().setCount(ptr.getRefData().getCount() + countToRemove); ptr.getRefData().setCount(ptr.getRefData().getCount(false) + signedCountToRemove);
// And now remove properly // And now remove properly
if (ptr.getContainerStore()) if (ptr.getContainerStore())
ptr.getContainerStore()->remove(ptr, countToRemove, false); ptr.getContainerStore()->remove(ptr, std::abs(signedCountToRemove), false);
else else
{ {
MWBase::Environment::get().getWorld()->disable(ptr); MWBase::Environment::get().getWorld()->disable(ptr);