Fix spectator check, small refactoring (#265)

* Fix spectator check, small refactoring

* Update observer.cpp

minor refactor-patch
This commit is contained in:
In-line 2018-02-08 13:37:02 +04:00 committed by Dmitry Novikov
parent ff3891d98b
commit 40f3bc488b
5 changed files with 45 additions and 45 deletions

View File

@ -362,7 +362,7 @@ void EXT_FUNC ClientKill(edict_t *pEntity)
entvars_t *pev = &pEntity->v; entvars_t *pev = &pEntity->v;
CBasePlayer *pPlayer = CBasePlayer::Instance(pev); CBasePlayer *pPlayer = CBasePlayer::Instance(pev);
if (pPlayer->IsObserver()) if (pPlayer->GetObserverMode() != OBS_NONE)
return; return;
if (pPlayer->m_iJoiningState != JOINED) if (pPlayer->m_iJoiningState != JOINED)
@ -3093,7 +3093,7 @@ void EXT_FUNC InternalCommand(edict_t *pEntity, const char *pcmd, const char *pa
{ {
// new spectator mode // new spectator mode
int mode = Q_atoi(parg1); int mode = Q_atoi(parg1);
if (pPlayer->IsObserver() && pPlayer->CanSwitchObserverModes()) if (pPlayer->GetObserverMode() != OBS_NONE && pPlayer->CanSwitchObserverModes())
pPlayer->Observer_SetMode(mode); pPlayer->Observer_SetMode(mode);
else else
pPlayer->m_iObserverLastMode = mode; pPlayer->m_iObserverLastMode = mode;
@ -3113,14 +3113,14 @@ void EXT_FUNC InternalCommand(edict_t *pEntity, const char *pcmd, const char *pa
{ {
// follow next player // follow next player
int arg = Q_atoi(parg1); int arg = Q_atoi(parg1);
if (pPlayer->IsObserver() && pPlayer->CanSwitchObserverModes()) if (pPlayer->GetObserverMode() != OBS_NONE && pPlayer->CanSwitchObserverModes())
{ {
pPlayer->Observer_FindNextPlayer(arg != 0); pPlayer->Observer_FindNextPlayer(arg != 0);
} }
} }
else if (FStrEq(pcmd, "follow")) else if (FStrEq(pcmd, "follow"))
{ {
if (pPlayer->IsObserver() && pPlayer->CanSwitchObserverModes()) if (pPlayer->GetObserverMode() != OBS_NONE && pPlayer->CanSwitchObserverModes())
{ {
pPlayer->Observer_FindNextPlayer(false, parg1); pPlayer->Observer_FindNextPlayer(false, parg1);
} }

View File

@ -97,32 +97,32 @@ bool CCStrikeGameMgrHelper::__API_HOOK(CanPlayerHearPlayer)(CBasePlayer *pListen
case 2: case 2:
return (pListener->m_iTeam == pSender->m_iTeam); return (pListener->m_iTeam == pSender->m_iTeam);
case 3: case 3:
return (pListener->m_iTeam == pSender->m_iTeam || pListener->IsObserver()); return (pListener->m_iTeam == pSender->m_iTeam || pListener->m_iTeam == SPECTATOR || pListener->m_iTeam == UNASSIGNED);
case 4: case 4:
return (pListener->IsAlive() == pSender->IsAlive() || pSender->IsAlive()); return (pListener->IsAlive() == pSender->IsAlive() || pSender->IsAlive());
default: // Default behavior
break;
#endif #endif
} default:
{
if ( if (
#ifndef REGAMEDLL_FIXES #ifndef REGAMEDLL_FIXES
!pSender->IsPlayer() || !pSender->IsPlayer() ||
#endif #endif
pListener->m_iTeam != pSender->m_iTeam) // Different teams can't hear each other pListener->m_iTeam != pSender->m_iTeam) // Different teams can't hear each other
{ {
return false; return false;
}
if (pListener->GetObserverMode() != OBS_NONE) // 2 spectators don't need isAlive() checks.
{
return true;
}
BOOL bListenerAlive = pListener->IsAlive();
BOOL bSenderAlive = pSender->IsAlive();
return (bListenerAlive == bSenderAlive || bSenderAlive); // Dead/alive voice chats are separated, but dead can hear alive.
} }
if (pListener->IsObserver()) // 2 spectators don't need isAlive() checks.
{
return true;
} }
BOOL bListenerAlive = pListener->IsAlive();
BOOL bSenderAlive = pSender->IsAlive();
return (bListenerAlive == bSenderAlive || bSenderAlive); // Dead/alive voice chats are separated, but dead can hear alive.
} }
void Broadcast(const char *sentence) void Broadcast(const char *sentence)

View File

@ -29,7 +29,7 @@ CBasePlayer *CBasePlayer::__API_HOOK(Observer_IsValidTarget)(int iPlayerIndex, b
CBasePlayer *pPlayer = UTIL_PlayerByIndex(iPlayerIndex); CBasePlayer *pPlayer = UTIL_PlayerByIndex(iPlayerIndex);
// Don't spec observers or players who haven't picked a class yet // Don't spec observers or players who haven't picked a class yet
if (!pPlayer || pPlayer == this || pPlayer->has_disconnected || pPlayer->IsObserver() || (pPlayer->pev->effects & EF_NODRAW) || pPlayer->m_iTeam == UNASSIGNED || (bSameTeam && pPlayer->m_iTeam != m_iTeam)) if (!pPlayer || pPlayer == this || pPlayer->has_disconnected || pPlayer->GetObserverMode() != OBS_NONE || (pPlayer->pev->effects & EF_NODRAW) || pPlayer->m_iTeam == UNASSIGNED || (bSameTeam && pPlayer->m_iTeam != m_iTeam))
return nullptr; return nullptr;
return pPlayer; return pPlayer;
@ -42,7 +42,7 @@ void UpdateClientEffects(CBasePlayer *pObserver, int oldMode)
bool blindnessOk = (fadetoblack.value == 0); bool blindnessOk = (fadetoblack.value == 0);
bool clearNightvision = false; bool clearNightvision = false;
if (pObserver->IsObserver() == OBS_IN_EYE) if (pObserver->GetObserverMode() == OBS_IN_EYE)
{ {
clearProgress = true; clearProgress = true;
clearBlindness = true; clearBlindness = true;
@ -424,14 +424,14 @@ void CBasePlayer::Observer_CheckProperties()
// Attempt to change the observer mode // Attempt to change the observer mode
void CBasePlayer::Observer_SetMode(int iMode) void CBasePlayer::Observer_SetMode(int iMode)
{ {
int _forcecamera; int forcecamera;
int oldMode; int oldMode;
// Just abort if we're changing to the mode we're already in // Just abort if we're changing to the mode we're already in
if (iMode == pev->iuser1) if (iMode == pev->iuser1)
return; return;
_forcecamera = GetForceCamera(this); forcecamera = GetForceCamera(this);
// is valid mode ? // is valid mode ?
if (iMode < OBS_CHASE_LOCKED || iMode > OBS_MAP_CHASE) if (iMode < OBS_CHASE_LOCKED || iMode > OBS_MAP_CHASE)
@ -441,21 +441,21 @@ void CBasePlayer::Observer_SetMode(int iMode)
if (m_iTeam != SPECTATOR) if (m_iTeam != SPECTATOR)
{ {
if (_forcecamera == CAMERA_MODE_SPEC_ONLY_TEAM) if (forcecamera == CAMERA_MODE_SPEC_ONLY_TEAM)
{ {
if (iMode == OBS_ROAMING) if (iMode == OBS_ROAMING)
iMode = OBS_MAP_FREE; iMode = OBS_MAP_FREE;
} }
else if (_forcecamera == CAMERA_MODE_SPEC_ONLY_FRIST_PERSON) else if (forcecamera == CAMERA_MODE_SPEC_ONLY_FRIST_PERSON)
iMode = OBS_IN_EYE; iMode = OBS_IN_EYE;
} }
// verify observer target again // verify observer target again
if (m_hObserverTarget) if (m_hObserverTarget)
{ {
CBaseEntity *pEnt = m_hObserverTarget; CBasePlayer *pTarget = m_hObserverTarget;
if (pEnt == this || !pEnt || pEnt->has_disconnected || ((CBasePlayer *)pEnt)->IsObserver() || (pEnt->pev->effects & EF_NODRAW) || (_forcecamera != CAMERA_MODE_SPEC_ANYONE && ((CBasePlayer *)pEnt)->m_iTeam != m_iTeam)) if (pTarget == this || !pTarget || pTarget->has_disconnected || pTarget->GetObserverMode() != OBS_NONE || (pTarget->pev->effects & EF_NODRAW) || (forcecamera != CAMERA_MODE_SPEC_ANYONE && pTarget->m_iTeam != m_iTeam))
m_hObserverTarget = nullptr; m_hObserverTarget = nullptr;
} }

View File

@ -317,7 +317,7 @@ CBasePlayer *CBasePlayer::GetNextRadioRecipient(CBasePlayer *pStartPlayer)
} }
else if (pPlayer) else if (pPlayer)
{ {
int iSpecMode = IsObserver(); int iSpecMode = GetObserverMode();
if (iSpecMode != OBS_CHASE_LOCKED && iSpecMode != OBS_CHASE_FREE && iSpecMode != OBS_IN_EYE) if (iSpecMode != OBS_CHASE_LOCKED && iSpecMode != OBS_CHASE_FREE && iSpecMode != OBS_IN_EYE)
continue; continue;
@ -378,7 +378,7 @@ void EXT_FUNC CBasePlayer::__API_HOOK(Radio)(const char *msg_id, const char *msg
else else
{ {
// do this when spectator mode is in // do this when spectator mode is in
int iSpecMode = pPlayer->IsObserver(); int iSpecMode = pPlayer->GetObserverMode();
if (iSpecMode != OBS_CHASE_LOCKED && iSpecMode != OBS_CHASE_FREE && iSpecMode != OBS_IN_EYE) if (iSpecMode != OBS_CHASE_LOCKED && iSpecMode != OBS_CHASE_FREE && iSpecMode != OBS_IN_EYE)
continue; continue;
@ -1587,7 +1587,7 @@ void CBasePlayer::SetProgressBarTime(int time)
CBasePlayer *pPlayer = GetClassPtr<CCSPlayer>((CBasePlayer *)pEntity->pev); CBasePlayer *pPlayer = GetClassPtr<CCSPlayer>((CBasePlayer *)pEntity->pev);
if (pPlayer->IsObserver() == OBS_IN_EYE && pPlayer->pev->iuser2 == playerIndex) if (pPlayer->GetObserverMode() == OBS_IN_EYE && pPlayer->pev->iuser2 == playerIndex)
{ {
MESSAGE_BEGIN(MSG_ONE, gmsgBarTime, nullptr, pPlayer->pev); MESSAGE_BEGIN(MSG_ONE, gmsgBarTime, nullptr, pPlayer->pev);
WRITE_SHORT(time); WRITE_SHORT(time);
@ -1627,7 +1627,7 @@ void CBasePlayer::SetProgressBarTime2(int time, float timeElapsed)
CBasePlayer *pPlayer = GetClassPtr<CCSPlayer>((CBasePlayer *)pEntity->pev); CBasePlayer *pPlayer = GetClassPtr<CCSPlayer>((CBasePlayer *)pEntity->pev);
if (pPlayer->IsObserver() == OBS_IN_EYE && pPlayer->pev->iuser2 == playerIndex) if (pPlayer->GetObserverMode() == OBS_IN_EYE && pPlayer->pev->iuser2 == playerIndex)
{ {
MESSAGE_BEGIN(MSG_ONE, gmsgBarTime2, nullptr, pPlayer->pev); MESSAGE_BEGIN(MSG_ONE, gmsgBarTime2, nullptr, pPlayer->pev);
WRITE_SHORT(time); WRITE_SHORT(time);
@ -3176,7 +3176,7 @@ void CBasePlayer::SyncRoundTimer()
if (!g_pGameRules->IsMultiplayer()) if (!g_pGameRules->IsMultiplayer())
return; return;
if (bFreezePeriod && TheTutor && !IsObserver()) if (bFreezePeriod && TheTutor && GetObserverMode() == OBS_NONE)
{ {
MESSAGE_BEGIN(MSG_ONE, gmsgBlinkAcct, nullptr, pev); MESSAGE_BEGIN(MSG_ONE, gmsgBlinkAcct, nullptr, pev);
WRITE_BYTE(MONEY_BLINK_AMOUNT); WRITE_BYTE(MONEY_BLINK_AMOUNT);
@ -3534,7 +3534,7 @@ void CBasePlayer::PlayerDeathThink()
if (pev->deadflag == DEAD_RESPAWNABLE) if (pev->deadflag == DEAD_RESPAWNABLE)
{ {
#ifdef REGAMEDLL_FIXES #ifdef REGAMEDLL_FIXES
if (IsObserver() && (m_iTeam == UNASSIGNED || m_iTeam == SPECTATOR)) if (GetObserverMode() != OBS_NONE && (m_iTeam == UNASSIGNED || m_iTeam == SPECTATOR))
return; return;
// Player cannot respawn while in the Choose Appearance menu // Player cannot respawn while in the Choose Appearance menu
@ -4230,7 +4230,7 @@ void EXT_FUNC CBasePlayer::__API_HOOK(PreThink)()
#endif #endif
// Observer Button Handling // Observer Button Handling
if (IsObserver() && (m_afPhysicsFlags & PFLAG_OBSERVER)) if (GetObserverMode() != OBS_NONE && (m_afPhysicsFlags & PFLAG_OBSERVER))
{ {
Observer_Think(); Observer_Think();
return; return;
@ -7011,7 +7011,7 @@ void EXT_FUNC CBasePlayer::__API_HOOK(ResetMaxSpeed)()
{ {
float speed; float speed;
if (IsObserver()) if (GetObserverMode() != OBS_NONE)
{ {
// Player gets speed bonus in observer mode // Player gets speed bonus in observer mode
speed = 900; speed = 900;
@ -7197,9 +7197,9 @@ void CBasePlayer::UpdateStatusBar()
newSBarState[SBAR_ID_TARGETNAME] = ENTINDEX(pTarget->edict()); newSBarState[SBAR_ID_TARGETNAME] = ENTINDEX(pTarget->edict());
newSBarState[SBAR_ID_TARGETTYPE] = (pTarget->m_iTeam == m_iTeam) ? SBAR_TARGETTYPE_TEAMMATE : SBAR_TARGETTYPE_ENEMY; newSBarState[SBAR_ID_TARGETTYPE] = (pTarget->m_iTeam == m_iTeam) ? SBAR_TARGETTYPE_TEAMMATE : SBAR_TARGETTYPE_ENEMY;
if (pTarget->m_iTeam == m_iTeam || IsObserver()) if (pTarget->m_iTeam == m_iTeam || GetObserverMode() != OBS_NONE)
{ {
if (playerid.value != PLAYERID_MODE_OFF || IsObserver()) if (playerid.value != PLAYERID_MODE_OFF || GetObserverMode() != OBS_NONE)
Q_strcpy(sbuf0, "1 %c1: %p2\n2 %h: %i3%%"); Q_strcpy(sbuf0, "1 %c1: %p2\n2 %h: %i3%%");
else else
Q_strcpy(sbuf0, " "); Q_strcpy(sbuf0, " ");
@ -7212,7 +7212,7 @@ void CBasePlayer::UpdateStatusBar()
HintMessage("#Hint_spotted_a_friend"); HintMessage("#Hint_spotted_a_friend");
} }
} }
else if (!IsObserver()) else if (GetObserverMode() == OBS_NONE)
{ {
if (playerid.value != PLAYERID_MODE_TEAMONLY && playerid.value != PLAYERID_MODE_OFF) if (playerid.value != PLAYERID_MODE_TEAMONLY && playerid.value != PLAYERID_MODE_OFF)
Q_strcpy(sbuf0, "1 %c1: %p2"); Q_strcpy(sbuf0, "1 %c1: %p2");
@ -7230,7 +7230,7 @@ void CBasePlayer::UpdateStatusBar()
} }
else if (pEntity->Classify() == CLASS_HUMAN_PASSIVE) else if (pEntity->Classify() == CLASS_HUMAN_PASSIVE)
{ {
if (playerid.value != PLAYERID_MODE_OFF || IsObserver()) if (playerid.value != PLAYERID_MODE_OFF || GetObserverMode() != OBS_NONE)
Q_strcpy(sbuf0, "1 %c1 %h: %i3%%"); Q_strcpy(sbuf0, "1 %c1 %h: %i3%%");
else else
Q_strcpy(sbuf0, " "); Q_strcpy(sbuf0, " ");
@ -9125,7 +9125,7 @@ bool CBasePlayer::IsObservingPlayer(CBasePlayer *pPlayer)
if (FNullEnt(pPlayer)) if (FNullEnt(pPlayer))
return false; return false;
return (IsObserver() == OBS_IN_EYE && pev->iuser2 == pPlayer->entindex()) != 0; return (GetObserverMode() == OBS_IN_EYE && pev->iuser2 == pPlayer->entindex()) != 0;
} }
void CBasePlayer::UpdateLocation(bool forceUpdate) void CBasePlayer::UpdateLocation(bool forceUpdate)
@ -9481,7 +9481,7 @@ bool EXT_FUNC CBasePlayer::__API_HOOK(GetIntoGame)()
void CBasePlayer::PlayerRespawnThink() void CBasePlayer::PlayerRespawnThink()
{ {
#ifdef REGAMEDLL_ADD #ifdef REGAMEDLL_ADD
if (IsObserver() && (m_iTeam == UNASSIGNED || m_iTeam == SPECTATOR)) if (GetObserverMode() != OBS_NONE && (m_iTeam == UNASSIGNED || m_iTeam == SPECTATOR))
return; return;
// Player cannot respawn while in the Choose Appearance menu // Player cannot respawn while in the Choose Appearance menu

View File

@ -438,7 +438,7 @@ public:
void Observer_SetMode(int iMode); void Observer_SetMode(int iMode);
void Observer_CheckTarget(); void Observer_CheckTarget();
void Observer_CheckProperties(); void Observer_CheckProperties();
int IsObserver() { return pev->iuser1; } int GetObserverMode() { return pev->iuser1; }
void PlantC4(); void PlantC4();
void Radio(const char *msg_id, const char *msg_verbose = nullptr, short pitch = 100, bool showIcon = true); void Radio(const char *msg_id, const char *msg_verbose = nullptr, short pitch = 100, bool showIcon = true);
CBasePlayer *GetNextRadioRecipient(CBasePlayer *pStartPlayer); CBasePlayer *GetNextRadioRecipient(CBasePlayer *pStartPlayer);