From 7189e7d0d5130b7bb37c4d65e595b1608b3c39c8 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sun, 12 Feb 2017 15:27:02 -0500 Subject: quest requirements/rewards --- include/quest.hpp | 5 +++++ src/components.cpp | 2 +- src/inventory.cpp | 3 ++- src/quest.cpp | 39 ++++++++++++++++++++++++++++++++++----- xml/!town.xml | 13 ++++++++++--- 5 files changed, 52 insertions(+), 10 deletions(-) diff --git a/include/quest.hpp b/include/quest.hpp index 27b533d..b74438d 100644 --- a/include/quest.hpp +++ b/include/quest.hpp @@ -7,6 +7,7 @@ #include +#include #include #include @@ -21,6 +22,10 @@ struct Quest std::string name; /**< the quest's title */ std::string desc; /**< the quest's description */ + + using Req = std::pair; + std::forward_list reqs; /**< the quest's item requirements */ + std::forward_list rewards; /**< the quest's rewards */ }; /** diff --git a/src/components.cpp b/src/components.cpp index cbec9fc..dfce75c 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -257,7 +257,7 @@ void DialogSystem::receive(const MouseClickEvent &mce) else { qname = qxml->Attribute("check"); if (qname != nullptr) { - if (qname != nullptr && qsys->hasQuest(qname) && qsys->finish(qname) == 0) { + if (qname != nullptr && qsys->finish(qname) == 0) { d.index = 9999; } else { ui::dialogBox(name.name, "", false, "Finish my quest u nug"); diff --git a/src/inventory.cpp b/src/inventory.cpp index bf6b6bb..3995e3a 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -240,11 +240,12 @@ bool InventorySystem::take(const std::string& name, int count) return ie.item != nullptr && ie.item->name == name; }); - if (i == items.end()) + if (i >= items.end()) break; toDelete.push_front(i); taken += i->count; + next = i + 1; } if (taken < count) diff --git a/src/quest.cpp b/src/quest.cpp index a0f9a6d..71a37fd 100644 --- a/src/quest.cpp +++ b/src/quest.cpp @@ -2,8 +2,20 @@ #include +#include +#include #include +std::string& trim(std::string& s) +{ + auto start = std::find_if(s.begin(), s.end(), + [](char c) { return !isspace(c); }) - s.begin(); + auto end = std::find_if(s.rbegin(), s.rend(), + [](char c) { return !isspace(c); }).base() - s.begin(); + s = s.substr(start, end - start); + return s; +} + void QuestSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) { (void)en; @@ -13,14 +25,25 @@ void QuestSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, int QuestSystem::assign(std::string title, std::string desc, std::string req) { + current.emplace_back(title, desc); + + auto* list = ¤t.back().reqs; + std::string reqTitle; for (auto s : tokens(req, ',')) { - s.erase(std::remove_if(s.begin(), s.end(), - [](char c) { return isspace(c); }), s.end()); + trim(s); - std::cout << s << '\n'; + if (s == "Reward") { + list = ¤t.back().rewards; + } else { + if (!reqTitle.empty()) { + list->emplace_front(reqTitle, std::stoi(s)); + reqTitle.clear(); + } else { + reqTitle = s; + } + } } - current.emplace_back(title, desc); return 0; } @@ -39,7 +62,13 @@ int QuestSystem::finish(std::string title) if (quest == std::end(current)) return -1; - // TODO requirements + for (const auto& r : quest->reqs) { + if (!game::engine.getSystem()->take(r.first, r.second)) + return -1; + } + + for (const auto& r : quest->rewards) + game::engine.getSystem()->add(r.first, r.second); drop(title); diff --git a/xml/!town.xml b/xml/!town.xml index 9ab4c1c..d256af9 100644 --- a/xml/!town.xml +++ b/xml/!town.xml @@ -7,6 +7,7 @@ Sunny -300 + @@ -15,20 +16,26 @@ - + Hey there! The name's Bob. Good to see you've finally woken up from your nap by the cliff there... lol - + - Debug, 5 + Debug, 5, + Reward, + Dank MayMay, 50 Looks like you've got yourself pretty empty handed... you know, I have a simple solution for that. Come on inside, I have somethin' to show you. + + + Hooray! + -- cgit v1.2.3