From 4bd7eeec09629d3c0f900d42c899fe23c69e07b6 Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Fri, 14 Dec 2012 22:25:07 -0800 Subject: Prepare to trim skills --- src/common/mmo.hpp | 8 +-- src/common/utils.hpp | 3 ++ src/common/utils2.hpp | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+), 3 deletions(-) create mode 100644 src/common/utils2.hpp (limited to 'src/common') diff --git a/src/common/mmo.hpp b/src/common/mmo.hpp index 7441a16..eb8ed1c 100644 --- a/src/common/mmo.hpp +++ b/src/common/mmo.hpp @@ -18,7 +18,8 @@ # define MAX_AMOUNT 30000 # define MAX_ZENY 1000000000 // 1G zeny # define MAX_CART 100 -# define MAX_SKILL 450 +enum class SkillID : uint16_t; +constexpr SkillID MAX_SKILL = SkillID(474); // not 450 # define GLOBAL_REG_NUM 96 # define ACCOUNT_REG_NUM 16 # define ACCOUNT_REG2_NUM 16 @@ -70,7 +71,8 @@ struct point struct skill { - unsigned short id, lv, flags; + SkillID id; + unsigned short lv, flags; }; struct global_reg @@ -107,7 +109,7 @@ struct mmo_charstatus struct point last_point, save_point, memo_point[10]; struct item inventory[MAX_INVENTORY], cart[MAX_CART]; - struct skill skill[MAX_SKILL]; + earray skill; int global_reg_num; struct global_reg global_reg[GLOBAL_REG_NUM]; int account_reg_num; diff --git a/src/common/utils.hpp b/src/common/utils.hpp index fdd9d40..76ac626 100644 --- a/src/common/utils.hpp +++ b/src/common/utils.hpp @@ -3,6 +3,9 @@ #include "sanity.hpp" +// unguarded! +#include "utils2.hpp" + /* Notes about memory allocation in tmwAthena: There used to be 3 sources of allocation: these macros, diff --git a/src/common/utils2.hpp b/src/common/utils2.hpp new file mode 100644 index 0000000..aef6f73 --- /dev/null +++ b/src/common/utils2.hpp @@ -0,0 +1,138 @@ +// included by utils.hpp as a porting aid. +// Eventually it will be promoted to one or more normal headers. + +#include +#include + +template +struct earray +{ + // no ctor/dtor and one public member variable for easy initialization + T _data[size_t(max)]; + + T& operator[](E v) + { + return _data[size_t(v)]; + } + + const T& operator[](E v) const + { + return _data[size_t(v)]; + } + + T *begin() + { + return _data; + } + + T *end() + { + return _data + size_t(max); + } +}; + +template +class eptr +{ + T *_data; +public: + eptr(decltype(nullptr)=nullptr) + : _data(nullptr) + {} + + template + eptr(earray& arr) + : _data(arr._data) + {} + + T& operator [](E v) + { + return _data[size_t(v)]; + } + + explicit operator bool() + { + return _data; + } + + bool operator not() + { + return not _data; + } +}; + +template +class IteratorPair +{ + It b, e; +public: + IteratorPair(It b, It e) + : b(b), e(e) + {} + + It begin() { return b; } + It end() { return e; } +}; + +template +IteratorPair iterator_pair(It b, It e) +{ + return {b, e}; +} + +#ifndef HAVE_STD_UNDERLYING_TYPE +// Note: you *must* correctly define/not define this - it conflicts! +namespace std +{ + template + struct underlying_type + { + static_assert(std::is_enum::value, "Only enums have underlying type!"); + typedef typename std::conditional< + std::is_signed::value, + typename std::make_signed::type, + typename std::make_unsigned::type + >::type type; + }; +} +#endif // HAVE_STD_UNDERLYING_TYPE + +template +class EnumValueIterator +{ + typedef typename std::underlying_type::type U; + E value; +public: + EnumValueIterator(E v) + : value(v) + {} + + E operator *() + { + return value; + } + EnumValueIterator& operator++ () + { + value = E(U(value) + 1); + return *this; + } + EnumValueIterator& operator-- () + { + value = E(U(value) - 1); + return *this; + } + friend bool operator == (EnumValueIterator l, EnumValueIterator r) + { + return l.value == r.value; + } + friend bool operator != (EnumValueIterator l, EnumValueIterator r) + { + return !(l == r); + } +}; + +template +IteratorPair> erange(E b, E e) +{ + return {b, e}; +} -- cgit v1.2.3-60-g2f50