From cb1824ca2534165359d64fbfad9175410ab3b6eb Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 15 May 2020 11:30:33 +0200 Subject: [PATCH] - store script code in a dynamic array. # Conflicts: # source/games/duke/src/zz_game.cpp --- source/games/duke/src/actors.cpp | 4 +- source/games/duke/src/actors_d.cpp | 2 +- source/games/duke/src/actors_r.cpp | 2 +- source/games/duke/src/gamedef.cpp | 114 ++++++++------------------ source/games/duke/src/gamedef.h | 3 - source/games/duke/src/gameexec.cpp | 20 ++--- source/games/duke/src/gameexec.h | 1 + source/games/duke/src/global.h | 5 +- source/games/duke/src/zz_actors.cpp | 2 +- source/games/duke/src/zz_game.cpp | 11 ++- source/games/duke/src/zz_savegame.cpp | 2 +- 11 files changed, 57 insertions(+), 109 deletions(-) diff --git a/source/games/duke/src/actors.cpp b/source/games/duke/src/actors.cpp index 0266fe16e..a03b1c3d1 100644 --- a/source/games/duke/src/actors.cpp +++ b/source/games/duke/src/actors.cpp @@ -4889,11 +4889,11 @@ void alterang(int a, int g_i, int g_p) { short aang, angdif, goalang, j; int ticselapsed; - intptr_t *moveptr; + intptr_t* moveptr; int* g_t = hittype[g_i].t_data; auto* g_sp = &sprite[g_i]; - moveptr = apScript + g_t[1]; + moveptr = &ScriptCode[g_t[1]]; ticselapsed = (g_t[0]) & 31; diff --git a/source/games/duke/src/actors_d.cpp b/source/games/duke/src/actors_d.cpp index 5ea72b603..44569b262 100644 --- a/source/games/duke/src/actors_d.cpp +++ b/source/games/duke/src/actors_d.cpp @@ -4030,7 +4030,7 @@ void move_d(int g_i, int g_p, int g_x) return; } - moveptr = apScript + g_t[1]; + moveptr = &ScriptCode[g_t[1]]; if (a & geth) g_sp->xvel += (*moveptr - g_sp->xvel) >> 1; if (a & getv) g_sp->zvel += ((*(moveptr + 1) << 4) - g_sp->zvel) >> 1; diff --git a/source/games/duke/src/actors_r.cpp b/source/games/duke/src/actors_r.cpp index a4ef32df0..eda40950b 100644 --- a/source/games/duke/src/actors_r.cpp +++ b/source/games/duke/src/actors_r.cpp @@ -4203,7 +4203,7 @@ void move_r(int g_i, int g_p, int g_x) return; } - moveptr = apScript + g_t[1]; + moveptr = &ScriptCode[g_t[1]]; if (a & geth) g_sp->xvel += (*moveptr - g_sp->xvel) >> 1; if (a & getv) g_sp->zvel += ((*(moveptr + 1) << 4) - g_sp->zvel) >> 1; diff --git a/source/games/duke/src/gamedef.cpp b/source/games/duke/src/gamedef.cpp index c5f68c4bb..6938b5d07 100644 --- a/source/games/duke/src/gamedef.cpp +++ b/source/games/duke/src/gamedef.cpp @@ -59,9 +59,9 @@ int checking_ifelse; //G_EXTERN char tempbuf[MAXSECTORS << 1], buf[1024]; todo - move to compile state. tempbuf gets used nearly everywhere as scratchpad memory. extern char tempbuf[]; -extern intptr_t* scriptaddress; extern int* labelcode; -extern intptr_t* apScript; + +TArray ScriptCode; //--------------------------------------------------------------------------- // @@ -375,68 +375,44 @@ void getlabel(void) // //--------------------------------------------------------------------------- -#if 0 static void setscriptvalue(int offset, int value) { - script[offset] = value; + ScriptCode[offset] = value; } int scriptpos() { - return script.Size(); + return ScriptCode.Size(); } static void appendscriptvalue(int value) { - script.Push(value); + ScriptCode.Push(value); } static int popscriptvalue() { - int p; - script.Pop(p); + decltype(ScriptCode)::value_type p; + ScriptCode.Pop(p); return p; } -void pushlabeladdress() -{ - labelcode.Push(script.Size()); -} - void reservescriptspace(int space) { - script.Reserve(space); + ScriptCode.Reserve(space); } -#else - -// TRANSITIONAL Helpers to write to the old script buffer while using the new interface. Allows to test the parser before implementing the rest. -void scriptWriteValue(int32_t const value); -void scriptWriteAtOffset(int32_t const value, intptr_t addr); -void scriptWritePointer(intptr_t const value, intptr_t addr); -static void setscriptvalue(int offset, int value) +/* +void pushlabeladdress() { - apScript[offset] = value; + labelcode.Push(script.Size()); } +*/ -static void appendscriptvalue(int value) -{ - *scriptaddress++ = value; -} - -static int popscriptvalue() -{ - return *--scriptaddress; -} - -int scriptpos() -{ - return int(scriptaddress - apScript); -} void appendlabeladdress(int offset = 0) { - labelcode[labelcnt++] = int(scriptaddress - apScript) + offset; + labelcode[labelcnt++] = ScriptCode.Size() + offset; labelcnt++; } @@ -446,14 +422,6 @@ void appendlabelvalue(int value) labelcnt++; } -void reservescriptspace(int space) -{ - scriptaddress += space; -} - - -#endif - //--------------------------------------------------------------------------- @@ -753,8 +721,7 @@ int parsecommand() while (keyword() == -1) { transnum(); - popscriptvalue(); - j |= *scriptaddress; + j |= popscriptvalue(); } appendscriptvalue(j); } @@ -791,9 +758,8 @@ int parsecommand() { popscriptvalue(); transnum(); // Volume Number (0/4) - popscriptvalue(); + k = popscriptvalue() - 1; - k = *scriptaddress - 1; if (k == -1) k = MAXVOLUMES; if (k >= 0) // if it's background music @@ -1037,8 +1003,7 @@ int parsecommand() parsing_event = parsing_actor = scriptpos(); transnum(); - popscriptvalue(); - j = *scriptaddress; // type of event + j = popscriptvalue(); if (j< 0 || j> EVENT_MAXEVENT) { Printf(TEXTCOLOR_RED " * ERROR!(%s, line %d) Invalid Event ID.\n", fn, line_number); @@ -1129,7 +1094,7 @@ int parsecommand() { checking_ifelse--; tempscrptr = scriptpos(); - scriptaddress++; //Leave a spot for the fail location + reservescriptspace(1); //Leave a spot for the fail location parsecommand(); setscriptvalue(tempscrptr, scriptpos()); } @@ -1304,8 +1269,7 @@ int parsecommand() do { transnum(); - popscriptvalue(); - j |= *scriptaddress; + j |= popscriptvalue(); } while (keyword() == -1); appendscriptvalue(j); goto if_common; @@ -1405,8 +1369,7 @@ int parsecommand() case concmd_definevolumename: popscriptvalue(); transnum(); - popscriptvalue(); - j = *scriptaddress; + j = popscriptvalue(); while (*textptr == ' ' || *textptr == '\t') textptr++; i = 0; @@ -1423,8 +1386,7 @@ int parsecommand() case concmd_defineskillname: popscriptvalue(); transnum(); - popscriptvalue(); - j = *scriptaddress; + j = popscriptvalue(); while (*textptr == ' ') textptr++; i = 0; @@ -1442,11 +1404,9 @@ int parsecommand() case concmd_definelevelname: popscriptvalue(); transnum(); - popscriptvalue(); - j = *scriptaddress; + j = popscriptvalue(); transnum(); - popscriptvalue(); - k = *scriptaddress; + k = popscriptvalue(); while (*textptr == ' ') textptr++; i = 0; @@ -1490,13 +1450,13 @@ int parsecommand() case concmd_definequote: popscriptvalue(); transnum(); - k = *(scriptaddress - 1); + k = popscriptvalue(); if (k >= MAXQUOTES) { Printf(TEXTCOLOR_RED " * ERROR!(%s, line %d) Quote number exceeds limit of %d.\n", line_number, MAXQUOTES); errorcount++; } - popscriptvalue(); + i = 0; while (*textptr == ' ') textptr++; @@ -1514,8 +1474,7 @@ int parsecommand() { popscriptvalue(); transnum(); - k = *(scriptaddress - 1); - popscriptvalue(); + k = popscriptvalue(); i = 0; while (*textptr == ' ') textptr++; @@ -1529,20 +1488,15 @@ int parsecommand() parsebuffer.Push(0); transnum(); - int ps = *(scriptaddress - 1); - popscriptvalue(); + int ps = popscriptvalue(); transnum(); - int pe = *(scriptaddress - 1); - popscriptvalue(); + int pe = popscriptvalue(); transnum(); - int pr = *(scriptaddress - 1); - popscriptvalue(); + int pr = popscriptvalue(); transnum(); - int m = *(scriptaddress - 1); - popscriptvalue(); + int m = popscriptvalue(); transnum(); - int vo = *(scriptaddress - 1); - popscriptvalue(); + int vo = popscriptvalue(); S_DefineSound(k, parsebuffer.Data(), ps, pe, pr, m, vo, 1.f); return 0; } @@ -1625,6 +1579,7 @@ int parsecommand() return 0; case concmd_gamestartup: { + popscriptvalue(); auto parseone = []() { transnum(); return popscriptvalue(); }; ud.const_visibility = parseone(); impact_damage = parseone(); @@ -1664,7 +1619,6 @@ int parsecommand() max_ammo_amount[14] = parseone(); max_ammo_amount[16] = parseone(); } - scriptaddress++; } return 0; } @@ -1714,7 +1668,7 @@ void loadcons(const char* filenam) memset(&actorinfo[i], 0, sizeof(actorinfo)); } - apScript = (intptr_t*)Xcalloc(1, g_scriptSize * sizeof(intptr_t)); + ScriptCode.Clear(); labelcnt = 0; @@ -1726,7 +1680,7 @@ void loadcons(const char* filenam) auto before = I_nsTime(); - scriptaddress = apScript + 1; + ScriptCode.Push(0); compilecon(filenam); //Tokenize if (userConfig.AddCons) for (FString& m : *userConfig.AddCons.get()) @@ -1744,7 +1698,7 @@ void loadcons(const char* filenam) { auto after = I_nsTime(); Printf("Compilation time:%.2f ms, Code Size:%d bytes. %d labels. %d/%d Variables.\n", (after-before) / 1000000., - ((scriptaddress - apScript) << 2) - 4, + (ScriptCode.Size() << 2) - 4, labelcnt, 0,//iGameVarCount, MAXGAMEVARS diff --git a/source/games/duke/src/gamedef.h b/source/games/duke/src/gamedef.h index 2e2e52960..cab132d9a 100644 --- a/source/games/duke/src/gamedef.h +++ b/source/games/duke/src/gamedef.h @@ -53,9 +53,6 @@ extern int32_t g_totalLines; extern int warningcount; extern int32_t otherp; -extern intptr_t *scriptaddress; - - int32_t C_AllocQuote(int32_t qnum); void C_InitQuotes(void); diff --git a/source/games/duke/src/gameexec.cpp b/source/games/duke/src/gameexec.cpp index a2e197759..a613fda0c 100644 --- a/source/games/duke/src/gameexec.cpp +++ b/source/games/duke/src/gameexec.cpp @@ -80,7 +80,7 @@ void parseifelse(int condition) } else { - insptr = apScript + *(insptr+1); + insptr = &ScriptCode[*(insptr+1)]; if(*insptr == 10) { // else... @@ -278,9 +278,9 @@ int parse(void) case concmd_ai: insptr++; g_t[5] = *insptr; - g_t[4] = apScript[g_t[5]]; // Action - g_t[1] = apScript[g_t[5] + 1]; // move - g_sp->hitag = apScript[g_t[5] + 2]; // Ai + g_t[4] = ScriptCode[g_t[5]]; // Action + g_t[1] = ScriptCode[g_t[5] + 1]; // move + g_sp->hitag = ScriptCode[g_t[5] + 2]; // Ai g_t[0] = g_t[2] = g_t[3] = 0; if (g_sp->hitag & random_angle) g_sp->ang = krand() & 2047; @@ -307,7 +307,7 @@ int parse(void) hittype[g_i].timetosleep = SLEEPTIME; break; case concmd_else: - insptr = apScript + *(insptr + 1); + insptr = &ScriptCode[*(insptr + 1)]; break; case concmd_addstrength: insptr++; @@ -826,7 +826,7 @@ int parse(void) case concmd_state: { auto tempscrptr = insptr + 2; - insptr = apScript + *(insptr + 1); + insptr = &ScriptCode[*(insptr + 1)]; while (1) if (parse()) break; insptr = tempscrptr; } @@ -1517,7 +1517,7 @@ int parse(void) default: Printf(TEXTCOLOR_RED "Unrecognized PCode of %ld in parse. Killing current sprite.\n",*insptr); - Printf(TEXTCOLOR_RED "Offset=%0lX\n",scriptaddress-apScript); + Printf(TEXTCOLOR_RED "Offset=%0lX\n",insptr-ScriptCode.Data()); killit_flag = 1; break; } @@ -1537,7 +1537,7 @@ void execute(int i,int p,int x) g_t = &hittype[g_i].temp_data[0]; // Sprite's 'extra' data if (actorinfo[g_sp->picnum].scriptaddress == 0) return; - insptr = apScript + 4 + (actorinfo[g_sp->picnum].scriptaddress); + insptr = &ScriptCode[4 + (actorinfo[g_sp->picnum].scriptaddress)]; killit_flag = 0; @@ -1552,7 +1552,7 @@ void execute(int i,int p,int x) if (g_t[4]) { // This code was utterly cryptic in the original source. - auto ptr = apScript + g_t[4]; + auto ptr = &ScriptCode[g_t[4]]; int numframes = ptr[1]; int increment = ptr[3]; int delay = ptr[4]; @@ -1649,7 +1649,7 @@ void OnEvent(int iEventID, int p, int i, int x) g_sp = &sprite[g_i]; g_t = &hittype[g_i].temp_data[0]; - insptr = apScript + apScriptGameEvent[iEventID]; + insptr = &ScriptCode[apScriptGameEvent[iEventID]]; killit_flag = 0; do diff --git a/source/games/duke/src/gameexec.h b/source/games/duke/src/gameexec.h index 401a6fb9e..7cf424f3e 100644 --- a/source/games/duke/src/gameexec.h +++ b/source/games/duke/src/gameexec.h @@ -77,6 +77,7 @@ enum EVENT_MAXEVENT = EVENT_NUMEVENTS - 1 }; +extern TArray ScriptCode; void OnEvent(int id, int pnum = -1, int snum = -1, int dist = -1); diff --git a/source/games/duke/src/global.h b/source/games/duke/src/global.h index ece5c61f6..8b9136727 100644 --- a/source/games/duke/src/global.h +++ b/source/games/duke/src/global.h @@ -201,9 +201,6 @@ G_EXTERN int16_t ambientlotag[64]; G_EXTERN int16_t ambienthitag[64]; G_EXTERN uint32_t g_ambientCnt; -G_EXTERN intptr_t *apScript; -G_EXTERN intptr_t *scriptaddress; - inline int32_t G_HaveActor(int spriteNum) { return actorinfo[spriteNum].scriptaddress != NULL; @@ -211,7 +208,7 @@ inline int32_t G_HaveActor(int spriteNum) inline int32_t G_DefaultActorHealth(int spriteNum) // rename! { - return G_HaveActor(spriteNum) ? apScript[actorinfo[spriteNum].scriptaddress] : 0; + return G_HaveActor(spriteNum) ? ScriptCode[actorinfo[spriteNum].scriptaddress] : 0; } diff --git a/source/games/duke/src/zz_actors.cpp b/source/games/duke/src/zz_actors.cpp index 4556a3eff..a7b6dd5e6 100644 --- a/source/games/duke/src/zz_actors.cpp +++ b/source/games/duke/src/zz_actors.cpp @@ -173,7 +173,7 @@ void A_MoveSector(int spriteNum) } // NOTE: T5 is AC_ACTION_ID -# define LIGHTRAD_PICOFS(i) (T5(i) ? *(apScript + T5(i)) + (*(apScript + T5(i) + 2)) * AC_CURFRAME(actor[i].t_data) : 0) +# define LIGHTRAD_PICOFS(i) (T5(i) ? ScriptCode[T5(i)] + ScriptCode[T5(i) + 2] * AC_CURFRAME(actor[i].t_data) : 0) // this is the same crap as in game.c's tspr manipulation. puke. // XXX: may access tilesizy out-of-bounds by bad user code. diff --git a/source/games/duke/src/zz_game.cpp b/source/games/duke/src/zz_game.cpp index b3079328c..e4116015f 100644 --- a/source/games/duke/src/zz_game.cpp +++ b/source/games/duke/src/zz_game.cpp @@ -1321,7 +1321,7 @@ static int32_t G_InitActor(int32_t i, int32_t tilenum, int32_t set_movflag_uncon { if (actorinfo[tilenum].scriptaddress) { - auto sa = &apScript[actorinfo[tilenum].scriptaddress]; + auto sa = &ScriptCode[actorinfo[tilenum].scriptaddress]; SH(i) = sa[0]; AC_ACTION_ID(actor[i].t_data) = sa[1]; AC_MOVE_ID(actor[i].t_data) = sa[2]; @@ -5065,7 +5065,7 @@ default_case1: { // Display TILE_APLAYER sprites with action PSTAND when viewed through // a camera. - auto aplayer_scr = apScript + actorinfo[TILE_APLAYER].scriptaddress; + auto aplayer_scr = &ScriptCode[actorinfo[TILE_APLAYER].scriptaddress]; // [0]=strength, [1]=actionofs, [2]=moveofs scrofs_action = aplayer_scr[1]; @@ -5269,8 +5269,8 @@ default_case2: if ((unsigned)scrofs_action + ACTION_VIEWTYPE >= (unsigned)g_scriptSize) goto skip; - int viewtype = apScript[scrofs_action + ACTION_VIEWTYPE]; - uint16_t const action_flags = apScript[scrofs_action + ACTION_FLAGS]; + int viewtype = ScriptCode[scrofs_action + ACTION_VIEWTYPE]; + uint16_t const action_flags = ScriptCode[scrofs_action + ACTION_FLAGS]; int const invertp = viewtype < 0; l = klabs(viewtype); @@ -5334,7 +5334,7 @@ default_case2: } } - t->picnum += frameOffset + apScript[scrofs_action + ACTION_STARTFRAME] + l*curframe; + t->picnum += frameOffset + ScriptCode[scrofs_action + ACTION_STARTFRAME] + l*curframe; // XXX: t->picnum can be out-of-bounds by bad user code. if (l > 0) @@ -6515,7 +6515,6 @@ static void G_Cleanup(void) #if 0 if (labeltype != (int32_t*)&wall[0]) Xfree(labeltype); #endif - Xfree(apScript); } diff --git a/source/games/duke/src/zz_savegame.cpp b/source/games/duke/src/zz_savegame.cpp index a93f5d234..497bef063 100644 --- a/source/games/duke/src/zz_savegame.cpp +++ b/source/games/duke/src/zz_savegame.cpp @@ -1077,7 +1077,7 @@ int32_t sv_loadheader(FileReader &fill, int32_t spot, savehead_t *h) return -2; } - if (h->majorver != SV_MAJOR_VER || h->minorver != SV_MINOR_VER || h->bytever != BYTEVERSION || h->userbytever != ud.userbytever || (apScript != NULL)) + if (h->majorver != SV_MAJOR_VER || h->minorver != SV_MINOR_VER || h->bytever != BYTEVERSION || h->userbytever != ud.userbytever || ScriptCode.Size()) { #ifndef DEBUGGINGAIDS if (havedemo)