Files
isaac-port/src/familiar_collision.cpp
T

333 lines
16 KiB
C++

#include "entity_bomb.h"
#include "entity_effect.h"
#include "entity_familiar.h"
#include "entity_list.h"
#include "entity_npc.h"
#include "entity_player.h"
#include "entity_projectile.h"
#include "game.h"
#include "log.h"
#include "lua_engine.h"
#include "npc_ai.h"
#include "room.h"
// The enemies a contact familiar swats outright: the flies (a Pooter only of its first kinds).
constexpr int SWATTED_TYPES[] = {13, 18, 80, 222, 256, 296, 281, 868};
constexpr int POOTER_SUPER = 2;
// The familiars that swat flies and otherwise hurt by touch.
constexpr int CONTACT_VARIANTS[] = {33, 60, 15, 14, 79, 77, 3, 104, 44, 45, 46, 47,
32, 83, 121, 126, 226, 224, 210, 218, 206, 237, 229, 204};
// The familiars whose touch, block or hit has an effect of its own not reconstructed yet.
constexpr int UNWRITTEN_TOUCH_VARIANTS[] = {87, 94, 113, 243, 200, 239, 105, 127, 206, 66, 131, 204, 231,
237, 25, 93, 42, 63, 117, 130, 106, 121, 128, 234, 232, 223};
constexpr int UNWRITTEN_BLOCK_VARIANTS[] = {51, 95, 83, 121, 128, 229, 234, 228, 238, 201, 216, 217, 231};
constexpr int UNWRITTEN_ENEMY_TOUCH_VARIANTS[] = {228, 238, 201, 216, 217, 206, 237};
constexpr int UNWRITTEN_DEATH_VARIANTS[] = {128, 216, 217, 206, 237, 229, 234, 201, 238};
// The breakables a dashing charger bumps into, and what each bump deals.
constexpr int CHARGER_BREAKABLE_TYPES[] = {ENTITY_FIREPLACE, ENTITY_MOVABLE_TNT, ENTITY_POOP};
constexpr float CHARGER_BUMP_DAMAGE = 3.5f;
constexpr int CHARGER_BUMP_LIMIT = -10;
constexpr int CHARGER_BUMP_RECOVER = 30;
// Spin to Win's orbit hit, and the damage RC Remote and Friendship Necklace lend the harmless familiars.
constexpr float SPIN_HIT_PUSH = 5;
constexpr float SPIN_HIT_SCALE = 1.5f;
constexpr float SPIN_HIT_BASE = 6;
constexpr int PLAYERFORM_LORD_OF_THE_FLIES_ID = 1;
constexpr int PLAYERFORM_SPIDERBABY_ID = 12;
// The blue flies' and spiders' pop, and the locusts' tear effects.
constexpr int SOUND_INSECT_POP = 1015;
constexpr int SOUND_POP_DELAY = 15;
constexpr float WRATH_EXPLOSION_DAMAGE = 60;
constexpr float WRATH_EXPLOSION_SCALE = 0.6f;
constexpr int PESTILENCE_POISON_FRAMES = 40;
constexpr int FAMINE_SLOW_FRAMES = 300;
constexpr float FAMINE_SLOWING = 0.9f;
constexpr ColorMod FAMINE_SLOW_COLOR = {{1, 1, 1, 1}, {0, 0, 0, 0}, {0.15686275f, 0.15686275f, 0.15686275f}};
// Bob's Brain and BBF's explosion.
constexpr float BRAIN_EXPLOSION_DAMAGE = 100;
constexpr float BRAIN_EXPLOSION_BONUS = 50;
constexpr float BRAIN_EXPLOSION_BONUS_THRESHOLD = 1.5f;
constexpr int BRAIN_ROOM_SETTLE_FRAMES = 20;
constexpr float BRAIN_POISON_FRAMES = 80;
constexpr int BBF_RESPAWN_FRAMES = 300;
// The blood a dying familiar spills.
constexpr float DEATH_BLOOD_HEIGHT = -10;
// What a familiar's hit takes: the shielding damage of spikes and poop, and the count each hit adds.
constexpr uint64_t DAMAGE_ISAACS_HEART = 1ull << 11;
constexpr float PUNCHING_BAG_KNOCKBACK = 5;
constexpr float DIP_HURT_VOLUME = 0.8f;
constexpr int DIP_HURT_DELAY = 15;
// The familiars that never yield to what they touch, unless it is a player or a bomb.
constexpr int SOLID_VARIANTS[] = {62, 67, 228, 238};
constexpr int LIL_DUMPY_FLOATING = 1;
// The tear flags of a blue fly's locust (Wrath, Pestilence, Famine and Death).
enum LocustKind
{
LOCUST_NONE,
LOCUST_WRATH,
LOCUST_PESTILENCE,
LOCUST_FAMINE,
LOCUST_DEATH
};
// Sorted insertion into a hit list, as the entities keep them.
static void hit_list_add(std::vector<uint32_t>& hitList, Entity& entity)
{
uint32_t index = entity.GetHitListIndex();
auto at = std::lower_bound(hitList.begin(), hitList.end(), index);
if (at == hitList.end() || *at != index) hitList.insert(at, index);
}
// A blue fly popping on what it hit: a puff, then its locust's effect (an explosion, poison, slowing or death).
static void blue_fly_pop(Entity_Familiar& fly, Entity& collider, float damageMultiplier)
{
g_game->Spawn({.position = fly.pos, .type = ENTITY_EFFECT, .variant = EFFECT_FLY_EXPLOSION, .seed = Random()});
fly.Remove();
switch (fly.subtype)
{
case LOCUST_WRATH:
explosion_effects({.tearFlags = fly.player->GetBombFlags(false),
.color = fly.sprite.color,
.position = fly.pos,
.source = &fly,
.damage = (damageMultiplier - 1) * 25 + WRATH_EXPLOSION_DAMAGE,
.radiusMultiplier = WRATH_EXPLOSION_SCALE});
break;
case LOCUST_PESTILENCE: collider.AddPoison(EntityRef(&fly), PESTILENCE_POISON_FRAMES, 0, false); break;
case LOCUST_FAMINE:
collider.AddSlowing(EntityRef(&fly), FAMINE_SLOW_FRAMES, FAMINE_SLOWING, FAMINE_SLOW_COLOR, false);
break;
case LOCUST_DEATH: collider.Kill(EntityRef(&fly)); break;
}
familiar_sound_play(SOUND_INSECT_POP, {.frameDelay = SOUND_POP_DELAY});
}
// Bob's Brain and BBF going off on an enemy once the room has settled: the player's bomb effects in a big blast, Bob's
// Brain poisoning what it hit. Both then hide to come back later.
static void brain_explode(Entity_Familiar& brain, Entity& collider, float damageMultiplier)
{
if (brain.subtype != 0 || g_game->room->GetFrameCount() <= BRAIN_ROOM_SETTLE_FRAMES) return;
Explosion blast = {.color = brain.sprite.color,
.position = brain.pos,
.source = &brain,
.damageFlags = DAMAGE_EXPLOSION | DAMAGE_IGNORE_ARMOR,
.damage = (damageMultiplier - 1) * 25 + BRAIN_EXPLOSION_DAMAGE};
if (brain.player)
{
blast.tearFlags = brain.player->GetBombFlags(false);
if (brain.player->check_bomb_tearflags(TearFlags{}) >= BRAIN_EXPLOSION_BONUS_THRESHOLD)
blast.damage += BRAIN_EXPLOSION_BONUS;
}
explosion_effects(blast);
brain.subtype = 1;
if (brain.variant == Entity_Familiar::BBF) brain.counter = BBF_RESPAWN_FRAMES;
if (brain.variant == Entity_Familiar::BOBS_BRAIN)
{
collider.AddPoison(EntityRef(&brain), (int)(damageMultiplier * BRAIN_POISON_FRAMES), 0, false);
brain.state = 0;
}
}
// A familiar's hit on an enemy (part of 006261f0): swats a fly outright when it hurts by touch, else hurts it unless a
// blue fly or spider bounces off an enemy too fast for it, then the blue flies and spiders pop and the brains blow.
bool Entity_Familiar::collision_contact(Entity& collider, float damageMultiplier, float damage)
{
hit_list_add(hitList, collider);
bool isIgnored = true;
bool isSwatted = false;
if (familiar_listed(CONTACT_VARIANTS, variant))
{
bool isPooter = collider.type == ENTITY_POOTER && collider.variant != POOTER_SUPER;
isSwatted = isPooter || familiar_listed(SWATTED_TYPES, collider.type);
if (isSwatted) collider.Kill(EntityRef(this));
}
if (!isSwatted && variant != FLY_ORBITAL)
{
Entity_NPC* npc = dynamic_cast<Entity_NPC*>(&collider);
bool isVelocityDamaging = !npc || npc->CanBeDamagedFromVelocity(velocity);
if ((variant != BLUE_FLY && variant != BLUE_SPIDER) || isVelocityDamaging)
collider.TakeDamage(damage * damageMultiplier, 0, EntityRef(this), 0);
}
if (variant == BLUE_FLY)
{
blue_fly_pop(*this, collider, damageMultiplier);
return true;
}
if (variant == BLUE_SPIDER)
{
g_game->Spawn({.position = pos, .type = ENTITY_EFFECT, .variant = EFFECT_SPIDER_EXPLOSION, .seed = Random()});
Remove();
familiar_sound_play(SOUND_INSECT_POP, {.frameDelay = SOUND_POP_DELAY});
return true;
}
if (variant == BBF || variant == BOBS_BRAIN) brain_explode(*this, collider, damageMultiplier);
return isIgnored;
}
// A projectile or tear meeting a familiar (part of 006261f0): one that cannot be stopped passes, a blocking familiar
// stops it. Slipped Rib's reflection and the familiars that react to a block stop the game.
bool Entity_Familiar::collision_block(Entity& collider, bool isIgnored)
{
if (collider.type == ENTITY_PROJECTILE &&
(static_cast<Entity_Projectile&>(collider).projectileFlags & PROJECTILE_CANT_HIT_PLAYER))
return true;
if (!CanBlockProjectiles())
{
if (variant != SLIPPED_RIB) return true;
reconstruction_missing("Entity_Familiar::HandleCollision 006261f0 (Slipped Rib's reflection)");
}
if (familiar_listed(UNWRITTEN_BLOCK_VARIANTS, variant))
reconstruction_missing("Entity_Familiar::HandleCollision 006261f0 (this familiar's block)");
collider.isDead = true;
return isIgnored;
}
// 006261f0 What a familiar does on touching an entity, after the mods. Friendly and charmed ones, and all in a pacifist
// room, leave enemies alone. A dashing charger breaks fireplaces and poop, a spinning orbital hits, Skatole and
// Beelzebub keep the flies off the player, a familiar with contact damage hurts enemies (once per hit list) and a
// blocking one stops shots. Returns whether the touch is ignored. A versus match and the familiars with their own touch
// stop the game.
bool Entity_Familiar::HandleCollision(Entity* collider, bool isLow)
{
if (familiar_versus()) reconstruction_missing("Entity_Familiar::HandleCollision 006261f0 (a versus match)");
LuaResult result = lua_engine_get().PreFamiliarCollision(this, collider, isLow);
if (result == LUA_RESULT_TRUE) return true;
if (result == LUA_RESULT_FALSE) return false;
bool isEnemy = Entity::is_enemy_type(collider->type);
if (isEnemy && (collider->HasEntityFlags(FLAG_FRIENDLY) || HasEntityFlags(FLAG_CHARM) || g_game->room->isPacifist))
return true;
if (variant == UMBILICAL_BABY) return true;
if (familiar_listed(UNWRITTEN_TOUCH_VARIANTS, variant) || sirenHelper)
reconstruction_missing("Entity_Familiar::HandleCollision 006261f0 (this familiar's touch)");
bool isIgnored = true;
float damageMultiplier = GetMultiplier();
bool isCharger = variant == LITTLE_CHUBBY || variant == BOBS_BRAIN || variant == BIG_CHUBBY;
if (isCharger && subtype == 0 && fireCooldown < 0 && familiar_listed(CHARGER_BREAKABLE_TYPES, collider->type))
{
collider->TakeDamage(CHARGER_BUMP_DAMAGE, 0, EntityRef(this), 0);
velocity = vector_scale(velocity, 0.5f);
if (--fireCooldown < CHARGER_BUMP_LIMIT) fireCooldown = CHARGER_BUMP_RECOVER;
}
if (isLow && HasEntityFlags(FLAG_SPIN) && collider->IsVulnerableEnemy(nullptr) &&
!EntityList::HitListContains(hitList, *collider))
{
if (orbitLayer == 0)
{
Vector away = vector_scale(vector_normalized(vector_sub(collider->pos, player->pos)), SPIN_HIT_PUSH);
collider->velocity = vector_add(collider->velocity, away);
player->velocity = vector_sub(player->velocity, away);
}
collider->TakeDamage(collisionDamage * SPIN_HIT_SCALE + SPIN_HIT_BASE, 0, EntityRef(this), 0);
hit_list_add(hitList, *collider);
}
bool isOwnersFoe = !spawnerEntity || !spawnerEntity->IsDescendantOf(collider);
if (isOwnersFoe && collider->IsActiveEnemy(DEAD_EXCLUDED) &&
(collider->IsVulnerableEnemy(nullptr) || collider->CanShutDoors()) &&
familiar_listed(UNWRITTEN_ENEMY_TOUCH_VARIANTS, variant))
reconstruction_missing("Entity_Familiar::HandleCollision 006261f0 (this "
"familiar's enemy touch)");
if (collider->type == ENTITY_PLAYER)
{
Entity_Player& touched = *static_cast<Entity_Player*>(collider);
bool isFlyLord =
touched.HasCollectible(COLLECTIBLE_SKATOLE) ||
(touched.variant == PLAYER_VARIANT_PLAYER && touched.HasPlayerForm(PLAYERFORM_LORD_OF_THE_FLIES_ID));
bool isSpiderLord = touched.variant == PLAYER_VARIANT_PLAYER && touched.HasPlayerForm(PLAYERFORM_SPIDERBABY_ID);
if (isFlyLord && (variant == BLUE_FLY || variant == BLUE_SPIDER || variant == ABYSS_LOCUST)) return true;
if (isSpiderLord && (variant == BLUE_SPIDER || variant == SPIDER_BABY)) return true;
}
float damage = collisionDamage;
if (damage == 0)
{
int remote = player->GetTrinketMultiplier(TRINKET_RC_REMOTE);
if (remote == 2) damage = 2;
else if (remote > 2) damage = 5;
if (player->GetTrinketMultiplier(TRINKET_FRIENDSHIP_NECKLACE) > 1) damage = 4;
}
if (!isLow) return solid_check(*collider, isIgnored);
bool isTouching =
collider->IsVulnerableEnemy(nullptr) && (damage > 0 || variant == FLY_ORBITAL) &&
!EntityList::HitListContains(hitList, *collider) &&
(variant != DADDY_LONGLEGS || entityCollisionClass == ENTCOLL_ENEMIES || entityCollisionClass == ENTCOLL_ALL);
if (isTouching) isIgnored = collision_contact(*collider, damageMultiplier, damage);
if (collider->type == ENTITY_PROJECTILE || collider->type == ENTITY_TEAR)
isIgnored = collision_block(*collider, isIgnored);
return solid_check(*collider, isIgnored);
}
// The end of 006261f0: Isaac's Heart, the Punching Bag, Minisaac, the Blood Baby and a floating Lil Dumpy push back
// what they touch, unless it is a player or a bomb.
bool Entity_Familiar::solid_check(Entity& collider, bool isIgnored)
{
bool isSolid = familiar_listed(SOLID_VARIANTS, variant) || (variant == LIL_DUMPY && state == LIL_DUMPY_FLOATING);
if (isSolid && collider.type != ENTITY_PLAYER && collider.type != ENTITY_BOMB) return false;
return isIgnored;
}
// 0062cdc0 A hit on a familiar: shielded from friends, spikes and poop (Isaac's Heart excepted), Isaac's Heart passing
// it to its player, the Punching Bag knocked back, a Dip squeaking. The wisps, the Lost Soul and Milk stop the game.
bool Entity_Familiar::TakeDamage(float damage, uint64_t damageFlags, const EntityRef& source, int damageCountdown)
{
if (IsDamageSourceFriendly(source) || source.IsFriendly()) return false;
if ((damageFlags & (DAMAGE_SPIKES | DAMAGE_POOP)) && variant != ISAACS_HEART) return false;
if (variant == WISP || variant == ITEM_WISP || variant == LOST_SOUL || variant == MILK)
reconstruction_missing("Entity_Familiar::TakeDamage 0062cdc0 (this familiar's hit)");
if (variant == BLOOD_BABY && counter > 0) return false;
bool isPassed = variant == ISAACS_HEART || (g_game->challenge == CHALLENGE_PONG && variant == PUNCHING_BAG);
bool isDamaged = isPassed ? player->TakeDamage(damage, damageFlags | DAMAGE_ISAACS_HEART, source, damageCountdown)
: Entity::TakeDamage(damage, damageFlags | DAMAGE_COUNTDOWN, source, damageCountdown);
if (!isDamaged || damage <= 0) return isDamaged;
if (variant == PUNCHING_BAG && friction != 0)
{
Vector away = vector_normalized(vector_sub(pos, source.position));
velocity = vector_add(velocity, vector_scale(away, PUNCHING_BAG_KNOCKBACK * speedMultiplier / friction));
}
if (variant == DIP && (Random() & 1) == 0)
familiar_sound_play(SOUND_DIP_HURT, {.volume = DIP_HURT_VOLUME, .frameDelay = DIP_HURT_DELAY});
return isDamaged;
}
// 00610540 A familiar's death: blood, then the entity's own kill and its removal. The familiars that die their own way
// (the bone orbitals, the wisps, the swarm flies, the Dips, the clots) stop the game.
void Entity_Familiar::Kill(const EntityRef& source)
{
if (!isDead)
{
if (familiar_listed(UNWRITTEN_DEATH_VARIANTS, variant))
reconstruction_missing("Entity_Familiar::Kill 00610540 (this familiar's death)");
Entity* blood = g_game->Spawn({.position = pos,
.subType = 1,
.type = ENTITY_EFFECT,
.variant = EFFECT_BLOOD_EXPLOSION,
.seed = Random()});
blood->posOffset = vector_make(0, DEATH_BLOOD_HEIGHT);
blood->SetColor(splatColor, -1, 255, false, true);
blood->Update();
BloodExplode();
}
Entity::Kill(source);
Remove();
}