00001
00002
00003
00004
00005
00006
00007
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
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
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
00072
00073
00074
00075
00076
00077
00078
00079 inline int calcItemFromSer(int ser)
00080 {
00081 if (ISVALIDPI(pointers::findItemBySerial(ser))) return (DEREF_P_ITEM(pointers::findItemBySerial(ser)));
00082 else return (INVALID);
00083 }
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
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 ); }
00121 inline bool isItemSerial( long ser ) { return ( 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
00147
00148
00149
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
00162
00163
00164
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__