Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

/home/groups/n/no/noxwizard/cvs/src/inlines.h

Go to the documentation of this file.
00001   /*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
00002     || NoX-Wizard UO Server Emulator (NXW) [http://noxwizard.sourceforge.net]  ||
00003     ||                                                                         ||
00004     || This software is free software released under GPL2 license.             ||
00005     || You can find detailed license information in nox-wizard.cpp file.       ||
00006     ||                                                                         ||
00007     || For any question post to NoX-Wizard forums.                             ||
00008     -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
00009 
00015 #ifndef __INLINES_H__
00016 #define __INLINES_H__
00017 
00018 #include "items.h"
00019 #include "chars.h"
00020 #include "basics.h"
00021 
00022 
00023 #define TIMEOUT(X) (((X) <= uiCurrentTime) || overflow)
00024 // Macros & Templates by Xan :
00025 #if __BORLANDC__ > 0x550
00026 #define qmin(x,y) ((x)<(y)?(x):(y))
00027 #define qmax(x,y) ((x)>(y)?(x):(y))
00028 #define min(x,y) ((x)<(y)?(x):(y))
00029 #define max(x,y) ((x)>(y)?(x):(y))
00030 inline float ceilf(float _X)
00031         {return ((float)ceil((double)_X)); }
00032 
00033 #else
00034 #define qmax(A,B) (((A)>(B)) ? (A) : (B))
00035 #define qmin(A,B) (((A)<(B)) ? (A) : (B))
00036 #endif
00037 
00038 // this delete is safe from multiple deletes of same ptr
00039 template<typename T> inline void safedelete(T*& p) { delete p; p = NULL; }
00040 template<typename T> inline void safedeletearray(T*& p) { delete[] p; p = NULL; }
00041 template<typename T> inline void qswap(T& a, T& b) { T dummy; dummy = a; a = b; b = dummy; }
00042 
00043 #define charsysmsg(PC) if (PC->getClient()!=NULL) PC->getClient()->sysmsg
00044 
00045 #define DBYTE2WORD(A,B) (((A)<<8) + ((B)&0xFF))
00046 #define WORD2DBYTE1(A)  ((char)((A)>>8))
00047 #define WORD2DBYTE2(A)  ((char)((A)&0xFF))
00048 #define WORD2DBYTE(A,B,C) { B = WORD2DBYTE1(A); C = WORD2DBYTE2(A); }
00049 
00050 
00051 inline bool chance(int percent) { return ( (rand()%100) < percent); }
00052 
00053 inline int calcserial(unsigned char a1,unsigned char a2,unsigned char a3,unsigned char a4) {return (static_cast<int>((a1<<24))|static_cast<int>((a2<<16)) | static_cast<int>((a3<<8)) | static_cast<int>(a4));}
00054 
00055 inline int calcCharFromPtr(unsigned char *p)
00056 {
00057         int serial;
00058         if((serial=LongFromCharPtr(p)) < 0) return INVALID;
00059         if (ISVALIDPC(pointers::findCharBySerial(serial))) return (DEREF_P_CHAR(pointers::findCharBySerial(serial)));
00060         else return (INVALID);
00061 }
00062 
00063 inline int calcItemFromPtr(unsigned char *p)
00064 {
00065         int serial;
00066         if((serial=LongFromCharPtr(p)) < 0) return INVALID;
00067         if (ISVALIDPI(pointers::findItemBySerial(serial))) return (DEREF_P_ITEM(pointers::findItemBySerial(serial)));
00068         else return (INVALID);
00069 }
00070 
00071 /*inline int calcItemFromSer(unsigned char s1, unsigned char s2, unsigned char s3, unsigned char s4)
00072 {
00073         if((s1==255) && (s2==255) && (s3==255) && (s4==255) ) return -1;//AntiChrist
00074         int serial = calcserial(s1, s2, s3, s4);
00075         if (ISVALIDPI(pointers::findItemBySerial(serial))) return (DEREF_P_ITEM(pointers::findItemBySerial(serial)));
00076         else return (-1);
00077 }*/
00078 
00079 inline int calcItemFromSer(int ser) // Aded by Magius(CHE) (2)
00080 {
00081         if (ISVALIDPI(pointers::findItemBySerial(ser))) return (DEREF_P_ITEM(pointers::findItemBySerial(ser)));
00082         else return (INVALID);
00083 }
00084 
00085 //Modified by Luxor for the erasing of charcount
00086 /*inline NXWSOCKET  calcSocketFromChar(CHARACTER i)
00087 {
00088         P_CHAR pc = MAKE_CHAR_REF(i);
00089         if (!ISVALIDPC(pc)) return INVALID;
00090 
00091         if (pc->npc) return INVALID;
00092 
00093                 NXWCLIENT cli = pc->getClient();
00094         if (cli == NULL) return INVALID;
00095 
00096         return cli->toInt();
00097 
00098 }*/
00099 
00100 /*inline int calcCharFromSer(unsigned char s1, unsigned char s2, unsigned char s3, unsigned char s4)
00101 {
00102         int serial;
00103 
00104         serial = calcserial(s1, s2, s3, s4);
00105         if (ISVALIDPC(pointers::findCharBySerial(serial))) return(DEREF_P_CHAR(pointers::findCharBySerial(serial)));
00106         else return(-1);
00107 }*/
00108 
00109 inline int calcCharFromSer(int serial)
00110 {
00111         if (ISVALIDPC(pointers::findCharBySerial(serial))) return (DEREF_P_CHAR(pointers::findCharBySerial(serial)));
00112         else return (INVALID);
00113 }
00114 
00115 inline void SetTimerSec( TIMERVAL *timer, const short seconds)
00116 {
00117         *timer=seconds * MY_CLOCKS_PER_SEC + uiCurrentTime;
00118 }
00119 
00120 inline bool isCharSerial( long ser ) { return ( ser > 0 && ser <  0x40000000 ); } //ndEndy 0 is not a char serial, see curr_charSerial note
00121 inline bool isItemSerial( long ser ) { return ( /*ser >= 0 && */ser >= 0x40000000 ); } 
00122 
00123 
00124 
00125 #define SETSOCK(A) g_nCurrentSocket = A;
00126 
00127 inline std::string toString(int value)
00128 {
00129         char s[21];
00130         snprintf(s, 20, "%d", value);
00131         s[19]=0x00;
00132 
00133         return std::string(s);
00134 }
00135 
00136 inline std::string toString(double value)
00137 {
00138         char s[21];
00139         snprintf(s, 20, "%f", value);
00140         s[19]=0x00;
00141 
00142         return std::string(s);
00143 }
00144 
00145 /*
00146 \brief Convert a string to an wstring
00147 \author Endymion
00148 \param from the source string
00149 \param to the dest wstring
00150 */
00151 inline void string2wstring( string from, wstring& to )
00152 {
00153         to.erase();
00154         string::iterator iter( from.begin() ), end( from.end() );
00155         for( ; iter!=end; iter++ ) {
00156                 to+=static_cast<wchar_t>(*iter);
00157         }
00158 }
00159 
00160 /*
00161 \brief Convert a wstring to an string
00162 \author Endymion
00163 \param from the source wstring
00164 \param to the dest string
00165 */
00166 inline void wstring2string( wstring from, string& to )
00167 {
00168         to.erase();
00169         wstring::iterator iter( from.begin() ), end( from.end() );
00170         for( ; iter!=end; iter++ ) {
00171                 to+=static_cast<char>(*iter);
00172         }
00173 }
00174 
00175 #endif //__INLINES_H__
SourceForge.net Logo