diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-30 19:59:38 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-30 19:59:38 +0100 |
commit | 6e1d62955c7a7f39bc9126d709a42a70e02a1d30 (patch) | |
tree | c2505409c68d554b1e776cdb0c8104af54d375bf /backend | |
parent | c1d43cddee94dd370078f755d33147c9a8181852 (diff) |
create backend string class
Diffstat (limited to 'backend')
-rw-r--r-- | backend/CMakeLists.txt | 1 | ||||
-rw-r--r-- | backend/Dungeon.cpp | 10 | ||||
-rw-r--r-- | backend/Enemy.cpp | 31 | ||||
-rw-r--r-- | backend/Enemy.h | 18 | ||||
-rw-r--r-- | backend/EnemyFactory.cpp | 2 | ||||
-rw-r--r-- | backend/EnemyFactory.h | 2 | ||||
-rw-r--r-- | backend/GoldObject.cpp | 5 | ||||
-rw-r--r-- | backend/GoldObject.h | 1 | ||||
-rw-r--r-- | backend/List.h | 10 | ||||
-rw-r--r-- | backend/List.hpp | 10 | ||||
-rw-r--r-- | backend/ListIterator.h | 8 | ||||
-rw-r--r-- | backend/ListIterator.hpp | 4 | ||||
-rw-r--r-- | backend/Location.cpp | 48 | ||||
-rw-r--r-- | backend/Location.h | 35 | ||||
-rw-r--r-- | backend/LocationFactory.cpp | 2 | ||||
-rw-r--r-- | backend/LocationFactory.h | 2 | ||||
-rw-r--r-- | backend/Object.cpp | 34 | ||||
-rw-r--r-- | backend/Object.h | 20 | ||||
-rw-r--r-- | backend/ObjectFactory.cpp | 2 | ||||
-rw-r--r-- | backend/ObjectFactory.h | 6 | ||||
-rw-r--r-- | backend/String.cpp | 65 | ||||
-rw-r--r-- | backend/String.h | 28 | ||||
-rw-r--r-- | backend/WeaponObject.cpp | 5 | ||||
-rw-r--r-- | backend/WeaponObject.h | 1 | ||||
-rwxr-xr-x | backend/check | 6 | ||||
-rw-r--r-- | backend/util.cpp | 4 | ||||
-rw-r--r-- | backend/util.h | 1 |
27 files changed, 233 insertions, 128 deletions
diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 6821388..3c6991a 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -12,5 +12,6 @@ target_sources(main PUBLIC GoldObject.cpp WeaponObject.cpp Enemy.cpp + String.cpp ) diff --git a/backend/Dungeon.cpp b/backend/Dungeon.cpp index 66a1fa3..e1cee70 100644 --- a/backend/Dungeon.cpp +++ b/backend/Dungeon.cpp @@ -13,7 +13,15 @@ Dungeon::~Dungeon() { } void Dungeon::update() { - + // TODO: iterators are broken (!????) + // for (Location * location : this->locations) { + // for (Enemy * enemy : location->get_enemies()) { + // if (RNG::get().rand_double() < 0.5) continue; + // Direction direction = random_direction(*location); + // // location->remove_enemy(enemy); // TODO: this breaks the for loop + // location->get_exit(direction)->add_enemy(enemy); + // } + // } } void Dungeon::add_location(Location * location) { diff --git a/backend/Enemy.cpp b/backend/Enemy.cpp index 699a303..bb39359 100644 --- a/backend/Enemy.cpp +++ b/backend/Enemy.cpp @@ -1,31 +1,10 @@ -#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); -} +Enemy::Enemy(const String & name, const String & description) : name(name), description(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_name(const String & name) { this->name = name; } +const String & Enemy::get_name() const { 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; -} +void Enemy::set_description(const String & description) { this->description = description; } +const String & Enemy::get_description() const { return this->description; } diff --git a/backend/Enemy.h b/backend/Enemy.h index fdc095e..3c58c82 100644 --- a/backend/Enemy.h +++ b/backend/Enemy.h @@ -1,20 +1,22 @@ #pragma once +#include "backend/String.h" + class Enemy { public: - void set_name(const char * name); - const char * get_name(); - void set_description(const char * description); - const char * get_description(); + void set_name(const String & name); + const String & get_name() const; + void set_description(const String & description); + const String & get_description() const; private: friend class EnemyFactory; - Enemy(const char * name = "", const char * description = ""); + Enemy(const String & name, const String & description); public: - virtual ~Enemy(); + virtual ~Enemy() = default; private: - const char * name = nullptr; - const char * description = nullptr; + String name; + String description; }; diff --git a/backend/EnemyFactory.cpp b/backend/EnemyFactory.cpp index 42e35ae..f022c2d 100644 --- a/backend/EnemyFactory.cpp +++ b/backend/EnemyFactory.cpp @@ -1,6 +1,6 @@ #include "EnemyFactory.h" -Enemy * EnemyFactory::create_enemy(const char * name, const char * description) { +Enemy * EnemyFactory::create_enemy(const String & name, const String & description) { return new Enemy(name, description); } diff --git a/backend/EnemyFactory.h b/backend/EnemyFactory.h index 6e8b6db..7aa8645 100644 --- a/backend/EnemyFactory.h +++ b/backend/EnemyFactory.h @@ -4,7 +4,7 @@ class EnemyFactory { public: - static Enemy * create_enemy(const char * name = "", const char * description = ""); + static Enemy * create_enemy(const String & name = "", const String & description = ""); private: EnemyFactory() = delete; diff --git a/backend/GoldObject.cpp b/backend/GoldObject.cpp index cf97b2e..525b2f2 100644 --- a/backend/GoldObject.cpp +++ b/backend/GoldObject.cpp @@ -7,3 +7,8 @@ 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; +} + diff --git a/backend/GoldObject.h b/backend/GoldObject.h index 03a074e..a532f07 100644 --- a/backend/GoldObject.h +++ b/backend/GoldObject.h @@ -8,6 +8,7 @@ class GoldObject : public Object { public: void set_count(const int & count); int get_count() const; + virtual const String & get_displayname() const; private: int count = 0; diff --git a/backend/List.h b/backend/List.h index 1d8893e..58efbe2 100644 --- a/backend/List.h +++ b/backend/List.h @@ -18,7 +18,7 @@ struct ListLink { template<typename T> class List { public: - size_t size(); + size_t size() const; void push_back(T & el); void remove(const T & val); @@ -27,11 +27,11 @@ public: void clear(); - T & operator [] (size_t index); + T & operator [] (size_t index) const; - ListIterator<T> begin(); - ListIterator<T> end(); - ListRange<T> range(); + ListIterator<T> begin() const; + ListIterator<T> end() const; + ListRange<T> range() const; private: ListLink<T> * head = nullptr; diff --git a/backend/List.hpp b/backend/List.hpp index 17b6ce7..c2c5ce2 100644 --- a/backend/List.hpp +++ b/backend/List.hpp @@ -3,7 +3,7 @@ #include "List.h" template <typename T> -size_t List<T>::size() { +size_t List<T>::size() const { return this->length; } @@ -63,7 +63,7 @@ void List<T>::clear() { } template <typename T> -T & List<T>::operator [] (size_t index) { +T & List<T>::operator [] (size_t index) const { ListLink<T> * link = this->tail; for (size_t i = 0; i < index; i++) link = link->next; @@ -71,17 +71,17 @@ T & List<T>::operator [] (size_t index) { } template <typename T> -ListRange<T> List<T>::range() { +ListRange<T> List<T>::range() const { return { *this }; } template <typename T> -ListIterator<T> List<T>::begin() { +ListIterator<T> List<T>::begin() const { return this->range().begin(); } template <typename T> -ListIterator<T> List<T>::end() { +ListIterator<T> List<T>::end() const { return this->range().end(); } diff --git a/backend/ListIterator.h b/backend/ListIterator.h index 5e4774b..c7fbf4a 100644 --- a/backend/ListIterator.h +++ b/backend/ListIterator.h @@ -10,7 +10,7 @@ class List; template <typename T> class ListIterator { public: - ListIterator(List<T> & list, size_t index); + ListIterator(const List<T> & list, size_t index); public: T operator * () const; @@ -18,14 +18,14 @@ public: bool operator != (const ListIterator<T> &) const; private: - List<T> & list; + const List<T> & list; size_t index; }; template <typename T> class ListRange { public: - ListRange(List<T> & list); + ListRange(const List<T> & list); public: ListIterator<T> begin() const; @@ -33,7 +33,7 @@ public: size_t size() const; private: - List<T> & list; + const List<T> & list; }; #include "ListIterator.hpp" diff --git a/backend/ListIterator.hpp b/backend/ListIterator.hpp index 5e4ea91..b6b7a36 100644 --- a/backend/ListIterator.hpp +++ b/backend/ListIterator.hpp @@ -3,7 +3,7 @@ #include "ListIterator.h" template <typename T> -ListRange<T>::ListRange(List<T> & list) : list(list) { } +ListRange<T>::ListRange(const List<T> & list) : list(list) { } template <typename T> ListIterator<T> ListRange<T>::begin() const { @@ -21,7 +21,7 @@ size_t ListRange<T>::size() const { } template <typename T> -ListIterator<T>::ListIterator(List<T> & list, size_t index) : list(list), index(index) { } +ListIterator<T>::ListIterator(const List<T> & list, size_t index) : list(list), index(index) { } template <typename T> T ListIterator<T>::operator * () const { diff --git a/backend/Location.cpp b/backend/Location.cpp index 31df1e3..d3227cf 100644 --- a/backend/Location.cpp +++ b/backend/Location.cpp @@ -1,43 +1,40 @@ -#include <string.h> - +#include "RNG.h" #include "Location.h" #include "ListIterator.h" #include "Enemy.h" #include "Object.h" #include "util.h" -Location::Location(const char * name, const char * description) { - this->set_name(name); - this->set_description(description); +Direction random_direction() { + return DIRECTIONS[RNG::get().rand_int(4)]; +} + +Direction random_direction(const Location & location) { + List<Direction> valid = {}; + for (Direction direction : DIRECTIONS) { + if (location.get_exit(direction) == nullptr) continue; + valid.push_back(direction); + } + return valid[RNG::get().rand_int(valid.size())]; } +Location::Location(const String & name, const String & description) : name(name), description(description) { } + Location::~Location() { - safe_free(this->name); - safe_free(this->description); safe_free(this->enemies); safe_free(this->objects); } -void Location::set_name(const char * name) { - safe_free(this->name); - this->name = strdup(name); -} -const char * Location::get_name() { - return this->name; -} +void Location::set_name(const String & name) { this->name = name; } +const String & Location::get_name() const { return this->name; } -void Location::set_description(const char * description) { - safe_free(this->description); - this->description = strdup(description); -} -const char * Location::get_description() { - return this->description; -} +void Location::set_description(const String & description) { this->description = description; } +const String & Location::get_description() const { return this->description; } void Location::set_exit(Direction dir, Location * location) { this->edges[dir] = location; } -Location * Location::get_exit(Direction dir) { +Location * Location::get_exit(Direction dir) const { return this->edges[dir]; } @@ -47,14 +44,17 @@ void Location::add_object(Object * object) { void Location::remove_object(Object * object) { this->objects.remove(object); } -ListRange<Object *> Location::get_objects() { +ListRange<Object *> Location::get_objects() const { return this->objects.range(); } void Location::add_enemy(Enemy * enemy) { this->enemies.push_back(enemy); } -ListRange<Enemy *> Location::get_enemies() { +void Location::remove_enemy(Enemy * enemy) { + this->enemies.remove(enemy); +} +ListRange<Enemy *> Location::get_enemies() const { return this->enemies.range(); } diff --git a/backend/Location.h b/backend/Location.h index fb0abfc..b139da8 100644 --- a/backend/Location.h +++ b/backend/Location.h @@ -2,9 +2,11 @@ #include "List.h" #include "ListIterator.h" +#include "backend/String.h" class Enemy; class Object; +class Location; enum Direction { NORTH = 0, @@ -14,31 +16,39 @@ enum Direction { }; static constexpr const Direction DIRECTIONS[] = { NORTH, EAST, SOUTH, WEST }; +Direction random_direction(); +Direction random_direction(const Location &); + class Location { public: - void set_name(const char * name); - const char * get_name(); - void set_description(const char * description); - const char * get_description(); + void set_name(const String & name); + const String & get_name() const; + + void set_description(const String & description); + const String & get_description() const; + void set_exit(Direction dir, Location * location = nullptr); - Location * get_exit(Direction dir); + Location * get_exit(Direction dir) const; + void add_object(Object *); void remove_object(Object *); - ListRange<Object *> get_objects(); + ListRange<Object *> get_objects() const; + void add_enemy(Enemy *); - ListRange<Enemy *> get_enemies(); + void remove_enemy(Enemy *); + ListRange<Enemy *> get_enemies() const; private: friend class LocationFactory; - Location(const char * name = "", const char * description = ""); + Location(const String & name, const String & description); public: virtual ~Location(); private: - const char * name = nullptr; - const char * description = nullptr; - List<Enemy*> enemies = {}; - List<Object*> objects = {}; + String name; + String description; + List<Enemy*> enemies; + List<Object*> objects; Location * edges[4] = { nullptr, @@ -47,3 +57,4 @@ private: nullptr, }; }; + diff --git a/backend/LocationFactory.cpp b/backend/LocationFactory.cpp index 6e0b6b8..106867d 100644 --- a/backend/LocationFactory.cpp +++ b/backend/LocationFactory.cpp @@ -1,6 +1,6 @@ #include "LocationFactory.h" -Location * LocationFactory::create_location(const char * name, const char * description) { +Location * LocationFactory::create_location(const String & name, const String & description) { return new Location(name, description); } diff --git a/backend/LocationFactory.h b/backend/LocationFactory.h index 178d5d3..864962a 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 String & name = "", const String & description = ""); private: LocationFactory() = delete; diff --git a/backend/Object.cpp b/backend/Object.cpp index ed8bc46..2640607 100644 --- a/backend/Object.cpp +++ b/backend/Object.cpp @@ -1,36 +1,22 @@ -#include <string.h> -#include <stdlib.h> - #include "Object.h" -#include "util.h" - -Object::Object(const char * name, const char * description) { - this->set_name(name); - this->set_description(description); -} - -Object::~Object() { - safe_free(this->name); - safe_free(this->description); -} +Object::Object(const String & name, const String & description) : name(name), description(description) { } -void Object::set_name(const char * name) { - safe_free(this->name); - this->name = strdup(name); +void Object::set_name(const String & name) { + this->name = name; } -const char * Object::get_name() { +const String & Object::get_name() const { return this->name; } -const char * Object::get_displayname() { - return this->get_name(); +const String & Object::get_displayname() const { + static String displayname = this->name; + return displayname; } -void Object::set_description(const char * description) { - safe_free(this->description); - this->description = strdup(description); +void Object::set_description(const String & description) { + this->description = description; } -const char * Object::get_description() { +const String & Object::get_description() const { return this->description; } diff --git a/backend/Object.h b/backend/Object.h index b98dc82..07b7b74 100644 --- a/backend/Object.h +++ b/backend/Object.h @@ -1,24 +1,26 @@ #pragma once +#include "String.h" + class Object { private: - const char * name = nullptr; - const char * description = nullptr; + String name; + String description; 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_name(const String & name); + const String & get_name() const; + 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; - Object(const char * name = "", const char * description = ""); + Object(const String & name, const String & description); public: - virtual ~Object(); + virtual ~Object() = default; protected: bool hidden = false; diff --git a/backend/ObjectFactory.cpp b/backend/ObjectFactory.cpp index d8e1c21..a3a6d83 100644 --- a/backend/ObjectFactory.cpp +++ b/backend/ObjectFactory.cpp @@ -34,7 +34,7 @@ Object * ObjectFactory::create_object(const UniversalObject & data) { return ObjectFactory::create_object(data.name, data.description); } -Object * ObjectFactory::create_object(const char * name, const char * description) { +Object * ObjectFactory::create_object(const String & name, const String & description) { return new Object(name, description); } diff --git a/backend/ObjectFactory.h b/backend/ObjectFactory.h index 60123bd..440d4bd 100644 --- a/backend/ObjectFactory.h +++ b/backend/ObjectFactory.h @@ -11,8 +11,8 @@ enum ObjectType { // database object table row struct UniversalObject { - const char * name; - const char * description; + String name; + String description; ObjectType type; int min_value; int max_value; @@ -22,7 +22,7 @@ struct UniversalObject { class ObjectFactory { public: static Object * create_object(const UniversalObject & universal); - static Object * create_object(const char * name = "", const char * description = ""); + static Object * create_object(const String & name = "", const String & description = ""); private: ObjectFactory() = delete; diff --git a/backend/String.cpp b/backend/String.cpp new file mode 100644 index 0000000..2b40c88 --- /dev/null +++ b/backend/String.cpp @@ -0,0 +1,65 @@ +#include <string.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> + +#include "String.h" +#include "util.h" + +String::String() { + this->set(""); +} + +String::String(const String & other) { + this->set(other.data(), other.size()); +} + +String::String(const char * c_str) { + if (c_str == nullptr) c_str = ""; + this->set(c_str); +} + +String String::fmt(const char * fmt, ...) { + String out; + va_list args, args_copy; + va_start(args, fmt); + + 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; +} + +String::~String() { + safe_free(this->_data); +} + +void String::set(const char * data) { + this->set(data, strlen(data)); +} + +void String::set(const char * data, size_t data_len) { + safe_free(this->_data); + this->_data = static_cast<char *>(malloc(data_len + 1)); + this->_data_len = data_len; + memcpy(this->_data, data, data_len); + this->_data[this->_data_len] = '\0'; +} + +const char * String::c_str() const { + return this->_data; +} +char * String::data() const { + return this->_data; +} +size_t String::size() const { + return this->_data_len; +} +bool String::empty() const { + return this->_data_len == 0; +} + diff --git a/backend/String.h b/backend/String.h new file mode 100644 index 0000000..b4025be --- /dev/null +++ b/backend/String.h @@ -0,0 +1,28 @@ +#pragma once + +#include <stddef.h> + +class String { +public: + String(); + String(const char * c_str); + String(const String &); + ~String(); +public: + static String fmt(const char * fmt, ...); + +public: + const char * c_str() const; + char * data() const; + size_t size() const; + bool empty() const; + +private: + void set(const char * data); + void set(const char * data, size_t data_len); +private: + char * _data = nullptr; + size_t _data_len = 0; + +}; + diff --git a/backend/WeaponObject.cpp b/backend/WeaponObject.cpp index ac7defd..a92f9c8 100644 --- a/backend/WeaponObject.cpp +++ b/backend/WeaponObject.cpp @@ -8,3 +8,8 @@ void WeaponObject::set_damage_max(int damage_max) { this->damage_max = 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; +} + diff --git a/backend/WeaponObject.h b/backend/WeaponObject.h index d4cfa7a..e1347ec 100644 --- a/backend/WeaponObject.h +++ b/backend/WeaponObject.h @@ -8,6 +8,7 @@ class WeaponObject : public Object { public: void set_damage_min(int damage_min); void set_damage_max(int damage_max); + virtual const String & get_displayname() const; private: int damage_min; diff --git a/backend/check b/backend/check index b283acf..f100e41 100755 --- a/backend/check +++ b/backend/check @@ -3,9 +3,15 @@ for file in *.cpp *.hpp *.h ; do # non C-style headers grep -Hn '#include\s*<[^.]\+>' "$file" | grep -v '<random>' # forbidden STL containers + grep -Hn '\<unique_ptr\>' "$file" grep -Hn '\<make_unique\>' "$file" + grep -Hn '\<shared_ptr\>' "$file" + grep -Hn '\<make_shared\>' "$file" grep -Hn '\<vector\>' "$file" + grep -Hn '\<string\>[^.]' "$file" # [^.] is to ignore C's <string.h> grep -Hn '\<pair\>' "$file" + grep -Hn '\<map\>' "$file" + grep -Hn '\<unordered_map\>' "$file" done exit 0 diff --git a/backend/util.cpp b/backend/util.cpp index 4a55f63..85b6950 100644 --- a/backend/util.cpp +++ b/backend/util.cpp @@ -11,4 +11,8 @@ void safe_free(const char * & ptr) { auto x = static_cast<void *>(const_cast<char *>(ptr)); safe_free(x); } +void safe_free(char * & ptr) { + auto x = static_cast<void *>(ptr); + safe_free(x); +} diff --git a/backend/util.h b/backend/util.h index 44d9a49..bbe307b 100644 --- a/backend/util.h +++ b/backend/util.h @@ -3,6 +3,7 @@ #include "List.h" void safe_free(void * & ptr); +void safe_free(char * & ptr); void safe_free(const char * & ptr); template <typename T> void safe_free(List<T *> & ptr_list); |