]> jfr.im git - irc/gameservirc.git/blob - gameserv-2.0/libgameservcore/src/Types.cpp
Changed all modifiers to be ranges. A single number is now treated as a range of...
[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::Exceptions::TypeException;
6
7 #include <string>
8 using std::string;
9
10
11 using std::exception;
12
13 #include <boost/format.hpp>
14 #include <boost/algorithm/string.hpp>
15 using boost::format;
16 using boost::str;
17
18 #include <ctime>
19 #include <boost/random/linear_congruential.hpp>
20 #include <boost/random/uniform_int.hpp>
21 #include <boost/random/uniform_real.hpp>
22 #include <boost/random/variate_generator.hpp>
23 // Sun CC doesn't handle boost::iterator_adaptor yet
24 #if !defined(__SUNPRO_CC) || (__SUNPRO_CC > 0x530)
25 #include <boost/generator_iterator.hpp>
26 #endif
27
28 #ifdef BOOST_NO_STDC_NAMESPACE
29 namespace std {
30 using ::time;
31 }
32 #endif
33
34 // This is a typedef for a random number generator.
35 // Try boost::mt19937 or boost::ecuyer1988 instead of boost::minstd_rand
36 typedef boost::minstd_rand base_generator_type;
37
38 TypeException::TypeException(const string &ErrorMsg, const char *pFilename, int SourceLine)
39 : GameServException(ErrorMsg, pFilename, SourceLine)
40 { }
41
42 TypeException::TypeException(const string &ErrorMsg)
43 : GameServException(ErrorMsg)
44 { }
45
46 TypeException::TypeException(void)
47 : GameServException("Type Exception occurred either do to conversion or improper type usage.", __FILE__, __LINE__)
48 { }
49
50 const ItemTypes::itemtypeinfo ItemTypes::mItemTypeInfoTable[] =
51 {
52 { Weapon, "Weapon" }, { Armor, "Armor" }, { Potion, "Potion"}
53 }; // mItemTypeInfoTable[]
54
55 string ItemTypes::GetName(ItemTypes::ItemType itemtype)
56 {
57 for (unsigned int i = 0; i < sizeof(mItemTypeInfoTable) / sizeof(itemtypeinfo); i++)
58 {
59 if (mItemTypeInfoTable[i].itemtype == itemtype)
60 {
61 return mItemTypeInfoTable[i].name;
62 }
63 }
64
65 // This should never happen because an enumeration is passed, but in the case where a user
66 // may attempt an invalid integer cast, it provides protection
67 throw TypeException(str(format("No item type found for %1% enumeration value") % itemtype).c_str());
68 }
69
70 ItemTypes::ItemType ItemTypes::Parse(const string &itemtype)
71 {
72 for (unsigned int i = 0; i < sizeof(mItemTypeInfoTable) / sizeof(itemtypeinfo); i++)
73 {
74 if (mItemTypeInfoTable[i].name == itemtype)
75 {
76 return mItemTypeInfoTable[i].itemtype;
77 }
78 }
79 return ItemTypes::NOTYPE;
80 }
81
82 const Modifiers::modifierinfo Modifiers::mModifierInfoTable[] =
83 {
84 { strength, "Strength"}, { defense, "Defense" }, { maxhealth, "MaxHealth" }, { health, "Health" }, { forestfights, "Forest Fights" },
85 { playerfights, "Player Fights" }, { gold, "Gold" }, { bank, "Bank" }
86 };
87
88 string Modifiers::GetName(Modifiers::Modifier modifier)
89 {
90 for (unsigned int i = 0; i < sizeof(mModifierInfoTable) / sizeof(modifierinfo); i++)
91 {
92 if (mModifierInfoTable[i].modifier == modifier)
93 {
94 return mModifierInfoTable[i].name;
95 }
96 }
97
98 // This should never happen because an enumeration is passed, but in the case where a user
99 // may attempt an invalid integer cast, it provides protection
100 throw TypeException(str(format("No item type found for %1% enumeration value") % modifier).c_str());
101 }
102
103 Modifiers::Modifier Modifiers::Parse(const string &modifier)
104 {
105 for (unsigned int i = 0; i < sizeof(mModifierInfoTable) / sizeof(modifierinfo); i++)
106 {
107 if (mModifierInfoTable[i].name == modifier)
108 {
109 return mModifierInfoTable[i].modifier;
110 }
111 }
112
113 throw TypeException(str(format("No modifier enumeration found for %1%") % modifier), __FILE__, __LINE__);
114 }
115
116 Range::Range()
117 {
118 mHigh = 0;
119 mLow = 0;
120 }
121
122 Range::Range(const unsigned int &high, const unsigned int &low)
123 {
124 mHigh = high;
125 mLow = low;
126 }
127
128 Range::~Range()
129 {
130 }
131
132 void Range::High(const unsigned int &value)
133 {
134 if (mHigh > mLow)
135 mHigh = value;
136 }
137
138 unsigned int Range::High(void) const
139 {
140 return mHigh;
141 }
142
143 void Range::Low(const unsigned int &value)
144 {
145 if (mLow < mHigh)
146 mLow = value;
147 }
148
149 unsigned int Range::Low(void) const
150 {
151 return mLow;
152 }
153
154 unsigned int Range::Random()
155 {
156 static base_generator_type generator(static_cast<unsigned int>(std::time(0)));
157 boost::uniform_int<> uni_dist(mHigh, mLow);
158 boost::variate_generator<base_generator_type&, boost::uniform_int<> > uni(generator, uni_dist);
159 return static_cast<unsigned int>(uni());
160 }