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#include <antibot/antibot_data.h>
4
5#include <base/dbg.h>
6#include <base/math.h>
7#include <base/mem.h>
8#include <base/vmath.h>
9
10#include <engine/map.h>
11#include <engine/shared/config.h>
12
13#include <game/collision.h>
14#include <game/layers.h>
15#include <game/mapitems.h>
16
17#include <cmath>
18
19vec2 ClampVel(int MoveRestriction, vec2 Vel)
20{
21 if(Vel.x > 0 && (MoveRestriction & CANTMOVE_RIGHT))
22 {
23 Vel.x = 0;
24 }
25 if(Vel.x < 0 && (MoveRestriction & CANTMOVE_LEFT))
26 {
27 Vel.x = 0;
28 }
29 if(Vel.y > 0 && (MoveRestriction & CANTMOVE_DOWN))
30 {
31 Vel.y = 0;
32 }
33 if(Vel.y < 0 && (MoveRestriction & CANTMOVE_UP))
34 {
35 Vel.y = 0;
36 }
37 return Vel;
38}
39
40CCollision::CCollision()
41{
42 m_pDoor = nullptr;
43 Unload();
44}
45
46CCollision::~CCollision()
47{
48 Unload();
49}
50
51void CCollision::Init(class CLayers *pLayers)
52{
53 Unload();
54
55 m_pLayers = pLayers;
56 m_Width = m_pLayers->GameLayer()->m_Width;
57 m_Height = m_pLayers->GameLayer()->m_Height;
58 m_pTiles = static_cast<CTile *>(m_pLayers->Map()->GetData(Index: m_pLayers->GameLayer()->m_Data));
59 m_HasHookTeleIns = false;
60
61 if(m_pLayers->TeleLayer())
62 {
63 m_pTele = static_cast<CTeleTile *>(m_pLayers->Map()->GetData(Index: m_pLayers->TeleLayer()->m_Tele));
64 }
65
66 if(m_pLayers->SpeedupLayer())
67 {
68 m_pSpeedup = static_cast<CSpeedupTile *>(m_pLayers->Map()->GetData(Index: m_pLayers->SpeedupLayer()->m_Speedup));
69 }
70
71 if(m_pLayers->SwitchLayer())
72 {
73 m_pSwitch = static_cast<CSwitchTile *>(m_pLayers->Map()->GetData(Index: m_pLayers->SwitchLayer()->m_Switch));
74 m_pDoor = new CDoorTile[m_Width * m_Height];
75 mem_zero(block: m_pDoor, size: (size_t)m_Width * m_Height * sizeof(CDoorTile));
76 }
77
78 if(m_pLayers->TuneLayer())
79 {
80 m_pTune = static_cast<CTuneTile *>(m_pLayers->Map()->GetData(Index: m_pLayers->TuneLayer()->m_Tune));
81 }
82
83 if(m_pLayers->FrontLayer())
84 {
85 m_pFront = static_cast<CTile *>(m_pLayers->Map()->GetData(Index: m_pLayers->FrontLayer()->m_Front));
86 }
87
88 if(m_pSwitch)
89 {
90 for(int i = 0; i < m_Width * m_Height; i++)
91 {
92 if(m_pSwitch[i].m_Number > m_HighestSwitchNumber)
93 {
94 m_HighestSwitchNumber = m_pSwitch[i].m_Number;
95 }
96
97 m_pDoor[i].m_Number = m_pSwitch[i].m_Number;
98
99 const unsigned char Index = m_pSwitch[i].m_Type;
100 if(Index <= TILE_NPH_ENABLE)
101 {
102 if((Index >= TILE_JUMP && Index <= TILE_SUBTRACT_TIME) ||
103 Index == TILE_ALLOW_TELE_GUN ||
104 Index == TILE_ALLOW_BLUE_TELE_GUN)
105 {
106 m_pSwitch[i].m_Type = Index;
107 }
108 else
109 {
110 m_pSwitch[i].m_Type = 0;
111 }
112 }
113 }
114 }
115
116 if(m_pTele)
117 {
118 for(int i = 0; i < m_Width * m_Height; i++)
119 {
120 const unsigned char Number = m_pTele[i].m_Number;
121 const unsigned char Type = m_pTele[i].m_Type;
122 if(Number && Type)
123 {
124 const vec2 TelePos = vec2(i % m_Width * 32.0f + 16.0f, i / m_Width * 32.0f + 16.0f);
125 if(Type == TILE_TELEIN)
126 {
127 m_TeleIns[Number - 1].push_back(x: TelePos);
128 }
129 else if(Type == TILE_TELEOUT)
130 {
131 m_TeleOuts[Number - 1].push_back(x: TelePos);
132 }
133 else if(Type == TILE_TELECHECKOUT)
134 {
135 m_TeleCheckOuts[Number - 1].push_back(x: TelePos);
136 }
137 else
138 {
139 m_TeleOthers[Number - 1].push_back(x: TelePos);
140 if(Type == TILE_TELEINHOOK)
141 m_HasHookTeleIns = true;
142 }
143 }
144 }
145 }
146}
147
148void CCollision::Unload()
149{
150 m_pTiles = nullptr;
151 m_Width = 0;
152 m_Height = 0;
153 m_pLayers = nullptr;
154
155 m_HighestSwitchNumber = 0;
156
157 m_TeleIns.clear();
158 m_TeleOuts.clear();
159 m_TeleCheckOuts.clear();
160 m_TeleOthers.clear();
161
162 m_pTele = nullptr;
163 m_pSpeedup = nullptr;
164 m_pFront = nullptr;
165 m_pSwitch = nullptr;
166 m_pTune = nullptr;
167 delete[] m_pDoor;
168 m_pDoor = nullptr;
169}
170
171void CCollision::FillAntibot(CAntibotMapData *pMapData) const
172{
173 pMapData->m_Width = m_Width;
174 pMapData->m_Height = m_Height;
175 pMapData->m_pTiles = (unsigned char *)malloc(size: (size_t)m_Width * m_Height);
176 for(int i = 0; i < m_Width * m_Height; i++)
177 {
178 pMapData->m_pTiles[i] = 0;
179 if(m_pTiles[i].m_Index >= TILE_SOLID && m_pTiles[i].m_Index <= TILE_NOLASER)
180 {
181 pMapData->m_pTiles[i] = m_pTiles[i].m_Index;
182 }
183 }
184}
185
186enum
187{
188 MR_DIR_HERE = 0,
189 MR_DIR_RIGHT,
190 MR_DIR_DOWN,
191 MR_DIR_LEFT,
192 MR_DIR_UP,
193 NUM_MR_DIRS
194};
195
196static int GetMoveRestrictionsRaw(int Direction, int Tile, int Flags)
197{
198 Flags = Flags & (TILEFLAG_XFLIP | TILEFLAG_YFLIP | TILEFLAG_ROTATE);
199 switch(Tile)
200 {
201 case TILE_STOP:
202 switch(Flags)
203 {
204 case ROTATION_0: return CANTMOVE_DOWN;
205 case ROTATION_90: return CANTMOVE_LEFT;
206 case ROTATION_180: return CANTMOVE_UP;
207 case ROTATION_270: return CANTMOVE_RIGHT;
208
209 case static_cast<int>(TILEFLAG_YFLIP) ^ static_cast<int>(ROTATION_0): return CANTMOVE_UP;
210 case static_cast<int>(TILEFLAG_YFLIP) ^ static_cast<int>(ROTATION_90): return CANTMOVE_RIGHT;
211 case static_cast<int>(TILEFLAG_YFLIP) ^ static_cast<int>(ROTATION_180): return CANTMOVE_DOWN;
212 case static_cast<int>(TILEFLAG_YFLIP) ^ static_cast<int>(ROTATION_270): return CANTMOVE_LEFT;
213 }
214 break;
215 case TILE_STOPS:
216 switch(Flags)
217 {
218 case ROTATION_0:
219 case ROTATION_180:
220 case static_cast<int>(TILEFLAG_YFLIP) ^ static_cast<int>(ROTATION_0):
221 case static_cast<int>(TILEFLAG_YFLIP) ^ static_cast<int>(ROTATION_180):
222 return CANTMOVE_DOWN | CANTMOVE_UP;
223 case ROTATION_90:
224 case ROTATION_270:
225 case static_cast<int>(TILEFLAG_YFLIP) ^ static_cast<int>(ROTATION_90):
226 case static_cast<int>(TILEFLAG_YFLIP) ^ static_cast<int>(ROTATION_270):
227 return CANTMOVE_LEFT | CANTMOVE_RIGHT;
228 }
229 break;
230 case TILE_STOPA:
231 return CANTMOVE_LEFT | CANTMOVE_RIGHT | CANTMOVE_UP | CANTMOVE_DOWN;
232 }
233 return 0;
234}
235
236static int GetMoveRestrictionsMask(int Direction)
237{
238 switch(Direction)
239 {
240 case MR_DIR_HERE: return 0;
241 case MR_DIR_RIGHT: return CANTMOVE_RIGHT;
242 case MR_DIR_DOWN: return CANTMOVE_DOWN;
243 case MR_DIR_LEFT: return CANTMOVE_LEFT;
244 case MR_DIR_UP: return CANTMOVE_UP;
245 default: dbg_assert_failed("Invalid Direction: %d", Direction);
246 }
247}
248
249static int GetMoveRestrictions(int Direction, int Tile, int Flags)
250{
251 int Result = GetMoveRestrictionsRaw(Direction, Tile, Flags);
252 // Generally, stoppers only have an effect if they block us from moving
253 // *onto* them. The one exception is one-way blockers, they can also
254 // block us from moving if we're on top of them.
255 if(Direction == MR_DIR_HERE && Tile == TILE_STOP)
256 {
257 return Result;
258 }
259 return Result & GetMoveRestrictionsMask(Direction);
260}
261
262int CCollision::GetMoveRestrictions(CALLBACK_SWITCHACTIVE pfnSwitchActive, void *pUser, vec2 Pos, float Distance, int OverrideCenterTileIndex) const
263{
264 static const vec2 DIRECTIONS[NUM_MR_DIRS] =
265 {
266 vec2(0, 0),
267 vec2(1, 0),
268 vec2(0, 1),
269 vec2(-1, 0),
270 vec2(0, -1)};
271 dbg_assert(0.0f <= Distance && Distance <= 32.0f, "Invalid Distance: %f", Distance);
272 int Restrictions = 0;
273 for(int d = 0; d < NUM_MR_DIRS; d++)
274 {
275 vec2 ModPos = Pos + DIRECTIONS[d] * Distance;
276 int ModMapIndex = GetPureMapIndex(Pos: ModPos);
277 if(d == MR_DIR_HERE && OverrideCenterTileIndex >= 0)
278 {
279 ModMapIndex = OverrideCenterTileIndex;
280 }
281 for(int Front = 0; Front < 2; Front++)
282 {
283 int Tile;
284 int Flags;
285 if(!Front)
286 {
287 Tile = GetTileIndex(Index: ModMapIndex);
288 Flags = GetTileFlags(Index: ModMapIndex);
289 }
290 else
291 {
292 Tile = GetFrontTileIndex(Index: ModMapIndex);
293 Flags = GetFrontTileFlags(Index: ModMapIndex);
294 }
295 Restrictions |= ::GetMoveRestrictions(Direction: d, Tile, Flags);
296 }
297 if(pfnSwitchActive)
298 {
299 CDoorTile DoorTile;
300 GetDoorTile(Index: ModMapIndex, pDoorTile: &DoorTile);
301 if((int)DoorTile.m_Number <= m_HighestSwitchNumber &&
302 pfnSwitchActive(DoorTile.m_Number, pUser))
303 {
304 Restrictions |= ::GetMoveRestrictions(Direction: d, Tile: DoorTile.m_Index, Flags: DoorTile.m_Flags);
305 }
306 }
307 }
308 return Restrictions;
309}
310
311int CCollision::GetTile(int x, int y) const
312{
313 if(!m_pTiles)
314 return 0;
315
316 int Nx = std::clamp(val: x / 32, lo: 0, hi: m_Width - 1);
317 int Ny = std::clamp(val: y / 32, lo: 0, hi: m_Height - 1);
318 const int Index = Ny * m_Width + Nx;
319
320 if(m_pTiles[Index].m_Index >= TILE_SOLID && m_pTiles[Index].m_Index <= TILE_NOLASER)
321 return m_pTiles[Index].m_Index;
322 return 0;
323}
324
325// TODO: rewrite this smarter!
326int CCollision::IntersectLine(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision) const
327{
328 float Distance = distance(a: Pos0, b: Pos1);
329 int End(Distance + 1);
330 vec2 Last = Pos0;
331 for(int i = 0; i <= End; i++)
332 {
333 float a = i / (float)End;
334 vec2 Pos = mix(a: Pos0, b: Pos1, amount: a);
335 // Temporary position for checking collision
336 int ix = round_to_int(f: Pos.x);
337 int iy = round_to_int(f: Pos.y);
338
339 if(CheckPoint(x: ix, y: iy))
340 {
341 if(pOutCollision)
342 *pOutCollision = Pos;
343 if(pOutBeforeCollision)
344 *pOutBeforeCollision = Last;
345 return GetCollisionAt(x: ix, y: iy);
346 }
347
348 Last = Pos;
349 }
350 if(pOutCollision)
351 *pOutCollision = Pos1;
352 if(pOutBeforeCollision)
353 *pOutBeforeCollision = Pos1;
354 return 0;
355}
356
357int CCollision::IntersectLineTeleHook(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision, int *pTeleNr) const
358{
359 float Distance = distance(a: Pos0, b: Pos1);
360 int End(Distance + 1);
361 vec2 Last = Pos0;
362 int dx = 0, dy = 0; // Offset for checking the "through" tile
363 ThroughOffset(Pos0, Pos1, pOffsetX: &dx, pOffsetY: &dy);
364 for(int i = 0; i <= End; i++)
365 {
366 float a = i / (float)End;
367 vec2 Pos = mix(a: Pos0, b: Pos1, amount: a);
368 // Temporary position for checking collision
369 int ix = round_to_int(f: Pos.x);
370 int iy = round_to_int(f: Pos.y);
371
372 int Index = GetPureMapIndex(Pos);
373 if(pTeleNr)
374 {
375 if(g_Config.m_SvOldTeleportHook)
376 *pTeleNr = IsTeleport(Index);
377 else
378 *pTeleNr = IsTeleportHook(Index);
379 }
380 if(pTeleNr && *pTeleNr)
381 {
382 if(pOutCollision)
383 *pOutCollision = Pos;
384 if(pOutBeforeCollision)
385 *pOutBeforeCollision = Last;
386 return TILE_TELEINHOOK;
387 }
388
389 int Hit = 0;
390 if(CheckPoint(x: ix, y: iy))
391 {
392 if(!IsThrough(x: ix, y: iy, OffsetX: dx, OffsetY: dy, Pos0, Pos1))
393 Hit = GetCollisionAt(x: ix, y: iy);
394 }
395 else if(IsHookBlocker(x: ix, y: iy, Pos0, Pos1))
396 {
397 Hit = TILE_NOHOOK;
398 }
399 if(Hit)
400 {
401 if(pOutCollision)
402 *pOutCollision = Pos;
403 if(pOutBeforeCollision)
404 *pOutBeforeCollision = Last;
405 return Hit;
406 }
407
408 Last = Pos;
409 }
410 if(pOutCollision)
411 *pOutCollision = Pos1;
412 if(pOutBeforeCollision)
413 *pOutBeforeCollision = Pos1;
414 return 0;
415}
416
417int CCollision::IntersectLineTeleWeapon(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision, int *pTeleNr) const
418{
419 float Distance = distance(a: Pos0, b: Pos1);
420 int End(Distance + 1);
421 vec2 Last = Pos0;
422 for(int i = 0; i <= End; i++)
423 {
424 float a = i / (float)End;
425 vec2 Pos = mix(a: Pos0, b: Pos1, amount: a);
426 // Temporary position for checking collision
427 int ix = round_to_int(f: Pos.x);
428 int iy = round_to_int(f: Pos.y);
429
430 int Index = GetPureMapIndex(Pos);
431 if(pTeleNr)
432 {
433 if(g_Config.m_SvOldTeleportWeapons)
434 *pTeleNr = IsTeleport(Index);
435 else
436 *pTeleNr = IsTeleportWeapon(Index);
437 }
438 if(pTeleNr && *pTeleNr)
439 {
440 if(pOutCollision)
441 *pOutCollision = Pos;
442 if(pOutBeforeCollision)
443 *pOutBeforeCollision = Last;
444 return TILE_TELEINWEAPON;
445 }
446
447 if(CheckPoint(x: ix, y: iy))
448 {
449 if(pOutCollision)
450 *pOutCollision = Pos;
451 if(pOutBeforeCollision)
452 *pOutBeforeCollision = Last;
453 return GetCollisionAt(x: ix, y: iy);
454 }
455
456 Last = Pos;
457 }
458 if(pOutCollision)
459 *pOutCollision = Pos1;
460 if(pOutBeforeCollision)
461 *pOutBeforeCollision = Pos1;
462 return 0;
463}
464
465// TODO: OPT: rewrite this smarter!
466void CCollision::MovePoint(vec2 *pInoutPos, vec2 *pInoutVel, float Elasticity, int *pBounces) const
467{
468 if(pBounces)
469 *pBounces = 0;
470
471 vec2 Pos = *pInoutPos;
472 vec2 Vel = *pInoutVel;
473 if(CheckPoint(Pos: Pos + Vel))
474 {
475 int Affected = 0;
476 if(CheckPoint(x: Pos.x + Vel.x, y: Pos.y))
477 {
478 pInoutVel->x *= -Elasticity;
479 if(pBounces)
480 (*pBounces)++;
481 Affected++;
482 }
483
484 if(CheckPoint(x: Pos.x, y: Pos.y + Vel.y))
485 {
486 pInoutVel->y *= -Elasticity;
487 if(pBounces)
488 (*pBounces)++;
489 Affected++;
490 }
491
492 if(Affected == 0)
493 {
494 pInoutVel->x *= -Elasticity;
495 pInoutVel->y *= -Elasticity;
496 }
497 }
498 else
499 {
500 *pInoutPos = Pos + Vel;
501 }
502}
503
504bool CCollision::TestBox(vec2 Pos, vec2 Size) const
505{
506 Size *= 0.5f;
507 if(CheckPoint(x: Pos.x - Size.x, y: Pos.y - Size.y))
508 return true;
509 if(CheckPoint(x: Pos.x + Size.x, y: Pos.y - Size.y))
510 return true;
511 if(CheckPoint(x: Pos.x - Size.x, y: Pos.y + Size.y))
512 return true;
513 if(CheckPoint(x: Pos.x + Size.x, y: Pos.y + Size.y))
514 return true;
515 return false;
516}
517
518bool CCollision::IsOnGround(vec2 Pos, float Size) const
519{
520 if(CheckPoint(x: Pos.x + Size / 2, y: Pos.y + Size / 2 + 5))
521 return true;
522 if(CheckPoint(x: Pos.x - Size / 2, y: Pos.y + Size / 2 + 5))
523 return true;
524
525 return false;
526}
527
528void CCollision::MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, vec2 Elasticity, bool *pGrounded) const
529{
530 // do the move
531 vec2 Pos = *pInoutPos;
532 vec2 Vel = *pInoutVel;
533
534 float Distance = length(a: Vel);
535 int Max = round_truncate(f: Distance);
536
537 if(Distance > 0.00001f)
538 {
539 float Fraction = 1.0f / (float)(Max + 1);
540 float ElasticityX = std::clamp(val: Elasticity.x, lo: -1.0f, hi: 1.0f);
541 float ElasticityY = std::clamp(val: Elasticity.y, lo: -1.0f, hi: 1.0f);
542
543 for(int i = 0; i <= Max; i++)
544 {
545 // Early break as optimization to stop checking for collisions for
546 // large distances after the obstacles we have already hit reduced
547 // our speed to exactly 0.
548 if(Vel == vec2(0, 0))
549 {
550 break;
551 }
552
553 vec2 NewPos = Pos + Vel * Fraction; // TODO: this row is not nice
554
555 // Fraction can be very small and thus the calculation has no effect, no
556 // reason to continue calculating.
557 if(NewPos == Pos)
558 {
559 break;
560 }
561
562 if(TestBox(Pos: vec2(NewPos.x, NewPos.y), Size))
563 {
564 int Hits = 0;
565
566 if(TestBox(Pos: vec2(Pos.x, NewPos.y), Size))
567 {
568 if(pGrounded && ElasticityY > 0 && Vel.y > 0)
569 *pGrounded = true;
570 NewPos.y = Pos.y;
571 Vel.y *= -ElasticityY;
572 Hits++;
573 }
574
575 if(TestBox(Pos: vec2(NewPos.x, Pos.y), Size))
576 {
577 NewPos.x = Pos.x;
578 Vel.x *= -ElasticityX;
579 Hits++;
580 }
581
582 // neither of the tests got a collision.
583 // this is a real _corner case_!
584 if(Hits == 0)
585 {
586 if(pGrounded && ElasticityY > 0 && Vel.y > 0)
587 *pGrounded = true;
588 NewPos.y = Pos.y;
589 Vel.y *= -ElasticityY;
590 NewPos.x = Pos.x;
591 Vel.x *= -ElasticityX;
592 }
593 }
594
595 Pos = NewPos;
596 }
597 }
598
599 *pInoutPos = Pos;
600 *pInoutVel = Vel;
601}
602
603// DDRace
604
605int CCollision::IsSolid(int x, int y) const
606{
607 const int Index = GetTile(x, y);
608 return Index == TILE_SOLID || Index == TILE_NOHOOK;
609}
610
611bool CCollision::IsThrough(int x, int y, int OffsetX, int OffsetY, vec2 Pos0, vec2 Pos1) const
612{
613 const int Index = GetPureMapIndex(x, y);
614 if(m_pFront && (m_pFront[Index].m_Index == TILE_THROUGH_ALL || m_pFront[Index].m_Index == TILE_THROUGH_CUT))
615 return true;
616 if(m_pFront && m_pFront[Index].m_Index == TILE_THROUGH_DIR && ((m_pFront[Index].m_Flags == ROTATION_0 && Pos0.y > Pos1.y) || (m_pFront[Index].m_Flags == ROTATION_90 && Pos0.x < Pos1.x) || (m_pFront[Index].m_Flags == ROTATION_180 && Pos0.y < Pos1.y) || (m_pFront[Index].m_Flags == ROTATION_270 && Pos0.x > Pos1.x)))
617 return true;
618 const int OffsetIndex = GetPureMapIndex(x: x + OffsetX, y: y + OffsetY);
619 return m_pTiles[OffsetIndex].m_Index == TILE_THROUGH || (m_pFront && m_pFront[OffsetIndex].m_Index == TILE_THROUGH);
620}
621
622bool CCollision::IsHookBlocker(int x, int y, vec2 Pos0, vec2 Pos1) const
623{
624 const int Index = GetPureMapIndex(x, y);
625 if(m_pTiles[Index].m_Index == TILE_THROUGH_ALL || (m_pFront && m_pFront[Index].m_Index == TILE_THROUGH_ALL))
626 return true;
627 if(m_pTiles[Index].m_Index == TILE_THROUGH_DIR && ((m_pTiles[Index].m_Flags == ROTATION_0 && Pos0.y < Pos1.y) ||
628 (m_pTiles[Index].m_Flags == ROTATION_90 && Pos0.x > Pos1.x) ||
629 (m_pTiles[Index].m_Flags == ROTATION_180 && Pos0.y > Pos1.y) ||
630 (m_pTiles[Index].m_Flags == ROTATION_270 && Pos0.x < Pos1.x)))
631 return true;
632 if(m_pFront && m_pFront[Index].m_Index == TILE_THROUGH_DIR && ((m_pFront[Index].m_Flags == ROTATION_0 && Pos0.y < Pos1.y) || (m_pFront[Index].m_Flags == ROTATION_90 && Pos0.x > Pos1.x) || (m_pFront[Index].m_Flags == ROTATION_180 && Pos0.y > Pos1.y) || (m_pFront[Index].m_Flags == ROTATION_270 && Pos0.x < Pos1.x)))
633 return true;
634 return false;
635}
636
637int CCollision::IsWallJump(int Index) const
638{
639 if(Index < 0)
640 return 0;
641
642 return m_pTiles[Index].m_Index == TILE_WALLJUMP;
643}
644
645int CCollision::IsNoLaser(int x, int y) const
646{
647 return (CCollision::GetTile(x, y) == TILE_NOLASER);
648}
649
650int CCollision::IsFrontNoLaser(int x, int y) const
651{
652 return (CCollision::GetFrontTile(x, y) == TILE_NOLASER);
653}
654
655int CCollision::IsTeleport(int Index) const
656{
657 if(Index < 0 || !m_pTele)
658 return 0;
659
660 if(m_pTele[Index].m_Type == TILE_TELEIN)
661 return m_pTele[Index].m_Number;
662
663 return 0;
664}
665
666int CCollision::IsEvilTeleport(int Index) const
667{
668 if(Index < 0 || !m_pTele)
669 return 0;
670
671 if(m_pTele[Index].m_Type == TILE_TELEINEVIL)
672 return m_pTele[Index].m_Number;
673
674 return 0;
675}
676
677bool CCollision::IsCheckTeleport(int Index) const
678{
679 if(Index < 0 || !m_pTele)
680 return false;
681 return m_pTele[Index].m_Type == TILE_TELECHECKIN;
682}
683
684bool CCollision::IsCheckEvilTeleport(int Index) const
685{
686 if(Index < 0 || !m_pTele)
687 return false;
688 return m_pTele[Index].m_Type == TILE_TELECHECKINEVIL;
689}
690
691int CCollision::IsTeleCheckpoint(int Index) const
692{
693 if(Index < 0 || !m_pTele)
694 return 0;
695
696 if(m_pTele[Index].m_Type == TILE_TELECHECK)
697 return m_pTele[Index].m_Number;
698
699 return 0;
700}
701
702int CCollision::IsTeleportWeapon(int Index) const
703{
704 if(Index < 0 || !m_pTele)
705 return 0;
706
707 if(m_pTele[Index].m_Type == TILE_TELEINWEAPON)
708 return m_pTele[Index].m_Number;
709
710 return 0;
711}
712
713int CCollision::IsTeleportHook(int Index) const
714{
715 if(Index < 0 || !m_pTele)
716 return 0;
717
718 if(m_pTele[Index].m_Type == TILE_TELEINHOOK)
719 return m_pTele[Index].m_Number;
720
721 return 0;
722}
723
724bool CCollision::IsSpeedup(int Index) const
725{
726 dbg_assert(Index >= 0, "Invalid speedup index %d", Index);
727 return m_pSpeedup && m_pSpeedup[Index].m_Force > 0;
728}
729
730int CCollision::IsTune(int Index) const
731{
732 if(Index < 0 || !m_pTune)
733 return 0;
734
735 if(m_pTune[Index].m_Type)
736 return m_pTune[Index].m_Number;
737
738 return 0;
739}
740
741void CCollision::GetSpeedup(int Index, vec2 *pDir, int *pForce, int *pMaxSpeed, int *pType) const
742{
743 if(Index < 0 || !m_pSpeedup)
744 return;
745 float Angle = m_pSpeedup[Index].m_Angle * (pi / 180.0f);
746 *pForce = m_pSpeedup[Index].m_Force;
747 *pType = m_pSpeedup[Index].m_Type;
748 *pDir = direction(angle: Angle);
749 if(pMaxSpeed)
750 *pMaxSpeed = m_pSpeedup[Index].m_MaxSpeed;
751}
752
753int CCollision::GetSwitchType(int Index) const
754{
755 if(Index < 0 || !m_pSwitch)
756 return 0;
757
758 if(m_pSwitch[Index].m_Type > 0)
759 return m_pSwitch[Index].m_Type;
760
761 return 0;
762}
763
764int CCollision::GetSwitchNumber(int Index) const
765{
766 if(Index < 0 || !m_pSwitch)
767 return 0;
768
769 if(m_pSwitch[Index].m_Type > 0 && m_pSwitch[Index].m_Number > 0 && m_pSwitch[Index].m_Number <= m_HighestSwitchNumber)
770 return m_pSwitch[Index].m_Number;
771
772 return 0;
773}
774
775int CCollision::GetSwitchDelay(int Index) const
776{
777 if(Index < 0 || !m_pSwitch)
778 return 0;
779
780 if(m_pSwitch[Index].m_Type > 0)
781 return m_pSwitch[Index].m_Delay;
782
783 return 0;
784}
785
786int CCollision::MoverSpeed(int x, int y, vec2 *pSpeed) const
787{
788 int Nx = std::clamp(val: x / 32, lo: 0, hi: m_Width - 1);
789 int Ny = std::clamp(val: y / 32, lo: 0, hi: m_Height - 1);
790 int Index = m_pTiles[Ny * m_Width + Nx].m_Index;
791
792 if(Index != TILE_CP && Index != TILE_CP_F)
793 {
794 return 0;
795 }
796
797 vec2 Target;
798 switch(m_pTiles[Ny * m_Width + Nx].m_Flags)
799 {
800 case ROTATION_0:
801 Target.x = 0.0f;
802 Target.y = -4.0f;
803 break;
804 case ROTATION_90:
805 Target.x = 4.0f;
806 Target.y = 0.0f;
807 break;
808 case ROTATION_180:
809 Target.x = 0.0f;
810 Target.y = 4.0f;
811 break;
812 case ROTATION_270:
813 Target.x = -4.0f;
814 Target.y = 0.0f;
815 break;
816 default:
817 Target = vec2(0.0f, 0.0f);
818 break;
819 }
820 if(Index == TILE_CP_F)
821 {
822 Target *= 4.0f;
823 }
824 *pSpeed = Target;
825 return Index;
826}
827
828bool CCollision::HasHookTeleIns() const
829{
830 return m_HasHookTeleIns || (g_Config.m_SvOldTeleportHook && !m_TeleIns.empty());
831}
832
833int CCollision::GetPureMapIndex(float x, float y) const
834{
835 int Nx = std::clamp(val: round_to_int(f: x) / 32, lo: 0, hi: m_Width - 1);
836 int Ny = std::clamp(val: round_to_int(f: y) / 32, lo: 0, hi: m_Height - 1);
837 return Ny * m_Width + Nx;
838}
839
840bool CCollision::TileExists(int Index) const
841{
842 if(Index < 0)
843 return false;
844
845 if((m_pTiles[Index].m_Index >= TILE_FREEZE && m_pTiles[Index].m_Index <= TILE_TELE_LASER_DISABLE) || (m_pTiles[Index].m_Index >= TILE_LFREEZE && m_pTiles[Index].m_Index <= TILE_LUNFREEZE))
846 return true;
847 if(m_pFront && ((m_pFront[Index].m_Index >= TILE_FREEZE && m_pFront[Index].m_Index <= TILE_TELE_LASER_DISABLE) || (m_pFront[Index].m_Index >= TILE_LFREEZE && m_pFront[Index].m_Index <= TILE_LUNFREEZE)))
848 return true;
849 if(m_pTele && (m_pTele[Index].m_Type == TILE_TELEIN || m_pTele[Index].m_Type == TILE_TELEINEVIL || m_pTele[Index].m_Type == TILE_TELECHECKINEVIL || m_pTele[Index].m_Type == TILE_TELECHECK || m_pTele[Index].m_Type == TILE_TELECHECKIN))
850 return true;
851 if(m_pSpeedup && m_pSpeedup[Index].m_Force > 0)
852 return true;
853 if(m_pDoor && m_pDoor[Index].m_Index)
854 return true;
855 if(m_pSwitch && m_pSwitch[Index].m_Type)
856 return true;
857 if(m_pTune && m_pTune[Index].m_Type)
858 return true;
859 return TileExistsNext(Index);
860}
861
862bool CCollision::TileExistsNext(int Index) const
863{
864 if(Index < 0)
865 return false;
866 int TileOnTheLeft = (Index - 1 > 0) ? Index - 1 : Index;
867 int TileOnTheRight = (Index + 1 < m_Width * m_Height) ? Index + 1 : Index;
868 int TileBelow = (Index + m_Width < m_Width * m_Height) ? Index + m_Width : Index;
869 int TileAbove = (Index - m_Width > 0) ? Index - m_Width : Index;
870
871 if((m_pTiles[TileOnTheRight].m_Index == TILE_STOP && m_pTiles[TileOnTheRight].m_Flags == ROTATION_270) || (m_pTiles[TileOnTheLeft].m_Index == TILE_STOP && m_pTiles[TileOnTheLeft].m_Flags == ROTATION_90))
872 return true;
873 if((m_pTiles[TileBelow].m_Index == TILE_STOP && m_pTiles[TileBelow].m_Flags == ROTATION_0) || (m_pTiles[TileAbove].m_Index == TILE_STOP && m_pTiles[TileAbove].m_Flags == ROTATION_180))
874 return true;
875 if(m_pTiles[TileOnTheRight].m_Index == TILE_STOPA || m_pTiles[TileOnTheLeft].m_Index == TILE_STOPA || ((m_pTiles[TileOnTheRight].m_Index == TILE_STOPS || m_pTiles[TileOnTheLeft].m_Index == TILE_STOPS)))
876 return true;
877 if(m_pTiles[TileBelow].m_Index == TILE_STOPA || m_pTiles[TileAbove].m_Index == TILE_STOPA || ((m_pTiles[TileBelow].m_Index == TILE_STOPS || m_pTiles[TileAbove].m_Index == TILE_STOPS) && m_pTiles[TileBelow].m_Flags | ROTATION_180 | ROTATION_0))
878 return true;
879 if(m_pFront)
880 {
881 if(m_pFront[TileOnTheRight].m_Index == TILE_STOPA || m_pFront[TileOnTheLeft].m_Index == TILE_STOPA || ((m_pFront[TileOnTheRight].m_Index == TILE_STOPS || m_pFront[TileOnTheLeft].m_Index == TILE_STOPS)))
882 return true;
883 if(m_pFront[TileBelow].m_Index == TILE_STOPA || m_pFront[TileAbove].m_Index == TILE_STOPA || ((m_pFront[TileBelow].m_Index == TILE_STOPS || m_pFront[TileAbove].m_Index == TILE_STOPS) && m_pFront[TileBelow].m_Flags | ROTATION_180 | ROTATION_0))
884 return true;
885 if((m_pFront[TileOnTheRight].m_Index == TILE_STOP && m_pFront[TileOnTheRight].m_Flags == ROTATION_270) || (m_pFront[TileOnTheLeft].m_Index == TILE_STOP && m_pFront[TileOnTheLeft].m_Flags == ROTATION_90))
886 return true;
887 if((m_pFront[TileBelow].m_Index == TILE_STOP && m_pFront[TileBelow].m_Flags == ROTATION_0) || (m_pFront[TileAbove].m_Index == TILE_STOP && m_pFront[TileAbove].m_Flags == ROTATION_180))
888 return true;
889 }
890 if(m_pDoor)
891 {
892 if(m_pDoor[TileOnTheRight].m_Index == TILE_STOPA || m_pDoor[TileOnTheLeft].m_Index == TILE_STOPA || ((m_pDoor[TileOnTheRight].m_Index == TILE_STOPS || m_pDoor[TileOnTheLeft].m_Index == TILE_STOPS)))
893 return true;
894 if(m_pDoor[TileBelow].m_Index == TILE_STOPA || m_pDoor[TileAbove].m_Index == TILE_STOPA || ((m_pDoor[TileBelow].m_Index == TILE_STOPS || m_pDoor[TileAbove].m_Index == TILE_STOPS) && m_pDoor[TileBelow].m_Flags | ROTATION_180 | ROTATION_0))
895 return true;
896 if((m_pDoor[TileOnTheRight].m_Index == TILE_STOP && m_pDoor[TileOnTheRight].m_Flags == ROTATION_270) || (m_pDoor[TileOnTheLeft].m_Index == TILE_STOP && m_pDoor[TileOnTheLeft].m_Flags == ROTATION_90))
897 return true;
898 if((m_pDoor[TileBelow].m_Index == TILE_STOP && m_pDoor[TileBelow].m_Flags == ROTATION_0) || (m_pDoor[TileAbove].m_Index == TILE_STOP && m_pDoor[TileAbove].m_Flags == ROTATION_180))
899 return true;
900 }
901 return false;
902}
903
904static int TileIndexFromCoord(float Coord, int Size)
905{
906 if(!(Coord > 0.0f))
907 return 0;
908 return (int)std::min(a: (double)Coord / 32.0, b: (double)(Size - 1));
909}
910
911int CCollision::GetMapIndex(vec2 Pos) const
912{
913 int Nx = TileIndexFromCoord(Coord: Pos.x, Size: m_Width);
914 int Ny = TileIndexFromCoord(Coord: Pos.y, Size: m_Height);
915 int Index = Ny * m_Width + Nx;
916
917 if(TileExists(Index))
918 return Index;
919 else
920 return -1;
921}
922
923std::vector<int> CCollision::GetMapIndices(vec2 PrevPos, vec2 Pos, unsigned MaxIndices) const
924{
925 std::vector<int> vIndices;
926 float d = distance(a: PrevPos, b: Pos);
927 int End(d + 1);
928 if(!d)
929 {
930 int Nx = std::clamp(val: (int)Pos.x / 32, lo: 0, hi: m_Width - 1);
931 int Ny = std::clamp(val: (int)Pos.y / 32, lo: 0, hi: m_Height - 1);
932 int Index = Ny * m_Width + Nx;
933
934 if(TileExists(Index))
935 {
936 vIndices.push_back(x: Index);
937 return vIndices;
938 }
939 else
940 return vIndices;
941 }
942 else
943 {
944 int LastIndex = 0;
945 for(int i = 0; i < End; i++)
946 {
947 float a = i / d;
948 vec2 Tmp = mix(a: PrevPos, b: Pos, amount: a);
949 int Nx = std::clamp(val: (int)Tmp.x / 32, lo: 0, hi: m_Width - 1);
950 int Ny = std::clamp(val: (int)Tmp.y / 32, lo: 0, hi: m_Height - 1);
951 int Index = Ny * m_Width + Nx;
952 if(TileExists(Index) && LastIndex != Index)
953 {
954 if(MaxIndices && vIndices.size() > MaxIndices)
955 return vIndices;
956 vIndices.push_back(x: Index);
957 LastIndex = Index;
958 }
959 }
960
961 return vIndices;
962 }
963}
964
965vec2 CCollision::GetPos(int Index) const
966{
967 if(Index < 0)
968 return vec2(0, 0);
969
970 int x = Index % m_Width;
971 int y = Index / m_Width;
972 return vec2(x * 32 + 16, y * 32 + 16);
973}
974
975int CCollision::GetTileIndex(int Index) const
976{
977 if(Index < 0)
978 return 0;
979 return m_pTiles[Index].m_Index;
980}
981
982int CCollision::GetFrontTileIndex(int Index) const
983{
984 if(Index < 0 || !m_pFront)
985 return 0;
986 return m_pFront[Index].m_Index;
987}
988
989int CCollision::GetTileFlags(int Index) const
990{
991 if(Index < 0)
992 return 0;
993 return m_pTiles[Index].m_Flags;
994}
995
996int CCollision::GetFrontTileFlags(int Index) const
997{
998 if(Index < 0 || !m_pFront)
999 return 0;
1000 return m_pFront[Index].m_Flags;
1001}
1002
1003int CCollision::GetIndex(int Nx, int Ny) const
1004{
1005 return m_pTiles[Ny * m_Width + Nx].m_Index;
1006}
1007
1008int CCollision::GetIndex(vec2 PrevPos, vec2 Pos) const
1009{
1010 float Distance = distance(a: PrevPos, b: Pos);
1011
1012 if(!Distance)
1013 {
1014 int Nx = std::clamp(val: (int)Pos.x / 32, lo: 0, hi: m_Width - 1);
1015 int Ny = std::clamp(val: (int)Pos.y / 32, lo: 0, hi: m_Height - 1);
1016
1017 if((m_pTele) ||
1018 (m_pSpeedup && m_pSpeedup[Ny * m_Width + Nx].m_Force > 0))
1019 {
1020 return Ny * m_Width + Nx;
1021 }
1022 }
1023
1024 const int DistanceRounded = std::ceil(x: Distance);
1025 for(int i = 0; i < DistanceRounded; i++)
1026 {
1027 float a = (float)i / Distance;
1028 vec2 Tmp = mix(a: PrevPos, b: Pos, amount: a);
1029 int Nx = std::clamp(val: (int)Tmp.x / 32, lo: 0, hi: m_Width - 1);
1030 int Ny = std::clamp(val: (int)Tmp.y / 32, lo: 0, hi: m_Height - 1);
1031 if((m_pTele) ||
1032 (m_pSpeedup && m_pSpeedup[Ny * m_Width + Nx].m_Force > 0))
1033 {
1034 return Ny * m_Width + Nx;
1035 }
1036 }
1037
1038 return -1;
1039}
1040
1041int CCollision::GetFrontIndex(int Nx, int Ny) const
1042{
1043 if(!m_pFront)
1044 return 0;
1045 return m_pFront[Ny * m_Width + Nx].m_Index;
1046}
1047
1048int CCollision::GetFrontTile(int x, int y) const
1049{
1050 if(!m_pFront)
1051 return 0;
1052 int Nx = std::clamp(val: x / 32, lo: 0, hi: m_Width - 1);
1053 int Ny = std::clamp(val: y / 32, lo: 0, hi: m_Height - 1);
1054 if(m_pFront[Ny * m_Width + Nx].m_Index == TILE_DEATH || m_pFront[Ny * m_Width + Nx].m_Index == TILE_NOLASER)
1055 return m_pFront[Ny * m_Width + Nx].m_Index;
1056 else
1057 return 0;
1058}
1059
1060int CCollision::Entity(int x, int y, int Layer) const
1061{
1062 if(x < 0 || x >= m_Width || y < 0 || y >= m_Height)
1063 return 0;
1064
1065 const int Index = y * m_Width + x;
1066 switch(Layer)
1067 {
1068 case LAYER_GAME:
1069 return m_pTiles[Index].m_Index - ENTITY_OFFSET;
1070 case LAYER_FRONT:
1071 return m_pFront[Index].m_Index - ENTITY_OFFSET;
1072 case LAYER_SWITCH:
1073 return m_pSwitch[Index].m_Type - ENTITY_OFFSET;
1074 case LAYER_TELE:
1075 return m_pTele[Index].m_Type - ENTITY_OFFSET;
1076 case LAYER_SPEEDUP:
1077 return m_pSpeedup[Index].m_Type - ENTITY_OFFSET;
1078 case LAYER_TUNE:
1079 return m_pTune[Index].m_Type - ENTITY_OFFSET;
1080 default:
1081 dbg_assert_failed("Invalid Layer: %d", Layer);
1082 }
1083}
1084
1085void CCollision::SetCollisionAt(float x, float y, int Index)
1086{
1087 int Nx = std::clamp(val: round_to_int(f: x) / 32, lo: 0, hi: m_Width - 1);
1088 int Ny = std::clamp(val: round_to_int(f: y) / 32, lo: 0, hi: m_Height - 1);
1089
1090 m_pTiles[Ny * m_Width + Nx].m_Index = Index;
1091}
1092
1093void CCollision::SetDoorCollisionAt(float x, float y, unsigned char Type, unsigned char Flags, unsigned char Number)
1094{
1095 if(!m_pDoor)
1096 return;
1097 int Nx = std::clamp(val: round_to_int(f: x) / 32, lo: 0, hi: m_Width - 1);
1098 int Ny = std::clamp(val: round_to_int(f: y) / 32, lo: 0, hi: m_Height - 1);
1099
1100 m_pDoor[Ny * m_Width + Nx].m_Index = Type;
1101 m_pDoor[Ny * m_Width + Nx].m_Flags = Flags;
1102 m_pDoor[Ny * m_Width + Nx].m_Number = Number;
1103}
1104
1105void CCollision::GetDoorTile(int Index, CDoorTile *pDoorTile) const
1106{
1107 if(!m_pDoor || Index < 0 || !m_pDoor[Index].m_Index)
1108 {
1109 pDoorTile->m_Index = 0;
1110 pDoorTile->m_Flags = 0;
1111 pDoorTile->m_Number = 0;
1112 return;
1113 }
1114 *pDoorTile = m_pDoor[Index];
1115}
1116
1117void ThroughOffset(vec2 Pos0, vec2 Pos1, int *pOffsetX, int *pOffsetY)
1118{
1119 float x = Pos0.x - Pos1.x;
1120 float y = Pos0.y - Pos1.y;
1121 if(absolute(a: x) > absolute(a: y))
1122 {
1123 if(x < 0)
1124 {
1125 *pOffsetX = -32;
1126 *pOffsetY = 0;
1127 }
1128 else
1129 {
1130 *pOffsetX = 32;
1131 *pOffsetY = 0;
1132 }
1133 }
1134 else
1135 {
1136 if(y < 0)
1137 {
1138 *pOffsetX = 0;
1139 *pOffsetY = -32;
1140 }
1141 else
1142 {
1143 *pOffsetX = 0;
1144 *pOffsetY = 32;
1145 }
1146 }
1147}
1148
1149int CCollision::IntersectNoLaser(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision) const
1150{
1151 float Distance = distance(a: Pos0, b: Pos1);
1152 vec2 Last = Pos0;
1153
1154 const int DistanceRounded = std::ceil(x: Distance);
1155 for(int i = 0; i < DistanceRounded; i++)
1156 {
1157 float a = i / Distance;
1158 vec2 Pos = mix(a: Pos0, b: Pos1, amount: a);
1159 int Nx = std::clamp(val: round_to_int(f: Pos.x) / 32, lo: 0, hi: m_Width - 1);
1160 int Ny = std::clamp(val: round_to_int(f: Pos.y) / 32, lo: 0, hi: m_Height - 1);
1161 if(GetIndex(Nx, Ny) == TILE_SOLID || GetIndex(Nx, Ny) == TILE_NOHOOK || GetIndex(Nx, Ny) == TILE_NOLASER || GetFrontIndex(Nx, Ny) == TILE_NOLASER)
1162 {
1163 if(pOutCollision)
1164 *pOutCollision = Pos;
1165 if(pOutBeforeCollision)
1166 *pOutBeforeCollision = Last;
1167 if(GetFrontIndex(Nx, Ny) == TILE_NOLASER)
1168 return GetFrontCollisionAt(x: Pos.x, y: Pos.y);
1169 else
1170 return GetCollisionAt(x: Pos.x, y: Pos.y);
1171 }
1172 Last = Pos;
1173 }
1174 if(pOutCollision)
1175 *pOutCollision = Pos1;
1176 if(pOutBeforeCollision)
1177 *pOutBeforeCollision = Pos1;
1178 return 0;
1179}
1180
1181int CCollision::IntersectNoLaserNoWalls(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision) const
1182{
1183 float Distance = distance(a: Pos0, b: Pos1);
1184 vec2 Last = Pos0;
1185
1186 const int DistanceRounded = std::ceil(x: Distance);
1187 for(int i = 0; i < DistanceRounded; i++)
1188 {
1189 float a = (float)i / Distance;
1190 vec2 Pos = mix(a: Pos0, b: Pos1, amount: a);
1191 if(IsNoLaser(x: round_to_int(f: Pos.x), y: round_to_int(f: Pos.y)) || IsFrontNoLaser(x: round_to_int(f: Pos.x), y: round_to_int(f: Pos.y)))
1192 {
1193 if(pOutCollision)
1194 *pOutCollision = Pos;
1195 if(pOutBeforeCollision)
1196 *pOutBeforeCollision = Last;
1197 if(IsNoLaser(x: round_to_int(f: Pos.x), y: round_to_int(f: Pos.y)))
1198 return GetCollisionAt(x: Pos.x, y: Pos.y);
1199 else
1200 return GetFrontCollisionAt(x: Pos.x, y: Pos.y);
1201 }
1202 Last = Pos;
1203 }
1204 if(pOutCollision)
1205 *pOutCollision = Pos1;
1206 if(pOutBeforeCollision)
1207 *pOutBeforeCollision = Pos1;
1208 return 0;
1209}
1210
1211int CCollision::IntersectAir(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision) const
1212{
1213 float Distance = distance(a: Pos0, b: Pos1);
1214 vec2 Last = Pos0;
1215
1216 const int DistanceRounded = std::ceil(x: Distance);
1217 for(int i = 0; i < DistanceRounded; i++)
1218 {
1219 float a = (float)i / Distance;
1220 vec2 Pos = mix(a: Pos0, b: Pos1, amount: a);
1221 if(IsSolid(x: round_to_int(f: Pos.x), y: round_to_int(f: Pos.y)) || (!GetTile(x: round_to_int(f: Pos.x), y: round_to_int(f: Pos.y)) && !GetFrontTile(x: round_to_int(f: Pos.x), y: round_to_int(f: Pos.y))))
1222 {
1223 if(pOutCollision)
1224 *pOutCollision = Pos;
1225 if(pOutBeforeCollision)
1226 *pOutBeforeCollision = Last;
1227 if(!GetTile(x: round_to_int(f: Pos.x), y: round_to_int(f: Pos.y)) && !GetFrontTile(x: round_to_int(f: Pos.x), y: round_to_int(f: Pos.y)))
1228 return -1;
1229 else if(!GetTile(x: round_to_int(f: Pos.x), y: round_to_int(f: Pos.y)))
1230 return GetTile(x: round_to_int(f: Pos.x), y: round_to_int(f: Pos.y));
1231 else
1232 return GetFrontTile(x: round_to_int(f: Pos.x), y: round_to_int(f: Pos.y));
1233 }
1234 Last = Pos;
1235 }
1236 if(pOutCollision)
1237 *pOutCollision = Pos1;
1238 if(pOutBeforeCollision)
1239 *pOutBeforeCollision = Pos1;
1240 return 0;
1241}
1242
1243int CCollision::IsTimeCheckpoint(int Index) const
1244{
1245 if(Index < 0)
1246 return -1;
1247
1248 int z = m_pTiles[Index].m_Index;
1249 if(z >= TILE_TIME_CHECKPOINT_FIRST && z <= TILE_TIME_CHECKPOINT_LAST)
1250 return z - TILE_TIME_CHECKPOINT_FIRST;
1251 return -1;
1252}
1253
1254int CCollision::IsFrontTimeCheckpoint(int Index) const
1255{
1256 if(Index < 0 || !m_pFront)
1257 return -1;
1258
1259 int z = m_pFront[Index].m_Index;
1260 if(z >= TILE_TIME_CHECKPOINT_FIRST && z <= TILE_TIME_CHECKPOINT_LAST)
1261 return z - TILE_TIME_CHECKPOINT_FIRST;
1262 return -1;
1263}
1264
1265vec2 CCollision::TeleAllGet(int Number, size_t Offset)
1266{
1267 if(m_TeleIns.contains(x: Number))
1268 {
1269 if(m_TeleIns[Number].size() > Offset)
1270 return m_TeleIns[Number][Offset];
1271 else
1272 Offset -= m_TeleIns[Number].size();
1273 }
1274 if(m_TeleOuts.contains(x: Number))
1275 {
1276 if(m_TeleOuts[Number].size() > Offset)
1277 return m_TeleOuts[Number][Offset];
1278 else
1279 Offset -= m_TeleOuts[Number].size();
1280 }
1281 if(m_TeleCheckOuts.contains(x: Number))
1282 {
1283 if(m_TeleCheckOuts[Number].size() > Offset)
1284 return m_TeleCheckOuts[Number][Offset];
1285 else
1286 Offset -= m_TeleCheckOuts[Number].size();
1287 }
1288 if(m_TeleOthers.contains(x: Number))
1289 {
1290 if(m_TeleOthers[Number].size() > Offset)
1291 return m_TeleOthers[Number][Offset];
1292 }
1293 return vec2(-1, -1);
1294}
1295
1296size_t CCollision::TeleAllSize(int Number)
1297{
1298 size_t Total = 0;
1299 if(m_TeleIns.contains(x: Number))
1300 Total += m_TeleIns[Number].size();
1301 if(m_TeleOuts.contains(x: Number))
1302 Total += m_TeleOuts[Number].size();
1303 if(m_TeleCheckOuts.contains(x: Number))
1304 Total += m_TeleCheckOuts[Number].size();
1305 if(m_TeleOthers.contains(x: Number))
1306 Total += m_TeleOthers[Number].size();
1307 return Total;
1308}
1309