aboutsummaryrefslogtreecommitdiff
path: root/frontend/cmd/use.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/cmd/use.cpp')
-rw-r--r--frontend/cmd/use.cpp18
1 files changed, 10 insertions, 8 deletions
diff --git a/frontend/cmd/use.cpp b/frontend/cmd/use.cpp
index 3134210..78d8233 100644
--- a/frontend/cmd/use.cpp
+++ b/frontend/cmd/use.cpp
@@ -1,4 +1,4 @@
-#include <algorithm>
+#include <memory>
#include "backend/ConsumableObject.h"
#include "backend/Exception.h"
@@ -14,16 +14,18 @@ void GameController::cmd_use(string & target_name) {
Location & location = player.get_location();
auto it = find_if_range(player.inventory.range(), by_name_case_insensitive(target_name));
-
if (!it)
throw Exception("object \"%s\" niet gevonden", target_name.c_str());
+ Object & object = **it;
- auto consumable = unique_ptr<ConsumableObject>(dynamic_cast<ConsumableObject *>(*it));
- if (consumable == nullptr)
- throw Exception("%s is niet consumeerbaar", (*it)->get_name().c_str());
+ try {
+ auto consumable = unique_ptr<ConsumableObject>(&dynamic_cast<ConsumableObject &>(object));
+ player.inventory.remove(consumable.get());
- lprtf("Je drinkt %s.\n", consumable->get_displayname().c_str());
- player.inventory.remove(consumable.get());
- consumable->consume(player);
+ lprtf("Je drinkt %s.\n", consumable->get_displayname().c_str());
+ consumable->consume(player);
+ } catch (std::bad_cast &) {
+ throw Exception("%s is niet consumeerbaar", object.get_name().c_str());
+ }
}