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
56 // DDRace
57 void SetCollisionAt(float x, float y, int Index);
58 void SetDoorCollisionAt(float x, float y, int Type, int Flags, int Number);
59 void GetDoorTile(int Index, CDoorTile *pDoorTile) const;
60 int GetFrontCollisionAt(float x, float y) const { return GetFrontTile(x: round_to_int(f: x), y: round_to_int(f: y)); }
61 int IntersectNoLaser(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision) const;
62 int IntersectNoLaserNoWalls(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision) const;
63 int IntersectAir(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision) const;
64 int GetIndex(int x, int y) const;
65 int GetIndex(vec2 PrevPos, vec2 Pos) const;
66 int GetFrontIndex(int x, int y) const;
67
68 int GetMoveRestrictions(CALLBACK_SWITCHACTIVE pfnSwitchActive, void *pUser, vec2 Pos, float Distance = 18.0f, int OverrideCenterTileIndex = -1) const;
69 int GetMoveRestrictions(vec2 Pos, float Distance = 18.0f) const
70 {
71 return GetMoveRestrictions(pfnSwitchActive: nullptr, pUser: nullptr, Pos, Distance);
72 }
73
74 int GetTile(int x, int y) const;
75 int GetFrontTile(int x, int y) const;
76 int Entity(int x, int y, int Layer) const;
77 int GetPureMapIndex(float x, float y) const;
78 int GetPureMapIndex(vec2 Pos) const { return GetPureMapIndex(x: Pos.x, y: Pos.y); }
79 std::vector<int> GetMapIndices(vec2 PrevPos, vec2 Pos, unsigned MaxIndices = 0) const;
80 int GetMapIndex(vec2 Pos) const;
81 bool TileExists(int Index) const;
82 bool TileExistsNext(int Index) const;
83 vec2 GetPos(int Index) const;
84 int GetTileIndex(int Index) const;
85 int GetFrontTileIndex(int Index) const;
86 int GetTileFlags(int Index) const;
87 int GetFrontTileFlags(int Index) const;
88 int IsTeleport(int Index) const;
89 int IsEvilTeleport(int Index) const;
90 bool IsCheckTeleport(int Index) const;
91 bool IsCheckEvilTeleport(int Index) const;
92 int IsTeleportWeapon(int Index) const;
93 int IsTeleportHook(int Index) const;
94 int IsTeleCheckpoint(int Index) const;
95 int IsSpeedup(int Index) const;
96 int IsTune(int Index) const;
97 void GetSpeedup(int Index, vec2 *pDir, int *pForce, int *pMaxSpeed, int *pType) const;
98 int GetSwitchType(int Index) const;
99 int GetSwitchNumber(int Index) const;
100 int GetSwitchDelay(int Index) const;
101
102 int IsSolid(int x, int y) const;
103 bool IsThrough(int x, int y, int OffsetX, int OffsetY, vec2 Pos0, vec2 Pos1) const;
104 bool IsHookBlocker(int x, int y, vec2 Pos0, vec2 Pos1) const;
105 int IsWallJump(int Index) const;
106 int IsNoLaser(int x, int y) const;
107 int IsFrontNoLaser(int x, int y) const;
108
109 int IsTimeCheckpoint(int Index) const;
110 int IsFrontTimeCheckpoint(int Index) const;
111
112 int MoverSpeed(int x, int y, vec2 *pSpeed) const;
113
114 const CLayers *Layers() const { return m_pLayers; }
115 const CTile *GameLayer() const { return m_pTiles; }
116 const CTeleTile *TeleLayer() const { return m_pTele; }
117 const CSpeedupTile *SpeedupLayer() const { return m_pSpeedup; }
118 const CTile *FrontLayer() const { return m_pFront; }
119 const CSwitchTile *SwitchLayer() const { return m_pSwitch; }
120 const CTuneTile *TuneLayer() const { return m_pTune; }
121
122 int m_HighestSwitchNumber;
123
124 /**
125 * Index all teleporter types (in, out and checkpoints)
126 * as one consecutive list.
127 *
128 * @param Number is the teleporter number (one less than what is shown in game)
129 * @param Offset picks the n'th occurrence of that teleporter in the map
130 *
131 * @return The coordinates of the teleporter in the map
132 * or (-1, -1) if not found
133 */
134 vec2 TeleAllGet(int Number, size_t Offset);
135
136 /**
137 * @param Number is the teleporter number (one less than what is shown in game)
138 * @return The amount of occurrences of that teleporter across all types (in, out, checkpoint)
139 */
140 size_t TeleAllSize(int Number);
141
142 const std::vector<vec2> &TeleIns(int Number) { return m_TeleIns[Number]; }
143 const std::vector<vec2> &TeleOuts(int Number) { return m_TeleOuts[Number]; }
144 const std::vector<vec2> &TeleCheckOuts(int Number) { return m_TeleCheckOuts[Number]; }
145 const std::vector<vec2> &TeleOthers(int Number) { return m_TeleOthers[Number]; }
146
147private:
148 CLayers *m_pLayers;
149
150 int m_Width;
151 int m_Height;
152
153 CTile *m_pTiles;
154 CTeleTile *m_pTele;
155 CSpeedupTile *m_pSpeedup;
156 CTile *m_pFront;
157 CSwitchTile *m_pSwitch;
158 CTuneTile *m_pTune;
159 CDoorTile *m_pDoor;
160
161 // TILE_TELEIN
162 std::map<int, std::vector<vec2>> m_TeleIns;
163 // TILE_TELEOUT
164 std::map<int, std::vector<vec2>> m_TeleOuts;
165 // TILE_TELECHECKOUT
166 std::map<int, std::vector<vec2>> m_TeleCheckOuts;
167 // TILE_TELEINEVIL, TILE_TELECHECK, TILE_TELECHECKIN, TILE_TELECHECKINEVIL
168 std::map<int, std::vector<vec2>> m_TeleOthers;
169};
170
171void ThroughOffset(vec2 Pos0, vec2 Pos1, int *pOffsetX, int *pOffsetY);
172#endif
173