Subversion Repositories eduke32

Rev

Rev 4680 | Rev 4735 | 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
 
23
#ifndef __gamevars_h__
24
#define __gamevars_h__
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
 
3789 helixhorne 53
# define MAXGAMEARRAYS (MAXGAMEVARS>>2) // must be lower than MAXGAMEVARS
54
# define MAXARRAYLABEL MAXVARLABEL
3268 terminx 55
 
1677 terminx 56
enum GamearrayFlags_t {
3274 hendricks2 57
 
58
    GAMEARRAY_READONLY = 0x00001000,
59
    GAMEARRAY_WARN = 0x00002000,
60
 
61
    GAMEARRAY_NORMAL   = 0x00004000,
62
    GAMEARRAY_OFCHAR   = 0x00000001,
63
    GAMEARRAY_OFSHORT  = 0x00000002,
64
    GAMEARRAY_OFINT    = 0x00000004,
65
    GAMEARRAY_TYPE_MASK = GAMEARRAY_OFCHAR|GAMEARRAY_OFSHORT|GAMEARRAY_OFINT,
66
 
67
    GAMEARRAY_VARSIZE = 0x00000020,
68
 
69
    GAMEARRAY_RESET    = 0x00000008,
70
///    GAMEARRAY_NORESET  = 0x00000001,
1677 terminx 71
};
72
 
73
#pragma pack(push,1)
74
typedef struct {
75
    union {
76
        intptr_t lValue;
77
        intptr_t *plValues;     // array of values when 'per-player', or 'per-actor'
78
    } val;
79
    intptr_t lDefault;
80
    uintptr_t dwFlags;
81
    char *szLabel;
82
} gamevar_t;
83
 
84
typedef struct {
85
    char *szLabel;
2664 terminx 86
    intptr_t *plValues;     // array of values
1677 terminx 87
    intptr_t size;
3274 hendricks2 88
    intptr_t dwFlags;
1677 terminx 89
} gamearray_t;
90
#pragma pack(pop)
91
 
3789 helixhorne 92
# define GAR_ELTSZ (sizeof(aGameArrays[0].plValues[0]))
2689 helixhorne 93
 
1677 terminx 94
extern gamevar_t aGameVars[MAXGAMEVARS];
95
extern gamearray_t aGameArrays[MAXGAMEARRAYS];
96
extern int32_t g_gameVarCount;
97
extern int32_t g_gameArrayCount;
98
 
4631 terminx 99
int32_t __fastcall Gv_GetVar(int32_t id, int32_t iActor, int32_t iPlayer);
100
void __fastcall Gv_SetVar(int32_t id, int32_t lValue, int32_t iActor, int32_t iPlayer);
101
int32_t __fastcall Gv_GetVarX(int32_t id);
102
void __fastcall Gv_SetVarX(int32_t id, int32_t lValue);
3343 helixhorne 103
 
1677 terminx 104
int32_t Gv_GetVarByLabel(const char *szGameLabel,int32_t lDefault,int32_t iActor,int32_t iPlayer);
3274 hendricks2 105
int32_t Gv_NewArray(const char *pszLabel,void *arrayptr,intptr_t asize,uint32_t dwFlags);
2005 terminx 106
int32_t Gv_NewVar(const char *pszLabel,intptr_t lValue,uint32_t dwFlags);
4698 terminx 107
static inline void A_ResetVars(int32_t iActor)
108
{
109
    int32_t i;
110
 
111
    for (i=0; i<g_gameVarCount; i++)
112
    {
113
        if ((aGameVars[i].dwFlags & (GAMEVAR_PERACTOR|GAMEVAR_NODEFAULT)) == GAMEVAR_PERACTOR)
114
            aGameVars[i].val.plValues[iActor]=aGameVars[i].lDefault;
115
    }
116
}
1677 terminx 117
void Gv_DumpValues(void);
118
void Gv_InitWeaponPointers(void);
119
void Gv_RefreshPointers(void);
120
void Gv_ResetVars(void);
3789 helixhorne 121
int32_t Gv_ReadSave(int32_t fil,int32_t newbehav);
122
void Gv_WriteSave(FILE *fil,int32_t newbehav);
3830 helixhorne 123
#else
4286 helixhorne 124
extern int32_t g_noResetVars;
125
extern LUNATIC_CB void (*A_ResetVars)(int32_t iActor);
3354 helixhorne 126
#endif
3830 helixhorne 127
 
3459 helixhorne 128
void Gv_ResetSystemDefaults(void);
3354 helixhorne 129
void Gv_Init(void);
3848 helixhorne 130
void Gv_FinalizeWeaponDefaults(void);
1909 terminx 131
 
3415 helixhorne 132
#if !defined LUNATIC
4698 terminx 133
#define VM_GAMEVAR_OPERATOR(func, operator)                                                                       \
134
    static inline void __fastcall func(int32_t id, int32_t lValue)                                                \
