aboutsummaryrefslogtreecommitdiff
path: root/frontend/cmd/equip.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/cmd/equip.cpp')
-rw-r--r--frontend/cmd/equip.cpp46
1 files changed, 24 insertions, 22 deletions
diff --git a/frontend/cmd/equip.cpp b/frontend/cmd/equip.cpp
index 9d2a006..fe951b1 100644
--- a/frontend/cmd/equip.cpp
+++ b/frontend/cmd/equip.cpp
@@ -3,33 +3,35 @@
#include "backend/Exception.h"
#include "../GameController.h"
-#include "../strings.h"
+#include "../util.h"
using namespace std;
void GameController::cmd_equip(string & target_name) {
Player & player = this->dungeon->get_player();
- for (Object * object : player.inventory) {
- if (str_lower(object->get_name().c_str()) != str_lower(target_name)) continue;
-
- WeaponObject * weapon = dynamic_cast<WeaponObject *>(object);
- if (weapon != nullptr) {
- player.inventory.remove(weapon);
- player.equip(weapon);
- this->dungeon->update();
- return;
- }
-
- ArmorObject * armor = dynamic_cast<ArmorObject *>(object);
- if (armor != nullptr) {
- player.inventory.remove(armor);
- player.equip(armor);
- this->dungeon->update();
- return;
- }
-
- throw Exception("object \"%s\" is niet draagbaar", target_name.c_str());
+
+ 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;
+
+ WeaponObject * weapon = dynamic_cast<WeaponObject *>(&object);
+ if (weapon != nullptr) {
+ player.inventory.remove(weapon);
+ player.equip(weapon);
+ this->dungeon->update();
+ return;
}
- throw Exception("object \"%s\" niet gevonden", target_name.c_str());
+
+ ArmorObject * armor = dynamic_cast<ArmorObject *>(&object);
+ if (armor != nullptr) {
+ player.inventory.remove(armor);
+ player.equip(armor);
+ this->dungeon->update();
+ return;
+ }
+
+ throw Exception("object \"%s\" is niet draagbaar", target_name.c_str());
}