Skip to content

Commit

Permalink
Access world through global, access local player through helper funct…
Browse files Browse the repository at this point in the history
…ion, remove some obsolete utility functions

ValveSoftware/halflife#3307
  • Loading branch information
SamVanheer committed Oct 7, 2022
1 parent 6f0ae55 commit f2c8e23
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 80 deletions.
41 changes: 23 additions & 18 deletions dlls/cbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,23 +279,9 @@ class CBaseEntity
bool IsDormant();
bool IsLockedByMaster() { return false; }

static CBaseEntity* Instance(edict_t* pent)
{
if (!pent)
pent = ENT(0);
CBaseEntity* pEnt = (CBaseEntity*)GET_PRIVATE(pent);
return pEnt;
}

static CBaseEntity* Instance(entvars_t* pev)
{
if (!pev)
return Instance(ENT(0));
static CBaseEntity* Instance(edict_t* pent);

return Instance(ENT(pev));
}

static CBaseEntity* Instance(int eoffset) { return Instance(ENT(eoffset)); }
static CBaseEntity* Instance(entvars_t* pev);

CBaseMonster* GetMonsterPointer(entvars_t* pevMonster)
{
Expand Down Expand Up @@ -360,7 +346,6 @@ class CBaseEntity

virtual bool FBecomeProne() { return false; }
edict_t* edict() { return ENT(pev); }
EOFFSET eoffset() { return OFFSET(pev); }
int entindex() { return ENTINDEX(edict()); }

virtual Vector Center() { return (pev->absmax + pev->absmin) * 0.5; } // center point of entity
Expand Down Expand Up @@ -756,9 +741,29 @@ push_trigger_data
class CWorld : public CBaseEntity
{
public:
CWorld();
~CWorld();

void Spawn() override;
void Precache() override;
bool KeyValue(KeyValueData* pkvd) override;

static inline CWorld* Instance = nullptr;
};

inline DLL_GLOBAL edict_t* g_pBodyQueueHead = nullptr;
inline DLL_GLOBAL edict_t* g_pBodyQueueHead = nullptr;

inline CBaseEntity* CBaseEntity::Instance(edict_t* pent)
{
if (!pent)
return CWorld::Instance;
return (CBaseEntity*)GET_PRIVATE(pent);
}

inline CBaseEntity* CBaseEntity::Instance(entvars_t* pev)
{
if (!pev)
return CWorld::Instance;

return Instance(ENT(pev));
}
10 changes: 5 additions & 5 deletions dlls/effects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1790,16 +1790,16 @@ Vector CBlood::BloodPosition(CBaseEntity* pActivator)
{
if ((pev->spawnflags & SF_BLOOD_PLAYER) != 0)
{
edict_t* pPlayer;
CBaseEntity* pPlayer;

if (pActivator && pActivator->IsPlayer())
{
pPlayer = pActivator->edict();
pPlayer = pActivator;
}
else
pPlayer = g_engfuncs.pfnPEntityOfEntIndex(1);
pPlayer = UTIL_GetLocalPlayer();
if (pPlayer)
return (pPlayer->v.origin + pPlayer->v.view_ofs) + Vector(RANDOM_FLOAT(-10, 10), RANDOM_FLOAT(-10, 10), RANDOM_FLOAT(-10, 10));
return (pPlayer->pev->origin + pPlayer->pev->view_ofs) + Vector(RANDOM_FLOAT(-10, 10), RANDOM_FLOAT(-10, 10), RANDOM_FLOAT(-10, 10));
}

return pev->origin;
Expand Down Expand Up @@ -2070,7 +2070,7 @@ void CMessage::Use(CBaseEntity* pActivator, CBaseEntity* pCaller, USE_TYPE useTy
pPlayer = pActivator;
else
{
pPlayer = CBaseEntity::Instance(g_engfuncs.pfnPEntityOfEntIndex(1));
pPlayer = UTIL_GetLocalPlayer();
}
if (pPlayer)
UTIL_ShowMessage(STRING(pev->message), pPlayer);
Expand Down
2 changes: 1 addition & 1 deletion dlls/gauss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ void CGauss::SecondaryAttack()
SendStopEvent(false);

#ifndef CLIENT_DLL
m_pPlayer->TakeDamage(VARS(eoNullEntity), VARS(eoNullEntity), 50, DMG_SHOCK);
m_pPlayer->TakeDamage(CWorld::Instance->pev, CWorld::Instance->pev, 50, DMG_SHOCK);
UTIL_ScreenFade(m_pPlayer, Vector(255, 128, 0), 2, 0.5, 128, FFADE_IN);
#endif
SendWeaponAnim(GAUSS_IDLE);
Expand Down
2 changes: 1 addition & 1 deletion dlls/nodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,7 @@ void CTestHull::DropDelay()
{
// UTIL_CenterPrintAll( "Node Graph out of Date. Rebuilding..." );

UTIL_SetOrigin(VARS(pev), WorldGraph.m_pNodes[0].m_vecOrigin);
UTIL_SetOrigin(pev, WorldGraph.m_pNodes[0].m_vecOrigin);

SetThink(&CTestHull::CallBuildNodeGraph);

Expand Down
18 changes: 9 additions & 9 deletions dlls/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ void CBasePlayer::WaterMove()
pev->dmg += 1;
if (pev->dmg > 5)
pev->dmg = 5;
TakeDamage(VARS(eoNullEntity), VARS(eoNullEntity), pev->dmg, DMG_DROWN);
TakeDamage(CWorld::Instance->pev, CWorld::Instance->pev, pev->dmg, DMG_DROWN);
pev->pain_finished = gpGlobals->time + 1;

// track drowning damage, give it back when
Expand Down Expand Up @@ -1148,12 +1148,12 @@ void CBasePlayer::WaterMove()
if (pev->watertype == CONTENT_LAVA) // do damage
{
if (pev->dmgtime < gpGlobals->time)
TakeDamage(VARS(eoNullEntity), VARS(eoNullEntity), 10 * pev->waterlevel, DMG_BURN);
TakeDamage(CWorld::Instance->pev, CWorld::Instance->pev, 10 * pev->waterlevel, DMG_BURN);
}
else if (pev->watertype == CONTENT_SLIME) // do damage
{
pev->dmgtime = gpGlobals->time + 1;
TakeDamage(VARS(eoNullEntity), VARS(eoNullEntity), 4 * pev->waterlevel, DMG_ACID);
TakeDamage(CWorld::Instance->pev, CWorld::Instance->pev, 4 * pev->waterlevel, DMG_ACID);
}

if (!FBitSet(pev->flags, FL_INWATER))
Expand Down Expand Up @@ -2548,7 +2548,7 @@ void CBasePlayer::PostThink()

if (flFallDamage > 0)
{
TakeDamage(VARS(eoNullEntity), VARS(eoNullEntity), flFallDamage, DMG_FALL);
TakeDamage(CWorld::Instance->pev, CWorld::Instance->pev, flFallDamage, DMG_FALL);
pev->punchangle.x = 0;
}
}
Expand Down Expand Up @@ -2740,7 +2740,7 @@ edict_t* EntSelectSpawnPoint(CBaseEntity* pPlayer)
{
// if ent is a client, kill em (unless they are ourselves)
if (ent->IsPlayer() && !(ent->edict() == player))
ent->TakeDamage(VARS(INDEXENT(0)), VARS(INDEXENT(0)), 300, DMG_GENERIC);
ent->TakeDamage(CWorld::Instance->pev, CWorld::Instance->pev, 300, DMG_GENERIC);
}
goto ReturnSpot;
}
Expand All @@ -2764,7 +2764,7 @@ edict_t* EntSelectSpawnPoint(CBaseEntity* pPlayer)
if (FNullEnt(pSpot))
{
ALERT(at_error, "PutClientInServer: no info_player_start on level");
return INDEXENT(0);
return CWorld::Instance->edict();
}

g_pLastSpawn = pSpot;
Expand Down Expand Up @@ -3554,7 +3554,7 @@ void CBasePlayer::CheatImpulseCommands(int iImpulse)
{
TraceResult tr;

edict_t* pWorld = g_engfuncs.pfnPEntityOfEntIndex(0);
edict_t* pWorld = CWorld::Instance->edict();

Vector start = pev->origin + pev->view_ofs;
Vector end = start + gpGlobals->v_forward * 1024;
Expand Down Expand Up @@ -4374,7 +4374,7 @@ Vector CBasePlayer::GetAutoaimVector(float flDelta)

Vector CBasePlayer::AutoaimDeflection(Vector& vecSrc, float flDist, float flDelta)
{
edict_t* pEdict = g_engfuncs.pfnPEntityOfEntIndex(1);
edict_t* pEdict = UTIL_GetEntityList() + 1;
CBaseEntity* pEntity;
float bestdot;
Vector bestdir;
Expand Down Expand Up @@ -4817,7 +4817,7 @@ void CStripWeapons::Use(CBaseEntity* pActivator, CBaseEntity* pCaller, USE_TYPE
}
else if (!g_pGameRules->IsDeathmatch())
{
pPlayer = (CBasePlayer*)CBaseEntity::Instance(g_engfuncs.pfnPEntityOfEntIndex(1));
pPlayer = (CBasePlayer*)UTIL_GetLocalPlayer();
}

if (pPlayer)
Expand Down
8 changes: 4 additions & 4 deletions dlls/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1677,7 +1677,7 @@ float TEXTURETYPE_PlaySound(TraceResult* ptr, Vector vecSrc, Vector vecEnd, int
if (pEntity)
pTextureName = TRACE_TEXTURE(ENT(pEntity->pev), rgfl1, rgfl2);
else
pTextureName = TRACE_TEXTURE(ENT(0), rgfl1, rgfl2);
pTextureName = TRACE_TEXTURE(CWorld::Instance->edict(), rgfl1, rgfl2);

if (pTextureName)
{
Expand Down Expand Up @@ -1804,10 +1804,10 @@ float TEXTURETYPE_PlaySound(TraceResult* ptr, Vector vecSrc, Vector vecEnd, int
switch (RANDOM_LONG(0, 1))
{
case 0:
UTIL_EmitAmbientSound(ENT(0), ptr->vecEndPos, "buttons/spark5.wav", flVolume, ATTN_NORM, 0, 100);
UTIL_EmitAmbientSound(CWorld::Instance->edict(), ptr->vecEndPos, "buttons/spark5.wav", flVolume, ATTN_NORM, 0, 100);
break;
case 1:
UTIL_EmitAmbientSound(ENT(0), ptr->vecEndPos, "buttons/spark6.wav", flVolume, ATTN_NORM, 0, 100);
UTIL_EmitAmbientSound(CWorld::Instance->edict(), ptr->vecEndPos, "buttons/spark6.wav", flVolume, ATTN_NORM, 0, 100);
break;
// case 0: EMIT_SOUND(ENT(pev), CHAN_VOICE, "buttons/spark5.wav", flVolume, ATTN_NORM); break;
// case 1: EMIT_SOUND(ENT(pev), CHAN_VOICE, "buttons/spark6.wav", flVolume, ATTN_NORM); break;
Expand All @@ -1816,7 +1816,7 @@ float TEXTURETYPE_PlaySound(TraceResult* ptr, Vector vecSrc, Vector vecEnd, int
}

// play material hit sound
UTIL_EmitAmbientSound(ENT(0), ptr->vecEndPos, rgsz[RANDOM_LONG(0, cnt - 1)], fvol, fattn, 0, 96 + RANDOM_LONG(0, 0xf));
UTIL_EmitAmbientSound(CWorld::Instance->edict(), ptr->vecEndPos, rgsz[RANDOM_LONG(0, cnt - 1)], fvol, fattn, 0, 96 + RANDOM_LONG(0, 0xf));
//EMIT_SOUND_DYN( ENT(m_pPlayer->pev), CHAN_WEAPON, rgsz[RANDOM_LONG(0,cnt-1)], fvol, ATTN_NORM, 0, 96 + RANDOM_LONG(0,0xf));

return fvolbar;
Expand Down
24 changes: 12 additions & 12 deletions dlls/talkmonster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,15 +480,15 @@ void CTalkMonster::RunTask(Task_t* pTask)
case TASK_TLK_LOOK_AT_CLIENT:
{
// Get edict for one player
edict_t* pPlayer = g_engfuncs.pfnPEntityOfEntIndex(1);
CBaseEntity* pPlayer = UTIL_GetLocalPlayer();

// track head to the client for a while.
if (pPlayer &&
m_MonsterState == MONSTERSTATE_IDLE &&
!IsMoving() &&
!IsTalking())
{
IdleHeadTurn(pPlayer->v.origin);
IdleHeadTurn(pPlayer->pev->origin);
}
else
{
Expand All @@ -500,14 +500,14 @@ void CTalkMonster::RunTask(Task_t* pTask)
if (pTask->iTask == TASK_TLK_CLIENT_STARE)
{
// fail out if the player looks away or moves away.
if ((pPlayer->v.origin - pev->origin).Length2D() > TLK_STARE_DIST)
if ((pPlayer->pev->origin - pev->origin).Length2D() > TLK_STARE_DIST)
{
// player moved away.
TaskFail();
}

UTIL_MakeVectors(pPlayer->v.angles);
if (UTIL_DotPoints(pPlayer->v.origin, pev->origin, gpGlobals->v_forward) < m_flFieldOfView)
UTIL_MakeVectors(pPlayer->pev->angles);
if (UTIL_DotPoints(pPlayer->pev->origin, pev->origin, gpGlobals->v_forward) < m_flFieldOfView)
{
// player looked away
TaskFail();
Expand All @@ -524,13 +524,13 @@ void CTalkMonster::RunTask(Task_t* pTask)
case TASK_FACE_PLAYER:
{
// Get edict for one player
edict_t* pPlayer = g_engfuncs.pfnPEntityOfEntIndex(1);
CBaseEntity* pPlayer = UTIL_GetLocalPlayer();

if (pPlayer)
{
MakeIdealYaw(pPlayer->v.origin);
MakeIdealYaw(pPlayer->pev->origin);
ChangeYaw(pev->yaw_speed);
IdleHeadTurn(pPlayer->v.origin);
IdleHeadTurn(pPlayer->pev->origin);
if (gpGlobals->time > m_flWaitFinished && FlYawDiff() < 10)
{
TaskComplete();
Expand Down Expand Up @@ -1243,14 +1243,14 @@ Schedule_t* CTalkMonster::GetScheduleOfType(int Type)

if (!IsTalking() && HasConditions(bits_COND_SEE_CLIENT) && RANDOM_LONG(0, 6) == 0)
{
edict_t* pPlayer = g_engfuncs.pfnPEntityOfEntIndex(1);
CBaseEntity* pPlayer = UTIL_GetLocalPlayer();

if (pPlayer)
{
// watch the client.
UTIL_MakeVectors(pPlayer->v.angles);
if ((pPlayer->v.origin - pev->origin).Length2D() < TLK_STARE_DIST &&
UTIL_DotPoints(pPlayer->v.origin, pev->origin, gpGlobals->v_forward) >= m_flFieldOfView)
UTIL_MakeVectors(pPlayer->pev->angles);
if ((pPlayer->pev->origin - pev->origin).Length2D() < TLK_STARE_DIST &&
UTIL_DotPoints(pPlayer->pev->origin, pev->origin, gpGlobals->v_forward) >= m_flFieldOfView)
{
// go into the special STARE schedule if the player is close, and looking at me too.
return &slTlkIdleWatchClient[1];
Expand Down
5 changes: 2 additions & 3 deletions dlls/teamplay_gamerules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ CHalfLifeTeamplay::CHalfLifeTeamplay()
// Cache this because the team code doesn't want to deal with changing this in the middle of a game
strncpy(m_szTeamList, teamlist.string, TEAMPLAY_TEAMLISTLENGTH);

edict_t* pWorld = INDEXENT(0);
edict_t* pWorld = CWorld::Instance->edict();
if (pWorld && !FStringNull(pWorld->v.team))
{
if (0 != teamoverride.value)
Expand Down Expand Up @@ -270,8 +270,7 @@ void CHalfLifeTeamplay::ChangePlayerTeam(CBasePlayer* pPlayer, const char* pTeam
m_DisableDeathMessages = true;
m_DisableDeathPenalty = true;

entvars_t* pevWorld = VARS(INDEXENT(0));
pPlayer->TakeDamage(pevWorld, pevWorld, 900, damageFlags);
pPlayer->TakeDamage(CWorld::Instance->pev, CWorld::Instance->pev, 900, damageFlags);

m_DisableDeathMessages = false;
m_DisableDeathPenalty = false;
Expand Down
23 changes: 12 additions & 11 deletions dlls/triggers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,10 +695,8 @@ void CTriggerCDAudio::Use(CBaseEntity* pActivator, CBaseEntity* pCaller, USE_TYP

void PlayCDTrack(int iTrack)
{
edict_t* pClient;

// manually find the single player.
pClient = g_engfuncs.pfnPEntityOfEntIndex(1);
CBaseEntity* pClient = UTIL_GetLocalPlayer();

// Can't play if the client is not connected!
if (!pClient)
Expand All @@ -712,14 +710,14 @@ void PlayCDTrack(int iTrack)

if (iTrack == -1)
{
CLIENT_COMMAND(pClient, "cd stop\n");
CLIENT_COMMAND(pClient->edict(), "cd stop\n");
}
else
{
char string[64];

sprintf(string, "cd play %3d\n", iTrack);
CLIENT_COMMAND(pClient, string);
CLIENT_COMMAND(pClient->edict(), string);
}
}

Expand Down Expand Up @@ -776,18 +774,16 @@ void CTargetCDAudio::Use(CBaseEntity* pActivator, CBaseEntity* pCaller, USE_TYPE
// only plays for ONE client, so only use in single play!
void CTargetCDAudio::Think()
{
edict_t* pClient;

// manually find the single player.
pClient = g_engfuncs.pfnPEntityOfEntIndex(1);
CBaseEntity* pClient = UTIL_GetLocalPlayer();

// Can't play if the client is not connected!
if (!pClient)
return;

pev->nextthink = gpGlobals->time + 0.5;

if ((pClient->v.origin - pev->origin).Length() <= pev->scale)
if ((pClient->pev->origin - pev->origin).Length() <= pev->scale)
Play();
}

Expand Down Expand Up @@ -1495,7 +1491,7 @@ void CChangeLevel::ChangeLevelNow(CBaseEntity* pActivator)
pev->dmgtime = gpGlobals->time;


CBaseEntity* pPlayer = CBaseEntity::Instance(g_engfuncs.pfnPEntityOfEntIndex(1));
CBaseEntity* pPlayer = UTIL_GetLocalPlayer();
if (!InTransitionVolume(pPlayer, m_szLandmarkName))
{
ALERT(at_aiconsole, "Player isn't in the transition volume %s, aborting\n", m_szLandmarkName);
Expand Down Expand Up @@ -2248,7 +2244,12 @@ void CTriggerCamera::Use(CBaseEntity* pActivator, CBaseEntity* pCaller, USE_TYPE
}
if (!pActivator || !pActivator->IsPlayer())
{
pActivator = CBaseEntity::Instance(g_engfuncs.pfnPEntityOfEntIndex(1));
pActivator = UTIL_GetLocalPlayer();

if (!pActivator)
{
return;
}
}

auto player = static_cast<CBasePlayer*>(pActivator);
Expand Down
Loading

0 comments on commit f2c8e23

Please sign in to comment.