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)(unsigned char 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, unsigned char Type, unsigned char Flags, unsigned char 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 bool 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 bool HasHookTeleIns() const;
115
116 const CLayers *Layers() const { return m_pLayers; }
117 const CTile *GameLayer() const { return m_pTiles; }
118 const CTeleTile *TeleLayer() const { return m_pTele; }
119 const CSpeedupTile *SpeedupLayer() const { return m_pSpeedup; }
120 const CTile *FrontLayer() const { return m_pFront; }
121 const CSwitchTile *SwitchLayer() const { return m_pSwitch; }
122 const CTuneTile *TuneLayer() const { return m_pTune; }
123
124 int m_HighestSwitchNumber;
125
126 /**
127 * Index all teleporter types (in, out and checkpoints)
128 * as one consecutive list.
129 *
130 * @param Number is the teleporter number (one less than what is shown in game)
131 * @param Offset picks the n'th occurrence of that teleporter in the map
132 *
133 * @return The coordinates of the teleporter in the map
134 * or (-1, -1) if not found
135 */
136 vec2 TeleAllGet(int Number, size_t Offset);
137
138 /**
139 * @param Number is the teleporter number (one less than what is shown in game)
140 * @return The amount of occurrences of that teleporter across all types (in, out, checkpoint)
141 */
142 size_t TeleAllSize(int Number);
143
144 const std::vector<vec2> &TeleIns(int Number) { return m_TeleIns[Number]; }
145 const std::vector<vec2> &TeleOuts(int Number) { return m_TeleOuts[Number]; }
146 const std::vector<vec2> &TeleCheckOuts(int Number) { return m_TeleCheckOuts[Number]; }
147 const std::vector<vec2> &TeleOthers(int Number) { return m_TeleOthers[Number]; }
148
149private:
150 CLayers *m_pLayers;
151
152 int m_Width;
153 int m_Height;
154
155 CTile *m_pTiles;
156 CTeleTile *m_pTele;
157 CSpeedupTile *m_pSpeedup;
158 CTile *m_pFront;
159 CSwitchTile *m_pSwitch;
160 CTuneTile *m_pTune;
161 CDoorTile *m_pDoor;
162
163 // TILE_TELEIN
164 std::map<int, std::vector<vec2>> m_TeleIns;
165 // TILE_TELEOUT
166 std::map<int, std::vector<vec2>> m_TeleOuts;
167 // TILE_TELECHECKOUT
168 std::map<int, std::vector<vec2>> m_TeleCheckOuts;
169 // TILE_TELEINEVIL, TILE_TELECHECK, TILE_TELECHECKIN, TILE_TELECHECKINEVIL, TILE_TELEINHOOK
170 std::map<int, std::vector<vec2>> m_TeleOthers;
171 bool m_HasHookTeleIns;
172};
173
174void ThroughOffset(vec2 Pos0, vec2 Pos1, int *pOffsetX, int *pOffsetY);
175#endif
176