Skip to content

Commit 11a9ebb

Browse files
committed
ItemManager: Fix extended items from other saves being deleted
Fixes #1913 This works by setting a flag on all items that represents whether or not they are "foreign". We assume all items are foreign by default, and *only* unset this flag if: - the item was initially loaded into the inventory or stash - it was generated during the current game session When saving the extended items file, we now only skip writing the item data if the item is not foreign. Otherwise, it gets preserved. There is still the issue where deleting a save does not clean up the extended items from that save, but that is a separate problem.
1 parent d3d539c commit 11a9ebb

File tree

6 files changed

+20
-2
lines changed

6 files changed

+20
-2
lines changed

src/ItemManager.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ Item::Item()
179179
, has_name(false)
180180
, book_is_readable(true)
181181
, quest_item(false)
182+
, is_foreign(true)
182183
, level(0)
183184
, icon(0)
184185
, max_quantity(INT_MAX)
@@ -1657,6 +1658,8 @@ ItemID ItemManager::getExtendedItem(ItemID item_id) {
16571658

16581659
items[extended_item]->randomizer_def = NULL;
16591660

1661+
items[extended_item]->is_foreign = false;
1662+
16601663
return extended_item;
16611664
}
16621665
else {

src/ItemManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ class Item {
242242
bool has_name; // flag that is set when the item name is parsed
243243
bool book_is_readable; // whether to display "use" or "read" in the tooltip
244244
bool quest_item;
245+
bool is_foreign; // used to track extended items for clean up during save
245246

246247
int level; // rough estimate of quality, used in the loot algorithm
247248
int icon; // icon index on small pixel sheet

src/ItemStorage.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ void ItemStorage::setQuantities(const std::string& s) {
7777
}
7878
}
7979

80+
void ItemStorage::setForeign(bool is_foreign) {
81+
for (int i = 0; i < slot_number; ++i) {
82+
if (items->isValid(storage[i].item) && items->items[storage[i].item]->parent) {
83+
items->items[storage[i].item]->is_foreign = is_foreign;
84+
}
85+
}
86+
}
87+
8088
int ItemStorage::getSlotNumber() {
8189
return slot_number;
8290
}

src/ItemStorage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class ItemStorage {
4242

4343
void setItems(const std::string& s);
4444
void setQuantities(const std::string& s);
45+
void setForeign(bool is_foreign);
4546
int getSlotNumber();
4647
std::string getItems();
4748
std::string getQuantities();

src/SaveLoad.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ void SaveLoad::saveGame() {
294294
}
295295
}
296296

297-
if (!item_in_storage)
297+
if (!item_in_storage && !item->is_foreign)
298298
continue;
299299

300300
outfile << "[item]" << std::endl;
@@ -434,6 +434,7 @@ void SaveLoad::loadGame() {
434434
}
435435
else if (infile.key == "equipped") {
436436
menu->inv->inventory[MenuInventory::EQUIPMENT].setItems(infile.val);
437+
menu->inv->inventory[MenuInventory::EQUIPMENT].setForeign(false);
437438
}
438439
else if (infile.key == "equipped_quantity") {
439440
menu->inv->inventory[MenuInventory::EQUIPMENT].setQuantities(infile.val);
@@ -443,6 +444,7 @@ void SaveLoad::loadGame() {
443444
}
444445
else if (infile.key == "carried") {
445446
menu->inv->inventory[MenuInventory::CARRIED].setItems(infile.val);
447+
menu->inv->inventory[MenuInventory::EQUIPMENT].setForeign(false);
446448
}
447449
else if (infile.key == "carried_quantity") {
448450
menu->inv->inventory[MenuInventory::CARRIED].setQuantities(infile.val);
@@ -665,6 +667,9 @@ void SaveLoad::loadStash() {
665667
while (infile.next()) {
666668
if (infile.key == "item") {
667669
menu->stash->tabs[i].stock.setItems(infile.val);
670+
if (menu->stash->tabs[i].is_private) {
671+
menu->stash->tabs[i].stock.setForeign(false);
672+
}
668673
}
669674
else if (infile.key == "quantity") {
670675
menu->stash->tabs[i].stock.setQuantities(infile.val);

src/Version.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ FLARE. If not, see http://www.gnu.org/licenses/
3030

3131
#include <SDL.h>
3232

33-
Version VersionInfo::ENGINE(1, 14, 221);
33+
Version VersionInfo::ENGINE(1, 14, 222);
3434
Version VersionInfo::MIN(0, 0, 0);
3535
Version VersionInfo::MAX(USHRT_MAX, USHRT_MAX, USHRT_MAX);
3636

0 commit comments

Comments
 (0)