Subversion Repositories eduke32

Rev

Rev 4839 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
398 terminx 1
//-------------------------------------------------------------------------
2
/*
1652 terminx 3
Copyright (C) 2010 EDuke32 developers and contributors
398 terminx 4
 
1652 terminx 5
This file is part of EDuke32.
398 terminx 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
4541 hendricks2 19
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
398 terminx 20
*/
21
//-------------------------------------------------------------------------
22
 
23
#include "duke3d.h"
2726 hendricks2 24
#include "common_game.h"
1677 terminx 25
#include "gamevars.h"
398 terminx 26
#include "gamedef.h"
514 terminx 27
#include "osd.h"
1950 helixhorne 28
#include "savegame.h"
4496 hendricks2 29
#include "menus.h"
398 terminx 30
 
4747 terminx 31
#define gamevars_c_
3459 helixhorne 32
 
4286 helixhorne 33
#ifdef LUNATIC
34
int32_t g_noResetVars;
35
LUNATIC_CB void (*A_ResetVars)(int32_t iActor);
36
#else
3354 helixhorne 37
# include "gamestructures.c"
1204 terminx 38
 
4839 helixhorne 39
// Frees the memory for the *values* of game variables and arrays. Resets their
40
// counts to zero. Call this function as many times as needed.
41
//
42
// Returns: old g_gameVarCount | (g_gameArrayCount<<16).
43
static int32_t Gv_Free(void)
398 terminx 44
{
4839 helixhorne 45
    for (int32_t i=0; i<g_gameVarCount; i++)
782 terminx 46
    {
4698 terminx 47
        if (aGameVars[i].dwFlags & GAMEVAR_USER_MASK)
48
            ALIGNED_FREE_AND_NULL(aGameVars[i].val.plValues);
1657 terminx 49
 
1189 terminx 50
        aGameVars[i].dwFlags |= GAMEVAR_RESET;
4839 helixhorne 51
    }
1657 terminx 52
 
4839 helixhorne 53
    for (int32_t i=0; i<g_gameArrayCount; i++)
54
    {
4698 terminx 55
        if (aGameArrays[i].dwFlags & GAMEARRAY_NORMAL)
56
            ALIGNED_FREE_AND_NULL(aGameArrays[i].plValues);
1657 terminx 57
 
3274 hendricks2 58
        aGameArrays[i].dwFlags |= GAMEARRAY_RESET;
782 terminx 59
    }
3416 helixhorne 60
 
4839 helixhorne 61
    EDUKE32_STATIC_ASSERT(MAXGAMEVARS < 32768);
62
    int32_t ret = g_gameVarCount | (g_gameArrayCount<<16);
3416 helixhorne 63
    g_gameVarCount = g_gameArrayCount = 0;
4839 helixhorne 64
 
1625 terminx 65
    hash_init(&h_gamevars);
66
    hash_init(&h_arrays);
4839 helixhorne 67
 
68
    return ret;
398 terminx 69
}
70
 
4839 helixhorne 71
// Calls Gv_Free() and in addition frees the labels of all game variables and
72
// arrays.
73
// Only call this function ONCE...
1143 terminx 74
static void Gv_Clear(void)
398 terminx 75
{
4839 helixhorne 76
    int32_t n = Gv_Free();
77
    int32_t gameVarCount = n&65535, gameArrayCount = n>>16;
398 terminx 78
 
3416 helixhorne 79
    // Now, only do work that Gv_Free() hasn't done.
4839 helixhorne 80
    for (int32_t i=0; i<gameVarCount; i++)
4698 terminx 81
        DO_FREE_AND_NULL(aGameVars[i].szLabel);
398 terminx 82
 
4839 helixhorne 83
    for (int32_t i=0; i<gameArrayCount; i++)
4698 terminx 84
        DO_FREE_AND_NULL(aGameArrays[i].szLabel);
398 terminx 85
}
86
 
1595 helixhorne 87
int32_t Gv_ReadSave(int32_t fil, int32_t newbehav)
398 terminx 88
{
1205 terminx 89
    int32_t i, j;
860 terminx 90
    char savedstate[MAXVOLUMES*MAXLEVELS];
1595 helixhorne 91
    char tbuf[12];
398 terminx 92
 
1595 helixhorne 93
    if (newbehav)
94
    {
95
        if (kread(fil, tbuf, 12)!=12) goto corrupt;
96
        if (Bmemcmp(tbuf, "BEG: EDuke32", 12)) { OSD_Printf("BEG ERR\n"); return 2; }
97
    }
98
 
860 terminx 99
    Bmemset(&savedstate,0,sizeof(savedstate));
100
 
782 terminx 101
    //     AddLog("Reading gamevars from savegame");
398 terminx 102
 
1143 terminx 103
    Gv_Free(); // nuke 'em from orbit, it's the only way to be sure...
1185 terminx 104
 
782 terminx 105
    //  Bsprintf(g_szBuf,"CP:%s %d",__FILE__,__LINE__);
106
    //  AddLog(g_szBuf);
398 terminx 107
 
1143 terminx 108
    if (kdfread(&g_gameVarCount,sizeof(g_gameVarCount),1,fil) != 1) goto corrupt;
1229 terminx 109
    for (i=0; i<g_gameVarCount; i++)
782 terminx 110
    {
4839 helixhorne 111
        char *const olabel = aGameVars[i].szLabel;
112
 
113
        if (kdfread(&aGameVars[i], sizeof(gamevar_t), 1, fil) != 1)
114
            goto corrupt;
115
 
116
        if (olabel == NULL)
117
            aGameVars[i].szLabel = (char *)Xmalloc(MAXVARLABEL * sizeof(uint8_t));
118
        else
119
            aGameVars[i].szLabel = olabel;
120
 
121
        if (kdfread(aGameVars[i].szLabel, MAXVARLABEL, 1, fil) != 1)
122
            goto corrupt;
1677 terminx 123
        hash_add(&h_gamevars, aGameVars[i].szLabel,i, 1);
398 terminx 124
 
1143 terminx 125
        if (aGameVars[i].dwFlags & GAMEVAR_PERPLAYER)
782 terminx 126
        {
4698 terminx 127
            aGameVars[i].val.plValues = (intptr_t*)Xaligned_alloc(PLAYER_VAR_ALIGNMENT, MAXPLAYERS * sizeof(intptr_t));
1189 terminx 128
            if (kdfread(aGameVars[i].val.plValues,sizeof(intptr_t) * MAXPLAYERS, 1, fil) != 1) goto corrupt;
782 terminx 129
        }
1143 terminx 130
        else if (aGameVars[i].dwFlags & GAMEVAR_PERACTOR)
782 terminx 131
        {
4698 terminx 132
            aGameVars[i].val.plValues = (intptr_t*)Xaligned_alloc(ACTOR_VAR_ALIGNMENT, MAXSPRITES * sizeof(intptr_t));
1189 terminx 133
            if (kdfread(&aGameVars[i].val.plValues[0],sizeof(intptr_t), MAXSPRITES, fil) != MAXSPRITES) goto corrupt;
782 terminx 134
        }
135
    }
1189 terminx 136
    //  Bsprintf(g_szBuf,"CP:%s %d",__FILE__,__LINE__);
137
    //  AddLog(g_szBuf);
398 terminx 138
 
1189 terminx 139
    Gv_InitWeaponPointers();
140
 
1182 terminx 141
    //  Bsprintf(g_szBuf,"CP:%s %d",__FILE__,__LINE__);
142
    //  AddLog(g_szBuf);
1143 terminx 143
    Gv_RefreshPointers();
534 terminx 144
 
1143 terminx 145
    if (kdfread(&g_gameArrayCount,sizeof(g_gameArrayCount),1,fil) != 1) goto corrupt;
1229 terminx 146
    for (i=0; i<g_gameArrayCount; i++)
782 terminx 147
    {
3295 helixhorne 148
        if (aGameArrays[i].dwFlags&GAMEARRAY_READONLY)
149
            continue;
150
 
4839 helixhorne 151
        char *const olabel = aGameArrays[i].szLabel;
152
 
3295 helixhorne 153
        // read for .size and .dwFlags (the rest are pointers):
4839 helixhorne 154
        if (kdfread(&aGameArrays[i], sizeof(gamearray_t), 1, fil) != 1)
155
            goto corrupt;
3295 helixhorne 156
 
4839 helixhorne 157
        if (olabel == NULL)
158
            aGameArrays[i].szLabel = (char *)Xmalloc(MAXARRAYLABEL * sizeof(uint8_t));
159
        else
160
            aGameArrays[i].szLabel = olabel;
161
 
162
        if (kdfread(aGameArrays[i].szLabel,sizeof(uint8_t) * MAXARRAYLABEL, 1, fil) != 1)
163
            goto corrupt;
1677 terminx 164
        hash_add(&h_arrays, aGameArrays[i].szLabel, i, 1);
1189 terminx 165
 
4698 terminx 166
        aGameArrays[i].plValues = (intptr_t *)Xaligned_alloc(ACTOR_VAR_ALIGNMENT, aGameArrays[i].size * GAR_ELTSZ);
2689 helixhorne 167
        if (kdfread(aGameArrays[i].plValues, GAR_ELTSZ * aGameArrays[i].size, 1, fil) < 1) goto corrupt;
782 terminx 168
    }
676 terminx 169
 
782 terminx 170
    //  Bsprintf(g_szBuf,"CP:%s %d",__FILE__,__LINE__);
171
    //  AddLog(g_szBuf);
172
    if (kdfread(apScriptGameEvent,sizeof(apScriptGameEvent),1,fil) != 1) goto corrupt;
1950 helixhorne 173
    G_Util_PtrToIdx(apScriptGameEvent, MAXGAMEEVENTS, script, P2I_BACK_NON0);
398 terminx 174
 
782 terminx 175
    //  Bsprintf(g_szBuf,"CP:%s %d",__FILE__,__LINE__);
176
    //  AddLog(g_szBuf);
860 terminx 177
 
178
    if (kdfread(&savedstate[0],sizeof(savedstate),1,fil) != 1) goto corrupt;
179
 
1229 terminx 180
    for (i=0; i<(MAXVOLUMES*MAXLEVELS); i++)
860 terminx 181
    {
182
        if (savedstate[i])
183
        {
1143 terminx 184
            if (MapInfo[i].savedstate == NULL)
4698 terminx 185
                MapInfo[i].savedstate = (mapstate_t *)Xaligned_alloc(16, sizeof(mapstate_t));
1143 terminx 186
            if (kdfread(MapInfo[i].savedstate,sizeof(mapstate_t),1,fil) != sizeof(mapstate_t)) goto corrupt;
1229 terminx 187
            for (j=0; j<g_gameVarCount; j++)
862 terminx 188
            {
1143 terminx 189
                if (aGameVars[j].dwFlags & GAMEVAR_NORESET) continue;
190
                if (aGameVars[j].dwFlags & GAMEVAR_PERPLAYER)
862 terminx 191
                {
1451 terminx 192
//                    if (!MapInfo[i].savedstate->vars[j])
4698 terminx 193
                    MapInfo[i].savedstate->vars[j] = (intptr_t *)Xaligned_alloc(PLAYER_VAR_ALIGNMENT, MAXPLAYERS * sizeof(intptr_t));
1143 terminx 194
                    if (kdfread(&MapInfo[i].savedstate->vars[j][0],sizeof(intptr_t) * MAXPLAYERS, 1, fil) != 1) goto corrupt;
862 terminx 195
                }
1143 terminx 196
                else if (aGameVars[j].dwFlags & GAMEVAR_PERACTOR)
862 terminx 197
                {
1451 terminx 198
//                    if (!MapInfo[i].savedstate->vars[j])
4698 terminx 199
                    MapInfo[i].savedstate->vars[j] = (intptr_t *)Xaligned_alloc(ACTOR_VAR_ALIGNMENT, MAXSPRITES * sizeof(intptr_t));
1143 terminx 200
                    if (kdfread(&MapInfo[i].savedstate->vars[j][0],sizeof(intptr_t), MAXSPRITES, fil) != MAXSPRITES) goto corrupt;
862 terminx 201
                }
202
            }
860 terminx 203
        }
3791 helixhorne 204
        else
860 terminx 205
        {
1143 terminx 206
            G_FreeMapState(i);
860 terminx 207
        }
208
    }
209
 
1595 helixhorne 210
    if (!newbehav)
211
    {
2185 helixhorne 212
        intptr_t l;
213
 
1595 helixhorne 214
        if (kdfread(&l,sizeof(l),1,fil) != 1) goto corrupt;
215
        if (kdfread(g_szBuf,l,1,fil) != 1) goto corrupt;
216
        g_szBuf[l]=0;
217
        OSD_Printf("%s\n",g_szBuf);
218
    }
219
    else
220
    {
221
        if (kread(fil, tbuf, 12)!=12) goto corrupt;
222
        if (Bmemcmp(tbuf, "EOF: EDuke32", 12)) { OSD_Printf("EOF ERR\n"); return 2; }
223
    }
398 terminx 224
 
3459 helixhorne 225
# if 0
782 terminx 226
    {
227
        FILE *fp;
228
        AddLog("Dumping Vars...");
229
        fp=fopen("xxx.txt","w");
230
        if (fp)
231
        {
1143 terminx 232
            Gv_DumpValues(fp);
782 terminx 233
            fclose(fp);
234
        }
235
        AddLog("Done Dumping...");
236
    }
3459 helixhorne 237
# endif
782 terminx 238
    return(0);
398 terminx 239
corrupt:
782 terminx 240
    return(1);
398 terminx 241
}
242
 
