1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3
4#include "entity.h"
5
6#include "gamecontext.h"
7#include "player.h"
8
9#include <game/server/gameworld.h>
10
11//////////////////////////////////////////////////
12// Entity
13//////////////////////////////////////////////////
14CEntity::CEntity(CGameWorld *pGameWorld, int ObjType, bool SnapFreeId, vec2 Pos, int ProximityRadius)
15{
16 m_pGameWorld = pGameWorld;
17 m_pCCollision = GameServer()->Collision();
18
19 m_ObjType = ObjType;
20 m_Pos = Pos;
21 m_ProximityRadius = ProximityRadius;
22
23 m_MarkedForDestroy = false;
24 if(SnapFreeId)
25 m_Id = Server()->SnapNewId();
26
27 m_pPrevTypeEntity = nullptr;
28 m_pNextTypeEntity = nullptr;
29}
30
31CEntity::~CEntity()
32{
33 GameWorld()->RemoveEntity(pEntity: this);
34 if(m_Id.has_value())
35 Server()->SnapFreeId(Id: m_Id.value());
36}
37
38bool CEntity::NetworkClipped(int SnappingClient) const
39{
40 return ::NetworkClipped(pGameServer: m_pGameWorld->GameServer(), SnappingClient, CheckPos: m_Pos);
41}
42
43bool CEntity::NetworkClipped(int SnappingClient, vec2 CheckPos) const
44{
45 return ::NetworkClipped(pGameServer: m_pGameWorld->GameServer(), SnappingClient, CheckPos);
46}
47
48bool CEntity::NetworkClippedLine(int SnappingClient, vec2 StartPos, vec2 EndPos) const
49{
50 return ::NetworkClippedLine(pGameServer: m_pGameWorld->GameServer(), SnappingClient, StartPos, EndPos);
51}
52
53bool CEntity::GameLayerClipped(vec2 CheckPos)
54{
55 return round_to_int(f: CheckPos.x) / 32 < -200 || round_to_int(f: CheckPos.x) / 32 > GameServer()->Collision()->GetWidth() + 200 ||
56 round_to_int(f: CheckPos.y) / 32 < -200 || round_to_int(f: CheckPos.y) / 32 > GameServer()->Collision()->GetHeight() + 200;
57}
58
59bool CEntity::GetNearestAirPos(vec2 Pos, vec2 PrevPos, vec2 *pOutPos)
60{
61 for(int k = 0; k < 16 && GameServer()->Collision()->CheckPoint(Pos); k++)
62 {
63 Pos -= normalize(v: PrevPos - Pos);
64 }
65
66 vec2 PosInBlock = vec2(round_to_int(f: Pos.x) % 32, round_to_int(f: Pos.y) % 32);
67 vec2 BlockCenter = vec2(round_to_int(f: Pos.x), round_to_int(f: Pos.y)) - PosInBlock + vec2(16.0f, 16.0f);
68
69 *pOutPos = vec2(BlockCenter.x + (PosInBlock.x < 16 ? -2.0f : 1.0f), Pos.y);
70 if(!GameServer()->Collision()->TestBox(Pos: *pOutPos, Size: CCharacterCore::PhysicalSizeVec2()))
71 return true;
72
73 *pOutPos = vec2(Pos.x, BlockCenter.y + (PosInBlock.y < 16 ? -2.0f : 1.0f));
74 if(!GameServer()->Collision()->TestBox(Pos: *pOutPos, Size: CCharacterCore::PhysicalSizeVec2()))
75 return true;
76
77 *pOutPos = vec2(BlockCenter.x + (PosInBlock.x < 16 ? -2.0f : 1.0f),
78 BlockCenter.y + (PosInBlock.y < 16 ? -2.0f : 1.0f));
79 return !GameServer()->Collision()->TestBox(Pos: *pOutPos, Size: CCharacterCore::PhysicalSizeVec2());
80}
81
82bool CEntity::GetNearestAirPosPlayer(vec2 PlayerPos, vec2 *pOutPos)
83{
84 for(int Distance = 5; Distance >= -1; Distance--)
85 {
86 *pOutPos = vec2(PlayerPos.x, PlayerPos.y - Distance);
87 if(!GameServer()->Collision()->TestBox(Pos: *pOutPos, Size: CCharacterCore::PhysicalSizeVec2()))
88 {
89 return true;
90 }
91 }
92 return false;
93}
94
95bool NetworkClipped(const CGameContext *pGameServer, int SnappingClient, vec2 CheckPos)
96{
97 if(SnappingClient == SERVER_DEMO_CLIENT || pGameServer->m_apPlayers[SnappingClient]->m_ShowAll)
98 return false;
99
100 float dx = pGameServer->m_apPlayers[SnappingClient]->m_ViewPos.x - CheckPos.x;
101 if(absolute(a: dx) > pGameServer->m_apPlayers[SnappingClient]->m_ShowDistance.x)
102 return true;
103
104 float dy = pGameServer->m_apPlayers[SnappingClient]->m_ViewPos.y - CheckPos.y;
105 return absolute(a: dy) > pGameServer->m_apPlayers[SnappingClient]->m_ShowDistance.y;
106}
107
108bool NetworkClippedLine(const CGameContext *pGameServer, int SnappingClient, vec2 StartPos, vec2 EndPos)
109{
110 if(SnappingClient == SERVER_DEMO_CLIENT || pGameServer->m_apPlayers[SnappingClient]->m_ShowAll)
111 return false;
112
113 vec2 &ViewPos = pGameServer->m_apPlayers[SnappingClient]->m_ViewPos;
114 vec2 &ShowDistance = pGameServer->m_apPlayers[SnappingClient]->m_ShowDistance;
115
116 vec2 DistanceToLine, ClosestPoint;
117 if(closest_point_on_line(line_pointA: StartPos, line_pointB: EndPos, target_point: ViewPos, out_pos&: ClosestPoint))
118 {
119 DistanceToLine = ViewPos - ClosestPoint;
120 }
121 else
122 {
123 // No line section was passed but two equal points
124 DistanceToLine = ViewPos - StartPos;
125 }
126 float ClippDistance = maximum(a: ShowDistance.x, b: ShowDistance.y);
127 return (absolute(a: DistanceToLine.x) > ClippDistance || absolute(a: DistanceToLine.y) > ClippDistance);
128}
129