Use references in some gamevar stuff

git-svn-id: https://svn.eduke32.com/eduke32@6853 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2018-04-23 06:36:02 +00:00
parent f903aac29f
commit 21c9f6dd7e
2 changed files with 101 additions and 107 deletions

View file

@ -623,21 +623,21 @@ int __fastcall Gv_GetVar(int gameVar, int spriteNum, int playerNum)
return *insptr++;
int invertResult = !!(gameVar & (MAXGAMEVARS << 1));
gamevar_t & var = aGameVars[gameVar &= (MAXGAMEVARS - 1)];
if ((gameVar & ~(MAXGAMEVARS << 1)) >= g_gameVarCount)
goto special;
gameVar &= (MAXGAMEVARS - 1);
int returnValue, varFlags;
varFlags = aGameVars[gameVar].flags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK);
varFlags = var.flags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK);
if (varFlags == GAMEVAR_PERACTOR)
{
if (EDUKE32_PREDICT_FALSE((unsigned) spriteNum >= MAXSPRITES)) goto badindex;
returnValue = aGameVars[gameVar].pValues[spriteNum];
returnValue = var.pValues[spriteNum];
}
else if (!varFlags) returnValue = aGameVars[gameVar].global;
else if (!varFlags) returnValue = var.global;
else if (varFlags == GAMEVAR_PERPLAYER)
{
if (EDUKE32_PREDICT_FALSE((unsigned) playerNum >= MAXPLAYERS))
@ -645,17 +645,14 @@ int __fastcall Gv_GetVar(int gameVar, int spriteNum, int playerNum)
spriteNum = playerNum;
goto badindex;
}
returnValue = aGameVars[gameVar].pValues[playerNum];
returnValue = var.pValues[playerNum];
}
else switch (varFlags)
{
case GAMEVAR_INT32PTR: returnValue = *(int32_t *)aGameVars[gameVar].global; break;
case GAMEVAR_INT16PTR: returnValue = *(int16_t *)aGameVars[gameVar].global; break;
case GAMEVAR_UINT8PTR: returnValue = *(char *)aGameVars[gameVar].global; break;
case GAMEVAR_Q16PTR:
returnValue = aGameVars[gameVar].flags & GAMEVAR_SPECIAL ? *(int32_t *)aGameVars[gameVar].global
: fix16_to_int(*(fix16_t *)aGameVars[gameVar].global);
break;
case GAMEVAR_INT32PTR: returnValue = *(int32_t *)var.global; break;
case GAMEVAR_INT16PTR: returnValue = *(int16_t *)var.global; break;
case GAMEVAR_UINT8PTR: returnValue = *(char *)var.global; break;
case GAMEVAR_Q16PTR: returnValue = var.flags & GAMEVAR_SPECIAL ? *(int32_t *)var.global : fix16_to_int(*(fix16_t *)var.global); break;
default: EDUKE32_UNREACHABLE_SECTION(returnValue = 0; break);
}
@ -779,7 +776,7 @@ badarrayindex:
return -1;
badindex:
CON_ERRPRINTF("Gv_GetVar(): invalid index %d for \"%s\"\n", spriteNum, aGameVars[gameVar].szLabel);
CON_ERRPRINTF("Gv_GetVar(): invalid index %d for \"%s\"\n", spriteNum, var.szLabel);
return -1;
}
@ -787,32 +784,31 @@ badindex:
void __fastcall Gv_SetVar(int const gameVar, int const newValue, int const spriteNum, int const playerNum)
{
int const varFlags = aGameVars[gameVar].flags & (GAMEVAR_USER_MASK|GAMEVAR_PTR_MASK);
gamevar_t & var = aGameVars[gameVar];
int const varFlags = var.flags & (GAMEVAR_USER_MASK|GAMEVAR_PTR_MASK);
if (EDUKE32_PREDICT_FALSE((unsigned)gameVar >= (unsigned)g_gameVarCount)) goto badvarid;
if (!varFlags) aGameVars[gameVar].global=newValue;
if (!varFlags) var.global=newValue;
else if (varFlags == GAMEVAR_PERPLAYER)
{
if (EDUKE32_PREDICT_FALSE((unsigned) playerNum > MAXPLAYERS-1)) goto badindex;
// for the current player
aGameVars[gameVar].pValues[playerNum]=newValue;
var.pValues[playerNum]=newValue;
}
else if (varFlags == GAMEVAR_PERACTOR)
{
if (EDUKE32_PREDICT_FALSE((unsigned) spriteNum > MAXSPRITES-1)) goto badindex;
aGameVars[gameVar].pValues[spriteNum]=newValue;
var.pValues[spriteNum]=newValue;
}
else
{
switch (varFlags)
{
case GAMEVAR_INT32PTR: *((int32_t *)aGameVars[gameVar].global) = (int32_t)newValue; break;
case GAMEVAR_INT16PTR: *((int16_t *)aGameVars[gameVar].global) = (int16_t)newValue; break;
case GAMEVAR_UINT8PTR: *((uint8_t *)aGameVars[gameVar].global) = (uint8_t)newValue; break;
case GAMEVAR_Q16PTR:
*(fix16_t *)aGameVars[gameVar].global
= aGameVars[gameVar].flags & GAMEVAR_SPECIAL ? (int32_t)newValue : fix16_from_int((int16_t)newValue);
case GAMEVAR_INT32PTR: *((int32_t *)var.global) = (int32_t)newValue; break;
case GAMEVAR_INT16PTR: *((int16_t *)var.global) = (int16_t)newValue; break;
case GAMEVAR_UINT8PTR: *((uint8_t *)var.global) = (uint8_t)newValue; break;
case GAMEVAR_Q16PTR: *(fix16_t *)var.global = var.flags & GAMEVAR_SPECIAL ? (int32_t)newValue : fix16_from_int((int16_t)newValue);
break;
}
}
@ -825,8 +821,8 @@ badvarid:
badindex:
CON_ERRPRINTF("Gv_SetVar(): invalid index (%d) for gamevar %s from sprite %d, player %d\n",
aGameVars[gameVar].flags & GAMEVAR_PERACTOR ? spriteNum : playerNum,
aGameVars[gameVar].szLabel,vm.spriteNum,vm.playerNum);
var.flags & GAMEVAR_PERACTOR ? spriteNum : playerNum,
var.szLabel,vm.spriteNum,vm.playerNum);
}
enum
@ -994,28 +990,24 @@ int __fastcall Gv_GetVarX(int gameVar)
returnValue = Gv_GetSpecialVarX(gameVar);
else
{
gameVar &= MAXGAMEVARS-1;
gamevar_t &var = aGameVars[gameVar &= MAXGAMEVARS - 1];
int const varFlags = var.flags & (GAMEVAR_USER_MASK|GAMEVAR_PTR_MASK);
int const varFlags = aGameVars[gameVar].flags & (GAMEVAR_USER_MASK|GAMEVAR_PTR_MASK);
if (!varFlags) returnValue = aGameVars[gameVar].global;
if (!varFlags) returnValue = var.global;
else if (varFlags == GAMEVAR_PERPLAYER)
{
if (EDUKE32_PREDICT_FALSE((unsigned) vm.playerNum >= MAXPLAYERS))
goto perr;
returnValue = aGameVars[gameVar].pValues[vm.playerNum];
returnValue = var.pValues[vm.playerNum];
}
else if (varFlags == GAMEVAR_PERACTOR)
returnValue = aGameVars[gameVar].pValues[vm.spriteNum];
returnValue = var.pValues[vm.spriteNum];
else switch (varFlags)
{
case GAMEVAR_INT32PTR: returnValue = (*(int32_t *)aGameVars[gameVar].global); break;
case GAMEVAR_INT16PTR: returnValue = (*(int16_t *)aGameVars[gameVar].global); break;
case GAMEVAR_UINT8PTR: returnValue = (*(uint8_t *)aGameVars[gameVar].global); break;
case GAMEVAR_Q16PTR:
returnValue = aGameVars[gameVar].flags & GAMEVAR_SPECIAL ? *(int32_t *)aGameVars[gameVar].global
: fix16_to_int(*(fix16_t *)aGameVars[gameVar].global);
break;
case GAMEVAR_INT32PTR: returnValue = (*(int32_t *)var.global); break;
case GAMEVAR_INT16PTR: returnValue = (*(int16_t *)var.global); break;
case GAMEVAR_UINT8PTR: returnValue = (*(uint8_t *)var.global); break;
case GAMEVAR_Q16PTR: returnValue = var.flags & GAMEVAR_SPECIAL ? *(int32_t *)var.global : fix16_to_int(*(fix16_t *)var.global); break;
}
}
@ -1049,30 +1041,28 @@ void __fastcall Gv_GetManyVars(int const numVars, int32_t * const outBuf)
continue;
}
gameVar &= MAXGAMEVARS - 1;
int const varFlags = aGameVars[gameVar].flags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK);
int value = aGameVars[gameVar].global;
gamevar_t &var = aGameVars[gameVar &= MAXGAMEVARS - 1];
int const varFlags = var.flags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK);
int value = var.global;
if (varFlags == GAMEVAR_PERPLAYER)
{
if (EDUKE32_PREDICT_FALSE((unsigned)vm.playerNum >= MAXPLAYERS))
goto perr;
value = aGameVars[gameVar].pValues[vm.playerNum];
value = var.pValues[vm.playerNum];
}
else if (varFlags == GAMEVAR_PERACTOR)
value = aGameVars[gameVar].pValues[vm.spriteNum];
value = var.pValues[vm.spriteNum];
else
{
switch (varFlags)
{
case GAMEVAR_INT32PTR: value = *(int32_t *)aGameVars[gameVar].global; break;
case GAMEVAR_INT16PTR: value = *(int16_t *)aGameVars[gameVar].global; break;
case GAMEVAR_UINT8PTR: value = *(uint8_t *)aGameVars[gameVar].global; break;
case GAMEVAR_Q16PTR:
value = aGameVars[gameVar].flags & GAMEVAR_SPECIAL ? *(int32_t *)aGameVars[gameVar].global
: fix16_to_int(*(fix16_t *)aGameVars[gameVar].global);
break;
case GAMEVAR_INT32PTR: value = *(int32_t *)var.global; break;
case GAMEVAR_INT16PTR: value = *(int16_t *)var.global; break;
case GAMEVAR_UINT8PTR: value = *(uint8_t *)var.global; break;
case GAMEVAR_Q16PTR: value = var.flags & GAMEVAR_SPECIAL ? *(int32_t *)var.global : fix16_to_int(*(fix16_t *)var.global); break;
}
}
@ -1086,36 +1076,34 @@ void __fastcall Gv_GetManyVars(int const numVars, int32_t * const outBuf)
void __fastcall Gv_SetVarX(int const gameVar, int const newValue)
{
int const varFlags = aGameVars[gameVar].flags & (GAMEVAR_USER_MASK|GAMEVAR_PTR_MASK);
gamevar_t & var = aGameVars[gameVar];
int const varFlags = var.flags & (GAMEVAR_USER_MASK|GAMEVAR_PTR_MASK);
if (!varFlags) aGameVars[gameVar].global = newValue;
if (!varFlags) var.global = newValue;
else if (varFlags == GAMEVAR_PERPLAYER)
{
if (EDUKE32_PREDICT_FALSE((unsigned)vm.playerNum >= MAXPLAYERS)) goto badindex;
aGameVars[gameVar].pValues[vm.playerNum] = newValue;
var.pValues[vm.playerNum] = newValue;
}
else if (varFlags == GAMEVAR_PERACTOR)
{
if (EDUKE32_PREDICT_FALSE((unsigned)vm.spriteNum >= MAXSPRITES)) goto badindex;
aGameVars[gameVar].pValues[vm.spriteNum] = newValue;
var.pValues[vm.spriteNum] = newValue;
}
else switch (varFlags)
{
case GAMEVAR_INT32PTR: *(int32_t *)aGameVars[gameVar].global = (int32_t)newValue; break;
case GAMEVAR_INT16PTR: *(int16_t *)aGameVars[gameVar].global = (int16_t)newValue; break;
case GAMEVAR_UINT8PTR: *(uint8_t *)aGameVars[gameVar].global = (uint8_t)newValue; break;
case GAMEVAR_Q16PTR:
*(fix16_t *)aGameVars[gameVar].global
= aGameVars[gameVar].flags & GAMEVAR_SPECIAL ? (int32_t)newValue : fix16_from_int((int16_t)newValue);
break;
case GAMEVAR_INT32PTR: *(int32_t *)var.global = (int32_t)newValue; break;
case GAMEVAR_INT16PTR: *(int16_t *)var.global = (int16_t)newValue; break;
case GAMEVAR_UINT8PTR: *(uint8_t *)var.global = (uint8_t)newValue; break;
case GAMEVAR_Q16PTR: *(fix16_t *)var.global = var.flags & GAMEVAR_SPECIAL ? (int32_t)newValue : fix16_from_int((int16_t)newValue); break;
}
return;
badindex:
CON_ERRPRINTF("Gv_SetVar(): invalid index (%d) for gamevar %s\n",
aGameVars[gameVar].flags & GAMEVAR_PERACTOR ? vm.spriteNum : vm.playerNum,
aGameVars[gameVar].szLabel);
var.flags & GAMEVAR_PERACTOR ? vm.spriteNum : vm.playerNum,
var.szLabel);
}
int Gv_GetVarByLabel(const char *szGameLabel, int const defaultValue, int const spriteNum, int const playerNum)
@ -1131,14 +1119,16 @@ static intptr_t *Gv_GetVarDataPtr(const char *szGameLabel)
if (EDUKE32_PREDICT_FALSE(gameVar < 0))
return NULL;
if (aGameVars[gameVar].flags & (GAMEVAR_PERACTOR | GAMEVAR_PERPLAYER))
gamevar_t & var = aGameVars[gameVar];
if (var.flags & (GAMEVAR_PERACTOR | GAMEVAR_PERPLAYER))
{
if (EDUKE32_PREDICT_FALSE(!aGameVars[gameVar].pValues))
if (EDUKE32_PREDICT_FALSE(!var.pValues))
CON_ERRPRINTF("Gv_GetVarDataPtr(): INTERNAL ERROR: NULL array !!!\n");
return aGameVars[gameVar].pValues;
return var.pValues;
}
return &(aGameVars[gameVar].global);
return &(var.global);
}
#endif // !defined LUNATIC