1595 helixhorne 243
void Gv_WriteSave(FILE *fil, int32_t newbehav)
398 terminx 244
{
1205 terminx 245
    int32_t i, j;
860 terminx 246
    char savedstate[MAXVOLUMES*MAXLEVELS];
398 terminx 247
 
860 terminx 248
    Bmemset(&savedstate,0,sizeof(savedstate));
249
 
782 terminx 250
    //   AddLog("Saving Game Vars to File");
1595 helixhorne 251
    if (newbehav)
252
        fwrite("BEG: EDuke32", 12, 1, fil);
253
 
1143 terminx 254
    dfwrite(&g_gameVarCount,sizeof(g_gameVarCount),1,fil);
398 terminx 255
 
1229 terminx 256
    for (i=0; i<g_gameVarCount; i++)
782 terminx 257
    {
258
        dfwrite(&(aGameVars[i]),sizeof(gamevar_t),1,fil);
1205 terminx 259
        dfwrite(aGameVars[i].szLabel,sizeof(uint8_t) * MAXVARLABEL, 1, fil);
398 terminx 260
 
1143 terminx 261
        if (aGameVars[i].dwFlags & GAMEVAR_PERPLAYER)
782 terminx 262
        {
1205 terminx 263
            //Bsprintf(g_szBuf,"Writing value array for %s (%d)",aGameVars[i].szLabel,sizeof(int32_t) * MAXPLAYERS);
782 terminx 264
            //AddLog(g_szBuf);
1189 terminx 265
            dfwrite(aGameVars[i].val.plValues,sizeof(intptr_t) * MAXPLAYERS, 1, fil);
782 terminx 266
        }
1143 terminx 267
        else if (aGameVars[i].dwFlags & GAMEVAR_PERACTOR)
782 terminx 268
        {
1205 terminx 269
            //Bsprintf(g_szBuf,"Writing value array for %s (%d)",aGameVars[i].szLabel,sizeof(int32_t) * MAXSPRITES);
782 terminx 270
            //AddLog(g_szBuf);
1189 terminx 271
            dfwrite(&aGameVars[i].val.plValues[0],sizeof(intptr_t), MAXSPRITES, fil);
782 terminx 272
        }
273
    }
398 terminx 274
 
1143 terminx 275
    dfwrite(&g_gameArrayCount,sizeof(g_gameArrayCount),1,fil);
676 terminx 276
 
1229 terminx 277
    for (i=0; i<g_gameArrayCount; i++)
782 terminx 278
    {
3295 helixhorne 279
        if (aGameArrays[i].dwFlags&GAMEARRAY_READONLY)
280
            continue;
281
 
282
        // write for .size and .dwFlags (the rest are pointers):
283
        dfwrite(&aGameArrays[i],sizeof(gamearray_t),1,fil);
284
 
1205 terminx 285
        dfwrite(aGameArrays[i].szLabel,sizeof(uint8_t) * MAXARRAYLABEL, 1, fil);
2689 helixhorne 286
        dfwrite(aGameArrays[i].plValues, GAR_ELTSZ * aGameArrays[i].size, 1, fil);
782 terminx 287
    }
676 terminx 288
 
1950 helixhorne 289
    G_Util_PtrToIdx(apScriptGameEvent, MAXGAMEEVENTS, script, P2I_FWD_NON0);
782 terminx 290
    dfwrite(apScriptGameEvent,sizeof(apScriptGameEvent),1,fil);
1950 helixhorne 291
    G_Util_PtrToIdx(apScriptGameEvent, MAXGAMEEVENTS, script, P2I_BACK_NON0);
398 terminx 292
 
1229 terminx 293
    for (i=0; i<(MAXVOLUMES*MAXLEVELS); i++)
1143 terminx 294
        if (MapInfo[i].savedstate != NULL)
860 terminx 295
            savedstate[i] = 1;
296
 
297
    dfwrite(&savedstate[0],sizeof(savedstate),1,fil);
298
 
1229 terminx 299
    for (i=0; i<(MAXVOLUMES*MAXLEVELS); i++)
1143 terminx 300
        if (MapInfo[i].savedstate)
862 terminx 301
        {
1143 terminx 302
            dfwrite(MapInfo[i].savedstate,sizeof(mapstate_t),1,fil);
1229 terminx 303
            for (j=0; j<g_gameVarCount; j++)
862 terminx 304
            {
1143 terminx 305
                if (aGameVars[j].dwFlags & GAMEVAR_NORESET) continue;
306
                if (aGameVars[j].dwFlags & GAMEVAR_PERPLAYER)
862 terminx 307
                {
1143 terminx 308
                    dfwrite(&MapInfo[i].savedstate->vars[j][0],sizeof(intptr_t) * MAXPLAYERS, 1, fil);
862 terminx 309
                }
1143 terminx 310
                else if (aGameVars[j].dwFlags & GAMEVAR_PERACTOR)
862 terminx 311
                {
1540 terminx 312
                    dfwrite(&MapInfo[i].savedstate->vars[j][0],sizeof(intptr_t), MAXSPRITES, fil);
862 terminx 313
                }
314
            }
315
        }
860 terminx 316
 
1595 helixhorne 317
    if (!newbehav)
318
    {
2185 helixhorne 319
        intptr_t l;
320
 
1595 helixhorne 321
        Bsprintf(g_szBuf,"EOF: EDuke32");
322
        l=Bstrlen(g_szBuf);
323
        dfwrite(&l,sizeof(l),1,fil);
324
        dfwrite(g_szBuf,l,1,fil);
325
    }
326
    else
327
        fwrite("EOF: EDuke32", 12, 1, fil);
398 terminx 328
}
329
 
1221 terminx 330
void Gv_DumpValues(void)
398 terminx 331
{
1205 terminx 332
    int32_t i;
1195 terminx 333
 
1221 terminx 334
    OSD_Printf("// Current Game Definitions\n\n");
1195 terminx 335
 
1229 terminx 336
    for (i=0; i<g_gameVarCount; i++)
782 terminx 337
    {
1143 terminx 338
        if (aGameVars[i].dwFlags & (GAMEVAR_SECRET))
782 terminx 339
            continue; // do nothing...
1195 terminx 340
 
1221 terminx 341
        OSD_Printf("gamevar %s ",aGameVars[i].szLabel);
1195 terminx 342
 
343
        if (aGameVars[i].dwFlags & (GAMEVAR_INTPTR))
1677 terminx 344
            OSD_Printf("%d",*((int32_t *)aGameVars[i].val.lValue));
1195 terminx 345
        else if (aGameVars[i].dwFlags & (GAMEVAR_SHORTPTR))
1677 terminx 346
            OSD_Printf("%d",*((int16_t *)aGameVars[i].val.lValue));
1195 terminx 347
        else if (aGameVars[i].dwFlags & (GAMEVAR_CHARPTR))
1677 terminx 348
            OSD_Printf("%d",*((char *)aGameVars[i].val.lValue));
782 terminx 349
        else
1221 terminx 350
            OSD_Printf("%" PRIdPTR "",aGameVars[i].val.lValue);
351
 
1195 terminx 352
        if (aGameVars[i].dwFlags & (GAMEVAR_PERPLAYER))
1221 terminx 353
            OSD_Printf(" GAMEVAR_PERPLAYER");
1195 terminx 354
        else if (aGameVars[i].dwFlags & (GAMEVAR_PERACTOR))
1221 terminx 355
            OSD_Printf(" GAMEVAR_PERACTOR");
1195 terminx 356
        else
4321 hendricks2 357
            OSD_Printf(" %" PRIdPTR,aGameVars[i].dwFlags/* & (GAMEVAR_USER_MASK)*/);
1221 terminx 358
 
359
        OSD_Printf(" // ");
1195 terminx 360
        if (aGameVars[i].dwFlags & (GAMEVAR_SYSTEM))
1221 terminx 361
            OSD_Printf(" (system)");
1857 terminx 362
        if (aGameVars[i].dwFlags & (GAMEVAR_PTR_MASK))
1221 terminx 363
            OSD_Printf(" (pointer)");
1195 terminx 364
        if (aGameVars[i].dwFlags & (GAMEVAR_READONLY))
1221 terminx 365
            OSD_Printf(" (read only)");
366
        if (aGameVars[i].dwFlags & (GAMEVAR_SPECIAL))
367
            OSD_Printf(" (special)");
368
        OSD_Printf("\n");
782 terminx 369
    }
1221 terminx 370
    OSD_Printf("\n// end of game definitions\n");
398 terminx 371
}
372
 
2230 helixhorne 373
// XXX: This function is very strange.
1179 terminx 374
void Gv_ResetVars(void) /* this is called during a new game and nowhere else */
398 terminx 375
{
1205 terminx 376
    int32_t i;
398 terminx 377
 
1143 terminx 378
    Gv_Free();
398 terminx 379
 
4536 terminx 380
    osd->log.errors = 0;
381
 
1229 terminx 382
    for (i=0; i<MAXGAMEVARS; i++)
782 terminx 383
    {
1665 terminx 384
        if (aGameVars[i].szLabel != NULL)
385
            Gv_NewVar(aGameVars[i].szLabel,
1677 terminx 386
                      aGameVars[i].dwFlags & GAMEVAR_NODEFAULT ? aGameVars[i].val.lValue : aGameVars[i].lDefault,
387
                      aGameVars[i].dwFlags);
782 terminx 388
    }
676 terminx 389
 
1229 terminx 390
    for (i=0; i<MAXGAMEARRAYS; i++)
782 terminx 391
    {
3274 hendricks2 392
        if (aGameArrays[i].szLabel != NULL && (aGameArrays[i].dwFlags & GAMEARRAY_RESET))
393
            Gv_NewArray(aGameArrays[i].szLabel,aGameArrays[i].plValues,aGameArrays[i].size,aGameArrays[i].dwFlags);
782 terminx 394
    }
398 terminx 395
}
396
 
3274 hendricks2 397
int32_t Gv_NewArray(const char *pszLabel, void *arrayptr, intptr_t asize, uint32_t dwFlags)
676 terminx 398
{
1205 terminx 399
    int32_t i;
676 terminx 400
 
4680 terminx 401
    if (EDUKE32_PREDICT_FALSE(g_gameArrayCount >= MAXGAMEARRAYS))
1179 terminx 402
    {
403
        g_numCompilerErrors++;
404
        C_ReportError(-1);
405
        initprintf("%s:%d: error: too many arrays!\n",g_szScriptFileName,g_lineNumber);
406
        return 0;
407
    }
408
 
4680 terminx 409
    if (EDUKE32_PREDICT_FALSE(Bstrlen(pszLabel) > (MAXARRAYLABEL-1)))
782 terminx 410
    {
1143 terminx 411
        g_numCompilerErrors++;
412
        C_ReportError(-1);
413
        initprintf("%s:%d: error: array name `%s' exceeds limit of %d characters.\n",g_szScriptFileName,g_lineNumber,pszLabel, MAXARRAYLABEL);
782 terminx 414
        return 0;
415
    }
1625 terminx 416
    i = hash_find(&h_arrays,pszLabel);
4680 terminx 417
 
418
    if (EDUKE32_PREDICT_FALSE(i >=0 && !(aGameArrays[i].dwFlags & GAMEARRAY_RESET)))
782 terminx 419
    {
1024 terminx 420
        // found it it's a duplicate in error
3274 hendricks2 421
 
1143 terminx 422
        g_numCompilerWarnings++;
3274 hendricks2 423
 
424
        if (aGameArrays[i].dwFlags&GAMEARRAY_TYPE_MASK)
425
        {
426
            C_ReportError(-1);
427
            initprintf("ignored redefining system array `%s'.", pszLabel);
428
        }
429
        else
430
            C_ReportError(WARNING_DUPLICATEDEFINITION);
431
 
1024 terminx 432
        return 0;
782 terminx 433
    }
1179 terminx 434
 
1143 terminx 435
    i = g_gameArrayCount;
1179 terminx 436
 
437
    if (aGameArrays[i].szLabel == NULL)
4839 helixhorne 438
        aGameArrays[i].szLabel = (char *)Xcalloc(MAXVARLABEL,sizeof(uint8_t));
4698 terminx 439
 
1179 terminx 440
    if (aGameArrays[i].szLabel != pszLabel)
441
        Bstrcpy(aGameArrays[i].szLabel,pszLabel);
3274 hendricks2 442
 
443
    if (!(dwFlags & GAMEARRAY_TYPE_MASK))
4698 terminx 444
    {
4793 helixhorne 445
        Baligned_free(aGameArrays[i].plValues);
4698 terminx 446
        aGameArrays[i].plValues = (intptr_t *)Xaligned_alloc(ACTOR_VAR_ALIGNMENT, asize * GAR_ELTSZ);
447
        Bmemset(aGameArrays[i].plValues, 0, asize * GAR_ELTSZ);
448
    }
3274 hendricks2 449
    else
450
        aGameArrays[i].plValues=(intptr_t *)arrayptr;
451
 
1179 terminx 452
    aGameArrays[i].size=asize;
3274 hendricks2 453
    aGameArrays[i].dwFlags = dwFlags & ~GAMEARRAY_RESET;
454
 
1179 terminx 455
    g_gameArrayCount++;
1677 terminx 456
    hash_add(&h_arrays, aGameArrays[i].szLabel, i, 1);
3354 helixhorne 457
 
1179 terminx 458
    return 1;
676 terminx 459
}
460
 
