Subversion Repositories eduke32

Rev

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

Rev Author Line No. Line
1677 terminx 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
4541 hendricks2 19
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
1677 terminx 20
*/
21
//-------------------------------------------------------------------------
22
 
4747 terminx 23
#ifndef gamevars_h_
24
#define gamevars_h_
1677 terminx 25
 
1909 terminx 26
#include "gamedef.h"
27
 
3268 terminx 28
#define MAXGAMEVARS 2048 // must be a power of two
29
#define MAXVARLABEL 26
30
 
1677 terminx 31
// store global game definitions
32
enum GamevarFlags_t {
33
    GAMEVAR_PERPLAYER  = 0x00000001, // per-player variable
34
    GAMEVAR_PERACTOR   = 0x00000002, // per-actor variable
1857 terminx 35
    GAMEVAR_USER_MASK  = (GAMEVAR_PERPLAYER|GAMEVAR_PERACTOR),
3417 helixhorne 36
    GAMEVAR_RESET      = 0x00000008, // INTERNAL, don't use
37
    GAMEVAR_DEFAULT    = 0x00000100, // UNUSED, but always cleared for user-defined gamevars
1677 terminx 38
    GAMEVAR_SECRET     = 0x00000200, // don't dump...
39
    GAMEVAR_NODEFAULT  = 0x00000400, // don't reset on actor spawn
40
    GAMEVAR_SYSTEM     = 0x00000800, // cannot change mode flags...(only default value)
41
    GAMEVAR_READONLY   = 0x00001000, // values are read-only (no setvar allowed)
42
    GAMEVAR_INTPTR     = 0x00002000, // plValues is a pointer to an int32_t
43
    GAMEVAR_SHORTPTR   = 0x00008000, // plValues is a pointer to a short
44
    GAMEVAR_CHARPTR    = 0x00010000, // plValues is a pointer to a char
1857 terminx 45
    GAMEVAR_PTR_MASK   = (GAMEVAR_INTPTR|GAMEVAR_SHORTPTR|GAMEVAR_CHARPTR),
1677 terminx 46
    GAMEVAR_NORESET    = 0x00020000, // var values are not reset when restoring map state
47
    GAMEVAR_SPECIAL    = 0x00040000, // flag for structure member shortcut vars
48
    GAMEVAR_NOMULTI    = 0x00080000, // don't attach to multiplayer packets
49
};
50
 
3415 helixhorne 51
#if !defined LUNATIC
52
 
4755 helixhorne 53
// Alignments for per-player and per-actor variables.
4735 helixhorne 54
#define PLAYER_VAR_ALIGNMENT (sizeof(intptr_t))
55
#define ACTOR_VAR_ALIGNMENT 16
56
 
3789 helixhorne 57
# define MAXGAMEARRAYS (MAXGAMEVARS>>2) // must be lower than MAXGAMEVARS
58
# define MAXARRAYLABEL MAXVARLABEL
3268 terminx 59
 
1677 terminx 60
enum GamearrayFlags_t {
3274 hendricks2 61
 
62
    GAMEARRAY_READONLY = 0x00001000,
63
    GAMEARRAY_WARN = 0x00002000,
64
 
65
    GAMEARRAY_NORMAL   = 0x00004000,
66
    GAMEARRAY_OFCHAR   = 0x00000001,
67
    GAMEARRAY_OFSHORT  = 0x00000002,
68
    GAMEARRAY_OFINT    = 0x00000004,
69
    GAMEARRAY_TYPE_MASK = GAMEARRAY_OFCHAR|GAMEARRAY_OFSHORT|GAMEARRAY_OFINT,
70
 
71
    GAMEARRAY_VARSIZE = 0x00000020,
72
 
73
    GAMEARRAY_RESET    = 0x00000008,
74
///    GAMEARRAY_NORESET  = 0x00000001,
1677 terminx 75
};
76
 
77
#pragma pack(push,1)
78
typedef struct {
79
    union {
80
        intptr_t lValue;
81
        intptr_t *plValues;     // array of values when 'per-player', or 'per-actor'
82
    } val;
83
    intptr_t lDefault;
84
    uintptr_t dwFlags;
85
    char *szLabel;
86
} gamevar_t;
87
 
