aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2017-09-20 11:25:26 -0400
committerClyne Sullivan <tullivan99@gmail.com>2017-09-20 11:25:26 -0400
commit8720f1f253b55fa5233626dd854671a5925d65de (patch)
tree8f6017a662288d65e5265a730a347301e596e81a /src
parentf7ec6aff4a7312586e986c891f370c442a6ae093 (diff)
inventory remembers slot positions
Diffstat (limited to 'src')
-rw-r--r--src/inventory.cpp53
-rw-r--r--src/systems/dialog.cpp6
2 files changed, 33 insertions, 26 deletions
diff --git a/src/inventory.cpp b/src/inventory.cpp
index 1fdaf63..36cc041 100644
--- a/src/inventory.cpp
+++ b/src/inventory.cpp
@@ -320,30 +320,34 @@ bool InventorySystem::receive(const KeyDownEvent &kde)
return true;
}
-void InventorySystem::add(const std::string& name, int count)
+void InventorySystem::add(const std::string& name, int count, int slot)
{
- auto i = std::find_if(items.begin(), items.end(),
- [&name](const InventoryEntry& ie) {
- // either matching item that isn't filled, or empty slow
- return (ie.item != nullptr && ie.item->name == name && ie.count < ie.item->stackSize) || ie.count == 0;
- });
+ auto& ie = items[slot];
- if (i != items.end()) {
- auto& ie = *i;
+ if (isGoodEntry(ie)) {
+ if (ie.item->name != name)
+ return; // TODO behavior?
+ ie.count += count;
+ } else {
+ ie.item = &itemList[name];
+ ie.count = count;
+ }
- // update the slot
- if (!isGoodEntry(ie)) {
- ie.item = &itemList[name];
- ie.count = count;
- } else {
- ie.count += count;
- }
+ if (ie.count > ie.item->stackSize) {
+ int diff = ie.count - ie.item->stackSize;
+ ie.count = ie.item->stackSize;
+ add(name, diff);
+ }
+}
- // handle overflow
- if (ie.count > ie.item->stackSize) {
- int diff = ie.count - ie.item->stackSize;
- ie.count = ie.item->stackSize;
- add(name, diff);
+void InventorySystem::add(const std::string& name, int count)
+{
+ for (unsigned int i = 0; i < items.size(); i++) {
+ if ((items[i].item != nullptr && items[i].item->name == name
+ && items[i].count < items[i].item->stackSize)
+ || items[i].count == 0) {
+ add(name, count, static_cast<int>(i));
+ break;
}
}
}
@@ -395,9 +399,11 @@ bool InventorySystem::save(void)
// signature?
s << "831998\n";
+ int idx = 0;
for (const auto& i : items) {
if (i.item != nullptr && i.count > 0)
- s << std::string(i.item->name) << '\n' << i.count << '\n';
+ s << std::string(i.item->name) << '\n' << i.count << '\n' << idx << '\n';
+ idx++;
}
// end of list?
@@ -417,11 +423,12 @@ void InventorySystem::load(void)
if (std::stoi(lines[0]) != 831998)
UserError("Save file signature is invalid... (delete it)");
- for (unsigned int i = 1; i < lines.size(); i += 2) {
+ for (unsigned int i = 1; i < lines.size(); i += 3) {
if (lines[i].size() > 0) {
int count = std::stoi(lines[i + 1]);
+ int slot = std::stoi(lines[i + 2]);
if (count > 0)
- add(lines[i], count);
+ add(lines[i], count, slot);
}
}
}
diff --git a/src/systems/dialog.cpp b/src/systems/dialog.cpp
index f4f479f..3c4b4fc 100644
--- a/src/systems/dialog.cpp
+++ b/src/systems/dialog.cpp
@@ -42,12 +42,12 @@ bool DialogSystem::receive(const MouseClickEvent &mce)
if (!dialogRun.load()) {
// copy entity, windows destroys the original after thread detach
+ dialogRun.store(true);
std::thread([e, &pos, &dim, &d, &name] {
std::string questAssignedText;
int newIndex;
auto exml = WorldSystem::getXML()->FirstChildElement("Dialog");
- dialogRun.store(true);
if (e.has_component<Direction>())
d.talking = true;
@@ -100,7 +100,7 @@ bool DialogSystem::receive(const MouseClickEvent &mce)
} else {
UISystem::dialogBox(name.name, "Finish my quest u nug");
UISystem::waitForDialog();
- return;
+ goto END;
}
// oldidx = d.index;
// d.index = qxml->UnsignedAttribute("fail");
@@ -139,7 +139,7 @@ bool DialogSystem::receive(const MouseClickEvent &mce)
else if (exml->QueryIntAttribute("nextid", &newIndex) == XML_NO_ERROR)
d.index = newIndex;
}
-
+END:
d.talking = false;
dialogRun.store(false);
}).detach();