From 7c24fcfecb6e362ab7833c2c8db9373d410acc4e Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 31 Jul 2024 06:07:43 +0200 Subject: [PATCH] Reapply "optimize precip thinker" This reverts commit dd73787c8b6bb8216fc646d4f7ce102347d713e5. --- src/hardware/hw_main.c | 16 ++--- src/info.c | 20 +++--- src/m_perfstats.c | 5 -- src/p_local.h | 1 + src/p_mobj.c | 157 +++++++++++++++++++++-------------------- src/p_mobj.h | 33 ++++----- src/p_spec.c | 5 +- src/p_tick.c | 34 +++++---- src/r_things.c | 20 ++---- 9 files changed, 141 insertions(+), 150 deletions(-) diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c index 04caef086..149a468f8 100644 --- a/src/hardware/hw_main.c +++ b/src/hardware/hw_main.c @@ -5116,6 +5116,12 @@ void HWR_ProjectPrecipitationSprite(precipmobj_t *thing) // uncapped/interpolation interpmobjstate_t interp = {0}; + // okay... this is a hack, but weather isn't networked, so it should be ok + if (!P_PrecipThinker(thing)) + { + return; + } + // do interpolation if (R_UsingFrameInterpolation() && !paused && (!cv_grmaxinterpdist.value || dist < cv_grmaxinterpdist.value)) { @@ -5209,16 +5215,6 @@ void HWR_ProjectPrecipitationSprite(precipmobj_t *thing) vis->gz = vis->gzt - FIXED_TO_FLOAT(spritecachedinfo[lumpoff].height); vis->precip = true; - - // okay... this is a hack, but weather isn't networked, so it should be ok - if (!(thing->precipflags & PCF_THUNK)) - { - if (thing->precipflags & PCF_RAIN) - P_RainThinker(thing); - else - P_SnowThinker(thing); - thing->precipflags |= PCF_THUNK; - } } static boolean drewsky = false; diff --git a/src/info.c b/src/info.c index 0d5dbea71..91fb99b5a 100644 --- a/src/info.c +++ b/src/info.c @@ -1822,7 +1822,7 @@ state_t states[NUMSTATES] = // Rain {SPR_RAIN, FF_TRANS50, -1, {NULL}, 0, 0, S_NULL}, // S_RAIN1 - {SPR_RAIN, FF_TRANS50, 1, {NULL}, 0, 0, S_RAIN1}, // S_RAINRETURN + {SPR_NULL, 0, -1, {NULL}, 0, 0, S_NULL}, // S_RAINRETURN // Snowflake {SPR_SNO1, 0, -1, {NULL}, 0, 0, S_NULL}, // S_SNOW1 @@ -1841,9 +1841,9 @@ state_t states[NUMSTATES] = {SPR_SPLH, FF_TRANS50|8, 2, {NULL}, 0, 0, S_NULL}, // S_SPLISH9 // Water Splash - {SPR_SPLA, FF_TRANS50 , 3, {NULL}, 0, 0, S_SPLASH2}, // S_SPLASH1 - {SPR_SPLA, FF_TRANS70|1, 3, {NULL}, 0, 0, S_SPLASH3}, // S_SPLASH2 - {SPR_SPLA, FF_TRANS90|2, 3, {NULL}, 0, 0, S_RAINRETURN}, // S_SPLASH3 + {SPR_SPLA, FF_TRANS50 , 3, {NULL}, 0, 0, S_SPLASH2}, // S_SPLASH1 + {SPR_SPLA, FF_TRANS70|1, 3, {NULL}, 0, 0, S_SPLASH3}, // S_SPLASH2 + {SPR_SPLA, FF_TRANS90|2, 3, {NULL}, 0, 0, S_NULL}, // S_SPLASH3 // Smoke {SPR_SMOK, FF_TRANS50 , 4, {NULL}, 0, 0, S_SMOKE2}, // S_SMOKE1 @@ -11198,22 +11198,22 @@ mobjinfo_t mobjinfo[NUMMOBJTYPES] = S_RAIN1, // spawnstate 1000, // spawnhealth S_NULL, // seestate - sfx_None, // seesound - 8, // reactiontime + sfx_rainin, // seesound + 0, // reactiontime sfx_None, // attacksound S_NULL, // painstate 0, // painchance sfx_None, // painsound S_NULL, // meleestate S_NULL, // missilestate - S_NULL, // deathstate + S_SPLASH1, // deathstate S_NULL, // xdeathstate sfx_None, // deathsound -72*FRACUNIT, // speed -- -24*FRACUNIT originally, srb2kart x3 (nya) 1*FRACUNIT, // radius 8*FRACUNIT, // height 0, // display offset - 4, // mass + 80, // mass 0, // damage sfx_None, // activesound MF_NOBLOCKMAP, // flags @@ -11226,7 +11226,7 @@ mobjinfo_t mobjinfo[NUMMOBJTYPES] = 1000, // spawnhealth S_NULL, // seestate sfx_None, // seesound - 8, // reactiontime + 0, // reactiontime sfx_None, // attacksound S_NULL, // painstate 0, // painchance @@ -11240,7 +11240,7 @@ mobjinfo_t mobjinfo[NUMMOBJTYPES] = 4*FRACUNIT, // radius 4*FRACUNIT, // height 0, // display offset - 4, // mass + 0, // mass 0, // damage sfx_None, // activesound MF_NOBLOCKMAP, // flags diff --git a/src/m_perfstats.c b/src/m_perfstats.c index 04e51a657..a8f9550f4 100644 --- a/src/m_perfstats.c +++ b/src/m_perfstats.c @@ -58,7 +58,6 @@ static ps_metric_t ps_regularcount = {0}; static ps_metric_t ps_scenerycount = {0}; static ps_metric_t ps_nothinkcount = {0}; static ps_metric_t ps_otherthcount = {0}; -static ps_metric_t ps_precipcount = {0}; static ps_metric_t ps_removecount = {0}; ps_metric_t ps_checkposition_calls = {0}; @@ -173,7 +172,6 @@ perfstatrow_t thinkercount_rows[] = { {" scenery", " Scenery: ", &ps_scenerycount, PS_LEVEL}, {" nothink", " Nothink: ", &ps_nothinkcount, PS_HIDE_ZERO|PS_LEVEL}, {" other ", " Other: ", &ps_otherthcount, PS_LEVEL}, - {" precip ", " Precipitation: ", &ps_precipcount, PS_LEVEL}, {" remove ", " Pending removal:", &ps_removecount, PS_LEVEL}, {0} }; @@ -572,7 +570,6 @@ static void PS_CountThinkers(void) ps_scenerycount.value.i = 0; ps_nothinkcount.value.i = 0; ps_otherthcount.value.i = 0; - ps_precipcount.value.i = 0; ps_removecount.value.i = 0; for (thinker = thinkercap.next; thinker != &thinkercap; thinker = thinker->next) { @@ -590,8 +587,6 @@ static void PS_CountThinkers(void) else ps_regularcount.value.i++; } - else if (thinker->function.acp1 == (actionf_p1)P_NullPrecipThinker) - ps_precipcount.value.i++; else ps_otherthcount.value.i++; } diff --git a/src/p_local.h b/src/p_local.h index 0ac98da7d..4c4e7b549 100644 --- a/src/p_local.h +++ b/src/p_local.h @@ -70,6 +70,7 @@ extern thinker_t thinkercap; void P_InitThinkers(void); void P_AddThinker(thinker_t *thinker); void P_RemoveThinker(thinker_t *thinker); +void P_UnlinkThinker(thinker_t *thinker); // // P_USER diff --git a/src/p_mobj.c b/src/p_mobj.c index eb49ae84c..3f3b816ae 100644 --- a/src/p_mobj.c +++ b/src/p_mobj.c @@ -338,7 +338,7 @@ static boolean P_SetPrecipMobjState(precipmobj_t *mobj, statenum_t state) if (state == S_NULL) { // Remove mobj - P_RemovePrecipMobj(mobj); + P_FreePrecipMobj(mobj); return false; } st = &states[state]; @@ -3611,67 +3611,86 @@ void P_RecalcPrecipInSector(sector_t *sector) // // P_NullPrecipThinker // -// For "Blank" precipitation +// Just the identification of a precip thinker. The thinker +// should never actually be called! // void P_NullPrecipThinker(precipmobj_t *mobj) { - //(void)mobj; - mobj->precipflags &= ~PCF_THUNK; - R_ResetPrecipitationMobjInterpolationState(mobj); + (void)mobj; + I_Assert("P_NullPrecipThinker should not be called" == 0); } -void P_SnowThinker(precipmobj_t *mobj) +boolean P_PrecipThinker(precipmobj_t *mobj) { + if (mobj->lastThink == leveltime) + return true; // already thinked this tick + + mobj->lastThink = leveltime; + + R_ResetPrecipitationMobjInterpolationState(mobj); P_CycleStateAnimation((mobj_t *)mobj); + if (mobj->state == &states[S_RAINRETURN]) + { + // Reset to ceiling! + if (!P_SetPrecipMobjState(mobj, mobj->info->spawnstate)) + return false; + + mobj->z = mobj->ceilingz; + mobj->momz = mobj->info->speed; + mobj->precipflags &= ~PCF_SPLASH; + } + + if (mobj->tics != -1) + { + if (mobj->tics) + { + mobj->tics--; + } + + if (mobj->tics == 0) + { + if ((mobj->precipflags & PCF_SPLASH) && (mobj->state->nextstate == S_NULL)) + { + // HACK: sprite changes are 1 tic late, so you would see splashes on the ceiling if not for this state. + // We need to use the settings from the previous state, since some of those are NOT 1 tic late. + INT32 frame = (mobj->frame & ~FF_FRAMEMASK); + + if (!P_SetPrecipMobjState(mobj, S_RAINRETURN)) + return false; + + mobj->frame = frame; + return true; + } + else + { + if (!P_SetPrecipMobjState(mobj, mobj->state->nextstate)) + return false; + } + } + } + + if (mobj->precipflags & PCF_SPLASH) + return true; + // adjust height if ((mobj->z += mobj->momz) <= mobj->floorz) { - mobj->z = mobj->ceilingz; - R_ResetPrecipitationMobjInterpolationState(mobj); - } -} + if ((mobj->info->deathstate == S_NULL) || (mobj->precipflags & PCF_PIT)) // no splashes on sky or bottomless pits + { + mobj->z = mobj->ceilingz; + } + else + { + if (!P_SetPrecipMobjState(mobj, mobj->info->deathstate)) + return false; -void P_RainThinker(precipmobj_t *mobj) -{ - P_CycleStateAnimation((mobj_t *)mobj); - - if (mobj->state != &states[S_RAIN1]) - { - // cycle through states, - // calling action functions at transitions - if (mobj->tics <= 0) - return; - - if (--mobj->tics) - return; - - if (!P_SetPrecipMobjState(mobj, mobj->state->nextstate)) - return; - - if (mobj->state != &states[S_RAINRETURN]) - return; - - mobj->z = mobj->ceilingz; - R_ResetPrecipitationMobjInterpolationState(mobj); - P_SetPrecipMobjState(mobj, S_RAIN1); - - return; + mobj->z = mobj->floorz; + mobj->precipflags |= PCF_SPLASH; + } } - // adjust height - if ((mobj->z += mobj->momz) > mobj->floorz) - return; - - // no splashes on sky or bottomless pits - if (mobj->precipflags & PCF_PIT) - { - mobj->z = mobj->ceilingz; - return; - } - - mobj->z = mobj->floorz; - P_SetPrecipMobjState(mobj, S_SPLASH1); + return true; } static void P_RingThinker(mobj_t *mobj) @@ -9943,17 +9962,21 @@ mobj_t *P_SpawnShadowMobj(mobj_t * caster) static precipmobj_t *P_SpawnPrecipMobj(fixed_t x, fixed_t y, fixed_t z, mobjtype_t type) { + const mobjinfo_t *info = &mobjinfo[type]; state_t *st; precipmobj_t *mobj = Z_Calloc(sizeof (*mobj), PU_LEVEL, NULL); fixed_t starting_floorz; + mobj->type = type; + mobj->info = info; + mobj->x = x; mobj->y = y; - mobj->flags = mobjinfo[type].flags; + mobj->flags = info->flags; // do not set the state with P_SetMobjState, // because action routines can not be called yet - st = &states[mobjinfo[type].spawnstate]; + st = &states[info->spawnstate]; mobj->state = st; mobj->tics = st->tics; @@ -9968,7 +9991,7 @@ static precipmobj_t *P_SpawnPrecipMobj(fixed_t x, fixed_t y, fixed_t z, mobjtype mobj->ceilingz = P_GetSectorCeilingZAt(mobj->subsector->sector, x, y); mobj->z = z; - mobj->momz = mobjinfo[type].speed; + mobj->momz = info->speed; mobj->thinker.function.acp1 = (actionf_p1)P_NullPrecipThinker; P_AddThinker(&mobj->thinker); @@ -9987,21 +10010,6 @@ static precipmobj_t *P_SpawnPrecipMobj(fixed_t x, fixed_t y, fixed_t z, mobjtype return mobj; } -static inline precipmobj_t *P_SpawnRainMobj(fixed_t x, fixed_t y, fixed_t z, mobjtype_t type) -{ - precipmobj_t *mo = P_SpawnPrecipMobj(x,y,z,type); - mo->precipflags |= PCF_RAIN; - //mo->thinker.function.acp1 = (actionf_p1)P_RainThinker; - return mo; -} - -static inline precipmobj_t *P_SpawnSnowMobj(fixed_t x, fixed_t y, fixed_t z, mobjtype_t type) -{ - precipmobj_t *mo = P_SpawnPrecipMobj(x,y,z,type); - //mo->thinker.function.acp1 = (actionf_p1)P_SnowThinker; - return mo; -} - // // P_RemoveMobj // @@ -10145,7 +10153,7 @@ inline boolean P_MobjWasRemoved(const mobj_t *mobj) return !(mobj && mobj->thinker.function.acp1 == (actionf_p1)P_MobjThinker); } -void P_RemovePrecipMobj(precipmobj_t *mobj) +void P_FreePrecipMobj(precipmobj_t *mobj) { // unlink from sector and block lists P_UnsetPrecipThingPosition(mobj); @@ -10157,7 +10165,9 @@ void P_RemovePrecipMobj(precipmobj_t *mobj) } // free block - P_RemoveThinker((thinker_t *)mobj); + // Precipmobjs don't actually think using their thinker, + // so the free cannot be delayed. + P_UnlinkThinker((thinker_t*)mobj); } // Clearing out stuff for savegames @@ -10193,12 +10203,7 @@ void P_RemoveSavegameMobj(mobj_t *mobj) // free block // Here we use the same code as R_RemoveThinkerDelayed, but without reference counting (we're removing everything so it shouldn't matter) and without touching currentthinker since we aren't in P_RunThinkers - { - thinker_t *thinker = (thinker_t *)mobj; - thinker_t *next = thinker->next; - (next->prev = thinker->prev)->next = next; - Z_Free(thinker); - } + P_UnlinkThinker((thinker_t*)mobj); } static CV_PossibleValue_t respawnitemtime_cons_t[] = {{1, "MIN"}, {300, "MAX"}, {0, NULL}}; @@ -10252,7 +10257,7 @@ void P_SpawnPrecipitation(void) if (curWeather == PRECIP_SNOW) { - rainmo = P_SpawnSnowMobj(x, y, height, MT_SNOWFLAKE); + rainmo = P_SpawnPrecipMobj(x, y, height, MT_SNOWFLAKE); mrand = M_RandomByte(); if (mrand < 64) P_SetPrecipMobjState(rainmo, S_SNOW3); @@ -10260,7 +10265,7 @@ void P_SpawnPrecipitation(void) P_SetPrecipMobjState(rainmo, S_SNOW2); } else // everything else. - rainmo = P_SpawnRainMobj(x, y, height, MT_RAIN); + rainmo = P_SpawnPrecipMobj(x, y, height, MT_RAIN); floorz = rainmo->floorz >> FRACBITS; ceilingz = rainmo->ceilingz >> FRACBITS; diff --git a/src/p_mobj.h b/src/p_mobj.h index 90c9cceb1..2f3d210c9 100644 --- a/src/p_mobj.h +++ b/src/p_mobj.h @@ -254,24 +254,21 @@ typedef enum // PRECIPITATION flags ?! ?! ?! // typedef enum { - // Don't draw. - PCF_INVISIBLE = 1, - // Above pit. - PCF_PIT = 2, - // Above FOF. - PCF_FOF = 4, - // Above MOVING FOF (this means we need to keep floorz up to date...) - PCF_MOVINGFOF = 8, - // Is rain. - PCF_RAIN = 16, - // Ran the thinker this tic. - PCF_THUNK = 32, + PCF_INVISIBLE = 1, // Don't draw. + PCF_PIT = 1<<1, // Above pit. + PCF_FOF = 1<<2, // Above FOF. + PCF_MOVINGFOF = 1<<3, // Above MOVING FOF (this means we need to keep floorz up to date...) + PCF_SPLASH = 1<<4, // Splashed on the ground, return to the ceiling after the animation's over + PCF_THUNK = 1<<5, // Ran the thinker this tic. } precipflag_t; // Map Object definition. typedef struct mobj_s { // List: thinker links. thinker_t thinker; + + mobjtype_t type; + const mobjinfo_t *info; // &mobjinfo[mobj->type] // Info for drawing: position. fixed_t x, y, z; @@ -345,9 +342,6 @@ typedef struct mobj_s struct mobj_s *hnext; struct mobj_s *hprev; - mobjtype_t type; - const mobjinfo_t *info; // &mobjinfo[mobj->type] - INT32 health; // for player this is rings + 1 // Movement direction, movement generation (zig-zagging). @@ -427,6 +421,9 @@ typedef struct precipmobj_s // List: thinker links. thinker_t thinker; + mobjtype_t type; + const mobjinfo_t *info; // &mobjinfo[mobj->type] + // Info for drawing: position. fixed_t x, y, z; fixed_t old_x, old_y, old_z; // position interpolation @@ -480,6 +477,7 @@ typedef struct precipmobj_s INT32 tics; // state tic counter state_t *state; INT32 flags; // flags from mobjinfo tables + tic_t lastThink; } precipmobj_t; typedef struct actioncache_s @@ -519,10 +517,9 @@ void P_SpawnParaloop(fixed_t x, fixed_t y, fixed_t z, fixed_t radius, INT32 numb boolean P_BossTargetPlayer(mobj_t *actor, boolean closest); boolean P_SupermanLook4Players(mobj_t *actor); void P_DestroyRobots(void); -void P_SnowThinker(precipmobj_t *mobj); -void P_RainThinker(precipmobj_t *mobj); +boolean P_PrecipThinker(precipmobj_t *mobj); void P_NullPrecipThinker(precipmobj_t *mobj); -void P_RemovePrecipMobj(precipmobj_t *mobj); +void P_FreePrecipMobj(precipmobj_t *mobj); void P_SetScale(mobj_t *mobj, fixed_t newscale); void P_XYMovement(mobj_t *mo); void P_EmeraldManager(void); diff --git a/src/p_spec.c b/src/p_spec.c index 37a3ef83d..64d9a5914 100644 --- a/src/p_spec.c +++ b/src/p_spec.c @@ -1782,7 +1782,7 @@ void P_SwitchWeather(INT32 weathernum) precipmobj = (precipmobj_t *)think; - P_RemovePrecipMobj(precipmobj); + P_FreePrecipMobj(precipmobj); } } else if (swap && !((swap == PRECIP_BLANK && curWeather == PRECIP_STORM_NORAIN) || (swap == PRECIP_STORM_NORAIN && curWeather == PRECIP_BLANK))) // Rather than respawn all that crap, reuse it! @@ -1809,7 +1809,6 @@ void P_SwitchWeather(INT32 weathernum) precipmobj->precipflags &= ~PCF_INVISIBLE; - precipmobj->precipflags |= PCF_RAIN; //think->function.acp1 = (actionf_p1)P_RainThinker; } else if (swap == PRECIP_SNOW) // Rain To Snow @@ -1833,7 +1832,7 @@ void P_SwitchWeather(INT32 weathernum) precipmobj->frame = st->frame; precipmobj->momz = mobjinfo[MT_SNOWFLAKE].speed; - precipmobj->precipflags &= ~(PCF_INVISIBLE|PCF_RAIN); + precipmobj->precipflags &= ~(PCF_INVISIBLE); //think->function.acp1 = (actionf_p1)P_SnowThinker; } diff --git a/src/p_tick.c b/src/p_tick.c index 66a3acd2a..bbd151e94 100644 --- a/src/p_tick.c +++ b/src/p_tick.c @@ -68,8 +68,6 @@ void Command_Numthinkers_f(void) CONS_Printf(M_GetText("numthinkers <#>: Count number of thinkers\n")); CONS_Printf( "\t1: P_MobjThinker\n" - /*"\t2: P_RainThinker\n" - "\t3: P_SnowThinker\n"*/ "\t2: P_NullPrecipThinker\n" "\t3: T_Friction\n" "\t4: T_Pusher\n" @@ -85,14 +83,6 @@ void Command_Numthinkers_f(void) action = (actionf_p1)P_MobjThinker; CONS_Printf(M_GetText("Number of %s: "), "P_MobjThinker"); break; - /*case 2: - action = (actionf_p1)P_RainThinker; - CONS_Printf(M_GetText("Number of %s: "), "P_RainThinker"); - break; - case 3: - action = (actionf_p1)P_SnowThinker; - CONS_Printf(M_GetText("Number of %s: "), "P_SnowThinker"); - break;*/ case 2: action = (actionf_p1)P_NullPrecipThinker; CONS_Printf(M_GetText("Number of %s: "), "P_NullPrecipThinker"); @@ -235,7 +225,6 @@ static thinker_t *currentthinker; // void P_RemoveThinkerDelayed(thinker_t *thinker) { - thinker_t *next; #ifdef PARANOIA if (thinker->next) thinker->next = NULL; @@ -245,14 +234,29 @@ void P_RemoveThinkerDelayed(thinker_t *thinker) if (thinker->references) return; - /* Remove from main thinker list */ - next = thinker->next; + R_DestroyLevelInterpolators(thinker); + /* Note that currentthinker is guaranteed to point to us, * and since we're freeing our memory, we had better change that. So * point it to thinker->prev, so the iterator will correctly move on to * thinker->prev->next = thinker->next */ - (next->prev = currentthinker = thinker->prev)->next = next; - R_DestroyLevelInterpolators(thinker); + currentthinker = thinker->prev; + /* Remove from main thinker list */ + P_UnlinkThinker(thinker); +} + +// +// P_UnlinkThinker() +// +// Actually removes thinker from the list and frees its memory. +// +void P_UnlinkThinker(thinker_t *thinker) +{ + thinker_t *next = thinker->next; + + I_Assert(thinker->references == 0); + + (next->prev = thinker->prev)->next = next; Z_Free(thinker); } diff --git a/src/r_things.c b/src/r_things.c index e0dd3182d..22936bdee 100644 --- a/src/r_things.c +++ b/src/r_things.c @@ -1752,6 +1752,12 @@ static void R_ProjectPrecipitationSprite(precipmobj_t *thing) // uncapped/interpolation interpmobjstate_t interp = {0}; + // okay... this is a hack, but weather isn't networked, so it should be ok + if (!P_PrecipThinker(thing)) + { + return; + } + // do interpolation if (R_UsingFrameInterpolation() && !paused && (!cv_grmaxinterpdist.value || dist < cv_grmaxinterpdist.value)) { @@ -1846,7 +1852,7 @@ static void R_ProjectPrecipitationSprite(precipmobj_t *thing) if (thing->subsector->sector->cullheight) { if (R_DoCulling(thing->subsector->sector->cullheight, viewsector->cullheight, viewz, gz, gzt)) - goto weatherthink; + return; } // store information in a vissprite @@ -1913,18 +1919,6 @@ static void R_ProjectPrecipitationSprite(precipmobj_t *thing) vis->precip = true; vis->vflip = false; vis->isScaled = false; - -weatherthink: - // okay... this is a hack, but weather isn't networked, so it should be ok - if (!(thing->precipflags & PCF_THUNK)) - { - if (thing->precipflags & PCF_RAIN) - P_RainThinker(thing); - else - P_SnowThinker(thing); - thing->precipflags |= PCF_THUNK; - } - } // R_AddSprites