135
    {                                                                                                             \
136
        switch (aGameVars[id].dwFlags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK))                                   \
137
        {                                                                                                         \
138
            default: aGameVars[id].val.lValue operator lValue; break;                                             \
139
            case GAMEVAR_PERPLAYER:                                                                               \
140
                if (EDUKE32_PREDICT_FALSE((unsigned)vm.g_p > MAXPLAYERS - 1))                                     \
141
                    break;                                                                                        \
142
                aGameVars[id].val.plValues[vm.g_p] operator lValue;                                               \
143
                break;                                                                                            \
144
            case GAMEVAR_PERACTOR:                                                                                \
145
                if (EDUKE32_PREDICT_FALSE((unsigned)vm.g_i > MAXSPRITES - 1))                                     \
146
                    break;                                                                                        \
147
                aGameVars[id].val.plValues[vm.g_i] operator lValue;                                               \
148
                break;                                                                                            \
149
            case GAMEVAR_INTPTR: *((int32_t *)aGameVars[id].val.lValue) operator(int32_t) lValue; break;          \
150
            case GAMEVAR_SHORTPTR: *((int16_t *)aGameVars[id].val.lValue) operator(int16_t) lValue; break;        \
151
            case GAMEVAR_CHARPTR: *((uint8_t *)aGameVars[id].val.lValue) operator(uint8_t) lValue; break;         \
152
        }                                                                                                         \
153
    }
1909 terminx 154
 
4658 terminx 155
#if defined(__arm__) || defined(LIBDIVIDE_ALWAYS)
156
static inline void __fastcall Gv_DivVar(int32_t id, int32_t lValue)
157
{
158
    static libdivide_s32_t sdiv;
159
    static int32_t lastlValue;
160
    libdivide_s32_t *dptr = &sdiv;
161
    intptr_t *iptr = &aGameVars[id].val.lValue;
162
 
4698 terminx 163
    if (EDUKE32_PREDICT_FALSE((aGameVars[id].dwFlags & GAMEVAR_PERPLAYER && (unsigned)vm.g_p > MAXPLAYERS - 1) ||
164
                              (aGameVars[id].dwFlags & GAMEVAR_PERACTOR && (unsigned)vm.g_i > MAXSPRITES - 1)))
165
        return;
4658 terminx 166
 
4698 terminx 167
    if ((unsigned)lValue < DIVTABLESIZE)
4658 terminx 168
        dptr = (libdivide_s32_t *)&divtable32[lValue];
169
    else if (lValue != lastlValue)
170
        sdiv = libdivide_s32_gen(lValue), lastlValue = lValue;
171
 
4698 terminx 172
    switch (aGameVars[id].dwFlags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK))
4658 terminx 173
    {
4698 terminx 174
        case GAMEVAR_PERPLAYER: iptr = &aGameVars[id].val.plValues[vm.g_p];
175
        default: break;
176
        case GAMEVAR_PERACTOR: iptr = &aGameVars[id].val.plValues[vm.g_i]; break;
177
        case GAMEVAR_INTPTR:
178
            *((int32_t *)aGameVars[id].val.lValue) =
179
            (int32_t)libdivide_s32_do(*((int32_t *)aGameVars[id].val.lValue), dptr);
180
            return;
181
        case GAMEVAR_SHORTPTR:
182
            *((int16_t *)aGameVars[id].val.lValue) =
183
            (int16_t)libdivide_s32_do(*((int16_t *)aGameVars[id].val.lValue), dptr);
184
            return;
185
        case GAMEVAR_CHARPTR:
186
            *((uint8_t *)aGameVars[id].val.lValue) =
187
            (uint8_t)libdivide_s32_do(*((uint8_t *)aGameVars[id].val.lValue), dptr);
188
            return;
4658 terminx 189
    }
190
 
191
    *iptr = libdivide_s32_do(*iptr, dptr);
192
}
193
#else
4698 terminx 194
VM_GAMEVAR_OPERATOR(Gv_DivVar, /= )
4658 terminx 195
#endif
196
 
4698 terminx 197
VM_GAMEVAR_OPERATOR(Gv_AddVar, +=)
198
VM_GAMEVAR_OPERATOR(Gv_SubVar, -=)
199
VM_GAMEVAR_OPERATOR(Gv_MulVar, *=)
200
VM_GAMEVAR_OPERATOR(Gv_ModVar, %=)
201
VM_GAMEVAR_OPERATOR(Gv_AndVar, &=)
202
VM_GAMEVAR_OPERATOR(Gv_XorVar, ^=)
203
VM_GAMEVAR_OPERATOR(Gv_OrVar, |=)
4658 terminx 204
 
4698 terminx 205
#undef VM_GAMEVAR_OPERATOR
206
 
3415 helixhorne 207
#endif
1909 terminx 208
 
1677 terminx 209
#endif