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