88
typedef struct {
89
    char *szLabel;
2664 terminx 90
    intptr_t *plValues;     // array of values
1677 terminx 91
    intptr_t size;
3274 hendricks2 92
    intptr_t dwFlags;
1677 terminx 93
} gamearray_t;
94
#pragma pack(pop)
95
 
3789 helixhorne 96
# define GAR_ELTSZ (sizeof(aGameArrays[0].plValues[0]))
2689 helixhorne 97
 
1677 terminx 98
extern gamevar_t aGameVars[MAXGAMEVARS];
99
extern gamearray_t aGameArrays[MAXGAMEARRAYS];
100
extern int32_t g_gameVarCount;
101
extern int32_t g_gameArrayCount;
102
 
4631 terminx 103
int32_t __fastcall Gv_GetVar(int32_t id, int32_t iActor, int32_t iPlayer);
104
void __fastcall Gv_SetVar(int32_t id, int32_t lValue, int32_t iActor, int32_t iPlayer);
105
int32_t __fastcall Gv_GetVarX(int32_t id);
106
void __fastcall Gv_SetVarX(int32_t id, int32_t lValue);
3343 helixhorne 107
 
1677 terminx 108
int32_t Gv_GetVarByLabel(const char *szGameLabel,int32_t lDefault,int32_t iActor,int32_t iPlayer);
3274 hendricks2 109
int32_t Gv_NewArray(const char *pszLabel,void *arrayptr,intptr_t asize,uint32_t dwFlags);
2005 terminx 110
int32_t Gv_NewVar(const char *pszLabel,intptr_t lValue,uint32_t dwFlags);
4745 terminx 111
 
112
static inline void A_ResetVars(const int32_t iActor)
4698 terminx 113
{
4762 hendricks2 114
    int i;
115
    for (i = 0; i < g_gameVarCount; i++)
4698 terminx 116
    {
4745 terminx 117
        if ((aGameVars[i].dwFlags & (GAMEVAR_PERACTOR | GAMEVAR_NODEFAULT)) == GAMEVAR_PERACTOR)
118
            aGameVars[i].val.plValues[iActor] = aGameVars[i].lDefault;
4698 terminx 119
    }
120
}
4745 terminx 121
 
1677 terminx 122
void Gv_DumpValues(void);
123
void Gv_InitWeaponPointers(void);
124
void Gv_RefreshPointers(void);
125
void Gv_ResetVars(void);
3789 helixhorne 126
int32_t Gv_ReadSave(int32_t fil,int32_t newbehav);
127
void Gv_WriteSave(FILE *fil,int32_t newbehav);
3830 helixhorne 128
#else
4286 helixhorne 129
extern int32_t g_noResetVars;
130
extern LUNATIC_CB void (*A_ResetVars)(int32_t iActor);
3354 helixhorne 131
#endif
3830 helixhorne 132
 
3459 helixhorne 133
void Gv_ResetSystemDefaults(void);
3354 helixhorne 134
void Gv_Init(void);
3848 helixhorne 135
void Gv_FinalizeWeaponDefaults(void);
1909 terminx 136
 
3415 helixhorne 137
#if !defined LUNATIC
4745 terminx 138
#define VM_GAMEVAR_OPERATOR(func, operator)                                                                            \
139
    static inline void __fastcall func(const int32_t id, const int32_t lValue)                                         \
140
    {                                                                                                                  \
141
        switch (aGameVars[id].dwFlags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK))                                        \
