Subversion Repositories eduke32

Compare Revisions

Ignore whitespace Rev 4697 → Rev 4698

/polymer/eduke32/source/gamevars.c
30,6 → 30,9
 
#define _gamevars_c_
 
#define PLAYER_VAR_ALIGNMENT (sizeof(intptr_t))
#define ACTOR_VAR_ALIGNMENT 16
 
#ifdef LUNATIC
int32_t g_noResetVars;
LUNATIC_CB void (*A_ResetVars)(int32_t iActor);
41,13 → 44,10
// call this function as many times as needed.
int32_t i;
 
for (i=MAXGAMEVARS-1; i>=0; i--)
for (i=0; i<g_gameVarCount; i++)
{
if ((aGameVars[i].dwFlags & GAMEVAR_USER_MASK) && aGameVars[i].val.plValues)
{
Bfree(aGameVars[i].val.plValues);
aGameVars[i].val.plValues=NULL;
}
if (aGameVars[i].dwFlags & GAMEVAR_USER_MASK)
ALIGNED_FREE_AND_NULL(aGameVars[i].val.plValues);
 
aGameVars[i].dwFlags |= GAMEVAR_RESET;
 
54,11 → 54,8
if (i >= MAXGAMEARRAYS)
continue;
 
if ((aGameArrays[i].dwFlags & GAMEARRAY_NORMAL) && aGameArrays[i].plValues)
{
Bfree(aGameArrays[i].plValues);
aGameArrays[i].plValues=NULL;
}
if (aGameArrays[i].dwFlags & GAMEARRAY_NORMAL)
ALIGNED_FREE_AND_NULL(aGameArrays[i].plValues);
 
aGameArrays[i].dwFlags |= GAMEARRAY_RESET;
}
76,10 → 73,9
Gv_Free();
 
// Now, only do work that Gv_Free() hasn't done.
for (i=MAXGAMEVARS-1; i>=0; i--)
for (i=0; i<g_gameVarCount; i++)
{
Bfree(aGameVars[i].szLabel);
aGameVars[i].szLabel=NULL;
DO_FREE_AND_NULL(aGameVars[i].szLabel);
 
aGameVars[i].val.lValue=0;
 
86,8 → 82,7
if (i >= MAXGAMEARRAYS)
continue;
 
Bfree(aGameArrays[i].szLabel);
aGameArrays[i].szLabel=NULL;
DO_FREE_AND_NULL(aGameArrays[i].szLabel);
}
}
 
116,18 → 111,18
for (i=0; i<g_gameVarCount; i++)
{
if (kdfread(&(aGameVars[i]),sizeof(gamevar_t),1,fil) != 1) goto corrupt;
aGameVars[i].szLabel = (char *)Xcalloc(MAXVARLABEL,sizeof(uint8_t));
aGameVars[i].szLabel = (char *)Xmalloc(MAXVARLABEL * sizeof(uint8_t));
if (kdfread(aGameVars[i].szLabel,sizeof(uint8_t) * MAXVARLABEL, 1, fil) != 1) goto corrupt;
hash_add(&h_gamevars, aGameVars[i].szLabel,i, 1);
 
if (aGameVars[i].dwFlags & GAMEVAR_PERPLAYER)
{
aGameVars[i].val.plValues = (intptr_t*)Xcalloc(MAXPLAYERS,sizeof(intptr_t));
aGameVars[i].val.plValues = (intptr_t*)Xaligned_alloc(PLAYER_VAR_ALIGNMENT, MAXPLAYERS * sizeof(intptr_t));
if (kdfread(aGameVars[i].val.plValues,sizeof(intptr_t) * MAXPLAYERS, 1, fil) != 1) goto corrupt;
}
else if (aGameVars[i].dwFlags & GAMEVAR_PERACTOR)
{
aGameVars[i].val.plValues = (intptr_t*)Xcalloc(MAXSPRITES,sizeof(intptr_t));
aGameVars[i].val.plValues = (intptr_t*)Xaligned_alloc(ACTOR_VAR_ALIGNMENT, MAXSPRITES * sizeof(intptr_t));
if (kdfread(&aGameVars[i].val.plValues[0],sizeof(intptr_t), MAXSPRITES, fil) != MAXSPRITES) goto corrupt;
}
}
149,11 → 144,11
// read for .size and .dwFlags (the rest are pointers):
if (kdfread(&aGameArrays[i],sizeof(gamearray_t),1,fil) != 1) goto corrupt;
 
