gamedef.cpp and friends: use preprocessor defines instead of hard-coded values for gamevar IDs that are really constants or arrays or structs

git-svn-id: https://svn.eduke32.com/eduke32@7197 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2018-11-18 18:10:57 +00:00
parent 2c3ba0fdb6
commit 34c6fcc69f
4 changed files with 94 additions and 90 deletions

View file

@ -1799,7 +1799,10 @@ static int32_t parse_hex_constant(const char *hexnum)
static void C_GetNextVarType(int32_t type) static void C_GetNextVarType(int32_t type)
{ {
int32_t i=0,f=0; int32_t id = 0;
int32_t flags = 0;
auto varptr = g_scriptPtr;
C_SkipComments(); C_SkipComments();
@ -1807,7 +1810,7 @@ static void C_GetNextVarType(int32_t type)
{ {
BITPTR_CLEAR(g_scriptPtr-apScript); BITPTR_CLEAR(g_scriptPtr-apScript);
*g_scriptPtr++ = MAXGAMEVARS; *g_scriptPtr++ = GV_FLAG_CONSTANT;
if (tolower(textptr[1])=='x') // hex constants if (tolower(textptr[1])=='x') // hex constants
*g_scriptPtr = parse_hex_constant(textptr+2); *g_scriptPtr = parse_hex_constant(textptr+2);
@ -1839,8 +1842,8 @@ static void C_GetNextVarType(int32_t type)
if (!(g_errorCnt || g_warningCnt) && g_scriptDebug) if (!(g_errorCnt || g_warningCnt) && g_scriptDebug)
initprintf("%s:%d: debug: flagging gamevar as negative.\n", g_scriptFileName, g_lineNumber); //,Batol(textptr)); initprintf("%s:%d: debug: flagging gamevar as negative.\n", g_scriptFileName, g_lineNumber); //,Batol(textptr));
f = (MAXGAMEVARS<<1);
flags = GV_FLAG_NEGATIVE;
textptr++; textptr++;
} }
@ -1853,39 +1856,40 @@ static void C_GetNextVarType(int32_t type)
return; return;
} }
C_SkipComments(); //skip comments and whitespace C_SkipComments();
if (*textptr == '[') //read of array as a gamevar if (*textptr == '[') //read of array as a gamevar
{ {
f |= (MAXGAMEVARS<<2); flags |= GV_FLAG_ARRAY;
textptr++; textptr++;
i=GetADefID(label+(g_labelCnt<<6)); id=GetADefID(label+(g_labelCnt<<6));
if (i < 0)
{
i=GetDefID(label+(g_labelCnt<<6));
if ((unsigned) (i - g_structVarIDs) >= NUMQUICKSTRUCTS)
i = -1;
if (EDUKE32_PREDICT_FALSE(i < 0)) if (id < 0)
{
id=GetDefID(label+(g_labelCnt<<6));
if ((unsigned) (id - g_structVarIDs) >= NUMQUICKSTRUCTS)
id = -1;
if (EDUKE32_PREDICT_FALSE(id < 0))
{ {
g_errorCnt++; g_errorCnt++;
C_ReportError(ERROR_NOTAGAMEARRAY); C_ReportError(ERROR_NOTAGAMEARRAY);
return; return;
} }
f &= ~(MAXGAMEVARS<<2); // not an array
f |= (MAXGAMEVARS<<3); flags &= ~GV_FLAG_ARRAY; // not an array
flags |= GV_FLAG_STRUCT;
} }
BITPTR_CLEAR(g_scriptPtr-apScript); BITPTR_CLEAR(g_scriptPtr-apScript);
*g_scriptPtr++=(i|f); *g_scriptPtr++=(id|flags);
if ((f & (MAXGAMEVARS<<3)) && i - g_structVarIDs == STRUCT_USERDEF) if ((flags & GV_FLAG_STRUCT) && id - g_structVarIDs == STRUCT_USERDEF)
{ {
// userdef doesn't really have an array index // userdef doesn't really have an array index
while (*textptr != ']') while (*textptr != ']')
{ {
if (*textptr == 0xa) if (*textptr == 0xa || *textptr == 0)
break;
if (!*textptr)
break; break;
textptr++; textptr++;
@ -1924,7 +1928,7 @@ static void C_GetNextVarType(int32_t type)
return; return;
} }
if (f & (MAXGAMEVARS<<3)) if (flags & GV_FLAG_STRUCT)
{ {
while (*textptr != '.') while (*textptr != '.')
{ {
@ -1949,7 +1953,7 @@ static void C_GetNextVarType(int32_t type)
int32_t labelNum = -1; int32_t labelNum = -1;
switch (i - g_structVarIDs) switch (id - g_structVarIDs)
{ {
case STRUCT_SPRITE: case STRUCT_SPRITE:
labelNum=C_GetLabelNameOffset(&h_actor,Bstrtolower(label+(g_labelCnt<<6))); labelNum=C_GetLabelNameOffset(&h_actor,Bstrtolower(label+(g_labelCnt<<6)));
@ -1994,7 +1998,7 @@ static void C_GetNextVarType(int32_t type)
BITPTR_CLEAR(g_scriptPtr-apScript); BITPTR_CLEAR(g_scriptPtr-apScript);
switch (i - g_structVarIDs) switch (id - g_structVarIDs)
{ {
case STRUCT_SPRITE: case STRUCT_SPRITE:
*g_scriptPtr++=ActorLabels[labelNum].lId; *g_scriptPtr++=ActorLabels[labelNum].lId;
@ -2045,44 +2049,39 @@ static void C_GetNextVarType(int32_t type)
return; return;
} }
// initprintf("not an array"); // initprintf("not an array");
i=GetDefID(label+(g_labelCnt<<6)); id=GetDefID(label+(g_labelCnt<<6));
if (i<0) //gamevar not found if (id<0) //gamevar not found
{ {
if (!type && !g_labelsOnly) if (!type && !g_labelsOnly)
{ {
//try looking for a define instead //try looking for a define instead
Bstrcpy(tempbuf,label+(g_labelCnt<<6)); Bstrcpy(tempbuf,label+(g_labelCnt<<6));
i = hash_find(&h_labels,tempbuf); id = hash_find(&h_labels,tempbuf);
if (EDUKE32_PREDICT_TRUE(i>=0))
{ if (EDUKE32_PREDICT_TRUE(id>=0 && labeltype[id] & LABEL_DEFINE))
if (EDUKE32_PREDICT_TRUE(labeltype[i] & LABEL_DEFINE))
{ {
if (!(g_errorCnt || g_warningCnt) && g_scriptDebug) if (!(g_errorCnt || g_warningCnt) && g_scriptDebug)
initprintf("%s:%d: debug: label `%s' in place of gamevar.\n",g_scriptFileName,g_lineNumber,label+(i<<6)); initprintf("%s:%d: debug: label `%s' in place of gamevar.\n",g_scriptFileName,g_lineNumber,label+(id<<6));
BITPTR_CLEAR(g_scriptPtr-apScript); BITPTR_CLEAR(g_scriptPtr-apScript);
*g_scriptPtr++ = MAXGAMEVARS; *g_scriptPtr++ = GV_FLAG_CONSTANT;
BITPTR_CLEAR(g_scriptPtr-apScript); BITPTR_CLEAR(g_scriptPtr-apScript);
*g_scriptPtr++ = labelcode[i]; *g_scriptPtr++ = labelcode[id];
return; return;
} }
} }
g_errorCnt++;
C_ReportError(ERROR_NOTAGAMEVAR);
return;
}
g_errorCnt++;
C_ReportError(ERROR_NOTAGAMEVAR);
textptr++;
return;
g_errorCnt++;
C_ReportError(ERROR_NOTAGAMEVAR);
return;
} }
if (EDUKE32_PREDICT_FALSE(type == GAMEVAR_READONLY && aGameVars[i].flags & GAMEVAR_READONLY))
if (EDUKE32_PREDICT_FALSE(type == GAMEVAR_READONLY && aGameVars[id].flags & GAMEVAR_READONLY))
{ {
g_errorCnt++; g_errorCnt++;
C_ReportError(ERROR_VARREADONLY); C_ReportError(ERROR_VARREADONLY);
return; return;
} }
else if (EDUKE32_PREDICT_FALSE(aGameVars[i].flags & type)) else if (EDUKE32_PREDICT_FALSE(aGameVars[id].flags & type))
{ {
g_errorCnt++; g_errorCnt++;
C_ReportError(ERROR_VARTYPEMISMATCH); C_ReportError(ERROR_VARTYPEMISMATCH);
@ -2093,7 +2092,7 @@ static void C_GetNextVarType(int32_t type)
initprintf("%s:%d: debug: gamevar `%s'.\n",g_scriptFileName,g_lineNumber,label+(g_labelCnt<<6)); initprintf("%s:%d: debug: gamevar `%s'.\n",g_scriptFileName,g_lineNumber,label+(g_labelCnt<<6));
BITPTR_CLEAR(g_scriptPtr-apScript); BITPTR_CLEAR(g_scriptPtr-apScript);
*g_scriptPtr++=(i|f); *g_scriptPtr++=(id|flags);
} }
#define C_GetNextVar() C_GetNextVarType(0) #define C_GetNextVar() C_GetNextVarType(0)
@ -3697,7 +3696,7 @@ DO_DEFSTATE:
BITPTR_CLEAR(previous_event_end-apScript); BITPTR_CLEAR(previous_event_end-apScript);
*(previous_event_end++) = CON_JUMP | (g_lineNumber << 12); *(previous_event_end++) = CON_JUMP | (g_lineNumber << 12);
BITPTR_CLEAR(previous_event_end-apScript); BITPTR_CLEAR(previous_event_end-apScript);
*(previous_event_end++) = MAXGAMEVARS; *(previous_event_end++) = GV_FLAG_CONSTANT;
C_FillEventBreakStackWithJump((intptr_t *)*previous_event_end, g_parsingEventPtr); C_FillEventBreakStackWithJump((intptr_t *)*previous_event_end, g_parsingEventPtr);
BITPTR_CLEAR(previous_event_end-apScript); BITPTR_CLEAR(previous_event_end-apScript);
*(previous_event_end++) = g_parsingEventPtr; *(previous_event_end++) = g_parsingEventPtr;
@ -6120,7 +6119,7 @@ repeatcase:
BITPTR_CLEAR(g_scriptPtr-apScript); BITPTR_CLEAR(g_scriptPtr-apScript);
*(g_scriptPtr++) = CON_JUMP | (g_lineNumber << 12); *(g_scriptPtr++) = CON_JUMP | (g_lineNumber << 12);
BITPTR_CLEAR(g_scriptPtr-apScript); BITPTR_CLEAR(g_scriptPtr-apScript);
*(g_scriptPtr++) = MAXGAMEVARS; *(g_scriptPtr++) = GV_FLAG_CONSTANT;
BITPTR_CLEAR(g_scriptPtr-apScript); BITPTR_CLEAR(g_scriptPtr-apScript);
*(g_scriptPtr++) = previous_event; *(g_scriptPtr++) = previous_event;
BITPTR_CLEAR(g_scriptPtr-apScript); BITPTR_CLEAR(g_scriptPtr-apScript);
@ -6193,7 +6192,7 @@ repeatcase:
BITPTR_CLEAR(g_scriptPtr-apScript); BITPTR_CLEAR(g_scriptPtr-apScript);
*(g_scriptPtr++) = CON_JUMP | (g_lineNumber << 12); *(g_scriptPtr++) = CON_JUMP | (g_lineNumber << 12);
BITPTR_CLEAR(g_scriptPtr-apScript); BITPTR_CLEAR(g_scriptPtr-apScript);
*(g_scriptPtr++) = MAXGAMEVARS; *(g_scriptPtr++) = GV_FLAG_CONSTANT;
BITPTR_CLEAR(g_scriptPtr-apScript); BITPTR_CLEAR(g_scriptPtr-apScript);
*g_scriptPtr = g_parsingEventBreakPtr; *g_scriptPtr = g_parsingEventBreakPtr;
g_parsingEventBreakPtr = g_scriptPtr++ - apScript; g_parsingEventBreakPtr = g_scriptPtr++ - apScript;

View file

@ -4293,10 +4293,10 @@ GAMEEXEC_STATIC void VM_Execute(native_t loop)
lVarID ^= (MAXGAMEVARS << 2); lVarID ^= (MAXGAMEVARS << 2);
if (lVarID & (MAXGAMEVARS << 1)) if (lVarID & GV_FLAG_NEGATIVE)
{ {
m = -m; m = -m;
lVarID ^= (MAXGAMEVARS << 1); lVarID ^= GV_FLAG_NEGATIVE;
} }
insptr++; insptr++;
@ -4331,10 +4331,10 @@ GAMEEXEC_STATIC void VM_Execute(native_t loop)
continue; continue;
} }
} }
else if (EDUKE32_PREDICT_TRUE(*insptr & (MAXGAMEVARS << 1))) else if (EDUKE32_PREDICT_TRUE(*insptr & GV_FLAG_NEGATIVE))
{ {
m = -m; m = -m;
lVarID ^= (MAXGAMEVARS << 1); lVarID ^= GV_FLAG_NEGATIVE;
} }
else else
{ {

View file

@ -622,13 +622,13 @@ int __fastcall Gv_GetVar(int gameVar, int spriteNum, int playerNum)
if (gameVar == g_thisActorVarID) if (gameVar == g_thisActorVarID)
return spriteNum; return spriteNum;
if (gameVar == MAXGAMEVARS) if (gameVar == GV_FLAG_CONSTANT)
return *insptr++; return *insptr++;
int invertResult = !!(gameVar & (MAXGAMEVARS << 1)); int invertResult = !!(gameVar & GV_FLAG_NEGATIVE);
gamevar_t & var = aGameVars[gameVar &= (MAXGAMEVARS - 1)]; gamevar_t const & var = aGameVars[gameVar &= (MAXGAMEVARS-1)];
if ((gameVar & ~(MAXGAMEVARS << 1)) >= g_gameVarCount) if ((gameVar & ~GV_FLAG_NEGATIVE) >= g_gameVarCount)
goto special; goto special;
@ -662,9 +662,9 @@ int __fastcall Gv_GetVar(int gameVar, int spriteNum, int playerNum)
return (returnValue ^ -invertResult) + invertResult; return (returnValue ^ -invertResult) + invertResult;
special: special:
if (gameVar & (MAXGAMEVARS << 2)) // array if (gameVar & GV_FLAG_ARRAY)
{ {
gameVar &= (MAXGAMEVARS - 1); // ~((MAXGAMEVARS<<2)|(MAXGAMEVARS<<1)); gameVar &= (MAXGAMEVARS-1); // ~((MAXGAMEVARS<<2)|(MAXGAMEVARS<<1));
int const arrayIndex = Gv_GetVar(*insptr++, spriteNum, playerNum); int const arrayIndex = Gv_GetVar(*insptr++, spriteNum, playerNum);
@ -676,13 +676,13 @@ special:
returnValue = Gv_GetArrayValue(gameVar, arrayIndex); returnValue = Gv_GetArrayValue(gameVar, arrayIndex);
} }
else if (gameVar&(MAXGAMEVARS<<3)) // struct shortcut vars else if (gameVar & GV_FLAG_STRUCT) // struct shortcut vars
{ {
int arrayIndexVar = *insptr++; int arrayIndexVar = *insptr++;
int arrayIndex = Gv_GetVar(arrayIndexVar, spriteNum, playerNum); int arrayIndex = Gv_GetVar(arrayIndexVar, spriteNum, playerNum);
int const labelNum = *insptr++; int const labelNum = *insptr++;
gameVar &= (MAXGAMEVARS - 1); gameVar &= (MAXGAMEVARS-1);
switch (gameVar - g_structVarIDs) switch (gameVar - g_structVarIDs)
{ {
@ -869,11 +869,11 @@ int __fastcall Gv_GetSpecialVarX(int gameVar)
{ {
int returnValue = -1; int returnValue = -1;
if (gameVar & (MAXGAMEVARS << 2)) // array if (gameVar & GV_FLAG_ARRAY) // array
{ {
int const arrayIndex = Gv_GetVarX(*insptr++); int const arrayIndex = Gv_GetVarX(*insptr++);
gameVar &= (MAXGAMEVARS - 1); // ~((MAXGAMEVARS<<2)|(MAXGAMEVARS<<1)); gameVar &= (MAXGAMEVARS-1); // ~((MAXGAMEVARS<<2)|(MAXGAMEVARS<<1));
int const arraySiz int const arraySiz
= (aGameArrays[gameVar].flags & GAMEARRAY_VARSIZE) ? Gv_GetVarX(aGameArrays[gameVar].size) : aGameArrays[gameVar].size; = (aGameArrays[gameVar].flags & GAMEARRAY_VARSIZE) ? Gv_GetVarX(aGameArrays[gameVar].size) : aGameArrays[gameVar].size;
@ -886,10 +886,10 @@ int __fastcall Gv_GetSpecialVarX(int gameVar)
returnValue = Gv_GetArrayValue(gameVar, arrayIndex); returnValue = Gv_GetArrayValue(gameVar, arrayIndex);
} }
else if (gameVar & (MAXGAMEVARS << 3)) // struct shortcut vars else if (gameVar & GV_FLAG_STRUCT) // struct shortcut vars
{ {
int arrayIndexVar = *insptr++; int arrayIndexVar = *insptr++;
auto const structIndex = (gameVar & (MAXGAMEVARS - 1)) - g_structVarIDs; auto const structIndex = (gameVar & (MAXGAMEVARS-1)) - g_structVarIDs;
int arrayIndex = structIndex != STRUCT_USERDEF ? Gv_GetVarX(arrayIndexVar) : -1; int arrayIndex = structIndex != STRUCT_USERDEF ? Gv_GetVarX(arrayIndexVar) : -1;
int const labelNum = *insptr++; int const labelNum = *insptr++;
@ -995,17 +995,17 @@ int __fastcall Gv_GetVarX(int gameVar)
if (gameVar == g_thisActorVarID) if (gameVar == g_thisActorVarID)
return vm.spriteNum; return vm.spriteNum;
if (gameVar == MAXGAMEVARS) if (gameVar == GV_FLAG_CONSTANT)
return *insptr++; return *insptr++;
int const invertResult = !!(gameVar & (MAXGAMEVARS << 1)); int const invertResult = !!(gameVar & GV_FLAG_NEGATIVE);
int returnValue = -1; int returnValue = -1;
if (gameVar >= g_gameVarCount && invertResult == 0) if (gameVar >= g_gameVarCount && invertResult == 0)
returnValue = Gv_GetSpecialVarX(gameVar); returnValue = Gv_GetSpecialVarX(gameVar);
else else
{ {
gamevar_t &var = aGameVars[gameVar &= MAXGAMEVARS - 1]; gamevar_t const &var = aGameVars[gameVar &= MAXGAMEVARS-1];
int const varFlags = var.flags & (GAMEVAR_USER_MASK|GAMEVAR_PTR_MASK); int const varFlags = var.flags & (GAMEVAR_USER_MASK|GAMEVAR_PTR_MASK);
if (!varFlags) returnValue = var.global; if (!varFlags) returnValue = var.global;
@ -1038,9 +1038,9 @@ void __fastcall Gv_GetManyVars(int const numVars, int32_t * const outBuf)
for (native_t j = 0; j < numVars; ++j) for (native_t j = 0; j < numVars; ++j)
{ {
int gameVar = *insptr++; int gameVar = *insptr++;
int const invertResult = !!(gameVar & (MAXGAMEVARS << 1)); int const invertResult = !!(gameVar & GV_FLAG_NEGATIVE);
if (gameVar == MAXGAMEVARS) if (gameVar == GV_FLAG_CONSTANT)
{ {
outBuf[j] = *insptr++; outBuf[j] = *insptr++;
continue; continue;
@ -1056,7 +1056,7 @@ void __fastcall Gv_GetManyVars(int const numVars, int32_t * const outBuf)
continue; continue;
} }
gamevar_t &var = aGameVars[gameVar &= MAXGAMEVARS - 1]; gamevar_t &var = aGameVars[gameVar &= MAXGAMEVARS-1];
int const varFlags = var.flags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK); int const varFlags = var.flags & (GAMEVAR_USER_MASK | GAMEVAR_PTR_MASK);
int value = var.global; int value = var.global;

View file

@ -29,6 +29,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#define MAXGAMEVARS 2048 // must be a power of two #define MAXGAMEVARS 2048 // must be a power of two
#define MAXVARLABEL 26 #define MAXVARLABEL 26
#define GV_FLAG_CONSTANT (MAXGAMEVARS)
#define GV_FLAG_NEGATIVE (MAXGAMEVARS<<1)
#define GV_FLAG_ARRAY (MAXGAMEVARS<<2)
#define GV_FLAG_STRUCT (MAXGAMEVARS<<3)
// store global game definitions // store global game definitions
enum GamevarFlags_t enum GamevarFlags_t
{ {