From cbda5b720a8c25ebdfb4ac929e7be0a59e5eabf2 Mon Sep 17 00:00:00 2001 From: def Date: Mon, 11 Jan 2021 18:28:14 +0100 Subject: [PATCH] Make netclipping of entities and events respect shown distance (fixes #3420) --- src/game/server/entities/character.cpp | 21 ---------------- src/game/server/entities/character.h | 2 -- src/game/server/entity.cpp | 35 +++++++++++++++----------- src/game/server/entity.h | 11 +++++--- src/game/server/eventhandler.cpp | 4 ++- src/game/server/eventhandler.h | 3 ++- 6 files changed, 32 insertions(+), 44 deletions(-) diff --git a/src/game/server/entities/character.cpp b/src/game/server/entities/character.cpp index 8ec28a7fd..3e66e1bbc 100644 --- a/src/game/server/entities/character.cpp +++ b/src/game/server/entities/character.cpp @@ -1293,27 +1293,6 @@ void CCharacter::Snap(int SnappingClient) pDDNetCharacter->m_StrongWeakID = m_StrongWeakID; } -int CCharacter::NetworkClipped(int SnappingClient) -{ - return NetworkClipped(SnappingClient, m_Pos); -} - -int CCharacter::NetworkClipped(int SnappingClient, vec2 CheckPos) -{ - if(SnappingClient == -1 || GameServer()->m_apPlayers[SnappingClient]->m_ShowAll) - return 0; - - float dx = GameServer()->m_apPlayers[SnappingClient]->m_ViewPos.x - CheckPos.x; - if(absolute(dx) > GameServer()->m_apPlayers[SnappingClient]->m_ShowDistance.x) - return 1; - - float dy = GameServer()->m_apPlayers[SnappingClient]->m_ViewPos.y - CheckPos.y; - if(absolute(dy) > GameServer()->m_apPlayers[SnappingClient]->m_ShowDistance.y) - return 1; - - return 0; -} - // DDRace bool CCharacter::CanCollide(int ClientID) diff --git a/src/game/server/entities/character.h b/src/game/server/entities/character.h index c61747c8b..edbe11e87 100644 --- a/src/game/server/entities/character.h +++ b/src/game/server/entities/character.h @@ -40,8 +40,6 @@ public: virtual void TickDefered(); virtual void TickPaused(); virtual void Snap(int SnappingClient); - virtual int NetworkClipped(int SnappingClient); - virtual int NetworkClipped(int SnappingClient, vec2 CheckPos); bool IsGrounded(); diff --git a/src/game/server/entity.cpp b/src/game/server/entity.cpp index 6a7944034..82e3feeed 100644 --- a/src/game/server/entity.cpp +++ b/src/game/server/entity.cpp @@ -29,25 +29,14 @@ CEntity::~CEntity() Server()->SnapFreeID(m_ID); } -int CEntity::NetworkClipped(int SnappingClient) +bool CEntity::NetworkClipped(int SnappingClient) { - return NetworkClipped(SnappingClient, m_Pos); + return ::NetworkClipped(GameServer(), SnappingClient, m_Pos); } -int CEntity::NetworkClipped(int SnappingClient, vec2 CheckPos) +bool CEntity::NetworkClipped(int SnappingClient, vec2 CheckPos) { - if(SnappingClient == -1) - return 0; - - float dx = GameServer()->m_apPlayers[SnappingClient]->m_ViewPos.x - CheckPos.x; - float dy = GameServer()->m_apPlayers[SnappingClient]->m_ViewPos.y - CheckPos.y; - - if(absolute(dx) > 1000.0f || absolute(dy) > 800.0f) - return 1; - - if(distance(GameServer()->m_apPlayers[SnappingClient]->m_ViewPos, CheckPos) > 4000.0f) - return 1; - return 0; + return ::NetworkClipped(GameServer(), SnappingClient, CheckPos); } bool CEntity::GameLayerClipped(vec2 CheckPos) @@ -96,3 +85,19 @@ bool CEntity::GetNearestAirPosPlayer(vec2 PlayerPos, vec2 *OutPos) } return false; } + +bool NetworkClipped(CGameContext *pGameServer, int SnappingClient, vec2 CheckPos) +{ + if(SnappingClient == -1 || pGameServer->m_apPlayers[SnappingClient]->m_ShowAll) + return false; + + float dx = pGameServer->m_apPlayers[SnappingClient]->m_ViewPos.x - CheckPos.x; + if(absolute(dx) > pGameServer->m_apPlayers[SnappingClient]->m_ShowDistance.x) + return true; + + float dy = pGameServer->m_apPlayers[SnappingClient]->m_ViewPos.y - CheckPos.y; + if(absolute(dy) > pGameServer->m_apPlayers[SnappingClient]->m_ShowDistance.y) + return true; + + return false; +} diff --git a/src/game/server/entity.h b/src/game/server/entity.h index 1c314a225..b92837f2f 100644 --- a/src/game/server/entity.h +++ b/src/game/server/entity.h @@ -6,6 +6,7 @@ #include #include "alloc.h" +#include "gamecontext.h" #include "gameworld.h" /* @@ -119,7 +120,7 @@ public: virtual void Snap(int SnappingClient) {} /* - Function: networkclipped(int snapping_client) + Function: NetworkClipped Performs a series of test to see if a client can see the entity. @@ -130,10 +131,10 @@ public: recording. Returns: - Non-zero if the entity doesn't have to be in the snapshot. + True if the entity doesn't have to be in the snapshot. */ - virtual int NetworkClipped(int SnappingClient); - virtual int NetworkClipped(int SnappingClient, vec2 CheckPos); + bool NetworkClipped(int SnappingClient); + bool NetworkClipped(int SnappingClient, vec2 CheckPos); bool GameLayerClipped(vec2 CheckPos); @@ -146,4 +147,6 @@ public: int m_Layer; }; +bool NetworkClipped(CGameContext *pGameServer, int SnappingClient, vec2 CheckPos); + #endif diff --git a/src/game/server/eventhandler.cpp b/src/game/server/eventhandler.cpp index 30e0e40e4..e8e31a17b 100644 --- a/src/game/server/eventhandler.cpp +++ b/src/game/server/eventhandler.cpp @@ -1,6 +1,8 @@ /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ /* If you are missing that file, acquire a complete release at teeworlds.com. */ #include "eventhandler.h" + +#include "entity.h" #include "gamecontext.h" #include "player.h" @@ -48,7 +50,7 @@ void CEventHandler::Snap(int SnappingClient) if(SnappingClient == -1 || CmaskIsSet(m_aClientMasks[i], SnappingClient)) { CNetEvent_Common *ev = (CNetEvent_Common *)&m_aData[m_aOffsets[i]]; - if(SnappingClient == -1 || distance(GameServer()->m_apPlayers[SnappingClient]->m_ViewPos, vec2(ev->m_X, ev->m_Y)) < 1500.0f) + if(!NetworkClipped(GameServer(), SnappingClient, vec2(ev->m_X, ev->m_Y))) { int Type = m_aTypes[i]; int Size = m_aSizes[i]; diff --git a/src/game/server/eventhandler.h b/src/game/server/eventhandler.h index 79c96ffca..13094e6d7 100644 --- a/src/game/server/eventhandler.h +++ b/src/game/server/eventhandler.h @@ -4,7 +4,8 @@ #define GAME_SERVER_EVENTHANDLER_H #include -// +#include + class CEventHandler { static const int MAX_EVENTS = 128;