2005 terminx 461
int32_t Gv_NewVar(const char *pszLabel, intptr_t lValue, uint32_t dwFlags)
398 terminx 462
{
1205 terminx 463
    int32_t i, j;
398 terminx 464
 
1179 terminx 465
    //Bsprintf(g_szBuf,"Gv_NewVar(%s, %d, %X)",pszLabel, lValue, dwFlags);
782 terminx 466
    //AddLog(g_szBuf);
398 terminx 467
 
4680 terminx 468
    if (EDUKE32_PREDICT_FALSE(g_gameVarCount >= MAXGAMEVARS))
1179 terminx 469
    {
470
        g_numCompilerErrors++;
471
        C_ReportError(-1);
472
        initprintf("%s:%d: error: too many gamevars!\n",g_szScriptFileName,g_lineNumber);
473
        return 0;
474
    }
475
 
4680 terminx 476
    if (EDUKE32_PREDICT_FALSE(Bstrlen(pszLabel) > (MAXVARLABEL-1)))
782 terminx 477
    {
1143 terminx 478
        g_numCompilerErrors++;
479
        C_ReportError(-1);
480
        initprintf("%s:%d: error: variable name `%s' exceeds limit of %d characters.\n",g_szScriptFileName,g_lineNumber,pszLabel, MAXVARLABEL);
782 terminx 481
        return 0;
482
    }
1179 terminx 483
 
1625 terminx 484
    i = hash_find(&h_gamevars,pszLabel);
1034 terminx 485
 
1189 terminx 486
    if (i >= 0 && !(aGameVars[i].dwFlags & GAMEVAR_RESET))
782 terminx 487
    {
1034 terminx 488
        // found it...
4680 terminx 489
        if (EDUKE32_PREDICT_FALSE(aGameVars[i].dwFlags & (GAMEVAR_PTR_MASK)))
782 terminx 490
        {
1143 terminx 491
            C_ReportError(-1);
492
            initprintf("%s:%d: warning: cannot redefine internal gamevar `%s'.\n",g_szScriptFileName,g_lineNumber,label+(g_numLabels<<6));
1034 terminx 493
            return 0;
782 terminx 494
        }
4680 terminx 495
        else if (EDUKE32_PREDICT_FALSE(!(aGameVars[i].dwFlags & GAMEVAR_SYSTEM)))
1034 terminx 496
        {
497
            // it's a duplicate in error
1143 terminx 498
            g_numCompilerWarnings++;
499
            C_ReportError(WARNING_DUPLICATEDEFINITION);
1034 terminx 500
            return 0;
501
        }
782 terminx 502
    }
1179 terminx 503
 
1034 terminx 504
    if (i == -1)
1143 terminx 505
        i = g_gameVarCount;
1034 terminx 506
 
3416 helixhorne 507
    // If it's a user gamevar...
1143 terminx 508
    if ((aGameVars[i].dwFlags & GAMEVAR_SYSTEM) == 0)
1035 terminx 509
    {
3416 helixhorne 510
        // Allocate and set its label
1035 terminx 511
        if (aGameVars[i].szLabel == NULL)
4491 helixhorne 512
            aGameVars[i].szLabel = (char *)Xcalloc(MAXVARLABEL,sizeof(uint8_t));
4698 terminx 513
 
1035 terminx 514
        if (aGameVars[i].szLabel != pszLabel)
515
            Bstrcpy(aGameVars[i].szLabel,pszLabel);
3416 helixhorne 516
 
517
        // and the flags
1035 terminx 518
        aGameVars[i].dwFlags=dwFlags;
519
 
4698 terminx 520
        // only free if per-{actor,player}
1195 terminx 521
        if (aGameVars[i].dwFlags & GAMEVAR_USER_MASK)
4698 terminx 522
            ALIGNED_FREE_AND_NULL(aGameVars[i].val.plValues);
782 terminx 523
    }
1035 terminx 524
 
525
    // if existing is system, they only get to change default value....
1189 terminx 526
    aGameVars[i].lDefault = lValue;
527
    aGameVars[i].dwFlags &= ~GAMEVAR_RESET;
1035 terminx 528
 
1179 terminx 529
    if (i == g_gameVarCount)
782 terminx 530
    {
1035 terminx 531
        // we're adding a new one.
1677 terminx 532
        hash_add(&h_gamevars, aGameVars[i].szLabel, g_gameVarCount++, 0);
782 terminx 533
    }
1035 terminx 534
 
3416 helixhorne 535
    // Set initial values. (Or, override values for system gamevars.)
1143 terminx 536
    if (aGameVars[i].dwFlags & GAMEVAR_PERPLAYER)
1035 terminx 537
    {
1189 terminx 538
        if (!aGameVars[i].val.plValues)
4698 terminx 539
        {
540
            aGameVars[i].val.plValues = (intptr_t *) Xaligned_alloc(PLAYER_VAR_ALIGNMENT, MAXPLAYERS * sizeof(intptr_t));
541
            Bmemset(aGameVars[i].val.plValues, 0, MAXPLAYERS * sizeof(intptr_t));
542
        }
1229 terminx 543
        for (j=MAXPLAYERS-1; j>=0; j--)
1189 terminx 544
            aGameVars[i].val.plValues[j]=lValue;
1035 terminx 545
    }
1143 terminx 546
    else if (aGameVars[i].dwFlags & GAMEVAR_PERACTOR)
1035 terminx 547
    {
1189 terminx 548
        if (!aGameVars[i].val.plValues)
4698 terminx 549
        {
550
            aGameVars[i].val.plValues = (intptr_t *) Xaligned_alloc(ACTOR_VAR_ALIGNMENT, MAXSPRITES * sizeof(intptr_t));
551
            Bmemset(aGameVars[i].val.plValues, 0, MAXSPRITES * sizeof(intptr_t));
552
        }
1229 terminx 553
        for (j=MAXSPRITES-1; j>=0; j--)
1189 terminx 554
            aGameVars[i].val.plValues[j]=lValue;
1035 terminx 555
    }
1189 terminx 556
    else aGameVars[i].val.lValue = lValue;
557
 
1035 terminx 558
    return 1;
398 terminx 559
}
560
 
1205 terminx 561
static int32_t Gv_GetVarIndex(const char *szGameLabel)
398 terminx 562
{
1625 terminx 563
    int32_t i = hash_find(&h_gamevars,szGameLabel);
4680 terminx 564
 
565
    if (EDUKE32_PREDICT_FALSE(i == -1))
1185 terminx 566
    {
3343 helixhorne 567
        OSD_Printf(OSD_ERROR "Gv_GetVarIndex(): INTERNAL ERROR: couldn't find gamevar %s!\n",szGameLabel);
1185 terminx 568
        return 0;
569
    }
4680 terminx 570
 
1185 terminx 571
    return i;
398 terminx 572
}
573
 
4631 terminx 574
int32_t __fastcall Gv_GetVar(int32_t id, int32_t iActor, int32_t iPlayer)
398 terminx 575
{
1440 terminx 576
    if (id == g_iThisActorID)
577
        return iActor;
578
 
782 terminx 579
    if (id == MAXGAMEVARS)
4895 terminx 580
        return *insptr++;
715 terminx 581
 
4895 terminx 582
    int negateResult = !!(id & (MAXGAMEVARS << 1));
4680 terminx 583
 
584
    if (EDUKE32_PREDICT_FALSE(id >= g_gameVarCount && !negateResult))
585
        goto nastyhacks;
586
 
4895 terminx 587
    id &= (MAXGAMEVARS - 1);
4680 terminx 588
 
4895 terminx 589
    int rv, f;
590
    f = aGameVars[id].dwFlags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK);
591
 
592
    if (!f) rv = (aGameVars[id].val.lValue ^ -negateResult) + negateResult;
593
    else if (f == GAMEVAR_PERPLAYER)
782 terminx 594
    {
4680 terminx 595
        if (EDUKE32_PREDICT_FALSE((unsigned) iPlayer >= MAXPLAYERS)) goto bad_id;
4895 terminx 596
        rv = (aGameVars[id].val.plValues[iPlayer] ^ -negateResult) + negateResult;
597
    }
598
    else if (f == GAMEVAR_PERACTOR)
599
    {
4680 terminx 600
        if (EDUKE32_PREDICT_FALSE((unsigned) iActor >= MAXSPRITES)) goto bad_id;
4895 terminx 601
        rv = (aGameVars[id].val.plValues[iActor] ^ -negateResult) + negateResult;
4680 terminx 602
    }
4895 terminx 603
    else switch (f)
604
    {
605
        case GAMEVAR_INTPTR: rv = ((*((int32_t *)aGameVars[id].val.lValue)) ^ -negateResult) + negateResult; break;
606
        case GAMEVAR_SHORTPTR: rv = ((*((int16_t *)aGameVars[id].val.lValue)) ^ -negateResult) + negateResult; break;
607
        case GAMEVAR_CHARPTR: rv = ((*((char *)aGameVars[id].val.lValue)) ^ -negateResult) + negateResult; break;
608
    }
961 terminx 609
 
4895 terminx 610
    return rv;
611
 
4680 terminx 612
nastyhacks:
4895 terminx 613
    if (id & (MAXGAMEVARS << 2))  // array
4680 terminx 614
    {
4895 terminx 615
        id &= (MAXGAMEVARS - 1);  // ~((MAXGAMEVARS<<2)|(MAXGAMEVARS<<1));
715 terminx 616
 
4895 terminx 617
        int32_t index = Gv_GetVar(*insptr++, iActor, iPlayer);
1179 terminx 618
 
4895 terminx 619
        if (EDUKE32_PREDICT_FALSE((unsigned)index >= (unsigned)aGameArrays[id].size))
4680 terminx 620
        {
621
            iActor = index;
622
            goto badindex;
623
        }
1440 terminx 624
 
4680 terminx 625
        return ((aGameArrays[id].plValues[index] ^ -negateResult) + negateResult);
626
    }
961 terminx 627
 
4680 terminx 628
    if (id&(MAXGAMEVARS<<3)) // struct shortcut vars
629
    {
630
        int32_t index=Gv_GetVar(*insptr++, iActor, iPlayer);
1204 terminx 631
 
4680 terminx 632
        switch ((id&(MAXGAMEVARS-1)) - g_iSpriteVarID)
633
        {
634
        case 0: //if (id == g_iSpriteVarID)
635
        {
636
            int32_t parm2 = 0, label = *insptr++;
1204 terminx 637
 
4680 terminx 638
            /*OSD_Printf("%d %d %d\n",__LINE__,index,label);*/
639
            if (EDUKE32_PREDICT_FALSE(ActorLabels[label].flags & LABEL_HASPARM2))
640
                parm2 = Gv_GetVar(*insptr++, iActor, iPlayer);
1204 terminx 641
 
4680 terminx 642
            if (EDUKE32_PREDICT_FALSE((unsigned) index >= MAXSPRITES))
643
            {
644
                iPlayer = index;
645
                goto badsprite;
646
            }
2016 terminx 647
 
4680 terminx 648
            return ((VM_AccessSpriteX(index, label, parm2) ^ -negateResult) + negateResult);
649
        }
650
        case 3: //else if (id == g_iPlayerVarID)
651
        {
652
            int32_t parm2 = 0, label = *insptr++;
1204 terminx 653
 
4680 terminx 654
            if (EDUKE32_PREDICT_FALSE(PlayerLabels[label].flags & LABEL_HASPARM2))
655
                parm2 = Gv_GetVar(*insptr++, iActor, iPlayer);
1204 terminx 656
 
4680 terminx 657
            if (index == vm.g_i) index = vm.g_p;
2016 terminx 658
 
4680 terminx 659
            if (EDUKE32_PREDICT_FALSE((unsigned) index >= MAXPLAYERS))
660
            {
661
                iPlayer = index;
662
                goto badplayer;
1204 terminx 663
            }
664
 
4680 terminx 665
            return ((VM_AccessPlayerX(index, label, parm2) ^ -negateResult) + negateResult);
782 terminx 666
        }
4680 terminx 667
        case 4: //else if (id == g_iActorVarID)
668
            return ((Gv_GetVar(*insptr++, index, iPlayer) ^ -negateResult) + negateResult);
669
        case 1: //else if (id == g_iSectorVarID)
670
            if (index == vm.g_i) index = sprite[vm.g_i].sectnum;
671
            if (EDUKE32_PREDICT_FALSE((unsigned) index >= MAXSECTORS))
672
            {
673
                iPlayer = index;
674
                insptr++;
675
                goto badsector;
676
            }
677
            return ((VM_AccessSectorX(index, *insptr++) ^ -negateResult) + negateResult);
678
        case 2: //else if (id == g_iWallVarID)
679
            if (EDUKE32_PREDICT_FALSE((unsigned) index >= MAXWALLS))
680
            {
681
                iPlayer = index;
682
                insptr++;
683
                goto badwall;
684
            }
685
            return ((VM_AccessWallX(index, *insptr++) ^ -negateResult) + negateResult);
1189 terminx 686
        default:
4680 terminx 687
            EDUKE32_UNREACHABLE_SECTION(return -1);
782 terminx 688
        }
689
    }
1857 terminx 690
 
4680 terminx 691
    CON_ERRPRINTF("Gv_GetVar(): invalid gamevar ID (%d)\n", id);
692
    return -1;
693
 
1440 terminx 694
bad_id:
3000 helixhorne 695
    CON_ERRPRINTF("Gv_GetVar(): invalid sprite/player ID %d/%d\n", iActor,iPlayer);
1440 terminx 696
    return -1;
1857 terminx 697
 
698
 
699
badindex:
3000 helixhorne 700
    CON_ERRPRINTF("Gv_GetVar(): invalid array index (%s[%d])\n", aGameArrays[id].szLabel,iActor);
1857 terminx 701
    return -1;
702
 
2016 terminx 703
badplayer:
3000 helixhorne 704
    CON_ERRPRINTF("Gv_GetVar(): invalid player ID %d\n", iPlayer);
2016 terminx 705
    return -1;
706
 
707
badsprite:
3000 helixhorne 708
    CON_ERRPRINTF("Gv_GetVar(): invalid sprite ID %d\n", iPlayer);
2016 terminx 709
    return -1;
710
 
2381 helixhorne 711
badsector:
3000 helixhorne 712
    CON_ERRPRINTF("Gv_GetVar(): invalid sector ID %d\n", iPlayer);
2381 helixhorne 713
    return -1;
714
 
715
badwall:
3000 helixhorne 716
    CON_ERRPRINTF("Gv_GetVar(): invalid wall ID %d\n", iPlayer);
2381 helixhorne 717
    return -1;
398 terminx 718
}
1179 terminx 719
 
