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
;
14 #include <boost/format.hpp>
15 #include <boost/algorithm/string.hpp>
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>
29 #ifdef BOOST_NO_STDC_NAMESPACE
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
;
39 TypeException::TypeException(const string
&ErrorMsg
, const char *pFilename
, int SourceLine
)
40 : GameServException(ErrorMsg
, pFilename
, SourceLine
)
43 TypeException::TypeException(const string
&ErrorMsg
)
44 : GameServException(ErrorMsg
)
47 TypeException::TypeException(void)
48 : GameServException("Type Exception occurred either do to conversion or improper type usage.", __FILE__
, __LINE__
)
51 const ItemTypes::itemtypeinfo
ItemTypes::mItemTypeInfoTable
[] =
53 { Weapon
, "Weapon" }, { Armor
, "Armor" }, { Potion
, "Potion"}, { NOTYPE
, "NOTYPE" }
54 }; // mItemTypeInfoTable[]
56 string
ItemTypes::GetName(ItemTypes::ItemType itemtype
)
58 for (unsigned int i
= 0; i
< sizeof(mItemTypeInfoTable
) / sizeof(itemtypeinfo
); i
++)
60 if (mItemTypeInfoTable
[i
].itemtype
== itemtype
)
62 return mItemTypeInfoTable
[i
].name
;
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());
71 ItemTypes::ItemType
ItemTypes::Parse(const string
&itemtype
)
73 for (unsigned int i
= 0; i
< sizeof(mItemTypeInfoTable
) / sizeof(itemtypeinfo
); i
++)
75 if (mItemTypeInfoTable
[i
].name
== itemtype
)
77 return mItemTypeInfoTable
[i
].itemtype
;
80 throw TypeException(str(format("No itemtype enumeration found for %1%") % itemtype
), __FILE__
, __LINE__
);
83 const ObjectTypes::objecttypeinfo
ObjectTypes::mObjectTypeInfoTable
[] =
85 { Player
, "Player" }, { Level
, "Level" }, { Item
, "Item" },
86 { Master
, "Master" }, { Monster
, "Monster" }
87 }; // mObjectTypeInfoTable[]
89 string
ObjectTypes::GetName(ObjectTypes::ObjectType objecttype
)
91 for (unsigned int i
= 0; i
< sizeof(mObjectTypeInfoTable
) / sizeof(objecttypeinfo
); i
++)
93 if (mObjectTypeInfoTable
[i
].objecttype
== objecttype
)
95 return mObjectTypeInfoTable
[i
].name
;
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());
104 ObjectTypes::ObjectType
ObjectTypes::Parse(const string
&objecttype
)
106 for (unsigned int i
= 0; i
< sizeof(mObjectTypeInfoTable
) / sizeof(objecttypeinfo
); i
++)
108 if (mObjectTypeInfoTable
[i
].name
== objecttype
)
110 return mObjectTypeInfoTable
[i
].objecttype
;
113 throw TypeException(str(format("No object type enumeration found for %1%") % objecttype
), __FILE__
, __LINE__
);
117 const Modifiers::modifierinfo
Modifiers::mModifierInfoTable
[] =
119 { strength
, "Strength"}, { defense
, "Defense" }, { maxhealth
, "MaxHealth" }, { health
, "Health" }, { forestfights
, "ForestFights" },
120 { playerfights
, "PlayerFights" }, { gold
, "Gold" }, { bank
, "Bank" }
123 string
Modifiers::GetName(Modifiers::Modifier modifier
)
125 for (unsigned int i
= 0; i
< sizeof(mModifierInfoTable
) / sizeof(modifierinfo
); i
++)
127 if (mModifierInfoTable
[i
].modifier
== modifier
)
129 return mModifierInfoTable
[i
].name
;
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());
138 Modifiers::Modifier
Modifiers::Parse(const string
&modifier
)
140 for (unsigned int i
= 0; i
< sizeof(mModifierInfoTable
) / sizeof(modifierinfo
); i
++)
142 if (mModifierInfoTable
[i
].name
== modifier
)
144 return mModifierInfoTable
[i
].modifier
;
148 throw TypeException(str(format("No modifier enumeration found for %1%") % modifier
), __FILE__
, __LINE__
);
158 Range::Range(const int &high
, const int &low
)
172 void Range::High(const int &value
)
178 int Range::High(void) const
183 void Range::Low(const int &value
)
189 int Range::Low(void) const
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
);
203 int Range::LastRandom(void) const