Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dont recreate stackables every move #4761

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1173,8 +1173,6 @@ ReturnValue Game::internalMoveItem(Cylinder* fromCylinder, Cylinder* toCylinder,
m = maxQueryCount;
}

Item* moveItem = item;

// check if we can remove this item
ret = fromCylinder->queryRemove(*item, m, flags, actor);
if (ret != RETURNVALUE_NOERROR) {
Expand All @@ -1196,6 +1194,8 @@ ReturnValue Game::internalMoveItem(Cylinder* fromCylinder, Cylinder* toCylinder,
}
}

Item* moveItem = item;

// remove the item
int32_t itemIndex = fromCylinder->getThingIndex(item);
Item* updateItem = nullptr;
Expand All @@ -1214,20 +1214,22 @@ ReturnValue Game::internalMoveItem(Cylinder* fromCylinder, Cylinder* toCylinder,
}

int32_t newCount = m - n;
if (newCount > 0) {
if (newCount == item->getItemCount()) {
// full item is moved (move count is the same as item count)
} else if (newCount > 0) {
moveItem = item->clone();
moveItem->setItemCount(newCount);
} else {
if (item->isRemoved()) {
ReleaseItem(item);
}

moveItem = nullptr;
}
Comment on lines +1217 to 1228
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming the new count can never be negative, how about this?

Suggested change
if (newCount == item->getItemCount()) {
// full item is moved (move count is the same as item count)
} else if (newCount > 0) {
moveItem = item->clone();
moveItem->setItemCount(newCount);
} else {
if (item->isRemoved()) {
ReleaseItem(item);
}
moveItem = nullptr;
}
assert(newCount >= 0);
if (newCount != item->getItemCount()) {
moveItem = item->clone();
moveItem->setItemCount(newCount);
} else if (newCount == 0) {
if (item->isRemoved()) {
ReleaseItem(item);
}
moveItem = nullptr;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attention: the code above may cause duplication bugs that I couldn't trace. Please don't apply it without testing beforehand.


if (item->isRemoved()) {
ReleaseItem(item);
}
}

// add item
if (moveItem /*m - n > 0*/) {
if (moveItem) {
toCylinder->addThing(index, moveItem);
}

Expand Down
Loading