4631 terminx 720
void __fastcall Gv_SetVar(int32_t id, int32_t lValue, int32_t iActor, int32_t iPlayer)
398 terminx 721
{
4680 terminx 722
    if (EDUKE32_PREDICT_FALSE((unsigned)id >= (unsigned)g_gameVarCount)) goto badvarid;
1440 terminx 723
 
4895 terminx 724
    int f;
725
    f = aGameVars[id].dwFlags & (GAMEVAR_USER_MASK|GAMEVAR_PTR_MASK);
1189 terminx 726
 
4895 terminx 727
    if (!f) aGameVars[id].val.lValue=lValue;
728
    else if (f == GAMEVAR_PERPLAYER)
782 terminx 729
    {
4895 terminx 730
        if (EDUKE32_PREDICT_FALSE((unsigned) iPlayer > MAXPLAYERS-1)) goto badindex;
782 terminx 731
        // for the current player
1189 terminx 732
        aGameVars[id].val.plValues[iPlayer]=lValue;
4895 terminx 733
    }
734
    else if (f == GAMEVAR_PERACTOR)
735
    {
736
        if (EDUKE32_PREDICT_FALSE((unsigned) iActor > MAXSPRITES-1)) goto badindex;
1189 terminx 737
        aGameVars[id].val.plValues[iActor]=lValue;
1143 terminx 738
    }
4895 terminx 739
    else
740
    {
741
        switch (f)
742
        {
743
            case GAMEVAR_INTPTR: *((int32_t *)aGameVars[id].val.lValue) = (int32_t)lValue; break;
744
            case GAMEVAR_SHORTPTR: *((int16_t *)aGameVars[id].val.lValue) = (int16_t)lValue; break;
745
            case GAMEVAR_CHARPTR: *((uint8_t *)aGameVars[id].val.lValue) = (uint8_t)lValue; break;
746
        }
747
    }
748
    return;
1440 terminx 749
 
750
badvarid:
3000 helixhorne 751
    CON_ERRPRINTF("Gv_SetVar(): invalid gamevar (%d) from sprite %d (%d), player %d\n",
3238 hendricks2 752
                  id,vm.g_i,TrackerCast(sprite[vm.g_i].picnum),vm.g_p);
1440 terminx 753
    return;
754
 
1820 terminx 755
badindex:
3000 helixhorne 756
    CON_ERRPRINTF("Gv_SetVar(): invalid index (%d) for gamevar %s from sprite %d, player %d\n",
1820 terminx 757
               aGameVars[id].dwFlags & GAMEVAR_PERACTOR ? iActor : iPlayer,
758
               aGameVars[id].szLabel,vm.g_i,vm.g_p);
1189 terminx 759
}
1143 terminx 760
 
4631 terminx 761
enum {
762
    GVX_BADVARID = 0,
763
    GVX_BADPLAYER,
764
    GVX_BADSPRITE,
765
    GVX_BADSECTOR,
766
    GVX_BADWALL,
767
    GVX_BADINDEX,
4707 terminx 768
};
4631 terminx 769
 
4895 terminx 770
static const char *gvxerrs[] = {
771
    "Gv_GetVarX(): invalid gamevar ID",
772
    "Gv_GetVarX(): invalid player ID",
773
    "Gv_GetVarX(): invalid sprite ID",
774
    "Gv_GetVarX(): invalid sector ID",
775
    "Gv_GetVarX(): invalid wall ID",
776
    "Gv_GetVarX(): invalid array index",
4631 terminx 777
};
778
 
779
int32_t __fastcall Gv_GetVarX(int32_t id)
1189 terminx 780
{
1440 terminx 781
    if (id == g_iThisActorID)
782
        return vm.g_i;
783
 
1189 terminx 784
    if (id == MAXGAMEVARS)
4895 terminx 785
        return *insptr++;
1189 terminx 786
 
4895 terminx 787
    int negateResult = !!(id & (MAXGAMEVARS << 1)), rv = -1, f;
788
 
4680 terminx 789
    if (EDUKE32_PREDICT_FALSE(id >= g_gameVarCount && negateResult == 0))
4631 terminx 790
        goto nastyhacks;
791
 
792
    id &= MAXGAMEVARS-1;
793
 
4895 terminx 794
    f = aGameVars[id].dwFlags & (GAMEVAR_USER_MASK|GAMEVAR_PTR_MASK);
795
 
796
    if (!f) rv = (aGameVars[id].val.lValue ^ -negateResult) + negateResult;
797
    else if (f == GAMEVAR_PERPLAYER)
1143 terminx 798
    {
4680 terminx 799
        if (EDUKE32_PREDICT_FALSE((unsigned) vm.g_p >= MAXPLAYERS))
4895 terminx 800
            goto perr;
801
        rv = (aGameVars[id].val.plValues[vm.g_p] ^ -negateResult) + negateResult;
4631 terminx 802
    }
4895 terminx 803
    else if (f == GAMEVAR_PERACTOR)
804
        rv = (aGameVars[id].val.plValues[vm.g_i] ^ -negateResult) + negateResult;
805
    else switch (f)
806
    {
807
        case GAMEVAR_INTPTR:
808
            rv = ((*((int32_t *) aGameVars[id].val.lValue)) ^ -negateResult) + negateResult; break;
809
        case GAMEVAR_SHORTPTR:
810
            rv = ((*((int16_t *) aGameVars[id].val.lValue)) ^ -negateResult) + negateResult; break;
811
        case GAMEVAR_CHARPTR:
812
            rv = ((*((uint8_t *) aGameVars[id].val.lValue)) ^ -negateResult) + negateResult; break;
813
    }
1189 terminx 814
 
4895 terminx 815
    return rv;
816
 
817
perr:
818
    id = vm.g_p;
819
    CON_ERRPRINTF("%s %d\n", gvxerrs[GVX_BADPLAYER], id);
820
    return -1;
821
 
4631 terminx 822
nastyhacks:
4895 terminx 823
    if (id & (MAXGAMEVARS << 2))  // array
4631 terminx 824
    {
4895 terminx 825
        int32_t index = Gv_GetVarX(*insptr++);
1189 terminx 826
 
4895 terminx 827
        id &= (MAXGAMEVARS - 1);  // ~((MAXGAMEVARS<<2)|(MAXGAMEVARS<<1));
3274 hendricks2 828
 
4895 terminx 829
        int siz = (aGameArrays[id].dwFlags & GAMEARRAY_VARSIZE) ? Gv_GetVarX(aGameArrays[id].size) : aGameArrays[id].size;
1857 terminx 830
 
4895 terminx 831
        if (EDUKE32_PREDICT_FALSE((unsigned) index >= (unsigned) siz))
4631 terminx 832
        {
833
            negateResult = index;
4839 helixhorne 834
            CON_ERRPRINTF("%s %s[%d]\n", gvxerrs[GVX_BADINDEX], aGameArrays[id].szLabel, index);
4631 terminx 835
            return -1;
836
        }
1189 terminx 837
 
4631 terminx 838
        switch (aGameArrays[id].dwFlags & GAMEARRAY_TYPE_MASK)
839
        {
4895 terminx 840
            case 0: return ((aGameArrays[id].plValues)[index] ^ -negateResult) + negateResult;
841
            case GAMEARRAY_OFINT: return (((int32_t *) aGameArrays[id].plValues)[index] ^ -negateResult) + negateResult;
842
            case GAMEARRAY_OFSHORT:
843
                return (((int16_t *) aGameArrays[id].plValues)[index] ^ -negateResult) + negateResult;
844
            case GAMEARRAY_OFCHAR: return (((uint8_t *) aGameArrays[id].plValues)[index] ^ -negateResult) + negateResult;
845
            default: EDUKE32_UNREACHABLE_SECTION(return -1);
4631 terminx 846
        }
847
    }
1204 terminx 848
 
4631 terminx 849
    if (id&(MAXGAMEVARS<<3)) // struct shortcut vars
850
    {
851
        int32_t index=Gv_GetVarX(*insptr++);
1204 terminx 852
 
4631 terminx 853
        switch ((id&(MAXGAMEVARS-1)) - g_iSpriteVarID)
854
        {
855
        case 0: //if (id == g_iSpriteVarID)
856
        {
857
            int32_t parm2 = 0, label = *insptr++;
1215 terminx 858
 
4631 terminx 859
            /*OSD_Printf("%d %d %d\n",__LINE__,index,label);*/
4680 terminx 860
            if (EDUKE32_PREDICT_FALSE(ActorLabels[label].flags & LABEL_HASPARM2))
4631 terminx 861
                parm2 = Gv_GetVarX(*insptr++);
2016 terminx 862
 
4680 terminx 863
            if (EDUKE32_PREDICT_FALSE((unsigned) index >= MAXSPRITES))
4631 terminx 864
            {
865
                id = index;
4633 terminx 866
                CON_ERRPRINTF("%s %d\n", gvxerrs[GVX_BADSPRITE], id);
4631 terminx 867
                return -1;
868
            }
1204 terminx 869
 
4631 terminx 870
            return ((VM_AccessSpriteX(index, label, parm2) ^ -negateResult) + negateResult);
871
        }
872
        case 3: //else if (id == g_iPlayerVarID)
873
        {
874
            int32_t parm2 = 0, label = *insptr++;
1204 terminx 875
 
4680 terminx 876
            if (EDUKE32_PREDICT_FALSE(PlayerLabels[label].flags & LABEL_HASPARM2))
4631 terminx 877
                parm2 = Gv_GetVarX(*insptr++);
2016 terminx 878
 
4631 terminx 879
            if (index == vm.g_i) index = vm.g_p;
880
 
4680 terminx 881
            if (EDUKE32_PREDICT_FALSE((unsigned) index >= MAXPLAYERS))
4631 terminx 882
            {
883
                id = index;
4633 terminx 884
                CON_ERRPRINTF("%s %d\n", gvxerrs[GVX_BADPLAYER], id);
4631 terminx 885
                return -1;
1204 terminx 886
            }
4631 terminx 887
            return ((VM_AccessPlayerX(index, label, parm2) ^ -negateResult) + negateResult);
1189 terminx 888
        }
4631 terminx 889
        case 4: //else if (id == g_iActorVarID)
890
            return ((Gv_GetVar(*insptr++, index, vm.g_p) ^ -negateResult) + negateResult);
891
        case 1: //else if (id == g_iSectorVarID)
892
            if (index == vm.g_i) index = sprite[vm.g_i].sectnum;
4680 terminx 893
            if (EDUKE32_PREDICT_FALSE((unsigned) index >= MAXSECTORS))
2102 helixhorne 894
            {
4631 terminx 895
                id = index;
896
                insptr++;
4633 terminx 897
                CON_ERRPRINTF("%s %d\n", gvxerrs[GVX_BADSECTOR], id);
4631 terminx 898
                return -1;
2102 helixhorne 899
            }
4631 terminx 900
            return ((VM_AccessSectorX(index, *insptr++) ^ -negateResult) + negateResult);
901
        case 2: //else if (id == g_iWallVarID)
4680 terminx 902
            if (EDUKE32_PREDICT_FALSE((unsigned) index >= MAXWALLS))
4631 terminx 903
            {
904
                id = index;
905
                insptr++;
4633 terminx 906
                CON_ERRPRINTF("%s %d\n", gvxerrs[GVX_BADWALL], id);
4631 terminx 907
                return -1;
908
            }
909
            return ((VM_AccessWallX(index, *insptr++) ^ -negateResult) + negateResult);
910
        default:
4680 terminx 911
            EDUKE32_UNREACHABLE_SECTION(return -1);
1189 terminx 912
        }
4631 terminx 913
    }
1857 terminx 914
 
4680 terminx 915
    EDUKE32_UNREACHABLE_SECTION(return -1);
1189 terminx 916
}
502 terminx 917
 
4631 terminx 918
void __fastcall Gv_SetVarX(int32_t id, int32_t lValue)
1189 terminx 919
{
4895 terminx 920
    int f = aGameVars[id].dwFlags & (GAMEVAR_USER_MASK|GAMEVAR_PTR_MASK);
921
 
922
    if (!f) aGameVars[id].val.lValue = lValue;
923
    else if (f == GAMEVAR_PERPLAYER)
782 terminx 924
    {
4895 terminx 925
        if (EDUKE32_PREDICT_FALSE((unsigned)vm.g_p >= MAXPLAYERS)) goto badindex;
926
        aGameVars[id].val.plValues[vm.g_p] = lValue;
782 terminx 927
    }
4895 terminx 928
    else if (f == GAMEVAR_PERACTOR)
929
    {
930
        if (EDUKE32_PREDICT_FALSE((unsigned)vm.g_i >= MAXSPRITES)) goto badindex;
931
        aGameVars[id].val.plValues[vm.g_i] = lValue;
932
    }
933
    else
934
    {
935
        switch (f)
936
        {
937
            case GAMEVAR_INTPTR: *((int32_t *)aGameVars[id].val.lValue) = (int32_t)lValue; break;
938
            case GAMEVAR_SHORTPTR: *((int16_t *)aGameVars[id].val.lValue) = (int16_t)lValue; break;
939
            case GAMEVAR_CHARPTR: *((uint8_t *)aGameVars[id].val.lValue) = (uint8_t)lValue; break;
940
        }
941
    }
1657 terminx 942
 
4895 terminx 943
    return;
944
 
1820 terminx 945
badindex:
3000 helixhorne 946
    CON_ERRPRINTF("Gv_SetVar(): invalid index (%d) for gamevar %s\n",
1820 terminx 947
               aGameVars[id].dwFlags & GAMEVAR_PERACTOR ? vm.g_i : vm.g_p,
948
               aGameVars[id].szLabel);
398 terminx 949
}
950
 
1205 terminx 951
int32_t Gv_GetVarByLabel(const char *szGameLabel, int32_t lDefault, int32_t iActor, int32_t iPlayer)
398 terminx 952
{
1625 terminx 953
    int32_t i = hash_find(&h_gamevars,szGameLabel);
1179 terminx 954
 
4680 terminx 955
    return EDUKE32_PREDICT_FALSE(i < 0) ? lDefault : Gv_GetVar(i, iActor, iPlayer);
398 terminx 956
}
957
 
1143 terminx 958
static intptr_t *Gv_GetVarDataPtr(const char *szGameLabel)
398 terminx 959
{
1625 terminx 960
    int32_t i = hash_find(&h_gamevars,szGameLabel);
715 terminx 961
 
4680 terminx 962
    if (EDUKE32_PREDICT_FALSE(i < 0))
1179 terminx 963
        return NULL;
1024 terminx 964
 
1143 terminx 965
    if (aGameVars[i].dwFlags & (GAMEVAR_PERACTOR | GAMEVAR_PERPLAYER))
782 terminx 966
    {
4680 terminx 967
        if (EDUKE32_PREDICT_FALSE(!aGameVars[i].val.plValues))
3000 helixhorne 968
            CON_ERRPRINTF("Gv_GetVarDataPtr(): INTERNAL ERROR: NULL array !!!\n");
1189 terminx 969
        return aGameVars[i].val.plValues;
782 terminx 970
    }
1179 terminx 971
 
1189 terminx 972
    return &(aGameVars[i].val.lValue);
398 terminx 973
}
3789 helixhorne 974
#endif  // !defined LUNATIC
398 terminx 975
 
