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
;
13 #include <boost/format.hpp>
14 #include <boost/algorithm/string.hpp>
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>
28 #ifdef BOOST_NO_STDC_NAMESPACE
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
;
38 TypeException::TypeException(const string
&ErrorMsg
, const char *pFilename
, int SourceLine
)
39 : GameServException(ErrorMsg
, pFilename
, SourceLine
)
42 TypeException::TypeException(const string
&ErrorMsg
)
43 : GameServException(ErrorMsg
)
46 TypeException::TypeException(void)
47 : GameServException("Type Exception occurred either do to conversion or improper type usage.", __FILE__
, __LINE__
)
50 const ItemTypes::itemtypeinfo
ItemTypes::mItemTypeInfoTable
[] =
52 { Weapon
, "Weapon" }, { Armor
, "Armor" }, { Potion
, "Potion"}
53 }; // mItemTypeInfoTable[]
55 string
ItemTypes::GetName(ItemTypes::ItemType itemtype
)
57 for (unsigned int i
= 0; i
< sizeof(mItemTypeInfoTable
) / sizeof(itemtypeinfo
); i
++)
59 if (mItemTypeInfoTable
[i
].itemtype
== itemtype
)
61 return mItemTypeInfoTable
[i
].name
;
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());
70 ItemTypes::ItemType
ItemTypes::Parse(const string
&itemtype
)
72 for (unsigned int i
= 0; i
< sizeof(mItemTypeInfoTable
) / sizeof(itemtypeinfo
); i
++)
74 if (mItemTypeInfoTable
[i
].name
== itemtype
)
76 return mItemTypeInfoTable
[i
].itemtype
;
79 return ItemTypes::NOTYPE
;
82 const Modifiers::modifierinfo
Modifiers::mModifierInfoTable
[] =
84 { strength
, "Strength"}, { defense
, "Defense" }, { maxhealth
, "MaxHealth" }, { health
, "Health" }, { forestfights
, "Forest Fights" },
85 { playerfights
, "Player Fights" }, { gold
, "Gold" }, { bank
, "Bank" }
88 string
Modifiers::GetName(Modifiers::Modifier modifier
)
90 for (unsigned int i
= 0; i
< sizeof(mModifierInfoTable
) / sizeof(modifierinfo
); i
++)
92 if (mModifierInfoTable
[i
].modifier
== modifier
)
94 return mModifierInfoTable
[i
].name
;
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());
103 Modifiers::Modifier
Modifiers::Parse(const string
&modifier
)
105 for (unsigned int i
= 0; i
< sizeof(mModifierInfoTable
) / sizeof(modifierinfo
); i
++)
107 if (mModifierInfoTable
[i
].name
== modifier
)
109 return mModifierInfoTable
[i
].modifier
;
113 throw TypeException(str(format("No modifier enumeration found for %1%") % modifier
), __FILE__
, __LINE__
);
122 Range::Range(const unsigned int &high
, const unsigned int &low
)
132 void Range::High(const unsigned int &value
)
138 unsigned int Range::High(void) const
143 void Range::Low(const unsigned int &value
)
149 unsigned int Range::Low(void) const
154 unsigned int Range::Random()
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());