From 5c34847218e8d754447f5cf71ed595bbb412eee7 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 29 Oct 2024 23:30:57 +0100 Subject: more WIP --- backend/Dungeon.h | 2 +- backend/List.h | 42 ++++++++++++++++ backend/List.hpp | 110 ++++++++++++++++++++++-------------------- backend/ListIterator.h | 38 +++++++++++++++ backend/ListIterator.hpp | 41 ++++++++++++++++ backend/Location.cpp | 5 ++ backend/Location.h | 4 +- backend/Object.cpp | 3 ++ backend/Object.h | 1 + frontend/CMakeLists.txt | 2 + frontend/DB.cpp | 30 ++++++++++++ frontend/DB.h | 18 +++++++ frontend/GameData.cpp | 15 ++++++ frontend/GameData.h | 18 +++++++ frontend/cmd/query.cpp | 33 +++++++++---- frontend/generate_dungeon.cpp | 42 +--------------- frontend/load_dungeon.cpp | 17 ++++--- 17 files changed, 310 insertions(+), 111 deletions(-) create mode 100644 backend/List.h create mode 100644 backend/ListIterator.h create mode 100644 backend/ListIterator.hpp create mode 100644 frontend/DB.cpp create mode 100644 frontend/DB.h create mode 100644 frontend/GameData.cpp create mode 100644 frontend/GameData.h diff --git a/backend/Dungeon.h b/backend/Dungeon.h index 2d5fa61..5740eb6 100644 --- a/backend/Dungeon.h +++ b/backend/Dungeon.h @@ -1,6 +1,6 @@ #pragma once -#include "List.hpp" +#include "List.h" class Location; diff --git a/backend/List.h b/backend/List.h new file mode 100644 index 0000000..2d3d6b1 --- /dev/null +++ b/backend/List.h @@ -0,0 +1,42 @@ +#pragma once + +#include + +template +class ListRange; + +template +class ListIterator; + +template +struct ListLink { + ListLink * prev; + ListLink * next; + T value; +}; + +template +class List { +public: + size_t size(); + + void push_back(T el); + + void pop_back(); + + void clear(); + + T & operator [] (size_t index); + + ListIterator begin(); + ListIterator end(); + ListRange range(); + +private: + ListLink * head = nullptr; + ListLink * tail = nullptr; + size_t length = 0; +}; + +#include "List.hpp" + diff --git a/backend/List.hpp b/backend/List.hpp index d6c7738..df075f9 100644 --- a/backend/List.hpp +++ b/backend/List.hpp @@ -1,66 +1,72 @@ #pragma once -#include +#include "List.h" template -struct ListLink { - ListLink * prev; - ListLink * next; - T value; -}; +size_t List::size() { + return this->length; +} -template -class List { -public: - size_t size() { return this->length; } +template +void List::push_back(T el) { + ListLink * link = static_cast *>(malloc(sizeof(ListLink))); + link->prev = this->head; + link->next = nullptr; + link->value = el; + if (this->head != nullptr) this->head->next = link; + if (this->head == nullptr) this->tail = link; + this->head = link; + this->length++; +} - void push_back(T el) { - ListLink * link = static_cast *>(malloc(sizeof(ListLink))); - link->prev = this->head; - link->next = nullptr; - link->value = el; - if (this->head != nullptr) this->head->next = link; - if (this->head == nullptr) this->tail = link; - this->head = link; - this->length++; - } +template +void List::pop_back() { + if (this->head == nullptr) return; // empty list - void pop_back() { - if (this->head == nullptr) return; // empty list + ListLink * secondlast = this->head->prev; + if (secondlast != nullptr) + secondlast->next = nullptr; + free(this->head); + if (this->tail == this->head) + this->tail = nullptr; + this->head = secondlast; - ListLink * secondlast = this->head->prev; - if (secondlast != nullptr) - secondlast->next = nullptr; - free(this->head); - if (this->tail == this->head) - this->tail = nullptr; - this->head = secondlast; + this->length--; +} - this->length--; +template +void List::clear() { + ListLink * link = this->tail; + while (link != nullptr) { + ListLink * next = link->next; + free(link); + link = next; } + this->head = nullptr; + this->tail = nullptr; + this->length = 0; +} - void clear() { - ListLink * link = this->tail; - while (link != nullptr) { - ListLink * next = link->next; - free(link); - link = next; - } - this->head = nullptr; - this->tail = nullptr; - this->length = 0; - } +template +T & List::operator [] (size_t index) { + ListLink * link = this->tail; + for (size_t i = 0; i < index; i++) + link = link->next; + return link->value; +} - T & operator [] (size_t index) { - ListLink * link = this->tail; - for (size_t i = 0; i < index; i++) - link = link->next; - return link->value; - } +template +ListRange List::range() { + return { *this }; +} -private: - ListLink * head = nullptr; - ListLink * tail = nullptr; - size_t length = 0; -}; +template +ListIterator List::begin() { + return this->range().begin(); +} + +template +ListIterator List::end() { + return this->range().end(); +} diff --git a/backend/ListIterator.h b/backend/ListIterator.h new file mode 100644 index 0000000..aa78931 --- /dev/null +++ b/backend/ListIterator.h @@ -0,0 +1,38 @@ +#pragma once + +#include "backend/List.h" +#include + +template +class List; + +template +class ListIterator { +public: + ListIterator(List & list, size_t index); + +public: + T operator * () const; + ListIterator & operator ++ (); + bool operator != (const ListIterator &) const; + +private: + List & list; + size_t index; +}; + +template +class ListRange { +public: + ListRange(List & list); + +public: + ListIterator begin() const; + ListIterator end() const; + size_t size() const; + +private: + List & list; +}; + +#include "ListIterator.hpp" diff --git a/backend/ListIterator.hpp b/backend/ListIterator.hpp new file mode 100644 index 0000000..5e4ea91 --- /dev/null +++ b/backend/ListIterator.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include "ListIterator.h" + +template +ListRange::ListRange(List & list) : list(list) { } + +template +ListIterator ListRange::begin() const { + return { this->list, 0 }; +} + +template +ListIterator ListRange::end() const { + return { this->list, this->list.size() }; +} + +template +size_t ListRange::size() const { + return this->list.size(); +} + +template +ListIterator::ListIterator(List & list, size_t index) : list(list), index(index) { } + +template +T ListIterator::operator * () const { + return this->list[this->index]; +} + +template +ListIterator & ListIterator::operator ++ () { + this->index++; + return *this; +} + +template +bool ListIterator::operator != (const ListIterator & rhs) const { + return this->index < rhs.index; +} + diff --git a/backend/Location.cpp b/backend/Location.cpp index 8a79af5..96d06ca 100644 --- a/backend/Location.cpp +++ b/backend/Location.cpp @@ -1,6 +1,7 @@ #include #include "Location.h" +#include "ListIterator.h" #include "util.h" Location::Location(const char * name, const char * description) { @@ -36,3 +37,7 @@ Location * Location::get_exit(Direction dir) { return this->edges[dir]; } +ListRange Location::get_objects() { + return this->objects.range(); +} + diff --git a/backend/Location.h b/backend/Location.h index f102728..8b600bb 100644 --- a/backend/Location.h +++ b/backend/Location.h @@ -1,6 +1,7 @@ #pragma once -#include "List.hpp" +#include "List.h" +#include "ListIterator.h" class Enemy; class Object; @@ -21,6 +22,7 @@ public: const char * get_description(); void set_exit(Direction dir, Location * location = nullptr); Location * get_exit(Direction dir); + ListRange get_objects(); protected: Location(const char * name = "", const char * description = ""); diff --git a/backend/Object.cpp b/backend/Object.cpp index e14c780..ed8bc46 100644 --- a/backend/Object.cpp +++ b/backend/Object.cpp @@ -22,6 +22,9 @@ void Object::set_name(const char * name) { const char * Object::get_name() { return this->name; } +const char * Object::get_displayname() { + return this->get_name(); +} void Object::set_description(const char * description) { safe_free(this->description); diff --git a/backend/Object.h b/backend/Object.h index 92652c4..cb2c209 100644 --- a/backend/Object.h +++ b/backend/Object.h @@ -8,6 +8,7 @@ private: public: void set_name(const char * name); const char * get_name(); + virtual const char * get_displayname(); void set_description(const char * description); const char * get_description(); void set_hidden(bool hidden); diff --git a/frontend/CMakeLists.txt b/frontend/CMakeLists.txt index 60b830c..d60b44f 100644 --- a/frontend/CMakeLists.txt +++ b/frontend/CMakeLists.txt @@ -7,6 +7,8 @@ target_sources(main PUBLIC load_dungeon.cpp generate_dungeon.cpp Exception.cpp + DB.cpp + GameData.cpp ) add_subdirectory(cmd) diff --git a/frontend/DB.cpp b/frontend/DB.cpp new file mode 100644 index 0000000..82933b8 --- /dev/null +++ b/frontend/DB.cpp @@ -0,0 +1,30 @@ +#include "DB.h" +#include "Exception.h" + +DB::DB(const std::string & path) { + sqlite3 * db = NULL; + int ret = sqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READONLY, NULL); + this->db = { + db, + [] (sqlite3 * db) { + sqlite3_close_v2(db); + }, + }; + if (ret != SQLITE_OK) + throw Exception("sqlite3_open_v2"); +} + +DB::unique_sqlite3_stmt DB::prepare(const std::string & query) { + sqlite3_stmt * stmt = NULL; + int ret = sqlite3_prepare_v2(this->db.get(), query.c_str(), query.size(), &stmt, NULL); + unique_sqlite3_stmt uniq_stmt = { + stmt, + [] (sqlite3_stmt * stmt) { + sqlite3_finalize(stmt); + }, + }; + if (ret != SQLITE_OK) + throw Exception("sqlite3_prepare_v2"); + return uniq_stmt; +} + diff --git a/frontend/DB.h b/frontend/DB.h new file mode 100644 index 0000000..b464f9f --- /dev/null +++ b/frontend/DB.h @@ -0,0 +1,18 @@ +#pragma once + +#include +#include +#include + +class DB { + typedef std::unique_ptr> unique_sqlite3; + typedef std::unique_ptr> unique_sqlite3_stmt; + +public: + DB(const std::string & path); + unique_sqlite3_stmt prepare(const std::string & query); + +private: + unique_sqlite3 db = NULL; +}; + diff --git a/frontend/GameData.cpp b/frontend/GameData.cpp new file mode 100644 index 0000000..ad22aa0 --- /dev/null +++ b/frontend/GameData.cpp @@ -0,0 +1,15 @@ +#include + +#include "GameData.h" + +using namespace std; + +GameData & GameData::get_instance() { + static GameData instance; + return instance; +} + +GameData::GameData() { + this->db = make_unique("kerkersendraken.db"); +} + diff --git a/frontend/GameData.h b/frontend/GameData.h new file mode 100644 index 0000000..d4c3712 --- /dev/null +++ b/frontend/GameData.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +#include "DB.h" + +class GameData { +public: + static GameData & get_instance(); + +private: + GameData(); + virtual ~GameData() = default; + +private: + std::unique_ptr db = nullptr; +}; + diff --git a/frontend/cmd/query.cpp b/frontend/cmd/query.cpp index 367d6cd..3a26e9c 100644 --- a/frontend/cmd/query.cpp +++ b/frontend/cmd/query.cpp @@ -1,4 +1,5 @@ #include "backend/Location.h" +#include "backend/Object.h" #include "../Player.h" #include "../print.h" @@ -16,17 +17,31 @@ FollowupAction Player::cmd_query(Argv argv) { lprtf("Je staat bij de locatie %s.\n", this->location.get_name()); lprtf("%s\n", this->location.get_description()); - // TODO: visible objects + { + lprtf("Zichtbare objecten: "); + size_t objects = 0; + for (Object * obj : this->location.get_objects()) { + if (obj->get_hidden() == true) continue; + if (objects > 0) lprtf(", "); + lprtf("%s", obj->get_displayname()); + objects++; + } + if (objects == 0) + lprtf("(geen)"); + lprtf("\n"); + } - lprtf("Uitgangen: "); - bool first = true; - for (Direction direction : DIRECTIONS) { - if (this->location.get_exit(direction) == nullptr) continue; - if (!first) lprtf(", "); - lprtf("%s", direction_map.at(direction).c_str()); - first = false; + { + lprtf("Uitgangen: "); + bool first = true; + for (Direction direction : DIRECTIONS) { + if (this->location.get_exit(direction) == nullptr) continue; + if (!first) lprtf(", "); + lprtf("%s", direction_map.at(direction).c_str()); + first = false; + } + lprtf("\n"); } - lprtf("\n"); // TODO: enemies diff --git a/frontend/generate_dungeon.cpp b/frontend/generate_dungeon.cpp index c6ddcbf..e2a7c74 100644 --- a/frontend/generate_dungeon.cpp +++ b/frontend/generate_dungeon.cpp @@ -1,54 +1,16 @@ #include -#include -#include #include "backend/Dungeon.h" #include "generate_dungeon.h" -#include "Exception.h" +#include "GameData.h" using namespace std; -class DB { - typedef unique_ptr> unique_sqlite3; - typedef unique_ptr> unique_sqlite3_stmt; - -public: - DB(const string & path) { - sqlite3 * db = NULL; - int ret = sqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READONLY, NULL); - this->db = { - db, - [] (sqlite3 * db) { - sqlite3_close_v2(db); - }, - }; - if (ret != SQLITE_OK) - throw Exception("sqlite3_open_v2"); - } - - unique_sqlite3_stmt prepare(const string & query) { - sqlite3_stmt * stmt = NULL; - int ret = sqlite3_prepare_v2(this->db.get(), query.c_str(), query.size(), &stmt, NULL); - unique_sqlite3_stmt uniq_stmt = { - stmt, - [] (sqlite3_stmt * stmt) { - sqlite3_finalize(stmt); - }, - }; - if (ret != SQLITE_OK) - throw Exception("sqlite3_prepare_v2"); - return uniq_stmt; - } - -private: - unique_sqlite3 db = NULL; -}; - unique_ptr generate_dungeon() { unique_ptr dungeon = make_unique(); - DB db { "kerkersendraken.db" }; + GameData & gd = GameData::get_instance(); return dungeon; } diff --git a/frontend/load_dungeon.cpp b/frontend/load_dungeon.cpp index e94cbcb..b60a7b9 100644 --- a/frontend/load_dungeon.cpp +++ b/frontend/load_dungeon.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -54,22 +55,22 @@ unique_ptr load_dungeon(const string & filename) { temp_map[tag.attribute("id").as_uint()] = { .location = location, .edges = { - tag.attribute("noord").as_uint(0), - tag.attribute("oost").as_uint(0), - tag.attribute("zuid").as_uint(0), - tag.attribute("west").as_uint(0), + [Direction::NORTH] = tag.attribute("noord").as_uint(0), + [Direction::EAST] = tag.attribute("oost").as_uint(0), + [Direction::SOUTH] = tag.attribute("zuid").as_uint(0), + [Direction::WEST] = tag.attribute("west").as_uint(0), }, }; dungeon->add_location(location); } // connect edges after creating all locations - for (auto & [here, temp] : temp_map) { + for (auto & [_, temp] : temp_map) { for (Direction direction : DIRECTIONS) { - unsigned there = temp.edges[direction]; + unsigned id = temp.edges[direction]; if (temp.edges[direction] == 0) continue; - if (!temp_map.contains(there)) continue; - temp.location->set_exit(direction, temp_map[there].location); + if (!temp_map.contains(id)) continue; + temp.location->set_exit(direction, temp_map[id].location); } } -- cgit v1.2.3