1143 terminx 976
void Gv_ResetSystemDefaults(void)
398 terminx 977
{
782 terminx 978
    // call many times...
3459 helixhorne 979
#if !defined LUNATIC
3328 helixhorne 980
    int32_t i;
981
    int32_t j;
782 terminx 982
    char aszBuf[64];
398 terminx 983
 
782 terminx 984
    //AddLog("ResetWeaponDefaults");
398 terminx 985
 
1229 terminx 986
    for (j=MAXPLAYERS-1; j>=0; j--)
782 terminx 987
    {
1229 terminx 988
        for (i=MAX_WEAPONS-1; i>=0; i--)
782 terminx 989
        {
990
            Bsprintf(aszBuf,"WEAPON%d_CLIP",i);
1143 terminx 991
            aplWeaponClip[i][j]=Gv_GetVarByLabel(aszBuf,0, -1, j);
782 terminx 992
            Bsprintf(aszBuf,"WEAPON%d_RELOAD",i);
1143 terminx 993
            aplWeaponReload[i][j]=Gv_GetVarByLabel(aszBuf,0, -1, j);
782 terminx 994
            Bsprintf(aszBuf,"WEAPON%d_FIREDELAY",i);
1143 terminx 995
            aplWeaponFireDelay[i][j]=Gv_GetVarByLabel(aszBuf,0, -1, j);
782 terminx 996
            Bsprintf(aszBuf,"WEAPON%d_TOTALTIME",i);
1143 terminx 997
            aplWeaponTotalTime[i][j]=Gv_GetVarByLabel(aszBuf,0, -1, j);
782 terminx 998
            Bsprintf(aszBuf,"WEAPON%d_HOLDDELAY",i);
1143 terminx 999
            aplWeaponHoldDelay[i][j]=Gv_GetVarByLabel(aszBuf,0, -1, j);
782 terminx 1000
            Bsprintf(aszBuf,"WEAPON%d_FLAGS",i);
1143 terminx 1001
            aplWeaponFlags[i][j]=Gv_GetVarByLabel(aszBuf,0, -1, j);
782 terminx 1002
            Bsprintf(aszBuf,"WEAPON%d_SHOOTS",i);
1143 terminx 1003
            aplWeaponShoots[i][j]=Gv_GetVarByLabel(aszBuf,0, -1, j);
2268 helixhorne 1004
            if ((unsigned)aplWeaponShoots[i][j] >= MAXTILES)
1005
                aplWeaponShoots[i][j] = 0;
782 terminx 1006
            Bsprintf(aszBuf,"WEAPON%d_SPAWNTIME",i);
1143 terminx 1007
            aplWeaponSpawnTime[i][j]=Gv_GetVarByLabel(aszBuf,0, -1, j);
782 terminx 1008
            Bsprintf(aszBuf,"WEAPON%d_SPAWN",i);
1143 terminx 1009
            aplWeaponSpawn[i][j]=Gv_GetVarByLabel(aszBuf,0, -1, j);
782 terminx 1010
            Bsprintf(aszBuf,"WEAPON%d_SHOTSPERBURST",i);
1143 terminx 1011
            aplWeaponShotsPerBurst[i][j]=Gv_GetVarByLabel(aszBuf,0, -1, j);
782 terminx 1012
            Bsprintf(aszBuf,"WEAPON%d_WORKSLIKE",i);
1143 terminx 1013
            aplWeaponWorksLike[i][j]=Gv_GetVarByLabel(aszBuf,0, -1, j);
782 terminx 1014
            Bsprintf(aszBuf,"WEAPON%d_INITIALSOUND",i);
1143 terminx 1015
            aplWeaponInitialSound[i][j]=Gv_GetVarByLabel(aszBuf,0, -1, j);
782 terminx 1016
            Bsprintf(aszBuf,"WEAPON%d_FIRESOUND",i);
1143 terminx 1017
            aplWeaponFireSound[i][j]=Gv_GetVarByLabel(aszBuf,0, -1, j);
782 terminx 1018
            Bsprintf(aszBuf,"WEAPON%d_SOUND2TIME",i);
1143 terminx 1019
            aplWeaponSound2Time[i][j]=Gv_GetVarByLabel(aszBuf,0, -1, j);
782 terminx 1020
            Bsprintf(aszBuf,"WEAPON%d_SOUND2SOUND",i);
1143 terminx 1021
            aplWeaponSound2Sound[i][j]=Gv_GetVarByLabel(aszBuf,0, -1, j);
782 terminx 1022
            Bsprintf(aszBuf,"WEAPON%d_RELOADSOUND1",i);
1143 terminx 1023
            aplWeaponReloadSound1[i][j]=Gv_GetVarByLabel(aszBuf,0, -1, j);
782 terminx 1024
            Bsprintf(aszBuf,"WEAPON%d_RELOADSOUND2",i);
1143 terminx 1025
            aplWeaponReloadSound2[i][j]=Gv_GetVarByLabel(aszBuf,0, -1, j);
1026
            Bsprintf(aszBuf,"WEAPON%d_SELECTSOUND",i);
1027
            aplWeaponSelectSound[i][j]=Gv_GetVarByLabel(aszBuf,0, -1, j);
1315 terminx 1028
            Bsprintf(aszBuf,"WEAPON%d_FLASHCOLOR",i);
1029
            aplWeaponFlashColor[i][j]=Gv_GetVarByLabel(aszBuf,0, -1, j);
782 terminx 1030
        }
1031
    }
3415 helixhorne 1032
 
1143 terminx 1033
    g_iReturnVarID=Gv_GetVarIndex("RETURN");
1034
    g_iWeaponVarID=Gv_GetVarIndex("WEAPON");
1035
    g_iWorksLikeVarID=Gv_GetVarIndex("WORKSLIKE");
1036
    g_iZRangeVarID=Gv_GetVarIndex("ZRANGE");
1037
    g_iAngRangeVarID=Gv_GetVarIndex("ANGRANGE");
1038
    g_iAimAngleVarID=Gv_GetVarIndex("AUTOAIMANGLE");
1039
    g_iLoTagID=Gv_GetVarIndex("LOTAG");
1040
    g_iHiTagID=Gv_GetVarIndex("HITAG");
1041
    g_iTextureID=Gv_GetVarIndex("TEXTURE");
1042
    g_iThisActorID=Gv_GetVarIndex("THISACTOR");
398 terminx 1043
 
1204 terminx 1044
    g_iSpriteVarID=Gv_GetVarIndex("sprite");
1045
    g_iSectorVarID=Gv_GetVarIndex("sector");
1046
    g_iWallVarID=Gv_GetVarIndex("wall");
1047
    g_iPlayerVarID=Gv_GetVarIndex("player");
1048
    g_iActorVarID=Gv_GetVarIndex("actorvar");
3343 helixhorne 1049
#endif
1204 terminx 1050
 
4895 terminx 1051
    for (i = 0; i <= MAXTILES - 1; i++)
1052
        Bmemcpy(&ProjectileData[i], &g_tile[i].defproj, sizeof(projectile_t));
1053
 
4710 helixhorne 1054
#ifndef LUNATIC
4631 terminx 1055
    // hackhackhackhackhack
4895 terminx 1056
    if (i = hash_find(&h_arrays, "tilesizx"), i >= 0)
4631 terminx 1057
    {
4895 terminx 1058
        for (int j = 0; j < MAXTILES; j++)
4631 terminx 1059
            aGameArrays[i].plValues[j] = tilesiz[j].x;
1060
    }
1061
 
4895 terminx 1062
    if (i = hash_find(&h_arrays, "tilesizy"), i >= 0)
4631 terminx 1063
    {
4895 terminx 1064
        for (int j = 0; j < MAXTILES; j++)
4631 terminx 1065
            aGameArrays[i].plValues[j] = tilesiz[j].y;
1066
    }
4710 helixhorne 1067
#endif
4631 terminx 1068
 
782 terminx 1069
    //AddLog("EOF:ResetWeaponDefaults");
398 terminx 1070
}
1071
 
3848 helixhorne 1072
// Will set members that were overridden at CON translation time to 1.
1073
// For example, if
1074
//   gamevar WEAPON1_SHOOTS 2200 GAMEVAR_PERPLAYER
1075
// was specified at file scope, g_weaponOverridden[1].Shoots will be 1.
1076
weapondata_t g_weaponOverridden[MAX_WEAPONS];
1077
 
3327 helixhorne 1078
static weapondata_t weapondefaults[MAX_WEAPONS] = {
1079
    /*
1080
        WorksLike, Clip, Reload, FireDelay, TotalTime, HoldDelay,
1081
        Flags,
1082
        Shoots, SpawnTime, Spawn, ShotsPerBurst, InitialSound, FireSound, Sound2Time, Sound2Sound,
1083
        ReloadSound1, ReloadSound2, SelectSound, FlashColor
1084
    */
398 terminx 1085
 
3327 helixhorne 1086
    {
1087
        KNEE_WEAPON, 0, 0, 7, 14, 0,
1088
        WEAPON_NOVISIBLE | WEAPON_RANDOMRESTART | WEAPON_AUTOMATIC,
1089
        KNEE__STATIC, 0, 0, 0, 0, 0, 0,
3834 hendricks2 1090
        0, EJECT_CLIP__STATIC, INSERT_CLIP__STATIC, 0, 0
3327 helixhorne 1091
    },
398 terminx 1092
 
3327 helixhorne 1093
    {
1094
        PISTOL_WEAPON, /*NAM?20:*/12, /*NAM?50:*/27, 2, 5, 0,
3375 helixhorne 1095
        /*(NAM?WEAPON_HOLSTER_CLEARS_CLIP:0) |*/ WEAPON_RELOAD_TIMING,
3834 hendricks2 1096
        SHOTSPARK1__STATIC, 2, SHELL__STATIC, 0, 0, PISTOL_FIRE__STATIC, 0, 0,
1097
        EJECT_CLIP__STATIC, INSERT_CLIP__STATIC, INSERT_CLIP__STATIC, 255+(95<<8)
3327 helixhorne 1098
    },
398 terminx 1099
 
3327 helixhorne 1100
    {
1101
        SHOTGUN_WEAPON, 0, 13, 4, 30, 0,
1102
        WEAPON_CHECKATRELOAD,
3834 hendricks2 1103
        SHOTGUN__STATIC, 24, SHOTGUNSHELL__STATIC, 7, 0, SHOTGUN_FIRE__STATIC, 15, SHOTGUN_COCK__STATIC,
1104
        EJECT_CLIP__STATIC, INSERT_CLIP__STATIC, SHOTGUN_COCK__STATIC, 255+(95<<8)
3327 helixhorne 1105
    },
1315 terminx 1106
 
3327 helixhorne 1107
    {
1108
        CHAINGUN_WEAPON, 0, 0, 3, 12, 3,
1109
        WEAPON_AUTOMATIC | WEAPON_FIREEVERYTHIRD | WEAPON_AMMOPERSHOT | WEAPON_SPAWNTYPE3 | WEAPON_RESET,
3834 hendricks2 1110
        CHAINGUN__STATIC, 1, SHELL__STATIC, 0, 0, CHAINGUN_FIRE__STATIC, 0, 0,
1111
        EJECT_CLIP__STATIC, INSERT_CLIP__STATIC, SELECT_WEAPON__STATIC, 255+(95<<8)
3327 helixhorne 1112
    },
398 terminx 1113
 
3327 helixhorne 1114
    {
1115
        RPG_WEAPON, 0, 0, 4, 20, 0,
1116
        0,
1117
        RPG__STATIC, 0, 0, 0, 0, 0, 0, 0,
3834 hendricks2 1118
        EJECT_CLIP__STATIC, INSERT_CLIP__STATIC, SELECT_WEAPON__STATIC, 255+(95<<8)
3327 helixhorne 1119
    },
398 terminx 1120
 
3327 helixhorne 1121
    {
1122
        HANDBOMB_WEAPON, 0, 30, 6, 19, 12,
1123
        WEAPON_THROWIT,
1124
        HEAVYHBOMB__STATIC, 0, 0, 0, 0, 0, 0,
3834 hendricks2 1125
        0, EJECT_CLIP__STATIC, INSERT_CLIP__STATIC, 0, 0
3327 helixhorne 1126
    },
398 terminx 1127
 
3327 helixhorne 1128
    {
1129
        SHRINKER_WEAPON, 0, 0, 10, /*NAM?30:*/12, 0,
1130
        WEAPON_GLOWS,
3834 hendricks2 1131
        SHRINKER__STATIC, 0, 0, 0, SHRINKER_FIRE__STATIC, 0, 0, 0,
1132
        EJECT_CLIP__STATIC, INSERT_CLIP__STATIC, SELECT_WEAPON__STATIC, 128+(255<<8)+(128<<16)
3327 helixhorne 1133
    },
398 terminx 1134
 
3327 helixhorne 1135
    {
1136
        DEVISTATOR_WEAPON, 0, 0, 3, 6, 5,
1137
        WEAPON_FIREEVERYOTHER | WEAPON_AMMOPERSHOT,
3834 hendricks2 1138
        RPG__STATIC, 0, 0, 2, CAT_FIRE__STATIC, 0, 0, 0,
1139
        EJECT_CLIP__STATIC, INSERT_CLIP__STATIC, SELECT_WEAPON__STATIC, 255+(95<<8)
3327 helixhorne 1140
    },
398 terminx 1141
 
3327 helixhorne 1142
    {
1143
        TRIPBOMB_WEAPON, 0, 16, 3, 16, 7,
1144
        WEAPON_NOVISIBLE | WEAPON_STANDSTILL | WEAPON_CHECKATRELOAD,
1145
        HANDHOLDINGLASER__STATIC, 0, 0, 0, 0, 0, 0,
3834 hendricks2 1146
        0, EJECT_CLIP__STATIC, INSERT_CLIP__STATIC, 0, 0
3327 helixhorne 1147
    },
398 terminx 1148
 
3327 helixhorne 1149
    {
1150
        FREEZE_WEAPON, 0, 0, 3, 5, 0,
1151
        WEAPON_RESET,
3834 hendricks2 1152
        FREEZEBLAST__STATIC, 0, 0, 0, CAT_FIRE__STATIC, CAT_FIRE__STATIC, 0, 0,
1153
        EJECT_CLIP__STATIC, INSERT_CLIP__STATIC, SELECT_WEAPON__STATIC, 128+(128<<8)+(255<<16)
3327 helixhorne 1154
    },
398 terminx 1155
 
3327 helixhorne 1156
    {
1157
        HANDREMOTE_WEAPON, 0, 10, 2, 10, 0,
1158
        WEAPON_BOMB_TRIGGER | WEAPON_NOVISIBLE,
1159
        0, 0, 0, 0, 0, 0, 0,
3834 hendricks2 1160
        0, EJECT_CLIP__STATIC, INSERT_CLIP__STATIC, 0, 0
3327 helixhorne 1161
    },
398 terminx 1162
 
3327 helixhorne 1163
    {
1164
        GROW_WEAPON, 0, 0, 3, /*NAM?30:*/5, 0,
1165
        WEAPON_GLOWS,
3834 hendricks2 1166
        GROWSPARK__STATIC, /*NAM?2:*/0, /*NAM?SHELL:*/0, 0, 0, /*NAM?0:*/EXPANDERSHOOT__STATIC, 0, 0,
1167
        EJECT_CLIP__STATIC, INSERT_CLIP__STATIC, SELECT_WEAPON__STATIC, 255+(95<<8)
3327 helixhorne 1168
    },
1169
};
398 terminx 1170
 
