aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-01 23:42:57 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-01 23:42:57 +0100
commit671a25463419691bc6c8d11bd57d932bdea9632b (patch)
tree8898e076bfd4b3d702c7c765bcb9d6d41947198d
parentf4d71aa6e241ae59ed7d305e8aadddf9d90b2b46 (diff)
more cleanup
-rw-r--r--frontend/CMakeLists.txt1
-rw-r--r--frontend/GameData.cpp7
-rw-r--r--frontend/cmd/get.cpp21
-rw-r--r--frontend/cmd/use.cpp8
-rw-r--r--frontend/util.cpp7
-rw-r--r--frontend/util.h12
-rwxr-xr-xtodo1
7 files changed, 38 insertions, 19 deletions
diff --git a/frontend/CMakeLists.txt b/frontend/CMakeLists.txt
index 8f086d3..03191dd 100644
--- a/frontend/CMakeLists.txt
+++ b/frontend/CMakeLists.txt
@@ -7,6 +7,7 @@ target_sources(main PUBLIC
generate_dungeon.cpp
DB.cpp
GameData.cpp
+ util.cpp
)
add_subdirectory(cmd)
diff --git a/frontend/GameData.cpp b/frontend/GameData.cpp
index 30816c8..3b9046d 100644
--- a/frontend/GameData.cpp
+++ b/frontend/GameData.cpp
@@ -6,6 +6,8 @@
#include "backend/LocationFactory.h"
#include "backend/Object.h"
#include "backend/ObjectFactory.h"
+#include "backend/RNG.h"
+#include "backend/Range.h"
#include "backend/print.h"
#include "GameData.h"
@@ -39,7 +41,10 @@ Enemy * GameData::create_enemy(const string & name) const {
try {
auto row = query.row();
auto enemy = unique_ptr<Enemy>{ EnemyFactory::create_enemy(row.col<const char *>(0), row.col<const char *>(1)) };
- // TODO: min/max objects(?)
+ int object_count = RNG::get().rand_int(Range<int> { row.col<int>(2), row.col<int>(3) });
+ vector<string> object_names = this->random_objects(object_count);
+ for (const string & name : object_names)
+ enemy->add_hidden_object(this->create_object(name));
enemy->set_health(row.col<int>(4));
enemy->set_attack(static_cast<float>(row.col<int>(5)) / 100);
enemy->set_damage({ row.col<int>(6), row.col<int>(7) });
diff --git a/frontend/cmd/get.cpp b/frontend/cmd/get.cpp
index 3e85e57..362830e 100644
--- a/frontend/cmd/get.cpp
+++ b/frontend/cmd/get.cpp
@@ -1,7 +1,9 @@
+#include <algorithm>
#include <memory>
#include "../GameController.h"
#include "../strings.h"
+#include "../util.h"
#include "backend/Exception.h"
#include "backend/print.h"
@@ -14,19 +16,16 @@ using namespace std;
void GameController::cmd_get(string & target_name) {
Player & player = this->dungeon->get_player();
Location & location = player.get_location();
- unique_ptr<Object> target = nullptr;
- for (Object * object : location.get_visible_objects()) {
- if (str_lower(object->get_name().c_str()) != str_lower(target_name)) continue;
- target = unique_ptr<Object>(object);
- location.remove_visible_object(object);
- break;
- }
- if (target == nullptr)
+ auto range = location.get_visible_objects();
+ auto it = find_if(range.begin(), range.end(), by_name_case_insensitive(target_name));
+ if (!it)
throw Exception("object \"%s\" niet gevonden", target_name.c_str());
+ auto object = unique_ptr<Object>(*it);
+ location.remove_visible_object(object.get());
// gold objects are collected and (implicitly) destroyed
- GoldObject * gold = dynamic_cast<GoldObject *>(target.get());
+ GoldObject * gold = dynamic_cast<GoldObject *>(object.get());
if (gold != nullptr) {
int count = gold->get_count();
player.gold += count;
@@ -35,7 +34,7 @@ void GameController::cmd_get(string & target_name) {
}
// other objects go in the inventory
- lprtf("Je voegt %s toe aan je bezit.\n", target->get_name().c_str());
- player.inventory.push_back(target.release());
+ lprtf("Je voegt %s toe aan je bezit.\n", object->get_name().c_str());
+ player.inventory.push_back(object.release());
}
diff --git a/frontend/cmd/use.cpp b/frontend/cmd/use.cpp
index c636077..002255a 100644
--- a/frontend/cmd/use.cpp
+++ b/frontend/cmd/use.cpp
@@ -5,7 +5,7 @@
#include "backend/print.h"
#include "../GameController.h"
-#include "../strings.h"
+#include "../util.h"
using namespace std;
@@ -13,11 +13,7 @@ void GameController::cmd_use(string & target_name) {
Player & player = this->dungeon->get_player();
Location & location = player.get_location();
- auto it = find_if(player.inventory.begin(), player.inventory.end(),
- [target_name] (Object * object) {
- return str_lower(object->get_name().c_str()) == str_lower(target_name);
- }
- );
+ auto it = find_if(player.inventory.begin(), player.inventory.end(), by_name_case_insensitive(target_name));
if (!it)
throw Exception("object \"%s\" niet gevonden", target_name.c_str());
diff --git a/frontend/util.cpp b/frontend/util.cpp
new file mode 100644
index 0000000..afff38f
--- /dev/null
+++ b/frontend/util.cpp
@@ -0,0 +1,7 @@
+#include "util.h"
+#include "strings.h"
+
+bool by_name_case_insensitive::operator () (Object * object) {
+ return str_lower(object->get_name().c_str()) == str_lower(this->target_name);
+}
+
diff --git a/frontend/util.h b/frontend/util.h
new file mode 100644
index 0000000..946539f
--- /dev/null
+++ b/frontend/util.h
@@ -0,0 +1,12 @@
+#pragma once
+
+#include <string>
+
+#include "backend/Object.h"
+
+struct by_name_case_insensitive {
+ by_name_case_insensitive(const std::string & target_name) : target_name(target_name) {}
+ std::string target_name;
+ bool operator () (Object * object);
+};
+
diff --git a/todo b/todo
index 2e7859b..5dd0386 100755
--- a/todo
+++ b/todo
@@ -1,3 +1,2 @@
#!/bin/sh
git ls-files | xargs -d '\n' -- grep '\<TO''DO\>' --
-# TODO: use <algorithm> somewhere