From c3cbd1b1ebfbade474f04cc01a0ba2998ccf2448 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 6 Nov 2021 21:53:15 +0100 Subject: [PATCH] - Duke: cleanup of cycler code. Now uses a struct instead of an array with magic indices. --- source/games/duke/src/actors.cpp | 20 ++++++++++---------- source/games/duke/src/global.cpp | 2 +- source/games/duke/src/global.h | 2 +- source/games/duke/src/premap_d.cpp | 12 ++++++------ source/games/duke/src/premap_r.cpp | 12 ++++++------ source/games/duke/src/savegame.cpp | 19 ++++++++++++++++++- source/games/duke/src/sectors.cpp | 18 ++++++++++-------- source/games/duke/src/types.h | 12 ++++++++++++ 8 files changed, 64 insertions(+), 33 deletions(-) diff --git a/source/games/duke/src/actors.cpp b/source/games/duke/src/actors.cpp index cb0f48f38..df580db4c 100644 --- a/source/games/duke/src/actors.cpp +++ b/source/games/duke/src/actors.cpp @@ -318,21 +318,21 @@ void movecyclers(void) { for (int q = numcyclers - 1; q >= 0; q--) { - short* c = &cyclers[q][0]; - int s = c[0]; + Cycler* c = &cyclers[q]; + auto sect = c->sector(); - int t = c[3]; - int j = t + bsin(c[1], -10); - int cshade = c[2]; + int t = c->shade2; + int j = t + bsin(c->lotag, -10); + int cshade = c->shade1; if (j < cshade) j = cshade; else if (j > t) j = t; - c[1] += sector[s].extra; - if (c[5]) + c->lotag += sect->extra; + if (c->state) { - auto wal = &wall[sector[s].wallptr]; - for (int x = sector[s].wallnum; x > 0; x--, wal++) + auto wal = &wall[sect->wallptr]; + for (int x = sect->wallnum; x > 0; x--, wal++) if (wal->hitag != 1) { wal->shade = j; @@ -341,7 +341,7 @@ void movecyclers(void) wall[wal->nextwall].shade = j; } - sector[s].floorshade = sector[s].ceilingshade = j; + sect->floorshade = sect->ceilingshade = j; } } } diff --git a/source/games/duke/src/global.cpp b/source/games/duke/src/global.cpp index 6986a2278..42ac0aa81 100644 --- a/source/games/duke/src/global.cpp +++ b/source/games/duke/src/global.cpp @@ -110,7 +110,7 @@ float cloudx; float cloudy; int cloudclock; int numcyclers; // sector lighting effects -int16_t cyclers[MAXCYCLERS][6]; +Cycler cyclers[MAXCYCLERS]; int mirrorcnt; int mirrorsector[64]; // mirrors int mirrorwall[64]; diff --git a/source/games/duke/src/global.h b/source/games/duke/src/global.h index 57185f7e7..305920c80 100644 --- a/source/games/duke/src/global.h +++ b/source/games/duke/src/global.h @@ -96,7 +96,7 @@ extern float cloudy; extern int cloudclock; extern DDukeActor *spriteq[1024]; -extern int16_t cyclers[MAXCYCLERS][6]; +extern Cycler cyclers[MAXCYCLERS]; extern int mirrorsector[64]; extern int mirrorwall[64]; diff --git a/source/games/duke/src/premap_d.cpp b/source/games/duke/src/premap_d.cpp index d6c489fc6..4819e0a2f 100644 --- a/source/games/duke/src/premap_d.cpp +++ b/source/games/duke/src/premap_d.cpp @@ -299,12 +299,12 @@ void prelevel_d(int g) case CYCLER: if (numcyclers >= MAXCYCLERS) I_Error("Too many cycling sectors."); - cyclers[numcyclers][0] = si->sectnum; - cyclers[numcyclers][1] = si->lotag; - cyclers[numcyclers][2] = si->shade; - cyclers[numcyclers][3] = si->sector()->floorshade; - cyclers[numcyclers][4] = si->hitag; - cyclers[numcyclers][5] = (si->ang == 1536); + cyclers[numcyclers].sectnum = si->sectnum; + cyclers[numcyclers].lotag = si->lotag; + cyclers[numcyclers].shade1 = si->shade; + cyclers[numcyclers].shade2 = si->sector()->floorshade; + cyclers[numcyclers].hitag = si->hitag; + cyclers[numcyclers].state = (si->ang == 1536); numcyclers++; deletesprite(ac); break; diff --git a/source/games/duke/src/premap_r.cpp b/source/games/duke/src/premap_r.cpp index d8c90267a..e70494881 100644 --- a/source/games/duke/src/premap_r.cpp +++ b/source/games/duke/src/premap_r.cpp @@ -554,12 +554,12 @@ void prelevel_r(int g) case CYCLER: if (numcyclers >= MAXCYCLERS) I_Error("Too many cycling sectors."); - cyclers[numcyclers][0] = si->sectnum; - cyclers[numcyclers][1] = si->lotag; - cyclers[numcyclers][2] = si->shade; - cyclers[numcyclers][3] = si->sector()->floorshade; - cyclers[numcyclers][4] = si->hitag; - cyclers[numcyclers][5] = (si->ang == 1536); + cyclers[numcyclers].sectnum = si->sectnum; + cyclers[numcyclers].lotag = si->lotag; + cyclers[numcyclers].shade1 = si->shade; + cyclers[numcyclers].shade2 = si->sector()->floorshade; + cyclers[numcyclers].hitag = si->hitag; + cyclers[numcyclers].state = (si->ang == 1536); numcyclers++; deletesprite(ac); break; diff --git a/source/games/duke/src/savegame.cpp b/source/games/duke/src/savegame.cpp index 7eef2795b..924dffe1f 100644 --- a/source/games/duke/src/savegame.cpp +++ b/source/games/duke/src/savegame.cpp @@ -298,6 +298,23 @@ FSerializer& Serialize(FSerializer& arc, const char* keyname, DDukeActor& w, DDu return arc; } +FSerializer& Serialize(FSerializer& arc, const char* keyname, Cycler& w, Cycler* def) +{ + static Cycler nul; + if (!def) def = &nul; + if (arc.BeginObject(keyname)) + { + arc("sector", w.sectnum, def->sectnum) + ("lotag", w.lotag, def->lotag) + ("hitag", w.hitag, def->hitag) + ("shade1", w.shade1, def->shade1) + ("shade2", w.shade2, def->shade2) + ("state", w.state, def->state) + .EndObject(); + } + return arc; +} + void GameInterface::SerializeGameState(FSerializer& arc) { @@ -366,7 +383,7 @@ void GameInterface::SerializeGameState(FSerializer& arc) .Array("spriteq", spriteq, 1024) ("numcyclers", numcyclers) - .Array("cyclers", &cyclers[0][0], 6 * numcyclers) + .Array("cycler", cyclers, numcyclers) ("mirrorcnt", mirrorcnt) .Array("mirrorsector", mirrorsector, mirrorcnt) .Array("mirrorwall", mirrorwall, mirrorcnt) diff --git a/source/games/duke/src/sectors.cpp b/source/games/duke/src/sectors.cpp index 980ed4df2..b9ac18651 100644 --- a/source/games/duke/src/sectors.cpp +++ b/source/games/duke/src/sectors.cpp @@ -1077,21 +1077,23 @@ void operatesectors(int sn, DDukeActor *actor) void operateactivators(int low, int plnum) { int i, j, k; - short * p; + Cycler * p; walltype* wal; for (i = numcyclers - 1; i >= 0; i--) { - p = &cyclers[i][0]; + p = &cyclers[i]; - if (p[4] == low) + + if (p->hitag == low) { - p[5] = !p[5]; + auto sect = p->sector(); + p->state = !p->state; - sector[p[0]].floorshade = sector[p[0]].ceilingshade = (int8_t)p[3]; - wal = &wall[sector[p[0]].wallptr]; - for (j = sector[p[0]].wallnum; j > 0; j--, wal++) - wal->shade = (int8_t)p[3]; + sect->floorshade = sect->ceilingshade = (int8_t)p->shade2; + wal = &wall[sect->wallptr]; + for (j = sect->wallnum; j > 0; j--, wal++) + wal->shade = (int8_t)p->shade2; } } diff --git a/source/games/duke/src/types.h b/source/games/duke/src/types.h index d60c76064..309187541 100644 --- a/source/games/duke/src/types.h +++ b/source/games/duke/src/types.h @@ -295,6 +295,18 @@ struct player_struct }; +struct Cycler +{ + int sectnum; + int16_t lotag; + int16_t hitag; + int16_t shade1; + int16_t shade2; + bool state; + + sectortype* sector() const { return &::sector[sectnum]; } +}; + // Wrapper around the insane collision info mess from Build. struct Collision {