]> jfr.im git - irc/gameservirc.git/blob - gameserv-2.0/libgameservcore/src/Types.cpp
Changed the ItemGO class to be abstract so you cannot define a simple ItemGO object...
[irc/gameservirc.git] / gameserv-2.0 / libgameservcore / src / Types.cpp
1 #include <GameServ/Types.h>
2 using GameServ::Types::ItemTypes;
3 using GameServ::Types::Modifiers;
4 using GameServ::Types::Range;
5 using GameServ::Types::ObjectTypes;
6 using GameServ::Types::Exceptions::TypeException;
7
8 #include <string>
9 using std::string;
10
11
12 using std::exception;
13
14 #include <boost/format.hpp>
15 #include <boost/algorithm/string.hpp>
16 using boost::format;
17 using boost::str;
18
19 #include <ctime>
20 #include <boost/random/linear_congruential.hpp>
21 #include <boost/random/uniform_int.hpp>
22 #include <boost/random/uniform_real.hpp>
23 #include <boost/random/variate_generator.hpp>
24 // Sun CC doesn't handle boost::iterator_adaptor yet
25 #if !defined(__SUNPRO_CC) || (__SUNPRO_CC > 0x530)
26 #include <boost/generator_iterator.hpp>
27 #endif
28
29 #ifdef BOOST_NO_STDC_NAMESPACE
30 namespace std {
31 using ::time;
32 }
33 #endif
34
35 // This is a typedef for a random number generator.
36 // Try boost::mt19937 or boost::ecuyer1988 instead of boost::minstd_rand
37 typedef boost::minstd_rand base_generator_type;
38
39 TypeException::TypeException(const string &ErrorMsg, const char *pFilename, int SourceLine)
40 : GameServException(ErrorMsg, pFilename, SourceLine)
41 { }
42
43 TypeException::TypeException(const string &ErrorMsg)
44 : GameServException(ErrorMsg)
45 { }
46
47 TypeException::TypeException(void)
48 : GameServException("Type Exception occurred either do to conversion or improper type usage.", __FILE__, __LINE__)
49 { }
50
51 const ItemTypes::itemtypeinfo ItemTypes::mItemTypeInfoTable[] =
52 {
53 { Weapon, "Weapon" }, { Armor, "Armor" }, { Potion, "Potion"}, { NOTYPE, "NOTYPE" }
54 }; // mItemTypeInfoTable[]
55
56 string ItemTypes::GetName(ItemTypes::ItemType itemtype)
57 {
58 for (unsigned int i = 0; i < sizeof(mItemTypeInfoTable) / sizeof(itemtypeinfo); i++)
59 {
60 if (mItemTypeInfoTable[i].itemtype == itemtype)
61 {
62 return mItemTypeInfoTable[i].name;
63 }
64 }
65
66 // This should never happen because an enumeration is passed, but in the case where a user
67 // may attempt an invalid integer cast, it provides protection
68 throw TypeException(str(format("No item type found for %1% enumeration value") % itemtype).c_str());
69 }
70
71 ItemTypes::ItemType ItemTypes::Parse(const string &itemtype)
72 {
73 for (unsigned int i = 0; i < sizeof(mItemTypeInfoTable) / sizeof(itemtypeinfo); i++)
74 {
75 if (mItemTypeInfoTable[i].name == itemtype)
76 {
77 return mItemTypeInfoTable[i].itemtype;
78 }
79 }
80 throw TypeException(str(format("No itemtype enumeration found for %1%") % itemtype), __FILE__, __LINE__);
81 }
82
83 const ObjectTypes::objecttypeinfo ObjectTypes::mObjectTypeInfoTable[] =
84 {
85 { Player, "Player" }, { Level, "Level" }, { Item, "Item" },
86 { Master, "Master" }, { Monster, "Monster" }
87 }; // mObjectTypeInfoTable[]
88
89 string ObjectTypes::GetName(ObjectTypes::ObjectType objecttype)
90 {
91 for (unsigned int i = 0; i < sizeof(mObjectTypeInfoTable) / sizeof(objecttypeinfo); i++)
92 {
93 if (mObjectTypeInfoTable[i].objecttype == objecttype)
94 {
95 return mObjectTypeInfoTable[i].name;
96 }
97 }
98
99 // This should never happen because an enumeration is passed, but in the case where a user
100 // may attempt an invalid integer cast, it provides protection
101 throw TypeException(str(format("No object type found for %1% enumeration value") % objecttype).c_str());
102 }
103
104 ObjectTypes::ObjectType ObjectTypes::Parse(const string &objecttype)
105 {
106 for (unsigned int i = 0; i < sizeof(mObjectTypeInfoTable) / sizeof(objecttypeinfo); i++)
107 {
108 if (mObjectTypeInfoTable[i].name == objecttype)
109 {
110 return mObjectTypeInfoTable[i].objecttype;
111 }
112 }
113 throw TypeException(str(format("No object type enumeration found for %1%") % objecttype), __FILE__, __LINE__);
114 }
115
116
117 const Modifiers::modifierinfo Modifiers::mModifierInfoTable[] =
118 {
119 { strength, "Strength"}, { defense, "Defense" }, { maxhealth, "MaxHealth" }, { health, "Health" }, { forestfights, "ForestFights" },
120 { playerfights, "PlayerFights" }, { gold, "Gold" }, { bank, "Bank" }
121 };
122
123 string Modifiers::GetName(Modifiers::Modifier modifier)
124 {
125 for (unsigned int i = 0; i < sizeof(mModifierInfoTable) / sizeof(modifierinfo); i++)
126 {
127 if (mModifierInfoTable[i].modifier == modifier)
128 {
129 return mModifierInfoTable[i].name;
130 }
131 }
132
133 // This should never happen because an enumeration is passed, but in the case where a user
134 // may attempt an invalid integer cast, it provides protection
135 throw TypeException(str(format("No item type found for %1% enumeration value") % modifier).c_str());
136 }
137
138 Modifiers::Modifier Modifiers::Parse(const string &modifier)
139 {
140 for (unsigned int i = 0; i < sizeof(mModifierInfoTable) / sizeof(modifierinfo); i++)
141 {
142 if (mModifierInfoTable[i].name == modifier)
143 {
144 return mModifierInfoTable[i].modifier;
145 }
146 }
147
148 throw TypeException(str(format("No modifier enumeration found for %1%") % modifier), __FILE__, __LINE__);
149 }
150
151 Range::Range()
152 {
153 mHigh = 0;
154 mLow = 0;
155 mLastRandom = 0;
156 }
157
158 Range::Range(const int &high, const int &low)
159 {
160 mHigh = high;
161 mLow = low;
162 mLastRandom = 0;
163 }
164
165 Range::~Range()
166 {
167 mHigh = 0;
168 mLow = 0;
169 mLastRandom = 0;
170 }
171
172 void Range::High(const int &value)
173 {
174 if (mHigh >= mLow)
175 mHigh = value;
176 }
177
178 int Range::High(void) const
179 {
180 return mHigh;
181 }
182
183 void Range::Low(const int &value)
184 {
185 if (mLow <= mHigh)
186 mLow = value;
187 }
188
189 int Range::Low(void) const
190 {
191 return mLow;
192 }
193
194 int Range::Random()
195 {
196 static base_generator_type generator(static_cast<int>(std::time(0)));
197 boost::uniform_int<> uni_dist(mHigh, mLow);
198 boost::variate_generator<base_generator_type&, boost::uniform_int<> > uni(generator, uni_dist);
199 mLastRandom = uni();
200 return mLastRandom;
201 }
202
203 int Range::LastRandom(void) const
204 {
205 return mLastRandom;
206 }