142
        {                                                                                                              \
143
            default: aGameVars[id].val.lValue operator lValue; break;                                                  \
144
            case GAMEVAR_PERPLAYER:                                                                                    \
145
                if (EDUKE32_PREDICT_FALSE((unsigned)vm.g_p > MAXPLAYERS - 1))                                          \
146
                    break;                                                                                             \
147
                aGameVars[id].val.plValues[vm.g_p] operator lValue;                                                    \
148
                break;                                                                                                 \
149
            case GAMEVAR_PERACTOR:                                                                                     \
150
                if (EDUKE32_PREDICT_FALSE((unsigned)vm.g_i > MAXSPRITES - 1))                                          \
151
                    break;                                                                                             \
152
                aGameVars[id].val.plValues[vm.g_i] operator lValue;                                                    \
153
                break;                                                                                                 \
154
            case GAMEVAR_INTPTR: *((int32_t *)aGameVars[id].val.lValue) operator (int32_t) lValue; break;              \
155
            case GAMEVAR_SHORTPTR: *((int16_t *)aGameVars[id].val.lValue) operator (int16_t) lValue; break;            \
156
            case GAMEVAR_CHARPTR: *((uint8_t *)aGameVars[id].val.lValue) operator (uint8_t) lValue; break;             \
157
        }                                                                                                              \
4698 terminx 158
    }
1909 terminx 159
 
4658 terminx 160
#if defined(__arm__) || defined(LIBDIVIDE_ALWAYS)
4745 terminx 161
static inline void __fastcall Gv_DivVar(const int32_t id, const int32_t lValue)
4658 terminx 162
{
163
    static libdivide_s32_t sdiv;
164
    static int32_t lastlValue;
165
    libdivide_s32_t *dptr = &sdiv;
166
    intptr_t *iptr = &aGameVars[id].val.lValue;
167
 
4698 terminx 168
    if (EDUKE32_PREDICT_FALSE((aGameVars[id].dwFlags & GAMEVAR_PERPLAYER && (unsigned)vm.g_p > MAXPLAYERS - 1) ||
169
                              (aGameVars[id].dwFlags & GAMEVAR_PERACTOR && (unsigned)vm.g_i > MAXSPRITES - 1)))
170
        return;
4658 terminx 171
 
4698 terminx 172
    if ((unsigned)lValue < DIVTABLESIZE)
4658 terminx 173
        dptr = (libdivide_s32_t *)&divtable32[lValue];
174
    else if (lValue != lastlValue)
175
        sdiv = libdivide_s32_gen(lValue), lastlValue = lValue;
176
 
4698 terminx 177
    switch (aGameVars[id].dwFlags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK))
4658 terminx 178
    {
4698 terminx 179
        case GAMEVAR_PERPLAYER: iptr = &aGameVars[id].val.plValues[vm.g_p];
180
        default: break;
181
        case GAMEVAR_PERACTOR: iptr = &aGameVars[id].val.plValues[vm.g_i]; break;
182
        case GAMEVAR_INTPTR:
183
            *((int32_t *)aGameVars[id].val.lValue) =
184
            (int32_t)libdivide_s32_do(*((int32_t *)aGameVars[id].val.lValue), dptr);
185
            return;
186
        case GAMEVAR_SHORTPTR:
187
            *((int16_t *)aGameVars[id].val.lValue) =
188
            (int16_t)libdivide_s32_do(*((int16_t *)aGameVars[id].val.lValue), dptr);
189
            return;
190
        case GAMEVAR_CHARPTR:
191
            *((uint8_t *)aGameVars[id].val.lValue) =
192
            (uint8_t)libdivide_s32_do(*((uint8_t *)aGameVars[id].val.lValue), dptr);
193
            return;
4658 terminx 194
    }
195
 
196
    *iptr = libdivide_s32_do(*iptr, dptr);
197
}
198
#else
4698 terminx 199
VM_GAMEVAR_OPERATOR(Gv_DivVar, /= )
4658 terminx 200
#endif
201
 
4698 terminx 202
VM_GAMEVAR_OPERATOR(Gv_AddVar, +=)
203
VM_GAMEVAR_OPERATOR(Gv_SubVar, -=)
204
VM_GAMEVAR_OPERATOR(Gv_MulVar, *=)
205
VM_GAMEVAR_OPERATOR(Gv_ModVar, %=)
206
VM_GAMEVAR_OPERATOR(Gv_AndVar, &=)
207
VM_GAMEVAR_OPERATOR(Gv_XorVar, ^=)
208
VM_GAMEVAR_OPERATOR(Gv_OrVar, |=)
4658 terminx 209
 
4698 terminx 210
#undef VM_GAMEVAR_OPERATOR
211
 
3415 helixhorne 212
#endif
1909 terminx 213
 
1677 terminx 214
#endif