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

fix: house items order being inverted in the stack #2983

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

lamonato29
Copy link
Contributor

@lamonato29 lamonato29 commented Oct 17, 2024

Description

This will fix the actual behaviour.

Behaviour

Actual

Put some items in the house (stacked), like sword -> mace -> shield.
When the server restarts, the stack will be inverted: shield -> mace -> sword.

fix: #2946

@lamonato29 lamonato29 changed the title fix: house items being inverted in the stack. fix: house items order being inverted in the stack. Oct 17, 2024
@kaleohanopahala
Copy link
Contributor

kaleohanopahala commented Oct 18, 2024

Suggestion:
Keep emplace_back instead of insert(items.begin(), item) -> Performance.

With a single reversal after loading, the items are placed in the correct order without the need for multiple insertions that could impact performance. This ensures that you achieve the desired sequence while maintaining efficiency.

Then I added reverse to correct the order obtained by emplace_back.
I would test it like this:

void IOMapSerialize::saveTile(PropWriteStream &stream, std::shared_ptr<Tile> tile) {
	const TileItemVector* tileItems = tile->getItemList();
	if (!tileItems) {
		return;
	}

	std::vector<std::shared_ptr<Item>> items;
	items.reserve(32);

	uint16_t count = 0;
	for (auto &item : *tileItems) {
		if (item->getID() == ITEM_BATHTUB_FILLED_NOTMOVABLE) {
			std::shared_ptr<Item> tub = Item::CreateItem(ITEM_BATHTUB_FILLED);
			items.emplace_back(tub);
			++count;
			continue;
		} else if (!item->isSavedToHouses()) {
			continue;
		}

		items.emplace_back(item);
		++count;
	}

	if (!items.empty()) {
		std::reverse(items.begin(), items.end());

		const Position &tilePosition = tile->getPosition();
		stream.write<uint16_t>(tilePosition.x);
		stream.write<uint16_t>(tilePosition.y);
		stream.write<uint8_t>(tilePosition.z);

		stream.write<uint32_t>(count);
		for (const std::shared_ptr<Item> &item : items) {
			saveItem(stream, item);
		}
	}
}

Copy link

sonarcloud bot commented Oct 18, 2024

@lamonato29
Copy link
Contributor Author

Suggestion: Keep emplace_back instead of insert(items.begin(), item) -> Performance.

With a single reversal after loading, the items are placed in the correct order without the need for multiple insertions that could impact performance. This ensures that you achieve the desired sequence while maintaining efficiency.

Then I added reverse to correct the order obtained by emplace_back. I would test it like this:

void IOMapSerialize::saveTile(PropWriteStream &stream, std::shared_ptr<Tile> tile) {
	const TileItemVector* tileItems = tile->getItemList();
	if (!tileItems) {
		return;
	}

	std::vector<std::shared_ptr<Item>> items;
	items.reserve(32);

	uint16_t count = 0;
	for (auto &item : *tileItems) {
		if (item->getID() == ITEM_BATHTUB_FILLED_NOTMOVABLE) {
			std::shared_ptr<Item> tub = Item::CreateItem(ITEM_BATHTUB_FILLED);
			items.emplace_back(tub);
			++count;
			continue;
		} else if (!item->isSavedToHouses()) {
			continue;
		}

		items.emplace_back(item);
		++count;
	}

	if (!items.empty()) {
		std::reverse(items.begin(), items.end());

		const Position &tilePosition = tile->getPosition();
		stream.write<uint16_t>(tilePosition.x);
		stream.write<uint16_t>(tilePosition.y);
		stream.write<uint8_t>(tilePosition.z);

		stream.write<uint32_t>(count);
		for (const std::shared_ptr<Item> &item : items) {
			saveItem(stream, item);
		}
	}
}

Thx for your suggestion.

@murilo09 murilo09 changed the title fix: house items order being inverted in the stack. fix: house items order being inverted in the stack Oct 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Item Repositioning Issue After Server Restart
3 participants