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#ifndef GAME_COLLISION_H
4#define GAME_COLLISION_H
5
6#include <base/vmath.h>
7
8#include <engine/shared/protocol.h>
9
10#include <map>
11#include <vector>
12
13class CTile;
14class CLayers;
15class CTeleTile;
16class CSpeedupTile;
17class CSwitchTile;
18class CTuneTile;
19class CDoorTile;
20
21enum
22{
23 CANTMOVE_LEFT = 1 << 0,
24 CANTMOVE_RIGHT = 1 << 1,
25 CANTMOVE_UP = 1 << 2,
26 CANTMOVE_DOWN = 1 << 3,
27};
28
29vec2 ClampVel(int MoveRestriction, vec2 Vel);
30
31typedef bool (*CALLBACK_SWITCHACTIVE)(int Number, void *pUser);
32struct CAntibotMapData;
33
34class CCollision
35{
36public:
37 CCollision();
38 ~CCollision();
39
40 void Init(CLayers *pLayers);
41 void Unload();
42 void FillAntibot(CAntibotMapData *pMapData) const;
43
44 bool CheckPoint(float x, float y) const { return IsSolid(x: round_to_int(f: x), y: round_to_int(f: y)); }
45 bool CheckPoint(vec2 Pos) const { return CheckPoint(x: Pos.x, y: Pos.y); }
46 int GetCollisionAt(float x, float y) const { return GetTile(x: round_to_int(f: x), y: round_to_int(f: y)); }
47 int GetWidth() const { return m_Width; }
48 int GetHeight() const { return m_Height; }
49 int IntersectLine(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision) const;
50 int IntersectLineTeleWeapon(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision, int *pTeleNr = nullptr) const;
51 int IntersectLineTeleHook(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision, int *pTeleNr = nullptr) const;
52 void MovePoint(vec2 *pInoutPos, vec2 *pInoutVel, float Elasticity, int *pBounces) const;
53 void MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, vec2 Elasticity, bool *pGrounded = nullptr) const;
54 bool TestBox(vec2 Pos, vec2 Size) const;
55 bool IsOnGround(vec2 Pos, float Size) const;
56
57 // DDRace
58 void SetCollisionAt(float x, float y, int Index);
59 void SetDoorCollisionAt(float x, float y, int Type, int Flags, int Number);
60 void GetDoorTile(int Index, CDoorTile *pDoorTile) const;
61 int GetFrontCollisionAt(float x, float y) const { return GetFrontTile(x: round_to_int(f: x), y: round_to_int(f: y)); }
62 int IntersectNoLaser(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision) const;
63 int IntersectNoLaserNoWalls(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision) const;
64 int IntersectAir(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision) const;
65 int GetIndex(int x, int y) const;
66 int GetIndex(vec2 PrevPos, vec2 Pos) const;
67 int GetFrontIndex(int x, int y) const;
68
69 int GetMoveRestrictions(CALLBACK_SWITCHACTIVE pfnSwitchActive, void *pUser, vec2 Pos, float Distance = 18.0f, int OverrideCenterTileIndex = -1) const;
70 int GetMoveRestrictions(vec2 Pos, float Distance = 18.0f) const
71 {
72 return GetMoveRestrictions(pfnSwitchActive: nullptr, pUser: nullptr, Pos, Distance);
73 }
74
75 int GetTile(int x, int y) const;
76 int GetFrontTile(int x, int y) const;
77 int Entity(int x, int y, int Layer) const;
78 int GetPureMapIndex(float x, float y) const;
79 int GetPureMapIndex(vec2 Pos) const { return GetPureMapIndex(x: Pos.x, y: Pos.y); }
80 std::vector<int> GetMapIndices(vec2 PrevPos, vec2 Pos, unsigned MaxIndices = 0) const;
81 int GetMapIndex(vec2 Pos) const;
82 bool TileExists(int Index) const;
83 bool TileExistsNext(int Index) const;
84 vec2 GetPos(int Index) const;
85 int GetTileIndex(int Index) const;
86 int GetFrontTileIndex(int Index) const;
87 int GetTileFlags(int Index) const;
88 int GetFrontTileFlags(int Index) const;
89 int IsTeleport(int Index) const;
90 int IsEvilTeleport(int Index) const;
91 bool IsCheckTeleport(int Index) const;
92 bool IsCheckEvilTeleport(int Index) const;
93 int IsTeleportWeapon(int Index) const;
94 int IsTeleportHook(int Index) const;
95 int IsTeleCheckpoint(int Index) const;
96 int IsSpeedup(int Index) const;
97 int IsTune(int Index) const;
98 void GetSpeedup(int Index, vec2 *pDir, int *pForce, int *pMaxSpeed, int *pType) const;
99 int GetSwitchType(int Index) const;
100 int GetSwitchNumber(int Index) const;
101 int GetSwitchDelay(int Index) const;
102
103 int IsSolid(int x, int y) const;
104 bool IsThrough(int x, int y, int OffsetX, int OffsetY, vec2 Pos0, vec2 Pos1) const;
105 bool IsHookBlocker(int x, int y, vec2 Pos0, vec2 Pos1) const;
106 int IsWallJump(int Index) const;
107 int IsNoLaser(int x, int y) const;
108 int IsFrontNoLaser(int x, int y) const;
109
110 int IsTimeCheckpoint(int Index) const;
111 int IsFrontTimeCheckpoint(int Index) const;
112
113 int MoverSpeed(int x, int y, vec2 *pSpeed) const;
114
115 const CLayers *Layers() const { return m_pLayers; }
116 const CTile *GameLayer() const { return m_pTiles; }
117 const CTeleTile *TeleLayer() const { return m_pTele; }
118 const CSpeedupTile *SpeedupLayer() const { return m_pSpeedup; }
119 const CTile *FrontLayer() const { return m_pFront; }
120 const CSwitchTile *SwitchLayer() const { return m_pSwitch; }
121 const CTuneTile *TuneLayer() const { return m_pTune; }
122
123 int m_HighestSwitchNumber;
124
125 /**
126 * Index all teleporter types (in, out and checkpoints)
127 * as one consecutive list.
128 *
129 * @param Number is the teleporter number (one less than what is shown in game)
130 * @param Offset picks the n'th occurrence of that teleporter in the map
131 *
132 * @return The coordinates of the teleporter in the map
133 * or (-1, -1) if not found
134 */
135 vec2 TeleAllGet(int Number, size_t Offset);
136
137 /**
138 * @param Number is the teleporter number (one less than what is shown in game)
139 * @return The amount of occurrences of that teleporter across all types (in, out, checkpoint)
140 */
141 size_t TeleAllSize(int Number);
142
143 const std::vector<vec2> &TeleIns(int Number) { return m_TeleIns[Number]; }
144 const std::vector<vec2> &TeleOuts(int Number) { return m_TeleOuts[Number]; }
145 const std::vector<vec2> &TeleCheckOuts(int Number) { return m_TeleCheckOuts[Number]; }
146 const std::vector<vec2> &TeleOthers(int Number) { return m_TeleOthers[Number]; }
147
148private:
149 CLayers *m_pLayers;
150
151 int m_Width;
152 int m_Height;
153
154 CTile *m_pTiles;
155 CTeleTile *m_pTele;
156 CSpeedupTile *m_pSpeedup;
157 CTile *m_pFront;
158 CSwitchTile *m_pSwitch;
159 CTuneTile *m_pTune;
160 CDoorTile *m_pDoor;
161
162 // TILE_TELEIN
163 std::map<int, std::vector<vec2>> m_TeleIns;
164 // TILE_TELEOUT
165 std::map<int, std::vector<vec2>> m_TeleOuts;
166 // TILE_TELECHECKOUT
167 std::map<int, std::vector<vec2>> m_TeleCheckOuts;
168 // TILE_TELEINEVIL, TILE_TELECHECK, TILE_TELECHECKIN, TILE_TELECHECKINEVIL
169 std::map<int, std::vector<vec2>> m_TeleOthers;
170};
171
172void ThroughOffset(vec2 Pos0, vec2 Pos1, int *pOffsetX, int *pOffsetY);
173#endif
174