1 //-------------------------------------------------------------------------
2 /*
3 Copyright (C) 2010 EDuke32 developers and contributors
4 
5 This file is part of EDuke32.
6 
7 EDuke32 is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License version 2
9 as published by the Free Software Foundation.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 
15 See the GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 */
21 //-------------------------------------------------------------------------
22 
23 #ifndef gamevars_h_
24 #define gamevars_h_
25 
26 #include "gamedef.h"
27 
28 #include "vfs.h"
29 
30 #define MAXGAMEVARS 2048 // must be a power of two
31 #define MAXVARLABEL 26
32 
33 #define GV_FLAG_CONSTANT (MAXGAMEVARS)
34 #define GV_FLAG_NEGATIVE (MAXGAMEVARS<<1)
35 #define GV_FLAG_ARRAY (MAXGAMEVARS<<2)
36 #define GV_FLAG_STRUCT (MAXGAMEVARS<<3)
37 
38 #define SAVEGAMEVARSKIPMASK (GAMEVAR_READONLY|GAMEVAR_SPECIAL)
39 #define SAVEGAMEARRAYSKIPMASK (GAMEARRAY_READONLY|GAMEARRAY_SYSTEM)
40 
41 #define SAVEGAMEMAPSTATEVARSKIPMASK (GAMEVAR_NORESET|SAVEGAMEVARSKIPMASK)
42 
43 // store global game definitions
44 enum GamevarFlags_t
45 {
46     GAMEVAR_PERPLAYER = 0x00000001,  // per-player variable
47     GAMEVAR_PERACTOR  = 0x00000002,  // per-actor variable
48     GAMEVAR_USER_MASK = (GAMEVAR_PERPLAYER | GAMEVAR_PERACTOR),
49     GAMEVAR_RESET     = 0x00000008,  // INTERNAL, don't use
50     GAMEVAR_DEFAULT   = 0x00000100,  // UNUSED, but always cleared for user-defined gamevars
51     GAMEVAR_NODEFAULT = 0x00000400,  // don't reset on actor spawn
52     GAMEVAR_SYSTEM    = 0x00000800,  // cannot change mode flags...(only default value)
53     GAMEVAR_READONLY  = 0x00001000,  // values are read-only (no setvar allowed)
54     GAMEVAR_INT32PTR  = 0x00002000,  // plValues is a pointer to an int32_t
55     GAMEVAR_INT16PTR  = 0x00008000,  // plValues is a pointer to a short
56     GAMEVAR_NORESET   = 0x00020000,  // var values are not reset when restoring map state
57     GAMEVAR_SPECIAL   = 0x00040000,  // flag for structure member shortcut vars
58     GAMEVAR_NOMULTI   = 0x00080000,  // don't attach to multiplayer packets
59     GAMEVAR_Q16PTR    = 0x00100000,  // plValues is a pointer to a q16.16
60     GAMEVAR_SERIALIZE = 0x00200000,  // write into permasaves
61 
62     GAMEVAR_RAWQ16PTR = GAMEVAR_Q16PTR | GAMEVAR_SPECIAL,  // plValues is a pointer to a q16.16 but we don't want conversion
63     GAMEVAR_PTR_MASK  = GAMEVAR_INT32PTR | GAMEVAR_INT16PTR | GAMEVAR_Q16PTR | GAMEVAR_RAWQ16PTR,
64 };
65 
66 // Alignments for per-player and per-actor variables.
67 #define PLAYER_VAR_ALIGNMENT (sizeof(intptr_t))
68 #define ACTOR_VAR_ALIGNMENT 16
69 
70 #define ARRAY_ALIGNMENT 16
71 
72 # define MAXGAMEARRAYS (MAXGAMEVARS>>2) // must be strictly smaller than MAXGAMEVARS
73 # define MAXARRAYLABEL MAXVARLABEL
74 
75 enum GamearrayFlags_t
76 {
77     GAMEARRAY_RESET     = 0x00000008,
78     GAMEARRAY_RESTORE   = 0x00000010,
79     GAMEARRAY_VARSIZE   = 0x00000020,
80     GAMEARRAY_STRIDE2   = 0x00000100,
81     GAMEARRAY_ALLOCATED = 0x00000200,  // memory allocated for user array
82     GAMEARRAY_SYSTEM    = 0x00000800,
83     GAMEARRAY_READONLY  = 0x00001000,
84     GAMEARRAY_INT16     = 0x00004000,
85     GAMEARRAY_INT8      = 0x00008000,
86     GAMEARRAY_UNSIGNED  = 0x00010000,
87     GAMEARRAY_UINT16    = GAMEARRAY_INT16 | GAMEARRAY_UNSIGNED,
88     GAMEARRAY_UINT8     = GAMEARRAY_INT8 | GAMEARRAY_UNSIGNED,
89     GAMEARRAY_BITMAP    = 0x00100000,
90     GAMEARRAY_WARN      = 0x00200000,
91 
92     GAMEARRAY_SIZE_MASK = GAMEARRAY_INT8 | GAMEARRAY_INT16 | GAMEARRAY_BITMAP,
93     GAMEARRAY_STORAGE_MASK = GAMEARRAY_INT8 | GAMEARRAY_INT16 | GAMEARRAY_BITMAP | GAMEARRAY_STRIDE2,
94     GAMEARRAY_TYPE_MASK = GAMEARRAY_UNSIGNED | GAMEARRAY_INT8 | GAMEARRAY_INT16 | GAMEARRAY_BITMAP,
95 };
96 
97 #pragma pack(push,1)
98 typedef struct
99 {
100     union {
101         intptr_t  global;
102         intptr_t *pValues;  // array of values when 'per-player', or 'per-actor'
103     };
104     intptr_t  defaultValue;
105     uintptr_t flags;
106     char *    szLabel;
107 } gamevar_t;
108 
109 typedef struct
110 {
111     char *    szLabel;
112     intptr_t *pValues;  // array of values
113     intptr_t  size;
114     uintptr_t flags;
115 } gamearray_t;
116 #pragma pack(pop)
117 
118 extern gamevar_t   aGameVars[MAXGAMEVARS];
119 extern gamearray_t aGameArrays[MAXGAMEARRAYS];
120 extern int32_t     g_gameVarCount;
121 extern int32_t     g_gameArrayCount;
122 
123 size_t __fastcall Gv_GetArrayAllocSizeForCount(int const arrayIdx, size_t const count);
124 size_t __fastcall Gv_GetArrayCountForAllocSize(int const arrayIdx, size_t const filelength);
Gv_GetArrayAllocSize(int const arrayIdx)125 static FORCE_INLINE size_t Gv_GetArrayAllocSize(int const arrayIdx) { return Gv_GetArrayAllocSizeForCount(arrayIdx, aGameArrays[arrayIdx].size); }
126 unsigned __fastcall Gv_GetArrayElementSize(int const arrayIdx);
127 
Gv_GetArrayValue(int const id,int index)128 static FORCE_INLINE int __fastcall Gv_GetArrayValue(int const id, int index)
129 {
130     if (aGameArrays[id].flags & GAMEARRAY_STRIDE2)
131         index <<= 1;
132 
133     switch (aGameArrays[id].flags & GAMEARRAY_TYPE_MASK)
134     {
135         default: return (aGameArrays[id].pValues)[index];
136         case GAMEARRAY_INT16:  return   ((int16_t  *)aGameArrays[id].pValues)[index];
137         case GAMEARRAY_INT8:   return   ((int8_t   *)aGameArrays[id].pValues)[index];
138         case GAMEARRAY_UINT16: return   ((uint16_t *)aGameArrays[id].pValues)[index];
139         case GAMEARRAY_UINT8:  return   ((uint8_t  *)aGameArrays[id].pValues)[index];
140         case GAMEARRAY_BITMAP: return !!(((uint8_t *)aGameArrays[id].pValues)[index >> 3] & pow2char[index & 7]);
141     }
142 }
143 
144 int __fastcall Gv_GetVar(int const gameVar, int const spriteNum, int const playerNum);
145 void __fastcall Gv_SetVar(int const gameVar, int const newValue, int const spriteNum, int const playerNum);
146 int __fastcall Gv_GetVar(int const gameVar);
147 void __fastcall Gv_GetManyVars(int const numVars, int32_t * const outBuf);
148 void __fastcall Gv_SetVar(int const gameVar, int const newValue);
149 
150 template <typename T>
Gv_FillWithVars(T & rv)151 static FORCE_INLINE void Gv_FillWithVars(T & rv)
152 {
153     EDUKE32_STATIC_ASSERT(sizeof(T) % sizeof(int32_t) == 0);
154     EDUKE32_STATIC_ASSERT(sizeof(T) > sizeof(int32_t));
155     Gv_GetManyVars(sizeof(T)/sizeof(int32_t), (int32_t *)&rv);
156 }
157 
158 int Gv_GetVarByLabel(const char *szGameLabel,int defaultValue,int spriteNum,int playerNum);
159 void Gv_NewArray(const char *pszLabel,void *arrayptr,intptr_t asize,uint32_t dwFlags);
160 void Gv_NewVar(const char *pszLabel,intptr_t lValue,uint32_t dwFlags);
161 
A_ResetVars(int const spriteNum)162 static FORCE_INLINE void A_ResetVars(int const spriteNum)
163 {
164     for (auto &gv : aGameVars)
165     {
166         if ((gv.flags & (GAMEVAR_PERACTOR|GAMEVAR_NODEFAULT)) == GAMEVAR_PERACTOR)
167             gv.pValues[spriteNum] = gv.defaultValue;
168     }
169 }
170 void VM_InitHashTables(void);
171 void Gv_DumpValues(void);
172 void Gv_InitWeaponPointers(void);
173 void Gv_RefreshPointers(void);
174 void Gv_ResetVars(void);
175 int Gv_ReadSave(buildvfs_kfd kFile);
176 void Gv_WriteSave(buildvfs_FILE fil);
177 void Gv_Clear(void);
178 
179 void Gv_ResetSystemDefaults(void);
180 void Gv_Init(void);
181 void Gv_FinalizeWeaponDefaults(void);
182 
183 #define LABEL_TYPES (LABEL_CHAR|LABEL_SHORT|LABEL_INT|LABEL_UNSIGNED)
184 
VM_GetStruct(uint32_t const flags,intptr_t * const addr)185 static inline int __fastcall VM_GetStruct(uint32_t const flags, intptr_t * const addr)
186 {
187     switch (flags & LABEL_TYPES)
188     {
189         case LABEL_CHAR:                 return *(int8_t   *)addr;
190         case LABEL_CHAR|LABEL_UNSIGNED:  return *(uint8_t  *)addr;
191         case LABEL_SHORT:                return *(int16_t  *)addr;
192         case LABEL_SHORT|LABEL_UNSIGNED: return *(uint16_t *)addr;
193         case LABEL_INT:                  return *(int32_t  *)addr;
194         case LABEL_INT|LABEL_UNSIGNED:   return *(uint32_t *)addr;
195         default: EDUKE32_UNREACHABLE_SECTION(break);
196     }
197 }
198 
VM_SetStruct(uint32_t const flags,intptr_t * const addr,int32_t newValue)199 static FORCE_INLINE void __fastcall VM_SetStruct(uint32_t const flags, intptr_t * const addr, int32_t newValue)
200 {
201     switch (flags & LABEL_TYPES)
202     {
203         case LABEL_CHAR:                 *(int8_t   *)addr = newValue; break;
204         case LABEL_CHAR|LABEL_UNSIGNED:  *(uint8_t  *)addr = newValue; break;
205         case LABEL_SHORT:                *(int16_t  *)addr = newValue; break;
206         case LABEL_SHORT|LABEL_UNSIGNED: *(uint16_t *)addr = newValue; break;
207         case LABEL_INT:                  *(int32_t  *)addr = newValue; break;
208         case LABEL_INT|LABEL_UNSIGNED:   *(uint32_t *)addr = newValue; break;
209         default: EDUKE32_UNREACHABLE_SECTION(break);
210     }
211 }
212 
213 #undef LABEL_TYPES
214 
215 #define VM_GAMEVAR_OPERATOR(func, operator)                                                            \
216     static FORCE_INLINE ATTRIBUTE((flatten)) void __fastcall func(int const id, int32_t const operand) \
217     {                                                                                                  \
218         auto &var = aGameVars[id];                                                                     \
219                                                                                                        \
220         switch (var.flags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK))                                    \
221         {                                                                                              \
222             default:                                                                                   \
223                 var.global operator operand;                                                           \
224                 break;                                                                                 \
225             case GAMEVAR_PERPLAYER:                                                                    \
226                 var.pValues[vm.playerNum & (MAXPLAYERS-1)] operator operand;                           \
227                 break;                                                                                 \
228             case GAMEVAR_PERACTOR:                                                                     \
229                 var.pValues[vm.spriteNum & (MAXSPRITES-1)] operator operand;                           \
230                 break;                                                                                 \
231             case GAMEVAR_INT32PTR: *(int32_t *)var.pValues operator(int32_t) operand; break;           \
232             case GAMEVAR_INT16PTR: *(int16_t *)var.pValues operator(int16_t) operand; break;           \
233             case GAMEVAR_Q16PTR:                                                                       \
234             {                                                                                          \
235                 Fix16 *pfix = (Fix16 *)var.global;                                                     \
236                 *pfix  operator fix16_from_int(operand);                                               \
237                 break;                                                                                 \
238             }                                                                                          \
239         }                                                                                              \
240     }
241 
Gv_DivVar(int const id,int32_t const d)242 static FORCE_INLINE void __fastcall Gv_DivVar(int const id, int32_t const d)
243 {
244     using namespace libdivide;
245 
246     static libdivide_s32_t sdiv;
247     static int32_t lastValue;
248 
249     auto &var = aGameVars[id];
250     auto *dptr = &sdiv;
251     auto iptr = &var.global;
252 
253     if ((unsigned)d < DIVTABLESIZE)
254         dptr = &divtable32[d];
255     else if (d != lastValue)
256         sdiv = libdivide_s32_gen((lastValue = d));
257 
258     switch (var.flags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK))
259     {
260         case GAMEVAR_PERACTOR: iptr = &var.pValues[vm.spriteNum & (MAXSPRITES-1)]; goto jmp;
261         case GAMEVAR_PERPLAYER: iptr = &var.pValues[vm.playerNum & (MAXPLAYERS-1)]; fallthrough__;
262         default: jmp: *iptr = libdivide_s32_do(*iptr, dptr); break;
263 
264         case GAMEVAR_INT32PTR:
265         {
266             auto &value = *(int32_t *)var.pValues;
267             value = libdivide_s32_do(value, dptr);
268             break;
269         }
270         case GAMEVAR_INT16PTR:
271         {
272             auto &value = *(int16_t *)var.pValues;
273             value = (int16_t)libdivide_s32_do(value, dptr);
274             break;
275         }
276         case GAMEVAR_Q16PTR:
277         {
278             auto &value = *(fix16_t *)var.pValues;
279             value = fix16_div(value, fix16_from_int(d));
280             break;
281         }
282     }
283 }
284 
285 VM_GAMEVAR_OPERATOR(Gv_AddVar, +=)
286 VM_GAMEVAR_OPERATOR(Gv_SubVar, -=)
287 VM_GAMEVAR_OPERATOR(Gv_MulVar, *=)
288 VM_GAMEVAR_OPERATOR(Gv_ModVar, %=)
289 VM_GAMEVAR_OPERATOR(Gv_AndVar, &=)
290 VM_GAMEVAR_OPERATOR(Gv_XorVar, ^=)
291 VM_GAMEVAR_OPERATOR(Gv_OrVar, |=)
292 VM_GAMEVAR_OPERATOR(Gv_ShiftVarL, <<=)
293 VM_GAMEVAR_OPERATOR(Gv_ShiftVarR, >>=)
294 
295 #undef VM_GAMEVAR_OPERATOR
296 
297 #endif
298