aboutsummaryrefslogtreecommitdiff
path: root/frontend/cmd
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-01 23:26:26 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-01 23:26:26 +0100
commitf4d71aa6e241ae59ed7d305e8aadddf9d90b2b46 (patch)
tree06ac7c54936c3ccc3b086428630bc6d1a3399cab /frontend/cmd
parent07796bea15a2d5f43766f062379b63fc9e9e1b5d (diff)
make ListIterator compatible with STL algorithm
Diffstat (limited to 'frontend/cmd')
-rw-r--r--frontend/cmd/use.cpp27
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);
}