diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-02 22:19:15 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-02 22:19:15 +0100 |
commit | 07b8a5b0baed8c7b23681c99f25f297045945bfc (patch) | |
tree | 8383b4b675aba53d19a667f4713c3e73c32e9652 | |
parent | d554cb39a2f1ec97dd214a5dd200ae5f03cf616d (diff) |
fix more bugs
-rw-r--r-- | backend/ArmorObject.cpp | 5 | ||||
-rw-r--r-- | backend/ArmorObject.h | 2 | ||||
-rw-r--r-- | backend/Enemy.cpp | 6 | ||||
-rw-r--r-- | backend/Enemy.h | 2 | ||||
-rw-r--r-- | backend/GoldObject.cpp | 5 | ||||
-rw-r--r-- | backend/GoldObject.h | 2 | ||||
-rw-r--r-- | backend/Object.cpp | 5 | ||||
-rw-r--r-- | backend/Object.h | 2 | ||||
-rw-r--r-- | backend/RNG.cpp | 4 | ||||
-rw-r--r-- | backend/RNG.h | 1 | ||||
-rw-r--r-- | backend/WeaponObject.cpp | 5 | ||||
-rw-r--r-- | backend/WeaponObject.h | 2 | ||||
-rw-r--r-- | frontend/cmd/hit.cpp | 6 | ||||
-rw-r--r-- | frontend/generate_dungeon.cpp | 23 |
14 files changed, 43 insertions, 27 deletions
diff --git a/backend/ArmorObject.cpp b/backend/ArmorObject.cpp index eb805e0..3e0a4e2 100644 --- a/backend/ArmorObject.cpp +++ b/backend/ArmorObject.cpp @@ -7,8 +7,7 @@ int ArmorObject::get_protection() const { return this->protection; } -const String & ArmorObject::get_displayname() const { - static String display = String::fmt("%s (%d bescherming)", this->get_name().c_str(), this->protection); - return display; +String ArmorObject::get_displayname() const { + return String::fmt("%s (%d bescherming)", this->get_name().c_str(), this->protection); } diff --git a/backend/ArmorObject.h b/backend/ArmorObject.h index 8889ed7..068594e 100644 --- a/backend/ArmorObject.h +++ b/backend/ArmorObject.h @@ -8,7 +8,7 @@ class ArmorObject : public Object { public: void set_protection(int protection); int get_protection() const; - virtual const String & get_displayname() const; + virtual String get_displayname() const; private: int protection = 0; diff --git a/backend/Enemy.cpp b/backend/Enemy.cpp index a2bab30..5f6f0cf 100644 --- a/backend/Enemy.cpp +++ b/backend/Enemy.cpp @@ -40,9 +40,7 @@ ListRange<Object *> Enemy::get_hidden_objects() { return this->hidden_objects.range(); } -const String & Enemy::get_displayname() const { - static String displayname; - displayname = String::fmt("%s%s", this->name.c_str(), this->health_points == 0 ? " [verslagen]" : ""); - return displayname; +String Enemy::get_displayname() const { + return String::fmt("%s%s", this->name.c_str(), this->health_points == 0 ? " [verslagen]" : ""); } diff --git a/backend/Enemy.h b/backend/Enemy.h index f85172e..d3bd97a 100644 --- a/backend/Enemy.h +++ b/backend/Enemy.h @@ -16,7 +16,7 @@ public: unsigned get_health() const; bool is_dead() const; void take_damage(unsigned int dmg); - virtual const String & get_displayname() const; + virtual String get_displayname() const; void set_attack(float attack_chance); float get_attack() const; void set_damage(const Range<int> & range); diff --git a/backend/GoldObject.cpp b/backend/GoldObject.cpp index 525b2f2..00ee205 100644 --- a/backend/GoldObject.cpp +++ b/backend/GoldObject.cpp @@ -7,8 +7,7 @@ int GoldObject::get_count() const { return this->count; } -const String & GoldObject::get_displayname() const { - static String display = String::fmt("%s (%d)", this->get_name().c_str(), this->count); - return display; +String GoldObject::get_displayname() const { + return String::fmt("%s (%d)", this->get_name().c_str(), this->count); } diff --git a/backend/GoldObject.h b/backend/GoldObject.h index a532f07..535def9 100644 --- a/backend/GoldObject.h +++ b/backend/GoldObject.h @@ -8,7 +8,7 @@ class GoldObject : public Object { public: void set_count(const int & count); int get_count() const; - virtual const String & get_displayname() const; + virtual String get_displayname() const; private: int count = 0; diff --git a/backend/Object.cpp b/backend/Object.cpp index da1738c..398f970 100644 --- a/backend/Object.cpp +++ b/backend/Object.cpp @@ -8,9 +8,8 @@ void Object::set_name(const String & name) { const String & Object::get_name() const { return this->name; } -const String & Object::get_displayname() const { - static String displayname = this->name; - return displayname; +String Object::get_displayname() const { + return this->name; } void Object::set_description(const String & description) { diff --git a/backend/Object.h b/backend/Object.h index 0cebd64..19472d0 100644 --- a/backend/Object.h +++ b/backend/Object.h @@ -10,7 +10,7 @@ private: public: void set_name(const String & name); const String & get_name() const; - virtual const String & get_displayname() const; + virtual String get_displayname() const; void set_description(const String & description); const String & get_description() const; diff --git a/backend/RNG.cpp b/backend/RNG.cpp index d804f7b..21d5090 100644 --- a/backend/RNG.cpp +++ b/backend/RNG.cpp @@ -10,6 +10,10 @@ RNG & RNG::get() { return instance; } +std::mt19937 & RNG::get_engine() { + return RNG::get().rng; +} + int RNG::rand_int(const int upper) { return this->rand_int(0, upper); } diff --git a/backend/RNG.h b/backend/RNG.h index 3c168bd..f6ae887 100644 --- a/backend/RNG.h +++ b/backend/RNG.h @@ -7,6 +7,7 @@ class RNG { public: static RNG & get(); + static std::mt19937 & get_engine(); public: int rand_int(const int upper); diff --git a/backend/WeaponObject.cpp b/backend/WeaponObject.cpp index a91b6bf..e1de42f 100644 --- a/backend/WeaponObject.cpp +++ b/backend/WeaponObject.cpp @@ -8,8 +8,7 @@ Range<int> WeaponObject::get_damage() const { return { this->damage_min, this->damage_max }; } -const String & WeaponObject::get_displayname() const { - static String display = String::fmt("%s (%d - %d schade)", this->get_name().c_str(), this->damage_min, this->damage_max); - return display; +String WeaponObject::get_displayname() const { + return String::fmt("%s (%d - %d schade)", this->get_name().c_str(), this->damage_min, this->damage_max); } diff --git a/backend/WeaponObject.h b/backend/WeaponObject.h index b54ab59..e7b7ed3 100644 --- a/backend/WeaponObject.h +++ b/backend/WeaponObject.h @@ -9,7 +9,7 @@ class WeaponObject : public Object { public: void set_damage(const Range<int> & range); Range<int> get_damage() const; - virtual const String & get_displayname() const; + virtual String get_displayname() const; private: int damage_min = 0; diff --git a/frontend/cmd/hit.cpp b/frontend/cmd/hit.cpp index f4a78fd..dc28199 100644 --- a/frontend/cmd/hit.cpp +++ b/frontend/cmd/hit.cpp @@ -19,8 +19,10 @@ 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 (enemy.is_dead()) { + lprtf("%s is al dood!", enemy.get_name().c_str()); + return; + } if (rng.rand_double() > player.get_attack()) { lprtf("Je hebt gemist!\n"); diff --git a/frontend/generate_dungeon.cpp b/frontend/generate_dungeon.cpp index 5484b2c..6393abb 100644 --- a/frontend/generate_dungeon.cpp +++ b/frontend/generate_dungeon.cpp @@ -1,4 +1,4 @@ -#include <map> +#include <algorithm> #include <memory> #include "backend/Dungeon.h" @@ -12,6 +12,12 @@ using namespace std; +static vector<Direction> shuffled_directions() { + vector<Direction> out = { NORTH, EAST, SOUTH, WEST }; + shuffle(out.begin(), out.end(), RNG::get_engine()); + return out; +} + unique_ptr<Dungeon> generate_dungeon() { unique_ptr<Dungeon> dungeon = make_unique<Dungeon>(); GameData & gd = GameData::get_instance(); @@ -54,10 +60,19 @@ unique_ptr<Dungeon> generate_dungeon() { auto & temp = temp_map[index]; temp.location = location; - for (Direction direction : DIRECTIONS) { - if (rng.rand_double() < 0.5) continue; - temp.edges[direction] = rng.rand_int(location_count); + for (Direction direction : shuffled_directions()) { + // skip over already connected edges + if (temp.edges[direction] >= 0) continue; + + // find another random location that is NOT here + int connection = rng.rand_int(location_count - 1); + if (connection == index) connection++; + + // make a bidirectional connection + temp.edges[direction] = connection; temp_map[temp.edges[direction]].edges[-direction] = index; + + if (rng.rand_double() < 0.8) break; } dungeon->add_location(location); |