From f4d71aa6e241ae59ed7d305e8aadddf9d90b2b46 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 1 Nov 2024 23:26:26 +0100 Subject: make ListIterator compatible with STL algorithm --- frontend/cmd/use.cpp | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'frontend/cmd') 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 + #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(dynamic_cast(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(dynamic_cast(*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); } -- cgit v1.2.3