1 //-------------------------------------------------------------------------
2 /*
3 Copyright (C) 1996, 2003 - 3D Realms Entertainment
4 Copyright (C) 2000, 2003 - Matt Saettler (EDuke Enhancements)
5 Copyright (C) 2004, 2007 - EDuke32 developers
6 
7 This file is part of EDuke32
8 
9 EDuke32 is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License version 2
11 as published by the Free Software Foundation.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 See the GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22 */
23 //-------------------------------------------------------------------------
24 
25 #ifndef _m32script_h_
26 #define _m32script_h_
27 
28 #include "compat.h"
29 #include "baselayer.h"
30 #include "build.h"
31 #include "editor.h"
32 
33 #define MAXQUOTES 16384
34 #define MAXQUOTELEN 128
35 
36 typedef int32_t instype;
37 typedef int32_t ofstype;
38 
39 extern char *apStrings[MAXQUOTES+1], *apXStrings[MAXQUOTES+1];
40 extern int32_t g_numQuoteRedefinitions;
41 
42 extern int32_t VM_Execute(int32_t once);
43 extern void VM_OnEvent(int32_t iEventID, int32_t spriteNum);
44 
45 extern void VM_ScriptInfo(void);
46 extern void VM_Disasm(ofstype beg, int32_t size);
47 
48 void Gv_NewVar(const char *pszLabel, intptr_t lValue, uint32_t dwFlags);
49 void Gv_NewArray(const char *pszLabel, void *arrayptr, intptr_t asize, uint32_t dwFlags);
50 extern void Gv_Init(void);
51 
52 extern int32_t __fastcall Gv_GetVar(int32_t id);
53 extern void __fastcall Gv_SetVar(int32_t id, int32_t lValue);
54 extern int32_t __fastcall Gv_GetVarN(int32_t id);  // 'N' for "no side-effects"... vars and locals only!
55 
56 extern void SetGamePalette(int32_t);
57 
58 extern int32_t *constants, constants_allocsize;
59 extern int32_t g_numSavedConstants;
60 
61 extern instype *apScript ,*insptr;
62 extern int32_t *labelval;
63 extern uint8_t *labeltype;
64 extern int32_t g_numLabels, g_numDefaultLabels;
65 extern int32_t g_scriptSize;
66 extern char *label;
67 //extern int32_t label_allocsize;
68 
69 extern hashtable_t h_labels;
70 
71 #define MAXLABELLEN 32
72 
73 //extern uint8_t waterpal[768],slimepal[768],titlepal[768],drealms[768],endingpal[768],animpal[768];
74 //extern char currentboardfilename[BMAVM_PATH];
75 
76 
77 enum GameEvent_t {
78     EVENT_ENTER3DMODE,
79     EVENT_ANALYZESPRITES,
80     EVENT_INSERTSPRITE2D,
81     EVENT_INSERTSPRITE3D,
82     EVENT_DRAW2DSCREEN,
83     EVENT_DRAW3DSCREEN,
84     EVENT_KEYS2D,
85     EVENT_KEYS3D,
86     EVENT_PREKEYS2D,
87     EVENT_PREKEYS3D,
88     EVENT_LINKTAGS,
89     EVENT_KEYPRESS,
90     EVENT_PREDRAW3DSCREEN,
91     EVENT_LOADMAP,
92     EVENT_SAVEMAP,
93     EVENT_PRELOADMAP,
94     EVENT_PRESAVEMAP,
95     EVENT_PREDRAW2DSCREEN,
96     EVENT_GETNUMBER,
97     MAXEVENTS
98 };
99 
100 extern ofstype aEventOffsets[MAXEVENTS];
101 extern int32_t aEventSizes[MAXEVENTS];
102 extern uint8_t aEventEnabled[MAXEVENTS];
103 extern uint16_t aEventNumLocals[MAXEVENTS];
104 
105 
106 enum GamevarFlags_t {
107     MAXGAMEVARS        = 1024,       // must be a power of two between 256 and 4096, inclusive
108     LOG2MAXGV          = 10,
109     MAXVARLABEL        = MAXLABELLEN, //26,
110 
111     GAMEVAR_PERBLOCK   = 0x00000001, // per-block (state, event, or top-level) variable
112     GAMEVAR_USER_MASK = GAMEVAR_PERBLOCK,
113 
114     GAMEVAR_RESET      = 0x00000008, // marks var for to default
115 
116     GAMEVAR_SYSTEM     = 0x00000800, // cannot change mode flags...(only default value)
117     GAMEVAR_READONLY   = 0x00001000, // values are read-only (no setvar allowed)
118 
119     GAMEVAR_INTPTR     = 0x00002000, // plValues is a pointer to an int32_t
120     GAMEVAR_FLOATPTR   = 0x00004000, // plValues is a pointer to a float
121     GAMEVAR_SHORTPTR   = 0x00008000, // plValues is a pointer to a short
122     GAMEVAR_CHARPTR    = 0x00010000, // plValues is a pointer to a char
123     GAMEVAR_PTR_MASK = GAMEVAR_INTPTR|GAMEVAR_FLOATPTR|GAMEVAR_SHORTPTR|GAMEVAR_CHARPTR,
124 
125     GAMEVAR_SPECIAL    = 0x00040000, // flag for structure member shortcut vars
126 };
127 
128 enum GamearrayFlags_t
129 {
130     MAXGAMEARRAYS = (MAXGAMEVARS >> 2),  // must be strictly smaller than MAXGAMEVARS
131     MAXARRAYLABEL = MAXVARLABEL,
132 
133     GAMEARRAY_READONLY = 0x00001000,
134     GAMEARRAY_WARN     = 0x00002000,
135 
136     GAMEARRAY_NORMAL    = 0x00004000,
137     GAMEARRAY_UINT8     = 0x00000001,
138     GAMEARRAY_INT16     = 0x00000002,
139     GAMEARRAY_INT32     = 0x00000004,
140     GAMEARRAY_TYPE_MASK = GAMEARRAY_UINT8 | GAMEARRAY_INT16 | GAMEARRAY_INT32,
141 
142     GAMEARRAY_RESET = 0x00000008,
143 
144     GAMEARRAY_VARSIZE = 0x00000020,
145     GAMEARRAY_STRIDE2 = 0x00000040,
146 };
147 
148 typedef struct {
149     union {
150         intptr_t lValue;   // pointer when (dwFlags & GAMEVAR_*PTR)
151         int32_t *plValues;     // array of values when (dwFlags & GAMEVAR_PERBLOCK)
152     } val;
153     intptr_t lDefault;
154     char *szLabel;
155     uint32_t dwFlags;
156 } gamevar_t;
157 
158 typedef struct {
159     char *szLabel;
160     void *vals;     // array of values, type determined by (dwFlags & GAMEARRAY_TYPEMASK)
161     uint32_t dwFlags;
162     int32_t size;  // id to size gamevar when (dwFlags & GAMEARRAY_VARSIZE)
163 } gamearray_t;
164 
165 extern gamevar_t aGameVars[MAXGAMEVARS];
166 extern gamearray_t aGameArrays[MAXGAMEARRAYS];
167 extern int32_t g_gameVarCount, g_systemVarCount;
168 extern int32_t g_gameArrayCount, g_systemArrayCount;
169 
170 extern uint32_t m32_drawlinepat;
171 
172 
173 extern int32_t g_iReturnVar;
174 extern int32_t g_doScreenShot;
175 extern int32_t m32_sortvar1, m32_sortvar2;
176 
177 //extern int32_t g_scriptDebug;
178 
179 
180 
181 extern hashtable_t h_gamevars;
182 extern hashtable_t h_arrays;
183 //extern hashtable_t h_keywords;
184 extern hashtable_t h_gamefuncs;
185 
186 
187 extern int32_t mousxplc;
188 extern int32_t mousyplc;
189 
190 
191 // gamevar bytecode format:
192 
193 //  FEDC|BA98|7654|3210|FEDC|BA98|7654|3120
194 //                     |       .. .... ....  gamevar ID
195 //                     |      .              constant bit (checked first) / get-payload-var bit for array or struct
196 //                     |     .               negate bit
197 //                     |   .                 array bit  \___\  if both set:
198 //                     |  .                  struct bit /   /  local var
199 //  .... .... .... ....|                     optional payload
200 
201 
202 
203 #define M32_FLAG_CONSTANT (MAXGAMEVARS)
204 #define M32_FLAG_NEGATE (MAXGAMEVARS<<1)
205 
206 #define M32_FLAG_VAR (0)
207 #define M32_FLAG_ARRAY (MAXGAMEVARS<<2)
208 #define M32_FLAG_STRUCT (MAXGAMEVARS<<3)
209 #define M32_FLAG_LOCAL (M32_FLAG_ARRAY|M32_FLAG_STRUCT)
210 #define M32_VARTYPE_MASK (M32_FLAG_ARRAY|M32_FLAG_STRUCT)
211 
212 #define M32_FLAG_CONSTANTINDEX M32_FLAG_CONSTANT
213 // if set, fetch index for array or struct array from 16 high bits as a constant (otherwise: gamevar)
214 
215 #define M32_BITS_MASK (0x0000ffff-(MAXGAMEVARS-1))
216 
217 // IDs of special vars
218 #define M32_SPRITE_VAR_ID 0
219 #define M32_SECTOR_VAR_ID 1
220 #define M32_WALL_VAR_ID 2
221 #define M32_TSPRITE_VAR_ID 3
222 #define M32_LIGHT_VAR_ID 4
223 
224 #define M32_THISACTOR_VAR_ID 5
225 #define M32_RETURN_VAR_ID 6
226 #define M32_LOTAG_VAR_ID 7
227 #define M32_HITAG_VAR_ID 8
228 #define M32_TEXTURE_VAR_ID 9
229 #define M32_DOSCRSHOT_VAR_ID 10
230 
231 #define M32_LOCAL_ARRAY_ID 0
232 
233 #define M32_PRINTERROR(Text, ...) OSD_Printf("%sLine %d, %s: " Text "\n", osd->draw.errorfmt, g_errorLineNum, keyw[g_tw], ## __VA_ARGS__)
234 #define M32_ERROR(Text, ...) do { M32_PRINTERROR(Text, ## __VA_ARGS__); vm.flags |= VMFLAG_ERROR; } while (0)
235 
236 
237 // how local gamevars are allocated:
238 
239 // uncomment if variable-length arrays are available
240 //#define M32_LOCALS_VARARRAY
241 
242 // if neither is there, use a constant number of them
243 #define M32_LOCALS_FIXEDNUM 64
244 
245 #if defined M32_LOCALS_VARARRAY
246 # define M32_MAX_LOCALS MAXGAMEVARS
247 #else
248 # define M32_MAX_LOCALS M32_LOCALS_FIXEDNUM
249 #endif
250 
Gv_GetArraySize(int32_t id)251 static inline int32_t Gv_GetArraySize(int32_t id)
252 {
253     if (aGameArrays[id].dwFlags & GAMEARRAY_VARSIZE)
254         return Gv_GetVarN(aGameArrays[id].size);
255 
256     return aGameArrays[id].size;
257 }
258 
259 #endif
260