]> jfr.im git - irc/gameservirc.git/blobdiff - gameserv-2.0/libgameservcore/include/GameServ/Types.h
I implemented a mock forest driver and found a problem in using the singleton design...
[irc/gameservirc.git] / gameserv-2.0 / libgameservcore / include / GameServ / Types.h
index ef6229efef4de12d30c665e1cd9a4a4079e5b596..de0fab22f7bec9ef24843919bc967db1ce77a6cd 100644 (file)
@@ -6,16 +6,27 @@ using std::string;
 #include <GameServ/GameServException.h>\r
 using GameServ::Exceptions::GameServException;\r
 \r
-\r
+#include <ctime>\r
 #include <boost/random/linear_congruential.hpp>\r
 #include <boost/random/uniform_int.hpp>\r
 #include <boost/random/uniform_real.hpp>\r
 #include <boost/random/variate_generator.hpp>\r
+// Sun CC doesn't handle boost::iterator_adaptor yet\r
+#if !defined(__SUNPRO_CC) || (__SUNPRO_CC > 0x530)\r
+#include <boost/generator_iterator.hpp>\r
+#endif\r
+\r
+#ifdef BOOST_NO_STDC_NAMESPACE\r
+namespace std {\r
+  using ::time;\r
+}\r
+#endif\r
 \r
 // This is a typedef for a random number generator.\r
 // Try boost::mt19937 or boost::ecuyer1988 instead of boost::minstd_rand\r
 typedef boost::minstd_rand base_generator_type;\r
 \r
+\r
 namespace GameServ \r
 {              \r
        namespace Types\r
@@ -61,6 +72,33 @@ namespace GameServ
 \r
                }; // ItemTypes class\r
 \r
+               class ObjectTypes\r
+               {\r
+               public:\r
+                       //! Logical ObjectType enumerator\r
+                       enum ObjectType \r
+                       { \r
+                               Player, Item, Level, Master, Monster\r
+                       };\r
+\r
+                       //! Get the name of an object type\r
+                       static string GetName(ObjectType objecttype);\r
+\r
+                       //! Parse the name and return the enumeration value\r
+                       static ObjectType Parse(const string &objecttype);\r
+\r
+               private:\r
+                       //! Internal struct to hold type information statically in a table\r
+                       typedef struct objecttypeinfo\r
+                       {\r
+                               ObjectTypes::ObjectType objecttype;     //!< The ObjectType enumeration value\r
+                               string name;                            //!< The Name of the type\r
+                       } objecttypeinfo;\r
+\r
+                       static const objecttypeinfo mObjectTypeInfoTable[];\r
+\r
+               }; // ObjectTypes class\r
+\r
                class Modifiers\r
                {\r
                public:\r
@@ -84,31 +122,82 @@ namespace GameServ
                        static const modifierinfo mModifierInfoTable[];\r
                }; // Modifiers class\r
 \r
+               template <class T>\r
                class Range\r
                {\r
                public:\r
-                       Range(const unsigned int &high, const unsigned int &low);\r
-                       ~Range();\r
+\r
+                       Range()\r
+                       {\r
+                       }\r
+\r
+                       Range(const T &high, const T &low)\r
+                       {\r
+                               mHigh = high;\r
+                               mLow = low;\r
+                       }\r
+\r
+                       ~Range()\r
+                       {\r
+                       }\r
 \r
                        //! Generate a random number within the range\r
-                       unsigned int Random();\r
+                       T Random()\r
+                       {\r
+                               static base_generator_type generator(static_cast<T>(std::time(0)));\r
+                               if (mLow == mHigh)\r
+                               {\r
+                                       return mLow;\r
+                               }\r
+                               else\r
+                               {\r
+                                       boost::uniform_int<> uni_dist(mLow, mHigh);\r
+                                       boost::variate_generator<base_generator_type&, boost::uniform_int<> > uni(generator, uni_dist);\r
+                                       mLastRandom = uni();\r
+                                       return mLastRandom;\r
+                               }\r
+                       }\r
 \r
                        //! Property get - High\r
-                       unsigned int High(void) const;\r
+                       T High(void) const\r
+                       {\r
+                               return mHigh;\r
+                       }\r
                        \r
                        //! Property set - High\r
-                       void High(const unsigned int &value);\r
+                       void High(const T &value)\r
+                       {\r
+                               if (value >= mLow)\r
+                               {\r
+                                       mHigh = value;\r
+                               }\r
+                       }\r
 \r
                        //! Property get - Low\r
-                       unsigned int Low(void) const;\r
+                       T Low(void) const\r
+                       {\r
+                               return mLow;\r
+                       }\r
 \r
                        //! Property set - Low\r
-                       void Low(const unsigned int &value);\r
+                       void Low(const T &value)\r
+                       {\r
+                               if (value <= mHigh)\r
+                               {\r
+                                       mLow = value;\r
+                               }\r
+                       }\r
+\r
+                       //! Property get - Last random number to be generated\r
+                       T LastRandom(void) const\r
+                       {\r
+                               return mLastRandom;\r
+                       }\r
                        \r
                private:\r
-                       unsigned int mHigh;\r
-                       unsigned int mLow;\r
-                       base_generator_type generator;\r
+                       T mHigh;\r
+                       T mLow;\r
+                       T mLastRandom;\r
                };\r
        } \r
 }\r