diff --git a/source/games/duke/src/gamedef.cpp b/source/games/duke/src/gamedef.cpp index c255d6a6e..80e224042 100644 --- a/source/games/duke/src/gamedef.cpp +++ b/source/games/duke/src/gamedef.cpp @@ -531,7 +531,7 @@ int transword(void) //Returns its code # // //--------------------------------------------------------------------------- -void transnum(void) +int transnum(void) { int i, l; @@ -540,7 +540,7 @@ void transnum(void) if (*textptr == 0x0a) line_number++; textptr++; if (*textptr == 0) - return; + return 0; } @@ -569,7 +569,7 @@ void transnum(void) { appendscriptvalue(labelcode[i]); textptr += l; - return; + return labelcode[i]; } } @@ -584,7 +584,7 @@ void transnum(void) Printf(TEXTCOLOR_ORANGE " Game Variable not expected\n"); } #endif - return; + return 0; } // Now it's getting nasty... With all of C's integer conversion functions we have to look for undefined behavior and truncation problems. This one's the least problematic approach @@ -600,6 +600,7 @@ void transnum(void) } appendscriptvalue(int(value)); // truncate the parsed value to 32 bit. textptr += l; + return int(value); } //--------------------------------------------------------------------------- @@ -702,7 +703,6 @@ int parsecommand(int tw) // for now just run an externally parsed command. } return 0; -#if 0 case concmd_gamevar: // syntax: gamevar // defines var1 and sets initial value. @@ -710,6 +710,7 @@ int parsecommand(int tw) // for now just run an externally parsed command. // (see top of this files for flags) getlabel(); //GetGameVarLabel(); // Check to see it's already defined + popscriptvalue(); if (getkeyword(label + (labelcnt << 6)) >= 0) { @@ -719,16 +720,13 @@ int parsecommand(int tw) // for now just run an externally parsed command. } transnum(); // get initial value + j = popscriptvalue(); transnum(); // get flags - AddGameVar(label + (labelcnt << 6), *(scriptptr - 2), - (*(scriptptr - 1)) - // can't define default or secret - & (~(GAMEVAR_FLAG_DEFAULT | GAMEVAR_FLAG_SECRET)) - ); - scriptptr -= 3; // no need to save in script... + lnum = popscriptvalue(); + AddGameVar(label + (labelcnt << 6), j, lnum & (~(GAMEVAR_FLAG_DEFAULT | GAMEVAR_FLAG_SECRET))); return 0; -#endif + case concmd_define: getlabel(); checkforkeyword(); @@ -1169,7 +1167,6 @@ int parsecommand(int tw) // for now just run an externally parsed command. } return 0; -#if 0 case concmd_setvar: case concmd_addvar: // syntax: [rand|add|set]var @@ -1180,12 +1177,7 @@ int parsecommand(int tw) // for now just run an externally parsed command. getlabel(); //GetGameVarLabel(); // Check to see if it's a keyword - if (getkeyword(label + (labelcnt << 6)) >= 0) - { - errorcount++; - ReportError(ERROR_ISAKEYWORD); - return 0; - } + checkforkeyword(); i = GetDefID(label + (labelcnt << 6)); if (i < 0) @@ -1216,12 +1208,7 @@ int parsecommand(int tw) // for now just run an externally parsed command. getlabel(); //GetGameVarLabel(); // Check to see if it's a keyword - if (getkeyword(label + (labelcnt << 6)) >= 0) - { - errorcount++; - ReportError(ERROR_ISAKEYWORD); - return 0; - } + checkforkeyword(); i = GetDefID(label + (labelcnt << 6)); if (i < 0) @@ -1241,14 +1228,7 @@ int parsecommand(int tw) // for now just run an externally parsed command. // get the ID of the DEF getlabel(); //GetGameVarLabel(); - - // Check to see if it's a keyword - if (getkeyword(label + (labelcnt << 6)) >= 0) - { - errorcount++; - ReportError(ERROR_ISAKEYWORD); - return 0; - } + checkforkeyword(); i = GetDefID(label + (labelcnt << 6)); if (i < 0) @@ -1257,16 +1237,7 @@ int parsecommand(int tw) // for now just run an externally parsed command. ReportError(ERROR_NOTAGAMEDEF); return 0; } - //#ifndef EDUKE - // this was a bug: second var can be RO because it is source - if (aGameVars[i].dwFlags & GAMEVAR_FLAG_READONLY) - { - errorcount++; - ReportError(ERROR_VARREADONLY); - return 0; - - } - //#endif + appendscriptvalue(i); // the ID of the DEF (offset into array...) return 0; @@ -1278,12 +1249,7 @@ int parsecommand(int tw) // for now just run an externally parsed command. getlabel(); //GetGameVarLabel(); // Check to see it's a keyword - if (getkeyword(label + (labelcnt << 6)) >= 0) - { - errorcount++; - ReportError(ERROR_ISAKEYWORD); - return 0; - } + checkforkeyword(); i = GetDefID(label + (labelcnt << 6)); if (i < 0) @@ -1298,12 +1264,7 @@ int parsecommand(int tw) // for now just run an externally parsed command. getlabel(); //GetGameVarLabel(); // Check to see it's a keyword - if (getkeyword(label + (labelcnt << 6)) >= 0) - { - errorcount++; - ReportError(ERROR_ISAKEYWORD); - return 0; - } + checkforkeyword(); i = GetDefID(label + (labelcnt << 6)); if (i < 0) @@ -1323,13 +1284,7 @@ int parsecommand(int tw) // for now just run an externally parsed command. getlabel(); //GetGameVarLabel(); // Check to see it's a keyword - if (getkeyword(label + (labelcnt << 6)) >= 0) - { - errorcount++; - ReportError(ERROR_ISAKEYWORD); - return 0; - } - + checkforkeyword(); i = GetDefID(label + (labelcnt << 6)); if (i < 0) { // not a defined DEF @@ -1346,7 +1301,7 @@ int parsecommand(int tw) // for now just run an externally parsed command. // syntax: addlogvar // source file. - appendscriptvalue(g_currentSourceFile); // the ID of the DEF (offset into array...) + appendscriptvalue(g_currentSourceFile); // prints the line number in the log file. appendscriptvalue(line_number); @@ -1355,12 +1310,7 @@ int parsecommand(int tw) // for now just run an externally parsed command. getlabel(); //GetGameVarLabel(); // Check to see if it's a keyword - if (getkeyword(label + (labelcnt << 6)) >= 0) - { - errorcount++; - ReportError(ERROR_ISAKEYWORD); - return 0; - } + checkforkeyword(); i = GetDefID(label + (labelcnt << 6)); if (i < 0) @@ -1369,7 +1319,7 @@ int parsecommand(int tw) // for now just run an externally parsed command. ReportError(ERROR_NOTAGAMEDEF); return 0; } - appendscriptvalue(i); // the ID of the DEF (offset into array...) + appendscriptvalue(i); return 0; @@ -1382,7 +1332,7 @@ int parsecommand(int tw) // for now just run an externally parsed command. // prints the line number in the log file. appendscriptvalue(line_number); return 0; -#endif + case concmd_ifp: j = 0; do diff --git a/source/games/duke/src/gamevars.h b/source/games/duke/src/gamevars.h index 24b354798..b31bcaca4 100644 --- a/source/games/duke/src/gamevars.h +++ b/source/games/duke/src/gamevars.h @@ -51,6 +51,9 @@ enum GamevarFlags_t GAMEVAR_NOMULTI = 0x00080000, // don't attach to multiplayer packets GAMEVAR_Q16PTR = 0x00100000, // plValues is a pointer to a q16.16 GAMEVAR_SERIALIZE = 0x00200000, // write into permasaves + GAMEVAR_FLAG_DEFAULT = GAMEVAR_DEFAULT, + GAMEVAR_FLAG_SECRET = 0x200, // placeholder + GAMEVAR_FLAG_READONLY = 0x1000, // placeholder GAMEVAR_RAWQ16PTR = GAMEVAR_Q16PTR | GAMEVAR_SPECIAL, // plValues is a pointer to a q16.16 but we don't want conversion GAMEVAR_PTR_MASK = GAMEVAR_INT32PTR | GAMEVAR_INT16PTR | GAMEVAR_Q16PTR | GAMEVAR_RAWQ16PTR, @@ -70,7 +73,11 @@ typedef struct intptr_t *pValues; // array of values when 'per-player', or 'per-actor' }; intptr_t defaultValue; - uintptr_t flags; + union + { + uintptr_t flags; + uintptr_t dwFlags; + }; char * szLabel; } gamevar_t; #pragma pack(pop) @@ -104,6 +111,9 @@ inline int GetGameVar(const char* szGameLabel, int defaultValue, int spriteNum, return Gv_GetVarByLabel(szGameLabel, defaultValue, spriteNum, playerNum); } void Gv_NewVar(const char *pszLabel,intptr_t lValue,uint32_t dwFlags); +#define AddGameVar Gv_NewVar + +int GetDefID(const char* label); static FORCE_INLINE void A_ResetVars(int const spriteNum) { diff --git a/source/games/duke/src/zz_gamedef.cpp b/source/games/duke/src/zz_gamedef.cpp index f8acc5f1b..3a4dc519c 100644 --- a/source/games/duke/src/zz_gamedef.cpp +++ b/source/games/duke/src/zz_gamedef.cpp @@ -83,21 +83,6 @@ static intptr_t g_scriptEventOffset; extern char *textptr; -static const vec2_t varvartable[] = -{ - { concmd_ifvarvare, concmd_ifvare }, - { concmd_ifvarvarg, concmd_ifvarg }, - { concmd_ifvarvarl, concmd_ifvarl }, - { concmd_addvarvar, concmd_addvar }, - { concmd_setvarvar, concmd_setvar }, -}; - -static inthashtable_t h_varvar = { NULL, INTHASH_SIZE(ARRAY_SIZE(varvartable)) }; - -static inthashtable_t *const inttables[] = { - &h_varvar, -}; - char const * VM_GetKeywordForID(int32_t id) { // do not really need this for now... @@ -120,12 +105,6 @@ void C_InitHashes() for (auto table : tables) hash_init(table); - for (auto table : inttables) - inthash_init(table); - - for (auto &varvar : varvartable) - inthash_add(&h_varvar, varvar.x, varvar.y, 0); - //inithashnames(); initsoundhashnames(); } @@ -225,7 +204,7 @@ static int32_t C_SkipComments(void) while (1); } -static inline int GetDefID(char const *label) { return hash_find(&h_gamevars, label); } +int GetDefID(char const *label) { return hash_find(&h_gamevars, label); } #define LAST_LABEL (label+(labelcnt<<6)) bool isaltok(const char c); @@ -765,7 +744,7 @@ int parsecommand(int tw); // for now just run an externally parsed command. int32_t C_ParseCommand(int32_t loop) { - int32_t i, j=0, k=0, tw; + int32_t j=0, k=0, tw; TArray buffer; do @@ -927,65 +906,21 @@ int32_t C_ParseCommand(int32_t loop) case concmd_motoloopsnd: case concmd_rndmove: case concmd_gamestartup: + case concmd_gamevar: + case concmd_addlogvar: + case concmd_setvar: + case concmd_addvar: + case concmd_setvarvar: + case concmd_addvarvar: + case concmd_ifvarvarg: + case concmd_ifvarvarl: + case concmd_ifvarvare: + case concmd_ifvarg: + case concmd_ifvarl: + case concmd_ifvare: if (parsecommand(g_lastKeyword)) return 1; continue; - case concmd_gamevar: - { - // syntax: gamevar - // defines var1 and sets initial value. - // flags are used to define usage - // (see top of this files for flags) - - if (EDUKE32_PREDICT_FALSE(isdigit(*textptr) || (*textptr == '-'))) - { - errorcount++; - C_ReportError(ERROR_SYNTAXERROR); - skiptoendofline(); - continue; - } - - scriptptr--; - - C_GetNextLabelName(); - - if (getkeyword(label + (labelcnt << 6)) >= 0) - { - errorcount++; - C_ReportError(WARNING_VARMASKSKEYWORD); - continue; - } - - int32_t defaultValue = 0; - int32_t varFlags = 0; - - if (C_GetKeyword() == -1) - { - C_GetNextValue(LABEL_DEFINE); // get initial value - defaultValue = *(--scriptptr); - - j = 0; - - while (C_GetKeyword() == -1) - C_BitOrNextValue(&j); - - C_FinishBitOr(j); - varFlags = *(--scriptptr); - - if (EDUKE32_PREDICT_FALSE((*(scriptptr)&GAMEVAR_USER_MASK)==(GAMEVAR_PERPLAYER|GAMEVAR_PERACTOR))) - { - warningcount++; - varFlags ^= GAMEVAR_PERPLAYER; - C_ReportError(WARNING_BADGAMEVAR); - } - } - - Gv_NewVar(LAST_LABEL, defaultValue, varFlags); - continue; - } - - - case concmd_onevent: if (EDUKE32_PREDICT_FALSE(parsing_state || parsing_actor)) { @@ -1026,143 +961,6 @@ int32_t C_ParseCommand(int32_t loop) continue; - case concmd_addlogvar: - g_labelsOnly = 1; - C_GetNextVar(); - g_labelsOnly = 0; - continue; - case concmd_setvar: - case concmd_addvar: - setvar: - { - auto ins = &scriptptr[-1]; - - C_GetNextVarType(GAMEVAR_READONLY); - C_GetNextValue(LABEL_DEFINE); - continue; - } - case concmd_setvarvar: - case concmd_addvarvar: - { -//setvarvar: - auto ins = &scriptptr[-1]; - auto tptr = textptr; - int const lnum = line_number; - - C_GetNextVarType(GAMEVAR_READONLY); - C_GetNextVar(); - - int const opcode = inthash_find(&h_varvar, *ins & VM_INSTMASK); - - if (ins[2] == GV_FLAG_CONSTANT && opcode != -1) - { - if (g_scriptDebug > 1 && !errorcount && !warningcount) - { - Printf("%s:%d: %s -> %s\n", g_scriptFileName, line_number, - VM_GetKeywordForID(*ins & VM_INSTMASK), VM_GetKeywordForID(opcode)); - } - - tw = opcode; - scriptWriteAtOffset(opcode | LINE_NUMBER, ins); - scriptptr = &ins[1]; - textptr = tptr; - line_number = lnum; - goto setvar; - } - - continue; - } - - case concmd_ifvarvarg: - case concmd_ifvarvarl: - case concmd_ifvarvare: - { - auto const ins = &scriptptr[-1]; - auto const lastScriptPtr = &scriptptr[-1] - apScript; - auto const lasttextptr = textptr; - int const lnum = line_number; - - g_skipBranch = false; - - C_GetNextVar(); - auto const var = scriptptr; - C_GetNextVar(); - - if (*var == GV_FLAG_CONSTANT) - { - int const opcode = inthash_find(&h_varvar, tw); - - if (opcode != -1) - { - if (g_scriptDebug > 1 && !errorcount && !warningcount) - { - Printf("%s:%d: replacing %s with %s\n", g_scriptFileName, line_number, - VM_GetKeywordForID(*ins & VM_INSTMASK), VM_GetKeywordForID(opcode)); - } - - scriptWriteAtOffset(opcode | LINE_NUMBER, ins); - tw = opcode; - scriptptr = &ins[1]; - textptr = lasttextptr; - line_number = lnum; - goto ifvar; - } - } - - if (C_CheckMalformedBranch(lastScriptPtr)) - continue; - - auto const offset = scriptptr - apScript; - scriptptr++; // Leave a spot for the fail location - - C_ParseCommand(0); - - if (C_CheckEmptyBranch(tw, lastScriptPtr)) - continue; - - auto const tempscrptr = apScript + offset; - scriptWritePointer((intptr_t)scriptptr, tempscrptr); - continue; - } - - case concmd_ifvarl: - case concmd_ifvarg: - case concmd_ifvare: - { -ifvar: - auto const ins = &scriptptr[-1]; - auto const lastScriptPtr = &scriptptr[-1] - apScript; - - g_skipBranch = false; - - C_GetNextVar(); - C_GetNextValue(LABEL_DEFINE); - - if (C_CheckMalformedBranch(lastScriptPtr)) - continue; - - // scriptUpdateOpcodeForVariableType(ins); - - auto const offset = scriptptr - apScript; - scriptptr++; //Leave a spot for the fail location - - C_ParseCommand(0); - - if (C_CheckEmptyBranch(tw, lastScriptPtr)) - continue; - - auto const tempscrptr = apScript + offset; - scriptWritePointer((intptr_t)scriptptr, tempscrptr); - - j = C_GetKeyword(); - - if (j == concmd_else) - checking_ifelse++; - - continue; - } - - case concmd_endevent: