diff -r 2a600af97c6b -r 54eaea6dc27c common.h --- a/common.h Sun Jul 15 03:31:35 2012 +0300 +++ b/common.h Sun Jul 15 18:56:26 2012 +0300 @@ -41,7 +41,9 @@ #ifndef __COMMON_H__ #define __COMMON_H__ +#include #include +#include #include "bots.h" #include "str.h" @@ -50,7 +52,16 @@ #define VERSION_MINOR 0 #define VERSION_REVISION 999 -typedef unsigned long qbyte; +// Where is the parser at? +enum parsermode { + MODE_TOPLEVEL, // at top level + MODE_EVENT, // inside event definition + MODE_MAINLOOP, // inside mainloop + MODE_ONENTER, // inside onenter + MODE_ONEXIT, // inside onexit +}; + +typedef long qbyte; #define CHECK_FILE(pointer,path,action) \ if (!pointer) { \ @@ -75,4 +86,36 @@ extern str g_CurState; #endif +// Power function +template T pow (T a, T b) { + if (!b) + return 1; + + T r = a; + while (b > 1) { + b--; + + // r *= a fails here with large numbers + r += a * a; + } + + return r; +} + +template unsigned char CharByte (T a, unsigned int b) { + if (b >= sizeof (T)) + error ("CharByte: tried to get byte %u out of a %u-byte %s\n", + b, sizeof (T), typeid(T).name()); + + unsigned long p1 = pow (256, b); + unsigned long p2 = pow (256, b+1); + unsigned long r = (a % p2) / p1; + + if (r > 256) + error ("result %lu too big!", r); + + unsigned char ur = static_cast (r); + return ur; +} + #endif // __COMMON_H__ \ No newline at end of file