Fri, 10 Jan 2014 21:58:42 +0200
- major refactoring begins
72
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
1 | #ifndef BOTC_COMMON_H |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
2 | #define BOTC_COMMON_H |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
3 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
4 | #if !defined (__cplusplus) || __cplusplus < 201103L |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
5 | # error botc requires a C++11-compliant compiler to be built |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
6 | #endif |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
7 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
8 | #include <cstdio> |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
9 | #include <cstdarg> |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
10 | #include <cstdint> |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
11 | #include "types.h" |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
12 | #include "bots.h" |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
13 | #include "str.h" |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
14 | #include "containers.h" |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
15 | #include "format.h" |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
16 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
17 | // Application name and version |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
18 | #define APPNAME "botc" |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
19 | #define VERSION_MAJOR 0 |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
20 | #define VERSION_MINOR 999 |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
21 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
22 | // On Windows, files are case-insensitive |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
23 | #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__) |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
24 | #define FILE_CASEINSENSITIVE |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
25 | #endif |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
26 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
27 | // Parser mode: where is the parser at? |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
28 | enum parsermode_e { |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
29 | MODE_TOPLEVEL, // at top level |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
30 | MODE_EVENT, // inside event definition |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
31 | MODE_MAINLOOP, // inside mainloop |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
32 | MODE_ONENTER, // inside onenter |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
33 | MODE_ONEXIT, // inside onexit |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
34 | }; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
35 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
36 | enum type_e { |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
37 | TYPE_UNKNOWN = 0, |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
38 | TYPE_VOID, |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
39 | TYPE_INT, |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
40 | TYPE_STRING, |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
41 | TYPE_BOOL, |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
42 | }; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
43 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
44 | #define elif else if |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
45 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
46 | #define CHECK_FILE(pointer,path,action) \ |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
47 | if (!pointer) { \ |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
48 | error ("couldn't open %s for %s!\n", path.chars(), action); \ |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
49 | exit (1); \ |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
50 | } |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
51 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
52 | // Plural expression |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
53 | #define PLURAL(n) (n != 1) ? "s" : "" |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
54 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
55 | // Shortcut for zeroing something |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
56 | #define ZERO(obj) memset (&obj, 0, sizeof (obj)); |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
57 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
58 | void error (const char* text, ...); |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
59 | string ObjectFileName (string s); |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
60 | bool fexists (string path); |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
61 | type_e GetTypeByName (string token); |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
62 | string GetTypeName (type_e type); |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
63 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
64 | // Make the parser's variables globally available |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
65 | extern int g_NumStates; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
66 | extern int g_NumEvents; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
67 | extern parsermode_e g_CurMode; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
68 | extern string g_CurState; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
69 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
70 | #define neurosphere if (g_Neurosphere) |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
71 | #define twice for (int repeat_token = 0; repeat_token < 2; repeat_token++) |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
72 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
73 | #ifndef __GNUC__ |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
74 | #define __attribute__(X) |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
75 | #endif |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
76 | #define deprecated __attribute__ ((deprecated)) |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
77 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
78 | // Power function |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
79 | template<class T> T pow (T a, unsigned int b) { |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
80 | if (!b) |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
81 | return 1; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
82 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
83 | T r = a; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
84 | while (b > 1) { |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
85 | b--; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
86 | r = r * a; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
87 | } |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
88 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
89 | return r; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
90 | } |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
91 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
92 | // Byte datatype |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
93 | typedef int32_t word; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
94 | typedef unsigned char byte; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
95 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
96 | // Keywords |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
97 | extern const char** g_Keywords; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
98 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
99 | bool IsKeyword (string s); |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
100 | unsigned int NumKeywords (); |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
101 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
102 | // Script mark and reference |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
103 | struct ScriptMark { |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
104 | string name; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
105 | size_t pos; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
106 | }; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
107 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
108 | struct ScriptMarkReference { |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
109 | unsigned int num; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
110 | size_t pos; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
111 | }; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
112 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
113 | // ==================================================================== |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
114 | // Generic union |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
115 | template <class T> union union_t { |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
116 | T val; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
117 | byte b[sizeof (T)]; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
118 | char c[sizeof (T)]; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
119 | double d; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
120 | float f; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
121 | int i; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
122 | word w; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
123 | }; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
124 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
125 | // ==================================================================== |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
126 | // Finds a byte in the given value. |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
127 | template <class T> inline unsigned char GetByteIndex (T a, unsigned int b) { |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
128 | union_t<T> uni; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
129 | uni.val = a; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
130 | return uni.b[b]; |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
131 | } |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
132 | |
03e4d9db3fd9
- major refactoring begins
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
133 | #endif // BOTC_COMMON_H |