aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--backend/CMakeLists.txt1
-rw-r--r--backend/Location.cpp34
-rw-r--r--backend/Location.h16
-rw-r--r--backend/Object.cpp7
-rw-r--r--backend/Object.h5
-rw-r--r--backend/String.cpp11
-rw-r--r--backend/String.h2
-rw-r--r--backend/print.cpp (renamed from frontend/print.cpp)34
-rw-r--r--backend/print.h (renamed from frontend/print.h)13
-rw-r--r--backend/util.cpp4
-rw-r--r--backend/util.h3
-rw-r--r--frontend/CMakeLists.txt1
-rw-r--r--frontend/Player.cpp1
-rw-r--r--frontend/cmd/cheat.cpp3
-rw-r--r--frontend/cmd/get.cpp7
-rw-r--r--frontend/cmd/go.cpp2
-rw-r--r--frontend/cmd/help.cpp3
-rw-r--r--frontend/cmd/query.cpp5
-rw-r--r--frontend/cmd/search.cpp9
-rw-r--r--frontend/cmd/view.cpp3
-rw-r--r--frontend/generate_dungeon.cpp2
-rw-r--r--frontend/load_dungeon.cpp6
-rw-r--r--frontend/main.cpp2
-rw-r--r--frontend/rl.cpp4
-rw-r--r--frontend/strings.cpp3
25 files changed, 101 insertions, 80 deletions
diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt
index 3c6991a..1f872e4 100644
--- a/backend/CMakeLists.txt
+++ b/backend/CMakeLists.txt
@@ -13,5 +13,6 @@ target_sources(main PUBLIC
WeaponObject.cpp
Enemy.cpp
String.cpp
+ print.cpp
)
diff --git a/backend/Location.cpp b/backend/Location.cpp
index d3227cf..7246246 100644
--- a/backend/Location.cpp
+++ b/backend/Location.cpp
@@ -22,7 +22,8 @@ Location::Location(const String & name, const String & description) : name(name)
Location::~Location() {
safe_free(this->enemies);
- safe_free(this->objects);
+ safe_free(this->hidden_objects);
+ safe_free(this->visible_objects);
}
void Location::set_name(const String & name) { this->name = name; }
@@ -38,14 +39,33 @@ Location * Location::get_exit(Direction dir) const {
return this->edges[dir];
}
-void Location::add_object(Object * object) {
- this->objects.push_back(object);
+void Location::add_visible_object(Object * object) {
+ this->visible_objects.push_back(object);
}
-void Location::remove_object(Object * object) {
- this->objects.remove(object);
+void Location::remove_visible_object(Object * object) {
+ this->visible_objects.remove(object);
}
-ListRange<Object *> Location::get_objects() const {
- return this->objects.range();
+ListRange<Object *> Location::get_visible_objects() const {
+ return this->visible_objects.range();
+}
+
+void Location::add_hidden_object(Object * object) {
+ this->hidden_objects.push_back(object);
+}
+void Location::remove_hidden_object(Object * object) {
+ this->hidden_objects.remove(object);
+}
+ListRange<Object *> Location::get_hidden_objects() const {
+ return this->hidden_objects.range();
+}
+
+void Location::hide_object(Object * object) {
+ this->visible_objects.remove(object);
+ this->hidden_objects.push_back(object);
+}
+void Location::unhide_object(Object * object) {
+ this->hidden_objects.remove(object);
+ this->visible_objects.push_back(object);
}
void Location::add_enemy(Enemy * enemy) {
diff --git a/backend/Location.h b/backend/Location.h
index b139da8..464328b 100644
--- a/backend/Location.h
+++ b/backend/Location.h
@@ -30,9 +30,16 @@ public:
void set_exit(Direction dir, Location * location = nullptr);
Location * get_exit(Direction dir) const;
- void add_object(Object *);
- void remove_object(Object *);
- ListRange<Object *> get_objects() const;
+ void add_visible_object(Object *);
+ void remove_visible_object(Object *);
+ ListRange<Object *> get_visible_objects() const;
+
+ void add_hidden_object(Object *);
+ void remove_hidden_object(Object *);
+ ListRange<Object *> get_hidden_objects() const;
+
+ void hide_object(Object *);
+ void unhide_object(Object *);
void add_enemy(Enemy *);
void remove_enemy(Enemy *);
@@ -48,7 +55,8 @@ private:
String name;
String description;
List<Enemy*> enemies;
- List<Object*> objects;
+ List<Object*> hidden_objects;
+ List<Object*> visible_objects;
Location * edges[4] = {
nullptr,
diff --git a/backend/Object.cpp b/backend/Object.cpp
index 2640607..da1738c 100644
--- a/backend/Object.cpp
+++ b/backend/Object.cpp
@@ -20,10 +20,3 @@ const String & Object::get_description() const {
return this->description;
}
-void Object::set_hidden(bool hidden) {
- this->hidden = hidden;
-}
-bool Object::get_hidden() {
- return this->hidden;
-}
-
diff --git a/backend/Object.h b/backend/Object.h
index 07b7b74..0cebd64 100644
--- a/backend/Object.h
+++ b/backend/Object.h
@@ -13,8 +13,6 @@ public:
virtual const String & get_displayname() const;
void set_description(const String & description);
const String & get_description() const;
- void set_hidden(bool hidden);
- bool get_hidden();
protected:
friend class ObjectFactory;
@@ -22,8 +20,5 @@ protected:
public:
virtual ~Object() = default;
-protected:
- bool hidden = false;
-
};
diff --git a/backend/String.cpp b/backend/String.cpp
index 2b40c88..e387589 100644
--- a/backend/String.cpp
+++ b/backend/String.cpp
@@ -20,17 +20,22 @@ String::String(const char * c_str) {
}
String String::fmt(const char * fmt, ...) {
- String out;
- va_list args, args_copy;
+ va_list args;
va_start(args, fmt);
+ String out = String::va_fmt(args, fmt);
+ va_end(args);
+ return out;
+}
+String String::va_fmt(va_list args, const char * fmt) {
+ String out;
+ va_list args_copy;
va_copy(args_copy, args);
out._data_len = vsnprintf(NULL, 0, fmt, args_copy);
va_end(args_copy);
out._data = static_cast<char *>(malloc(out._data_len + 1));
vsnprintf(out._data, out._data_len + 1, fmt, args);
- va_end(args);
return out;
}
diff --git a/backend/String.h b/backend/String.h
index b4025be..e41222e 100644
--- a/backend/String.h
+++ b/backend/String.h
@@ -1,6 +1,7 @@
#pragma once
#include <stddef.h>
+#include <stdarg.h>
class String {
public:
@@ -9,6 +10,7 @@ public:
String(const String &);
~String();
public:
+ static String va_fmt(va_list args, const char * fmt);
static String fmt(const char * fmt, ...);
public:
diff --git a/frontend/print.cpp b/backend/print.cpp
index 0c60892..fc8ad9b 100644
--- a/frontend/print.cpp
+++ b/backend/print.cpp
@@ -1,12 +1,12 @@
-#include <cstdarg>
-#include <string>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
#include <unistd.h>
#include "print.h"
+#include "util.h"
-using namespace std;
-
-static string va_stringf(va_list args, const char * fmt) {
+static String va_stringf(va_list args, const char * fmt) {
va_list args_copy;
va_copy(args_copy, args);
@@ -16,26 +16,17 @@ static string va_stringf(va_list args, const char * fmt) {
vsnprintf(msg, sz, fmt, args);
- string out = msg;
+ String out = msg;
free(msg);
va_end(args);
-
- return out;
-}
-
-static string stringf(const char * fmt, ...) {
- va_list args;
- va_start(args, fmt);
- string out = va_stringf(args, fmt);
- va_end(args);
return out;
}
void lprtf(const char * fmt, ...) {
va_list args;
va_start(args, fmt);
- string formatted = va_stringf(args, fmt);
+ String formatted = String::va_fmt(args, fmt);
va_end(args);
fwrite(formatted.c_str(), 1, formatted.size(), stdout);
@@ -52,12 +43,15 @@ SessionLog & SessionLog::get() {
SessionLog::SessionLog() {
if (!this->enable) return;
- string filename = stringf("%lu.log", getpid());
+ String filename = String::fmt("%lu.log", getpid());
FILE * file = fopen(filename.c_str(), "w+");
- this->file = { file, [] (FILE * file) { fclose(file); } };
}
-void SessionLog::append(const string & str) const {
+SessionLog::~SessionLog() {
+ safe_free(this->file);
+}
+
+void SessionLog::append(const String & str) const {
this->append(str.data(), str.size());
}
@@ -68,6 +62,6 @@ void SessionLog::append(const char * str) const {
void SessionLog::append(const char * buf, size_t buf_size) const {
if (this->file == nullptr) return;
if (buf_size == 0) return;
- fwrite(buf, 1, buf_size, this->file.get());
+ fwrite(buf, 1, buf_size, this->file);
}
diff --git a/frontend/print.h b/backend/print.h
index b8034f2..8fa0501 100644
--- a/frontend/print.h
+++ b/backend/print.h
@@ -1,9 +1,8 @@
#pragma once
-#include <cstring>
-#include <memory>
-#include <functional>
-#include <string>
+#include <stdio.h>
+
+#include "String.h"
void lprtf(const char * fmt, ...);
@@ -13,15 +12,15 @@ public:
private:
SessionLog();
- virtual ~SessionLog() = default;
+ virtual ~SessionLog();
public:
- virtual void append(const std::string & str) const;
+ virtual void append(const String & str) const;
virtual void append(const char * str) const;
virtual void append(const char * buf, size_t buf_size) const;
private:
- std::unique_ptr<FILE, std::function<void(FILE*)>> file = nullptr;
+ FILE * file = nullptr;
static constexpr const bool enable = false;
};
diff --git a/backend/util.cpp b/backend/util.cpp
index 85b6950..afac06e 100644
--- a/backend/util.cpp
+++ b/backend/util.cpp
@@ -15,4 +15,8 @@ void safe_free(char * & ptr) {
auto x = static_cast<void *>(ptr);
safe_free(x);
}
+void safe_free(FILE * & ptr) {
+ auto x = static_cast<void *>(ptr);
+ safe_free(x);
+}
diff --git a/backend/util.h b/backend/util.h
index bbe307b..e544e19 100644
--- a/backend/util.h
+++ b/backend/util.h
@@ -1,5 +1,7 @@
#pragma once
+#include <stdio.h>
+
#include "List.h"
void safe_free(void * & ptr);
@@ -7,6 +9,7 @@ void safe_free(char * & ptr);
void safe_free(const char * & ptr);
template <typename T>
void safe_free(List<T *> & ptr_list);
+void safe_free(FILE * & ptr);
#include "util.hpp"
diff --git a/frontend/CMakeLists.txt b/frontend/CMakeLists.txt
index d60b44f..92d03bc 100644
--- a/frontend/CMakeLists.txt
+++ b/frontend/CMakeLists.txt
@@ -2,7 +2,6 @@ target_sources(main PUBLIC
main.cpp
rl.cpp
strings.cpp
- print.cpp
Player.cpp
load_dungeon.cpp
generate_dungeon.cpp
diff --git a/frontend/Player.cpp b/frontend/Player.cpp
index c579f83..27c660c 100644
--- a/frontend/Player.cpp
+++ b/frontend/Player.cpp
@@ -1,6 +1,5 @@
#include "strings.h"
#include "Player.h"
-#include "print.h"
#include "backend/Dungeon.h"
diff --git a/frontend/cmd/cheat.cpp b/frontend/cmd/cheat.cpp
index aeed5f4..7a020a3 100644
--- a/frontend/cmd/cheat.cpp
+++ b/frontend/cmd/cheat.cpp
@@ -1,5 +1,6 @@
+#include "backend/print.h"
+
#include "../Player.h"
-#include "../print.h"
using namespace std;
diff --git a/frontend/cmd/get.cpp b/frontend/cmd/get.cpp
index 12c3353..8aa2d75 100644
--- a/frontend/cmd/get.cpp
+++ b/frontend/cmd/get.cpp
@@ -1,7 +1,7 @@
#include "../Player.h"
#include "../strings.h"
-#include "../print.h"
+#include "backend/print.h"
#include "backend/GoldObject.h"
#include "backend/Location.h"
@@ -9,11 +9,10 @@ using namespace std;
FollowupAction Player::cmd_get(string & target_name) {
unique_ptr<Object> target = nullptr;
- for (Object * object : this->location.get_objects()) {
- if (object->get_hidden() == true) continue;
+ for (Object * object : this->location.get_visible_objects()) {
if (str_lower(object->get_name().c_str()) != str_lower(target_name)) continue;
target = unique_ptr<Object>(object);
- this->location.remove_object(object);
+ this->location.remove_visible_object(object);
break;
}
if (target == nullptr) {
diff --git a/frontend/cmd/go.cpp b/frontend/cmd/go.cpp
index 624bf6c..9565e99 100644
--- a/frontend/cmd/go.cpp
+++ b/frontend/cmd/go.cpp
@@ -1,7 +1,7 @@
#include "backend/Location.h"
+#include "backend/print.h"
#include "../Player.h"
-#include "../print.h"
#include "../strings.h"
using namespace std;
diff --git a/frontend/cmd/help.cpp b/frontend/cmd/help.cpp
index e614f28..b655a45 100644
--- a/frontend/cmd/help.cpp
+++ b/frontend/cmd/help.cpp
@@ -1,4 +1,5 @@
-#include "../print.h"
+#include "backend/print.h"
+
#include "../Player.h"
using namespace std;
diff --git a/frontend/cmd/query.cpp b/frontend/cmd/query.cpp
index acd6cee..19f2584 100644
--- a/frontend/cmd/query.cpp
+++ b/frontend/cmd/query.cpp
@@ -1,9 +1,9 @@
#include "backend/Location.h"
#include "backend/Object.h"
#include "backend/Enemy.h"
+#include "backend/print.h"
#include "../Player.h"
-#include "../print.h"
using namespace std;
@@ -21,8 +21,7 @@ FollowupAction Player::cmd_query(string &) {
{
lprtf("Zichtbare objecten: ");
size_t objects = 0;
- for (Object * obj : this->location.get_objects()) {
- if (obj->get_hidden() == true) continue;
+ for (Object * obj : this->location.get_visible_objects()) {
if (objects > 0) lprtf(", ");
lprtf("%s", obj->get_displayname().c_str());
objects++;
diff --git a/frontend/cmd/search.cpp b/frontend/cmd/search.cpp
index 67b0bf1..f10709a 100644
--- a/frontend/cmd/search.cpp
+++ b/frontend/cmd/search.cpp
@@ -1,17 +1,16 @@
#include "../Player.h"
-#include "../print.h"
+#include "backend/print.h"
#include "backend/Location.h"
using namespace std;
FollowupAction Player::cmd_search(string &) {
bool found = false;
- for (Object * object : this->location.get_objects()) {
- if (object->get_hidden() == false) continue;
+ for (Object * object : this->location.get_hidden_objects()) {
if (!found) lprtf("Je vindt:\n");
- lprtf("- %s\n", object->get_displayname());
- object->set_hidden(false);
+ lprtf("- %s\n", object->get_displayname().c_str());
+ this->location.unhide_object(object);
found = true;
}
if (!found)
diff --git a/frontend/cmd/view.cpp b/frontend/cmd/view.cpp
index b9d179b..a252715 100644
--- a/frontend/cmd/view.cpp
+++ b/frontend/cmd/view.cpp
@@ -1,7 +1,8 @@
-#include "../print.h"
#include "../Player.h"
#include "../strings.h"
+#include "backend/print.h"
+
using namespace std;
FollowupAction Player::cmd_view(string & target) {
diff --git a/frontend/generate_dungeon.cpp b/frontend/generate_dungeon.cpp
index 80eb0cb..868fe43 100644
--- a/frontend/generate_dungeon.cpp
+++ b/frontend/generate_dungeon.cpp
@@ -1,9 +1,9 @@
#include <memory>
#include "backend/Dungeon.h"
+#include "backend/print.h"
#include "rl.h"
-#include "print.h"
#include "generate_dungeon.h"
#include "GameData.h"
#include "Exception.h"
diff --git a/frontend/load_dungeon.cpp b/frontend/load_dungeon.cpp
index 23b4094..c671fe7 100644
--- a/frontend/load_dungeon.cpp
+++ b/frontend/load_dungeon.cpp
@@ -56,14 +56,12 @@ unique_ptr<Dungeon> load_dungeon(const string & filename) {
vector<string> objects_hidden = str_split(tag.attribute("objectenverborgen").as_string(), ";");
for (string & name : objects_hidden) {
Object * object = gamedata.create_object(name);
- object->set_hidden(true);
- location->add_object(object);
+ location->add_hidden_object(object);
}
vector<string> objects_visible = str_split(tag.attribute("objectenzichtbaar").as_string(), ";");
for (string & name : objects_visible) {
Object * object = gamedata.create_object(name);
- object->set_hidden(false);
- location->add_object(object);
+ location->add_visible_object(object);
}
vector<string> enemies = str_split(tag.attribute("vijand").as_string(), ";");
diff --git a/frontend/main.cpp b/frontend/main.cpp
index 366473d..56743c1 100644
--- a/frontend/main.cpp
+++ b/frontend/main.cpp
@@ -2,11 +2,11 @@
#include <cstdio>
#include <memory>
+#include "backend/print.h"
#include "backend/Dungeon.h"
#include "Player.h"
#include "Exception.h"
-#include "print.h"
#include "load_dungeon.h"
#include "generate_dungeon.h"
#include "rl.h"
diff --git a/frontend/rl.cpp b/frontend/rl.cpp
index 1c0df0e..3ce360f 100644
--- a/frontend/rl.cpp
+++ b/frontend/rl.cpp
@@ -5,7 +5,7 @@
#include <readline/history.h>
#include "rl.h"
-#include "print.h"
+#include "backend/print.h"
using namespace std;
@@ -19,7 +19,7 @@ string rl() {
if (out.size() > 0) add_history(input);
free(input);
- SessionLog::get().append(out);
+ SessionLog::get().append(out.c_str());
SessionLog::get().append("\n");
return out;
diff --git a/frontend/strings.cpp b/frontend/strings.cpp
index 687309d..05c1548 100644
--- a/frontend/strings.cpp
+++ b/frontend/strings.cpp
@@ -1,7 +1,8 @@
#include <algorithm>
#include "strings.h"
-#include "print.h"
+
+#include "backend/print.h"
using namespace std;