aGameArrays[i].szLabel = (char *)Xcalloc(MAXARRAYLABEL,sizeof(uint8_t));
aGameArrays[i].szLabel = (char *)Xmalloc(MAXARRAYLABEL * sizeof(uint8_t));
if (kdfread(aGameArrays[i].szLabel,sizeof(uint8_t) * MAXARRAYLABEL, 1, fil) != 1) goto corrupt;
hash_add(&h_arrays, aGameArrays[i].szLabel, i, 1);
 
aGameArrays[i].plValues = (intptr_t *)Xcalloc(aGameArrays[i].size, GAR_ELTSZ);
aGameArrays[i].plValues = (intptr_t *)Xaligned_alloc(ACTOR_VAR_ALIGNMENT, aGameArrays[i].size * GAR_ELTSZ);
if (kdfread(aGameArrays[i].plValues, GAR_ELTSZ * aGameArrays[i].size, 1, fil) < 1) goto corrupt;
}
 
172,7 → 167,7
if (savedstate[i])
{
if (MapInfo[i].savedstate == NULL)
MapInfo[i].savedstate = (mapstate_t *)Xcalloc(1,sizeof(mapstate_t));
MapInfo[i].savedstate = (mapstate_t *)Xaligned_alloc(16, sizeof(mapstate_t));
if (kdfread(MapInfo[i].savedstate,sizeof(mapstate_t),1,fil) != sizeof(mapstate_t)) goto corrupt;
for (j=0; j<g_gameVarCount; j++)
{
180,13 → 175,13
if (aGameVars[j].dwFlags & GAMEVAR_PERPLAYER)
{
// if (!MapInfo[i].savedstate->vars[j])
MapInfo[i].savedstate->vars[j] = (intptr_t *)Xcalloc(MAXPLAYERS,sizeof(intptr_t));
MapInfo[i].savedstate->vars[j] = (intptr_t *)Xaligned_alloc(PLAYER_VAR_ALIGNMENT, MAXPLAYERS * sizeof(intptr_t));
if (kdfread(&MapInfo[i].savedstate->vars[j][0],sizeof(intptr_t) * MAXPLAYERS, 1, fil) != 1) goto corrupt;
}
else if (aGameVars[j].dwFlags & GAMEVAR_PERACTOR)
{
// if (!MapInfo[i].savedstate->vars[j])
MapInfo[i].savedstate->vars[j] = (intptr_t *)Xcalloc(MAXSPRITES,sizeof(intptr_t));
MapInfo[i].savedstate->vars[j] = (intptr_t *)Xaligned_alloc(ACTOR_VAR_ALIGNMENT, MAXSPRITES * sizeof(intptr_t));
if (kdfread(&MapInfo[i].savedstate->vars[j][0],sizeof(intptr_t), MAXSPRITES, fil) != MAXSPRITES) goto corrupt;
}
}
426,11 → 421,15
 
if (aGameArrays[i].szLabel == NULL)
aGameArrays[i].szLabel=(char *)Xcalloc(MAXVARLABEL,sizeof(uint8_t));
 
if (aGameArrays[i].szLabel != pszLabel)
Bstrcpy(aGameArrays[i].szLabel,pszLabel);
 
if (!(dwFlags & GAMEARRAY_TYPE_MASK))
aGameArrays[i].plValues=(intptr_t *)Xcalloc(asize,GAR_ELTSZ);
{
aGameArrays[i].plValues = (intptr_t *)Xaligned_alloc(ACTOR_VAR_ALIGNMENT, asize * GAR_ELTSZ);
Bmemset(aGameArrays[i].plValues, 0, asize * GAR_ELTSZ);
}
else
aGameArrays[i].plValues=(intptr_t *)arrayptr;
 
495,6 → 494,7
// Allocate and set its label
if (aGameVars[i].szLabel == NULL)
aGameVars[i].szLabel = (char *)Xcalloc(MAXVARLABEL,sizeof(uint8_t));
 
if (aGameVars[i].szLabel != pszLabel)
Bstrcpy(aGameVars[i].szLabel,pszLabel);
 
501,12 → 501,9
// and the flags
aGameVars[i].dwFlags=dwFlags;
 
// only free if per-{actor,player}
if (aGameVars[i].dwFlags & GAMEVAR_USER_MASK)
{
// only free if per-{actor,player}
Bfree(aGameVars[i].val.plValues);
aGameVars[i].val.plValues=NULL;
}
ALIGNED_FREE_AND_NULL(aGameVars[i].val.plValues);
}
 
