diff options
Diffstat (limited to 'frontend/cmd')
-rw-r--r-- | frontend/cmd/get.cpp | 21 | ||||
-rw-r--r-- | frontend/cmd/use.cpp | 8 |
2 files changed, 12 insertions, 17 deletions
diff --git a/frontend/cmd/get.cpp b/frontend/cmd/get.cpp index 3e85e57..362830e 100644 --- a/frontend/cmd/get.cpp +++ b/frontend/cmd/get.cpp @@ -1,7 +1,9 @@ +#include <algorithm> #include <memory> #include "../GameController.h" #include "../strings.h" +#include "../util.h" #include "backend/Exception.h" #include "backend/print.h" @@ -14,19 +16,16 @@ using namespace std; void GameController::cmd_get(string & target_name) { Player & player = this->dungeon->get_player(); Location & location = player.get_location(); - unique_ptr<Object> target = nullptr; - for (Object * object : location.get_visible_objects()) { - if (str_lower(object->get_name().c_str()) != str_lower(target_name)) continue; - target = unique_ptr<Object>(object); - location.remove_visible_object(object); - break; - } - if (target == nullptr) + auto range = location.get_visible_objects(); + auto it = find_if(range.begin(), range.end(), by_name_case_insensitive(target_name)); + if (!it) throw Exception("object \"%s\" niet gevonden", target_name.c_str()); + auto object = unique_ptr<Object>(*it); + location.remove_visible_object(object.get()); // gold objects are collected and (implicitly) destroyed - GoldObject * gold = dynamic_cast<GoldObject *>(target.get()); + GoldObject * gold = dynamic_cast<GoldObject *>(object.get()); if (gold != nullptr) { int count = gold->get_count(); player.gold += count; @@ -35,7 +34,7 @@ void GameController::cmd_get(string & target_name) { } // other objects go in the inventory - lprtf("Je voegt %s toe aan je bezit.\n", target->get_name().c_str()); - player.inventory.push_back(target.release()); + lprtf("Je voegt %s toe aan je bezit.\n", object->get_name().c_str()); + player.inventory.push_back(object.release()); } diff --git a/frontend/cmd/use.cpp b/frontend/cmd/use.cpp index c636077..002255a 100644 --- a/frontend/cmd/use.cpp +++ b/frontend/cmd/use.cpp @@ -5,7 +5,7 @@ #include "backend/print.h" #include "../GameController.h" -#include "../strings.h" +#include "../util.h" using namespace std; @@ -13,11 +13,7 @@ void GameController::cmd_use(string & target_name) { Player & player = this->dungeon->get_player(); Location & location = player.get_location(); - auto it = find_if(player.inventory.begin(), player.inventory.end(), - [target_name] (Object * object) { - return str_lower(object->get_name().c_str()) == str_lower(target_name); - } - ); + auto it = find_if(player.inventory.begin(), player.inventory.end(), by_name_case_insensitive(target_name)); if (!it) throw Exception("object \"%s\" niet gevonden", target_name.c_str()); |