ddnet/src/game/server/entity.cpp

96 lines
3 KiB
C++
Raw Normal View History

2010-11-20 10:37:14 +00:00
/* (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. */
2010-05-29 07:25:38 +00:00
#include "entity.h"
#include "gamecontext.h"
#include "player.h"
//////////////////////////////////////////////////
// Entity
//////////////////////////////////////////////////
CEntity::CEntity(CGameWorld *pGameWorld, int ObjType, vec2 Pos, int ProximityRadius)
{
2010-05-29 07:25:38 +00:00
m_pGameWorld = pGameWorld;
m_ObjType = ObjType;
m_Pos = Pos;
m_ProximityRadius = ProximityRadius;
m_MarkedForDestroy = false;
m_ID = Server()->SnapNewID();
2010-05-29 07:25:38 +00:00
m_pPrevTypeEntity = 0;
m_pNextTypeEntity = 0;
}
2010-05-29 07:25:38 +00:00
CEntity::~CEntity()
{
2010-05-29 07:25:38 +00:00
GameWorld()->RemoveEntity(this);
Server()->SnapFreeID(m_ID);
}
2021-12-08 17:02:04 +00:00
bool CEntity::NetworkClipped(int SnappingClient) const
{
2021-12-08 17:02:04 +00:00
return ::NetworkClipped(m_pGameWorld->GameServer(), SnappingClient, m_Pos);
}
2021-12-08 17:02:04 +00:00
bool CEntity::NetworkClipped(int SnappingClient, vec2 CheckPos) const
{
2021-12-08 17:02:04 +00:00
return ::NetworkClipped(m_pGameWorld->GameServer(), SnappingClient, CheckPos);
}
bool CEntity::GameLayerClipped(vec2 CheckPos)
{
return round_to_int(CheckPos.x) / 32 < -200 || round_to_int(CheckPos.x) / 32 > GameServer()->Collision()->GetWidth() + 200 ||
2022-01-22 16:34:23 +00:00
round_to_int(CheckPos.y) / 32 < -200 || round_to_int(CheckPos.y) / 32 > GameServer()->Collision()->GetHeight() + 200;
}
2018-02-14 15:27:26 +00:00
bool CEntity::GetNearestAirPos(vec2 Pos, vec2 PrevPos, vec2 *pOutPos)
2018-07-14 23:04:26 +00:00
{
for(int k = 0; k < 16 && GameServer()->Collision()->CheckPoint(Pos); k++)
2018-07-15 09:58:12 +00:00
{
Pos -= normalize(PrevPos - Pos);
2018-07-14 23:04:26 +00:00
}
vec2 PosInBlock = vec2(round_to_int(Pos.x) % 32, round_to_int(Pos.y) % 32);
vec2 BlockCenter = vec2(round_to_int(Pos.x), round_to_int(Pos.y)) - PosInBlock + vec2(16.0f, 16.0f);
*pOutPos = vec2(BlockCenter.x + (PosInBlock.x < 16 ? -2.0f : 1.0f), Pos.y);
if(!GameServer()->Collision()->TestBox(*pOutPos, vec2(28.0f, 28.0f)))
2018-07-14 23:04:26 +00:00
return true;
*pOutPos = vec2(Pos.x, BlockCenter.y + (PosInBlock.y < 16 ? -2.0f : 1.0f));
if(!GameServer()->Collision()->TestBox(*pOutPos, vec2(28.0f, 28.0f)))
2018-07-14 23:04:26 +00:00
return true;
*pOutPos = vec2(BlockCenter.x + (PosInBlock.x < 16 ? -2.0f : 1.0f),
BlockCenter.y + (PosInBlock.y < 16 ? -2.0f : 1.0f));
2022-01-22 16:34:23 +00:00
return !GameServer()->Collision()->TestBox(*pOutPos, vec2(28.0f, 28.0f));
2018-07-14 23:04:26 +00:00
}
bool CEntity::GetNearestAirPosPlayer(vec2 PlayerPos, vec2 *OutPos)
2018-02-14 15:27:26 +00:00
{
for(int dist = 5; dist >= -1; dist--)
2018-02-14 15:27:26 +00:00
{
*OutPos = vec2(PlayerPos.x, PlayerPos.y - dist);
if(!GameServer()->Collision()->TestBox(*OutPos, vec2(28.0f, 28.0f)))
2018-02-14 15:27:26 +00:00
{
return true;
}
}
return false;
}
2021-12-08 17:02:04 +00:00
bool NetworkClipped(const CGameContext *pGameServer, int SnappingClient, vec2 CheckPos)
{
if(SnappingClient == SERVER_DEMO_CLIENT || 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;
2022-01-22 16:34:23 +00:00
return absolute(dy) > pGameServer->m_apPlayers[SnappingClient]->m_ShowDistance.y;
}