aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-30 15:27:59 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-30 15:27:59 +0100
commita3c1ba7b49e4c5901d7c9dd917049744ad20fc96 (patch)
tree66750c2c740aca751f47c2896985d2b79f6f7806
parent6dfa3fb34fb0a2ea028fd46e77296e26b092fb99 (diff)
enemy loading kinda works
-rw-r--r--backend/CMakeLists.txt1
-rw-r--r--backend/Enemy.cpp31
-rw-r--r--backend/Enemy.h15
-rw-r--r--backend/EnemyFactory.cpp4
-rw-r--r--backend/EnemyFactory.h2
-rw-r--r--backend/Location.h3
-rw-r--r--backend/LocationFactory.h2
-rw-r--r--backend/Object.h3
-rw-r--r--backend/RNG.cpp3
-rw-r--r--frontend/GameData.cpp30
-rw-r--r--frontend/cmd/query.cpp14
-rw-r--r--frontend/cmd/search.cpp16
12 files changed, 108 insertions, 16 deletions
diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt
index 8797bca..6821388 100644
--- a/backend/CMakeLists.txt
+++ b/backend/CMakeLists.txt
@@ -11,5 +11,6 @@ target_sources(main PUBLIC
ConsumableObject.cpp
GoldObject.cpp
WeaponObject.cpp
+ Enemy.cpp
)
diff --git a/backend/Enemy.cpp b/backend/Enemy.cpp
new file mode 100644
index 0000000..699a303
--- /dev/null
+++ b/backend/Enemy.cpp
@@ -0,0 +1,31 @@
+#include <string.h>
+
+#include "Enemy.h"
+#include "util.h"
+
+Enemy::Enemy(const char * name, const char * description) {
+ this->set_name(name);
+ this->set_description(description);
+}
+
+Enemy::~Enemy() {
+ safe_free(this->name);
+ safe_free(this->description);
+}
+
+void Enemy::set_name(const char * name) {
+ safe_free(this->name);
+ this->name = strdup(name);
+}
+const char * Enemy::get_name() {
+ return this->name;
+}
+
+void Enemy::set_description(const char * description) {
+ safe_free(this->description);
+ this->description = strdup(description);
+}
+const char * Enemy::get_description() {
+ return this->description;
+}
+
diff --git a/backend/Enemy.h b/backend/Enemy.h
index fb04472..fdc095e 100644
--- a/backend/Enemy.h
+++ b/backend/Enemy.h
@@ -1,9 +1,20 @@
#pragma once
class Enemy {
+public:
+ void set_name(const char * name);
+ const char * get_name();
+ void set_description(const char * description);
+ const char * get_description();
+
private:
- Enemy() = default;
- virtual ~Enemy() = default;
friend class EnemyFactory;
+ Enemy(const char * name = "", const char * description = "");
+public:
+ virtual ~Enemy();
+
+private:
+ const char * name = nullptr;
+ const char * description = nullptr;
};
diff --git a/backend/EnemyFactory.cpp b/backend/EnemyFactory.cpp
index 9c92c0b..42e35ae 100644
--- a/backend/EnemyFactory.cpp
+++ b/backend/EnemyFactory.cpp
@@ -1,6 +1,6 @@
#include "EnemyFactory.h"
-Enemy * EnemyFactory::create_enemy() {
- return new Enemy();
+Enemy * EnemyFactory::create_enemy(const char * name, const char * description) {
+ return new Enemy(name, description);
}
diff --git a/backend/EnemyFactory.h b/backend/EnemyFactory.h
index ebb956b..6e8b6db 100644
--- a/backend/EnemyFactory.h
+++ b/backend/EnemyFactory.h
@@ -4,7 +4,7 @@
class EnemyFactory {
public:
- static Enemy * create_enemy();
+ static Enemy * create_enemy(const char * name = "", const char * description = "");
private:
EnemyFactory() = delete;
diff --git a/backend/Location.h b/backend/Location.h
index 4ce2349..59a531f 100644
--- a/backend/Location.h
+++ b/backend/Location.h
@@ -28,9 +28,10 @@ public:
ListRange<Enemy *> get_enemies();
private:
+ friend class LocationFactory;
Location(const char * name = "", const char * description = "");
+public:
virtual ~Location();
- friend class LocationFactory;
private:
const char * name = nullptr;
diff --git a/backend/LocationFactory.h b/backend/LocationFactory.h
index 2da590e..178d5d3 100644
--- a/backend/LocationFactory.h
+++ b/backend/LocationFactory.h
@@ -4,7 +4,7 @@
class LocationFactory {
public:
- static Location * create_location(const char * name, const char * description);
+ static Location * create_location(const char * name = "", const char * description = "");
private:
LocationFactory() = delete;
diff --git a/backend/Object.h b/backend/Object.h
index cb2c209..b98dc82 100644
--- a/backend/Object.h
+++ b/backend/Object.h
@@ -15,9 +15,10 @@ public:
bool get_hidden();
protected:
+ friend class ObjectFactory;
Object(const char * name = "", const char * description = "");
+public:
virtual ~Object();
- friend class ObjectFactory;
protected:
bool hidden = false;
diff --git a/backend/RNG.cpp b/backend/RNG.cpp
index bc3e57b..2d7895b 100644
--- a/backend/RNG.cpp
+++ b/backend/RNG.cpp
@@ -14,7 +14,8 @@ int RNG::rand_int(const int upper) {
return this->rand_int(0, upper);
}
int RNG::rand_int(const int lower, const int upper) {
- uniform_int_distribution<int> random_dist(lower, upper);
+ // NOTE: random_dist's upper limit is inclusive
+ uniform_int_distribution<int> random_dist(lower, upper - 1);
return random_dist(rng);
}
diff --git a/frontend/GameData.cpp b/frontend/GameData.cpp
index edcd854..739ad1e 100644
--- a/frontend/GameData.cpp
+++ b/frontend/GameData.cpp
@@ -21,9 +21,31 @@ GameData::GameData() {
}
Enemy * GameData::create_enemy(const string & name) {
- Enemy * enemy = EnemyFactory::create_enemy();
- // TODO: fill fields
- return enemy;
+ static DBStatement query = this->db->prepare(R"(
+ select
+ naam,
+ omschrijving,
+ minimumobjecten,
+ maximumobjecten,
+ levenspunten,
+ aanvalskans,
+ minimumschade,
+ maximumschade
+ from Vijanden
+ where lower(naam) = lower(?)
+ limit 1
+ )");
+ query.reset()
+ .bind(name)
+ ;
+
+ try {
+ auto row = query.row();
+ return EnemyFactory::create_enemy(row.col<const char *>(0), row.col<const char *>(1));
+ // TODO: set all other properties
+ } catch (...) {
+ return EnemyFactory::create_enemy(name.c_str());
+ }
}
static const unordered_map<string, ObjectType> type_map = {
@@ -85,7 +107,7 @@ Location * GameData::create_location(const string & name) {
auto row = query.row();
return LocationFactory::create_location(row.col<const char *>(0), row.col<const char *>(1));
} catch (...) {
- return LocationFactory::create_location(name.c_str(), "");
+ return LocationFactory::create_location(name.c_str());
}
}
diff --git a/frontend/cmd/query.cpp b/frontend/cmd/query.cpp
index e1e45a1..23d2a42 100644
--- a/frontend/cmd/query.cpp
+++ b/frontend/cmd/query.cpp
@@ -1,5 +1,6 @@
#include "backend/Location.h"
#include "backend/Object.h"
+#include "backend/Enemy.h"
#include "../Player.h"
#include "../print.h"
@@ -43,7 +44,18 @@ FollowupAction Player::cmd_query(string &) {
lprtf("\n");
}
- // TODO: enemies
+ {
+ lprtf("Vijanden: ");
+ size_t enemies = 0;
+ for (Enemy * enemy : this->location.get_enemies()) {
+ if (enemies > 0) lprtf(", ");
+ lprtf("%s", enemy->get_name());
+ enemies++;
+ }
+ if (enemies == 0)
+ lprtf("(geen)");
+ lprtf("\n");
+ }
return FollowupAction::NONE;
}
diff --git a/frontend/cmd/search.cpp b/frontend/cmd/search.cpp
index 4646ddc..67b0bf1 100644
--- a/frontend/cmd/search.cpp
+++ b/frontend/cmd/search.cpp
@@ -1,9 +1,21 @@
#include "../Player.h"
+#include "../print.h"
+
+#include "backend/Location.h"
using namespace std;
-FollowupAction Player::cmd_search(string & argv) {
- // TODO
+FollowupAction Player::cmd_search(string &) {
+ bool found = false;
+ for (Object * object : this->location.get_objects()) {
+ if (object->get_hidden() == false) continue;
+ if (!found) lprtf("Je vindt:\n");
+ lprtf("- %s\n", object->get_displayname());
+ object->set_hidden(false);
+ found = true;
+ }
+ if (!found)
+ lprtf("Je hebt niks gevonden.\n");
return FollowupAction::UPDATE;
}