// if existing is system, they only get to change default value....
523,7 → 520,10
if (aGameVars[i].dwFlags & GAMEVAR_PERPLAYER)
{
if (!aGameVars[i].val.plValues)
aGameVars[i].val.plValues = (intptr_t *)Xcalloc(MAXPLAYERS,sizeof(intptr_t));
{
aGameVars[i].val.plValues = (intptr_t *) Xaligned_alloc(PLAYER_VAR_ALIGNMENT, MAXPLAYERS * sizeof(intptr_t));
Bmemset(aGameVars[i].val.plValues, 0, MAXPLAYERS * sizeof(intptr_t));
}
for (j=MAXPLAYERS-1; j>=0; j--)
aGameVars[i].val.plValues[j]=lValue;
}
530,7 → 530,10
else if (aGameVars[i].dwFlags & GAMEVAR_PERACTOR)
{
if (!aGameVars[i].val.plValues)
aGameVars[i].val.plValues = (intptr_t *)Xcalloc(MAXSPRITES,sizeof(intptr_t));
{
aGameVars[i].val.plValues = (intptr_t *) Xaligned_alloc(ACTOR_VAR_ALIGNMENT, MAXSPRITES * sizeof(intptr_t));
Bmemset(aGameVars[i].val.plValues, 0, MAXSPRITES * sizeof(intptr_t));
}
for (j=MAXSPRITES-1; j>=0; j--)
aGameVars[i].val.plValues[j]=lValue;
}
539,17 → 542,6
return 1;
}
 
void __fastcall A_ResetVars(int32_t iActor)
{
int32_t i=(MAXGAMEVARS-1);
do
{
if ((aGameVars[i].dwFlags & (GAMEVAR_PERACTOR|GAMEVAR_NODEFAULT)) == GAMEVAR_PERACTOR)
aGameVars[i].val.plValues[iActor]=aGameVars[i].lDefault;
}
while (i--);
}
 
