1 /*
2 	C-Dogs SDL
3 	A port of the legendary (and fun) action/arcade cdogs.
4 	Copyright (C) 1995 Ronny Wester
5 	Copyright (C) 2003 Jeremy Chin
6 	Copyright (C) 2003-2007 Lucas Martin-King
7 
8 	This program is free software; you can redistribute it and/or modify
9 	it under the terms of the GNU General Public License as published by
10 	the Free Software Foundation; either version 2 of the License, or
11 	(at your option) any later version.
12 
13 	This program is distributed in the hope that it will be useful,
14 	but WITHOUT ANY WARRANTY; without even the implied warranty of
15 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 	GNU General Public License for more details.
17 
18 	You should have received a copy of the GNU General Public License
19 	along with this program; if not, write to the Free Software
20 	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 
22 	This file incorporates work covered by the following copyright and
23 	permission notice:
24 
25 	Copyright (c) 2013-2015, 2017, 2020 Cong Xu
26 	All rights reserved.
27 
28 	Redistribution and use in source and binary forms, with or without
29 	modification, are permitted provided that the following conditions are met:
30 
31 	Redistributions of source code must retain the above copyright notice, this
32 	list of conditions and the following disclaimer.
33 	Redistributions in binary form must reproduce the above copyright notice,
34 	this list of conditions and the following disclaimer in the documentation
35 	and/or other materials provided with the distribution.
36 
37 	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
41 	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 	POSSIBILITY OF SUCH DAMAGE.
48 */
49 #pragma once
50 
51 #include "actors.h"
52 #include "map.h"
53 
54 typedef struct
55 {
56 	AllyCollision allyCollision;
57 	// Cache of tiles to check for potential collisions, of tile coords
58 	CArray tileCache; // of struct vec2i
59 } CollisionSystem;
60 
61 extern CollisionSystem gCollisionSystem;
62 
63 void CollisionSystemInit(CollisionSystem *cs);
64 void CollisionSystemReset(CollisionSystem *cs);
65 void CollisionSystemTerminate(CollisionSystem *cs);
66 
67 #define HitWall(x, y)                                                         \
68 	(!TileCanWalk(MapGetTile(                                                 \
69 		&gMap, svec2i((int)(x) / TILE_WIDTH, (int)(y) / TILE_HEIGHT))))
70 
71 // Which "team" the actor's on, for collision
72 // Actors on the same team don't have to collide
73 typedef enum
74 {
75 	COLLISIONTEAM_NONE,
76 	COLLISIONTEAM_GOOD,
77 	COLLISIONTEAM_BAD
78 } CollisionTeam;
79 
80 CollisionTeam CalcCollisionTeam(const bool isActor, const TActor *actor);
81 
82 // Parameters that determine whether two objects can collide
83 // TODO: replace with single layer mask
84 typedef struct
85 {
86 	int ThingMask;
87 	CollisionTeam Team;
88 	bool IsPVP;
89 	bool AllActors;
90 } CollisionParams;
91 
92 bool IsCollisionWithWall(const struct vec2 pos, const struct vec2i size2);
93 // Check collision of an object with a diamond shape
94 bool IsCollisionDiamond(
95 	const Map *map, const struct vec2 pos, const struct vec2i size2);
96 
97 // Get all Thing that overlap with a target Thing, with callback.
98 // The callback returns bool continue, as multiple callbacks can result.
99 typedef bool (*CollideItemFunc)(
100 	Thing *, void *, const struct vec2, const struct vec2, const struct vec2);
101 typedef bool (*CheckWallFunc)(const struct vec2i);
102 typedef bool (*CollideWallFunc)(
103 	const struct vec2i, void *, const struct vec2, const struct vec2);
104 void OverlapThings(
105 	const Thing *item, const struct vec2 pos, const struct vec2 vel,
106 	const struct vec2i size, const CollisionParams params,
107 	CollideItemFunc func, void *data, CheckWallFunc checkWallFunc,
108 	CollideWallFunc wallFunc, void *wallData);
109 // Get the first Thing that overlaps
110 Thing *OverlapGetFirstItem(
111 	const Thing *item, const struct vec2 pos, const struct vec2i size,
112 	const struct vec2 vel, const CollisionParams params);
113 
114 bool AABBOverlap(
115 	const struct vec2 pos1, const struct vec2 pos2, const struct vec2i size1,
116 	const struct vec2i size2);
117 
118 // Resolve wall bounces
119 void GetWallBouncePosVel(
120 	const struct vec2 pos, const struct vec2 vel, const struct vec2 colPos,
121 	const struct vec2 colNormal, struct vec2 *outPos, struct vec2 *outVel);
122