1/* (c) Shereef Marzouk. See "licence DDRace.txt" and the readme.txt in the root of the distribution for more information. */
2#include "door.h"
3
4#include "character.h"
5
6#include <game/client/laser_data.h>
7#include <game/collision.h>
8#include <game/mapitems.h>
9
10#include <cmath>
11
12CDoor::CDoor(CGameWorld *pGameWorld, int Id, const CLaserData *pData) :
13 CEntity(pGameWorld, CGameWorld::ENTTYPE_DOOR)
14{
15 m_Id = Id;
16 m_Active = false;
17
18 m_Number = pData->m_SwitchNumber;
19 m_Layer = m_Number > 0 ? LAYER_SWITCH : LAYER_GAME;
20
21 Read(pData);
22}
23
24// The eight tiles IGameController::OnEntity checks around a door tile, in its order: the
25// door's rotation is pi / 4 * side, so side n points along (sin, cos) of that angle.
26static const int s_aSideOffsetX[8] = {0, 1, 1, 1, 0, -1, -1, -1};
27static const int s_aSideOffsetY[8] = {1, 1, 0, -1, -1, -1, 0, 1};
28
29int CDoor::MapSide() const
30{
31 const int OffsetX = round_to_int(f: m_Direction.x);
32 const int OffsetY = round_to_int(f: m_Direction.y);
33 for(int Side = 0; Side < 8; Side++)
34 {
35 if(s_aSideOffsetX[Side] == OffsetX && s_aSideOffsetY[Side] == OffsetY)
36 return Side;
37 }
38 return -1;
39}
40
41int CDoor::MapLength(int Side)
42{
43 const int Nx = round_to_int(f: m_Pos.x) / 32;
44 const int Ny = round_to_int(f: m_Pos.y) / 32;
45 int Length = -1;
46 for(const int Layer : {LAYER_GAME, LAYER_FRONT, LAYER_SWITCH})
47 {
48 if(Layer == LAYER_FRONT && Collision()->FrontLayer() == nullptr)
49 continue;
50 if(Layer == LAYER_SWITCH && Collision()->SwitchLayer() == nullptr)
51 continue;
52 if(Collision()->Entity(x: Nx, y: Ny, Layer) != ENTITY_DOOR)
53 continue;
54 const int Index = Collision()->Entity(x: Nx + s_aSideOffsetX[Side], y: Ny + s_aSideOffsetY[Side], Layer);
55 if(Index < ENTITY_LASER_SHORT || Index > ENTITY_LASER_LONG)
56 continue;
57 const int Candidate = 32 * 3 + 32 * (Index - ENTITY_LASER_SHORT) * 3;
58 if(Length != -1 && Length != Candidate)
59 return -1;
60 Length = Candidate;
61 }
62 return Length;
63}
64
65void CDoor::ResetCollision()
66{
67 if(Collision()->GetTile(x: m_Pos.x, y: m_Pos.y) || Collision()->GetFrontTile(x: m_Pos.x, y: m_Pos.y))
68 return;
69
70 m_Active = true;
71
72 const vec2 Dir = m_To - m_Pos;
73 const float DirLength = length(a: Dir);
74 m_Direction = DirLength > 0.0f ? normalize_pre_length(v: Dir, len: DirLength) : vec2(0.0f, 0.0f);
75
76 // The snapshot only carries the endpoint CCollision::IntersectNoLaser produced, which
77 // also stops at TILE_NOLASER, while the server lays down door collision along the full
78 // length configured in the map and stops only at solid tiles. Recover that length the
79 // way IGameController::OnEntity derives it, from the ENTITY_LASER_* tile next to the
80 // door's tile on the side the door points at.
81 const int Side = DirLength > 0.0f ? MapSide() : -1;
82 const int ConfiguredLength = Side == -1 ? -1 : MapLength(Side);
83 if(ConfiguredLength != -1)
84 {
85 m_Length = ConfiguredLength;
86 m_Direction = vec2(std::sin(x: pi / 4 * Side), std::cos(x: pi / 4 * Side));
87 }
88 else
89 {
90 // No configured length: the length comes straight off the wire, so a server can
91 // snap a door whose endpoints are billions of units apart and spin the walk below
92 // (which never breaks while it clamps to an off-map air tile) at 100% CPU on every
93 // snapshot. A door's collision can only ever matter inside the map, since both
94 // CheckPoint and SetDoorCollisionAt clamp to it, so refuse to walk anything longer
95 // than the map itself. This also keeps the float length in int range.
96 const float MaxLength = (Collision()->GetWidth() + Collision()->GetHeight()) * 32.0f;
97 if(DirLength <= 0.0f || DirLength > MaxLength)
98 {
99 m_Active = false;
100 return;
101 }
102 m_Length = round_to_int(f: DirLength);
103 }
104
105 for(int i = 0; i < m_Length - 1; i++)
106 {
107 vec2 CurrentPos = m_Pos + m_Direction * i;
108 if(Collision()->CheckPoint(Pos: CurrentPos))
109 break;
110 else
111 Collision()->SetDoorCollisionAt(x: CurrentPos.x, y: CurrentPos.y, Type: TILE_STOPA, Flags: 0, Number: m_Number);
112 }
113}
114
115void CDoor::Destroy()
116{
117 if(m_Active)
118 {
119 for(int i = 0; i < m_Length - 1; i++)
120 {
121 vec2 CurrentPos = m_Pos + m_Direction * i;
122 if(Collision()->CheckPoint(Pos: CurrentPos))
123 break;
124 else
125 Collision()->SetDoorCollisionAt(x: CurrentPos.x, y: CurrentPos.y, Type: TILE_AIR, Flags: 0, Number: 0);
126 }
127 }
128 delete this;
129}
130
131void CDoor::Read(const CLaserData *pData)
132{
133 // it's flipped in the laser object
134 m_Pos = pData->m_To;
135 m_To = pData->m_From;
136}
137
138bool CDoor::Match(const CDoor *pDoor) const
139{
140 return pDoor->m_Pos == m_Pos && pDoor->m_To == m_To && pDoor->m_Number == m_Number;
141}
142