static int32_t Gv_GetVarIndex(const char *szGameLabel)
{
int32_t i = hash_find(&h_gamevars,szGameLabel);
/polymer/eduke32/source/gamevars.h
104,7 → 104,16
int32_t Gv_GetVarByLabel(const char *szGameLabel,int32_t lDefault,int32_t iActor,int32_t iPlayer);
int32_t Gv_NewArray(const char *pszLabel,void *arrayptr,intptr_t asize,uint32_t dwFlags);
int32_t Gv_NewVar(const char *pszLabel,intptr_t lValue,uint32_t dwFlags);
void __fastcall A_ResetVars(int32_t iActor);
static inline void A_ResetVars(int32_t iActor)
{
int32_t i;
 
for (i=0; i<g_gameVarCount; i++)
{
if ((aGameVars[i].dwFlags & (GAMEVAR_PERACTOR|GAMEVAR_NODEFAULT)) == GAMEVAR_PERACTOR)
aGameVars[i].val.plValues[iActor]=aGameVars[i].lDefault;
}
}
void Gv_DumpValues(void);
void Gv_InitWeaponPointers(void);
void Gv_RefreshPointers(void);
121,35 → 130,28
void Gv_FinalizeWeaponDefaults(void);
 
#if !defined LUNATIC
#define GV_VAROP(func, operator) static inline void __fastcall func(int32_t id, int32_t lValue) \
{ \
switch (aGameVars[id].dwFlags & (GAMEVAR_USER_MASK|GAMEVAR_PTR_MASK)) \
{ \
default: \
aGameVars[id].val.lValue operator lValue; \
break; \
case GAMEVAR_PERPLAYER: \
if (EDUKE32_PREDICT_FALSE((unsigned)vm.g_p > MAXPLAYERS-1)) break; \
aGameVars[id].val.plValues[vm.g_p] operator lValue; \
break; \
case GAMEVAR_PERACTOR: \
if (EDUKE32_PREDICT_FALSE((unsigned)vm.g_i > MAXSPRITES-1)) break; \
aGameVars[id].val.plValues[vm.g_i] operator lValue; \
break; \
case GAMEVAR_INTPTR: \
*((int32_t *)aGameVars[id].val.lValue) operator (int32_t)lValue; \
break; \
case GAMEVAR_SHORTPTR: \
*((int16_t *)aGameVars[id].val.lValue) operator (int16_t)lValue; \
break; \
case GAMEVAR_CHARPTR: \
*((uint8_t *)aGameVars[id].val.lValue) operator (uint8_t)lValue; \
break; \
} \
}
#define VM_GAMEVAR_OPERATOR(func, operator) \
static inline void __fastcall func(int32_t id, int32_t lValue) \
{ \
switch (aGameVars[id].dwFlags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK)) \
{ \
default: aGameVars[id].val.lValue operator lValue; break; \
case GAMEVAR_PERPLAYER: \
if (EDUKE32_PREDICT_FALSE((unsigned)vm.g_p > MAXPLAYERS - 1)) \
break; \
aGameVars[id].val.plValues[vm.g_p] operator lValue; \
break; \
case GAMEVAR_PERACTOR: \
if (EDUKE32_PREDICT_FALSE((unsigned)vm.g_i > MAXSPRITES - 1)) \
break; \
aGameVars[id].val.plValues[vm.g_i] operator lValue; \
break; \
case GAMEVAR_INTPTR: *((int32_t *)aGameVars[id].val.lValue) operator(int32_t) lValue; break; \
case GAMEVAR_SHORTPTR: *((int16_t *)aGameVars[id].val.lValue) operator(int16_t) lValue; break; \
case GAMEVAR_CHARPTR: *((uint8_t *)aGameVars[id].val.lValue) operator(uint8_t) lValue; break; \
} \
}
 
