diff options
Diffstat (limited to 'frontend/cmd')
-rw-r--r-- | frontend/cmd/use.cpp | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/frontend/cmd/use.cpp b/frontend/cmd/use.cpp index 36824b5..c636077 100644 --- a/frontend/cmd/use.cpp +++ b/frontend/cmd/use.cpp @@ -1,3 +1,5 @@ +#include <algorithm> + #include "backend/ConsumableObject.h" #include "backend/Exception.h" #include "backend/print.h" @@ -10,19 +12,22 @@ using namespace std; void GameController::cmd_use(string & target_name) { Player & player = this->dungeon->get_player(); Location & location = player.get_location(); - for (Object * object : player.inventory) { - if (str_lower(object->get_name().c_str()) != str_lower(target_name)) continue; + + 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 consumable = unique_ptr<ConsumableObject>(dynamic_cast<ConsumableObject *>(object)); - if (consumable == nullptr) - throw Exception("%s is niet consumeerbaar", object->get_name().c_str()); + if (!it) + throw Exception("object \"%s\" niet gevonden", target_name.c_str()); - lprtf("Je drinkt %s.\n", object->get_displayname().c_str()); - player.inventory.remove(object); - consumable->consume(player); - return; - } + auto consumable = unique_ptr<ConsumableObject>(dynamic_cast<ConsumableObject *>(*it)); + if (consumable == nullptr) + throw Exception("%s is niet consumeerbaar", (*it)->get_name().c_str()); - throw Exception("object \"%s\" niet gevonden", target_name.c_str()); + lprtf("Je drinkt %s.\n", consumable->get_displayname().c_str()); + player.inventory.remove(consumable.get()); + consumable->consume(player); } |