From fe30cbb4aa57df913127f74646f37e13da03a958 Mon Sep 17 00:00:00 2001 From: Alug Date: Sat, 13 Sep 2025 14:32:57 +0200 Subject: [PATCH] big cleanup levelloading code - made levelfadecol a local variable - renamed all the "Raw" functions - moved axis mapthing spawn into the mapthing spawn function - moved mapthing load to mapdata load, saves opening the THING virtual lump twice - split out linedef init out of the loop it now gets done within linedef load code saving the hassle of iterating through all the lines twice - removed a bunch of dead code and useless variables - moved virtual resource free to after the mapdata has been loaded completely - sidedef loop now increments pointer instead of reassinging it each iteration - lastloadedmaplumpnum now gets checked for LUMPERROR instead of INT16_MAX since W_CheckNumForName returns LUMPERROR if lump was not found --- src/p_setup.c | 371 ++++++++++++++++++++++++-------------------------- src/p_setup.h | 1 - 2 files changed, 179 insertions(+), 193 deletions(-) diff --git a/src/p_setup.c b/src/p_setup.c index 54f17cea8..5e1bca43e 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -116,7 +116,6 @@ line_t *spawnlines; side_t *spawnsides; INT32 numstarposts; boolean levelloading; -UINT8 levelfadecol; virtres_t *curmapvirt; @@ -370,7 +369,7 @@ UINT32 P_GetScoreForGrade(INT16 map, UINT8 mare, UINT8 grade) } // Loads the vertexes for a level. -FUNCINLINE static ATTRINLINE void P_LoadRawVertexes(UINT8 *data) +FUNCINLINE static ATTRINLINE void P_LoadVertices(UINT8 *data) { mapvertex_t *ml = (mapvertex_t *)data; vertex_t *li = vertexes; @@ -549,7 +548,7 @@ static void P_SetupDirectionalLight(void) } // Loads the SEGS resource from a level. -static void P_LoadRawSegs(UINT8 *data) +static void P_LoadSegs(UINT8 *data) { INT32 rawlinedef, rawside; mapseg_t *ml = (mapseg_t*)data; @@ -578,14 +577,16 @@ static void P_LoadRawSegs(UINT8 *data) li->side = rawside = SHORT(ml->side); li->sidedef = &sides[ldef->sidenum[rawside]]; li->frontsector = sides[ldef->sidenum[rawside]].sector; + if (ldef->flags & ML_TWOSIDED) li->backsector = sides[ldef->sidenum[rawside^1]].sector; + P_UpdateSegLightOffset(li); } } // Loads the SSECTORS resource from a level. -FUNCINLINE static ATTRINLINE void P_LoadRawSubsectors(void *data) +FUNCINLINE static ATTRINLINE void P_LoadSubsectors(void *data) { mapsubsector_t *ms = (mapsubsector_t*)data; subsector_t *ss = subsectors; @@ -784,7 +785,7 @@ static const sector_t sector_default = { }; // Sets up the ingame sectors structures. -static void P_LoadRawSectors(UINT8 *data) +static void P_LoadSectors(UINT8 *data) { mapsector_t *ms = (mapsector_t *)data; sector_t *ss = sectors; @@ -831,7 +832,7 @@ static void P_LoadRawSectors(UINT8 *data) // // P_LoadNodes // -static void P_LoadRawNodes(UINT8 *data) +static void P_LoadNodes(UINT8 *data) { UINT8 j, k; mapnode_t *mn = (mapnode_t*)data; @@ -858,15 +859,12 @@ static void P_LoadRawNodes(UINT8 *data) // P_LoadThings // -static void P_PrepareRawThings(UINT8 *data) +static void P_LoadThings(UINT8 *data) { mapthing_t *mt; size_t i; - // Spawn axis points first so they are - // at the front of the list for fast searching. - mt = mapthings; - for (i = 0; i < nummapthings; i++, mt++) + for (i = 0, mt = mapthings; i < nummapthings; i++, mt++) { mt->x = READINT16(data); mt->y = READINT16(data); @@ -876,7 +874,19 @@ static void P_PrepareRawThings(UINT8 *data) mt->extrainfo = (UINT8)(mt->type >> 12); mt->type &= 4095; + } +} +static void P_SpawnMapThings(void) +{ + size_t i; + mapthing_t *mt; + + numhuntemeralds = 0; + + // Spawn axis points first so they are at the front of the list for fast searching. + for (i = 0, mt = mapthings; i < nummapthings; i++, mt++) + { switch (mt->type) { case 1700: // MT_AXIS @@ -889,19 +899,8 @@ static void P_PrepareRawThings(UINT8 *data) break; } } -} -static void P_LoadThings(void) -{ - size_t i; - mapthing_t *mt; - - // Loading the things lump itself into memory is now handled in P_PrepareThings, above - - mt = mapthings; - numhuntemeralds = 0; - - for (i = 0; i < nummapthings; i++, mt++) + for (i = 0, mt = mapthings; i < nummapthings; i++, mt++) { sector_t *mtsector = R_PointInSubsector((mt->x << FRACBITS), (mt->y << FRACBITS))->sector; @@ -968,8 +967,7 @@ static void P_LoadThings(void) return; // Run through the list of mapthings again to spawn hoops and rings - mt = mapthings; - for (i = 0; i < nummapthings; i++, mt++) + for (i = 0, mt = mapthings; i < nummapthings; i++, mt++) { if (mt->type == 300 || mt->type == 308 || mt->type == 309 || mt->type == 1706 || (mt->type >= 600 && mt->type <= 609) @@ -981,16 +979,20 @@ static void P_LoadThings(void) mt->z = (INT16)(R_PointInSubsector(mt->x << FRACBITS, mt->y << FRACBITS) ->sector->floorheight>>FRACBITS); - P_SpawnHoopsAndRings (mt); + P_SpawnHoopsAndRings(mt); } } } -static inline void P_SpawnEmblems(void) +static void P_SpawnEmblems(boolean loademblems) { INT32 i, color; mobj_t *emblemmobj; + // Now let's spawn those funky emblem things! Tails 12-08-2002 + if (!loademblems || netgame || multiplayer) // No cheating!! + return; + for (i = 0; i < numemblems; i++) { if (emblemlocations[i].level != gamemap || emblemlocations[i].type > ET_SKIN) @@ -1022,16 +1024,6 @@ static inline void P_SpawnEmblems(void) } } -static void P_SpawnSecretItems(boolean loademblems) -{ - // Now let's spawn those funky emblem things! Tails 12-08-2002 - if (netgame || multiplayer) // No cheating!! - return; - - if (loademblems) - P_SpawnEmblems(); -} - // Experimental groovy write function! void P_WriteThings(lumpnum_t lumpnum) { @@ -1051,8 +1043,7 @@ void P_WriteThings(lumpnum_t lumpnum) return; } - mt = mapthings; - for (i = 0; i < nummapthings; i++, mt++) + for (i = 0, mt = mapthings; i < nummapthings; i++, mt++) { WRITEINT16(save.p, mt->x); WRITEINT16(save.p, mt->y); @@ -1075,7 +1066,86 @@ void P_WriteThings(lumpnum_t lumpnum) CONS_Printf(M_GetText("newthings%d.lmp saved.\n"), gamemap); } -static void P_LoadRawLineDefs(UINT8 *data) +static void P_InitializeLinedef(line_t *ld) +{ + UINT8 j; + + vertex_t *v1 = ld->v1; + vertex_t *v2 = ld->v2; + + ld->dx = v2->x - v1->x; + ld->dy = v2->y - v1->y; + + ld->angle = R_PointToAngle2(0, 0, ld->dx, ld->dy); + + ld->alpha = FRACUNIT; + + if (!ld->dx) + ld->slopetype = ST_VERTICAL; + else if (!ld->dy) + ld->slopetype = ST_HORIZONTAL; + else if ((ld->dy > 0) == (ld->dx > 0)) + ld->slopetype = ST_POSITIVE; + else + ld->slopetype = ST_NEGATIVE; + + if (v1->x < v2->x) + { + ld->bbox[BOXLEFT] = v1->x; + ld->bbox[BOXRIGHT] = v2->x; + } + else + { + ld->bbox[BOXLEFT] = v2->x; + ld->bbox[BOXRIGHT] = v1->x; + } + + if (v1->y < v2->y) + { + ld->bbox[BOXBOTTOM] = v1->y; + ld->bbox[BOXTOP] = v2->y; + } + else + { + ld->bbox[BOXBOTTOM] = v2->y; + ld->bbox[BOXTOP] = v1->y; + } + + // cph 2006/09/30 - fix sidedef errors right away. + // cph 2002/07/20 - these errors are fatal if not fixed, so apply them + for (j = 0; j < 2; j++) + { + if (ld->sidenum[j] != 0xffff && ld->sidenum[j] >= (UINT16)numsides) + { + ld->sidenum[j] = 0xffff; + CONS_Debug(DBG_SETUP, "P_InitializeLinedef: Linedef %s has out-of-range sidedef number\n", sizeu1((size_t)(ld - lines))); + } + } + + ld->firsttag = ld->nexttag = -1; + + // killough 11/98: fix common wad errors (missing sidedefs): + if (ld->sidenum[0] == 0xffff) + { + ld->sidenum[0] = 0; // Substitute dummy sidedef for missing right side + // cph - print a warning about the bug + CONS_Debug(DBG_SETUP, "P_InitializeLinedef: Linedef %s missing first sidedef\n", sizeu1((size_t)(ld - lines))); + } + + if ((ld->sidenum[1] == 0xffff) && (ld->flags & ML_TWOSIDED)) + { + ld->flags &= ~ML_TWOSIDED; // Clear 2s flag for missing left side + // cph - print a warning about the bug + CONS_Debug(DBG_SETUP, "P_InitializeLinedef: Linedef %s has two-sided flag set, but no second sidedef\n", sizeu1((size_t)(ld - lines))); + } + + if (ld->sidenum[0] != 0xffff && ld->special) + sides[ld->sidenum[0]].special = ld->special; + if (ld->sidenum[1] != 0xffff && ld->special) + sides[ld->sidenum[1]].special = ld->special; +} + +static void P_LoadLinedefs(UINT8 *data) { maplinedef_t *mld = (maplinedef_t *)data; line_t *ld = lines; @@ -1091,93 +1161,8 @@ static void P_LoadRawLineDefs(UINT8 *data) ld->sidenum[0] = SHORT(mld->sidenum[0]); ld->sidenum[1] = SHORT(mld->sidenum[1]); - } -} -static void P_SetupLines(void) -{ - line_t *ld = lines; - size_t i; - - for (i = 0; i < numlines; i++, ld++) - { - vertex_t *v1 = ld->v1; - vertex_t *v2 = ld->v2; - - ld->dx = v2->x - v1->x; - ld->dy = v2->y - v1->y; - - ld->angle = R_PointToAngle2(0, 0, ld->dx, ld->dy); - - ld->alpha = FRACUNIT; - - if (!ld->dx) - ld->slopetype = ST_VERTICAL; - else if (!ld->dy) - ld->slopetype = ST_HORIZONTAL; - else if ((ld->dy > 0) == (ld->dx > 0)) - ld->slopetype = ST_POSITIVE; - else - ld->slopetype = ST_NEGATIVE; - - if (v1->x < v2->x) - { - ld->bbox[BOXLEFT] = v1->x; - ld->bbox[BOXRIGHT] = v2->x; - } - else - { - ld->bbox[BOXLEFT] = v2->x; - ld->bbox[BOXRIGHT] = v1->x; - } - - if (v1->y < v2->y) - { - ld->bbox[BOXBOTTOM] = v1->y; - ld->bbox[BOXTOP] = v2->y; - } - else - { - ld->bbox[BOXBOTTOM] = v2->y; - ld->bbox[BOXTOP] = v1->y; - } - - { - // cph 2006/09/30 - fix sidedef errors right away. - // cph 2002/07/20 - these errors are fatal if not fixed, so apply them - UINT8 j; - - for (j = 0; j < 2; j++) - { - if (ld->sidenum[j] != 0xffff && ld->sidenum[j] >= (UINT16)numsides) - { - ld->sidenum[j] = 0xffff; - CONS_Debug(DBG_SETUP, "P_LoadRawLineDefs: linedef %s has out-of-range sidedef number\n", sizeu1(numlines-i-1)); - } - } - } - - ld->firsttag = ld->nexttag = -1; - - // killough 11/98: fix common wad errors (missing sidedefs): - if (ld->sidenum[0] == 0xffff) - { - ld->sidenum[0] = 0; // Substitute dummy sidedef for missing right side - // cph - print a warning about the bug - CONS_Debug(DBG_SETUP, "Linedef %s missing first sidedef\n", sizeu1(numlines-i-1)); - } - - if ((ld->sidenum[1] == 0xffff) && (ld->flags & ML_TWOSIDED)) - { - ld->flags &= ~ML_TWOSIDED; // Clear 2s flag for missing left side - // cph - print a warning about the bug - CONS_Debug(DBG_SETUP, "Linedef %s has two-sided flag set, but no second sidedef\n", sizeu1(numlines-i-1)); - } - - if (ld->sidenum[0] != 0xffff && ld->special) - sides[ld->sidenum[0]].special = ld->special; - if (ld->sidenum[1] != 0xffff && ld->special) - sides[ld->sidenum[1]].special = ld->special; + P_InitializeLinedef(ld); } } @@ -1223,7 +1208,6 @@ static void P_LoadLineDefs2(void) case 900 ... 908: ld->alpha = ((909 - ld->special) << FRACBITS)/10; break; - // Set alpha for additive/subtractive/reverse subtractive walls case 910 ... 919: // additive ld->alpha = ((10 - ld->special % 10) << FRACBITS)/10; @@ -1377,34 +1361,27 @@ static void P_LoadSideChangeMusic(boolean firstside, mapsidedef_t *msd, side_t * sd->text[0] = 0; } -static void P_LoadRawSideDefs2(void *data) +static void P_LoadSidedefs(void *data) { - UINT16 i; + mapsidedef_t *msd = (mapsidedef_t *)data; + side_t *sd = sides; + size_t i; - for (i = 0; i < numsides; i++) + for (i = 0; i < numsides; i++, sd++, msd++) { - register mapsidedef_t *msd = (mapsidedef_t *)data + i; - register side_t *sd = sides + i; - register sector_t *sec; - sd->textureoffset = SHORT(msd->textureoffset)<rowoffset = SHORT(msd->rowoffset)<sector); + UINT16 sector_num = SHORT(msd->sector); - if (sector_num >= numsectors) - { - CONS_Debug(DBG_SETUP, "P_LoadRawSideDefs2: sidedef %u has out-of-range sector num %u\n", i, sector_num); - sector_num = 0; - } - sd->sector = sec = §ors[sector_num]; + // cph 2006/09/30 - catch out-of-range sector numbers; use sector 0 instead + if (sector_num >= numsectors) + { + CONS_Debug(DBG_SETUP, "P_LoadSidedefs: sidedef %s has out-of-range sector num %u\n", sizeu1(i), sector_num); + sector_num = 0; } - // refined to allow colormaps to work as wall textures if invalid as colormaps - // but valid as textures. - - sd->sector = sec = §ors[SHORT(msd->sector)]; + sd->sector = §ors[sector_num]; // Colormaps! switch (sd->special) @@ -1413,7 +1390,7 @@ static void P_LoadRawSideDefs2(void *data) case 606: //SoM: 4/4/2000: Just colormap transfer // SoM: R_CreateColormap will only create a colormap in software mode... // Perhaps we should just call it instead of doing the calculations here. - P_LoadSideColormaps(msd, sd, sec); + P_LoadSideColormaps(msd, sd, sd->sector); break; case 413: // Change music P_LoadSideChangeMusic((i == 1), msd, sd); @@ -1615,7 +1592,8 @@ static void P_CreateBlockMap(void) bmap_t *bmap = calloc(tot, sizeof (*bmap)); // array of blocklists boolean straight; - if (bmap == NULL) I_Error("%s: Out of memory making blockmap", "P_CreateBlockMap"); + if (bmap == NULL) + I_Error("%s: Out of memory making blockmap", "P_CreateBlockMap"); for (i = 0; i < numlines; i++) { @@ -1691,7 +1669,9 @@ static void P_CreateBlockMap(void) bmap[b].nalloc = 8; else bmap[b].nalloc *= 2; + bmap[b].list = Z_Realloc(bmap[b].list, bmap[b].nalloc * sizeof (*bmap->list), PU_CACHE, &bmap[b].list); + if (!bmap[b].list) I_Error("Out of Memory in P_CreateBlockMap"); } @@ -1727,6 +1707,7 @@ static void P_CreateBlockMap(void) blockmaplump[ndx++] = -1; // (Used for compression) for (i = 4; i < tot; i++, bp++) + { if (bp->n) // Non-empty blocklist { blockmaplump[blockmaplump[i] = (INT32)(ndx++)] = 0; // Store index & header @@ -1738,6 +1719,7 @@ static void P_CreateBlockMap(void) } else // Empty blocklist: point to reserved empty blocklist blockmaplump[i] = (INT32)tot; + } free(bmap); // Free uncompressed blockmap } @@ -1915,12 +1897,12 @@ static void P_GroupLines(void) // PK3 version // -- Monster Iestyn 09/01/18 -static void P_LoadRawReject(UINT8 *data, size_t count) +static void P_LoadReject(UINT8 *data, size_t count) { if (!count) // zero length, someone probably used ZDBSP { rejectmatrix = NULL; - CONS_Debug(DBG_SETUP, "P_LoadRawReject: REJECT lump has size 0, will not be loaded\n"); + CONS_Debug(DBG_SETUP, "P_LoadReject: REJECT lump has size 0, will not be loaded\n"); } else { @@ -1951,9 +1933,9 @@ static void P_LoadMapBSP(const virtres_t* virt) segs = Z_Calloc(numsegs * sizeof(*segs), PU_LEVEL, NULL); // Nodes - P_LoadRawSubsectors(virtssectors->data); - P_LoadRawNodes(virtnodes->data); - P_LoadRawSegs(virtsegs->data); + P_LoadSubsectors(virtssectors->data); + P_LoadNodes(virtnodes->data); + P_LoadSegs(virtsegs->data); } static void P_LoadMapLUT(const virtres_t* virt) @@ -1963,7 +1945,7 @@ static void P_LoadMapLUT(const virtres_t* virt) // Lookup tables if (virtreject) - P_LoadRawReject(virtreject->data, virtreject->size); + P_LoadReject(virtreject->data, virtreject->size); else rejectmatrix = NULL; @@ -2016,11 +1998,11 @@ static void P_LoadMapData(const virtres_t* virt) mapthings = Z_Calloc(nummapthings * sizeof(*mapthings), PU_LEVEL, NULL); // Strict map data - P_LoadRawVertexes(virtvertexes->data); - P_LoadRawSectors(virtsectors->data); - P_LoadRawLineDefs(virtlinedefs->data); - P_SetupLines(); - P_LoadRawSideDefs2(virtsidedefs->data); + P_LoadVertices(virtvertexes->data); + P_LoadSectors(virtsectors->data); + P_LoadLinedefs(virtlinedefs->data); + P_LoadSidedefs(virtsidedefs->data); + P_LoadThings(virtthings->data); // Copy relevant map data for NetArchive purposes. spawnsectors = Z_Calloc(numsectors * sizeof(*sectors), PU_LEVEL, NULL); @@ -2250,19 +2232,7 @@ static void P_LoadMapFromFile(void) P_LoadLineDefs2(); P_GroupLines(); - P_PrepareRawThings(vres_Find(curmapvirt, "THINGS")->data); - P_MakeMapMD5(curmapvirt, &mapmd5); - - // We do the following silly - // construction because vres_Free - // no-sells deletions of pointers - // that are == curmapvirt. - { - virtres_t *temp = curmapvirt; - curmapvirt = NULL; - vres_Free(temp); - } } static void P_RunLevelScript(const char *scriptname) @@ -2443,14 +2413,13 @@ static void P_SetupCamera(UINT8 pnum, camera_t *cam) switch (gametype) { - case GT_MATCH: - case GT_TAG: - thing = deathmatchstarts[0]; - break; - - default: - thing = playerstarts[0]; - break; + case GT_MATCH: + case GT_TAG: + thing = deathmatchstarts[0]; + break; + default: + thing = playerstarts[0]; + break; } if (!thing) @@ -2688,12 +2657,15 @@ static void P_SetupPlayer(void) } else if (G_RaceGametype() && server) - CV_StealthSetValue(&cv_numlaps, - ((netgame || multiplayer) && cv_basenumlaps.value - && (!(mapheaderinfo[gamemap - 1]->levelflags & LF_SECTIONRACE) - || (mapheaderinfo[gamemap - 1]->numlaps > cv_basenumlaps.value))) - ? cv_basenumlaps.value - : mapheaderinfo[gamemap - 1]->numlaps); + { + INT32 numLaps = ((netgame || multiplayer) && cv_basenumlaps.value + && (!(mapheaderinfo[gamemap - 1]->levelflags & LF_SECTIONRACE) + || (mapheaderinfo[gamemap - 1]->numlaps > cv_basenumlaps.value))) + ? cv_basenumlaps.value + : mapheaderinfo[gamemap - 1]->numlaps; + + CV_StealthSetValue(&cv_numlaps, numLaps); + } // Start recording replay in multiplayer with a temp filename // Ensure dedis only record a replay if there is a player at the start of the map, otherwise we get invalid replays! @@ -2752,6 +2724,8 @@ boolean P_SetupLevel(boolean fromnetsave, boolean reloadinggamestate) INT32 i; boolean ranspecialwipe = false; sector_t *ss; + lumpnum_t encoreLump = LUMPERROR; + UINT8 levelfadecol; midgamejoin = fromnetsave; // makes dynslopes run in P_Ticker/P_PreTicker to avoid synch issues and other stuff @@ -2895,13 +2869,14 @@ boolean P_SetupLevel(boolean fromnetsave, boolean reloadinggamestate) // internal game map maplumpname = G_BuildMapName(gamemap); lastloadedmaplumpnum = W_CheckNumForName(maplumpname); - if (lastloadedmaplumpnum == INT16_MAX) + if (lastloadedmaplumpnum == LUMPERROR) I_Error("Map %s not found.\n", maplumpname); curmapvirt = vres_GetMap(lastloadedmaplumpnum); - R_ReInitColormaps(mapheaderinfo[gamemap-1]->palette, - W_CheckNumForName(va("%s%c", maplumpname, (encoremode ? 'E' : 'T')))); + encoreLump = W_CheckNumForName(va("%s%c", maplumpname, (encoremode ? 'E' : 'T'))); + R_ReInitColormaps(mapheaderinfo[gamemap-1]->palette, encoreLump); + CON_SetupBackColormap(); // SRB2 determines the sky texture to be used depending on the map header. @@ -2915,14 +2890,13 @@ boolean P_SetupLevel(boolean fromnetsave, boolean reloadinggamestate) P_MapStart(); - if (lastloadedmaplumpnum) - P_LoadMapFromFile(); + P_LoadMapFromFile(); P_ResetDynamicSlopes(); - P_LoadThings(); + P_SpawnMapThings(); - P_SpawnSecretItems(!fromnetsave); + P_SpawnEmblems(!fromnetsave); P_InitMinimapInfo(); @@ -2954,6 +2928,17 @@ boolean P_SetupLevel(boolean fromnetsave, boolean reloadinggamestate) if (!fromnetsave) P_SetupPlayer(); + // Now safe to free. + // We do the following silly + // construction because vres_Free + // no-sells deletions of pointers + // that are == curmapvirt. + { + virtres_t *temp = curmapvirt; + curmapvirt = NULL; + vres_Free(temp); + } + if (!reloadinggamestate) { P_InitCamera(); @@ -3008,7 +2993,9 @@ boolean P_SetupLevel(boolean fromnetsave, boolean reloadinggamestate) for (i = 0; i < MAXPLAYERS; i++) { if (playeringame[i]) + { G_CopyTiccmd(&players[i].cmd, &netcmds[buf][i], 1); + } } P_PreTicker(2); diff --git a/src/p_setup.h b/src/p_setup.h index 6d76c05ab..dc656cf4a 100644 --- a/src/p_setup.h +++ b/src/p_setup.h @@ -35,7 +35,6 @@ extern mapthing_t *deathmatchstarts[MAX_DM_STARTS]; extern INT32 numdmstarts, numcoopstarts, numredctfstarts, numbluectfstarts; extern boolean levelloading; -extern UINT8 levelfadecol; extern lumpnum_t lastloadedmaplumpnum; // for comparative savegame extern virtres_t *curmapvirt;