// even though libdivide is faster than straight division (when using the LUT) the overhead makes this slower on x86
// ARM, however, has no hardware integer division
#if defined(__arm__) || defined(LIBDIVIDE_ALWAYS)
static inline void __fastcall Gv_DivVar(int32_t id, int32_t lValue)
{
158,48 → 160,50
libdivide_s32_t *dptr = &sdiv;
intptr_t *iptr = &aGameVars[id].val.lValue;
 
if (EDUKE32_PREDICT_FALSE((aGameVars[id].dwFlags & GAMEVAR_PERPLAYER && (unsigned) vm.g_p > MAXPLAYERS-1) ||
(aGameVars[id].dwFlags & GAMEVAR_PERACTOR && (unsigned) vm.g_i > MAXSPRITES-1))) return;
if (EDUKE32_PREDICT_FALSE((aGameVars[id].dwFlags & GAMEVAR_PERPLAYER && (unsigned)vm.g_p > MAXPLAYERS - 1) ||
(aGameVars[id].dwFlags & GAMEVAR_PERACTOR && (unsigned)vm.g_i > MAXSPRITES - 1)))
return;
 
if ((unsigned) lValue < DIVTABLESIZE)
if ((unsigned)lValue < DIVTABLESIZE)
dptr = (libdivide_s32_t *)&divtable32[lValue];
else if (lValue != lastlValue)
sdiv = libdivide_s32_gen(lValue), lastlValue = lValue;
 
switch (aGameVars[id].dwFlags & (GAMEVAR_USER_MASK|GAMEVAR_PTR_MASK))
switch (aGameVars[id].dwFlags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK))
{
case GAMEVAR_PERPLAYER:
iptr = &aGameVars[id].val.plValues[vm.g_p];
default:
break;
case GAMEVAR_PERACTOR:
iptr = &aGameVars[id].val.plValues[vm.g_i];
break;
case GAMEVAR_INTPTR:
*((int32_t *) aGameVars[id].val.lValue) = (int32_t) libdivide_s32_do(*((int32_t *) aGameVars[id].val.lValue), dptr);
return;
case GAMEVAR_SHORTPTR:
*((int16_t *) aGameVars[id].val.lValue) = (int16_t) libdivide_s32_do(*((int16_t *) aGameVars[id].val.lValue), dptr);
return;
case GAMEVAR_CHARPTR:
*((uint8_t *) aGameVars[id].val.lValue) = (uint8_t) libdivide_s32_do(*((uint8_t *) aGameVars[id].val.lValue), dptr);
return;
case GAMEVAR_PERPLAYER: iptr = &aGameVars[id].val.plValues[vm.g_p];
default: break;
case GAMEVAR_PERACTOR: iptr = &aGameVars[id].val.plValues[vm.g_i]; break;
case GAMEVAR_INTPTR:
*((int32_t *)aGameVars[id].val.lValue) =
(int32_t)libdivide_s32_do(*((int32_t *)aGameVars[id].val.lValue), dptr);
return;
case GAMEVAR_SHORTPTR:
*((int16_t *)aGameVars[id].val.lValue) =
(int16_t)libdivide_s32_do(*((int16_t *)aGameVars[id].val.lValue), dptr);
return;
case GAMEVAR_CHARPTR:
*((uint8_t *)aGameVars[id].val.lValue) =
(uint8_t)libdivide_s32_do(*((uint8_t *)aGameVars[id].val.lValue), dptr);
return;
}
 
*iptr = libdivide_s32_do(*iptr, dptr);
}
#else
GV_VAROP(Gv_DivVar, /=)
VM_GAMEVAR_OPERATOR(Gv_DivVar, /= )
#endif
 
GV_VAROP(Gv_AddVar, +=)
GV_VAROP(Gv_SubVar, -=)
GV_VAROP(Gv_MulVar, *=)
GV_VAROP(Gv_ModVar, %=)
GV_VAROP(Gv_AndVar, &=)
GV_VAROP(Gv_XorVar, ^=)
GV_VAROP(Gv_OrVar, |=)
VM_GAMEVAR_OPERATOR(Gv_AddVar, +=)
VM_GAMEVAR_OPERATOR(Gv_SubVar, -=)
VM_GAMEVAR_OPERATOR(Gv_MulVar, *=)
VM_GAMEVAR_OPERATOR(Gv_ModVar, %=)
VM_GAMEVAR_OPERATOR(Gv_AndVar, &=)
VM_GAMEVAR_OPERATOR(Gv_XorVar, ^=)
VM_GAMEVAR_OPERATOR(Gv_OrVar, |=)
 
#undef VM_GAMEVAR_OPERATOR
 
#endif
 
#endif