3327 helixhorne 1171
// KEEPINSYNC with what is contained above
1172
// XXX: ugly
1173
static int32_t G_StaticToDynamicTile(int32_t tile)
1174
{
1175
    switch (tile)
1176
    {
1177
    case CHAINGUN__STATIC: return CHAINGUN;
1178
    case FREEZEBLAST__STATIC: return FREEZEBLAST;
1179
    case GROWSPARK__STATIC: return GROWSPARK;
1180
    case HANDHOLDINGLASER__STATIC: return HANDHOLDINGLASER;
1181
    case HEAVYHBOMB__STATIC: return HEAVYHBOMB;
1182
    case KNEE__STATIC: return KNEE;
1183
    case RPG__STATIC: return RPG;
1184
    case SHELL__STATIC: return SHELL;
1185
    case SHOTGUNSHELL__STATIC: return SHOTGUNSHELL;
1186
    case SHOTGUN__STATIC: return SHOTGUN;
1187
    case SHOTSPARK1__STATIC: return SHOTSPARK1;
1188
    case SHRINKER__STATIC: return SHRINKER;
1189
    default: return tile;
1190
    }
1191
}
4895 terminx 1192
 
3834 hendricks2 1193
static int32_t G_StaticToDynamicSound(int32_t sound)
1194
{
1195
    switch (sound)
1196
    {
1197
    case CAT_FIRE__STATIC: return CAT_FIRE;
1198
    case CHAINGUN_FIRE__STATIC: return CHAINGUN_FIRE;
1199
    case EJECT_CLIP__STATIC: return EJECT_CLIP;
1200
    case EXPANDERSHOOT__STATIC: return EXPANDERSHOOT;
1201
    case INSERT_CLIP__STATIC: return INSERT_CLIP;
1202
    case PISTOL_FIRE__STATIC: return PISTOL_FIRE;
1203
    case SELECT_WEAPON__STATIC: return SELECT_WEAPON;
1204
    case SHOTGUN_FIRE__STATIC: return SHOTGUN_FIRE;
1205
    case SHOTGUN_COCK__STATIC: return SHOTGUN_COCK;
1206
    case SHRINKER_FIRE__STATIC: return SHRINKER_FIRE;
1207
    default: return sound;
1208
    }
1209
}
398 terminx 1210
 
3848 helixhorne 1211
// Initialize WEAPONx_* gamevars. Since for Lunatic, they reside on the C side,
1212
// they're set directly. In C-CON, a new CON variable is defined together with
1213
// its initial value.
3328 helixhorne 1214
#ifdef LUNATIC
1215
# define ADDWEAPONVAR(Weapidx, Membname) do { \
1216
    int32_t j; \
1217
    for (j=0; j<MAXPLAYERS; j++) \
1218
        g_playerWeapon[j][Weapidx].Membname = weapondefaults[Weapidx].Membname; \
1219
} while (0)
1220
#else
1221
# define ADDWEAPONVAR(Weapidx, Membname) do { \
3327 helixhorne 1222
    Bsprintf(aszBuf, "WEAPON%d_" #Membname, Weapidx); \
1223
    Bstrupr(aszBuf); \
1224
    Gv_NewVar(aszBuf, weapondefaults[Weapidx].Membname, GAMEVAR_PERPLAYER | GAMEVAR_SYSTEM); \
1225
} while (0)
3328 helixhorne 1226
#endif
398 terminx 1227
 
3848 helixhorne 1228
// After CON translation, get not-overridden members from weapondefaults[] back
1229
// into the live arrays! (That is, g_playerWeapon[][] for Lunatic, WEAPONx_*
1230
// gamevars on the CON side in C-CON.)
1231
#ifdef LUNATIC
1232
# define POSTADDWEAPONVAR(Weapidx, Membname) ADDWEAPONVAR(Weapidx, Membname)
1233
#else
1234
// NYI
1235
# define POSTADDWEAPONVAR(Weapidx, Membname) do {} while (0)
1236
#endif
1237
 
1238
// Finish a default weapon member after CON translation. If it was not
1239
// overridden from CON itself (see example at g_weaponOverridden[]), we set
1240
// both the weapondefaults[] entry (probably dead by now) and the live value.
1241
#define FINISH_WEAPON_DEFAULT_X(What, i, Membname) do {  \
1242
    if (!g_weaponOverridden[i].Membname) \
1243
    { \
1244
        weapondefaults[i].Membname = G_StaticToDynamic##What(weapondefaults[i].Membname); \
1245
        POSTADDWEAPONVAR(i, Membname); \
1246
    } \
1247
} while (0)
1248
 
1249
#define FINISH_WEAPON_DEFAULT_TILE(i, Membname) FINISH_WEAPON_DEFAULT_X(Tile, i, Membname)
1250
#define FINISH_WEAPON_DEFAULT_SOUND(i, Membname) FINISH_WEAPON_DEFAULT_X(Sound, i, Membname)
1251
 
1252
// Process the dynamic {tile,sound} mappings after CON has been translated.
1253
// We cannot do this before, because the dynamic maps are not yet set up then.
1254
void Gv_FinalizeWeaponDefaults(void)
1255
{
1256
    int32_t i;
1257
 
1258
    for (i=0; i<MAX_WEAPONS; i++)
1259
    {
1260
        FINISH_WEAPON_DEFAULT_TILE(i, Shoots);
1261
        FINISH_WEAPON_DEFAULT_TILE(i, Spawn);
1262
 
1263
        FINISH_WEAPON_DEFAULT_SOUND(i, InitialSound);
1264
        FINISH_WEAPON_DEFAULT_SOUND(i, FireSound);
1265
        FINISH_WEAPON_DEFAULT_SOUND(i, ReloadSound1);
1266
        FINISH_WEAPON_DEFAULT_SOUND(i, Sound2Sound);
1267
        FINISH_WEAPON_DEFAULT_SOUND(i, ReloadSound2);
1268
        FINISH_WEAPON_DEFAULT_SOUND(i, SelectSound);
1269
    }
1270
}
1271
#undef FINISH_WEAPON_DEFAULT_SOUND
1272
#undef FINISH_WEAPON_DEFAULT_TILE
1273
#undef FINISH_WEAPON_DEFAULT_X
1274
#undef POSTADDWEAPONVAR
1275
 
3961 helixhorne 1276
#if !defined LUNATIC
1277
static int32_t lastvisinc;
1278
#endif
1279
 
