Subversion Repositories eduke32

Rev

Rev 4747 | Go to most recent revision | 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
{
4745 terminx 114
    for (int i = 0; i < g_gameVarCount; i++)
4698 terminx 115
    {
4745 terminx 116
        if ((aGameVars[i].dwFlags & (GAMEVAR_PERACTOR | GAMEVAR_NODEFAULT)) == GAMEVAR_PERACTOR)
117
            aGameVars[i].val.plValues[iActor] = aGameVars[i].lDefault;
4698 terminx 118
    }
119
}
4745 terminx 120
 
1677 terminx 121
void Gv_DumpValues(void);
122
void Gv_InitWeaponPointers(void);
123
void Gv_RefreshPointers(void);
124
void Gv_ResetVars(void);
3789 helixhorne 125
int32_t Gv_ReadSave(int32_t fil,int32_t newbehav);
126
void Gv_WriteSave(FILE *fil,int32_t newbehav);
3830 helixhorne 127
#else
4286 helixhorne 128
extern int32_t g_noResetVars;
129
extern LUNATIC_CB void (*A_ResetVars)(int32_t iActor);
3354 helixhorne 130
#endif
3830 helixhorne 131
 
3459 helixhorne 132
void Gv_ResetSystemDefaults(void);
3354 helixhorne 133
void Gv_Init(void);
3848 helixhorne 134
void Gv_FinalizeWeaponDefaults(void);
1909 terminx 135
 
3415 helixhorne 136
#if !defined LUNATIC
4745 terminx 137
#define VM_GAMEVAR_OPERATOR(func, operator)                                                                            \
138
    static inline void __fastcall func(const int32_t id, const int32_t lValue)                                         \
139
    {                                                                                                                  \
140
        switch (aGameVars[id].dwFlags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK))                                        \
141
        {                                                                                                              \
142
            default: aGameVars[id].val.lValue operator lValue; break;                                                  \
143
            case GAMEVAR_PERPLAYER:                                                                                    \
144
                if (EDUKE32_PREDICT_FALSE((unsigned)vm.g_p > MAXPLAYERS - 1))                                          \
145
                    break;                                                                                             \
146
                aGameVars[id].val.plValues[vm.g_p] operator lValue;                                                    \
147
                break;                                                                                                 \
148
            case GAMEVAR_PERACTOR:                                                                                     \
149
                if (EDUKE32_PREDICT_FALSE((unsigned)vm.g_i > MAXSPRITES - 1))                                          \
150
                    break;                                                                                             \
151
                aGameVars[id].val.plValues[vm.g_i] operator lValue;                                                    \
152
                break;                                                                                                 \
153
            case GAMEVAR_INTPTR: *((int32_t *)aGameVars[id].val.lValue) operator (int32_t) lValue; break;              \
154
            case GAMEVAR_SHORTPTR: *((int16_t *)aGameVars[id].val.lValue) operator (int16_t) lValue; break;            \
155
            case GAMEVAR_CHARPTR: *((uint8_t *)aGameVars[id].val.lValue) operator (uint8_t) lValue; break;             \
156
        }                                                                                                              \
4698 terminx 157
    }
1909 terminx 158
 
4658 terminx 159
#if defined(__arm__) || defined(LIBDIVIDE_ALWAYS)
4745 terminx 160
static inline void __fastcall Gv_DivVar(const int32_t id, const int32_t lValue)
4658 terminx 161
{
162
    static libdivide_s32_t sdiv;
163
    static int32_t lastlValue;
164
    libdivide_s32_t *dptr = &sdiv;
165
    intptr_t *iptr = &aGameVars[id].val.lValue;
166
 
4698 terminx 167
    if (EDUKE32_PREDICT_FALSE((aGameVars[id].dwFlags & GAMEVAR_PERPLAYER && (unsigned)vm.g_p > MAXPLAYERS - 1) ||
168
                              (aGameVars[id].dwFlags & GAMEVAR_PERACTOR && (unsigned)vm.g_i > MAXSPRITES - 1)))
169
        return;
4658 terminx 170
 
4698 terminx 171
    if ((unsigned)lValue < DIVTABLESIZE)
4658 terminx 172
        dptr = (libdivide_s32_t *)&divtable32[lValue];
173
    else if (lValue != lastlValue)
174
        sdiv = libdivide_s32_gen(lValue), lastlValue = lValue;
175
 
4698 terminx 176
    switch (aGameVars[id].dwFlags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK))
4658 terminx 177
    {
4698 terminx 178
        case GAMEVAR_PERPLAYER: iptr = &aGameVars[id].val.plValues[vm.g_p];
179
        default: break;
180
        case GAMEVAR_PERACTOR: iptr = &aGameVars[id].val.plValues[vm.g_i]; break;
181
        case GAMEVAR_INTPTR:
182
            *((int32_t *)aGameVars[id].val.lValue) =
183
            (int32_t)libdivide_s32_do(*((int32_t *)aGameVars[id].val.lValue), dptr);
184
            return;
185
        case GAMEVAR_SHORTPTR:
186
            *((int16_t *)aGameVars[id].val.lValue) =
187
            (int16_t)libdivide_s32_do(*((int16_t *)aGameVars[id].val.lValue), dptr);
188
            return;
189
        case GAMEVAR_CHARPTR:
190
            *((uint8_t *)aGameVars[id].val.lValue) =
191
            (uint8_t)libdivide_s32_do(*((uint8_t *)aGameVars[id].val.lValue), dptr);
192
            return;
4658 terminx 193
    }
194
 
195
    *iptr = libdivide_s32_do(*iptr, dptr);
196
}
197
#else
4698 terminx 198
VM_GAMEVAR_OPERATOR(Gv_DivVar, /= )
4658 terminx 199
#endif
200
 
4698 terminx 201
VM_GAMEVAR_OPERATOR(Gv_AddVar, +=)
202
VM_GAMEVAR_OPERATOR(Gv_SubVar, -=)
203
VM_GAMEVAR_OPERATOR(Gv_MulVar, *=)
204
VM_GAMEVAR_OPERATOR(Gv_ModVar, %=)
205
VM_GAMEVAR_OPERATOR(Gv_AndVar, &=)
206
VM_GAMEVAR_OPERATOR(Gv_XorVar, ^=)
207
VM_GAMEVAR_OPERATOR(Gv_OrVar, |=)
4658 terminx 208
 
4698 terminx 209
#undef VM_GAMEVAR_OPERATOR
210
 
3415 helixhorne 211
#endif
1909 terminx 212
 
1677 terminx 213
#endif