diff --git a/src/Makefile.d/versions.mk b/src/Makefile.d/versions.mk index ce23c3e4e..e74321bd1 100644 --- a/src/Makefile.d/versions.mk +++ b/src/Makefile.d/versions.mk @@ -21,9 +21,6 @@ CXXFLAGS+= -Wno-aggregate-return # -W -Wno-unused -ifdef GCC150 - opts+=-std=gnu17 -endif WFLAGS:=-Wall -Wno-trigraphs ifndef GCC295 #WFLAGS+=-Wno-packed diff --git a/src/g_demo.c b/src/g_demo.c index 820ef45fb..33957fb67 100644 --- a/src/g_demo.c +++ b/src/g_demo.c @@ -1051,7 +1051,7 @@ void G_ConsGhostTic(INT32 playernum) mobj = NULL; for (th = thinkercap.next; th != &thinkercap; th = th->next) { - if (th->function.acp1 != (actionf_p1)P_MobjThinker) + if (th->function != (actionf_p1)P_MobjThinker) continue; mobj = (mobj_t *)th; if (mobj->type == (mobjtype_t)type && mobj->x == x && mobj->y == y && mobj->z == z) @@ -3701,7 +3701,7 @@ void G_DoPlayMetal(void) // find metal sonic for (th = thinkercap.next; th != &thinkercap; th = th->next) { - if (th->function.acp1 != (actionf_p1)P_MobjThinker) + if (th->function != (actionf_p1)P_MobjThinker) continue; mo = (mobj_t *)th; diff --git a/src/g_game.c b/src/g_game.c index 6ec1eb132..d02c610fa 100644 --- a/src/g_game.c +++ b/src/g_game.c @@ -4222,3944 +4222,6 @@ INT32 G_FindMapByNameOrCode(const char *mapname, char **realmapnamep) return newmapnum; } -// -// DEMO RECORDING -// - -#define DEMOVERSION 0x0002 -#define DEMOHEADER "\xF0" "KartReplay" "\x0F" - -#define DF_GHOST 0x01 // This demo contains ghost data too! -#define DF_RECORDATTACK 0x02 // This demo is from record attack and contains its final completion time! -#define DF_NIGHTSATTACK 0x04 // This demo is from NiGHTS attack and contains its time left, score, and mares! -#define DF_ATTACKMASK 0x06 // This demo is from ??? attack and contains ??? - -#define DF_LUAVARS 0x20 // this demo contains extra lua vars; this is mostly used for backwards compability - -#define DF_ATTACKSHIFT 1 -#define DF_ENCORE 0x40 -#define DF_MULTIPLAYER 0x80 // This demo was recorded in multiplayer mode! - -#ifdef DEMO_COMPAT_100 -#define DF_FILELIST 0x08 // This demo contains an extra files list -#define DF_GAMETYPEMASK 0x30 -#define DF_GAMESHIFT 4 -#endif - -#define DEMO_SPECTATOR 0x40 - -// For demos -#define ZT_FWD 0x01 -#define ZT_SIDE 0x02 -#define ZT_ANGLE 0x04 -#define ZT_BUTTONS 0x08 -#define ZT_AIMING 0x10 -#define ZT_DRIFT 0x20 -#define ZT_LATENCY 0x40 -#define DEMOMARKER 0x80 // demoend - -UINT8 demo_extradata[MAXPLAYERS]; -UINT8 demo_writerng; // 0=no, 1=yes, 2=yes but on a timeout -static ticcmd_t oldcmd[MAXPLAYERS]; - -#define DW_END 0xFF // End of extradata block -#define DW_RNG 0xFE // Check RNG seed! - -#define DW_EXTRASTUFF 0xFE // Numbers below this are reserved for writing player slot data - -// Below consts are only used for demo extrainfo sections -#define DW_STANDING 0x00 - -// For Metal Sonic and time attack ghosts -#define GZT_XYZ 0x01 -#define GZT_MOMXY 0x02 -#define GZT_MOMZ 0x04 -#define GZT_ANGLE 0x08 -// Not used for Metal Sonic -#define GZT_SPRITE 0x10 // Animation frame -#define GZT_EXTRA 0x20 -#define GZT_NIGHTS 0x40 // NiGHTS Mode stuff! - -// GZT_EXTRA flags -#define EZT_THOK 0x01 // Spawned a thok object -#define EZT_SPIN 0x02 // Because one type of thok object apparently wasn't enough -#define EZT_REV 0x03 // And two types wasn't enough either yet -#define EZT_THOKMASK 0x03 -#define EZT_COLOR 0x04 // Changed color (Super transformation, Mario fireflowers/invulnerability, etc.) -#define EZT_FLIP 0x08 // Reversed gravity -#define EZT_SCALE 0x10 // Changed size -#define EZT_HIT 0x20 // Damaged a mobj -#define EZT_SPRITE 0x40 // Changed sprite set completely out of PLAY (NiGHTS, SOCs, whatever) -#define EZT_KART 0x80 // SRB2Kart: Changed current held item/quantity and bumpers for battle - -static mobj_t oldmetal, oldghost[MAXPLAYERS]; - -void G_SaveMetal(UINT8 **buffer) -{ - I_Assert(buffer != NULL && *buffer != NULL); - - WRITEUINT32(*buffer, metal_p - metalbuffer); -} - -void G_LoadMetal(UINT8 **buffer) -{ - I_Assert(buffer != NULL && *buffer != NULL); - - G_DoPlayMetal(); - metal_p = metalbuffer + READUINT32(*buffer); -} - -ticcmd_t *G_CopyTiccmd(ticcmd_t* dest, const ticcmd_t* src, const size_t n) -{ - return M_Memcpy(dest, src, n*sizeof(*src)); -} - -ticcmd_t *G_MoveTiccmd(ticcmd_t* dest, const ticcmd_t* src, const size_t n) -{ - size_t i; - for (i = 0; i < n; i++) - { - dest[i].forwardmove = src[i].forwardmove; - dest[i].sidemove = src[i].sidemove; - dest[i].angleturn = SHORT(src[i].angleturn); - dest[i].aiming = (INT16)SHORT(src[i].aiming); - dest[i].buttons = (UINT16)SHORT(src[i].buttons); - dest[i].driftturn = (INT16)SHORT(src[i].driftturn); - dest[i].latency = (INT16)SHORT(src[i].latency); - } - return dest; -} - -// Finds a skin with the closest stats if the expected skin doesn't exist. -static INT32 GetSkinNumClosestToStats(UINT8 kartspeed, UINT8 kartweight) -{ - INT32 i, closest_skin = 0; - UINT8 closest_stats = UINT8_MAX, stat_diff; - - for (i = 0; i < numskins; i++) - { - stat_diff = abs(skins[i].kartspeed - kartspeed) + abs(skins[i].kartweight - kartweight); - if (stat_diff < closest_stats) - { - closest_stats = stat_diff; - closest_skin = i; - } - } - - return closest_skin; -} - -static void FindClosestSkinForStats(UINT32 p, UINT8 kartspeed, UINT8 kartweight) -{ - INT32 closest_skin = GetSkinNumClosestToStats(kartspeed, kartweight); - - //CONS_Printf("Using %s instead...\n", skins[closest_skin].name); - SetPlayerSkinByNum(p, closest_skin); -} - -void G_ReadDemoExtraData(void) -{ - INT32 p, extradata, i; - char name[17]; - - if (leveltime > starttime) - { - rewind_t *rewind = CL_SaveRewindPoint(demobuf.p - demobuf.buffer); - if (rewind) - { - memcpy(rewind->oldcmd, oldcmd, sizeof (oldcmd)); - memcpy(rewind->oldghost, oldghost, sizeof (oldghost)); - } - } - - memset(name, '\0', 17); - - p = READUINT8(demobuf.p); - - while (p < DW_EXTRASTUFF) - { - extradata = READUINT8(demobuf.p); - - if (extradata & DXD_RESPAWN) - { - if (players[p].mo) - P_DamageMobj(players[p].mo, NULL, NULL, DMG_INSTAKILL); // Is this how this should work..? - } - if (extradata & DXD_SKIN) - { - UINT8 kartspeed, kartweight; - - // Skin - M_Memcpy(name, demobuf.p, 16); - demobuf.p += 16; - SetPlayerSkin(p, name); - - kartspeed = READUINT8(demobuf.p); - kartweight = READUINT8(demobuf.p); - - if (stricmp(skins[players[p].skin].name, name) != 0) - FindClosestSkinForStats(p, kartspeed, kartweight); - - players[p].kartspeed = kartspeed; - players[p].kartweight = kartweight; - } - if (extradata & DXD_COLOR) - { - // Color - M_Memcpy(name, demobuf.p, 16); - demobuf.p += 16; - for (i = 0; i < MAXSKINCOLORS; i++) - if (!stricmp(KartColor_Names[i], name)) // SRB2kart - { - players[p].skincolor = i; - if (players[p].mo) - players[p].mo->color = i; - break; - } - } - if (extradata & DXD_NAME) - { - // Name - M_Memcpy(player_names[p],demobuf.p,16); - demobuf.p += 16; - } - if (extradata & DXD_PLAYSTATE) - { - extradata = READUINT8(demobuf.p); - - switch (extradata) { - case DXD_PST_PLAYING: - players[p].pflags |= PF_WANTSTOJOIN; // fuck you - break; - - case DXD_PST_SPECTATING: - players[p].pflags &= ~PF_WANTSTOJOIN; // double-fuck you - if (!playeringame[p]) - { - CL_ClearPlayer(p); - playeringame[p] = true; - G_AddPlayer(p); - players[p].spectator = true; - - // There's likely an off-by-one error in timing recording or playback of joins. This hacks around it so I don't have to find out where that is. \o/ - if (oldcmd[p].forwardmove) - P_RandomByte(); - } - else - { - players[p].spectator = true; - if (players[p].mo) - P_DamageMobj(players[p].mo, NULL, NULL, DMG_INSTAKILL); - else - players[p].playerstate = PST_REBORN; - } - break; - - case DXD_PST_LEFT: - CL_RemovePlayer(p, 0); - break; - } - - G_ResetViews(); - - // maybe these are necessary? - if (G_BattleGametype()) - K_CheckBumpers(); // SRB2Kart - else if (G_RaceGametype()) - P_CheckRacers(); // also SRB2Kart - } - - - p = READUINT8(demobuf.p); - } - - while (p != DW_END) - { - UINT32 rng; - UINT32 checkrng; - - switch (p) - { - case DW_RNG: - rng = READUINT32(demobuf.p); - checkrng = P_GetRandSeed(); - if (checkrng != rng) - { - if (demosynced) - { - CONS_Alert(CONS_WARNING, M_GetText("Demo playback has desynced (RNG)!\n")); - CONS_Printf("expected rng %d got %d\n", rng, checkrng); - } - demosynced = false; - - P_SetRandSeed(rng); - } - } - - p = READUINT8(demobuf.p); - } - - if (!(demoflags & DF_GHOST) && *demobuf.p == DEMOMARKER) - { - // end of demo data stream - G_CheckDemoStatus(); - return; - } -} - -void G_WriteDemoExtraData(void) -{ - INT32 i; - char name[17]; - - for (i = 0; i < MAXPLAYERS; i++) - { - if (demo_extradata[i]) - { - WRITEUINT8(demobuf.p, i); - WRITEUINT8(demobuf.p, demo_extradata[i]); - - //if (demo_extradata[i] & DXD_RESPAWN) has no extra data - if (demo_extradata[i] & DXD_SKIN) - { - // Skin - memset(name, 0, 16); - strncpy(name, skins[players[i].skin].name, 16); - M_Memcpy(demobuf.p,name,16); - demobuf.p += 16; - - WRITEUINT8(demobuf.p, skins[players[i].skin].kartspeed); - WRITEUINT8(demobuf.p, skins[players[i].skin].kartweight); - } - if (demo_extradata[i] & DXD_COLOR) - { - // Color - memset(name, 0, 16); - strncpy(name, KartColor_Names[players[i].skincolor], 16); - M_Memcpy(demobuf.p,name,16); - demobuf.p += 16; - } - if (demo_extradata[i] & DXD_NAME) - { - // Name - memset(name, 0, 16); - memcpy(name, player_names[i], 15); // Keeping 1 null byte for safety, sorry players with name containing more than 15 characters - M_Memcpy(demobuf.p,name,16); - demobuf.p += 16; - } - if (demo_extradata[i] & DXD_PLAYSTATE) - { - demo_writerng = 1; - if (!playeringame[i]) - WRITEUINT8(demobuf.p, DXD_PST_LEFT); - else if ( - players[i].spectator && - !(players[i].pflags & PF_WANTSTOJOIN) // <= fuck you specifically - ) - WRITEUINT8(demobuf.p, DXD_PST_SPECTATING); - else - WRITEUINT8(demobuf.p, DXD_PST_PLAYING); - } - } - - demo_extradata[i] = 0; - } - - // May not be necessary, but might as well play it safe... - if ((leveltime & 255) == 128) - demo_writerng = 1; - - { - static UINT8 timeout = 0; - - if (timeout) timeout--; - - if (demo_writerng == 1 || (demo_writerng == 2 && timeout == 0)) - { - demo_writerng = 0; - timeout = 16; - WRITEUINT8(demobuf.p, DW_RNG); - WRITEUINT32(demobuf.p, P_GetRandSeed()); - } - } - - WRITEUINT8(demobuf.p, DW_END); -} - -void G_ReadDemoTiccmd(ticcmd_t *cmd, INT32 playernum) -{ - UINT8 ziptic; - - if (!demobuf.p || !demo.deferstart) - return; - ziptic = READUINT8(demobuf.p); - - if (ziptic & ZT_FWD) - oldcmd[playernum].forwardmove = READSINT8(demobuf.p); - if (ziptic & ZT_SIDE) - oldcmd[playernum].sidemove = READSINT8(demobuf.p); - if (ziptic & ZT_ANGLE) - oldcmd[playernum].angleturn = READINT16(demobuf.p); - if (ziptic & ZT_BUTTONS) - oldcmd[playernum].buttons = READUINT16(demobuf.p); - if (ziptic & ZT_AIMING) - oldcmd[playernum].aiming = READINT16(demobuf.p); - if (ziptic & ZT_DRIFT) - oldcmd[playernum].driftturn = READINT16(demobuf.p); - if (ziptic & ZT_LATENCY) - oldcmd[playernum].latency = READUINT8(demobuf.p); - - G_CopyTiccmd(cmd, &oldcmd[playernum], 1); - - // SRB2kart: Copy-pasted from ticcmd building, removes that crappy demo cam - if (((players[displayplayers[0]].mo && players[displayplayers[0]].speed > 0) // Moving - || (leveltime > starttime && (cmd->buttons & BT_ACCELERATE && cmd->buttons & BT_BRAKE)) // Rubber-burn turn - || (players[displayplayers[0]].kartstuff[k_respawn]) // Respawning - || (players[displayplayers[0]].spectator || objectplacing)) // Not a physical player - && !(players[displayplayers[0]].kartstuff[k_spinouttimer] && players[displayplayers[0]].kartstuff[k_sneakertimer])) // Spinning and boosting cancels out spinout - localangle[0] += (cmd->angleturn<<16); - - if (!(demoflags & DF_GHOST) && *demobuf.p == DEMOMARKER) - { - // end of demo data stream - G_CheckDemoStatus(); - return; - } -} - -void G_WriteDemoTiccmd(ticcmd_t *cmd, INT32 playernum) -{ - char ziptic = 0; - UINT8 *ziptic_p; - - if (!demobuf.p) - return; - - ziptic_p = demobuf.p++; // the ziptic, written at the end of this function - - if (cmd->forwardmove != oldcmd[playernum].forwardmove) - { - WRITEUINT8(demobuf.p,cmd->forwardmove); - oldcmd[playernum].forwardmove = cmd->forwardmove; - ziptic |= ZT_FWD; - } - - if (cmd->sidemove != oldcmd[playernum].sidemove) - { - WRITEUINT8(demobuf.p,cmd->sidemove); - oldcmd[playernum].sidemove = cmd->sidemove; - ziptic |= ZT_SIDE; - } - - if (cmd->angleturn != oldcmd[playernum].angleturn) - { - WRITEINT16(demobuf.p,cmd->angleturn); - oldcmd[playernum].angleturn = cmd->angleturn; - ziptic |= ZT_ANGLE; - } - - if (cmd->buttons != oldcmd[playernum].buttons) - { - WRITEUINT16(demobuf.p,cmd->buttons); - oldcmd[playernum].buttons = cmd->buttons; - ziptic |= ZT_BUTTONS; - } - - if (cmd->aiming != oldcmd[playernum].aiming) - { - WRITEINT16(demobuf.p,cmd->aiming); - oldcmd[playernum].aiming = cmd->aiming; - ziptic |= ZT_AIMING; - } - - if (cmd->driftturn != oldcmd[playernum].driftturn) - { - WRITEINT16(demobuf.p,cmd->driftturn); - oldcmd[playernum].driftturn = cmd->driftturn; - ziptic |= ZT_DRIFT; - } - - if (cmd->latency != oldcmd[playernum].latency) - { - WRITEUINT8(demobuf.p,cmd->latency); - oldcmd[playernum].latency = cmd->latency; - ziptic |= ZT_LATENCY; - } - - *ziptic_p = ziptic; - - // attention here for the ticcmd size! - // latest demos with mouse aiming byte in ticcmd - if (!(demoflags & DF_GHOST) && ziptic_p > demoend - 9) - { - G_CheckDemoStatus(); // no more space - return; - } -} - -void G_GhostAddThok(INT32 playernum) -{ - if (!demo.recording || !(demoflags & DF_GHOST)) - return; - - ghostext[playernum].flags = (ghostext[playernum].flags & ~EZT_THOKMASK) | EZT_THOK; -} - -void G_GhostAddSpin(INT32 playernum) -{ - if (!demo.recording || !(demoflags & DF_GHOST)) - return; - - ghostext[playernum].flags = (ghostext[playernum].flags & ~EZT_THOKMASK) | EZT_SPIN; -} - -void G_GhostAddRev(INT32 playernum) -{ - if (!demo.recording || !(demoflags & DF_GHOST)) - return; - - ghostext[playernum].flags = (ghostext[playernum].flags & ~EZT_THOKMASK) | EZT_REV; -} - -void G_GhostAddFlip(INT32 playernum) -{ - if (!demo.recording || !(demoflags & DF_GHOST)) - return; - - ghostext[playernum].flags |= EZT_FLIP; -} - -void G_GhostAddColor(INT32 playernum, ghostcolor_t color) -{ - if (!demo.recording || !(demoflags & DF_GHOST)) - return; - - if (ghostext[playernum].lastcolor == (UINT8)color) - { - ghostext[playernum].flags &= ~EZT_COLOR; - return; - } - - ghostext[playernum].flags |= EZT_COLOR; - ghostext[playernum].color = (UINT8)color; -} - -void G_GhostAddScale(INT32 playernum, fixed_t scale) -{ - if (!demo.recording || !(demoflags & DF_GHOST)) - return; - - if (ghostext[playernum].lastscale == scale) - { - ghostext[playernum].flags &= ~EZT_SCALE; - return; - } - - ghostext[playernum].flags |= EZT_SCALE; - ghostext[playernum].scale = scale; -} - -void G_GhostAddHit(INT32 playernum, mobj_t *victim) -{ - if (!demo.recording || !(demoflags & DF_GHOST)) - return; - - ghostext[playernum].flags |= EZT_HIT; - ghostext[playernum].hits++; - ghostext[playernum].hitlist = Z_Realloc(ghostext[playernum].hitlist, ghostext[playernum].hits * sizeof(mobj_t *), PU_LEVEL, &ghostext[playernum].hitlist); - P_SetTarget(ghostext[playernum].hitlist + (ghostext[playernum].hits-1), victim); -} - -void G_WriteAllGhostTics(void) -{ - if (!demobuf.p) - return; - - UINT8 *save_demo_p = demobuf.p; -#define CHECKSPACE(num) if (demobuf.p+(num) > demoend) { demobuf.p = save_demo_p; G_CheckDemoStatus(); return; } - - boolean toobig = false; - INT32 i, counter = leveltime; - - for (i = 0; i < MAXPLAYERS; i++) - { - if (!playeringame[i] || players[i].spectator) - continue; - - if (P_MobjWasRemoved(players[i].mo)) - continue; - - counter++; - - if (multiplayer && ((counter % cv_netdemosyncquality.value) != 0)) // Only write 1 in this many ghost datas per tic to cut down on multiplayer replay size. - continue; - - if (((ghostext[i].flags && (ghostext[i].flags & EZT_HIT)) || ghostext[i].hits) - && !ghostext[i].hitlist) // hitlist might be freed during resynch, beware PU_LEVEL! - { - ghostext[i].hits = 0; - continue; - } - - CHECKSPACE(1); - - WRITEUINT8(demobuf.p, i); - G_WriteGhostTic(players[i].mo, i); - - // attention here for the ticcmd size! - // latest demos with mouse aiming byte in ticcmd - if (demobuf.p >= demoend - (13 + 9 + 9)) - { - toobig = true; - break; - } - } - - CHECKSPACE(1); - WRITEUINT8(demobuf.p, 0xFF); - - if (toobig) - { - G_CheckDemoStatus(); // no more space - return; - } - -#undef CHECKSPACE -} - -void G_WriteGhostTic(mobj_t *ghost, INT32 playernum) -{ - char ziptic = 0; - UINT8 *ziptic_p; - UINT32 i; - UINT8 sprite; - UINT8 frame; - - if (!demobuf.p) - return; - - UINT8 *save_demo_p = demobuf.p; -#define CHECKSPACE(num) if (demobuf.p+(num) > demoend) { demobuf.p = save_demo_p; G_CheckDemoStatus(); return; } - - if (!(demoflags & DF_GHOST)) - return; // No ghost data to write. - - if (ghost->player && ghost->player->pflags & PF_NIGHTSMODE && ghost->tracer) - { - // We're talking about the NiGHTS thing, not the normal platforming thing! - ziptic |= GZT_NIGHTS; - ghost = ghost->tracer; - } - - ziptic_p = demobuf.p++; // the ziptic, written at the end of this function - -#define MAXMOM (0x7FFF<<8) - - // GZT_XYZ is only useful if you've moved 256 FRACUNITS or more in a single tic. - if (abs(ghost->x-oldghost[playernum].x) > MAXMOM - || abs(ghost->y-oldghost[playernum].y) > MAXMOM - || abs(ghost->z-oldghost[playernum].z) > MAXMOM - || ((UINT8)(leveltime & 255) > 0 && (UINT8)(leveltime & 255) <= (UINT8)cv_netdemosyncquality.value)) // Hack to enable slightly nicer resyncing - { - oldghost[playernum].x = ghost->x; - oldghost[playernum].y = ghost->y; - oldghost[playernum].z = ghost->z; - ziptic |= GZT_XYZ; - - CHECKSPACE(sizeof(fixed_t)*3); - WRITEFIXED(demobuf.p, oldghost[playernum].x); - WRITEFIXED(demobuf.p, oldghost[playernum].y); - WRITEFIXED(demobuf.p, oldghost[playernum].z); - } - else - { - // For moving normally: - // Store one full byte of movement, plus one byte of fractional movement. - INT16 momx = (INT16)((ghost->x-oldghost[playernum].x + (1<<4))>>8); - INT16 momy = (INT16)((ghost->y-oldghost[playernum].y + (1<<4))>>8); - - if (momx != oldghost[playernum].momx - || momy != oldghost[playernum].momy) - { - oldghost[playernum].momx = momx; - oldghost[playernum].momy = momy; - ziptic |= GZT_MOMXY; - - CHECKSPACE(4); - - WRITEINT16(demobuf.p, momx); - WRITEINT16(demobuf.p, momy); - } - - momx = (INT16)((ghost->z-oldghost[playernum].z + (1<<4))>>8); - - if (momx != oldghost[playernum].momz) - { - oldghost[playernum].momz = momx; - ziptic |= GZT_MOMZ; - - CHECKSPACE(2); - - WRITEINT16(demobuf.p, momx); - } - - // This SHOULD set oldghost.x/y/z to match ghost->x/y/z - // but it keeps the fractional loss of one byte, - // so it will hopefully be made up for in future tics. - oldghost[playernum].x += oldghost[playernum].momx<<8; - oldghost[playernum].y += oldghost[playernum].momy<<8; - oldghost[playernum].z += oldghost[playernum].momz<<8; - } - -#undef MAXMOM - - // Only store the 8 most relevant bits of angle - // because exact values aren't too easy to discern to begin with when only 8 angles have different sprites - // and it does not affect this mode of movement at all anyway. - if (ghost->angle>>24 != oldghost[playernum].angle) - { - oldghost[playernum].angle = ghost->angle>>24; - ziptic |= GZT_ANGLE; - - CHECKSPACE(1); - - WRITEUINT8(demobuf.p, oldghost[playernum].angle); - } - - // Store the sprite frame. - frame = ghost->frame & 0xFF; - if (frame != oldghost[playernum].frame) - { - oldghost[playernum].frame = frame; - ziptic |= GZT_SPRITE; - - CHECKSPACE(1); - - WRITEUINT8(demobuf.p, oldghost[playernum].frame); - } - - // Check for sprite set changes - sprite = ghost->sprite; - if (sprite != oldghost[playernum].sprite) - { - oldghost[playernum].sprite = sprite; - ghostext[playernum].flags |= EZT_SPRITE; - } - - if (ghost->player) - { - if ( - ghostext[playernum].kartitem != ghost->player->kartstuff[k_itemtype] || - ghostext[playernum].kartamount != ghost->player->kartstuff[k_itemamount] || - ghostext[playernum].kartbumpers != ghost->player->kartstuff[k_bumper] - ) - { - ghostext[playernum].flags |= EZT_KART; - ghostext[playernum].kartitem = ghost->player->kartstuff[k_itemtype]; - ghostext[playernum].kartamount = ghost->player->kartstuff[k_itemamount]; - ghostext[playernum].kartbumpers = ghost->player->kartstuff[k_bumper]; - - } - } - - if (ghostext[playernum].color == ghostext[playernum].lastcolor) - ghostext[playernum].flags &= ~EZT_COLOR; - - if (ghostext[playernum].scale == ghostext[playernum].lastscale) - ghostext[playernum].flags &= ~EZT_SCALE; - - if (ghostext[playernum].flags) - { - ziptic |= GZT_EXTRA; - WRITEUINT8(demobuf.p, ghostext[playernum].flags); - - if (ghostext[playernum].flags & EZT_COLOR) - { - CHECKSPACE(1); - WRITEUINT8(demobuf.p, ghostext[playernum].color); - ghostext[playernum].lastcolor = ghostext[playernum].color; - } - - if (ghostext[playernum].flags & EZT_SCALE) - { - CHECKSPACE(sizeof(fixed_t)); - WRITEFIXED(demobuf.p, ghostext[playernum].scale); - ghostext[playernum].lastscale = ghostext[playernum].scale; - } - - if (ghostext[playernum].flags & EZT_HIT) - { - CHECKSPACE(2); - WRITEUINT16(demobuf.p, ghostext[playernum].hits); - - for (i = 0; i < ghostext[playernum].hits; i++) - { - mobj_t *mo = ghostext[playernum].hitlist[i]; - - CHECKSPACE(4+4+2+sizeof(fixed_t)*3+sizeof(angle_t)); - - WRITEUINT32(demobuf.p, UINT32_MAX); // reserved for some method of determining exactly which mobj this is. (mobjnum doesn't work here.) - WRITEUINT32(demobuf.p, mo->type); - WRITEUINT16(demobuf.p, (UINT16)mo->health); - WRITEFIXED(demobuf.p, mo->x); - WRITEFIXED(demobuf.p, mo->y); - WRITEFIXED(demobuf.p, mo->z); - WRITEANGLE(demobuf.p, mo->angle); - P_SetTarget(ghostext[playernum].hitlist+i, NULL); - } - - ghostext[playernum].hits = 0; - } - - if (ghostext[playernum].flags & EZT_SPRITE) - { - CHECKSPACE(1); - WRITEUINT8(demobuf.p, sprite); - } - - if (ghostext[playernum].flags & EZT_KART) - { - CHECKSPACE(12); - WRITEINT32(demobuf.p, ghostext[playernum].kartitem); - WRITEINT32(demobuf.p, ghostext[playernum].kartamount); - WRITEINT32(demobuf.p, ghostext[playernum].kartbumpers); - } - - ghostext[playernum].flags = 0; - } - - *ziptic_p = ziptic; -#undef CHECKSPACE -} - -void G_ConsAllGhostTics(void) -{ - UINT8 p; - - if (!demobuf.p || !demo.deferstart) - return; - - p = READUINT8(demobuf.p); - - while (p != 0xFF) - { - G_ConsGhostTic(p); - p = READUINT8(demobuf.p); - } - - if (*demobuf.p == DEMOMARKER) - { - // end of demo data stream - G_CheckDemoStatus(); - return; - } -} - -// Uses ghost data to do consistency checks on your position. -// This fixes desynchronising demos when fighting eggman. -void G_ConsGhostTic(INT32 playernum) -{ - UINT8 ziptic; - fixed_t px,py,pz,gx,gy,gz; - mobj_t *testmo; - fixed_t syncleeway; - boolean nightsfail = false; - - if (!(demoflags & DF_GHOST)) - return; // No ghost data to use. - - testmo = players[playernum].mo; - - // Grab ghost data. - ziptic = READUINT8(demobuf.p); - - if (ziptic & GZT_XYZ) - { - oldghost[playernum].x = READFIXED(demobuf.p); - oldghost[playernum].y = READFIXED(demobuf.p); - oldghost[playernum].z = READFIXED(demobuf.p); - syncleeway = 0; - } - else - { - if (ziptic & GZT_MOMXY) - { - oldghost[playernum].momx = READINT16(demobuf.p)<<8; - oldghost[playernum].momy = READINT16(demobuf.p)<<8; - } - - if (ziptic & GZT_MOMZ) - oldghost[playernum].momz = READINT16(demobuf.p)<<8; - - oldghost[playernum].x += oldghost[playernum].momx; - oldghost[playernum].y += oldghost[playernum].momy; - oldghost[playernum].z += oldghost[playernum].momz; - syncleeway = FRACUNIT; - } - - if (ziptic & GZT_ANGLE) - demobuf.p++; - - if (ziptic & GZT_SPRITE) - demobuf.p++; - - if (ziptic & GZT_NIGHTS) - { - if (!testmo || !testmo->player || !(testmo->player->pflags & PF_NIGHTSMODE) || !testmo->tracer) - nightsfail = true; - else - testmo = testmo->tracer; - } - - if (ziptic & GZT_EXTRA) - { // But wait, there's more! - ziptic = READUINT8(demobuf.p); - if (ziptic & EZT_COLOR) - demobuf.p++; - - if (ziptic & EZT_SCALE) - demobuf.p += sizeof(fixed_t); - - if (ziptic & EZT_HIT) - { // Resync mob damage. - UINT16 i, count = READUINT16(demobuf.p); - thinker_t *th; - mobj_t *mobj; - - UINT32 type; - UINT16 health; - fixed_t x; - fixed_t y; - fixed_t z; - - for (i = 0; i < count; i++) - { - demobuf.p += 4; // reserved. - type = READUINT32(demobuf.p); - health = READUINT16(demobuf.p); - x = READFIXED(demobuf.p); - y = READFIXED(demobuf.p); - z = READFIXED(demobuf.p); - demobuf.p += sizeof(angle_t); // angle, unnecessary for cons. - - mobj = NULL; - for (th = thinkercap.next; th != &thinkercap; th = th->next) - { - if (th->function != (actionf_p1)P_MobjThinker) - continue; - mobj = (mobj_t *)th; - if (mobj->type == (mobjtype_t)type && mobj->x == x && mobj->y == y && mobj->z == z) - break; - mobj = NULL; // wasn't this one, keep searching. - } - - if (mobj && mobj->health != health) // Wasn't damaged?! This is desync! Fix it! - { - if (demosynced) - { - CONS_Alert(CONS_WARNING, M_GetText("Demo playback has desynced (health)!\n")); - CONS_Printf("expected health %d got %d\n", health, mobj->health); - } - - demosynced = false; - P_DamageMobj(mobj, players[0].mo, players[0].mo, 1); - } - } - } - - if (ziptic & EZT_SPRITE) - demobuf.p++; - - if (ziptic & EZT_KART) - { - ghostext[playernum].kartitem = READINT32(demobuf.p); - ghostext[playernum].kartamount = READINT32(demobuf.p); - ghostext[playernum].kartbumpers = READINT32(demobuf.p); - } - } - - if (testmo) - { - // Re-synchronise - px = testmo->x; - py = testmo->y; - pz = testmo->z; - gx = oldghost[playernum].x; - gy = oldghost[playernum].y; - gz = oldghost[playernum].z; - - if (nightsfail || abs(px-gx) > syncleeway || abs(py-gy) > syncleeway || abs(pz-gz) > syncleeway) - { - ghostext[playernum].desyncframes++; - - if (ghostext[playernum].desyncframes >= 2) - { - if (demosynced) - CONS_Alert(CONS_WARNING, "Demo playback has desynced (player %s)!\n", player_names[playernum]); - demosynced = false; - - P_UnsetThingPosition(testmo); - testmo->x = oldghost[playernum].x; - testmo->y = oldghost[playernum].y; - P_SetThingPosition(testmo); - testmo->z = oldghost[playernum].z; - - if (abs(testmo->z - testmo->floorz) < 4*FRACUNIT) - testmo->z = testmo->floorz; // Sync players to the ground when they're likely supposed to be there... - - ghostext[playernum].desyncframes = 2; - } - } - else - ghostext[playernum].desyncframes = 0; - - if ( -#ifdef DEMO_COMPAT_100 - demo.version != 0x0001 && -#endif - ( - players[playernum].kartstuff[k_itemtype] != ghostext[playernum].kartitem || - players[playernum].kartstuff[k_itemamount] != ghostext[playernum].kartamount || - players[playernum].kartstuff[k_bumper] != ghostext[playernum].kartbumpers - ) - ) - { - if (demosynced) - { - CONS_Alert(CONS_WARNING, "Demo playback has desynced (item/bumpers)!(player %s)!\n", player_names[playernum]); - CONS_Printf("expected item type %d got %d\n", ghostext[playernum].kartitem, players[playernum].kartstuff[k_itemtype]); - CONS_Printf("expected item amount %d got %d\n", ghostext[playernum].kartamount, players[playernum].kartstuff[k_itemamount]); - } - demosynced = false; - - players[playernum].kartstuff[k_itemtype] = ghostext[playernum].kartitem; - players[playernum].kartstuff[k_itemamount] = ghostext[playernum].kartamount; - players[playernum].kartstuff[k_bumper] = ghostext[playernum].kartbumpers; - } - } - - if (*demobuf.p == DEMOMARKER) - { - // end of demo data stream - G_CheckDemoStatus(); - return; - } -} - -void G_GhostTicker(void) -{ - demoghost *g,*p; - - for (g = ghosts, p = NULL; g; g = g->next) - { - // Skip normal demo data. - UINT8 ziptic = READUINT8(g->p); - -#ifdef DEMO_COMPAT_100 - if (g->version != 0x0001) - { -#endif - while (ziptic != DW_END) // Get rid of extradata stuff - { - if (ziptic == 0) // Only support player 0 info for now - { - ziptic = READUINT8(g->p); - if (ziptic & DXD_SKIN) - g->p += 18; // We _could_ read this info, but it shouldn't change anything in record attack... - if (ziptic & DXD_COLOR) - g->p += 16; // Same tbh - if (ziptic & DXD_NAME) - g->p += 16; // yea - if (ziptic & DXD_PLAYSTATE && READUINT8(g->p) != DXD_PST_PLAYING) - I_Error("Ghost is not a record attack ghost"); //@TODO lmao don't blow up like this - } - else if (ziptic == DW_RNG) - g->p += 4; // RNG seed - else - I_Error("Ghost is not a record attack ghost"); //@TODO lmao don't blow up like this - - ziptic = READUINT8(g->p); - } - - ziptic = READUINT8(g->p); // Back to actual ziptic stuff -#ifdef DEMO_COMPAT_100 - } -#endif - - if (ziptic & ZT_FWD) - g->p++; - if (ziptic & ZT_SIDE) - g->p++; - if (ziptic & ZT_ANGLE) - g->p += 2; - if (ziptic & ZT_BUTTONS) - g->p += 2; - if (ziptic & ZT_AIMING) - g->p += 2; - if (ziptic & ZT_DRIFT) - g->p += 2; - if (ziptic & ZT_LATENCY) - g->p += 1; - - // Grab ghost data. - ziptic = READUINT8(g->p); - -#ifdef DEMO_COMPAT_100 - if (g->version != 0x0001) - { -#endif - if (ziptic == 0xFF) - goto skippedghosttic; // Didn't write ghost info this frame - else if (ziptic != 0) - I_Error("Ghost is not a record attack ghost"); //@TODO lmao don't blow up like this - ziptic = READUINT8(g->p); -#ifdef DEMO_COMPAT_100 - } -#endif - if (ziptic & GZT_XYZ) - { - g->oldmo.x = READFIXED(g->p); - g->oldmo.y = READFIXED(g->p); - g->oldmo.z = READFIXED(g->p); - } - else - { - if (ziptic & GZT_MOMXY) - { - g->oldmo.momx = READINT16(g->p)<<8; - g->oldmo.momy = READINT16(g->p)<<8; - } - if (ziptic & GZT_MOMZ) - g->oldmo.momz = READINT16(g->p)<<8; - g->oldmo.x += g->oldmo.momx; - g->oldmo.y += g->oldmo.momy; - g->oldmo.z += g->oldmo.momz; - } - if (ziptic & GZT_ANGLE) - g->oldmo.angle = READUINT8(g->p)<<24; - if (ziptic & GZT_SPRITE) - g->oldmo.frame = READUINT8(g->p); - - // Update ghost - P_UnsetThingPosition(g->mo); - g->mo->x = g->oldmo.x; - g->mo->y = g->oldmo.y; - g->mo->z = g->oldmo.z; - P_SetThingPosition(g->mo); - g->mo->angle = g->oldmo.angle; - g->mo->frame = g->oldmo.frame | tr_trans30<p); - if (ziptic & EZT_COLOR) - { - g->color = READUINT8(g->p); - switch(g->color) - { - default: - case GHC_NORMAL: // Go back to skin color - g->mo->color = g->oldmo.color; - break; - // Handled below - case GHC_SUPER: - case GHC_INVINCIBLE: - break; - case GHC_FIREFLOWER: // Fireflower - g->mo->color = SKINCOLOR_WHITE; - break; - } - } - if (ziptic & EZT_FLIP) - g->mo->eflags ^= MFE_VERTICALFLIP; - if (ziptic & EZT_SCALE) - { - g->mo->destscale = READFIXED(g->p); - if (g->mo->destscale != g->mo->scale) - P_SetScale(g->mo, g->mo->destscale); - } - if (ziptic & EZT_THOKMASK) - { // Let's only spawn ONE of these per frame, thanks. - mobj_t *mobj; - INT32 type = -1; - if (g->mo->skin) - { - switch (ziptic & EZT_THOKMASK) - { - case EZT_THOK: - type = (UINT32)mobjinfo[MT_PLAYER].painchance; - break; - case EZT_SPIN: - type = (UINT32)mobjinfo[MT_PLAYER].damage; - break; - case EZT_REV: - type = (UINT32)mobjinfo[MT_PLAYER].raisestate; - break; - } - } - if (type != -1) - { - if (type == MT_GHOST) - { - mobj = P_SpawnGhostMobj(g->mo); // does a large portion of the work for us - mobj->frame = (mobj->frame & ~FF_FRAMEMASK)|tr_trans60<mo->x, g->mo->y, g->mo->z - FixedDiv(FixedMul(g->mo->info->height, g->mo->scale) - g->mo->height,3*FRACUNIT), MT_THOK); - mobj->sprite = states[mobjinfo[type].spawnstate].sprite; - mobj->frame = (states[mobjinfo[type].spawnstate].frame & FF_FRAMEMASK) | tr_trans60<tics = -1; // nope. - mobj->color = g->mo->color; - if (g->mo->eflags & MFE_VERTICALFLIP) - { - mobj->flags2 |= MF2_OBJECTFLIP; - mobj->eflags |= MFE_VERTICALFLIP; - } - P_SetScale(mobj, g->mo->scale); - mobj->destscale = g->mo->scale; - } - mobj->floorz = mobj->z; - mobj->ceilingz = mobj->z+mobj->height; - P_UnsetThingPosition(mobj); - mobj->flags = MF_NOBLOCKMAP|MF_NOCLIP|MF_NOCLIPHEIGHT|MF_NOGRAVITY; // make an ATTEMPT to curb crazy SOCs fucking stuff up... - P_SetThingPosition(mobj); - mobj->fuse = 8; - P_SetTarget(&mobj->target, g->mo); - } - } - if (ziptic & EZT_HIT) - { // Spawn hit poofs for killing things! - UINT16 i, count = READUINT16(g->p), health; - //UINT32 type; - fixed_t x,y,z; - angle_t angle; - mobj_t *poof; - for (i = 0; i < count; i++) - { - g->p += 4; // reserved - g->p += 4; // backwards compat., type used to be here - health = READUINT16(g->p); - x = READFIXED(g->p); - y = READFIXED(g->p); - z = READFIXED(g->p); - angle = READANGLE(g->p); - if (health != 0 || i >= 4) // only spawn for the first 4 hits per frame, to prevent ghosts from splode-spamming too bad. - continue; - poof = P_SpawnMobj(x, y, z, MT_GHOST); - poof->angle = angle; - poof->flags = MF_NOBLOCKMAP|MF_NOCLIP|MF_NOCLIPHEIGHT|MF_NOGRAVITY; // make an ATTEMPT to curb crazy SOCs fucking stuff up... - poof->health = 0; - P_SetMobjStateNF(poof, S_XPLD1); - } - } - if (ziptic & EZT_SPRITE) - g->mo->sprite = READUINT8(g->p); - if (ziptic & EZT_KART) - g->p += 12; // kartitem, kartamount, kartbumpers - } - -#ifdef DEMO_COMPAT_100 - if (g->version != 0x0001) - { -#endif - if (READUINT8(g->p) != 0xFF) // Make sure there isn't other ghost data here. - I_Error("Ghost is not a record attack ghost"); //@TODO lmao don't blow up like this -#ifdef DEMO_COMPAT_100 - } -#endif - -skippedghosttic: - // Tick ghost colors (Super and Mario Invincibility flashing) - switch(g->color) - { - case GHC_SUPER: // Super Sonic (P_DoSuperStuff) - g->mo->color = SKINCOLOR_SUPER1; - g->mo->color += abs( ( (signed)( (unsigned)leveltime >> 1 ) % 9) - 4); - break; - case GHC_INVINCIBLE: // Mario invincibility (P_CheckInvincibilityTimer) - g->mo->color = (UINT8)(leveltime % MAXSKINCOLORS); - break; - default: - break; - } - - // Demo ends after ghost data. - if (*g->p == DEMOMARKER) - { - g->mo->momx = g->mo->momy = g->mo->momz = 0; - if (p) - p->next = g->next; - else - ghosts = g->next; - Z_Free(g); - continue; - } - p = g; - } -} - -// Demo rewinding functions -typedef struct rewindinfo_s { - tic_t leveltime; - - struct { - boolean ingame; - player_t player; - mobj_t mobj; - } playerinfo[MAXPLAYERS]; - - struct rewindinfo_s *prev; -} rewindinfo_t; - -static tic_t currentrewindnum; -static rewindinfo_t *rewindhead = NULL; // Reverse chronological order - -void G_InitDemoRewind(void) -{ - CL_ClearRewinds(); - - while (rewindhead) - { - rewindinfo_t *p = rewindhead->prev; - Z_Free(rewindhead); - rewindhead = p; - } - - currentrewindnum = 0; -} - -void G_StoreRewindInfo(void) -{ - static UINT8 timetolog = 8; - rewindinfo_t *info; - size_t i; - - if (timetolog-- > 0) - return; - timetolog = 8; - - info = Z_Calloc(sizeof(rewindinfo_t), PU_STATIC, NULL); - - for (i = 0; i < MAXPLAYERS; i++) - { - if (!playeringame[i] || players[i].spectator) - { - info->playerinfo[i].ingame = false; - continue; - } - - info->playerinfo[i].ingame = true; - memcpy(&info->playerinfo[i].player, &players[i], sizeof(player_t)); - if (players[i].mo) - memcpy(&info->playerinfo[i].mobj, players[i].mo, sizeof(mobj_t)); - } - - info->leveltime = leveltime; - info->prev = rewindhead; - rewindhead = info; -} - -void G_PreviewRewind(tic_t previewtime) -{ - SINT8 i; - size_t j; - fixed_t tweenvalue = 0; - rewindinfo_t *info = rewindhead, *next_info = rewindhead; - - if (!info) - return; - - while (info->leveltime > previewtime && info->prev) - { - next_info = info; - info = info->prev; - } - if (info != next_info) - tweenvalue = FixedDiv(previewtime - info->leveltime, next_info->leveltime - info->leveltime); - - - for (i = 0; i < MAXPLAYERS; i++) - { - if (!playeringame[i] || players[i].spectator) - { - if (info->playerinfo[i].player.mo) - { - //@TODO spawn temp object to act as a player display - } - - continue; - } - - if (!info->playerinfo[i].ingame || !info->playerinfo[i].player.mo) - { - if (players[i].mo) - players[i].mo->flags2 |= MF2_DONTDRAW; - - continue; - } - - if (!players[i].mo) - continue; //@TODO spawn temp object to act as a player display - - players[i].mo->flags2 &= ~MF2_DONTDRAW; - - P_UnsetThingPosition(players[i].mo); -#define TWEEN(pr) info->playerinfo[i].mobj.pr + FixedMul((INT32) (next_info->playerinfo[i].mobj.pr - info->playerinfo[i].mobj.pr), tweenvalue) - players[i].mo->x = TWEEN(x); - players[i].mo->y = TWEEN(y); - players[i].mo->z = TWEEN(z); - players[i].mo->angle = TWEEN(angle); -#undef TWEEN - P_SetThingPosition(players[i].mo); - - players[i].frameangle = info->playerinfo[i].player.frameangle + FixedMul((INT32) (next_info->playerinfo[i].player.frameangle - info->playerinfo[i].player.frameangle), tweenvalue); - - players[i].mo->sprite = info->playerinfo[i].mobj.sprite; - players[i].mo->frame = info->playerinfo[i].mobj.frame; - - players[i].realtime = info->playerinfo[i].player.realtime; - for (j = 0; j < NUMKARTSTUFF; j++) - players[i].kartstuff[j] = info->playerinfo[i].player.kartstuff[j]; - } - - for (i = splitscreen; i >= 0; i--) - P_ResetCamera(&players[displayplayers[i]], &camera[i]); -} - -void G_ConfirmRewind(tic_t rewindtime) -{ - SINT8 i; - tic_t j; - boolean oldmenuactive = menuactive, oldsounddisabled = sound_disabled; - - INT32 olddp1 = displayplayers[0], olddp2 = displayplayers[1], olddp3 = displayplayers[2], olddp4 = displayplayers[3]; - UINT8 oldss = splitscreen; - - menuactive = false; // Prevent loops - - CV_StealthSetValue(&cv_renderview, 0); - - if (rewindtime <= starttime) - { - demo.rewinding = false; - G_DoPlayDemo(NULL); // Restart the current demo - } - else - { - rewind_t *rewind; - sound_disabled = true; // Prevent sound spam - demo.rewinding = true; - - rewind = CL_RewindToTime(rewindtime); - - if (rewind) - { - demobuf.p = demobuf.buffer + rewind->demopos; - memcpy(oldcmd, rewind->oldcmd, sizeof (oldcmd)); - memcpy(oldghost, rewind->oldghost, sizeof (oldghost)); - paused = false; - } - else - { - demo.rewinding = true; - G_DoPlayDemo(NULL); // Restart the current demo - } - } - - for (j = 0; j < rewindtime && leveltime < rewindtime; j++) - { - G_Ticker((j % NEWTICRATERATIO) == 0); - } - - demo.rewinding = false; - menuactive = oldmenuactive; // Bring the menu back up - sound_disabled = oldsounddisabled; // Re-enable SFX - - wipegamestate = gamestate; // No fading back in! - - COM_BufInsertText("renderview on\n"); - - splitscreen = oldss; - displayplayers[0] = olddp1; - displayplayers[1] = olddp2; - displayplayers[2] = olddp3; - displayplayers[3] = olddp4; - R_ExecuteSetViewSize(); - G_ResetViews(); - - for (i = splitscreen; i >= 0; i--) - P_ResetCamera(&players[displayplayers[i]], &camera[i]); -} - -void G_ReadMetalTic(mobj_t *metal) -{ - UINT8 ziptic; - UINT16 speed; - UINT8 statetype; - - if (!metal_p) - return; - ziptic = READUINT8(metal_p); - - // Read changes from the tic - if (ziptic & GZT_XYZ) - { - oldmetal.x = READFIXED(metal_p); - oldmetal.y = READFIXED(metal_p); - oldmetal.z = READFIXED(metal_p); - P_MoveOrigin(metal, oldmetal.x, oldmetal.y, oldmetal.z); - oldmetal.x = metal->x; - oldmetal.y = metal->y; - oldmetal.z = metal->z; - } - else - { - if (ziptic & GZT_MOMXY) - { - oldmetal.momx = READINT16(metal_p)<<8; - oldmetal.momy = READINT16(metal_p)<<8; - } - if (ziptic & GZT_MOMZ) - oldmetal.momz = READINT16(metal_p)<<8; - oldmetal.x += oldmetal.momx; - oldmetal.y += oldmetal.momy; - oldmetal.z += oldmetal.momz; - } - if (ziptic & GZT_ANGLE) - oldmetal.angle = READUINT8(metal_p)<<24; - if (ziptic & GZT_SPRITE) - metal_p++; // Currently unused. (Metal Sonic figures out what he's doing his own damn self.) - - // Set movement, position, and angle - // oldmetal contains where you're supposed to be. - metal->momx = oldmetal.momx; - metal->momy = oldmetal.momy; - metal->momz = oldmetal.momz; - P_UnsetThingPosition(metal); - metal->x = oldmetal.x; - metal->y = oldmetal.y; - metal->z = oldmetal.z; - P_SetThingPosition(metal); - metal->angle = oldmetal.angle; - - if (ziptic & GZT_EXTRA) - { // But wait, there's more! - ziptic = READUINT8(metal_p); - if (ziptic & EZT_FLIP) - metal->eflags ^= MFE_VERTICALFLIP; - if (ziptic & EZT_SCALE) - { - metal->destscale = READFIXED(metal_p); - if (metal->destscale != metal->scale) - P_SetScale(metal, metal->destscale); - } - } - - // Calculates player's speed based on distance-of-a-line formula - speed = FixedDiv(P_AproxDistance(oldmetal.momx, oldmetal.momy), metal->scale)>>FRACBITS; - - // Use speed to decide an appropriate state - if (speed > 20) // default skin runspeed - statetype = 2; - else if (speed > 1) // stopspeed - statetype = 1; - else - statetype = 0; - - // Set state - if (statetype != metal->threshold) - { - switch (statetype) - { - case 2: // run - P_SetMobjState(metal,metal->info->meleestate); - break; - case 1: // walk - P_SetMobjState(metal,metal->info->seestate); - break; - default: // stand - P_SetMobjState(metal,metal->info->spawnstate); - break; - } - metal->threshold = statetype; - } - - // TODO: Modify state durations based on movement speed, similar to players? - - if (*metal_p == DEMOMARKER) - { - // end of demo data stream - G_StopMetalDemo(); - return; - } -} - -void G_WriteMetalTic(mobj_t *metal) -{ - UINT8 ziptic = 0; - UINT8 *ziptic_p; - - if (!demobuf.p) // demobuf.p will be NULL until the race start linedef executor is triggered! - return; - - ziptic_p = demobuf.p++; // the ziptic, written at the end of this function - - #define MAXMOM (0xFFFF<<8) - - // GZT_XYZ is only useful if you've moved 256 FRACUNITS or more in a single tic. - if (abs(metal->x-oldmetal.x) > MAXMOM - || abs(metal->y-oldmetal.y) > MAXMOM - || abs(metal->z-oldmetal.z) > MAXMOM) - { - oldmetal.x = metal->x; - oldmetal.y = metal->y; - oldmetal.z = metal->z; - WRITEFIXED(demobuf.p,oldmetal.x); - WRITEFIXED(demobuf.p,oldmetal.y); - WRITEFIXED(demobuf.p,oldmetal.z); - ziptic |= GZT_XYZ; - } - else - { - // For moving normally: - // Store one full byte of movement, plus one byte of fractional movement. - INT16 momx = (INT16)((metal->x-oldmetal.x)>>8); - INT16 momy = (INT16)((metal->y-oldmetal.y)>>8); - if (momx != oldmetal.momx - || momy != oldmetal.momy) - { - oldmetal.momx = momx; - oldmetal.momy = momy; - WRITEINT16(demobuf.p,momx); - WRITEINT16(demobuf.p,momy); - ziptic |= GZT_MOMXY; - } - momx = (INT16)((metal->z-oldmetal.z)>>8); - if (momx != oldmetal.momz) - { - oldmetal.momz = momx; - WRITEINT16(demobuf.p,momx); - ziptic |= GZT_MOMZ; - } - - // This SHOULD set oldmetal.x/y/z to match metal->x/y/z - // but it keeps the fractional loss of one byte, - // so it will hopefully be made up for in future tics. - oldmetal.x += oldmetal.momx<<8; - oldmetal.y += oldmetal.momy<<8; - oldmetal.z += oldmetal.momz<<8; - } - - #undef MAXMOM - - // Only store the 8 most relevant bits of angle - // because exact values aren't too easy to discern to begin with when only 8 angles have different sprites - // and it does not affect movement at all anyway. - if (metal->angle>>24 != oldmetal.angle) - { - oldmetal.angle = metal->angle>>24; - WRITEUINT8(demobuf.p,oldmetal.angle); - ziptic |= GZT_ANGLE; - } - - // Metal Sonic does not need our state changes. - // ... currently. - - { - UINT8 *exttic_p = NULL; - UINT8 exttic = 0; - if ((metal->eflags & MFE_VERTICALFLIP) != (oldmetal.eflags & MFE_VERTICALFLIP)) - { - if (!exttic_p) - exttic_p = demobuf.p++; - exttic |= EZT_FLIP; - oldmetal.eflags ^= MFE_VERTICALFLIP; - } - if (metal->scale != oldmetal.scale) - { - if (!exttic_p) - exttic_p = demobuf.p++; - exttic |= EZT_SCALE; - WRITEFIXED(demobuf.p,metal->scale); - oldmetal.scale = metal->scale; - } - if (exttic_p) - { - *exttic_p = exttic; - ziptic |= GZT_EXTRA; - } - } - - *ziptic_p = ziptic; - - // attention here for the ticcmd size! - // latest demos with mouse aiming byte in ticcmd - if (demobuf.p >= demoend - 32) - { - G_StopMetalRecording(); // no more space - return; - } -} - -// -// G_RecordDemo -// -void G_RecordDemo(const char *name) -{ - INT32 maxsize; - - demobuf.p = NULL; - G_ResetDemoRecording(); - demoend = NULL; - - if (cv_recordmultiplayerdemos.value) - { - CONS_Printf("Recording demo %s.lmp\n", name); - - strcpy(demoname, name); - strcat(demoname, ".lmp"); - - maxsize = cv_maxdemosize.value*1024*1024; - - demobuf.buffer = Z_Malloc(maxsize + 100*1024, PU_STATIC, NULL); - demoend = demobuf.buffer + maxsize; - - if (demobuf.buffer) - demo.recording = true; - else - CONS_Alert(CONS_ERROR, "Failed to allocate demo buffer\n"); - } -} - -void G_RecordMetal(void) -{ - INT32 maxsize; - maxsize = cv_maxdemosize.value*1024*1024; - if (demobuf.buffer) - Z_Free(demobuf.buffer); - demobuf.p = NULL; - metalrecording = false; - demobuf.buffer = Z_Malloc(maxsize + 100*1024, PU_STATIC, NULL); - demoend = demobuf.buffer + maxsize; - - if (demobuf.buffer) - metalrecording = true; - else - CONS_Alert(CONS_ERROR, "Failed to allocate demo buffer\n"); -} - -void G_BeginRecording(void) -{ - UINT8 i, p; - char name[17]; - player_t *player = &players[consoleplayer]; - - char *filename; - UINT8 totalfiles; - UINT8 *m; - - if (!cv_recordmultiplayerdemos.value) - { - G_ResetDemoRecording(); - return; - } - - if (demobuf.buffer == NULL) - { - CONS_Alert(CONS_ERROR, "No demo buffer allocated\n"); - G_ResetDemoRecording(); - return; - } - - if (demobuf.p) - { - G_ResetDemoRecording(); - return; - } - - memset(name,0,sizeof(name)); - - demobuf.p = demobuf.buffer; - demoflags = DF_GHOST|(multiplayer ? DF_MULTIPLAYER : (modeattacking<important) - { - nameonly(( filename = va("%s", wadfiles[i]->filename) )); - WRITESTRINGL(demobuf.p, filename, MAX_WADPATH); - WRITEMEM(demobuf.p, wadfiles[i]->md5sum, 16); - - totalfiles++; - } - - WRITEUINT8(m, totalfiles); - - switch ((demoflags & DF_ATTACKMASK)>>DF_ATTACKSHIFT) - { - case ATTACKING_NONE: // 0 - break; - case ATTACKING_RECORD: // 1 - demotime_p = demobuf.p; - WRITEUINT32(demobuf.p,UINT32_MAX); // time - WRITEUINT32(demobuf.p,UINT32_MAX); // lap - break; - default: // 3 - break; - } - - WRITEUINT32(demobuf.p, P_GetInitSeed()); - - // Reserved for extrainfo location from start of file - demoinfo_p = demobuf.p; - WRITEUINT32(demobuf.p, 0); - - // Save netvars - CV_SaveNetVars(&demobuf.p, true); - - // Now store some info for each in-game player - for (p = 0; p < MAXPLAYERS; p++) - { - if (!playeringame[p]) - continue; - - player = &players[p]; - - WRITEUINT8(demobuf.p, p | (player->spectator ? DEMO_SPECTATOR : 0)); - - // Name - memset(name, 0, 16); - memcpy(name, player_names[p], 15); - M_Memcpy(demobuf.p,name,16); - demobuf.p += 16; - - // Skin - memset(name, 0, 16); - strncpy(name, skins[player->skin].name, 16); - M_Memcpy(demobuf.p,name,16); - demobuf.p += 16; - - // Color - memset(name, 0, 16); - strncpy(name, KartColor_Names[player->skincolor], 16); - M_Memcpy(demobuf.p,name,16); - demobuf.p += 16; - - // Score, since Kart uses this to determine where you start on the map - WRITEUINT32(demobuf.p, player->score); - - // Kart speed and weight - WRITEUINT8(demobuf.p, skins[player->skin].kartspeed); - WRITEUINT8(demobuf.p, skins[player->skin].kartweight); - } - - WRITEUINT8(demobuf.p, 0xFF); // Denote the end of the player listing - - // player lua vars, always saved even if empty... Unless it's record attack. - if (demoflags & DF_LUAVARS) - LUA_Archive(&demobuf, false); - - memset(&oldcmd,0,sizeof(oldcmd)); - memset(&oldghost,0,sizeof(oldghost)); - memset(&ghostext,0,sizeof(ghostext)); - - for (i = 0; i < MAXPLAYERS; i++) - { - ghostext[i].lastcolor = ghostext[i].color = GHC_NORMAL; - ghostext[i].lastscale = ghostext[i].scale = FRACUNIT; - - if (players[i].mo) - { - oldghost[i].x = players[i].mo->x; - oldghost[i].y = players[i].mo->y; - oldghost[i].z = players[i].mo->z; - oldghost[i].angle = players[i].mo->angle; - - // preticker started us gravity flipped - if (players[i].mo->eflags & MFE_VERTICALFLIP) - ghostext[i].flags |= EZT_FLIP; - } - } -} - -void G_BeginMetal(void) -{ - mobj_t *mo = players[consoleplayer].mo; - - if (demobuf.buffer == NULL) - { - CONS_Alert(CONS_ERROR, "No metal demo buffer allocated\n"); - metalrecording = false; - return; - } - - if (demobuf.p) - { - Z_Free(demobuf.buffer); - demobuf.buffer = NULL; - metalrecording = false; - return; - } - - demobuf.p = demobuf.buffer; - - // Write header. - M_Memcpy(demobuf.p, DEMOHEADER, 12); demobuf.p += 12; - WRITEUINT8(demobuf.p,VERSION); - WRITEUINT8(demobuf.p,SUBVERSION); - WRITEUINT16(demobuf.p,DEMOVERSION); - - // demo checksum - demobuf.p += 16; - - M_Memcpy(demobuf.p, "METL", 4); demobuf.p += 4; - - // Set up our memory. - memset(&oldmetal,0,sizeof(oldmetal)); - oldmetal.x = mo->x; - oldmetal.y = mo->y; - oldmetal.z = mo->z; - oldmetal.angle = mo->angle; -} - -void G_WriteStanding(UINT8 ranking, char *name, INT32 skinnum, UINT8 color, UINT32 val) -{ - char temp[17]; - - if (!demobuf.p) - return; - - if (demoinfo_p && *(UINT32 *)demoinfo_p == 0 && demobuf.buffer != NULL) - { - WRITEUINT8(demobuf.p, DEMOMARKER); // add the demo end marker - *(UINT32 *)demoinfo_p = demobuf.p - demobuf.buffer; - } - - WRITEUINT8(demobuf.p, DW_STANDING); - WRITEUINT8(demobuf.p, ranking); - - // Name - memset(temp, 0, 16); - strncpy(temp, name, 16); - M_Memcpy(demobuf.p,temp,16); - demobuf.p += 16; - - // Skin - memset(temp, 0, 16); - strncpy(temp, skins[skinnum].name, 16); - M_Memcpy(demobuf.p,temp,16); - demobuf.p += 16; - - // Color - memset(temp, 0, 16); - strncpy(temp, KartColor_Names[color], 16); - M_Memcpy(demobuf.p,temp,16); - demobuf.p += 16; - - // Score/time/whatever - WRITEUINT32(demobuf.p, val); -} - -void G_SetDemoTime(UINT32 ptime, UINT32 plap) -{ - if (!demo.recording || !demotime_p) - return; - if (demoflags & DF_RECORDATTACK) - { - WRITEUINT32(demotime_p, ptime); - WRITEUINT32(demotime_p, plap); - demotime_p = NULL; - } -} - -static void G_LoadDemoExtraFiles(UINT8 **pp) -{ - UINT8 totalfiles; - char filename[MAX_WADPATH]; - UINT8 md5sum[16]; - filestatus_t ncs = FS_NOTFOUND; - boolean toomany = false; - boolean alreadyloaded; - UINT8 i, j; - - totalfiles = READUINT8((*pp)); - for (i = 0; i < totalfiles; ++i) - { - if (toomany) - SKIPSTRING((*pp)); - else - { - strlcpy(filename, (char *)(*pp), sizeof filename); - SKIPSTRING((*pp)); - } - READMEM((*pp), md5sum, 16); - - if (!toomany) - { - alreadyloaded = false; - - for (j = 0; j < numwadfiles; ++j) - { - if (memcmp(md5sum, wadfiles[j]->md5sum, 16) == 0) - { - alreadyloaded = true; - break; - } - } - - if (alreadyloaded) - continue; - - if (numwadfiles >= MAX_WADFILES) - toomany = true; - else - ncs = findfile(filename, md5sum, false); - - if (toomany) - { - CONS_Alert(CONS_WARNING, M_GetText("Too many files loaded to add anymore for demo playback\n")); - if (!CON_Ready()) - M_StartMessage(M_GetText("There are too many files loaded to add this demo's addons.\n\nDemo playback may desync.\n\nPress ESC\n"), NULL, MM_NOTHING); - } - else if (ncs != FS_FOUND) - { - if (ncs == FS_NOTFOUND) - CONS_Alert(CONS_NOTICE, M_GetText("You do not have a copy of %s\n"), filename); - else if (ncs == FS_MD5SUMBAD) - CONS_Alert(CONS_NOTICE, M_GetText("Checksum mismatch on %s\n"), filename); - else - CONS_Alert(CONS_NOTICE, M_GetText("Unknown error finding file %s\n"), filename); - - if (!CON_Ready()) - M_StartMessage(M_GetText("There were errors trying to add this demo's addons. Check the console for more information.\n\nDemo playback may desync.\n\nPress ESC\n"), NULL, MM_NOTHING); - } - else - { - P_PartialAddWadFile(filename, false); - } - } - } - - if (P_PartialAddGetStage() >= 0) - P_MultiSetupWadFiles(true); // in case any partial adds were done -} - -static void G_SkipDemoExtraFiles(UINT8 **pp) -{ - UINT8 totalfiles; - UINT8 i; - - totalfiles = READUINT8((*pp)); - for (i = 0; i < totalfiles; ++i) - { - SKIPSTRING((*pp));// file name - (*pp) += 16;// md5 - } -} - -// G_CheckDemoExtraFiles: checks if our loaded WAD list matches the demo's. -// Enabling quick prevents filesystem checks to see if needed files are available to load. -static UINT8 G_CheckDemoExtraFiles(UINT8 **pp, boolean quick) -{ - UINT8 totalfiles, filesloaded, nmusfilecount; - char filename[MAX_WADPATH]; - UINT8 md5sum[16]; - boolean toomany = false; - boolean alreadyloaded; - UINT8 i, j; - UINT8 error = 0; - - totalfiles = READUINT8((*pp)); - filesloaded = 0; - for (i = 0; i < totalfiles; ++i) - { - if (toomany) - SKIPSTRING((*pp)); - else - { - strlcpy(filename, (char *)(*pp), sizeof filename); - SKIPSTRING((*pp)); - } - READMEM((*pp), md5sum, 16); - - if (!toomany) - { - alreadyloaded = false; - nmusfilecount = 0; - - for (j = 0; j < numwadfiles; ++j) - { - if (wadfiles[j]->important && j > mainwads) - nmusfilecount++; - else - continue; - - if (memcmp(md5sum, wadfiles[j]->md5sum, 16) == 0) - { - alreadyloaded = true; - - if (i != nmusfilecount-1 && error < DFILE_ERROR_OUTOFORDER) - error |= DFILE_ERROR_OUTOFORDER; - - break; - } - } - - if (alreadyloaded) - { - filesloaded++; - continue; - } - - if (numwadfiles >= MAX_WADFILES) - error = DFILE_ERROR_CANNOTLOAD; - else if (!quick && findfile(filename, md5sum, false) != FS_FOUND) - error = DFILE_ERROR_CANNOTLOAD; - else if (error < DFILE_ERROR_INCOMPLETEOUTOFORDER) - error |= DFILE_ERROR_NOTLOADED; - } else - error = DFILE_ERROR_CANNOTLOAD; - } - - // Get final file count - nmusfilecount = 0; - - for (j = 0; j < numwadfiles; ++j) - if (wadfiles[j]->important && j > mainwads) - nmusfilecount++; - - if (!error && filesloaded < nmusfilecount) - error = DFILE_ERROR_EXTRAFILES; - - return error; -} - -// Returns bitfield: -// 1 == new demo has lower time -// 2 == new demo has higher score -// 4 == new demo has higher rings -UINT8 G_CmpDemoTime(char *oldname, char *newname) -{ - UINT8 *buffer,*p; - UINT8 flags; - UINT32 oldtime, newtime, oldlap, newlap; - UINT16 oldversion; - size_t bufsize ATTRUNUSED; - UINT8 c; - UINT16 s ATTRUNUSED; - UINT8 aflags = 0; - - // load the new file - FIL_DefaultExtension(newname, ".lmp"); - bufsize = FIL_ReadFile(newname, &buffer); - I_Assert(bufsize != 0); - p = buffer; - - // read demo header - I_Assert(!memcmp(p, DEMOHEADER, 12)); - p += 12; // DEMOHEADER - c = READUINT8(p); // VERSION - I_Assert(c == VERSION); - c = READUINT8(p); // SUBVERSION - I_Assert(c == SUBVERSION); - s = READUINT16(p); - I_Assert(s == DEMOVERSION); - p += 64; // full demo title - p += 16; // demo checksum - I_Assert(!memcmp(p, "PLAY", 4)); - p += 4; // PLAY - p += 2; // gamemap - p += 16; // map md5 - flags = READUINT8(p); // demoflags - p++; // gametype - G_SkipDemoExtraFiles(&p); - - aflags = flags & (DF_RECORDATTACK|DF_NIGHTSATTACK); - I_Assert(aflags); - if (flags & DF_RECORDATTACK) - { - newtime = READUINT32(p); - newlap = READUINT32(p); - } - else // appease compiler - return 0; - - Z_Free(buffer); - - // load old file - FIL_DefaultExtension(oldname, ".lmp"); - if (!FIL_ReadFile(oldname, &buffer)) - { - CONS_Alert(CONS_ERROR, M_GetText("Failed to read file '%s'.\n"), oldname); - return UINT8_MAX; - } - p = buffer; - - // read demo header - if (memcmp(p, DEMOHEADER, 12)) - { - CONS_Alert(CONS_NOTICE, M_GetText("File '%s' invalid format. It will be overwritten.\n"), oldname); - Z_Free(buffer); - return UINT8_MAX; - } p += 12; // DEMOHEADER - p++; // VERSION - p++; // SUBVERSION - oldversion = READUINT16(p); - switch(oldversion) // demoversion - { - case DEMOVERSION: // latest always supported - p += 64; // full demo title - break; -#ifdef DEMO_COMPAT_100 - case 0x0001: - // Old replays gotta go :] - CONS_Alert(CONS_NOTICE, M_GetText("File '%s' outdated version. It will be overwritten. Nyeheheh.\n"), oldname); - Z_Free(buffer); - return UINT8_MAX; -#endif - // too old, cannot support. - default: - CONS_Alert(CONS_NOTICE, M_GetText("File '%s' invalid format. It will be overwritten.\n"), oldname); - Z_Free(buffer); - return UINT8_MAX; - } - p += 16; // demo checksum - if (memcmp(p, "PLAY", 4)) - { - CONS_Alert(CONS_NOTICE, M_GetText("File '%s' invalid format. It will be overwritten.\n"), oldname); - Z_Free(buffer); - return UINT8_MAX; - } p += 4; // "PLAY" - p += 2; // gamemap - p += 16; // mapmd5 - flags = READUINT8(p); - p++; // gametype - G_SkipDemoExtraFiles(&p); - if (!(flags & aflags)) - { - CONS_Alert(CONS_NOTICE, M_GetText("File '%s' not from same game mode. It will be overwritten.\n"), oldname); - Z_Free(buffer); - return UINT8_MAX; - } - if (flags & DF_RECORDATTACK) - { - oldtime = READUINT32(p); - oldlap = READUINT32(p); - } - else // appease compiler - return UINT8_MAX; - - Z_Free(buffer); - - c = 0; - if (newtime < oldtime - || (newtime == oldtime && (newlap < oldlap))) - c |= 1; // Better time - if (newlap < oldlap - || (newlap == oldlap && newtime < oldtime)) - c |= 1<<1; // Better lap time - return c; -} - -void G_LoadDemoInfo(menudemo_t *pdemo) -{ - UINT8 *infobuffer, *info_p, *extrainfo_p; - UINT8 version, subversion, pdemoflags; - UINT16 pdemoversion, count; - - if (!FIL_ReadFile(pdemo->filepath, &infobuffer)) - { - CONS_Alert(CONS_ERROR, M_GetText("Failed to read file '%s'.\n"), pdemo->filepath); - pdemo->type = MD_INVALID; - sprintf(pdemo->title, "INVALID REPLAY"); - - return; - } - - info_p = infobuffer; - - if (memcmp(info_p, DEMOHEADER, 12)) - { - CONS_Alert(CONS_ERROR, M_GetText("%s is not a SRB2Kart replay file.\n"), pdemo->filepath); - pdemo->type = MD_INVALID; - sprintf(pdemo->title, "INVALID REPLAY"); - Z_Free(infobuffer); - return; - } - - pdemo->type = MD_LOADED; - - info_p += 12; // DEMOHEADER - - version = READUINT8(info_p); - subversion = READUINT8(info_p); - pdemoversion = READUINT16(info_p); - - memset(pdemo->version, 0, sizeof(pdemo->version)); - snprintf(pdemo->version, sizeof(pdemo->version), "v%d.%d", version, subversion); - - switch(pdemoversion) - { - case DEMOVERSION: // latest always supported - // demo title - M_Memcpy(pdemo->title, info_p, 64); - info_p += 64; - - break; -#ifdef DEMO_COMPAT_100 - case 0x0001: - pdemo->type = MD_OUTDATED; - sprintf(pdemo->title, "Legacy Replay"); - break; -#endif - // too old, cannot support. - default: - CONS_Alert(CONS_ERROR, M_GetText("%s is an incompatible replay format and cannot be played.\n"), pdemo->filepath); - pdemo->type = MD_INVALID; - sprintf(pdemo->title, "INVALID REPLAY"); - Z_Free(infobuffer); - return; - } - - if (version != VERSION || subversion != SUBVERSION) - pdemo->type = MD_OUTDATED; - - info_p += 16; // demo checksum - if (memcmp(info_p, "PLAY", 4)) - { - CONS_Alert(CONS_ERROR, M_GetText("%s is the wrong type of recording and cannot be played.\n"), pdemo->filepath); - pdemo->type = MD_INVALID; - sprintf(pdemo->title, "INVALID REPLAY"); - Z_Free(infobuffer); - return; - } - info_p += 4; // "PLAY" - pdemo->map = READINT16(info_p); - info_p += 16; // mapmd5 - - pdemoflags = READUINT8(info_p); - - // temp? - if (!(pdemoflags & DF_MULTIPLAYER)) - { - CONS_Alert(CONS_ERROR, M_GetText("%s is not a multiplayer replay and can't be listed on this menu fully yet.\n"), pdemo->filepath); - Z_Free(infobuffer); - return; - } -#ifdef DEMO_COMPAT_100 - else if (pdemoversion == 0x0001) - { - CONS_Alert(CONS_ERROR, M_GetText("%s is a legacy multiplayer replay and cannot be played.\n"), pdemo->filepath); - pdemo->type = MD_INVALID; - sprintf(pdemo->title, "INVALID REPLAY"); - Z_Free(infobuffer); - return; - } -#endif - - pdemo->gametype = READUINT8(info_p); - - pdemo->addonstatus = G_CheckDemoExtraFiles(&info_p, true); - info_p += 4; // RNG seed - - extrainfo_p = infobuffer + READUINT32(info_p); - - // Pared down version of CV_LoadNetVars to find the kart speed - pdemo->kartspeed = 1; // Default to normal speed - count = READUINT16(info_p); - while (count--) - { - UINT16 netid; - char *svalue; - - netid = READUINT16(info_p); - svalue = (char *)info_p; - SKIPSTRING(info_p); - info_p++; // stealth - - if (netid == cv_kartspeed.netid) - { - UINT8 j; - for (j = 0; kartspeed_cons_t[j].strvalue; j++) - if (!stricmp(kartspeed_cons_t[j].strvalue, svalue)) - pdemo->kartspeed = kartspeed_cons_t[j].value; - } - else if (netid == cv_basenumlaps.netid && pdemo->gametype == GT_RACE) - pdemo->numlaps = atoi(svalue); - } - - if (pdemoflags & DF_ENCORE) - pdemo->kartspeed |= DF_ENCORE; - - // Read standings! - count = 0; - - while (READUINT8(extrainfo_p) == DW_STANDING) // Assume standings are always first in the extrainfo - { - INT32 i; - char temp[16]; - - pdemo->standings[count].ranking = READUINT8(extrainfo_p); - - // Name - M_Memcpy(pdemo->standings[count].name, extrainfo_p, 16); - extrainfo_p += 16; - - // Skin - M_Memcpy(temp,extrainfo_p,16); - extrainfo_p += 16; - pdemo->standings[count].skin = UINT8_MAX; - for (i = 0; i < numskins; i++) - if (stricmp(skins[i].name, temp) == 0) - { - pdemo->standings[count].skin = i; - break; - } - - // Color - M_Memcpy(temp,extrainfo_p,16); - extrainfo_p += 16; - for (i = 0; i < MAXSKINCOLORS; i++) - if (!stricmp(KartColor_Names[i],temp)) // SRB2kart - { - pdemo->standings[count].color = i; - break; - } - - // Score/time/whatever - pdemo->standings[count].timeorscore = READUINT32(extrainfo_p); - - count++; - - if (count >= MAXPLAYERS) - break; //@TODO still cycle through the rest of these if extra demo data is ever used - } - - // I think that's everything we need? - Z_Free(infobuffer); -} - -#if defined (_WIN32) -// return the file creation time -// useful for demos that were renamed -static long G_GetCreationTime(char *filepath) -{ - struct stat fileinfo; - - if (stat(filepath, &fileinfo) == 0) - return fileinfo.st_ctime; - - return 0; -} -#endif - -static char *G_GetDemoDate(menudemo_t *pdemo) -{ - char *datetime; - datetime = malloc(sizeof(pdemo->date)); // mallocma balls - - // no mallocma balls... :c - if (!datetime) - { - return NULL; - } - - time_t file_time = 0; - - // get le filepath - char *filename; - filename = strdup(pdemo->filepath); - -#if defined (_WIN32) - if (!filename) - { - // if we cant get a filename try just getting the file create time - file_time = G_GetCreationTime(pdemo->filepath); - goto skipfilenametime; - } -#else - if (!filename) - { - free(datetime); - return NULL; - } -#endif - - // get the actual filename Zzz... - nameonly(filename); - - // convert it to long Zzz.... - file_time = strtol(filename, NULL, 10); - free(filename); // dont need this anymore a - -#if defined (_WIN32) -skipfilenametime: -#endif - - // then throw it into localtime to get an actual human readable format lmao - struct tm *tm_buf = NULL; - tm_buf = localtime(&file_time); - - // cant believe we ended up in 1970 - if (tm_buf == NULL || tm_buf->tm_year <= 110) - { -#if defined (_WIN32) - // uh ohh, we got an invalid time - // try one more time getting the creation time - file_time = G_GetCreationTime(pdemo->filepath); - tm_buf = localtime(&file_time); - - if (tm_buf == NULL || tm_buf->tm_year <= 110) - { - free(datetime); - return NULL; - } - - goto gotcreationtime; -#else - free(datetime); - return NULL; -#endif - } - -#if defined (_WIN32) -gotcreationtime: -#endif - - const char *format; - - // US ppl are special (: - if (cv_demodateformat.value == 2) - format = "%m.%d.%Y"; - else if (cv_demodateformat.value == 1) - format = "%d.%m.%Y"; - else - format = strstr(setlocale(LC_TIME, NULL), "en_US") ? "%m.%d.%Y" : "%d.%m.%Y"; - - strftime(datetime, sizeof(pdemo->date), format, tm_buf); - - return datetime; -} - -void G_LoadDemoTitle(menudemo_t *pdemo) -{ - UINT8 infobuffer[96], *info_p; - UINT16 pdemoversion; - size_t count; - - FILE *handle = fopen(pdemo->filepath, "rb"); - - if (!handle) - { - CONS_Alert(CONS_ERROR, M_GetText("Failed to read file '%s'.\n"), pdemo->filepath); - sprintf(pdemo->title, "INVALID REPLAY"); - return; - } - - count = fread(infobuffer, 1, 96, handle); - fclose(handle); - info_p = infobuffer; - - // First check isn't too accurate technically, but well, valid replays should be larger than that anyway - if (count < 96 || memcmp(info_p, DEMOHEADER, 12)) - { - CONS_Alert(CONS_ERROR, M_GetText("%s is not a SRB2Kart replay file.\n"), pdemo->filepath); - sprintf(pdemo->title, "INVALID REPLAY"); - return; - } - - info_p += 12; // DEMOHEADER - - READUINT8(info_p); - READUINT8(info_p); - pdemoversion = READUINT16(info_p); - - memset(pdemo->date, 0, sizeof(pdemo->date)); - - switch(pdemoversion) - { - case DEMOVERSION: // latest always supported - // demo title - M_Memcpy(pdemo->title, info_p, 64); - - // demo date - char *demodate; - demodate = G_GetDemoDate(pdemo); - if (demodate) - strncpy(pdemo->date, demodate, sizeof(pdemo->date)); - free(demodate); - break; -#ifdef DEMO_COMPAT_100 - case 0x0001: - sprintf(pdemo->title, "Legacy Replay"); - break; -#endif - // too old, cannot support. - default: - CONS_Alert(CONS_ERROR, M_GetText("%s is an incompatible replay format and cannot be played.\n"), pdemo->filepath); - sprintf(pdemo->title, "INVALID REPLAY"); - } -} - -// -// G_PlayDemo -// -void G_DeferedPlayDemo(const char *name) -{ - COM_BufAddText("playdemo \""); - COM_BufAddText(name); - COM_BufAddText("\" -addfiles\n"); -} - -// -// Start a demo from a .LMP file or from a wad resource -// -#define SKIPERRORS -void G_DoPlayDemo(char *defdemoname) -{ - UINT8 i, p; - lumpnum_t l; - char skin[17],color[17],*n,*pdemoname; - UINT8 version,subversion; - UINT32 randseed; - char msg[1024]; -#if defined(SKIPERRORS) && !defined(DEVELOP) - boolean skiperrors = false; -#endif - boolean spectator; - UINT8 slots[MAXPLAYERS], kartspeed[MAXPLAYERS], kartweight[MAXPLAYERS], numslots = 0; - - G_InitDemoRewind(); - - skin[16] = '\0'; - color[16] = '\0'; - - // No demo name means we're restarting the current demo - if (defdemoname == NULL) - { - demobuf.p = demobuf.buffer; - pdemoname = ZZ_Alloc(1); // Easier than adding checks for this everywhere it's freed - } - else - { - n = defdemoname+strlen(defdemoname); - while (*n != '/' && *n != '\\' && n != defdemoname) - n--; - if (n != defdemoname) - n++; - pdemoname = ZZ_Alloc(strlen(n)+1); - strcpy(pdemoname,n); - - M_SetPlaybackMenuPointer(); - - // Internal if no extension, external if one exists - if (FIL_CheckExtension(defdemoname)) - { - //FIL_DefaultExtension(defdemoname, ".lmp"); - if (!FIL_ReadFile(defdemoname, &demobuf.buffer)) - { - snprintf(msg, 1024, M_GetText("Failed to read file '%s'.\n"), defdemoname); - CONS_Alert(CONS_ERROR, "%s", msg); - gameaction = ga_nothing; - M_StartMessage(msg, NULL, MM_NOTHING); - return; - } - demobuf.p = demobuf.buffer; - } - // load demo resource from WAD - else if ((l = W_CheckNumForName(defdemoname)) == LUMPERROR) - { - snprintf(msg, 1024, M_GetText("Failed to read lump '%s'.\n"), defdemoname); - CONS_Alert(CONS_ERROR, "%s", msg); - gameaction = ga_nothing; - M_StartMessage(msg, NULL, MM_NOTHING); - return; - } - else // it's an internal demo - { - demobuf.buffer = demobuf.p = W_CacheLumpNum(l, PU_STATIC); -#if defined(SKIPERRORS) && !defined(DEVELOP) - skiperrors = true; // SRB2Kart: Don't print warnings for staff ghosts, since they'll inevitably happen when we make bugfixes/changes... -#endif - } - } - - // read demo header - gameaction = ga_nothing; - demo.playback = true; - if (memcmp(demobuf.p, DEMOHEADER, 12)) - { - snprintf(msg, 1024, M_GetText("%s is not a SRB2Kart replay file.\n"), pdemoname); - CONS_Alert(CONS_ERROR, "%s", msg); - M_StartMessage(msg, NULL, MM_NOTHING); - G_ResetDemoPlayback(pdemoname); - return; - } - demobuf.p += 12; // DEMOHEADER - - version = READUINT8(demobuf.p); - subversion = READUINT8(demobuf.p); - demo.version = READUINT16(demobuf.p); - switch(demo.version) - { - case DEMOVERSION: // latest always supported - // demo title - M_Memcpy(demo.titlename, demobuf.p, 64); - demobuf.p += 64; - - break; -#ifdef DEMO_COMPAT_100 - case 0x0001: - break; -#endif - // too old, cannot support. - default: - snprintf(msg, 1024, M_GetText("%s is an incompatible replay format and cannot be played.\n"), pdemoname); - CONS_Alert(CONS_ERROR, "%s", msg); - M_StartMessage(msg, NULL, MM_NOTHING); - G_ResetDemoPlayback(pdemoname); - return; - } - demobuf.p += 16; // demo checksum - if (memcmp(demobuf.p, "PLAY", 4)) - { - snprintf(msg, 1024, M_GetText("%s is the wrong type of recording and cannot be played.\n"), pdemoname); - CONS_Alert(CONS_ERROR, "%s", msg); - M_StartMessage(msg, NULL, MM_NOTHING); - G_ResetDemoPlayback(pdemoname); - return; - } - demobuf.p += 4; // "PLAY" - gamemap = READINT16(demobuf.p); - demobuf.p += 16; // mapmd5 - - demoflags = READUINT8(demobuf.p); -#ifdef DEMO_COMPAT_100 - if (demo.version == 0x0001) - { - if (demoflags & DF_MULTIPLAYER) - { - snprintf(msg, 1024, M_GetText("%s is an alpha multiplayer replay and cannot be played.\n"), pdemoname); - CONS_Alert(CONS_ERROR, "%s", msg); - M_StartMessage(msg, NULL, MM_NOTHING); - G_ResetDemoPlayback(pdemoname); - return; - } - } - else - { -#endif - gametype = READUINT8(demobuf.p); - - if (demo.title) // Titledemos should always play and ought to always be compatible with whatever wadlist is running. - G_SkipDemoExtraFiles(&demobuf.p); - else if (demo.loadfiles) - G_LoadDemoExtraFiles(&demobuf.p); - else if (demo.ignorefiles) - G_SkipDemoExtraFiles(&demobuf.p); - else - { - UINT8 error = G_CheckDemoExtraFiles(&demobuf.p, false); - - if (error) - { - switch (error) - { - case DFILE_ERROR_NOTLOADED: - snprintf(msg, 1024, - M_GetText("Required files for this demo are not loaded.\n\nUse\n\"playdemo %s -addfiles\"\nto load them and play the demo.\n"), - pdemoname); - break; - - case DFILE_ERROR_OUTOFORDER: - snprintf(msg, 1024, - M_GetText("Required files for this demo are loaded out of order.\n\nUse\n\"playdemo %s -force\"\nto play the demo anyway.\n"), - pdemoname); - break; - - case DFILE_ERROR_INCOMPLETEOUTOFORDER: - snprintf(msg, 1024, - M_GetText("Required files for this demo are not loaded, and some are out of order.\n\nUse\n\"playdemo %s -addfiles\"\nto load needed files and play the demo.\n"), - pdemoname); - break; - - case DFILE_ERROR_CANNOTLOAD: - snprintf(msg, 1024, - M_GetText("Required files for this demo cannot be loaded.\n\nUse\n\"playdemo %s -force\"\nto play the demo anyway.\n"), - pdemoname); - break; - - case DFILE_ERROR_EXTRAFILES: - snprintf(msg, 1024, - M_GetText("You have additional files loaded beyond the demo's file list.\n\nUse\n\"playdemo %s -force\"\nto play the demo anyway.\n"), - pdemoname); - break; - } - - CONS_Alert(CONS_ERROR, "%s", msg); - if (!CON_Ready()) // In the console they'll just see the notice there! No point pulling them out. - M_StartMessage(msg, NULL, MM_NOTHING); - G_ResetDemoPlayback(pdemoname); - return; - } - } -#ifdef DEMO_COMPAT_100 - } -#endif - - modeattacking = (demoflags & DF_ATTACKMASK)>>DF_ATTACKSHIFT; - multiplayer = !!(demoflags & DF_MULTIPLAYER); - CON_ToggleOff(); - - hu_demotime = UINT32_MAX; - hu_demolap = UINT32_MAX; - - switch (modeattacking) - { - case ATTACKING_NONE: // 0 - break; - case ATTACKING_RECORD: // 1 - hu_demotime = READUINT32(demobuf.p); - hu_demolap = READUINT32(demobuf.p); - break; - default: // 3 - modeattacking = ATTACKING_NONE; - break; - } - - // Random seed - randseed = READUINT32(demobuf.p); -#ifdef DEMO_COMPAT_100 - if (demo.version != 0x0001) -#endif - demobuf.p += 4; // Extrainfo location - -#ifdef DEMO_COMPAT_100 - if (demo.version == 0x0001) - { - // Player name - M_Memcpy(player_names[0],demobuf.p,16); - demobuf.p += 16; - - // Skin - M_Memcpy(skin,demobuf.p,16); - demobuf.p += 16; - - // Color - M_Memcpy(color,demobuf.p,16); - demobuf.p += 16; - - demobuf.p += 5; // Backwards compat - some stats - // SRB2kart - kartspeed[0] = READUINT8(demobuf.p); - kartweight[0] = READUINT8(demobuf.p); - // - demobuf.p += 9; // Backwards compat - more stats - - // Skin not loaded? - if (!SetPlayerSkin(0, skin)) - { - snprintf(msg, 1024, M_GetText("%s features a character that is not currently loaded.\n"), pdemoname); - CONS_Alert(CONS_ERROR, "%s", msg); - M_StartMessage(msg, NULL, MM_NOTHING); - G_ResetDemoPlayback(pdemoname); - return; - } - - // ...*map* not loaded? - if (!gamemap || (gamemap > NUMMAPS) || !mapheaderinfo[gamemap-1] || !(mapheaderinfo[gamemap-1]->menuflags & LF2_EXISTSHACK)) - { - snprintf(msg, 1024, M_GetText("%s features a course that is not currently loaded.\n"), pdemoname); - CONS_Alert(CONS_ERROR, "%s", msg); - M_StartMessage(msg, NULL, MM_NOTHING); - G_ResetDemoPlayback(pdemoname); - return; - } - - // Set color - for (i = 0; i < MAXSKINCOLORS; i++) - if (!stricmp(KartColor_Names[i],color)) // SRB2kart - { - players[0].skincolor = i; - break; - } - - // net var data - CV_LoadNetVars(&demobuf.p); - - // Sigh ... it's an empty demo. - if (*demobuf.p == DEMOMARKER) - { - snprintf(msg, 1024, M_GetText("%s contains no data to be played.\n"), pdemoname); - CONS_Alert(CONS_ERROR, "%s", msg); - M_StartMessage(msg, NULL, MM_NOTHING); - G_ResetDemoPlayback(pdemoname); - return; - } - - Z_Free(pdemoname); - - memset(&oldcmd,0,sizeof(oldcmd)); - memset(&oldghost,0,sizeof(oldghost)); - memset(&ghostext,0,sizeof(ghostext)); - - CONS_Alert(CONS_WARNING, M_GetText("Demo version does not match game version. Desyncs may occur.\n")); - - // console warning messages -#if defined(SKIPERRORS) && !defined(DEVELOP) - demosynced = (!skiperrors); -#else - demosynced = true; -#endif - - // didn't start recording right away. - demo.deferstart = false; - - consoleplayer = 0; - memset(displayplayers, 0, sizeof(displayplayers)); - memset(playeringame, 0, sizeof(playeringame)); - playeringame[0] = true; - - goto post_compat; - } -#endif - - // net var data - CV_LoadNetVars(&demobuf.p); - - // Sigh ... it's an empty demo. - if (*demobuf.p == DEMOMARKER) - { - snprintf(msg, 1024, M_GetText("%s contains no data to be played.\n"), pdemoname); - CONS_Alert(CONS_ERROR, "%s", msg); - M_StartMessage(msg, NULL, MM_NOTHING); - G_ResetDemoPlayback(pdemoname); - return; - } - - Z_Free(pdemoname); - - memset(&oldcmd,0,sizeof(oldcmd)); - memset(&oldghost,0,sizeof(oldghost)); - memset(&ghostext,0,sizeof(ghostext)); - -#if defined(SKIPERRORS) && !defined(DEVELOP) - if ((VERSION != version || SUBVERSION != subversion) && !skiperrors) -#else - if (VERSION != version || SUBVERSION != subversion) -#endif - CONS_Alert(CONS_WARNING, M_GetText("Demo version does not match game version. Desyncs may occur.\n")); - - // console warning messages -#if defined(SKIPERRORS) && !defined(DEVELOP) - demosynced = (!skiperrors); -#else - demosynced = true; -#endif - - // didn't start recording right away. - demo.deferstart = false; - - //LUA_HookInt(gamemap, HOOK(MapChange)); - - consoleplayer = 0; - memset(playeringame, 0, sizeof(playeringame)); - memset(displayplayers, 0, sizeof(displayplayers)); - memset(camera, 0, sizeof(camera)); // reset freecam - - // Load players that were in-game when the map started - p = READUINT8(demobuf.p); - - while (p != 0xFF) - { - spectator = false; - if (p & DEMO_SPECTATOR) - { - spectator = true; - p &= ~DEMO_SPECTATOR; - - if (modeattacking) - { - snprintf(msg, 1024, M_GetText("%s is a Record Attack replay with spectators, and is thus invalid.\n"), pdemoname); - CONS_Alert(CONS_ERROR, "%s", msg); - M_StartMessage(msg, NULL, MM_NOTHING); - G_ResetDemoPlayback(pdemoname); - return; - } - } - slots[numslots] = p; numslots++; - - if (modeattacking && numslots > 1) - { - snprintf(msg, 1024, M_GetText("%s is a Record Attack replay with multiple players, and is thus invalid.\n"), pdemoname); - CONS_Alert(CONS_ERROR, "%s", msg); - M_StartMessage(msg, NULL, MM_NOTHING); - G_ResetDemoPlayback(pdemoname); - return; - } - - if (!playeringame[displayplayers[0]] || players[displayplayers[0]].spectator) - displayplayers[0] = consoleplayer = serverplayer = p; - - playeringame[p] = true; - players[p].spectator = spectator; - - // Name - M_Memcpy(player_names[p],demobuf.p,16); - demobuf.p += 16; - - // Skin - M_Memcpy(skin,demobuf.p,16); - demobuf.p += 16; - SetPlayerSkin(p, skin); - - // Color - M_Memcpy(color,demobuf.p,16); - demobuf.p += 16; - for (i = 0; i < MAXSKINCOLORS; i++) - if (!stricmp(KartColor_Names[i],color)) // SRB2kart - { - players[p].skincolor = i; - break; - } - - // Score, since Kart uses this to determine where you start on the map - players[p].score = READUINT32(demobuf.p); - - // Kart stats, temporarily - kartspeed[p] = READUINT8(demobuf.p); - kartweight[p] = READUINT8(demobuf.p); - - if (stricmp(skins[players[p].skin].name, skin) != 0) - FindClosestSkinForStats(p, kartspeed[p], kartweight[p]); - - // Look for the next player - p = READUINT8(demobuf.p); - } - - // end of player read (the 0xFF marker) - // so this is where we are to read our lua variables (if possible!) - if (demoflags & DF_LUAVARS) // again, used for compability, lua shit will be saved to replays regardless of if it's even been loaded - { - if (!gL) // No Lua state! ...I guess we'll just start one... - LUA_ClearState(); - - // No modeattacking check, DF_LUAVARS won't be present here. - LUA_UnArchive(&demobuf, false); - } - - splitscreen = 0; - - if (demo.title) - { - splitscreen = M_RandomKey(6)-1; - splitscreen = min(min(3, numslots-1), splitscreen); // Bias toward 1p and 4p views - - for (p = 0; p <= splitscreen; p++) - G_ResetView(p+1, slots[M_RandomKey(numslots)], false); - } - - R_ExecuteSetViewSize(); - -#ifdef DEMO_COMPAT_100 -post_compat: -#endif - - P_SetRandSeed(randseed); - G_InitNew(demoflags & DF_ENCORE, G_BuildMapName(gamemap), true, true); // Doesn't matter whether you reset or not here, given changes to resetplayer. - - for (i = 0; i < MAXPLAYERS; i++) - { - if (players[i].mo) - { - players[i].mo->color = players[i].skincolor; - oldghost[i].x = players[i].mo->x; - oldghost[i].y = players[i].mo->y; - oldghost[i].z = players[i].mo->z; - } - - // Set saved attribute values - // No cheat checking here, because even if they ARE wrong... - // it would only break the replay if we clipped them. - players[i].kartspeed = kartspeed[i]; - players[i].kartweight = kartweight[i]; - } - - if (cv_director.value) - CV_SetValue(&cv_director, 0); - - demo.deferstart = true; -} -#undef SKIPERRORS - -void G_AddGhost(char *defdemoname) -{ - INT32 i; - lumpnum_t l; - char name[17],skin[17],color[17],*n,*pdemoname,md5[16]; - demoghost *gh; - UINT8 flags; - UINT8 *buffer,*p; - mapthing_t *mthing; - UINT16 count, ghostversion; - skin_t *ghskin = &skins[0]; - UINT8 kartspeed = UINT8_MAX, kartweight = UINT8_MAX; - - name[16] = '\0'; - skin[16] = '\0'; - color[16] = '\0'; - - n = defdemoname+strlen(defdemoname); - while (*n != '/' && *n != '\\' && n != defdemoname) - n--; - if (n != defdemoname) - n++; - pdemoname = ZZ_Alloc(strlen(n)+1); - strcpy(pdemoname,n); - - // Internal if no extension, external if one exists - if (FIL_CheckExtension(defdemoname)) - { - //FIL_DefaultExtension(defdemoname, ".lmp"); - if (!FIL_ReadFileTag(defdemoname, &buffer, PU_LEVEL)) - { - CONS_Alert(CONS_ERROR, M_GetText("Failed to read file '%s'.\n"), defdemoname); - Z_Free(pdemoname); - return; - } - p = buffer; - } - // load demo resource from WAD - else if ((l = W_CheckNumForName(defdemoname)) == LUMPERROR) - { - CONS_Alert(CONS_ERROR, M_GetText("Failed to read lump '%s'.\n"), defdemoname); - Z_Free(pdemoname); - return; - } - else // it's an internal demo - buffer = p = W_CacheLumpNum(l, PU_LEVEL); - - // read demo header - if (memcmp(p, DEMOHEADER, 12)) - { - CONS_Alert(CONS_NOTICE, M_GetText("Ghost %s: Not a SRB2Kart replay.\n"), pdemoname); - Z_Free(pdemoname); - Z_Free(buffer); - return; - } - - p += 12; // DEMOHEADER - p++; // VERSION - p++; // SUBVERSION - - ghostversion = READUINT16(p); - switch(ghostversion) - { - case DEMOVERSION: // latest always supported - p += 64; // title - break; -#ifdef DEMO_COMPAT_100 - case 0x0001: - break; -#endif - // too old, cannot support. - default: - CONS_Alert(CONS_NOTICE, M_GetText("Ghost %s: Demo version incompatible.\n"), pdemoname); - Z_Free(pdemoname); - Z_Free(buffer); - return; - } - - M_Memcpy(md5, p, 16); p += 16; // demo checksum - for (gh = ghosts; gh; gh = gh->next) - if (!memcmp(md5, gh->checksum, 16)) // another ghost in the game already has this checksum? - { // Don't add another one, then! - CONS_Debug(DBG_SETUP, "Rejecting duplicate ghost %s (MD5 was matched)\n", pdemoname); - Z_Free(pdemoname); - Z_Free(buffer); - return; - } - - if (memcmp(p, "PLAY", 4)) - { - CONS_Alert(CONS_NOTICE, M_GetText("Ghost %s: Demo format unacceptable.\n"), pdemoname); - Z_Free(pdemoname); - Z_Free(buffer); - return; - } - - p += 4; // "PLAY" - p += 2; // gamemap - p += 16; // mapmd5 (possibly check for consistency?) - - flags = READUINT8(p); - - if (!(flags & DF_GHOST)) - { - CONS_Alert(CONS_NOTICE, M_GetText("Ghost %s: No ghost data in this demo.\n"), pdemoname); - Z_Free(pdemoname); - Z_Free(buffer); - return; - } - -#ifdef DEMO_COMPAT_100 - if (ghostversion != 0x0001) -#endif - p++; // gametype - -#ifdef DEMO_COMPAT_100 - if (ghostversion != 0x0001) -#endif - G_SkipDemoExtraFiles(&p); // Don't wanna modify the file list for ghosts. - switch ((flags & DF_ATTACKMASK)>>DF_ATTACKSHIFT) - { - case ATTACKING_NONE: // 0 - break; - case ATTACKING_RECORD: // 1 - p += 8; // demo time, lap - break; - default: // 3 - break; - } - - p += 4; // random seed - -#ifdef DEMO_COMPAT_100 - if (ghostversion == 0x0001) - { - // Player name (TODO: Display this somehow if it doesn't match cv_playername!) - M_Memcpy(name, p,16); - p += 16; - - // Skin - M_Memcpy(skin, p,16); - p += 16; - - // Color - M_Memcpy(color, p,16); - p += 16; - - // Ghosts do not have a player structure to put this in. - p++; // charability - p++; // charability2 - p++; // actionspd - p++; // mindash - p++; // maxdash - // SRB2kart - p++; // kartspeed - p++; // kartweight - // - p++; // normalspeed - p++; // runspeed - p++; // thrustfactor - p++; // accelstart - p++; // acceleration - p += 4; // jumpfactor - } - else -#endif - p += 4; // Extra data location reference - - // net var data - count = READUINT16(p); - while (count--) - { - p += 2; - SKIPSTRING(p); - p++; - } - - if (*p == DEMOMARKER) - { - CONS_Alert(CONS_NOTICE, M_GetText("Failed to add ghost %s: Replay is empty.\n"), pdemoname); - Z_Free(pdemoname); - Z_Free(buffer); - return; - } - -#ifdef DEMO_COMPAT_100 - if (ghostversion != 0x0001) - { -#endif - if (READUINT8(p) != 0) - { - CONS_Alert(CONS_NOTICE, M_GetText("Failed to add ghost %s: Invalid player slot.\n"), pdemoname); - Z_Free(pdemoname); - Z_Free(buffer); - return; - } - - // Player name (TODO: Display this somehow if it doesn't match cv_playername!) - M_Memcpy(name, p, 16); - p += 16; - - // Skin - M_Memcpy(skin, p, 16); - p += 16; - - // Color - M_Memcpy(color, p, 16); - p += 16; - - p += 4; // score - - kartspeed = READUINT8(p); - kartweight = READUINT8(p); - - if (READUINT8(p) != 0xFF) - { - CONS_Alert(CONS_NOTICE, M_GetText("Failed to add ghost %s: Invalid player slot.\n"), pdemoname); - Z_Free(pdemoname); - Z_Free(buffer); - return; - } -#ifdef DEMO_COMPAT_100 - } -#endif - - for (i = 0; i < numskins; i++) - if (!stricmp(skins[i].name,skin)) - { - ghskin = &skins[i]; - break; - } - - if (i == numskins) - { - if (kartspeed != UINT8_MAX && kartweight != UINT8_MAX) - ghskin = &skins[GetSkinNumClosestToStats(kartspeed, kartweight)]; - - CONS_Alert(CONS_NOTICE, M_GetText("Ghost %s: Invalid character. Falling back to %s.\n"), pdemoname, ghskin->name); - } - - gh = Z_Calloc(sizeof(demoghost), PU_LEVEL, NULL); - gh->next = ghosts; - gh->buffer = buffer; - M_Memcpy(gh->checksum, md5, 16); - gh->p = p; - - ghosts = gh; - - gh->version = ghostversion; - mthing = playerstarts[0]; - I_Assert(mthing); - { // A bit more complex than P_SpawnPlayer because ghosts aren't solid and won't just push themselves out of the ceiling. - fixed_t z,f,c; - gh->mo = P_SpawnMobj(mthing->x << FRACBITS, mthing->y << FRACBITS, 0, MT_GHOST); - gh->mo->angle = FixedAngle(mthing->angle*FRACUNIT); - f = gh->mo->floorz; - c = gh->mo->ceilingz - mobjinfo[MT_PLAYER].height; - if (!!(mthing->options & MTF_AMBUSH) ^ !!(mthing->options & MTF_OBJECTFLIP)) - { - z = c; - if (mthing->options >> ZSHIFT) - z -= ((mthing->options >> ZSHIFT) << FRACBITS); - if (z < f) - z = f; - } - else - { - z = f; - if (mthing->options >> ZSHIFT) - z += ((mthing->options >> ZSHIFT) << FRACBITS); - if (z > c) - z = c; - } - gh->mo->z = z; - } - gh->mo->state = states+S_KART_STND1; // SRB2kart - was S_PLAY_STND - gh->mo->sprite = gh->mo->state->sprite; - gh->mo->frame = (gh->mo->state->frame & FF_FRAMEMASK) | tr_trans20<mo->tics = -1; - - gh->oldmo.x = gh->mo->x; - gh->oldmo.y = gh->mo->y; - gh->oldmo.z = gh->mo->z; - - // Set skin - gh->mo->skin = gh->oldmo.skin = ghskin; - - // Set color - gh->mo->color = ((skin_t*)gh->mo->skin)->prefcolor; - for (i = 0; i < MAXSKINCOLORS; i++) - if (!stricmp(KartColor_Names[i],color)) // SRB2kart - { - gh->mo->color = (UINT8)i; - break; - } - gh->oldmo.color = gh->mo->color; - - CONS_Printf(M_GetText("Added ghost %s from %s\n"), name, pdemoname); - Z_Free(pdemoname); -} - -// A simplified version of G_AddGhost... -void G_UpdateStaffGhostName(lumpnum_t l) -{ - UINT8 *buffer,*p; - UINT16 ghostversion; - UINT8 flags; - - buffer = p = W_CacheLumpNum(l, PU_CACHE); - - // read demo header - if (memcmp(p, DEMOHEADER, 12)) - { - goto fail; - } - - p += 12; // DEMOHEADER - p++; // VERSION - p++; // SUBVERSION - - ghostversion = READUINT16(p); - switch(ghostversion) - { - case DEMOVERSION: // latest always supported - p += 64; // full demo title - break; - -#ifdef DEMO_COMPAT_100 - case 0x0001: - break; -#endif - - // too old, cannot support. - default: - goto fail; - } - - p += 16; // demo checksum - - if (memcmp(p, "PLAY", 4)) - { - goto fail; - } - - p += 4; // "PLAY" - p += 2; // gamemap - p += 16; // mapmd5 (possibly check for consistency?) - - flags = READUINT8(p); - if (!(flags & DF_GHOST)) - { - goto fail; // we don't NEED to do it here, but whatever - } - -#ifdef DEMO_COMPAT_100 - if (ghostversion != 0x0001) -#endif - p++; // Gametype - -#ifdef DEMO_COMPAT_100 - if (ghostversion != 0x0001) -#endif - G_SkipDemoExtraFiles(&p); - - switch ((flags & DF_ATTACKMASK)>>DF_ATTACKSHIFT) - { - case ATTACKING_NONE: // 0 - break; - case ATTACKING_RECORD: // 1 - p += 8; // demo time, lap - break; - default: // 3 - break; - } - - p += 4; // random seed - - -#ifdef DEMO_COMPAT_100 - if (ghostversion == 0x0001) - { - // Player name - M_Memcpy(dummystaffname, p,16); - dummystaffname[16] = '\0'; - goto fail; // Not really a failure but whatever - } -#endif - - p += 4; // Extrainfo location marker - - // Ehhhh don't need ghostversion here (?) so I'll reuse the var here - ghostversion = READUINT16(p); - while (ghostversion--) - { - p += 2; - SKIPSTRING(p); - p++; // stealth - } - - // Assert first player is in and then read name - if (READUINT8(p) != 0) - goto fail; - M_Memcpy(dummystaffname, p,16); - dummystaffname[16] = '\0'; - - // Ok, no longer any reason to care, bye -fail: - Z_Free(buffer); - return; -} - -// -// G_TimeDemo -// NOTE: name is a full filename for external demos -// -static INT32 restorecv_vidwait; - -void G_TimeDemo(const char *name) -{ - nodrawers = M_CheckParm("-nodraw"); - noblit = M_CheckParm("-noblit"); - restorecv_vidwait = cv_vidwait.value; - if (cv_vidwait.value) - CV_Set(&cv_vidwait, "0"); - demo.timing = true; - singletics = true; - framecount = 0; - demostarttime = I_GetTime(); - G_DeferedPlayDemo(name); -} - -void G_DoPlayMetal(void) -{ - lumpnum_t l; - mobj_t *mo = NULL; - thinker_t *th; - - // it's an internal demo - if ((l = W_CheckNumForName(va("%sMS",G_BuildMapName(gamemap)))) == LUMPERROR) - { - CONS_Alert(CONS_WARNING, M_GetText("No bot recording for this map.\n")); - return; - } - else - metalbuffer = metal_p = W_CacheLumpNum(l, PU_STATIC); - - // find metal sonic - for (th = thinkercap.next; th != &thinkercap; th = th->next) - { - if (th->function != (actionf_p1)P_MobjThinker) - continue; - - mo = (mobj_t *)th; - if (mo->type == MT_METALSONIC_RACE) - break; - } - if (!mo) - { - CONS_Alert(CONS_ERROR, M_GetText("Failed to find bot entity.\n")); - Z_Free(metalbuffer); - return; - } - - // read demo header - metal_p += 12; // DEMOHEADER - metal_p++; // VERSION - metal_p++; // SUBVERSION - metalversion = READUINT16(metal_p); - switch(metalversion) - { - case DEMOVERSION: // latest always supported - break; -#ifdef DEMO_COMPAT_100 - case 0x0001: - I_Error("You need to implement demo compat here, doofus! %s:%d", __FILE__, __LINE__); -#endif - // too old, cannot support. - default: - CONS_Alert(CONS_WARNING, M_GetText("Failed to load bot recording for this map, format version incompatible.\n")); - Z_Free(metalbuffer); - return; - } - metal_p += 16; // demo checksum - if (memcmp(metal_p, "METL", 4)) - { - CONS_Alert(CONS_WARNING, M_GetText("Failed to load bot recording for this map, wasn't recorded in Metal format.\n")); - Z_Free(metalbuffer); - return; - } metal_p += 4; // "METL" - - // read initial tic - memset(&oldmetal,0,sizeof(oldmetal)); - oldmetal.x = mo->x; - oldmetal.y = mo->y; - oldmetal.z = mo->z; - oldmetal.angle = mo->angle; - metalplayback = mo; -} - -void G_DoneLevelLoad(void) -{ - CONS_Printf(M_GetText("Loaded level in %f sec\n"), (double)(I_GetTime() - demostarttime) / TICRATE); - framecount = 0; - demostarttime = I_GetTime(); -} - -/* -=================== -= -= G_CheckDemoStatus -= -= Called after a death or level completion to allow demos to be cleaned up -= Returns true if a new demo loop action will take place -=================== -*/ - -// Stops metal sonic's demo. Separate from other functions because metal + replays can coexist -void G_StopMetalDemo(void) -{ - // Metal Sonic finishing doesn't end the game, dammit. - Z_Free(metalbuffer); - metalbuffer = NULL; - metalplayback = NULL; - metal_p = NULL; -} - -// Stops metal sonic recording. -ATTRNORETURN void FUNCNORETURN G_StopMetalRecording(void) -{ - boolean saved = false; - if (demobuf.p && demobuf.buffer != NULL) - { - UINT8 *p = demobuf.buffer+16; // checksum position -#ifdef NOMD5 - UINT8 i; - WRITEUINT8(demobuf.p, DEMOMARKER); // add the demo end marker - for (i = 0; i < 16; i++, p++) - *p = P_RandomByte(); // This MD5 was chosen by fair dice roll and most likely < 50% correct. -#else - WRITEUINT8(demobuf.p, DEMOMARKER); // add the demo end marker - md5_buffer((char *)p+16, demobuf.p - (p+16), (void *)p); // make a checksum of everything after the checksum in the file. -#endif - saved = FIL_WriteFile(va("%sMS.LMP", G_BuildMapName(gamemap)), demobuf.buffer, demobuf.p - demobuf.buffer); // finally output the file. - } - Z_Free(demobuf.buffer); - demobuf.buffer = NULL; - metalrecording = false; - if (saved) - I_Error("Saved to %sMS.LMP", G_BuildMapName(gamemap)); - I_Error("Failed to save demo!"); -} - -// reset engine variable set for the demos -// called from stopdemo command, map command, and g_checkdemoStatus. -void G_StopDemo(void) -{ - Z_Free(demobuf.buffer); - demobuf.buffer = NULL; - demo.playback = false; - if (demo.title) - modeattacking = ATTACKING_NONE; - demo.title = false; - demo.timing = false; - singletics = false; - - UINT8 i; - for (i = 0; i < MAXSPLITSCREENPLAYERS; ++i) - { - camera[i].freecam = false; - camera[i].localangle = 0; - camera[i].localaiming = 0; - } - - CV_SetValue(&cv_playbackspeed, 1); - demo.rewinding = false; - CL_ClearRewinds(); - - if (gamestate == GS_LEVEL && rendermode != render_none) - { - V_SetPaletteLump("PLAYPAL"); // Reset the palette - R_ReInitColormaps(0, LUMPERROR); - } - if (gamestate == GS_INTERMISSION) - Y_EndIntermission(); // cleanup - if (gamestate == GS_VOTING) - Y_EndVote(); - - M_ClearMenus(true); - - G_SetGamestate(GS_NULL); - wipegamestate = GS_NULL; - SV_StopServer(); - SV_ResetServer(); -} - -// Stops timing a demo. -static void G_StopTimingDemo(void) -{ - INT32 demotime; - double f1, f2; - demotime = I_GetTime() - demostarttime; - if (!demotime) - return; - G_StopDemo(); - demo.timing = false; - f1 = (double)demotime; - f2 = (double)framecount*TICRATE; - - CONS_Printf(M_GetText("timed %u gametics in %d realtics - %u frames\n%f seconds, %f avg fps\n"), - leveltime, demotime, (UINT32)framecount, f1/TICRATE, f2/f1); - - if (restorecv_vidwait != cv_vidwait.value) - CV_SetValue(&cv_vidwait, restorecv_vidwait); - - D_StartTitle(); -} - - -boolean G_CheckDemoStatus(void) -{ - while (ghosts) - { - demoghost *next = ghosts->next; - Z_Free(ghosts); - ghosts = next; - } - ghosts = NULL; - - // DO NOT end metal sonic demos here - - if (demo.timing) - { - G_StopTimingDemo(); - return true; - } - - if (demo.playback) - { - if (demo.quitafterplaying) - I_Quit(); - - if (multiplayer && !demo.title) - { - G_ExitLevel(); - } - else if (modeattacking && !demo.title) // nooo dont crash our titledemos - { - G_StopDemo(); - M_EndModeAttackRun(); - } - else - { - G_StopDemo(); - D_StartTitle(); - } - - return true; - } - - if (!demo.recording) - return false; - - if (modeattacking || demo.savemode != DSM_NOTSAVING) - { - G_SaveDemo(); - return true; - } - - G_ResetDemoRecording(); - - return false; -} - -void G_ResetDemoRecording(void) -{ - if (demobuf.buffer) - Z_Free(demobuf.buffer); - demobuf.buffer = NULL; - demo.recording = false; -} - -static void G_ResetDemoPlayback(char *pdemoname) -{ - if (pdemoname) - Z_Free(pdemoname); - if (demobuf.buffer) - Z_Free(demobuf.buffer); - demobuf.buffer = NULL; - demo.playback = false; - if (demo.title) - modeattacking = ATTACKING_NONE; - demo.title = false; -} - -void G_SaveDemo(void) -{ - if (!demobuf.p) - { - CONS_Alert(CONS_ERROR, "Failed to save Demo. No Demo pointer exists!\n"); - // reset the demo buffer - G_ResetDemoRecording(); - return; - } - - if (demobuf.buffer == NULL) - { - CONS_Alert(CONS_ERROR, "Failed to save Demo. No Demo buffer allocated!\n"); - // reset the demo buffer - G_ResetDemoRecording(); - return; - } - - UINT8 *p = demobuf.buffer+16; // after version - UINT32 length; - - // Ensure extrainfo pointer is always available, even if no info is present. - if (demoinfo_p && *(UINT32 *)demoinfo_p == 0) - { - WRITEUINT8(demobuf.p, DEMOMARKER); // add the demo end marker - *(UINT32 *)demoinfo_p = demobuf.p - demobuf.buffer; - } - WRITEUINT8(demobuf.p, DW_END); // Mark end of demo extra data. - - M_Memcpy(p, demo.titlename, 64); // Write demo title here - p += 64; - - if (multiplayer) - { - // Change the demo's name to be a slug of the title - char demo_slug[128]; - char *writepoint; - size_t i, strindex = 0; - boolean dash = true; - - for (i = 0; demo.titlename[i] && i < 127; i++) - { - if ((demo.titlename[i] >= 'a' && demo.titlename[i] <= 'z') || - (demo.titlename[i] >= '0' && demo.titlename[i] <= '9')) - { - demo_slug[strindex] = demo.titlename[i]; - strindex++; - dash = false; - } - else if (demo.titlename[i] >= 'A' && demo.titlename[i] <= 'Z') - { - demo_slug[strindex] = demo.titlename[i] + 'a' - 'A'; - strindex++; - dash = false; - } - else if (!dash) - { - demo_slug[strindex] = '-'; - strindex++; - dash = true; - } - } - - demo_slug[strindex] = 0; - if (dash) demo_slug[strindex-1] = 0; - - writepoint = strstr(demoname, "-") + 1; - demo_slug[128 - (writepoint - demoname) - 4] = 0; - sprintf(writepoint, "%s.lmp", demo_slug); - } - - length = *(UINT32 *)demoinfo_p; - WRITEUINT32(demoinfo_p, length); -#ifdef NOMD5 - for (UINT8 k = 0; k < 16; k++, p++) - *p = M_RandomByte(); // This MD5 was chosen by fair dice roll and most likely < 50% correct. -#else - // Make a checksum of everything after the checksum in the file up to the end of the standard data. Extrainfo is freely modifiable. - md5_buffer((char *)p+16, (demobuf.buffer + length) - (p+16), p); -#endif - - - if (FIL_WriteFile(va(pandf, srb2home, demoname), demobuf.buffer, demobuf.p - demobuf.buffer)) // finally output the file. - demo.savemode = DSM_SAVED; - - G_ResetDemoRecording(); - - if (modeattacking != ATTACKING_RECORD) - { - if (demo.savemode == DSM_SAVED) - CONS_Printf(M_GetText("Demo %s recorded\n"), demoname); - else - CONS_Alert(CONS_WARNING, M_GetText("Demo %s not saved\n"), demoname); - } -} - -boolean G_DemoTitleResponder(event_t *ev) -{ - INT32 ch; - - if (ev->type != ev_keydown) - return false; - - ch = (INT32)ev->data1; - - // Only ESC and non-keyboard keys abort connection - if (ch == KEY_ESCAPE) - { - demo.savemode = (cv_recordmultiplayerdemos.value == 2) ? DSM_WILLAUTOSAVE : DSM_NOTSAVING; - return true; - } - - if (ch == KEY_ENTER || ch >= KEY_MOUSE1) - { - demo.savemode = DSM_WILLSAVE; - return true; - } - - M_TextInputHandle(&demo.titlenameinput, ch); - - return true; -} - // // G_SetGamestate // diff --git a/src/hardware/hw_batching.h b/src/hardware/hw_batching.h index d1808ce43..1f263d1df 100644 --- a/src/hardware/hw_batching.h +++ b/src/hardware/hw_batching.h @@ -21,8 +21,6 @@ extern "C" { #include "hw_data.h" #include "hw_gl.h" -extern boolean currently_batching; - typedef struct { FSurfaceInfo surf;// surf also has its own polyflags for some reason, but it seems unused diff --git a/src/hardware/hw_main.cpp b/src/hardware/hw_main.cpp index 067fc2b3a..8dc46b0bb 100644 --- a/src/hardware/hw_main.cpp +++ b/src/hardware/hw_main.cpp @@ -132,7 +132,17 @@ static void HWR_AddTransparentFloor(lumpnum_t lumpnum, extrasubsector_t *xsub, b static void HWR_AddTransparentWall(FOutVector *wallVerts, FSurfaceInfo *pSurf, INT32 texnum, boolean noencore, FBITFIELD blend, boolean fogwall, INT32 lightlevel, extracolormap_t *wallcolormap); static void HWR_AddTransparentPolyobjectFloor(lumpnum_t lumpnum, polyobj_t *polysector, boolean isceiling, fixed_t fixedheight, INT32 lightlevel, INT32 alpha, sector_t *FOFSector, FBITFIELD blend, extracolormap_t *planecolormap); -static void HWR_AddSprites(sector_t *sec); +namespace +{ + enum class AddSpritesType + { + kNoLimitDist, + kLimitDist, + }; + + template static void HWR_AddSprites(sector_t *); +} + static void HWR_ProjectSprite(mobj_t *thing); static void HWR_AddPrecipitationSprites(void); static void HWR_ProjectPrecipitationSprite(precipmobj_t *thing); @@ -3166,7 +3176,10 @@ doaddline: { // draw sprites first, coz they are clipped to the solidsegs of // subsectors more 'in front' - HWR_AddSprites(gl_frontsector); + if (cv_drawdist.value || current_bsp_culling_distance) + HWR_AddSprites(gl_frontsector); + else + HWR_AddSprites(gl_frontsector); //Hurdler: at this point validcount must be the same, but is not because // gl_frontsector doesn't point anymore to sub->sector due to @@ -3519,6 +3532,8 @@ static void HWR_DrawSpriteShadow(gl_vissprite_t *spr, patch_t *gpatch, GLPatch_t } } +#define std_R_QuickCamDist(x, y) std::max(abs(((x)>>FRACBITS) - (viewx>>FRACBITS)), abs(((y)>>FRACBITS) - (viewy>>FRACBITS))) + // This is expecting a pointer to an array containing 4 wallVerts for a sprite static void HWR_RotateSpritePolyToAim(gl_vissprite_t *spr, FOutVector *wallVerts, const boolean precip, const boolean papersprite) { @@ -4528,6 +4543,10 @@ static void HWR_DrawSprites(void) // HWR_AddSprites // During BSP traversal, this adds sprites by sector. // -------------------------------------------------------------------------- + +namespace +{ +template static void HWR_AddSprites(sector_t *sec) { mobj_t *thing; @@ -4543,22 +4562,29 @@ static void HWR_AddSprites(sector_t *sec) // Well, now it will be done. sec->validcount = validcount; - limit_dist = cv_drawdist.value; - - if (current_bsp_culling_distance) + if constexpr (Type == AddSpritesType::kLimitDist) { - // Use the smaller setting - if (limit_dist) - limit_dist = std::min(current_bsp_culling_distance/mapobjectscale, limit_dist); - else - limit_dist = current_bsp_culling_distance/mapobjectscale; + limit_dist = cv_drawdist.value; + + if (current_bsp_culling_distance) + { + // Use the smaller setting + if (limit_dist) + limit_dist = std::min(current_bsp_culling_distance/mapobjectscale, limit_dist); + else + limit_dist = current_bsp_culling_distance/mapobjectscale; + } } // Handle all things in sector. for (thing = sec->thinglist; thing; thing = thing->snext) { - if (!R_ThingWithinDist(thing, limit_dist)) - continue; + // reduce branching in this rather tight loop + if constexpr (Type == AddSpritesType::kLimitDist) + { + if (!R_ThingWithinDist(thing, limit_dist)) + continue; + } if (!R_ThingVisible(thing)) continue; @@ -4566,6 +4592,7 @@ static void HWR_AddSprites(sector_t *sec) HWR_ProjectSprite(thing); } } +} // -------------------------------------------------------------------------- // HWR_AddPrecipitationSprites diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c index 497b1b08a..e21219083 100644 --- a/src/hardware/r_opengl/r_opengl.c +++ b/src/hardware/r_opengl/r_opengl.c @@ -2031,8 +2031,7 @@ void GL_SetTexture(GLMipmap_t *pTexInfo) { if (pTexInfo->downloaded != tex_downloaded) { - if (!currently_batching) - pglBindTexture(GL_TEXTURE_2D, pTexInfo->downloaded); + pglBindTexture(GL_TEXTURE_2D, pTexInfo->downloaded); tex_downloaded = pTexInfo->downloaded; } } diff --git a/src/http-mserv.c b/src/http-mserv.c index 4cab52acf..b79c66946 100644 --- a/src/http-mserv.c +++ b/src/http-mserv.c @@ -110,13 +110,21 @@ HMS_on_read (char *s, size_t _1, size_t n, void *userdata) buffer = userdata; - if (n >= (size_t)( buffer->end - buffer->needle )) + while (n >= (size_t)( buffer->end - buffer->needle )) { /* resize to next multiple of buffer size */ blocks = ( n / DEFAULT_BUFFER_SIZE + 1 ); buffer->end += ( blocks * DEFAULT_BUFFER_SIZE ); - buffer->buffer = realloc(buffer->buffer, buffer->end); + void *tmp = realloc(buffer->buffer, buffer->end); + if (tmp == NULL) + { + // not enough memory to read it, bail + free(buffer->buffer); + buffer->buffer = NULL; + return 0; + } + buffer->buffer = tmp; } memcpy(&buffer->buffer[buffer->needle], s, n); diff --git a/src/k_kart.c b/src/k_kart.c index ffbbc5449..5d20a237c 100644 --- a/src/k_kart.c +++ b/src/k_kart.c @@ -551,26 +551,41 @@ void K_RainbowColormap(UINT8 *dest_colormap, UINT8 skincolor) */ void K_GenerateKartColormap(UINT8 *dest_colormap, INT32 skinnum, UINT8 color, boolean local) { - INT32 i; - INT32 starttranscolor; + INT32 i, starttranscolor; // Handle a couple of simple special cases - if (skinnum == TC_BOSS - || skinnum == TC_ALLWHITE - || skinnum == TC_METALSONIC - || skinnum == TC_BLINK - || color == SKINCOLOR_NONE) + if (skinnum < TC_DEFAULT) { - for (i = 0; i < NUM_PALETTE_ENTRIES; i++) + switch (skinnum) { - if (skinnum == TC_ALLWHITE) - dest_colormap[i] = 0; - else if (skinnum == TC_BLINK) - dest_colormap[i] = colortranslations[color][3]; - else - dest_colormap[i] = (UINT8)i; + case TC_ALLWHITE: + memset(dest_colormap, 0, NUM_PALETTE_ENTRIES * sizeof(UINT8)); + return; + case TC_RAINBOW: + if (color >= MAXTRANSLATIONS) + I_Error("Invalid skin color #%hu.", (UINT16)color); + if (color != SKINCOLOR_NONE) + { + K_RainbowColormap(dest_colormap, color); + return; + } + break; + case TC_BLINK: + if (color >= MAXTRANSLATIONS) + I_Error("Invalid skin color #%hu.", (UINT16)color); + if (color != SKINCOLOR_NONE) + { + memset(dest_colormap, colortranslations[color][3], NUM_PALETTE_ENTRIES * sizeof(UINT8)); + return; + } + break; + default: + break; } + for (i = 0; i < NUM_PALETTE_ENTRIES; i++) + dest_colormap[i] = (UINT8)i; + // White! if (skinnum == TC_BOSS) dest_colormap[31] = 0; @@ -579,14 +594,25 @@ void K_GenerateKartColormap(UINT8 *dest_colormap, INT32 skinnum, UINT8 color, bo return; } - else if (skinnum == TC_RAINBOW) + else if (color == SKINCOLOR_NONE) { - K_RainbowColormap(dest_colormap, color); + for (i = 0; i < NUM_PALETTE_ENTRIES; i++) + dest_colormap[i] = (UINT8)i; + return; } + if (color >= MAXTRANSLATIONS) + I_Error("Invalid skin color #%hu.", (UINT16)color); + + if (skinnum < 0 && skinnum > TC_DEFAULT) + I_Error("Invalid translation colormap index %d.", skinnum); + starttranscolor = (skinnum != TC_DEFAULT) ? K_GetSkinArray(local)[skinnum].starttranscolor : DEFAULT_STARTTRANSCOLOR; + if (starttranscolor >= NUM_PALETTE_ENTRIES) + I_Error("Invalid startcolor #%d.", starttranscolor); + // Fill in the entries of the palette that are fixed for (i = 0; i < starttranscolor; i++) dest_colormap[i] = (UINT8)i; @@ -1586,7 +1612,7 @@ static UINT8 K_CheckOffroadCollide(mobj_t *mo) // we will do essentially the same checks as above instead of bothering with top/bottom height of the FOF. // Reminder that an FOF's floor is its bottom, silly! - if ( ((s2->flags & SF_FLIPSPECIAL_FLOOR) && mo->z == cel) // "floor" check + if ( ((s2->flags & SF_FLIPSPECIAL_FLOOR) && mo->z == cel) // "floor" check || ((s2->flags & SF_FLIPSPECIAL_CEILING) && (mo->z + mo->height) == flr) ) // "ceiling" check. for (i = 2; i < 5; i++) // check for sector special @@ -1670,10 +1696,10 @@ static void K_SpawnDashDustRelease(player_t *player) I_Assert(player->mo != NULL); I_Assert(!P_MobjWasRemoved(player->mo)); - if (!P_IsObjectOnGround(player->mo)) + if (!player->speed && !player->kartstuff[k_startboost]) return; - if (!player->speed && !player->kartstuff[k_startboost]) + if (!P_IsObjectOnGround(player->mo)) return; travelangle = player->mo->angle; @@ -1903,11 +1929,11 @@ static void K_PlayGenericCombatSound(mobj_t *source, mobj_t *other, sfxenum_t sf boolean alwaysHear = false; - if (other != NULL && P_MobjWasRemoved(other) == false && other->player != NULL) + if (cv_kartvoices.value && !P_MobjWasRemoved(other) && other->player != NULL) + { alwaysHear = P_IsDisplayPlayer(other->player); - - if (cv_kartvoices.value) S_StartSound(alwaysHear ? NULL : source, skin->soundsid[S_sfx[sfx_id].skinsound]); + } K_RegularVoiceTimers(source->player); } @@ -1929,7 +1955,7 @@ void K_PlayAttackTaunt(mobj_t *source) void K_PlayBoostTaunt(mobj_t *source) { sfxenum_t pick = P_RandomKey(2); // Gotta roll the RNG every time this is called for sync reasons - boolean tasteful = (!source->player || !source->player->kartstuff[k_tauntvoices]); + const boolean tasteful = (!source->player || !source->player->kartstuff[k_tauntvoices]); if (cv_kartvoices.value && (tasteful || cv_kartvoices.value == 2)) S_StartSound(source, sfx_kbost1+pick); @@ -1942,8 +1968,6 @@ void K_PlayBoostTaunt(mobj_t *source) void K_PlayOvertakeSound(mobj_t *source) { - boolean tasteful = (!source->player || !source->player->kartstuff[k_voices]); - if (!G_RaceGametype()) // Only in race return; @@ -1951,6 +1975,8 @@ void K_PlayOvertakeSound(mobj_t *source) if (leveltime < starttime+(10*TICRATE)) return; + const boolean tasteful = (!source->player || !source->player->kartstuff[k_voices]); + if (cv_kartvoices.value && (tasteful || cv_kartvoices.value == 2)) S_StartSound(source, sfx_kslow); @@ -2186,6 +2212,7 @@ fixed_t K_GetKartSpeed(player_t *player, boolean doboostpower) if (doboostpower) return FixedMul(finalspeed, player->kartstuff[k_boostpower]+player->kartstuff[k_speedboost]); + return finalspeed; } @@ -2222,10 +2249,14 @@ fixed_t K_3dKartMovement(player_t *player, boolean onground, fixed_t forwardmove { fixed_t accelmax = 4000; fixed_t newspeed, oldspeed, finalspeed; - fixed_t p_speed = K_GetKartSpeed(player, true); - fixed_t p_accel = K_GetKartAccel(player); + fixed_t p_speed, p_accel; - if (!onground) return 0; // If the player isn't on the ground, there is no change in speed + // If the player isn't on the ground, there is no change in speed + if (!onground) + return 0; + + p_speed = K_GetKartSpeed(player, true); + p_accel = K_GetKartAccel(player); // ACCELCODE!!!1!11! oldspeed = R_PointToDist2(0, 0, player->rmomx, player->rmomy); // FixedMul(P_AproxDistance(player->rmomx, player->rmomy), player->mo->scale); @@ -2329,6 +2360,7 @@ void K_SpinPlayer(player_t *player, mobj_t *source, INT32 type, mobj_t *inflicto if (P_MobjWasRemoved(player->mo)) return; // mobj was removed (in theory that shouldn't happen) + if (shouldForce == 1) force = true; else if (shouldForce == 2) @@ -2469,8 +2501,10 @@ void K_SquishPlayer(player_t *player, mobj_t *source, mobj_t *inflictor) // PS: Inflictor is unused for all purposes here and is actually only ever relevant to Lua. It may be nil too. boolean force = false; // Used to check if Lua ShouldSquish should get us damaged reguardless of flashtics or heck knows what. UINT8 shouldForce = LUA_HookShouldSquish(player, inflictor, source); + if (P_MobjWasRemoved(player->mo)) return; // mobj was removed (in theory that shouldn't happen) + if (shouldForce == 1) force = true; else if (shouldForce == 2) @@ -2583,6 +2617,7 @@ void K_ExplodePlayer(player_t *player, mobj_t *source, mobj_t *inflictor) // A b if (P_MobjWasRemoved(player->mo)) return; // mobj was removed (in theory that shouldn't happen) + if (shouldForce == 1) force = true; else if (shouldForce == 2) @@ -2668,6 +2703,7 @@ void K_ExplodePlayer(player_t *player, mobj_t *source, mobj_t *inflictor) // A b player->powers[pw_flashing] = K_GetKartFlashing(player); upgoer = (18*mapobjectscale*P_MobjFlip(player->mo)); + if (player->mo->eflags & MFE_UNDERWATER) upgoer = (117 * upgoer) / 200; @@ -3603,7 +3639,6 @@ void K_SpawnWipeoutTrail(mobj_t *mo, boolean translucent) void K_DriftDustHandling(mobj_t *spawner) { angle_t anglediff; - const INT16 spawnrange = spawner->radius>>FRACBITS; if (!P_IsObjectOnGround(spawner) || leveltime % 2 != 0) return; @@ -3640,6 +3675,8 @@ void K_DriftDustHandling(mobj_t *spawner) if (anglediff > ANGLE_180) anglediff = InvAngle(anglediff); + const INT16 spawnrange = spawner->radius>>FRACBITS; + if (anglediff > ANG10*4) // Trying to turn further than 40 degrees { fixed_t spawnx = P_RandomRange(-spawnrange, spawnrange)<momx, punter->momy) >> ANGLETOFINESHIFT; - fixed_t z = 30*mapobjectscale + punter->momz; - fixed_t spd; + angle_t fa; + fixed_t z, spd; mobj_t *mine; if (P_MobjWasRemoved(thismine)) @@ -3936,6 +3972,9 @@ void K_PuntMine(mobj_t *thismine, mobj_t *punter) if (P_MobjWasRemoved(mine)) return; + fa = R_PointToAngle2(0, 0, punter->momx, punter->momy) >> ANGLETOFINESHIFT; + z = 30*mapobjectscale + punter->momz; + spd = K_GetProjectileSpeed(); mine->flags |= MF_NOCLIPTHING; @@ -4147,8 +4186,10 @@ static void K_DoShrink(player_t *user) { if (!playeringame[i] || players[i].spectator || !players[i].mo) continue; + if (&players[i] == user) continue; + if (players[i].kartstuff[k_position] < user->kartstuff[k_position]) { //P_FlashPal(&players[i], PAL_NUKE, 10); @@ -5033,6 +5074,7 @@ player_t *K_FindJawzTarget(mobj_t *actor, player_t *source) // Find the angle, see who's got the best. thisang = actor->angle - R_PointToAngle2(actor->x, actor->y, player->mo->x, player->mo->y); + if (thisang > ANGLE_180) thisang = InvAngle(thisang); @@ -5229,10 +5271,11 @@ static void K_UpdateEngineSounds(player_t *player, ticcmd_t *cmd) static void K_UpdateInvincibilitySounds(player_t *player) { INT32 sfxnum = sfx_None; - const boolean localplayer = P_IsLocalPlayer(player); if (player->mo->health > 0) { + const boolean localplayer = P_IsLocalPlayer(player); + if (player->kartstuff[k_growshrinktimer] > 0 && (!localplayer || cv_growmusic.value == 2)) // Prioritize Grow sfxnum = cv_kartinvinsfx.value ? sfx_alarmg : sfx_kgrow; else if (player->kartstuff[k_invincibilitytimer] > 0 && (!localplayer || cv_supermusic.value == 2)) @@ -5772,7 +5815,7 @@ boolean K_CheckPlayersRespawnColliding(INT32 playernum, fixed_t x, fixed_t y) static INT16 K_GetKartDriftValue(player_t *player, fixed_t countersteer) { INT16 basedrift, driftangle; - fixed_t driftweight = player->kartweight*14; // 12 + fixed_t driftweight; // If they aren't drifting or on the ground this doesn't apply if (player->kartstuff[k_drift] == 0 || !P_IsObjectOnGround(player->mo)) @@ -5783,6 +5826,8 @@ static INT16 K_GetKartDriftValue(player_t *player, fixed_t countersteer) return -266*player->kartstuff[k_drift]; // Drift has ended and we are tweaking their angle back a bit } + driftweight = player->kartweight*14; // 12 + //basedrift = 90*player->kartstuff[k_drift]; // 450 //basedrift = 93*player->kartstuff[k_drift] - driftweight*3*player->kartstuff[k_drift]/10; // 447 - 303 basedrift = 83*player->kartstuff[k_drift] - (driftweight - 14)*player->kartstuff[k_drift]/5; // 415 - 303 @@ -5793,6 +5838,9 @@ static INT16 K_GetKartDriftValue(player_t *player, fixed_t countersteer) INT16 K_GetKartTurnValue(player_t *player, INT16 turnvalue) { + fixed_t p_topspeed, p_curspeed, p_maxspeed; + fixed_t adjustangle; + if (player->spectator) return turnvalue; @@ -5811,10 +5859,10 @@ INT16 K_GetKartTurnValue(player_t *player, INT16 turnvalue) return turnvalue; } - fixed_t p_topspeed = K_GetKartSpeed(player, false); - fixed_t p_curspeed = min(player->speed, p_topspeed * 2); - fixed_t p_maxspeed = p_topspeed * 3; - fixed_t adjustangle = FixedDiv((p_maxspeed>>16) - (p_curspeed>>16), (p_maxspeed>>16) + player->kartweight); + p_topspeed = K_GetKartSpeed(player, false); + p_curspeed = min(player->speed, p_topspeed * 2); + p_maxspeed = p_topspeed * 3; + adjustangle = FixedDiv((p_maxspeed>>16) - (p_curspeed>>16), (p_maxspeed>>16) + player->kartweight); turnvalue = FixedMul(turnvalue, adjustangle); // Weight has a small effect on turning @@ -6216,7 +6264,7 @@ void K_MoveKartPlayer(player_t *player, boolean onground) K_KartUpdatePosition(player); - if (!player->exiting) + if (LIKELY(!player->exiting)) { if (player->kartstuff[k_oldposition] < player->kartstuff[k_position]) // But first, if you lost a place, { @@ -6796,10 +6844,9 @@ void K_MoveKartPlayer(player_t *player, boolean onground) K_KartDrift(player, onground); - if ((!player->kartstuff[k_aizdriftstrat]) || (!P_IsObjectOnGround(player->mo)) || (player->kartstuff[k_drift])) + if (player->sliproll && ((!player->kartstuff[k_aizdriftstrat]) || (!P_IsObjectOnGround(player->mo)) || (player->kartstuff[k_drift]))) { - if (player->sliproll && (player->sliproll > 0)) - player->sliproll -= (4*ANG1); + player->sliproll -= (4*ANG1); } // funneh Saturn sprite stuff diff --git a/src/m_menudefs.c b/src/m_menudefs.c index 87e766481..a5f617a07 100644 --- a/src/m_menudefs.c +++ b/src/m_menudefs.c @@ -574,10 +574,10 @@ static menuitem_t MP_PlayerSetupMenu[] = #ifndef NONET static menuitem_t MP_ConnectMenu[] = { - {IT_STRING | IT_KEYHANDLER, NULL, "", M_HandleServerSearch,0}, - {IT_STRING | IT_CVAR, NULL, "Sort By", &cv_serversort, 16}, - {IT_STRING | IT_KEYHANDLER, NULL, "Page", M_HandleServerPage, 24}, - {IT_STRING | IT_CALL, NULL, "Refresh", M_Refresh, 32}, + {IT_STRING | IT_CVAR, NULL, "Sort By", &cv_serversort, 0}, + {IT_STRING | IT_KEYHANDLER, NULL, "Page", M_HandleServerPage, 8}, + {IT_STRING | IT_CALL, NULL, "Refresh", M_Refresh, 16}, + {IT_STRING | IT_KEYHANDLER, NULL, "", M_HandleServerSearch,25}, {IT_STRING | IT_SPACE, NULL, "", M_Connect, 48}, {IT_STRING | IT_SPACE, NULL, "", M_Connect, 60}, @@ -593,10 +593,10 @@ static menuitem_t MP_ConnectMenu[] = enum { - mp_connect_search, mp_connect_sort, mp_connect_page, mp_connect_refresh, + mp_connect_search, FIRSTSERVERLINE }; #endif diff --git a/src/m_misc.c b/src/m_misc.c index df860c92e..dace68e72 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -2294,6 +2294,51 @@ void M_MkdirEach(const char *path, int start, int mode) M_MkdirEachUntil(path, start, -1, mode); } +// Hashes some message using FNV-1a +#define FNV1A_OFFSET_BASIS 0x811C9DC5 +#define FNV1A_PRIME 0x01000193 + +UINT32 FNV1a_Hash(const char *message, size_t size) +{ + UINT32 hash = FNV1A_OFFSET_BASIS; + + for (size_t i = 0; i < size; i++) + { + hash ^= message[i]; + hash *= FNV1A_PRIME; + } + + return hash; +} + +UINT32 FNV1a_HashString(const char *message) +{ + UINT32 hash = FNV1A_OFFSET_BASIS; + + while (*message) + { + hash ^= *message; + hash *= FNV1A_PRIME; + message++; + } + + return hash; +} + +UINT32 FNV1a_HashLowercaseString(const char *message) +{ + UINT32 hash = FNV1A_OFFSET_BASIS; + + while (*message) + { + hash ^= tolower(*message); + hash *= FNV1A_PRIME; + message++; + } + + return hash; +} + #ifdef __cplusplus } // extern "C" diff --git a/src/m_misc.h b/src/m_misc.h index 0939a2747..2cdad977a 100644 --- a/src/m_misc.h +++ b/src/m_misc.h @@ -124,6 +124,11 @@ const char * M_Ftrim (double); // counting bits, for weapon ammo code, usually FUNCMATH UINT8 M_CountBits(UINT32 num, UINT8 size); +// Hashes some message using FNV-1a +UINT32 FNV1a_Hash(const char *message, size_t size); +UINT32 FNV1a_HashString(const char *message); +UINT32 FNV1a_HashLowercaseString(const char *message); + #include "w_wad.h" extern char configfile[MAX_WADPATH]; diff --git a/src/p_enemy.c b/src/p_enemy.c index 7d3e04f94..283773c9e 100644 --- a/src/p_enemy.c +++ b/src/p_enemy.c @@ -250,7 +250,7 @@ boolean P_Move(mobj_t *actor, fixed_t speed) I_Assert(movedir < NUMDIRS); tryx = actor->x + FixedMul(speed*xspeed[movedir], actor->scale); - if (twodlevel || actor->flags2 & MF2_TWOD) + if (UNLIKELY(twodlevel || actor->flags2 & MF2_TWOD)) tryy = actor->y; else tryy = actor->y + FixedMul(speed*yspeed[movedir], actor->scale); @@ -4387,7 +4387,7 @@ void A_MouseThink(void *thing) || (actor->eflags & MFE_VERTICALFLIP && actor->z + actor->height == actor->ceilingz)) && !actor->reactiontime) { - if (twodlevel || actor->flags2 & MF2_TWOD) + if (UNLIKELY(twodlevel || actor->flags2 & MF2_TWOD)) { if (P_RandomChance(FRACUNIT/2)) actor->angle += ANGLE_180; diff --git a/src/p_inter.c b/src/p_inter.c index b559c6cc1..5412d3eec 100644 --- a/src/p_inter.c +++ b/src/p_inter.c @@ -587,7 +587,7 @@ void P_TouchSpecialThing(mobj_t *special, mobj_t *toucher, boolean heightcheck) special->momx = special->momy = special->momz = 0; P_GivePlayerRings(player, 1); - if ((maptol & TOL_NIGHTS) && special->type != MT_FLINGRING) + if (UNLIKELY((maptol & TOL_NIGHTS) && special->type != MT_FLINGRING)) P_DoNightsScore(player); break; @@ -599,7 +599,7 @@ void P_TouchSpecialThing(mobj_t *special, mobj_t *toucher, boolean heightcheck) special->momx = special->momy = 0; P_GivePlayerRings(player, 1); - if ((maptol & TOL_NIGHTS) && special->type != MT_FLINGCOIN) + if (UNLIKELY((maptol & TOL_NIGHTS) && special->type != MT_FLINGCOIN)) P_DoNightsScore(player); break; case MT_BLUEBALL: @@ -615,7 +615,7 @@ void P_TouchSpecialThing(mobj_t *special, mobj_t *toucher, boolean heightcheck) else special->scalespeed = 4*FRACUNIT/5; - if (maptol & TOL_NIGHTS) + if (UNLIKELY(maptol & TOL_NIGHTS)) P_DoNightsScore(player); break; case MT_AUTOPICKUP: @@ -832,6 +832,7 @@ void P_TouchSpecialThing(mobj_t *special, mobj_t *toucher, boolean heightcheck) if (player->bumpertime < TICRATE/4) { S_StartSound(toucher, special->info->seesound); + if (player->pflags & PF_NIGHTSMODE) { player->bumpertime = TICRATE/2; @@ -1226,9 +1227,9 @@ void P_TouchSpecialThing(mobj_t *special, mobj_t *toucher, boolean heightcheck) return; // SRB2kart - don't need bubbles mucking with the player if ((player->powers[pw_shield] & SH_NOSTACK) == SH_ELEMENTAL) return; - if (maptol & TOL_NIGHTS) + if (UNLIKELY(maptol & TOL_NIGHTS)) return; - if (mariomode) + if (UNLIKELY(mariomode)) return; else if (toucher->eflags & MFE_VERTICALFLIP) { @@ -2352,11 +2353,11 @@ boolean P_DamageMobj(mobj_t *target, mobj_t *inflictor, mobj_t *source, INT32 da if (player->exiting) return false; - if (!(target->player->pflags & (PF_NIGHTSMODE|PF_NIGHTSFALL)) && (maptol & TOL_NIGHTS)) + if (UNLIKELY(!(target->player->pflags & (PF_NIGHTSMODE|PF_NIGHTSFALL)) && (maptol & TOL_NIGHTS))) return false; } - if (player->pflags & PF_NIGHTSMODE) // NiGHTS damage handling + if (UNLIKELY(player->pflags & PF_NIGHTSMODE)) // NiGHTS damage handling { if (!force) { @@ -2601,7 +2602,7 @@ void P_PlayerRingBurst(player_t *player, INT32 num_rings) ns = FixedMul(((i*FRACUNIT)/16)+2*FRACUNIT, mo->scale); mo->momx = FixedMul(FINECOSINE(fa),ns); - if (!(twodlevel || (player->mo->flags2 & MF2_TWOD))) + if (LIKELY(!(twodlevel || (player->mo->flags2 & MF2_TWOD)))) mo->momy = FixedMul(FINESINE(fa),ns); P_SetObjectMomZ(mo, 8*FRACUNIT, false); @@ -2625,7 +2626,7 @@ void P_PlayerRingBurst(player_t *player, INT32 num_rings) ns = FixedMul(momxy, mo->scale); mo->momx = FixedMul(FINECOSINE(fa),ns); - if (!(twodlevel || (player->mo->flags2 & MF2_TWOD))) + if (LIKELY(!(twodlevel || (player->mo->flags2 & MF2_TWOD)))) mo->momy = FixedMul(FINESINE(fa),ns); ns = momz; @@ -2732,7 +2733,7 @@ void P_PlayerWeaponPanelBurst(player_t *player) ns = FixedMul(3*FRACUNIT, mo->scale); mo->momx = FixedMul(FINECOSINE(fa),ns); - if (!(twodlevel || (player->mo->flags2 & MF2_TWOD))) + if (LIKELY(!(twodlevel || (player->mo->flags2 & MF2_TWOD)))) mo->momy = FixedMul(FINESINE(fa),ns); P_SetObjectMomZ(mo, 4*FRACUNIT, false); @@ -2816,7 +2817,7 @@ void P_PlayerWeaponAmmoBurst(player_t *player) ns = FixedMul(2*FRACUNIT, mo->scale); mo->momx = FixedMul(FINECOSINE(fa), ns); - if (!(twodlevel || (player->mo->flags2 & MF2_TWOD))) + if (LIKELY(!(twodlevel || (player->mo->flags2 & MF2_TWOD)))) mo->momy = FixedMul(FINESINE(fa),ns); P_SetObjectMomZ(mo, 3*FRACUNIT, false); @@ -2932,7 +2933,7 @@ void P_PlayerEmeraldBurst(player_t *player, boolean toss) momx = FixedMul(FINECOSINE(fa), ns); - if (!(twodlevel || (player->mo->flags2 & MF2_TWOD))) + if (LIKELY(!(twodlevel || (player->mo->flags2 & MF2_TWOD)))) momy = FixedMul(FINESINE(fa),ns); else momy = 0; @@ -2989,7 +2990,7 @@ void P_PlayerFlagBurst(player_t *player, boolean toss) { angle_t fa = P_RandomByte()*FINEANGLES/256; flag->momx = FixedMul(FINECOSINE(fa), FixedMul(6*FRACUNIT, player->mo->scale)); - if (!(twodlevel || (player->mo->flags2 & MF2_TWOD))) + if (LIKELY(!(twodlevel || (player->mo->flags2 & MF2_TWOD)))) flag->momy = FixedMul(FINESINE(fa), FixedMul(6*FRACUNIT, player->mo->scale)); } diff --git a/src/p_map.c b/src/p_map.c index 3dc6a8498..1002a39d5 100644 --- a/src/p_map.c +++ b/src/p_map.c @@ -140,7 +140,7 @@ boolean P_DoSpring(mobj_t *spring, mobj_t *object) if (object->player && object->player->spectator) return false; - if (object->player && (object->player->pflags & PF_NIGHTSMODE)) + if (UNLIKELY(object->player && (object->player->pflags & PF_NIGHTSMODE))) { /*Someone want to make these work like bumpers?*/ return false; @@ -335,7 +335,7 @@ static boolean PIT_CheckThing(mobj_t *thing) #ifdef SEENAMES // Do name checks all the way up here // So that NOTHING ELSE can see MT_NAMECHECK because it is client-side. - if (tmthing->type == MT_NAMECHECK) + if (UNLIKELY(tmthing->type == MT_NAMECHECK)) { // Ignore things that aren't players, ignore spectators, ignore yourself. // (also don't bother to check that tmthing->target->player is non-NULL because we're not actually using it here.) @@ -361,8 +361,8 @@ static boolean PIT_CheckThing(mobj_t *thing) return true; // Don't collide with your buddies while NiGHTS-flying. - if (tmthing->player && thing->player && (maptol & TOL_NIGHTS) - && ((tmthing->player->pflags & PF_NIGHTSMODE) || (thing->player->pflags & PF_NIGHTSMODE))) + if (UNLIKELY(tmthing->player && thing->player && (maptol & TOL_NIGHTS) + && ((tmthing->player->pflags & PF_NIGHTSMODE) || (thing->player->pflags & PF_NIGHTSMODE)))) return true; blockdist = thing->radius + tmthing->radius; @@ -3183,7 +3183,7 @@ stairstep: P_HitSlideLine(bestslideline); // clip the moves - if ((twodlevel || (mo->flags2 & MF2_TWOD)) && mo->player) + if (UNLIKELY((twodlevel || (mo->flags2 & MF2_TWOD)) && mo->player)) { mo->momx = tmxmove; tmymove = 0; diff --git a/src/p_mobj.c b/src/p_mobj.c index d6c04b2de..dca31e623 100644 --- a/src/p_mobj.c +++ b/src/p_mobj.c @@ -371,7 +371,7 @@ static boolean P_SetPrecipMobjState(precipmobj_t *mobj, statenum_t state) // boolean P_WeaponOrPanel(mobjtype_t type) { - if (type >= MT_BOUNCERING && type <= MT_GRENADEPICKUP) + if (UNLIKELY(type >= MT_BOUNCERING && type <= MT_GRENADEPICKUP)) // doubt those get used at all in kart tbh return true; return false; @@ -461,6 +461,7 @@ void P_ExplodeMissile(mobj_t *mo) boolean P_InsideANonSolidFFloor(mobj_t *mobj, ffloor_t *rover) { fixed_t topheight, bottomheight; + if (!(rover->flags & FF_EXISTS)) return false; @@ -891,7 +892,7 @@ static void P_PlayerFlip(mobj_t *mo) G_GhostAddFlip((INT32) (mo->player - players)); // Flip aiming to match! - if (mo->player->pflags & PF_NIGHTSMODE) // NiGHTS doesn't use flipcam + if (UNLIKELY(mo->player->pflags & PF_NIGHTSMODE)) // NiGHTS doesn't use flipcam { if (mo->tracer) mo->tracer->eflags ^= MFE_VERTICALFLIP; @@ -989,7 +990,7 @@ fixed_t P_GetMobjGravity(mobj_t *mo) //|| (mo->player->charability == CA_FLY && (mo->player->powers[pw_tailsfly] // || (mo->state >= &states[S_PLAY_SPC1] && mo->state <= &states[S_PLAY_SPC4])))) // gravityadd = gravityadd/3; // less gravity while flying/gliding - if (mo->player->climbing || (mo->player->pflags & PF_NIGHTSMODE)) + if (UNLIKELY(mo->player->climbing || (mo->player->pflags & PF_NIGHTSMODE))) return 0; if (!(mo->flags2 & MF2_OBJECTFLIP) != !(mo->player->powers[pw_gravityboots])) // negated to turn numeric into bool - would be double negated, but not needed if both would be @@ -1607,7 +1608,7 @@ void P_XYMovement(mobj_t *mo) // Check the gravity status. P_CheckGravity(mo, false); - if (player && !moved && player->pflags & PF_NIGHTSMODE && mo->target) + if (UNLIKELY(player && !moved && player->pflags & PF_NIGHTSMODE && mo->target)) { angle_t fa; @@ -1652,7 +1653,7 @@ void P_XYMovement(mobj_t *mo) if (player && player->homing) // no friction for homing return; - if (player && player->pflags & PF_NIGHTSMODE) + if (UNLIKELY(player && player->pflags & PF_NIGHTSMODE)) return; // no friction for NiGHTS players if ((mo->type == MT_BIGTUMBLEWEED || mo->type == MT_LITTLETUMBLEWEED) @@ -1735,9 +1736,6 @@ static void P_AdjustMobjFloorZ_FFloors(mobj_t *mo, sector_t *sector, UINT8 motyp if (!(rover->flags & FF_EXISTS)) continue; - topheight = P_GetFOFTopZ(mo, sector, rover, mo->x, mo->y, NULL); - bottomheight = P_GetFOFBottomZ(mo, sector, rover, mo->x, mo->y, NULL); - if (mo->player && P_CheckSolidLava(mo, rover)) // only the player should be affected ; else if (motype != 0 && rover->flags & FF_SWIMMABLE) // "scenery" only @@ -1747,6 +1745,10 @@ static void P_AdjustMobjFloorZ_FFloors(mobj_t *mo, sector_t *sector, UINT8 motyp else if (!((rover->flags & FF_BLOCKPLAYER && mo->player) // solid to players? || (rover->flags & FF_BLOCKOTHERS && !mo->player))) // solid to others? continue; + + topheight = P_GetFOFTopZ(mo, sector, rover, mo->x, mo->y, NULL); + bottomheight = P_GetFOFBottomZ(mo, sector, rover, mo->x, mo->y, NULL); + if (rover->flags & FF_QUICKSAND) { switch (motype) @@ -1770,6 +1772,7 @@ static void P_AdjustMobjFloorZ_FFloors(mobj_t *mo, sector_t *sector, UINT8 motyp delta1 = mo->z - (bottomheight + ((topheight - bottomheight)/2)); delta2 = thingtop - (bottomheight + ((topheight - bottomheight)/2)); + if (topheight > mo->floorz && abs(delta1) < abs(delta2) && !(rover->flags & FF_REVERSEPLATFORM) && ((P_MobjFlip(mo)*mo->momz >= 0) || (!(rover->flags & FF_PLATFORM)))) // In reverse gravity, only clip for FOFs that are intangible from their bottom (the "top" you're falling through) if you're coming from above ("below" in your frame of reference) @@ -1803,7 +1806,7 @@ static void P_AdjustMobjFloorZ_PolyObjs(mobj_t *mo, subsector_t *subsec) thingtop = mo->z + mo->height; - while(po) + while (po) { if (!P_MobjInsidePolyobj(po, mo) || !(po->flags & POF_SOLID)) { @@ -1855,6 +1858,7 @@ static void P_RingZMovement(mobj_t *mo) mo->momz += mo->pmomz; mo->eflags &= ~MFE_APPLYPMOMZ; } + mo->z += mo->momz; // clip movement @@ -1925,6 +1929,7 @@ static boolean P_ZMovement(mobj_t *mo) mo->momz += mo->pmomz; mo->eflags &= ~MFE_APPLYPMOMZ; } + mo->z += mo->momz; if (mo->standingslope) @@ -2209,7 +2214,7 @@ static boolean P_ZMovement(mobj_t *mo) || mo->type == MT_CANNONBALLDECOR || mo->type == MT_FALLINGROCK) { - if (maptol & TOL_NIGHTS) + if (UNLIKELY(maptol & TOL_NIGHTS)) mom.z = -FixedDiv(mom.z, 10*FRACUNIT); else mom.z = -FixedMul(mom.z, FixedDiv(17*FRACUNIT,20*FRACUNIT)); @@ -2349,9 +2354,9 @@ static boolean P_ZMovement(mobj_t *mo) mo->momz = -mo->momz; else // Flags bounce - if (mo->type == MT_REDFLAG || mo->type == MT_BLUEFLAG) + if (UNLIKELY(mo->type == MT_REDFLAG || mo->type == MT_BLUEFLAG)) { - if (maptol & TOL_NIGHTS) + if (UNLIKELY(maptol & TOL_NIGHTS)) mo->momz = -FixedDiv(mo->momz, 10*FRACUNIT); else mo->momz = -FixedMul(mo->momz, FixedDiv(17*FRACUNIT,20*FRACUNIT)); @@ -2418,7 +2423,7 @@ static void P_PlayerZMovement(mobj_t *mo) else mo->z = mo->floorz; - if (mo->player->pflags & PF_NIGHTSMODE) + if (UNLIKELY(mo->player->pflags & PF_NIGHTSMODE)) { // bounce off floor if you were flying towards it if ((mo->eflags & MFE_VERTICALFLIP && mo->player->flyangle > 0 && mo->player->flyangle < 180) @@ -2608,7 +2613,7 @@ nightsdone: else mo->z = mo->ceilingz - mo->height; - if (mo->player->pflags & PF_NIGHTSMODE) + if (UNLIKELY(mo->player->pflags & PF_NIGHTSMODE)) { // bounce off ceiling if you were flying towards it if ((mo->eflags & MFE_VERTICALFLIP && mo->player->flyangle > 180 && mo->player->flyangle <= 359) @@ -2653,7 +2658,7 @@ nightsdone: } // hit the ceiling - if (mariomode) + if (UNLIKELY(mariomode)) S_StartSound(mo, sfx_mario1); if (!mo->player->climbing) @@ -3114,8 +3119,6 @@ static boolean P_CameraCheckHeat(camera_t *thiscam) if (!thiscam || !thiscam->subsector) return false; - halfheight = thiscam->z + (thiscam->height >> 1); - // see if we are in water sector = thiscam->subsector->sector; @@ -3126,6 +3129,8 @@ static boolean P_CameraCheckHeat(camera_t *thiscam) { ffloor_t *rover; + halfheight = thiscam->z + (thiscam->height >> 1); + for (rover = sector->ffloors; rover; rover = rover->next) { if (!(rover->flags & FF_EXISTS)) @@ -3152,8 +3157,6 @@ static boolean P_CameraCheckWater(camera_t *thiscam) if (!thiscam || !thiscam->subsector) return false; - halfheight = thiscam->z + (thiscam->height >> 1); - // see if we are in water sector = thiscam->subsector->sector; @@ -3161,6 +3164,8 @@ static boolean P_CameraCheckWater(camera_t *thiscam) { ffloor_t *rover; + halfheight = thiscam->z + (thiscam->height >> 1); + for (rover = sector->ffloors; rover; rover = rover->next) { if (!(rover->flags & FF_EXISTS) || !(rover->flags & FF_SWIMMABLE) || rover->flags & FF_BLOCKOTHERS) @@ -3489,15 +3494,18 @@ static void P_PlayerMobjThinker(mobj_t *mobj) } else { - if (!(mobj->player->pflags & PF_NIGHTSMODE)) // "jumping" is used for drilling + if (LIKELY(!(mobj->player->pflags & PF_NIGHTSMODE))) // "jumping" is used for drilling mobj->player->jumping = 0; + mobj->player->pflags &= ~PF_JUMPED; + if (mobj->player->secondjump || mobj->player->powers[pw_tailsfly]) { mobj->player->secondjump = 0; mobj->player->powers[pw_tailsfly] = 0; P_SetPlayerMobjState(mobj, S_KART_WALK1); // SRB2kart - was S_PLAY_RUN1 } + mobj->eflags &= ~MFE_JUSTHITFLOOR; } @@ -5403,8 +5411,6 @@ static void P_GimmeAxisXYPos(mobj_t *closestaxis, degenmobj_t *mobj) static void P_MoveHoop(mobj_t *mobj) { - const fixed_t fuse = (mobj->fuse*mobj->extravalue2); - const angle_t fa = mobj->movedir*(FINEANGLES/mobj->extravalue1); TVector v; TVector *res; fixed_t finalx, finaly, finalz; @@ -5414,6 +5420,9 @@ static void P_MoveHoop(mobj_t *mobj) if (!mobj->target) /// \todo DEBUG ME! Target was P_RemoveMobj'd at some point, and therefore no longer valid! return; + const fixed_t fuse = (mobj->fuse*mobj->extravalue2); + const angle_t fa = mobj->movedir*(FINEANGLES/mobj->extravalue1); + x = mobj->target->x; y = mobj->target->y; z = mobj->target->z+mobj->target->height/2; @@ -8967,7 +8976,7 @@ void P_MobjThinker(mobj_t *mobj) I_Assert(mobj != NULL); I_Assert(!P_MobjWasRemoved(mobj)); - if (mobj->flags & MF_NOTHINK) + if (UNLIKELY(mobj->flags & MF_NOTHINK)) return; // Remove dead target/tracer. @@ -8986,11 +8995,12 @@ void P_MobjThinker(mobj_t *mobj) tmfloorthing = tmhitthing = NULL; + const sector_t *sec1 = mobj->subsector ? mobj->subsector->sector : NULL; + // 970 allows ANY mobj to trigger a linedef exec if (UNLIKELY(!mobj->islocal && sec1 && GETSECSPECIAL(sec1->special, 2) == 8)) // BEWARE: islocal does not exist in vanilla { sector_t *sec2; - sec2 = P_ThingOnSpecial3DFloor(mobj); if (sec2 && GETSECSPECIAL(sec2->special, 2) == 1) P_LinedefExecute(sec2->tag, mobj, sec2); @@ -9104,7 +9114,7 @@ void P_MobjThinker(mobj_t *mobj) || mobj->type == MT_FLINGEMERALD || mobj->type == MT_BIGTUMBLEWEED || mobj->type == MT_LITTLETUMBLEWEED - || mobj->type == MT_CANNONBALLDECOR + || mobj->type == MT_CANNONBALLDECOR) || mobj->type == MT_FALLINGROCK) { P_TryMove(mobj, mobj->x, mobj->y, true); // Sets mo->standingslope correctly @@ -9195,6 +9205,9 @@ void P_PushableThinker(mobj_t *mobj) I_Assert(mobj != NULL); I_Assert(!P_MobjWasRemoved(mobj)); + if (!mobj) + return; + sec = mobj->subsector->sector; if (GETSECSPECIAL(sec->special, 2) == 1 && mobj->z == sec->floorheight) @@ -9211,9 +9224,6 @@ void P_PushableThinker(mobj_t *mobj) if (mobj->flags & MF_PUSHABLE && !(mobj->momx || mobj->momy)) P_TryMove(mobj, mobj->x, mobj->y, true); - if (!mobj) - return; - if (mobj->fuse == 1) // it would explode in the MobjThinker code { mobj_t *spawnmo; @@ -9982,6 +9992,7 @@ static precipmobj_t *P_SpawnPrecipMobj(fixed_t x, fixed_t y, fixed_t z, mobjtype else { const INT32 special = GETSECSPECIAL(mobj->subsector->sector->special, 1); + if (special == 7 || special == 6 || mobj->subsector->sector->floorpic == skyflatnum) mobj->precipflags |= PCF_PIT; } @@ -10313,9 +10324,11 @@ void P_PrecipitationEffects(void) P_SpawnLightningFlash(ss); // Spawn a quick flash thinker } + const mobj_t *pmo = players[displayplayers[0]].mo; + // Local effects from here on out! // If we're not in game fully yet, we don't worry about them. - if (!playeringame[displayplayers[0]] || !players[displayplayers[0]].mo) + if (!playeringame[displayplayers[0]] || !pmo) return; if (sound_disabled) @@ -10324,7 +10337,7 @@ void P_PrecipitationEffects(void) if (!sounds_rain && !sounds_thunder) return; // no need to calculate volume at ALL - if (players[displayplayers[0]].mo->subsector->sector->ceilingpic == skyflatnum) + if (pmo->subsector->sector->ceilingpic == skyflatnum) volume = 255; // Sky above? We get it full blast. else { @@ -10335,7 +10348,7 @@ void P_PrecipitationEffects(void) // Essentially check in a 1024 unit radius of the player for an outdoor area. #define RADIUSSTEP (64*FRACUNIT) #define SEARCHRADIUS (16*RADIUSSTEP) - yl = yh = players[displayplayers[0]].mo->y; + yl = yh = pmo->y; yl -= SEARCHRADIUS; while (yl < INT32_MIN) yl += RADIUSSTEP; @@ -10343,7 +10356,7 @@ void P_PrecipitationEffects(void) while (yh > INT32_MAX) yh -= RADIUSSTEP; - xl = xh = players[displayplayers[0]].mo->x; + xl = xh = pmo->x; xl -= SEARCHRADIUS; while (xl < INT32_MIN) xl += RADIUSSTEP; @@ -10359,20 +10372,19 @@ void P_PrecipitationEffects(void) if (R_PointInSubsectorFast((fixed_t)x, (fixed_t)y)->sector->ceilingpic != skyflatnum) // Found the outdoors! continue; - newdist = S_CalculateSoundDistance(players[displayplayers[0]].mo->x, players[displayplayers[0]].mo->y, 0, (fixed_t)x, (fixed_t)y, 0); + newdist = S_CalculateSoundDistance(pmo->x, pmo->y, 0, (fixed_t)x, (fixed_t)y, 0); if (newdist < closedist) closedist = newdist; } volume = 255 - (closedist>>(FRACBITS+2)); + volume = CLAMP(volume, 0, 255); } #undef RADIUSSTEP - volume = CLAMP(volume, 0, 255); - if (sounds_rain) - S_StartSoundAtVolume(players[displayplayers[0]].mo, sfx_rainin, volume); + S_StartSoundAtVolume(pmo, sfx_rainin, volume); if (!sounds_thunder) return; @@ -10380,7 +10392,7 @@ void P_PrecipitationEffects(void) if (effects_lightning && lightningStrike && volume) { // Large, close thunder sounds to go with our lightning. - S_StartSoundAtVolume(players[displayplayers[0]].mo, sfx_litng1 + M_RandomKey(4), volume); + S_StartSoundAtVolume(pmo, sfx_litng1 + M_RandomKey(4), volume); } else if (thunderchance < 20) { @@ -10388,7 +10400,7 @@ void P_PrecipitationEffects(void) if (volume < 80) volume = 80; - S_StartSoundAtVolume(players[displayplayers[0]].mo, sfx_athun1 + M_RandomKey(2), volume); + S_StartSoundAtVolume(pmo, sfx_athun1 + M_RandomKey(2), volume); } } diff --git a/src/p_setup.c b/src/p_setup.c index c5bd01b4d..4c311024e 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -2573,6 +2573,7 @@ static void P_InitMinimapInfo(void) lumpnum_t lumpnum; fixed_t a; fixed_t b; + node_t *bsp = &nodes[numnodes-1]; minimapinfo.minimap_pic = NULL; @@ -3353,7 +3354,8 @@ UINT16 P_PartialAddWadFile(const char *wadfilename, boolean local) // Only exists to make sure there's no way to overwrite partadd_stage externally // unless you really push yourself. -SINT8 P_PartialAddGetStage(void) { +SINT8 P_PartialAddGetStage(void) +{ return partadd_stage; } diff --git a/src/p_spec.c b/src/p_spec.c index 9b0fc932e..a7a7f89f3 100644 --- a/src/p_spec.c +++ b/src/p_spec.c @@ -3913,7 +3913,7 @@ DoneSection2: player->kartstuff[k_lapanimation] = 80; - if (player->pflags & PF_NIGHTSMODE) + if (UNLIKELY(player->pflags & PF_NIGHTSMODE)) player->drillmeter += 48*20; if (netgame && player->laps >= (UINT8)cv_numlaps.value) @@ -7103,7 +7103,7 @@ static inline boolean PIT_PushThing(mobj_t *thing) return false; // Allow this to affect pushable objects at some point? - if (thing->player && (!(thing->flags & (MF_NOGRAVITY | MF_NOCLIP)) || thing->player->pflags & PF_NIGHTSMODE)) + if (thing->player && (!(thing->flags & (MF_NOGRAVITY | MF_NOCLIP)) || UNLIKELY(thing->player->pflags & PF_NIGHTSMODE))) { INT32 dist; INT32 speed; @@ -7134,7 +7134,7 @@ static inline boolean PIT_PushThing(mobj_t *thing) // Written with bits and pieces of P_HomingAttack if ((speed > 0) && (P_CheckSight(thing, tmpusher->source))) { - if (!(thing->player->pflags & PF_NIGHTSMODE)) + if (LIKELY(!(thing->player->pflags & PF_NIGHTSMODE))) { // only push wrt Z if health & 1 (mapthing has ambush flag) if (tmpusher->source->health & 1) diff --git a/src/p_tick.c b/src/p_tick.c index 8cf354c8a..67bb184fa 100644 --- a/src/p_tick.c +++ b/src/p_tick.c @@ -190,7 +190,7 @@ void Command_CountMobjs_f(void) void P_InitThinkers(void) { thinkercap.prev = thinkercap.next = &thinkercap; - precipcap.prev = precipcap.next = &precipcap; + precipcap.prev = precipcap.next = &precipcap; waypointcap = NULL; } @@ -735,8 +735,10 @@ void P_Ticker(boolean run) PS_START_TIMING(ps_playerthink_time); for (i = 0; i < MAXPLAYERS; i++) { - if (playeringame[i] && !P_MobjWasRemoved(players[i].mo)) - P_PlayerThink(&players[i]); + if (!playeringame[i] || P_MobjWasRemoved(players[i].mo)) + continue; + + P_PlayerThink(&players[i]); } PS_STOP_TIMING(ps_playerthink_time); } @@ -757,8 +759,10 @@ void P_Ticker(boolean run) // Run any "after all the other thinkers" stuff for (i = 0; i < MAXPLAYERS; i++) { - if (playeringame[i] && !P_MobjWasRemoved(players[i].mo)) - P_PlayerAfterThink(&players[i]); + if (!playeringame[i] || P_MobjWasRemoved(players[i].mo)) + continue; + + P_PlayerAfterThink(&players[i]); } // Apply rumble to local players @@ -837,12 +841,12 @@ void P_Ticker(boolean run) if (demo.recording) { - INT32 axis = JoyAxis(AXISLOOKBACK, 1); - G_WriteAllGhostTics(); if (cv_recordmultiplayerdemos.value) { + const INT32 axis = JoyAxis(AXISLOOKBACK, 1); + if (demo.savemode == DSM_NOTSAVING || demo.savemode == DSM_WILLAUTOSAVE) if (demo.savebutton && demo.savebutton + 3*TICRATE < leveltime && (InputDown(gc_lookback, 1) || (cv_usejoystick[0].value && axis > 0))) demo.savemode = DSM_TITLEENTRY; @@ -958,10 +962,7 @@ void P_PreTicker(INT32 frames) // Run any "after all the other thinkers" stuff for (i = 0; i < MAXPLAYERS; i++) { - if (!playeringame[i]) - continue; - - if (P_MobjWasRemoved(players[i].mo)) + if (!playeringame[i] || P_MobjWasRemoved(players[i].mo)) continue; P_PlayerAfterThink(&players[i]); diff --git a/src/p_user.c b/src/p_user.c index 7ea0e84c4..1f3749406 100644 --- a/src/p_user.c +++ b/src/p_user.c @@ -73,7 +73,7 @@ void P_Thrust(mobj_t *mo, angle_t angle, fixed_t move) mo->momx += FixedMul(move, FINECOSINE(angle)); - if (!(twodlevel || (mo->flags2 & MF2_TWOD))) + if (LIKELY(!(twodlevel || (mo->flags2 & MF2_TWOD)))) mo->momy += FixedMul(move, FINESINE(angle)); } @@ -90,7 +90,7 @@ void P_InstaThrust(mobj_t *mo, angle_t angle, fixed_t move) mo->momx = FixedMul(move, FINECOSINE(angle)); - if (!(twodlevel || (mo->flags2 & MF2_TWOD))) + if (LIKELY(!(twodlevel || (mo->flags2 & MF2_TWOD)))) mo->momy = FixedMul(move,FINESINE(angle)); } @@ -498,9 +498,9 @@ void P_PlayLivesJingle(player_t *player) if (player && !P_IsLocalPlayer(player)) return; - if (use1upSound) + if (UNLIKELY(use1upSound)) S_StartSound(NULL, sfx_oneup); - else if (mariomode) + else if (UNLIKELY(mariomode)) S_StartSound(NULL, sfx_marioa); else { @@ -528,7 +528,7 @@ void P_PlayRinglossSound(mobj_t *source, mobj_t *damager) S_StartSound(NULL, sfx); } else - S_StartSound(source, (mariomode) ? sfx_mario8 : sfx_khurt1 + key); + S_StartSound(source, UNLIKELY(mariomode) ? sfx_mario8 : sfx_khurt1 + key); } else S_StartSound(source, sfx_slip); @@ -1287,13 +1287,14 @@ boolean P_InQuicksand(mobj_t *mo) // Returns true if you are in quicksand { sector_t *sector = mo->subsector->sector; fixed_t topheight, bottomheight; - - fixed_t flipoffset = ((mo->eflags & MFE_VERTICALFLIP) ? (mo->height/2) : 0); + fixed_t flipoffset; if (sector->ffloors) { ffloor_t *rover; + flipoffset = ((mo->eflags & MFE_VERTICALFLIP) ? (mo->height/2) : 0); + for (rover = sector->ffloors; rover; rover = rover->next) { if (!(rover->flags & FF_EXISTS)) @@ -1682,6 +1683,7 @@ static void P_DoBubbleBreath(player_t *player) bubble = P_SpawnMobj(player->mo->x, player->mo->y, zh, MT_SMALLBUBBLE); else if (P_RandomChance(3*FRACUNIT/256)) bubble = P_SpawnMobj(player->mo->x, player->mo->y, zh, MT_MEDIUMBUBBLE); + if (bubble) { bubble->threshold = 42; @@ -1689,8 +1691,8 @@ static void P_DoBubbleBreath(player_t *player) P_SetScale(bubble, bubble->destscale); } - if (player->pflags & PF_NIGHTSMODE) // NiGHTS Super doesn't spawn flight bubbles - return; + //if (player->pflags & PF_NIGHTSMODE) // NiGHTS Super doesn't spawn flight bubbles + //return; } // @@ -2159,7 +2161,7 @@ static void P_MovePlayer(player_t *player) } // note: don't unset stasis here - if (!player->spectator && G_TagGametype()) + if (UNLIKELY(!player->spectator && G_TagGametype())) { // If we have stasis already here, it's because it's forced on us // by a linedef executor or what have you @@ -2219,7 +2221,7 @@ static void P_MovePlayer(player_t *player) player->lturn_max[leveltime%MAXPREDICTTICS] = player->rturn_max[leveltime%MAXPREDICTTICS] = 0; } - if (leveltime >= starttime) + if (LIKELY(leveltime >= starttime)) { // KART: Don't directly apply angleturn! It may have been either A) forged by a malicious client, or B) not be a smooth turn due to a player dropping frames. // Instead, turn the player only up to the amount they're supposed to turn accounting for latency. Allow exactly 1 extra turn unit to try to keep old replays synced. @@ -2258,7 +2260,7 @@ static void P_MovePlayer(player_t *player) P_3dMovement(player); } - if (maptol & TOL_2D) + if (UNLIKELY(maptol & TOL_2D)) // psure this doesent even work at all runspd = FixedMul(runspd, 2*FRACUNIT/3); ///////////////////////// @@ -2319,10 +2321,10 @@ static void P_MovePlayer(player_t *player) // Drifting sound // Start looping the sound now. - if (leveltime % 50 == 0 && onground && player->kartstuff[k_drift] != 0) + if (onground && leveltime % 50 == 0 && player->kartstuff[k_drift] != 0) S_StartSound(player->mo, sfx_drift); // Leveltime being 50 might take a while at times. We'll start it up once, isntantly. - else if (!S_SoundPlaying(player->mo, sfx_drift) && onground && player->kartstuff[k_drift] != 0) + else if (onground && !S_SoundPlaying(player->mo, sfx_drift) && player->kartstuff[k_drift] != 0) S_StartSound(player->mo, sfx_drift); // Ok, we'll stop now. else if (player->kartstuff[k_drift] == 0 || !onground) @@ -2347,7 +2349,7 @@ static void P_MovePlayer(player_t *player) P_SetPlayerMobjState(player->mo, S_KART_STND1); // SRB2kart - was S_PLAY_STND } - if (player->gotflag) // If you can't glide, then why the heck would you be gliding? + if (UNLIKELY(player->gotflag)) // If you can't glide, then why the heck would you be gliding? { player->pflags &= ~PF_GLIDING; player->glidetime = 0; @@ -2708,8 +2710,8 @@ boolean P_LookForEnemies(player_t *player) player->mo->z-mo->z) > FixedMul(RING_DIST, player->mo->scale)) continue; // out of range - if ((twodlevel || player->mo->flags2 & MF2_TWOD) - && abs(player->mo->y-mo->y) > player->mo->radius) + if (UNLIKELY((twodlevel || player->mo->flags2 & MF2_TWOD) + && abs(player->mo->y-mo->y) > player->mo->radius)) continue; // not in your 2d plane if (mo->type == MT_PLAYER) // Don't chase after other players! @@ -4081,7 +4083,7 @@ boolean P_SpectatorJoinGame(player_t *player) // Team changing in Team Match and CTF // Pressing fire assigns you to a team that needs players if allowed. // Partial code reproduction from p_tick.c autobalance code. - else if (G_GametypeHasTeams()) + else if (UNLIKELY(G_GametypeHasTeams())) { INT32 changeto = 0; INT32 z, numplayersred = 0, numplayersblue = 0; @@ -4323,10 +4325,10 @@ static INT32 Quaketilt(player_t *player) if (abs(delta) > ANGLE_90) delta = (INT32)(( moma + ANGLE_180 ) - player->mo->angle ); - const boolean sliptiding = player->kartstuff[k_drift] ? 0 : player->kartstuff[k_aizdriftstrat]; - if (P_IsObjectOnGround(player->mo)) { + const boolean sliptiding = player->kartstuff[k_drift] ? 0 : player->kartstuff[k_aizdriftstrat]; + if (sliptiding) { tilt = ANGLE_45; @@ -4479,8 +4481,8 @@ void P_PlayerThink(player_t *player) { seenplayer = NULL; - if (cv_seenames.value && cv_allowseenames.value && - !(G_TagGametype() && (player->pflags & PF_TAGIT))) + if (UNLIKELY(cv_seenames.value && cv_allowseenames.value && + !(G_TagGametype() && (player->pflags & PF_TAGIT)))) { mobj_t *mo = P_SpawnNameFinder(player->mo, MT_NAMECHECK); @@ -4769,8 +4771,8 @@ void P_PlayerThink(player_t *player) } // Even if not NiGHTS, pull in nearby objects when walking around as John Q. Elliot. - if (!objectplacing && !((netgame || multiplayer) && player->spectator) - && maptol & TOL_NIGHTS && (!(player->pflags & PF_NIGHTSMODE) || player->powers[pw_nights_helper])) + if (UNLIKELY(!objectplacing && !((netgame || multiplayer) && player->spectator) + && maptol & TOL_NIGHTS && (!(player->pflags & PF_NIGHTSMODE) || player->powers[pw_nights_helper]))) { thinker_t *th; mobj_t *mo2; @@ -4835,7 +4837,7 @@ void P_PlayerThink(player_t *player) player->pflags &= ~PF_FULLSTASIS; if (player->onconveyor == 1) - player->cmomy = player->cmomx = 0; + player->cmomy = player->cmomx = 0; P_DoBubbleBreath(player); // Spawn Sonic's bubbles P_CheckInvincibilityTimer(player); // Spawn Invincibility Sparkles @@ -4872,7 +4874,7 @@ void P_PlayerThink(player_t *player) #endif // check for use - if (!(player->pflags & PF_NIGHTSMODE)) + if (LIKELY(!(player->pflags & PF_NIGHTSMODE))) { if (cmd->buttons & BT_BRAKE) player->pflags |= PF_USEDOWN; @@ -5044,6 +5046,11 @@ void P_PlayerAfterThink(player_t *player) P_PlayerInSpecialSector(player); #endif + if (player->playerstate == PST_DEAD) + { + return; + } + for (i = 0; i <= splitscreen; i++) { if (player == &players[displayplayers[i]]) @@ -5053,12 +5060,7 @@ void P_PlayerAfterThink(player_t *player) } } - if (player->playerstate == PST_DEAD) - { - return; - } - - if (player->pflags & PF_NIGHTSMODE) + if (UNLIKELY(player->pflags & PF_NIGHTSMODE)) { player->powers[pw_gravityboots] = 0; } diff --git a/src/s_sound.c b/src/s_sound.c index c93570232..fee9c1312 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -607,7 +607,7 @@ void S_StartSound(const void *origin, sfxenum_t sfx_id) if (S_SoundDisabled()) return; - if (mariomode) // Sounds change in Mario mode! + if (UNLIKELY(mariomode)) // Sounds change in Mario mode! { switch (sfx_id) { @@ -630,7 +630,7 @@ void S_StartSound(const void *origin, sfxenum_t sfx_id) break; } } - if (maptol & TOL_XMAS) // Some sounds change for xmas + if (UNLIKELY(maptol & TOL_XMAS)) // Some sounds change for xmas { switch (sfx_id) { diff --git a/src/sdl/i_system.cpp b/src/sdl/i_system.cpp index eea00fb67..535f519bd 100644 --- a/src/sdl/i_system.cpp +++ b/src/sdl/i_system.cpp @@ -1806,13 +1806,22 @@ void I_SleepDuration(precise_t duration) { #if defined(__linux__) || defined(__FreeBSD__) || defined(__HAIKU__) UINT64 precision = I_GetPrecisePrecision(); - struct timespec ts = { + precise_t dest = I_GetPreciseTime() + duration; + precise_t slack = (precision / 5000); // 0.2 ms slack + if (duration > slack) + { + duration -= slack; + struct timespec ts = { .tv_sec = static_cast<__time_t>(duration / precision), .tv_nsec = static_cast<__syscall_slong_t>(duration * 1000000000 / precision % 1000000000), - }; - int status; - do status = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &ts); - while (status == EINTR); + }; + int status; + do status = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &ts); + while (status == EINTR); + } + + // busy-wait the rest + while (((INT64)dest - (INT64)I_GetPreciseTime()) > 0); #elif defined (MIN_SLEEP_DURATION_MS) UINT64 precision = I_GetPrecisePrecision(); INT32 sleepvalue = cv_sleep.value; diff --git a/src/w_wad.c b/src/w_wad.c index 63071ea3b..51fc22559 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -338,6 +338,11 @@ static void W_InvalidateLumpnumCache(void) memset(lumpnumcache, 0, sizeof (lumpnumcache)); } +UINT32 W_HashLumpName(const char *name) +{ + return FNV1a_HashLowercaseString(name); +} + /** Detect a file type. * \todo Actually detect the wad/pkzip headers and whatnot, instead of just checking the extensions. */ @@ -362,11 +367,24 @@ static lumpinfo_t* ResGetLumpsStandalone (FILE* handle, UINT16* numlumps, const fseek(handle, 0, SEEK_END); lumpinfo->size = ftell(handle); fseek(handle, 0, SEEK_SET); - strlcpy(lumpinfo->name, lumpname, 9); - // Allocate the lump's full and long name. - lumpinfo->fullname = Z_StrDup(lumpname); - lumpinfo->longname = Z_StrDup(lumpname); + strlcpy(lumpinfo->name, lumpname, sizeof(lumpinfo->name)); + lumpinfo->namelength = strlen(lumpinfo->name); + lumpinfo->hash.name = W_HashLumpName(lumpname); + + // Allocate the lump's long name. + lumpinfo->longname = Z_Malloc((lumpinfo->namelength + 1) * sizeof(char), PU_STATIC, NULL); + strcpy(lumpinfo->longname, lumpname); + lumpinfo->longname[lumpinfo->namelength] = '\0'; + lumpinfo->longnamelength = lumpinfo->namelength; + lumpinfo->hash.longname = lumpinfo->hash.name; + + // Allocate the lump's full name. + lumpinfo->fullname = Z_Malloc((lumpinfo->namelength + 1) * sizeof(char), PU_STATIC, NULL); + strcpy(lumpinfo->fullname, lumpname); + lumpinfo->fullname[lumpinfo->namelength] = '\0'; + lumpinfo->fullnamelength = lumpinfo->namelength; + lumpinfo->hash.fullname = lumpinfo->hash.name; *numlumps = 1; return lumpinfo; @@ -454,16 +472,22 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen lump_p->compression = CM_NOCOMPRESSION; memset(lump_p->name, 0x00, 9); strncpy(lump_p->name, fileinfo->name, 8); + lump_p->namelength = strlen(lump_p->name); + lump_p->hash.name = W_HashLumpName(lump_p->name); // Allocate the lump's long name. lump_p->longname = Z_Malloc(9 * sizeof(char), PU_STATIC, NULL); strncpy(lump_p->longname, fileinfo->name, 8); lump_p->longname[8] = '\0'; + lump_p->longnamelength = lump_p->namelength; + lump_p->hash.longname = lump_p->hash.name; // Allocate the lump's full name. lump_p->fullname = Z_Malloc(9 * sizeof(char), PU_STATIC, NULL); strncpy(lump_p->fullname, fileinfo->name, 8); lump_p->fullname[8] = '\0'; + lump_p->fullnamelength = lump_p->namelength; + lump_p->hash.fullname = lump_p->hash.name; } free(fileinfov); *nlmp = numlumps; @@ -656,12 +680,18 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp) memset(lump_p->name, '\0', 9); // Making sure they're initialized to 0. Is it necessary? strncpy(lump_p->name, trimname, min(8, dotpos - trimname)); + lump_p->namelength = strlen(lump_p->name); + lump_p->hash.name = W_HashLumpName(lump_p->name); lump_p->longname = Z_Calloc(dotpos - trimname + 1, PU_STATIC, NULL); strlcpy(lump_p->longname, trimname, dotpos - trimname + 1); + lump_p->longnamelength = strlen(lump_p->longname); + lump_p->hash.longname = W_HashLumpName(lump_p->longname); lump_p->fullname = (char*)(Z_Calloc(SHORT(zentry->namelen) + 1, PU_STATIC, NULL)); strncpy(lump_p->fullname, fullname, SHORT(zentry->namelen)); + lump_p->fullnamelength = zentry->namelen; + lump_p->hash.fullname = W_HashLumpName(lump_p->fullname); switch(SHORT(zentry->compression)) { @@ -976,6 +1006,8 @@ UINT16 W_CheckNumForNamePwad(const char *name, UINT16 wad, UINT16 startlump) { UINT16 i; static char uname[9]; + UINT32 hash; + size_t namelen; if (!TestValidLump(wad,0)) return INT16_MAX; @@ -983,6 +1015,8 @@ UINT16 W_CheckNumForNamePwad(const char *name, UINT16 wad, UINT16 startlump) memset(uname, 0, sizeof uname); strncpy(uname, name, sizeof(uname)-1); strupr(uname); + namelen = strlen(uname); + hash = W_HashLumpName(uname); // // scan forward @@ -993,7 +1027,9 @@ UINT16 W_CheckNumForNamePwad(const char *name, UINT16 wad, UINT16 startlump) { lumpinfo_t *lump_p = wadfiles[wad]->lumpinfo + startlump; for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) - if (memcmp(lump_p->name, uname, sizeof(uname) - 1) == 0) + if (lump_p->namelength == namelen + && lump_p->hash.name == hash + && !memcmp(lump_p->name, uname, sizeof(uname) - 1)) return i; } @@ -1010,13 +1046,12 @@ UINT16 W_CheckNumForNamePwad(const char *name, UINT16 wad, UINT16 startlump) UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump) { UINT16 i; - static char uname[256 + 1]; if (!TestValidLump(wad,0)) return INT16_MAX; - strlcpy(uname, name, sizeof uname); - strupr(uname); + size_t namelen = strlen(name); + UINT32 hash = W_HashLumpName(name); // // scan forward @@ -1027,7 +1062,9 @@ UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump) { lumpinfo_t *lump_p = wadfiles[wad]->lumpinfo + startlump; for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) - if (!strcmp(lump_p->longname, uname)) + if (lump_p->longnamelength == namelen + && lump_p->hash.longname == hash + && !stricmp(lump_p->longname, name)) return i; } @@ -1058,10 +1095,10 @@ UINT16 W_CheckNumForFolderStartPK3(const char *name, UINT16 wad, UINT16 startlum /* SLADE is special and puts a single directory entry. Skip that. */ if (strlen(lump_p->fullname) == name_length) i++; - break; + return i; } } - return i; + return INT16_MAX; } // In a PK3 type of resource file, it looks for the next lumpinfo entry that doesn't share the specified pathfile. @@ -1201,41 +1238,6 @@ lumpnum_t W_CheckNumForLongName(const char *name) } } -// Look for valid map data through all added files in descendant order. -// Get a map marker for WADs, and a standalone WAD file lump inside PK3s. -// TODO: Make it search through cache first, maybe...? -lumpnum_t W_CheckNumForMap(const char *name) -{ - UINT16 lumpNum, end; - UINT32 i; - for (i = numwadfiles - 1; i < numwadfiles; i--) - { - if (wadfiles[i]->type == RET_WAD) - { - for (lumpNum = 0; lumpNum < wadfiles[i]->numlumps; lumpNum++) - if (!strncmp(name, (wadfiles[i]->lumpinfo + lumpNum)->name, 8)) - return (i<<16) + lumpNum; - } - else if (wadfiles[i]->type == RET_PK3) - { - lumpNum = W_CheckNumForFolderStartPK3("maps/", i, 0); - if (lumpNum != INT16_MAX) - end = W_CheckNumForFolderEndPK3("maps/", i, lumpNum); - else - continue; - // Now look for the specified map. - for (; lumpNum < end; lumpNum++) - if (!strnicmp(name, (wadfiles[i]->lumpinfo + lumpNum)->name, 8)) - { - const char *extension = strrchr(wadfiles[i]->lumpinfo[lumpNum].fullname, '.'); - if (!(extension && stricmp(extension, ".wad"))) - return (i<<16) + lumpNum; - } - } - } - return LUMPERROR; -} - // // W_GetNumForName // diff --git a/src/w_wad.h b/src/w_wad.h index 9b1035fa2..f49b0b8a5 100644 --- a/src/w_wad.h +++ b/src/w_wad.h @@ -75,8 +75,19 @@ typedef struct char name[9]; // filelump_t name[] e.g. "LongEntr" char *longname; // e.g. "LongEntryName" char *fullname; // e.g. "Folder/Subfolder/LongEntryName.extension" + + size_t namelength; // length of name + size_t longnamelength; // length of longname + size_t fullnamelength; // length of fullname + size_t size; // real (uncompressed) size compmethod compression; // lump compression method + + struct { + UINT32 name; // hash of name + UINT32 longname; // hash of longname + UINT32 fullname; // hash of fullname + } hash; } lumpinfo_t; // ========================================================================= @@ -167,6 +178,8 @@ INT32 W_AddAutoloadedLocalFiles(char **filenames); #define W_FileHasFolders(wadfile) ((wadfile)->type == RET_PK3) +UINT32 W_HashLumpName(const char *name); + const char *W_CheckNameForNumPwad(UINT16 wad, UINT16 lump); const char *W_CheckNameForNum(lumpnum_t lumpnum); @@ -180,7 +193,6 @@ UINT16 W_CheckNumForFullNamePK3(const char *name, UINT16 wad, UINT16 startlump); UINT16 W_CheckNumForFolderStartPK3(const char *name, UINT16 wad, UINT16 startlump); UINT16 W_CheckNumForFolderEndPK3(const char *name, UINT16 wad, UINT16 startlump); -lumpnum_t W_CheckNumForMap(const char *name); lumpnum_t W_CheckNumForName(const char *name); lumpnum_t W_CheckNumForLongName(const char *name); lumpnum_t W_GetNumForName(const char *name); // like W_CheckNumForName but I_Error on LUMPERROR