3327 helixhorne 1280
static void Gv_AddSystemVars(void)
1281
{
1282
    // only call ONCE
3328 helixhorne 1283
#if !defined LUNATIC
3327 helixhorne 1284
    char aszBuf[64];
3328 helixhorne 1285
#endif
3327 helixhorne 1286
    int32_t i;
1287
 
1288
    if (NAM)
1289
    {
1290
        weapondefaults[PISTOL_WEAPON].Clip = 20;
1291
        weapondefaults[PISTOL_WEAPON].Reload = 50;
3367 hendricks2 1292
        weapondefaults[PISTOL_WEAPON].Flags |= WEAPON_HOLSTER_CLEARS_CLIP;
3327 helixhorne 1293
 
1294
        weapondefaults[SHRINKER_WEAPON].TotalTime = 30;
1295
 
1296
        weapondefaults[GROW_WEAPON].TotalTime = 30;
1297
        weapondefaults[GROW_WEAPON].SpawnTime = 2;
1298
        weapondefaults[GROW_WEAPON].Spawn = SHELL;
1299
        weapondefaults[GROW_WEAPON].FireSound = 0;
1300
    }
1301
 
1302
    for (i=0; i<MAX_WEAPONS; i++)
1303
    {
1304
        ADDWEAPONVAR(i, WorksLike);
1305
        ADDWEAPONVAR(i, Clip);
1306
        ADDWEAPONVAR(i, Reload);
1307
        ADDWEAPONVAR(i, FireDelay);
1308
        ADDWEAPONVAR(i, TotalTime);
1309
        ADDWEAPONVAR(i, HoldDelay);
1310
        ADDWEAPONVAR(i, Flags);
1311
        ADDWEAPONVAR(i, Shoots);
1312
        ADDWEAPONVAR(i, SpawnTime);
1313
        ADDWEAPONVAR(i, Spawn);
1314
        ADDWEAPONVAR(i, ShotsPerBurst);
1315
        ADDWEAPONVAR(i, InitialSound);
1316
        ADDWEAPONVAR(i, FireSound);
1317
        ADDWEAPONVAR(i, Sound2Time);
1318
        ADDWEAPONVAR(i, Sound2Sound);
1319
        ADDWEAPONVAR(i, ReloadSound1);
1320
        ADDWEAPONVAR(i, ReloadSound2);
1321
        ADDWEAPONVAR(i, SelectSound);
1322
        ADDWEAPONVAR(i, FlashColor);
1323
    }
3409 helixhorne 1324
#ifdef LUNATIC
1325
    for (i=0; i<MAXPLAYERS; i++)
1326
    {
1327
        DukePlayer_t *ps = g_player[i].ps;
1328
 
1329
        ps->pipebombControl = NAM ? PIPEBOMB_TIMER : PIPEBOMB_REMOTE;
1330
        ps->pipebombLifetime = NAM_GRENADE_LIFETIME;
1331
        ps->pipebombLifetimeVar = NAM_GRENADE_LIFETIME_VAR;
1332
 
1333
        ps->tripbombControl = TRIPBOMB_TRIPWIRE;
1334
        ps->tripbombLifetime = NAM_GRENADE_LIFETIME;
1335
        ps->tripbombLifetimeVar = NAM_GRENADE_LIFETIME_VAR;
1336
    }
1337
#else
1179 terminx 1338
    Gv_NewVar("GRENADE_LIFETIME", NAM_GRENADE_LIFETIME, GAMEVAR_PERPLAYER | GAMEVAR_SYSTEM);
1339
    Gv_NewVar("GRENADE_LIFETIME_VAR", NAM_GRENADE_LIFETIME_VAR, GAMEVAR_PERPLAYER | GAMEVAR_SYSTEM);
398 terminx 1340
 
1179 terminx 1341
    Gv_NewVar("STICKYBOMB_LIFETIME", NAM_GRENADE_LIFETIME, GAMEVAR_PERPLAYER | GAMEVAR_SYSTEM);
1342
    Gv_NewVar("STICKYBOMB_LIFETIME_VAR", NAM_GRENADE_LIFETIME_VAR, GAMEVAR_PERPLAYER | GAMEVAR_SYSTEM);
398 terminx 1343
 
1179 terminx 1344
    Gv_NewVar("TRIPBOMB_CONTROL", TRIPBOMB_TRIPWIRE, GAMEVAR_PERPLAYER | GAMEVAR_SYSTEM);
1345
    Gv_NewVar("PIPEBOMB_CONTROL", NAM?PIPEBOMB_TIMER:PIPEBOMB_REMOTE, GAMEVAR_PERPLAYER | GAMEVAR_SYSTEM);
398 terminx 1346
 
1179 terminx 1347
    Gv_NewVar("RESPAWN_MONSTERS", (intptr_t)&ud.respawn_monsters,GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1348
    Gv_NewVar("RESPAWN_ITEMS",(intptr_t)&ud.respawn_items, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1349
    Gv_NewVar("RESPAWN_INVENTORY",(intptr_t)&ud.respawn_inventory, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1350
    Gv_NewVar("MONSTERS_OFF",(intptr_t)&ud.monsters_off, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1351
    Gv_NewVar("MARKER",(intptr_t)&ud.marker, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1352
    Gv_NewVar("FFIRE",(intptr_t)&ud.ffire, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1353
    Gv_NewVar("LEVEL",(intptr_t)&ud.level_number, GAMEVAR_SYSTEM | GAMEVAR_INTPTR | GAMEVAR_READONLY);
1354
    Gv_NewVar("VOLUME",(intptr_t)&ud.volume_number, GAMEVAR_SYSTEM | GAMEVAR_INTPTR | GAMEVAR_READONLY);
398 terminx 1355
 
1179 terminx 1356
    Gv_NewVar("COOP",(intptr_t)&ud.coop, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1357
    Gv_NewVar("MULTIMODE",(intptr_t)&ud.multimode, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
398 terminx 1358
 
1179 terminx 1359
    Gv_NewVar("WEAPON", 0, GAMEVAR_PERPLAYER | GAMEVAR_READONLY | GAMEVAR_SYSTEM);
1360
    Gv_NewVar("WORKSLIKE", 0, GAMEVAR_PERPLAYER | GAMEVAR_READONLY | GAMEVAR_SYSTEM);
1361
    Gv_NewVar("RETURN", 0, GAMEVAR_SYSTEM);
1362
    Gv_NewVar("ZRANGE", 4, GAMEVAR_PERPLAYER | GAMEVAR_SYSTEM);
1363
    Gv_NewVar("ANGRANGE", 18, GAMEVAR_PERPLAYER | GAMEVAR_SYSTEM);
1364
    Gv_NewVar("AUTOAIMANGLE", 0, GAMEVAR_PERPLAYER | GAMEVAR_SYSTEM);
1365
    Gv_NewVar("LOTAG", 0, GAMEVAR_SYSTEM);
1366
    Gv_NewVar("HITAG", 0, GAMEVAR_SYSTEM);
1367
    Gv_NewVar("TEXTURE", 0, GAMEVAR_SYSTEM);
1368
    Gv_NewVar("THISACTOR", 0, GAMEVAR_READONLY | GAMEVAR_SYSTEM);
1204 terminx 1369
 
1370
    // special vars for struct access
1221 terminx 1371
    Gv_NewVar("sprite", -1, GAMEVAR_READONLY | GAMEVAR_SYSTEM | GAMEVAR_SPECIAL);
1372
    Gv_NewVar("sector", -1, GAMEVAR_READONLY | GAMEVAR_SYSTEM | GAMEVAR_SPECIAL);
1373
    Gv_NewVar("wall", -1, GAMEVAR_READONLY | GAMEVAR_SYSTEM | GAMEVAR_SPECIAL);
1374
    Gv_NewVar("player", -1, GAMEVAR_READONLY | GAMEVAR_SYSTEM | GAMEVAR_SPECIAL);
1375
    Gv_NewVar("actorvar", -1, GAMEVAR_READONLY | GAMEVAR_SYSTEM | GAMEVAR_SPECIAL);
1204 terminx 1376
 
1857 terminx 1377
    Gv_NewVar("myconnectindex", (intptr_t)&myconnectindex, GAMEVAR_READONLY | GAMEVAR_INTPTR | GAMEVAR_SYSTEM);
1378
    Gv_NewVar("screenpeek", (intptr_t)&screenpeek, GAMEVAR_READONLY | GAMEVAR_INTPTR | GAMEVAR_SYSTEM);
3408 helixhorne 1379
    Gv_NewVar("currentweapon",(intptr_t)&hudweap.cur, GAMEVAR_INTPTR | GAMEVAR_SYSTEM);
1380
    Gv_NewVar("gs",(intptr_t)&hudweap.shade, GAMEVAR_INTPTR | GAMEVAR_SYSTEM);
1381
    Gv_NewVar("looking_arc",(intptr_t)&hudweap.lookhoriz, GAMEVAR_INTPTR | GAMEVAR_SYSTEM);
1382
    Gv_NewVar("gun_pos",(intptr_t)&hudweap.gunposy, GAMEVAR_INTPTR | GAMEVAR_SYSTEM);
1383
    Gv_NewVar("weapon_xoffset",(intptr_t)&hudweap.gunposx, GAMEVAR_INTPTR | GAMEVAR_SYSTEM);
1384
    Gv_NewVar("weaponcount",(intptr_t)&hudweap.count, GAMEVAR_INTPTR | GAMEVAR_SYSTEM);
1385
    Gv_NewVar("looking_angSR1",(intptr_t)&hudweap.lookhalfang, GAMEVAR_INTPTR | GAMEVAR_SYSTEM);
1857 terminx 1386
    Gv_NewVar("xdim",(intptr_t)&xdim, GAMEVAR_INTPTR | GAMEVAR_SYSTEM | GAMEVAR_READONLY);
1387
    Gv_NewVar("ydim",(intptr_t)&ydim, GAMEVAR_INTPTR | GAMEVAR_SYSTEM | GAMEVAR_READONLY);
1388
    Gv_NewVar("windowx1",(intptr_t)&windowx1, GAMEVAR_INTPTR | GAMEVAR_SYSTEM | GAMEVAR_READONLY);
1389
    Gv_NewVar("windowx2",(intptr_t)&windowx2, GAMEVAR_INTPTR | GAMEVAR_SYSTEM | GAMEVAR_READONLY);
1390
    Gv_NewVar("windowy1",(intptr_t)&windowy1, GAMEVAR_INTPTR | GAMEVAR_SYSTEM | GAMEVAR_READONLY);
1391
    Gv_NewVar("windowy2",(intptr_t)&windowy2, GAMEVAR_INTPTR | GAMEVAR_SYSTEM | GAMEVAR_READONLY);
1392
    Gv_NewVar("totalclock",(intptr_t)&totalclock, GAMEVAR_INTPTR | GAMEVAR_SYSTEM | GAMEVAR_READONLY);
1393
    Gv_NewVar("lastvisinc",(intptr_t)&lastvisinc, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1179 terminx 1394
    Gv_NewVar("numsectors",(intptr_t)&numsectors, GAMEVAR_SYSTEM | GAMEVAR_SHORTPTR | GAMEVAR_READONLY);
897 hnt_ts 1395
 
1179 terminx 1396
    Gv_NewVar("current_menu",(intptr_t)&g_currentMenu, GAMEVAR_SYSTEM | GAMEVAR_INTPTR | GAMEVAR_READONLY);
1397
    Gv_NewVar("numplayers",(intptr_t)&numplayers, GAMEVAR_SYSTEM | GAMEVAR_INTPTR | GAMEVAR_READONLY);
1857 terminx 1398
    Gv_NewVar("viewingrange",(intptr_t)&viewingrange, GAMEVAR_SYSTEM | GAMEVAR_INTPTR | GAMEVAR_READONLY);
1399
    Gv_NewVar("yxaspect",(intptr_t)&yxaspect, GAMEVAR_SYSTEM | GAMEVAR_INTPTR | GAMEVAR_READONLY);
1179 terminx 1400
    Gv_NewVar("gravitationalconstant",(intptr_t)&g_spriteGravity, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1401
    Gv_NewVar("gametype_flags",(intptr_t)&GametypeFlags[ud.coop], GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1857 terminx 1402
    Gv_NewVar("framerate",(intptr_t)&g_currentFrameRate, GAMEVAR_SYSTEM | GAMEVAR_INTPTR | GAMEVAR_READONLY);
1179 terminx 1403
    Gv_NewVar("CLIPMASK0", CLIPMASK0, GAMEVAR_SYSTEM|GAMEVAR_READONLY);
1404
    Gv_NewVar("CLIPMASK1", CLIPMASK1, GAMEVAR_SYSTEM|GAMEVAR_READONLY);
502 terminx 1405
 
3405 helixhorne 1406
    Gv_NewVar("camerax",(intptr_t)&ud.camerapos.x, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1407
    Gv_NewVar("cameray",(intptr_t)&ud.camerapos.y, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1408
    Gv_NewVar("cameraz",(intptr_t)&ud.camerapos.z, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1857 terminx 1409
    Gv_NewVar("cameraang",(intptr_t)&ud.cameraang, GAMEVAR_SYSTEM | GAMEVAR_SHORTPTR);
1410
    Gv_NewVar("camerahoriz",(intptr_t)&ud.camerahoriz, GAMEVAR_SYSTEM | GAMEVAR_SHORTPTR);
1411
    Gv_NewVar("camerasect",(intptr_t)&ud.camerasect, GAMEVAR_SYSTEM | GAMEVAR_SHORTPTR);
1412
    Gv_NewVar("cameradist",(intptr_t)&g_cameraDistance, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1413
    Gv_NewVar("cameraclock",(intptr_t)&g_cameraClock, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
502 terminx 1414
 
1857 terminx 1415
    Gv_NewVar("myx",(intptr_t)&my.x, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1416
    Gv_NewVar("myy",(intptr_t)&my.y, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1417
    Gv_NewVar("myz",(intptr_t)&my.z, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1418
    Gv_NewVar("omyx",(intptr_t)&omy.x, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1419
    Gv_NewVar("omyy",(intptr_t)&omy.y, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1420
    Gv_NewVar("omyz",(intptr_t)&omy.z, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1421
    Gv_NewVar("myvelx",(intptr_t)&myvel.x, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1422
    Gv_NewVar("myvely",(intptr_t)&myvel.y, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
1423
    Gv_NewVar("myvelz",(intptr_t)&myvel.z, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
508 terminx 1424
 
1857 terminx 1425
    Gv_NewVar("myhoriz",(intptr_t)&myhoriz, GAMEVAR_SYSTEM | GAMEVAR_SHORTPTR);
1426
    Gv_NewVar("myhorizoff",(intptr_t)&myhorizoff, GAMEVAR_SYSTEM | GAMEVAR_SHORTPTR);
1427
    Gv_NewVar("omyhoriz",(intptr_t)&omyhoriz, GAMEVAR_SYSTEM | GAMEVAR_SHORTPTR);
1428
    Gv_NewVar("omyhorizoff",(intptr_t)&omyhorizoff, GAMEVAR_SYSTEM | GAMEVAR_SHORTPTR);
1429
    Gv_NewVar("myang",(intptr_t)&myang, GAMEVAR_SYSTEM | GAMEVAR_SHORTPTR);
1430
    Gv_NewVar("omyang",(intptr_t)&omyang, GAMEVAR_SYSTEM | GAMEVAR_SHORTPTR);
1431
    Gv_NewVar("mycursectnum",(intptr_t)&mycursectnum, GAMEVAR_SYSTEM | GAMEVAR_SHORTPTR);
1432
    Gv_NewVar("myjumpingcounter",(intptr_t)&myjumpingcounter, GAMEVAR_SYSTEM | GAMEVAR_SHORTPTR);
508 terminx 1433
 
1857 terminx 1434
    Gv_NewVar("myjumpingtoggle",(intptr_t)&myjumpingtoggle, GAMEVAR_SYSTEM | GAMEVAR_CHARPTR);
1435
    Gv_NewVar("myonground",(intptr_t)&myonground, GAMEVAR_SYSTEM | GAMEVAR_CHARPTR);
1436
    Gv_NewVar("myhardlanding",(intptr_t)&myhardlanding, GAMEVAR_SYSTEM | GAMEVAR_CHARPTR);
1437
    Gv_NewVar("myreturntocenter",(intptr_t)&myreturntocenter, GAMEVAR_SYSTEM | GAMEVAR_CHARPTR);
522 terminx 1438
 
1857 terminx 1439
    Gv_NewVar("display_mirror",(intptr_t)&display_mirror, GAMEVAR_SYSTEM | GAMEVAR_CHARPTR);
1179 terminx 1440
    Gv_NewVar("randomseed",(intptr_t)&randomseed, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
770 hnt_ts 1441
 
1179 terminx 1442
    Gv_NewVar("NUMWALLS",(intptr_t)&numwalls, GAMEVAR_SYSTEM | GAMEVAR_SHORTPTR | GAMEVAR_READONLY);
1443
    Gv_NewVar("NUMSECTORS",(intptr_t)&numsectors, GAMEVAR_SYSTEM | GAMEVAR_SHORTPTR | GAMEVAR_READONLY);
2484 helixhorne 1444
    Gv_NewVar("Numsprites",(intptr_t)&Numsprites, GAMEVAR_SYSTEM | GAMEVAR_INTPTR | GAMEVAR_READONLY);
1068 terminx 1445
 
1857 terminx 1446
    Gv_NewVar("lastsavepos",(intptr_t)&g_lastSaveSlot, GAMEVAR_SYSTEM | GAMEVAR_INTPTR);
3343 helixhorne 1447
# ifdef USE_OPENGL
1857 terminx 1448
    Gv_NewVar("rendmode",(intptr_t)&rendmode, GAMEVAR_READONLY | GAMEVAR_INTPTR | GAMEVAR_SYSTEM);
3343 helixhorne 1449
# else
1857 terminx 1450
    Gv_NewVar("rendmode", 0, GAMEVAR_READONLY | GAMEVAR_SYSTEM);
3343 helixhorne 1451
# endif
3274 hendricks2 1452
 
4191 helixhorne 1453
    // SYSTEM_GAMEARRAY
4631 terminx 1454
    Gv_NewArray("tilesizx", NULL, MAXTILES, GAMEARRAY_READONLY);
1455
    Gv_NewArray("tilesizy", NULL, MAXTILES, GAMEARRAY_READONLY);
3343 helixhorne 1456
#endif
398 terminx 1457
}
1458
 
3848 helixhorne 1459
#undef ADDWEAPONVAR
1460
 
1143 terminx 1461
void Gv_Init(void)
398 terminx 1462
{
4895 terminx 1463
    // already initialized
1464
    if (aGameVars[0].dwFlags)
1942 helixhorne 1465
        return;
1466
 
3846 helixhorne 1467
#if !defined LUNATIC
1143 terminx 1468
    Gv_Clear();
3846 helixhorne 1469
#endif
1470
    // Set up weapon defaults, g_playerWeapon[][].
1143 terminx 1471
    Gv_AddSystemVars();
3846 helixhorne 1472
#if !defined LUNATIC
1143 terminx 1473
    Gv_InitWeaponPointers();
3459 helixhorne 1474
#endif
1143 terminx 1475
    Gv_ResetSystemDefaults();
398 terminx 1476
}
1477
 
3354 helixhorne 1478
#if !defined LUNATIC
1143 terminx 1479
void Gv_InitWeaponPointers(void)
398 terminx 1480
{
1205 terminx 1481
    int32_t i;
782 terminx 1482
    char aszBuf[64];
1483
    // called from game Init AND when level is loaded...
398 terminx 1484
 
1143 terminx 1485
    //AddLog("Gv_InitWeaponPointers");
398 terminx 1486
 
1229 terminx 1487
    for (i=(MAX_WEAPONS-1); i>=0; i--)
782 terminx 1488
    {
1489
        Bsprintf(aszBuf,"WEAPON%d_CLIP",i);
1143 terminx 1490
        aplWeaponClip[i]=Gv_GetVarDataPtr(aszBuf);
782 terminx 1491
        if (!aplWeaponClip[i])
1492
        {
1143 terminx 1493
            initprintf("ERROR: NULL weapon!  WTF?!\n");
4502 hendricks2 1494
            // Bexit(0);
1143 terminx 1495
            G_Shutdown();
782 terminx 1496
        }
1497
        Bsprintf(aszBuf,"WEAPON%d_RELOAD",i);
1143 terminx 1498
        aplWeaponReload[i]=Gv_GetVarDataPtr(aszBuf);
782 terminx 1499
        Bsprintf(aszBuf,"WEAPON%d_FIREDELAY",i);
1143 terminx 1500
        aplWeaponFireDelay[i]=Gv_GetVarDataPtr(aszBuf);
782 terminx 1501
        Bsprintf(aszBuf,"WEAPON%d_TOTALTIME",i);
1143 terminx 1502
        aplWeaponTotalTime[i]=Gv_GetVarDataPtr(aszBuf);
782 terminx 1503
        Bsprintf(aszBuf,"WEAPON%d_HOLDDELAY",i);
1143 terminx 1504
        aplWeaponHoldDelay[i]=Gv_GetVarDataPtr(aszBuf);
782 terminx 1505
        Bsprintf(aszBuf,"WEAPON%d_FLAGS",i);
1143 terminx 1506
        aplWeaponFlags[i]=Gv_GetVarDataPtr(aszBuf);
782 terminx 1507
        Bsprintf(aszBuf,"WEAPON%d_SHOOTS",i);
1143 terminx 1508
        aplWeaponShoots[i]=Gv_GetVarDataPtr(aszBuf);
782 terminx 1509
        Bsprintf(aszBuf,"WEAPON%d_SPAWNTIME",i);
1143 terminx 1510
        aplWeaponSpawnTime[i]=Gv_GetVarDataPtr(aszBuf);
782 terminx 1511
        Bsprintf(aszBuf,"WEAPON%d_SPAWN",i);
1143 terminx 1512
        aplWeaponSpawn[i]=Gv_GetVarDataPtr(aszBuf);
782 terminx 1513
        Bsprintf(aszBuf,"WEAPON%d_SHOTSPERBURST",i);
1143 terminx 1514
        aplWeaponShotsPerBurst[i]=Gv_GetVarDataPtr(aszBuf);
782 terminx 1515
        Bsprintf(aszBuf,"WEAPON%d_WORKSLIKE",i);
1143 terminx 1516
        aplWeaponWorksLike[i]=Gv_GetVarDataPtr(aszBuf);
782 terminx 1517
        Bsprintf(aszBuf,"WEAPON%d_INITIALSOUND",i);
1143 terminx 1518
        aplWeaponInitialSound[i]=Gv_GetVarDataPtr(aszBuf);
782 terminx 1519
        Bsprintf(aszBuf,"WEAPON%d_FIRESOUND",i);
1143 terminx 1520
        aplWeaponFireSound[i]=Gv_GetVarDataPtr(aszBuf);
782 terminx 1521
        Bsprintf(aszBuf,"WEAPON%d_SOUND2TIME",i);
1143 terminx 1522
        aplWeaponSound2Time[i]=Gv_GetVarDataPtr(aszBuf);
782 terminx 1523
        Bsprintf(aszBuf,"WEAPON%d_SOUND2SOUND",i);
1143 terminx 1524
        aplWeaponSound2Sound[i]=Gv_GetVarDataPtr(aszBuf);
782 terminx 1525
        Bsprintf(aszBuf,"WEAPON%d_RELOADSOUND1",i);
1143 terminx 1526
        aplWeaponReloadSound1[i]=Gv_GetVarDataPtr(aszBuf);
782 terminx 1527
        Bsprintf(aszBuf,"WEAPON%d_RELOADSOUND2",i);
1143 terminx 1528
        aplWeaponReloadSound2[i]=Gv_GetVarDataPtr(aszBuf);
1529
        Bsprintf(aszBuf,"WEAPON%d_SELECTSOUND",i);
1530
        aplWeaponSelectSound[i]=Gv_GetVarDataPtr(aszBuf);
1315 terminx 1531
        Bsprintf(aszBuf,"WEAPON%d_FLASHCOLOR",i);
1532
        aplWeaponFlashColor[i]=Gv_GetVarDataPtr(aszBuf);
782 terminx 1533
    }
398 terminx 1534
}
1535
 
1143 terminx 1536
void Gv_RefreshPointers(void)
534 terminx 1537
{
1189 terminx 1538
    aGameVars[Gv_GetVarIndex("RESPAWN_MONSTERS")].val.lValue = (intptr_t)&ud.respawn_monsters;
1539
    aGameVars[Gv_GetVarIndex("RESPAWN_ITEMS")].val.lValue = (intptr_t)&ud.respawn_items;
1540
    aGameVars[Gv_GetVarIndex("RESPAWN_INVENTORY")].val.lValue = (intptr_t)&ud.respawn_inventory;
1541
    aGameVars[Gv_GetVarIndex("MONSTERS_OFF")].val.lValue = (intptr_t)&ud.monsters_off;
1542
    aGameVars[Gv_GetVarIndex("MARKER")].val.lValue = (intptr_t)&ud.marker;
1543
    aGameVars[Gv_GetVarIndex("FFIRE")].val.lValue = (intptr_t)&ud.ffire;
1544
    aGameVars[Gv_GetVarIndex("LEVEL")].val.lValue = (intptr_t)&ud.level_number;
1545
    aGameVars[Gv_GetVarIndex("VOLUME")].val.lValue = (intptr_t)&ud.volume_number;
534 terminx 1546
 
1189 terminx 1547
    aGameVars[Gv_GetVarIndex("COOP")].val.lValue = (intptr_t)&ud.coop;
1548
    aGameVars[Gv_GetVarIndex("MULTIMODE")].val.lValue = (intptr_t)&ud.multimode;
534 terminx 1549
 
1189 terminx 1550
    aGameVars[Gv_GetVarIndex("myconnectindex")].val.lValue = (intptr_t)&myconnectindex;
1551
    aGameVars[Gv_GetVarIndex("screenpeek")].val.lValue = (intptr_t)&screenpeek;
3408 helixhorne 1552
    aGameVars[Gv_GetVarIndex("currentweapon")].val.lValue = (intptr_t)&hudweap.cur;
1553
    aGameVars[Gv_GetVarIndex("gs")].val.lValue = (intptr_t)&hudweap.shade;
1554
    aGameVars[Gv_GetVarIndex("looking_arc")].val.lValue = (intptr_t)&hudweap.lookhoriz;
1555
    aGameVars[Gv_GetVarIndex("gun_pos")].val.lValue = (intptr_t)&hudweap.gunposy;
1556
    aGameVars[Gv_GetVarIndex("weapon_xoffset")].val.lValue = (intptr_t)&hudweap.gunposx;
1557
    aGameVars[Gv_GetVarIndex("weaponcount")].val.lValue = (intptr_t)&hudweap.count;
1558
    aGameVars[Gv_GetVarIndex("looking_angSR1")].val.lValue = (intptr_t)&hudweap.lookhalfang;
1189 terminx 1559
    aGameVars[Gv_GetVarIndex("xdim")].val.lValue = (intptr_t)&xdim;
1560
    aGameVars[Gv_GetVarIndex("ydim")].val.lValue = (intptr_t)&ydim;
1561
    aGameVars[Gv_GetVarIndex("windowx1")].val.lValue = (intptr_t)&windowx1;
1562
    aGameVars[Gv_GetVarIndex("windowx2")].val.lValue = (intptr_t)&windowx2;
1563
    aGameVars[Gv_GetVarIndex("windowy1")].val.lValue = (intptr_t)&windowy1;
1564
    aGameVars[Gv_GetVarIndex("windowy2")].val.lValue = (intptr_t)&windowy2;
1565
    aGameVars[Gv_GetVarIndex("totalclock")].val.lValue = (intptr_t)&totalclock;
1566
    aGameVars[Gv_GetVarIndex("lastvisinc")].val.lValue = (intptr_t)&lastvisinc;
1567
    aGameVars[Gv_GetVarIndex("numsectors")].val.lValue = (intptr_t)&numsectors;
1568
    aGameVars[Gv_GetVarIndex("numplayers")].val.lValue = (intptr_t)&numplayers;
1569
    aGameVars[Gv_GetVarIndex("current_menu")].val.lValue = (intptr_t)&g_currentMenu;
1570
    aGameVars[Gv_GetVarIndex("viewingrange")].val.lValue = (intptr_t)&viewingrange;
1571
    aGameVars[Gv_GetVarIndex("yxaspect")].val.lValue = (intptr_t)&yxaspect;
1572
    aGameVars[Gv_GetVarIndex("gravitationalconstant")].val.lValue = (intptr_t)&g_spriteGravity;
1573
    aGameVars[Gv_GetVarIndex("gametype_flags")].val.lValue = (intptr_t)&GametypeFlags[ud.coop];
1574
    aGameVars[Gv_GetVarIndex("framerate")].val.lValue = (intptr_t)&g_currentFrameRate;
534 terminx 1575
 
3405 helixhorne 1576
    aGameVars[Gv_GetVarIndex("camerax")].val.lValue = (intptr_t)&ud.camerapos.x;
1577
    aGameVars[Gv_GetVarIndex("cameray")].val.lValue = (intptr_t)&ud.camerapos.y;
1578
    aGameVars[Gv_GetVarIndex("cameraz")].val.lValue = (intptr_t)&ud.camerapos.z;
1189 terminx 1579
    aGameVars[Gv_GetVarIndex("cameraang")].val.lValue = (intptr_t)&ud.cameraang;
1580
    aGameVars[Gv_GetVarIndex("camerahoriz")].val.lValue = (intptr_t)&ud.camerahoriz;
1581
    aGameVars[Gv_GetVarIndex("camerasect")].val.lValue = (intptr_t)&ud.camerasect;
1582
    aGameVars[Gv_GetVarIndex("cameradist")].val.lValue = (intptr_t)&g_cameraDistance;
1583
    aGameVars[Gv_GetVarIndex("cameraclock")].val.lValue = (intptr_t)&g_cameraClock;
534 terminx 1584
 
1208 terminx 1585
    aGameVars[Gv_GetVarIndex("myx")].val.lValue = (intptr_t)&my.x;
1586
    aGameVars[Gv_GetVarIndex("myy")].val.lValue = (intptr_t)&my.y;
1587
    aGameVars[Gv_GetVarIndex("myz")].val.lValue = (intptr_t)&my.z;
1588
    aGameVars[Gv_GetVarIndex("omyx")].val.lValue = (intptr_t)&omy.x;
1589
    aGameVars[Gv_GetVarIndex("omyy")].val.lValue = (intptr_t)&omy.y;
1590
    aGameVars[Gv_GetVarIndex("omyz")].val.lValue = (intptr_t)&omy.z;
1591
    aGameVars[Gv_GetVarIndex("myvelx")].val.lValue = (intptr_t)&myvel.x;
1592
    aGameVars[Gv_GetVarIndex("myvely")].val.lValue = (intptr_t)&myvel.y;
1593
    aGameVars[Gv_GetVarIndex("myvelz")].val.lValue = (intptr_t)&myvel.z;
534 terminx 1594
 
1189 terminx 1595
    aGameVars[Gv_GetVarIndex("myhoriz")].val.lValue = (intptr_t)&myhoriz;
1596
    aGameVars[Gv_GetVarIndex("myhorizoff")].val.lValue = (intptr_t)&myhorizoff;
1597
    aGameVars[Gv_GetVarIndex("omyhoriz")].val.lValue = (intptr_t)&omyhoriz;
1598
    aGameVars[Gv_GetVarIndex("omyhorizoff")].val.lValue = (intptr_t)&omyhorizoff;
1599
    aGameVars[Gv_GetVarIndex("myang")].val.lValue = (intptr_t)&myang;
1600
    aGameVars[Gv_GetVarIndex("omyang")].val.lValue = (intptr_t)&omyang;
1601
    aGameVars[Gv_GetVarIndex("mycursectnum")].val.lValue = (intptr_t)&mycursectnum;
1602
    aGameVars[Gv_GetVarIndex("myjumpingcounter")].val.lValue = (intptr_t)&myjumpingcounter;
534 terminx 1603
 
1189 terminx 1604
    aGameVars[Gv_GetVarIndex("myjumpingtoggle")].val.lValue = (intptr_t)&myjumpingtoggle;
1605
    aGameVars[Gv_GetVarIndex("myonground")].val.lValue = (intptr_t)&myonground;
1606
    aGameVars[Gv_GetVarIndex("myhardlanding")].val.lValue = (intptr_t)&myhardlanding;
1607
    aGameVars[Gv_GetVarIndex("myreturntocenter")].val.lValue = (intptr_t)&myreturntocenter;
534 terminx 1608
 
1189 terminx 1609
    aGameVars[Gv_GetVarIndex("display_mirror")].val.lValue = (intptr_t)&display_mirror;
1610
    aGameVars[Gv_GetVarIndex("randomseed")].val.lValue = (intptr_t)&randomseed;
770 hnt_ts 1611
 
1189 terminx 1612
    aGameVars[Gv_GetVarIndex("NUMWALLS")].val.lValue = (intptr_t)&numwalls;
1613
    aGameVars[Gv_GetVarIndex("NUMSECTORS")].val.lValue = (intptr_t)&numsectors;
2484 helixhorne 1614
    aGameVars[Gv_GetVarIndex("Numsprites")].val.lValue = (intptr_t)&Numsprites;
1143 terminx 1615
 
1189 terminx 1616
    aGameVars[Gv_GetVarIndex("lastsavepos")].val.lValue = (intptr_t)&g_lastSaveSlot;
3343 helixhorne 1617
# ifdef USE_OPENGL
1367 qbix79 1618
    aGameVars[Gv_GetVarIndex("rendmode")].val.lValue = (intptr_t)&rendmode;
3343 helixhorne 1619
# endif
3354 helixhorne 1620
}
1366 plagman 1621
#endif