common.h

changeset 26
54eaea6dc27c
parent 22
b48e10ca8832
child 27
15c06a191f9b
equal deleted inserted replaced
25:2a600af97c6b 26:54eaea6dc27c
39 */ 39 */
40 40
41 #ifndef __COMMON_H__ 41 #ifndef __COMMON_H__
42 #define __COMMON_H__ 42 #define __COMMON_H__
43 43
44 #include <stdio.h>
44 #include <stdarg.h> 45 #include <stdarg.h>
46 #include <typeinfo>
45 #include "bots.h" 47 #include "bots.h"
46 #include "str.h" 48 #include "str.h"
47 49
48 #define APPNAME "botc" 50 #define APPNAME "botc"
49 #define VERSION_MAJOR 0 51 #define VERSION_MAJOR 0
50 #define VERSION_MINOR 0 52 #define VERSION_MINOR 0
51 #define VERSION_REVISION 999 53 #define VERSION_REVISION 999
52 54
53 typedef unsigned long qbyte; 55 // Where is the parser at?
56 enum parsermode {
57 MODE_TOPLEVEL, // at top level
58 MODE_EVENT, // inside event definition
59 MODE_MAINLOOP, // inside mainloop
60 MODE_ONENTER, // inside onenter
61 MODE_ONEXIT, // inside onexit
62 };
63
64 typedef long qbyte;
54 65
55 #define CHECK_FILE(pointer,path,action) \ 66 #define CHECK_FILE(pointer,path,action) \
56 if (!pointer) { \ 67 if (!pointer) { \
57 error ("couldn't open %s for %s!\n", (char*)path, action); \ 68 error ("couldn't open %s for %s!\n", (char*)path, action); \
58 exit (1); \ 69 exit (1); \
73 extern int g_NumEvents; 84 extern int g_NumEvents;
74 extern int g_CurMode; 85 extern int g_CurMode;
75 extern str g_CurState; 86 extern str g_CurState;
76 #endif 87 #endif
77 88
89 // Power function
90 template<class T> T pow (T a, T b) {
91 if (!b)
92 return 1;
93
94 T r = a;
95 while (b > 1) {
96 b--;
97
98 // r *= a fails here with large numbers
99 r += a * a;
100 }
101
102 return r;
103 }
104
105 template <class T> unsigned char CharByte (T a, unsigned int b) {
106 if (b >= sizeof (T))
107 error ("CharByte: tried to get byte %u out of a %u-byte %s\n",
108 b, sizeof (T), typeid(T).name());
109
110 unsigned long p1 = pow<unsigned long> (256, b);
111 unsigned long p2 = pow<unsigned long> (256, b+1);
112 unsigned long r = (a % p2) / p1;
113
114 if (r > 256)
115 error ("result %lu too big!", r);
116
117 unsigned char ur = static_cast<unsigned char> (r);
118 return ur;
119 }
120
78 #endif // __COMMON_H__ 121 #endif // __COMMON_H__

mercurial