View file

@ -161,25 +161,27 @@ void Gv_FinalizeWeaponDefaults(void);
#define VM_GAMEVAR_OPERATOR(func, operator) \
static FORCE_INLINE void __fastcall func(int const id, int32_t const operand) \
{ \
switch (aGameVars[id].flags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK)) \
gamevar_t &var = aGameVars[id]; \
\
switch (var.flags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK)) \
{ \
default: aGameVars[id].global operator operand; break; \
default: var.global operator operand; break; \
case GAMEVAR_PERPLAYER: \
if (EDUKE32_PREDICT_FALSE((unsigned)vm.playerNum > MAXPLAYERS - 1)) \
break; \
aGameVars[id].pValues[vm.playerNum] operator operand; \
var.pValues[vm.playerNum] operator operand; \
break; \
case GAMEVAR_PERACTOR: \
if (EDUKE32_PREDICT_FALSE((unsigned)vm.spriteNum > MAXSPRITES - 1)) \
break; \
aGameVars[id].pValues[vm.spriteNum] operator operand; \
var.pValues[vm.spriteNum] operator operand; \
break; \
case GAMEVAR_INT32PTR: *(int32_t *)aGameVars[id].global operator(int32_t) operand; break; \
case GAMEVAR_INT16PTR: *(int16_t *)aGameVars[id].global operator(int16_t) operand; break; \
case GAMEVAR_UINT8PTR: *(uint8_t *)aGameVars[id].global operator(uint8_t) operand; break; \
case GAMEVAR_INT32PTR: *(int32_t *)var.global operator(int32_t) operand; break; \
case GAMEVAR_INT16PTR: *(int16_t *)var.global operator(int16_t) operand; break; \
case GAMEVAR_UINT8PTR: *(uint8_t *)var.global operator(uint8_t) operand; break; \
case GAMEVAR_Q16PTR: \
{ \
Fix16 *pfix = (Fix16 *)aGameVars[id].global; \
Fix16 *pfix = (Fix16 *)var.global; \
*pfix operator(int16_t) operand; \
break; \
} \
@ -189,13 +191,15 @@ void Gv_FinalizeWeaponDefaults(void);
#if defined(__arm__) || defined(LIBDIVIDE_ALWAYS)
static FORCE_INLINE void __fastcall Gv_DivVar(int const id, int32_t const operand)
{
if (EDUKE32_PREDICT_FALSE((aGameVars[id].flags & GAMEVAR_PERPLAYER && (unsigned) vm.playerNum > MAXPLAYERS - 1) ||
(aGameVars[id].flags & GAMEVAR_PERACTOR && (unsigned) vm.spriteNum > MAXSPRITES - 1)))
gamevar_t &var = aGameVars[id];
if (EDUKE32_PREDICT_FALSE((var.flags & GAMEVAR_PERPLAYER && (unsigned) vm.playerNum > MAXPLAYERS - 1) ||
(var.flags & GAMEVAR_PERACTOR && (unsigned) vm.spriteNum > MAXSPRITES - 1)))
return;
static libdivide_s32_t sdiv;
static int32_t lastValue;
intptr_t *iptr = &aGameVars[id].global;
intptr_t *iptr = &var.global;
bool const foundInTable = (unsigned) operand < DIVTABLESIZE;
libdivide_s32_t *dptr = foundInTable ? (libdivide_s32_t *) &divtable32[operand] : &sdiv;
@ -205,33 +209,33 @@ static FORCE_INLINE void __fastcall Gv_DivVar(int const id, int32_t const operan
sdiv = libdivide_s32_gen((lastValue = operand));
skip:
switch (aGameVars[id].flags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK))
switch (var.flags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK))
{
case GAMEVAR_PERPLAYER: iptr = &aGameVars[id].pValues[vm.playerNum];
case GAMEVAR_PERPLAYER: iptr = &var.pValues[vm.playerNum];
default: break;
case GAMEVAR_PERACTOR: iptr = &aGameVars[id].pValues[vm.spriteNum]; break;
case GAMEVAR_PERACTOR: iptr = &var.pValues[vm.spriteNum]; break;
case GAMEVAR_INT32PTR:
{
int32_t & var = *(int32_t *)aGameVars[id].global;
var = (int32_t)libdivide_s32_do(var, dptr);
int32_t & value = *(int32_t *)var.global;
value = (int32_t)libdivide_s32_do(value, dptr);
return;
}
case GAMEVAR_INT16PTR:
{
int16_t & var = *(int16_t *)aGameVars[id].global;
var = (int16_t)libdivide_s32_do(var, dptr);
int16_t & value = *(int16_t *)var.global;
value = (int16_t)libdivide_s32_do(value, dptr);
return;
}
case GAMEVAR_UINT8PTR:
{
uint8_t & var = *(uint8_t *)aGameVars[id].global;
var = (uint8_t)libdivide_s32_do(var, dptr);
uint8_t & value = *(uint8_t *)var.global;
value = (uint8_t)libdivide_s32_do(value, dptr);
return;
}
case GAMEVAR_Q16PTR:
{
fix16_t & var = *(fix16_t *)aGameVars[id].global;
var = fix16_div(var, fix16_from_int(operand));
fix16_t & value = *(fix16_t *)var.global;
value = fix16_div(value, fix16_from_int(operand));
return;
}
}