aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-02 20:57:37 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-02 20:57:37 +0100
commitb4507b3601bedcaa599673b9f9083d1574132157 (patch)
treea25a2f249f3434aa204da2095a26b220de188941
parentc17df7d3e28e0eeb21f7a62d1c66f525b487a5fa (diff)
more fixes
-rw-r--r--backend/Dungeon.cpp2
-rw-r--r--backend/Player.cpp15
-rw-r--r--backend/Player.h1
-rw-r--r--backend/String.cpp4
-rw-r--r--frontend/cmd/equip.cpp20
-rw-r--r--frontend/cmd/hit.cpp3
6 files changed, 25 insertions, 20 deletions
diff --git a/backend/Dungeon.cpp b/backend/Dungeon.cpp
index e1fae39..ce9ba9f 100644
--- a/backend/Dungeon.cpp
+++ b/backend/Dungeon.cpp
@@ -23,7 +23,7 @@ void Dungeon::update_attacks(ListRange<Enemy *> & enemies) {
for (Enemy * enemy : enemies) {
if (enemy->is_dead()) continue;
if (rng.rand_double() < enemy->get_attack()) continue;
- if (!first)
+ if (first)
lprtf(":: De %s in je locatie %s aan! ::\n", plural ? "vijanden" : "vijand", plural ? "vallen" : "valt");
unsigned damage = rng.rand_int(enemy->get_damage());
diff --git a/backend/Player.cpp b/backend/Player.cpp
index 8a44a3a..46d924f 100644
--- a/backend/Player.cpp
+++ b/backend/Player.cpp
@@ -1,6 +1,7 @@
#include <assert.h>
#include "Player.h"
+#include "Exception.h"
#include "print.h"
#include "Dungeon.h"
#include "util.h"
@@ -46,6 +47,20 @@ void Player::set_location(Location & location) {
lprtf("Je staat nu bij de locatie %s.\n", location.get_name().c_str());
}
+void Player::equip(Object * object) {
+ if (object == nullptr) return;
+
+ auto weapon = dynamic_cast<WeaponObject *>(object);
+ if (weapon != nullptr)
+ return this->equip(weapon);
+
+ auto armor = dynamic_cast<ArmorObject *>(object);
+ if (armor != nullptr)
+ return this->equip(armor);
+
+ throw Exception("object \"%s\" is niet draagbaar", object->get_name().c_str());
+}
+
void Player::equip(WeaponObject * weapon) {
if (weapon == nullptr) return;
diff --git a/backend/Player.h b/backend/Player.h
index bf9815c..8090063 100644
--- a/backend/Player.h
+++ b/backend/Player.h
@@ -39,6 +39,7 @@ public:
void set_location(Location &);
void equip(WeaponObject *);
void equip(ArmorObject *);
+ void equip(Object *);
bool is_dead() const;
private:
diff --git a/backend/String.cpp b/backend/String.cpp
index 84dc2ec..8be6892 100644
--- a/backend/String.cpp
+++ b/backend/String.cpp
@@ -95,7 +95,9 @@ bool String::empty() const {
bool operator == (const String & a, const String & b) {
- return strncmp(a._data, b._data, min(a._data_len, b._data_len)) == 0;
+ if (a._data_len != b._data_len) return false;
+ if (a._data_len == 0) return true;
+ return strncmp(a._data, b._data, a._data_len) == 0;
}
bool operator != (const String & a, const String & b) {
diff --git a/frontend/cmd/equip.cpp b/frontend/cmd/equip.cpp
index 8f37c0c..72795a8 100644
--- a/frontend/cmd/equip.cpp
+++ b/frontend/cmd/equip.cpp
@@ -15,23 +15,7 @@ void GameController::cmd_equip(string & target_name) {
throw Exception("object \"%s\" niet gevonden", target_name.c_str());
Object & object = **it;
-
- try {
- WeaponObject & weapon = dynamic_cast<WeaponObject &>(object);
- player.inventory.remove(&weapon);
- player.equip(&weapon);
- this->dungeon->update();
- return;
- } catch (std::bad_cast &) {};
-
- try {
- ArmorObject & armor = dynamic_cast<ArmorObject &>(object);
- player.inventory.remove(&armor);
- player.equip(&armor);
- this->dungeon->update();
- return;
- } catch (std::bad_cast &) {};
-
- throw Exception("object \"%s\" is niet draagbaar", target_name.c_str());
+ player.equip(&object);
+ this->dungeon->update();
}
diff --git a/frontend/cmd/hit.cpp b/frontend/cmd/hit.cpp
index cb5c56e..f4a78fd 100644
--- a/frontend/cmd/hit.cpp
+++ b/frontend/cmd/hit.cpp
@@ -19,6 +19,9 @@ void GameController::cmd_hit(string & target_name) {
throw Exception("vijand \"%s\" niet gevonden", target_name.c_str());
Enemy & enemy = **it;
+ if (enemy.is_dead())
+ throw Exception("%s is al dood!", enemy.get_name().c_str());
+
if (rng.rand_double() > player.get_attack()) {
lprtf("Je hebt gemist!\n");
} else {