sm64coopdx/src/game/object_collision.c

235 lines
8.4 KiB
C
Raw Normal View History

2020-06-02 16:44:34 +00:00
#include <PR/ultratypes.h>
2019-08-25 04:46:40 +00:00
#include "sm64.h"
#include "debug.h"
2019-09-01 19:50:50 +00:00
#include "interaction.h"
2020-06-02 16:44:34 +00:00
#include "mario.h"
#include "object_list_processor.h"
#include "spawn_object.h"
2019-08-25 04:46:40 +00:00
2020-03-02 03:42:52 +00:00
struct Object *debug_print_obj_collision(struct Object *a) {
2019-08-25 04:46:40 +00:00
struct Object *sp24;
UNUSED s32 unused;
s32 i;
for (i = 0; i < a->numCollidedObjs; i++) {
print_debug_top_down_objectinfo("ON", 0);
sp24 = a->collidedObjs[i];
2019-09-01 19:50:50 +00:00
if (sp24 != gMarioObject) {
2019-08-25 04:46:40 +00:00
return sp24;
2019-09-01 19:50:50 +00:00
}
2019-08-25 04:46:40 +00:00
}
return NULL;
}
int detect_player_hitbox_overlap(struct MarioState* local, struct MarioState* remote) {
if (local->marioObj == NULL || local->marioObj->oIntangibleTimer != 0) { return FALSE; }
if (remote->marioObj == NULL || remote->marioObj->oIntangibleTimer != 0) { return FALSE; }
struct Object* a = local->marioObj;
f32* aTorso = local->marioBodyState->torsoPos;
struct Object* b = remote->marioObj;
f32* bTorso = remote->marioBodyState->torsoPos;
f32 sp3C = aTorso[1] - a->hitboxDownOffset;
f32 sp38 = bTorso[1] - b->hitboxDownOffset;
f32 dx = aTorso[0] - bTorso[0];
UNUSED f32 sp30 = sp3C - sp38;
f32 dz = aTorso[2] - bTorso[2];
f32 collisionRadius = (a->hitboxRadius + b->hitboxRadius) * 1.25f;
f32 distance = sqrtf(dx * dx + dz * dz);
if (collisionRadius > distance) {
f32 sp20 = a->hitboxHeight + sp3C;
f32 sp1C = b->hitboxHeight + sp38;
if (sp3C > sp1C) {
return FALSE;
}
if (sp20 < sp38) {
return FALSE;
}
if (a->numCollidedObjs >= 4) {
return FALSE;
}
if (b->numCollidedObjs >= 4) {
return FALSE;
}
a->collidedObjs[a->numCollidedObjs] = b;
b->collidedObjs[b->numCollidedObjs] = a;
a->collidedObjInteractTypes |= b->oInteractType;
b->collidedObjInteractTypes |= a->oInteractType;
a->numCollidedObjs++;
b->numCollidedObjs++;
return TRUE;
}
//! no return value
return FALSE;
}
2020-03-02 03:42:52 +00:00
int detect_object_hitbox_overlap(struct Object *a, struct Object *b) {
2019-08-25 04:46:40 +00:00
f32 sp3C = a->oPosY - a->hitboxDownOffset;
f32 sp38 = b->oPosY - b->hitboxDownOffset;
f32 dx = a->oPosX - b->oPosX;
UNUSED f32 sp30 = sp3C - sp38;
f32 dz = a->oPosZ - b->oPosZ;
f32 collisionRadius = a->hitboxRadius + b->hitboxRadius;
f32 distance = sqrtf(dx * dx + dz * dz);
if (collisionRadius > distance) {
f32 sp20 = a->hitboxHeight + sp3C;
f32 sp1C = b->hitboxHeight + sp38;
2019-09-01 19:50:50 +00:00
if (sp3C > sp1C) {
2019-08-25 04:46:40 +00:00
return 0;
2019-09-01 19:50:50 +00:00
}
if (sp20 < sp38) {
2019-08-25 04:46:40 +00:00
return 0;
2019-09-01 19:50:50 +00:00
}
if (a->numCollidedObjs >= 4) {
2019-08-25 04:46:40 +00:00
return 0;
2019-09-01 19:50:50 +00:00
}
if (b->numCollidedObjs >= 4) {
2019-08-25 04:46:40 +00:00
return 0;
2019-09-01 19:50:50 +00:00
}
2019-08-25 04:46:40 +00:00
a->collidedObjs[a->numCollidedObjs] = b;
b->collidedObjs[b->numCollidedObjs] = a;
a->collidedObjInteractTypes |= b->oInteractType;
b->collidedObjInteractTypes |= a->oInteractType;
a->numCollidedObjs++;
b->numCollidedObjs++;
return 1;
}
//! no return value
}
2020-03-02 03:42:52 +00:00
int detect_object_hurtbox_overlap(struct Object *a, struct Object *b) {
2019-08-25 04:46:40 +00:00
f32 sp3C = a->oPosY - a->hitboxDownOffset;
f32 sp38 = b->oPosY - b->hitboxDownOffset;
f32 sp34 = a->oPosX - b->oPosX;
UNUSED f32 sp30 = sp3C - sp38;
f32 sp2C = a->oPosZ - b->oPosZ;
f32 sp28 = a->hurtboxRadius + b->hurtboxRadius;
f32 sp24 = sqrtf(sp34 * sp34 + sp2C * sp2C);
// two-player hack
if (a == gMarioObject) { b->oInteractionSubtype |= INT_SUBTYPE_DELAY_INVINCIBILITY; }
if (a == gMario2Object) { b->oInteractionSubtype |= INT_SUBTYPE_DELAY_INVINCIBILITY_MARIO2; }
2019-08-25 04:46:40 +00:00
if (sp28 > sp24) {
f32 sp20 = a->hitboxHeight + sp3C;
f32 sp1C = b->hurtboxHeight + sp38;
2019-09-01 19:50:50 +00:00
if (sp3C > sp1C) {
2019-08-25 04:46:40 +00:00
return 0;
2019-09-01 19:50:50 +00:00
}
if (sp20 < sp38) {
2019-08-25 04:46:40 +00:00
return 0;
2019-09-01 19:50:50 +00:00
}
// two-player hack
if (a == gMarioObject) { b->oInteractionSubtype &= ~INT_SUBTYPE_DELAY_INVINCIBILITY; }
if (a == gMario2Object) { b->oInteractionSubtype &= ~INT_SUBTYPE_DELAY_INVINCIBILITY_MARIO2; }
2019-08-25 04:46:40 +00:00
return 1;
}
//! no return value
}
2020-03-02 03:42:52 +00:00
void clear_object_collision(struct Object *a) {
2019-08-25 04:46:40 +00:00
struct Object *sp4 = (struct Object *) a->header.next;
while (sp4 != a) {
sp4->numCollidedObjs = 0;
sp4->collidedObjInteractTypes = 0;
2019-09-01 19:50:50 +00:00
if (sp4->oIntangibleTimer > 0) {
2019-08-25 04:46:40 +00:00
sp4->oIntangibleTimer--;
2019-09-01 19:50:50 +00:00
}
2019-08-25 04:46:40 +00:00
sp4 = (struct Object *) sp4->header.next;
}
}
2020-03-02 03:42:52 +00:00
void check_collision_in_list(struct Object *a, struct Object *b, struct Object *c) {
2019-08-25 04:46:40 +00:00
if (a->oIntangibleTimer == 0) {
while (b != c) {
if (b->oIntangibleTimer == 0) {
2020-03-02 03:42:52 +00:00
if (detect_object_hitbox_overlap(a, b) && b->hurtboxRadius != 0.0f) {
detect_object_hurtbox_overlap(a, b);
2019-09-01 19:50:50 +00:00
}
2019-08-25 04:46:40 +00:00
}
b = (struct Object *) b->header.next;
}
}
}
2020-03-02 03:42:52 +00:00
void check_player_object_collision(void) {
2019-08-25 04:46:40 +00:00
struct Object *sp1C = (struct Object *) &gObjectLists[OBJ_LIST_PLAYER];
struct Object *sp18 = (struct Object *) sp1C->header.next;
while (sp18 != sp1C) {
2020-03-02 03:42:52 +00:00
check_collision_in_list(sp18, (struct Object *) sp18->header.next, sp1C);
check_collision_in_list(sp18, (struct Object *) gObjectLists[OBJ_LIST_POLELIKE].next,
2019-08-25 04:46:40 +00:00
(struct Object *) &gObjectLists[OBJ_LIST_POLELIKE]);
2020-03-02 03:42:52 +00:00
check_collision_in_list(sp18, (struct Object *) gObjectLists[OBJ_LIST_LEVEL].next,
2019-08-25 04:46:40 +00:00
(struct Object *) &gObjectLists[OBJ_LIST_LEVEL]);
2020-03-02 03:42:52 +00:00
check_collision_in_list(sp18, (struct Object *) gObjectLists[OBJ_LIST_GENACTOR].next,
2019-08-25 04:46:40 +00:00
(struct Object *) &gObjectLists[OBJ_LIST_GENACTOR]);
2020-03-02 03:42:52 +00:00
check_collision_in_list(sp18, (struct Object *) gObjectLists[OBJ_LIST_PUSHABLE].next,
2019-08-25 04:46:40 +00:00
(struct Object *) &gObjectLists[OBJ_LIST_PUSHABLE]);
2020-03-02 03:42:52 +00:00
check_collision_in_list(sp18, (struct Object *) gObjectLists[OBJ_LIST_SURFACE].next,
2019-08-25 04:46:40 +00:00
(struct Object *) &gObjectLists[OBJ_LIST_SURFACE]);
2020-03-02 03:42:52 +00:00
check_collision_in_list(sp18, (struct Object *) gObjectLists[OBJ_LIST_DESTRUCTIVE].next,
2019-08-25 04:46:40 +00:00
(struct Object *) &gObjectLists[OBJ_LIST_DESTRUCTIVE]);
sp18 = (struct Object *) sp18->header.next;
}
// two-player hack ?
extern struct MarioState gMarioStates[];
for (int i = 1; i < MAX_PLAYERS; i++) {
detect_player_hitbox_overlap(&gMarioStates[0], &gMarioStates[i]);
}
2019-08-25 04:46:40 +00:00
}
2020-03-02 03:42:52 +00:00
void check_pushable_object_collision(void) {
2019-08-25 04:46:40 +00:00
struct Object *sp1C = (struct Object *) &gObjectLists[OBJ_LIST_PUSHABLE];
struct Object *sp18 = (struct Object *) sp1C->header.next;
while (sp18 != sp1C) {
2020-03-02 03:42:52 +00:00
check_collision_in_list(sp18, (struct Object *) sp18->header.next, sp1C);
2019-08-25 04:46:40 +00:00
sp18 = (struct Object *) sp18->header.next;
}
}
2020-03-02 03:42:52 +00:00
void check_destructive_object_collision(void) {
2019-08-25 04:46:40 +00:00
struct Object *sp1C = (struct Object *) &gObjectLists[OBJ_LIST_DESTRUCTIVE];
struct Object *sp18 = (struct Object *) sp1C->header.next;
while (sp18 != sp1C) {
if (sp18->oDistanceToMario < 2000.0f && !(sp18->activeFlags & ACTIVE_FLAG_UNK9)) {
2020-03-02 03:42:52 +00:00
check_collision_in_list(sp18, (struct Object *) sp18->header.next, sp1C);
check_collision_in_list(sp18, (struct Object *) gObjectLists[OBJ_LIST_GENACTOR].next,
2019-08-25 04:46:40 +00:00
(struct Object *) &gObjectLists[OBJ_LIST_GENACTOR]);
2020-03-02 03:42:52 +00:00
check_collision_in_list(sp18, (struct Object *) gObjectLists[OBJ_LIST_PUSHABLE].next,
2019-08-25 04:46:40 +00:00
(struct Object *) &gObjectLists[OBJ_LIST_PUSHABLE]);
2020-03-02 03:42:52 +00:00
check_collision_in_list(sp18, (struct Object *) gObjectLists[OBJ_LIST_SURFACE].next,
2019-08-25 04:46:40 +00:00
(struct Object *) &gObjectLists[OBJ_LIST_SURFACE]);
}
sp18 = (struct Object *) sp18->header.next;
}
}
void detect_object_collisions(void) {
2020-03-02 03:42:52 +00:00
clear_object_collision((struct Object *) &gObjectLists[OBJ_LIST_POLELIKE]);
clear_object_collision((struct Object *) &gObjectLists[OBJ_LIST_PLAYER]);
clear_object_collision((struct Object *) &gObjectLists[OBJ_LIST_PUSHABLE]);
clear_object_collision((struct Object *) &gObjectLists[OBJ_LIST_GENACTOR]);
clear_object_collision((struct Object *) &gObjectLists[OBJ_LIST_LEVEL]);
clear_object_collision((struct Object *) &gObjectLists[OBJ_LIST_SURFACE]);
clear_object_collision((struct Object *) &gObjectLists[OBJ_LIST_DESTRUCTIVE]);
check_player_object_collision();
check_destructive_object_collision();
check_pushable_object_collision();
2019-08-25 04:46:40 +00:00
}