| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
#include "collision.h" |
| 50 |
|
| 51 |
#include "actors.h" |
| 52 |
#include "algorithms.h" |
| 53 |
#include "campaigns.h" |
| 54 |
#include "config.h" |
| 55 |
#include "minkowski_hex.h" |
| 56 |
#include "objs.h" |
| 57 |
|
| 58 |
static void TileCacheInit(CArray *tc) |
| 59 |
{ |
| 60 |
CArrayInit(tc, sizeof(struct vec2i)); |
| 61 |
} |
| 62 |
static void TileCacheReset(CArray *tc) |
| 63 |
{ |
| 64 |
CArrayClear(tc); |
| 65 |
} |
| 66 |
static void TileCacheTerminate(CArray *tc) |
| 67 |
{ |
| 68 |
CArrayTerminate(tc); |
| 69 |
} |
| 70 |
static void TileCacheAddImpl( |
| 71 |
CArray *tc, const struct vec2i v, const bool addAdjacents); |
| 72 |
static void TileCacheAdd(CArray *tc, const struct vec2i v) |
| 73 |
{ |
| 74 |
TileCacheAddImpl(tc, v, true); |
| 75 |
} |
| 76 |
static void TileCacheAddImpl( |
| 77 |
CArray *tc, const struct vec2i v, const bool addAdjacents) |
| 78 |
{ |
| 79 |
if (!MapIsTileIn(&gMap, v)) |
| 80 |
{ |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
CA_FOREACH(const struct vec2i, t, *tc) |
| 85 |
if (t->y > v.y || (t->y == v.y && t->x > v.x)) |
| 86 |
{ |
| 87 |
CArrayInsert(tc, _ca_index, &v); |
| 88 |
break; |
| 89 |
} |
| 90 |
else if (svec2i_is_equal(*t, v)) |
| 91 |
{ |
| 92 |
|
| 93 |
return; |
| 94 |
} |
| 95 |
CA_FOREACH_END() |
| 96 |
CArrayPushBack(tc, &v); |
| 97 |
|
| 98 |
|
| 99 |
if (addAdjacents) |
| 100 |
{ |
| 101 |
struct vec2i dv; |
| 102 |
for (dv.y = -1; dv.y <= 1; dv.y++) |
| 103 |
{ |
| 104 |
for (dv.x = -1; dv.x <= 1; dv.x++) |
| 105 |
{ |
| 106 |
if (svec2i_is_zero(dv)) |
| 107 |
{ |
| 108 |
continue; |
| 109 |
} |
| 110 |
const struct vec2i dtv = svec2i_add(v, dv); |
| 111 |
TileCacheAddImpl(tc, dtv, false); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
CollisionSystem gCollisionSystem; |
| 118 |
|
| 119 |
void CollisionSystemInit(CollisionSystem *cs) |
| 120 |
{ |
| 121 |
CollisionSystemReset(cs); |
| 122 |
TileCacheInit(&cs->tileCache); |
| 123 |
} |
| 124 |
void CollisionSystemReset(CollisionSystem *cs) |
| 125 |
{ |
| 126 |
cs->allyCollision = ConfigGetEnum(&gConfig, "Game.AllyCollision"); |
| 127 |
} |
| 128 |
void CollisionSystemTerminate(CollisionSystem *cs) |
| 129 |
{ |
| 130 |
TileCacheTerminate(&cs->tileCache); |
| 131 |
} |
| 132 |
|
| 133 |
CollisionTeam CalcCollisionTeam(const bool isActor, const TActor *actor) |
| 134 |
{ |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
if (!isActor || (actor->flags & (FLAGS_PRISONER | FLAGS_VICTIM)) || |
| 139 |
IsPVP(gCampaign.Entry.Mode)) |
| 140 |
{ |
| 141 |
return COLLISIONTEAM_NONE; |
| 142 |
} |
| 143 |
if (actor->pilotUID != actor->uid && actor->pilotUID >= 0) |
| 144 |
{ |
| 145 |
|
| 146 |
actor = ActorGetByUID(actor->pilotUID); |
| 147 |
} |
| 148 |
if (actor->PlayerUID >= 0 || (actor->flags & FLAGS_GOOD_GUY)) |
| 149 |
{ |
| 150 |
return COLLISIONTEAM_GOOD; |
| 151 |
} |
| 152 |
return COLLISIONTEAM_BAD; |
| 153 |
} |
| 154 |
|
| 155 |
bool IsCollisionWithWall(const struct vec2 pos, const struct vec2i size2) |
| 156 |
{ |
| 157 |
const struct vec2i size = svec2i_scale_divide(size2, 2); |
| 158 |
if (pos.x - size.x < 0 || pos.y - size.y < 0 || |
| 159 |
pos.x + size.x >= gMap.Size.x * TILE_WIDTH || |
| 160 |
pos.y + size.y >= gMap.Size.y * TILE_HEIGHT) |
| 161 |
{ |
| 162 |
return true; |
| 163 |
} |
| 164 |
if (HitWall((int)pos.x - size.x, (int)pos.y - size.y) || |
| 165 |
HitWall((int)pos.x - size.x, (int)pos.y) || |
| 166 |
HitWall((int)pos.x - size.x, (int)pos.y + size.y) || |
| 167 |
HitWall((int)pos.x, (int)pos.y + size.y) || |
| 168 |
HitWall((int)pos.x + size.x, (int)pos.y + size.y) || |
| 169 |
HitWall((int)pos.x + size.x, (int)pos.y) || |
| 170 |
HitWall((int)pos.x + size.x, (int)pos.y - size.y) || |
| 171 |
HitWall((int)pos.x, (int)pos.y - size.y)) |
| 172 |
{ |
| 173 |
return true; |
| 174 |
} |
| 175 |
return false; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
bool IsCollisionDiamond( |
| 195 |
const Map *map, const struct vec2 pos, const struct vec2i size2) |
| 196 |
{ |
| 197 |
const struct vec2i mapSize = |
| 198 |
svec2i(map->Size.x * TILE_WIDTH, map->Size.y * TILE_HEIGHT); |
| 199 |
const struct vec2i size = svec2i_scale_divide(size2, 2); |
| 200 |
if (pos.x - size.x < 0 || pos.x + size.x >= mapSize.x || |
| 201 |
pos.y - size.y < 0 || pos.y + size.y >= mapSize.y) |
| 202 |
{ |
| 203 |
return true; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
CASSERT(size.x >= size.y, "not implemented, taller than wider diamond"); |
| 208 |
const float gradient = (float)size.y / size.x; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
for (int i = 0; i < size.x; i++) |
| 214 |
{ |
| 215 |
const float y = (-size.x + i) * gradient; |
| 216 |
const struct vec2 p = svec2_add(pos, svec2((float)i, y)); |
| 217 |
if (HitWall(p.x, p.y)) |
| 218 |
{ |
| 219 |
return true; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
for (int i = 0; i < size.x; i++) |
| 224 |
{ |
| 225 |
const float y = i * gradient; |
| 226 |
const struct vec2 p = svec2_add(pos, svec2(size.x - (float)i, y)); |
| 227 |
if (HitWall(p.x, p.y)) |
| 228 |
{ |
| 229 |
return true; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
for (int i = 0; i < size.x; i++) |
| 234 |
{ |
| 235 |
const float y = (size.x - i) * gradient; |
| 236 |
const struct vec2 p = svec2_add(pos, svec2((float)-i, y)); |
| 237 |
if (HitWall(p.x, p.y)) |
| 238 |
{ |
| 239 |
return true; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
for (int i = 0; i < size.x; i++) |
| 244 |
{ |
| 245 |
const float y = -i * gradient; |
| 246 |
const struct vec2 p = svec2_add(pos, svec2((float)-size.x + i, y)); |
| 247 |
if (HitWall(p.x, p.y)) |
| 248 |
{ |
| 249 |
return true; |
| 250 |
} |
| 251 |
} |
| 252 |
return false; |
| 253 |
} |
| 254 |
|
| 255 |
bool AABBOverlap( |
| 256 |
const struct vec2 pos1, const struct vec2 pos2, const struct vec2i size1, |
| 257 |
const struct vec2i size2) |
| 258 |
{ |
| 259 |
|
| 260 |
const struct vec2 d = |
| 261 |
svec2(fabsf(pos1.x - pos2.x), fabsf(pos1.y - pos2.y)); |
| 262 |
const struct vec2i r = svec2i_scale_divide(svec2i_add(size1, size2), 2); |
| 263 |
return d.x < r.x && d.y < r.y; |
| 264 |
} |
| 265 |
|
| 266 |
static bool CollisionIsOnSameTeam( |
| 267 |
const Thing *i, const CollisionTeam team, const bool isPVP) |
| 268 |
{ |
| 269 |
if (gCollisionSystem.allyCollision == ALLYCOLLISION_NORMAL) |
| 270 |
{ |
| 271 |
return false; |
| 272 |
} |
| 273 |
CollisionTeam itemTeam = COLLISIONTEAM_NONE; |
| 274 |
if (i->kind == KIND_CHARACTER) |
| 275 |
{ |
| 276 |
const TActor *a = CArrayGet(&gActors, i->id); |
| 277 |
itemTeam = CalcCollisionTeam(true, a); |
| 278 |
} |
| 279 |
return team != COLLISIONTEAM_NONE && itemTeam != COLLISIONTEAM_NONE && |
| 280 |
team == itemTeam && !isPVP; |
| 281 |
} |
| 282 |
|
| 283 |
static bool CheckParams( |
| 284 |
const CollisionParams params, const Thing *a, const Thing *b); |
| 285 |
|
| 286 |
static void AddPosToTileCache(void *data, struct vec2i pos); |
| 287 |
static bool CheckOverlaps( |
| 288 |
const Thing *item, const struct vec2 pos, const struct vec2 vel, |
| 289 |
const struct vec2i size, const CollisionParams params, |
| 290 |
CollideItemFunc func, void *data, CheckWallFunc checkWallFunc, |
| 291 |
CollideWallFunc wallFunc, void *wallData, const struct vec2i tilePos); |
| 292 |
void OverlapThings( |
| 293 |
const Thing *item, const struct vec2 pos, const struct vec2 vel, |
| 294 |
const struct vec2i size, const CollisionParams params, |
| 295 |
CollideItemFunc func, void *data, CheckWallFunc checkWallFunc, |
| 296 |
CollideWallFunc wallFunc, void *wallData) |
| 297 |
{ |
| 298 |
TileCacheReset(&gCollisionSystem.tileCache); |
| 299 |
|
| 300 |
|
| 301 |
const int dtx = (size.x + TILE_WIDTH - 1) / 2 / TILE_WIDTH; |
| 302 |
const int dty = (size.y + TILE_HEIGHT - 1) / 2 / TILE_HEIGHT; |
| 303 |
const struct vec2i tv = Vec2iToTile(svec2i_assign_vec2(pos)); |
| 304 |
for (int dy = -dty; dy < 2 * dty; dy++) |
| 305 |
{ |
| 306 |
for (int dx = -dtx; dx < 2 * dtx; dx++) |
| 307 |
{ |
| 308 |
const struct vec2i dtv = svec2i(tv.x + dx, tv.y + dy); |
| 309 |
TileCacheAdd(&gCollisionSystem.tileCache, dtv); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
AlgoLineDrawData drawData; |
| 314 |
drawData.Draw = AddPosToTileCache; |
| 315 |
drawData.data = &gCollisionSystem.tileCache; |
| 316 |
BresenhamLineDraw( |
| 317 |
svec2i_assign_vec2(pos), svec2i_assign_vec2(svec2_add(pos, vel)), |
| 318 |
&drawData); |
| 319 |
|
| 320 |
|
| 321 |
CA_FOREACH(const struct vec2i, dtv, gCollisionSystem.tileCache) |
| 322 |
if (!CheckOverlaps( |
| 323 |
item, pos, vel, size, params, func, data, checkWallFunc, wallFunc, |
| 324 |
wallData, *dtv)) |
| 325 |
{ |
| 326 |
return; |
| 327 |
} |
| 328 |
CA_FOREACH_END() |
| 329 |
} |
| 330 |
static void AddPosToTileCache(void *data, struct vec2i pos) |
| 331 |
{ |
| 332 |
CArray *tileCache = data; |
| 333 |
const struct vec2i tv = Vec2iToTile(pos); |
| 334 |
TileCacheAdd(tileCache, tv); |
| 335 |
} |
| 336 |
static bool CheckOverlaps( |
| 337 |
const Thing *item, const struct vec2 pos, const struct vec2 vel, |
| 338 |
const struct vec2i size, const CollisionParams params, |
| 339 |
CollideItemFunc func, void *data, CheckWallFunc checkWallFunc, |
| 340 |
CollideWallFunc wallFunc, void *wallData, const struct vec2i tilePos) |
| 341 |
{ |
| 342 |
struct vec2 colA, colB, normal; |
| 343 |
|
| 344 |
if (func != NULL) |
| 345 |
{ |
| 346 |
const CArray *tileThings = &MapGetTile(&gMap, tilePos)->things; |
| 347 |
CA_FOREACH(const ThingId, tid, *tileThings) |
| 348 |
Thing *ti = ThingIdGetThing(tid); |
| 349 |
if (!CheckParams(params, item, ti)) |
| 350 |
{ |
| 351 |
continue; |
| 352 |
} |
| 353 |
if (!MinkowskiHexCollide( |
| 354 |
pos, vel, size, ti->Pos, ti->Vel, ti->size, &colA, &colB, |
| 355 |
&normal)) |
| 356 |
{ |
| 357 |
continue; |
| 358 |
} |
| 359 |
|
| 360 |
if (!func(ti, data, colA, colB, normal)) |
| 361 |
{ |
| 362 |
return false; |
| 363 |
} |
| 364 |
CA_FOREACH_END() |
| 365 |
} |
| 366 |
|
| 367 |
if (checkWallFunc != NULL && wallFunc != NULL && checkWallFunc(tilePos)) |
| 368 |
{ |
| 369 |
|
| 370 |
|
| 371 |
const struct vec2i sizeForWall = |
| 372 |
item->kind == KIND_MOBILEOBJECT ? svec2i_zero() : size; |
| 373 |
if (MinkowskiHexCollide( |
| 374 |
pos, vel, sizeForWall, Vec2CenterOfTile(tilePos), svec2_zero(), |
| 375 |
TILE_SIZE, &colA, &colB, &normal) && |
| 376 |
!wallFunc(tilePos, wallData, colA, normal)) |
| 377 |
{ |
| 378 |
return false; |
| 379 |
} |
| 380 |
} |
| 381 |
return true; |
| 382 |
} |
| 383 |
|
| 384 |
static bool OverlapGetFirstItemCallback( |
| 385 |
Thing *ti, void *data, const struct vec2 colA, const struct vec2 colB, |
| 386 |
const struct vec2 normal); |
| 387 |
Thing *OverlapGetFirstItem( |
| 388 |
const Thing *item, const struct vec2 pos, const struct vec2i size, |
| 389 |
const struct vec2 vel, const CollisionParams params) |
| 390 |
{ |
| 391 |
Thing *firstItem = NULL; |
| 392 |
OverlapThings( |
| 393 |
item, pos, vel, size, params, OverlapGetFirstItemCallback, |
| 394 |
&firstItem, NULL, NULL, NULL); |
| 395 |
return firstItem; |
| 396 |
} |
| 397 |
static bool OverlapGetFirstItemCallback( |
| 398 |
Thing *ti, void *data, const struct vec2 colA, const struct vec2 colB, |
| 399 |
const struct vec2 normal) |
| 400 |
{ |
| 401 |
UNUSED(colA); |
| 402 |
UNUSED(colB); |
| 403 |
UNUSED(normal); |
| 404 |
Thing **pFirstItem = data; |
| 405 |
|
| 406 |
*pFirstItem = ti; |
| 407 |
return false; |
| 408 |
} |
| 409 |
|
| 410 |
static bool CheckParams( |
| 411 |
const CollisionParams params, const Thing *a, const Thing *b) |
| 412 |
{ |
| 413 |
|
| 414 |
if (a == b) |
| 415 |
return false; |
| 416 |
if (!params.AllActors) |
| 417 |
{ |
| 418 |
|
| 419 |
if (CollisionIsOnSameTeam(b, params.Team, params.IsPVP)) |
| 420 |
{ |
| 421 |
return false; |
| 422 |
} |
| 423 |
|
| 424 |
if (a->kind == KIND_CHARACTER && b->kind == KIND_CHARACTER) |
| 425 |
{ |
| 426 |
const TActor *aa = CArrayGet(&gActors, a->id); |
| 427 |
const TActor *ab = CArrayGet(&gActors, b->id); |
| 428 |
if (aa != NULL && ab != NULL) |
| 429 |
{ |
| 430 |
|
| 431 |
if (aa->vehicleUID != -1 || ab->vehicleUID != -1) |
| 432 |
{ |
| 433 |
return false; |
| 434 |
} |
| 435 |
|
| 436 |
if (aa->pilotUID == -1 || ab->pilotUID == -1) |
| 437 |
{ |
| 438 |
return false; |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
if (params.ThingMask != 0 && !(b->flags & params.ThingMask)) |
| 444 |
{ |
| 445 |
return false; |
| 446 |
} |
| 447 |
|
| 448 |
return true; |
| 449 |
} |
| 450 |
|
| 451 |
void GetWallBouncePosVel( |
| 452 |
const struct vec2 pos, const struct vec2 vel, const struct vec2 colPos, |
| 453 |
const struct vec2 colNormal, struct vec2 *outPos, struct vec2 *outVel) |
| 454 |
{ |
| 455 |
const struct vec2 velBeforeCol = svec2_subtract(colPos, pos); |
| 456 |
const struct vec2 velAfterCol = svec2_subtract(vel, velBeforeCol); |
| 457 |
|
| 458 |
|
| 459 |
if (svec2_is_zero(colNormal)) |
| 460 |
{ |
| 461 |
|
| 462 |
*outPos = svec2_subtract(pos, vel); |
| 463 |
*outVel = svec2_scale(vel, -1); |
| 464 |
return; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
const struct vec2 velReflected = svec2( |
| 469 |
colNormal.x == 0 ? velAfterCol.x : colNormal.x * fabsf(velAfterCol.x), |
| 470 |
colNormal.y == 0 ? velAfterCol.y : colNormal.y * fabsf(velAfterCol.y)); |
| 471 |
*outPos = svec2_add(colPos, velReflected); |
| 472 |
|
| 473 |
|
| 474 |
*outVel = svec2( |
| 475 |
colNormal.x == 0 ? vel.x : colNormal.x * fabsf(vel.x), |
| 476 |
colNormal.y == 0 ? vel.y : colNormal.y * fabsf(vel.y)); |
| 477 |
} |