]> jfr.im git - irc/gameservirc.git/blob - gameserv-2.0/libgameservcore/include/GameServ/Types.h
Scrapping work on the File DAOs and going straight for MySQL for now. The file based...
[irc/gameservirc.git] / gameserv-2.0 / libgameservcore / include / GameServ / Types.h
1 #ifndef __GS__TYPES_H__
2 #define __GS__TYPES_H__
3
4 #include <string>
5 using std::string;
6 #include <GameServ/GameServException.h>
7 using GameServ::Exceptions::GameServException;
8
9 #include <ctime>
10 #include <boost/random/linear_congruential.hpp>
11 #include <boost/random/uniform_int.hpp>
12 #include <boost/random/uniform_real.hpp>
13 #include <boost/random/variate_generator.hpp>
14 // Sun CC doesn't handle boost::iterator_adaptor yet
15 #if !defined(__SUNPRO_CC) || (__SUNPRO_CC > 0x530)
16 #include <boost/generator_iterator.hpp>
17 #endif
18
19 #ifdef BOOST_NO_STDC_NAMESPACE
20 namespace std {
21 using ::time;
22 }
23 #endif
24
25 // This is a typedef for a random number generator.
26 // Try boost::mt19937 or boost::ecuyer1988 instead of boost::minstd_rand
27 typedef boost::minstd_rand base_generator_type;
28
29
30 namespace GameServ
31 {
32 namespace Types
33 {
34
35 //! Holds the exceptions related to types and their conversions
36 namespace Exceptions
37 {
38 //! Generic type exception possibly due to conversion or improper usage
39 class TypeException : public GameServException
40 {
41 public:
42 TypeException(const string &ErrorMsg, const char *pFilename, int SourceLine);
43 TypeException(const string &ErrorMsg);
44 TypeException(void);
45 };
46 }
47
48 class ItemTypes
49 {
50 public:
51 //! Logical ItemType enumerator
52 enum ItemType
53 {
54 Weapon, Armor, Potion, NOTYPE
55 };
56
57 //! Get the name of an item type
58 static string GetName(ItemType itemtype);
59
60 //! Parse the name and return the enumeration value
61 static ItemType Parse(const string &itemtype);
62
63 private:
64 //! Internal struct to hold type information statically in a table
65 typedef struct itemtypeinfo
66 {
67 ItemTypes::ItemType itemtype; //!< The ItemType enumeration value
68 string name; //!< The Name of the type
69 } itemtypeinfo;
70
71 static const itemtypeinfo mItemTypeInfoTable[];
72
73 }; // ItemTypes class
74
75 class ObjectTypes
76 {
77 public:
78 //! Logical ObjectType enumerator
79 enum ObjectType
80 {
81 Player, Item, Level, Master, Monster
82 };
83
84 //! Get the name of an object type
85 static string GetName(ObjectType objecttype);
86
87 //! Parse the name and return the enumeration value
88 static ObjectType Parse(const string &objecttype);
89
90 private:
91 //! Internal struct to hold type information statically in a table
92 typedef struct objecttypeinfo
93 {
94 ObjectTypes::ObjectType objecttype; //!< The ObjectType enumeration value
95 string name; //!< The Name of the type
96 } objecttypeinfo;
97
98 static const objecttypeinfo mObjectTypeInfoTable[];
99
100 }; // ObjectTypes class
101
102 class Modifiers
103 {
104 public:
105 //! Logical Modifer enumerator
106 enum Modifier
107 {
108 strength, defense, maxhealth, health, forestfights, playerfights, gold, bank, experience
109 };
110
111 //! Get the name of a modifier
112 static string GetName(Modifier modifier);
113
114 //! Parse the name and return the modifier enumeration value
115 static Modifier Parse(const string &modifier);
116 private:
117 typedef struct modifierinfo
118 {
119 Modifiers::Modifier modifier; //!< The modifier enumeration value
120 string name; //!< The name of the modifier
121 } modifierinfo;
122 static const modifierinfo mModifierInfoTable[];
123 }; // Modifiers class
124
125 template <class T>
126 class Range
127 {
128 public:
129
130 Range()
131 {
132 }
133
134 Range(const T &high, const T &low)
135 {
136 mHigh = high;
137 mLow = low;
138 mLastRandom = T(0);
139 }
140
141 ~Range()
142 {
143 }
144
145 //! Generate a random number within the range
146 T Random()
147 {
148 static base_generator_type generator(static_cast<T>(std::time(0)));
149 if (mLow == mHigh)
150 {
151 return mLow;
152 }
153 else
154 {
155 boost::uniform_int<> uni_dist(mLow, mHigh);
156 boost::variate_generator<base_generator_type&, boost::uniform_int<> > uni(generator, uni_dist);
157 mLastRandom = uni();
158 return mLastRandom;
159 }
160 }
161
162 //! Property get - High
163 T High(void) const
164 {
165 return mHigh;
166 }
167
168 //! Property set - High
169 void High(const T &value)
170 {
171 if (value >= mLow)
172 {
173 mHigh = value;
174 }
175 }
176
177 //! Property get - Low
178 T Low(void) const
179 {
180 return mLow;
181 }
182
183 //! Property set - Low
184 void Low(const T &value)
185 {
186 if (value <= mHigh)
187 {
188 mLow = value;
189 }
190 }
191
192 //! Property get - Last random number to be generated
193 T LastRandom(void) const
194 {
195 return mLastRandom;
196 }
197
198 private:
199 T mHigh;
200 T mLow;
201 T mLastRandom;
202 };
203 }
204 }
205 #endif