#include "entity_familiar.h" #include "entity_player.h" #include "game.h" #include "log.h" #include "manager.h" #include "npc_ai.h" #include "room.h" #include "room_grid.h" #include // The items, trinkets, challenge and boss the following line answers to. constexpr int COLLECTIBLE_BIG_FAN = 279; constexpr int COLLECTIBLE_GELLO = 728; constexpr int SIREN_TYPE = 904; // How far behind its leader a follower keeps, closer for Milk and on Child's Leash, and how hard it catches up. constexpr float FOLLOW_DISTANCE = 20; constexpr float MILK_FOLLOW_DISTANCE = 10; constexpr float LEASH_FOLLOW_DISTANCE = 5; constexpr float FOLLOW_PULL = 0.2f; constexpr float FOLLOW_FRICTION = 0.8f; constexpr float FOLLOW_POSITION_PULL = 0.1f; // The followers' places in the Duct Tape formations: a row, a V and a ring. enum FormationMode : uint8_t { FORMATION_FREE, FORMATION_ROW, FORMATION_V, FORMATION_RING }; constexpr float FORMATION_ROW_STEP = 32; constexpr float FORMATION_V_DEPTH = 32; constexpr float FORMATION_V_STEP = 24; constexpr float FORMATION_RING_RADIUS = 54; constexpr float FORMATION_LERP = 0.1f; constexpr float FORMATION_AIM_THRESHOLD = 0.1f; // The familiars the Duct Tape formation leaves where they are. constexpr int FORMATION_EXEMPT_VARIANTS[] = {8, 13, 14, 16, 17, 24, 42, 43, 53, 56, 58, 62, 63, 64, 66, 67, 73, 75, 85, 87, 88, 90, 94, 96, 106, 110, 111, 118, 122, 127, 200, 210, 211, 212, 225, 233, 235, 236, 239, 241}; // Each orbit layer: its ellipse, its speed (faster with Guardian Angel), and where its first orbital starts. struct OrbitLayer { float distanceX = 0; float distanceY = 0; float speed = 0; float guardianAngelSpeed = 0; float angleOffset = 0; }; constexpr OrbitLayer ORBIT_LAYER_TABLE[] = { {25, 20, 0.045f, 2, 1.5f}, {35, 30, 0.045f, 1.5f, 0.5f}, {60, 45, -0.03f, 1.5f, 2}, {110, 90, 0.02f, 1.5f, 1.2f}, {85, 67.5f, 0.02f, 1.5f, 0.8f}, {35, 30, 0.045f, 1.5f, 0.5f}, {50, 42, 0.03f, 1.5f, 0}, {28, 21, 0.04f, 1.5f, 0}, {40, 30, -0.03f, 1.5f, 0}, {64, 50, 0.02f, 1.5f, 0}, {40, 36, 0.02f, 1.5f, 0}}; constexpr int ORBIT_LAYER_MAX = 10; // The orbit speeds of the wide wisps (My Little Unicorn's and Unicorn Stump's), Big Fan's slow orbit and Best Bud. constexpr int WISP_UNICORN = 77; constexpr int WISP_UNICORN_STUMP = 298; constexpr float WIDE_WISP_SPEED = 2; constexpr float BIG_FAN_SPEED = 1.0f / 3; constexpr float BEST_BUD_SPEED = 3; constexpr float TWO_PI = 6.2831855f; // The orbitals that turn their sprite with the orbit. constexpr int SACRIFICIAL_DAGGER_ROTATION_FRAME = 6; // The follower priorities, highest leading. constexpr int PRIORITY_1_VARIANTS[] = {1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 25, 59, 76, 81, 92, 93, 97, 104, 119, 125, 209, 220, 223}; constexpr int PRIORITY_2_VARIANTS[] = {51, 74, 89, 230}; constexpr int PRIORITY_KING_BABY = 9999; // The chargers, which follow only while not dashing. constexpr int CHARGER_VARIANTS[] = {3, 59, 25, 93, 104, 232, 223}; // Clamps a layer into the table. static const OrbitLayer& orbit_layer(int layer) { return ORBIT_LAYER_TABLE[std::clamp(layer, 0, ORBIT_LAYER_MAX)]; } // Whether a familiar is one of the wisps whose orbit is twice as fast. static bool is_wide_wisp(Entity_Familiar& familiar) { return familiar.type == ENTITY_FAMILIAR && familiar.variant == Entity_Familiar::WISP && (familiar.subtype == WISP_UNICORN || familiar.subtype == WISP_UNICORN_STUMP); } // 00612970 Its rank in the following line: the shooters first, then Mongo Baby and its peers, the Lils, Incubus, Milk // and King Baby at the very front. Others do not follow. int Entity_Familiar::GetFollowerPriority() { if (familiar_listed(PRIORITY_1_VARIANTS, variant)) return 1; if (familiar_listed(PRIORITY_2_VARIANTS, variant)) return 2; switch (variant) { case LIL_BRIMSTONE: case LIL_MONSTRO: return 3; case INCUBUS: case TWISTED_BABY: return 10; case MILK: return 9; case KING_BABY: return PRIORITY_KING_BABY; case LIL_PORTAL: return 5; default: return 0; } } // 00612890 Whether it keeps its place in the line now: not while a charger dashes, Milk is full, a hidden familiar // waits or the Vanishing Twin hunts. bool Entity_Familiar::is_following() { if (!isFollower) return false; if (familiar_listed(CHARGER_VARIANTS, variant)) return fireCooldown == 0; switch (variant) { case MILK: return subtype != maxSubtype; case MYSTERY_EGG: case HALLOWED_GROUND: return isVisible; case JAW_BONE: return fireCooldown >= 0; case VANISHING_TWIN: return isVisible && coins == 0; default: return true; } } // 00612ad0 Whether it counts in the line (Lilith's Birthright spreads the counted followers). bool Entity_Familiar::is_counted_follower() { return GetFollowerPriority() > 0 && !isNotCountedAsFollower; } // 00634920 Whether Duct Tape's formation leaves it alone: the familiars that move their own way, and permanent ones. bool Entity_Familiar::is_formation_exempt() { return familiar_listed(FORMATION_EXEMPT_VARIANTS, variant) || isSubtype55555; } // 00612af0 Whether it is a following Incubus that Tainted Lilith's Birthright hides inside her whip. bool Entity_Familiar::IsLilithBirthrightIncubus() { if (player->playerType != PLAYER_LILITH_B || !player->HasCollectible(COLLECTIBLE_BIRTHRIGHT)) return false; Weapon* held = player->weapons[0] ? player->weapons[0].get() : player->weapons[1].get(); return held && held->weaponType == WEAPON_UMBILICAL_WHIP && isFollower && !isNotCountedAsFollower; } // 00612b50 The whip Tainted Lilith's hidden Incubus sits in. Entity* Entity_Familiar::get_incubus_umbilical_whip() { if (!IsLilithBirthrightIncubus()) return nullptr; reconstruction_missing("Entity_Familiar::get_incubus_umbilical_whip 00612b50"); } // 0062db20 Joins its player's following line by priority: behind the first familiar of higher or equal rank, ahead of // the first lower one, at the end otherwise. The first follower starts a line of its own. void Entity_Familiar::AddToFollowers() { if (isSubtype55555) return; if (variant == TWISTED_BABY) return reconstruction_missing("Entity_Familiar::AddToFollowers 0062db20 (Twisted Pair)"); if (isFollower && parent) { Entity_Familiar* leader = parent->type == ENTITY_FAMILIAR ? dynamic_cast(parent.get()) : nullptr; bool isFamiliarLeader = leader && leader->isFollower && leader->player == player; if (isFamiliarLeader || (parent->type == ENTITY_PLAYER && parent == player)) { KAGE_LogMessage(LOG_INFO, (uint8_t*)"[warn] Tried to add familiar %d to followers again!\n"); return; } } isFollower = true; int priority = GetFollowerPriority(); for (Entity_Familiar* other : familiar_query(-1)) { if (other == this || !other->isFollower || other->player != player) continue; Entity_Familiar* chain = other; while (chain->parent && chain->parent->type == ENTITY_FAMILIAR) chain = static_cast(chain->parent.get()); while (true) { if (chain->GetFollowerPriority() < priority) { child = chain; parent = chain->parent.get(); if (parent && parent->type == ENTITY_FAMILIAR) parent->child = this; chain->parent = this; return; } Entity* next = chain->child; if (!next || next->type != ENTITY_FAMILIAR) { chain->child = this; parent = chain; return; } chain = static_cast(next); } } } // Unlinks an entity from the chain its parent and child hold, the neighbours joining (part of 0062d7e0 and 006141e0). static void chain_unlink(Entity& entity) { if (entity.parent) entity.parent->child = entity.child.get(); if (entity.child) entity.child->parent = entity.parent.get(); entity.parent = nullptr; entity.child = nullptr; } // 0062d7e0 Leaves the following line, its neighbours closing the gap. void Entity_Familiar::RemoveFromFollowers() { if (variant == TWISTED_BABY && parent) return reconstruction_missing("Entity_Familiar::RemoveFromFollowers 0062d7e0 (Twisted Pair)"); if (isFollower) chain_unlink(*this); isFollower = false; } // 006141e0 Leaves the delayed line, its neighbours closing the gap. void Entity_Familiar::RemoveFromDelayed() { if (isDelayed) chain_unlink(*this); isDelayed = false; } // 00613910 Drifts toward a position, or stays pinned in place when permanent. void Entity_Familiar::FollowPosition(const Vector& position) { if (isSubtype55555) { pos = formationOffset; velocity = {}; return; } friction *= FOLLOW_FRICTION; velocity = vector_scale(vector_sub(position, pos), FOLLOW_POSITION_PULL); } // The leader a follower trails: the first familiar ahead that keeps its place, else the King Baby at the head of its // line, else none (part of 00612bb0). static Entity* follow_leader_find(Entity_Familiar& familiar, bool isLilithBirthright) { for (Entity* leader = familiar.parent; leader; leader = leader->parent) { Entity_Familiar* ahead = dynamic_cast(leader); if (!ahead || ahead->is_following() || (isLilithBirthright && ahead->is_counted_follower())) return leader; } if (familiar.variant == Entity_Familiar::KING_BABY) return nullptr; for (Entity_Familiar* king : familiar_query(Entity_Familiar::KING_BABY)) { if (!king->isExists || king->player != familiar.player || king->parent != familiar.parent) continue; Entity* last = king; for (Entity* next = king->child; next && next->type == ENTITY_FAMILIAR && next->variant == Entity_Familiar::KING_BABY; next = next->child) last = next; return last; } return nullptr; } // 00612bb0 Trails its leader at a short distance, catching up the further it falls behind. Permanent familiars stay // put. Tainted Lilith's Incubus, the Siren's charm, RC Remote, Duct Tape, Friendship Necklace and Lilith's Birthright // arrange the line their own ways, which stops the game. void Entity_Familiar::FollowParent() { if (isSubtype55555) { pos = formationOffset; velocity = {}; return; } if (IsLilithBirthrightIncubus()) return reconstruction_missing("Entity_Familiar::FollowParent 00612bb0 (Tainted Lilith's Incubus)"); if (player->spawnerEntity && player->spawnerEntity->type == SIREN_TYPE && player->spawnerEntity->variant == 0 && player->HasEntityFlags(FLAG_SPAWN_STICKY_SPIDERS)) return reconstruction_missing("Entity_Familiar::FollowParent 00612bb0 (the Siren's charm)"); if (player->HasTrinket(TRINKET_RC_REMOTE)) return predict_player_movement(); if (player->HasTrinket(TRINKET_DUCT_TAPE)) return move_to_parent_offset(); if (!vector_is_zero(formationOffset)) reset_follow_target(); if (g_game->challenge == CHALLENGE_PONG && variant == ISAACS_HEART) return reconstruction_missing("Entity_Familiar::FollowParent 00612bb0 (the punching bag challenge)"); bool isLilithBirthright = player->playerType == PLAYER_LILITH && player->HasCollectible(COLLECTIBLE_BIRTHRIGHT); if (isLilithBirthright && is_counted_follower()) return reconstruction_missing("Entity_Familiar::FollowParent 00612bb0 (Lilith's Birthright)"); if (player->HasTrinket(TRINKET_FRIENDSHIP_NECKLACE) && variant != KING_BABY) return reconstruction_missing("Entity_Familiar::FollowParent 00612bb0 (Friendship Necklace)"); orbitDistance = {}; Entity* leader = follow_leader_find(*this, isLilithBirthright); Vector leaderPos = leader ? leader->pos : player->pos; float distance = variant == MILK ? MILK_FOLLOW_DISTANCE : FOLLOW_DISTANCE; if (player->HasTrinket(TRINKET_CHILD_LEASH)) distance = LEASH_FOLLOW_DISTANCE; Vector spot = vector_add(leaderPos, vector_resized(vector_sub(pos, leaderPos), distance)); float gap = vector_length(vector_sub(leaderPos, pos)); if (distance < gap) velocity = vector_scale(vector_sub(spot, pos), (gap - distance) / gap * FOLLOW_PULL); friction *= FOLLOW_FRICTION; } // 006348d0 Drops its place in the formation, to be given a new one. void Entity_Familiar::reset_follow_target() { formationTargetOffset = {}; formationOffset = {}; formationAngle = 0; formationMode = FORMATION_NONE; } // Its place among the player's formed-up followers by rank, and how many there are (part of 00634260). static std::pair formation_slot(Entity_Familiar& familiar) { std::multimap ranked; for (Entity_Familiar* other : familiar_query(-1)) if (!other->isDead && other->isExists && !other->is_formation_exempt() && other->player == familiar.player) ranked.emplace(other->GetFollowerPriority(), other); int slot = 0; for (auto it = ranked.begin(); it != ranked.end() && it->second != &familiar; ++it) slot++; return {slot, (int)ranked.size()}; } // The offset from the player a formation gives a slot. static Vector formation_offset(Entity_Familiar& familiar, int slot, int count) { switch (familiar.formationMode) { case FORMATION_ROW: return vector_make((float)(slot / 2 + 1) * FORMATION_ROW_STEP * (slot % 2 == 0 ? 1 : -1), 0); case FORMATION_V: { int side = count == 2 ? slot : slot - 1; if (side < 0) return vector_make(0, FORMATION_V_DEPTH); float x = (float)(side / 2 + 1) * FORMATION_V_STEP * (side % 2 == 0 ? -1 : 1); return vector_make(x, FORMATION_V_DEPTH - std::fabs(x)); } case FORMATION_RING: { float angle = 360.0f / (float)count * (float)slot * DEGREES_TO_RADIANS; return vector_make(-std::sin(angle) * FORMATION_RING_RADIUS, std::cos(angle) * FORMATION_RING_RADIUS); } default: return vector_sub(familiar.pos, familiar.player->pos); } } // 00634260 Its offset from the player in Duct Tape's formation, turned with the player's aim: a slot by rank in a row, // a V or a ring, eased toward and refreshed when the player changes the formation. Vector Entity_Familiar::UpdateFollowPosition() { if (is_formation_exempt()) return {}; if ((uint32_t)player->ductTapeMode != formationMode) formationTargetOffset = {}; if (vector_is_zero(formationTargetOffset)) { formationMode = (uint8_t)player->ductTapeMode; auto [slot, count] = formationMode != FORMATION_FREE ? formation_slot(*this) : std::pair{0, 0}; formationTargetOffset = formation_offset(*this, slot, count); if (formationMode != FORMATION_ROW && formationMode != FORMATION_V && formationMode != FORMATION_RING) formationOffset = formationTargetOffset; } if (formationOffset.x != formationTargetOffset.x || formationOffset.y != formationTargetOffset.y) formationOffset = vector_add(formationOffset, vector_scale(vector_sub(formationTargetOffset, formationOffset), FORMATION_LERP)); float aimAngle = 0; Vector aim = player->aimDirection; if (formationMode != FORMATION_FREE && vector_length_squared(aim) > FORMATION_AIM_THRESHOLD) aimAngle = std::atan2(aim.y, aim.x) * RADIANS_TO_DEGREES - 90; float from = angle_wrap(formationAngle); float to = angle_wrap(aimAngle); if (std::fabs(to - from) > 180) { if (to <= from) to += 360; else from += 360; } formationAngle = angle_wrap((to - from) * FORMATION_LERP + from); float radians = formationAngle * DEGREES_TO_RADIANS; float cosine = std::cos(radians); float sine = std::sin(radians); return vector_make(cosine * formationOffset.x - sine * formationOffset.y, cosine * formationOffset.y + sine * formationOffset.x); } // 0063b4a0 Where it should be when it arrives with its player: its orbit spot, a free spot for the Sprinkler, or beside // the player (scattered when it is new), kept off the walls. Vector Entity_Familiar::GetFollowTargetPosition(FollowSpot spot) { if (g_game->nbRooms == 0) return player->pos; if (orbitLayer >= 0) return GetOrbitPosition(player->pos); if (variant == SPRINKLER) return room_free_pickup_position_find({.position = player->pos}); if (variant == FORGOTTEN_BODY) return pos; if (variant == ISAACS_BODY || variant == SCISSORS || variant == DECAP_ATTACK) return player->pos; Vector target = player->pos; if (spot == FOLLOW_SCATTERED) target = vector_add(target, vector_scale(random_direction(), 20)); if (is_following() && player->HasTrinket(TRINKET_RC_REMOTE)) target = vector_add(target, random_direction()); if (spot == FOLLOW_EXACT) { if (variant == BLOOD_BABY) target = vector_add(target, random_direction()); if (variant == BLOOD_PUPPY && linkedEntity) reconstruction_missing("Entity_Familiar::GetFollowTargetPosition " "0063b4a0 (Blood Puppy's spot)"); } return g_game->room->GetClampedPositionImpl(target, 20, 20, 20, 20); } // 00612330 What it orbits: a blue fly's or wisp's parent, Gello's cord for wisps with Book of Virtues, else its player. Entity* Entity_Familiar::get_orbit_parent() { if ((variant == BLUE_FLY || variant == WISP) && parent) return parent; if (player->HasCollectible(COLLECTIBLE_BOOK_OF_VIRTUES) && player->HasCollectible(COLLECTIBLE_GELLO)) { std::vector cords = familiar_query(UMBILICAL_BABY); if (!cords.empty()) return cords.front(); } return player; } // 00612440 Spaces the orbitals of a layer around the same centre evenly, with the familiar joining or leaving it, and // gives them the layer's speed: faster with Guardian Angel, slower with Big Fan, doubled for the unicorn wisps and // tripled for Best Bud. void Entity_Familiar::RecalculateOrbitOffset(int layer, OrbitChange change) { if (isSubtype55555 || layer < 0) return; bool isWide = is_wide_wisp(*this); std::vector orbiters; for (Entity_Familiar* other : familiar_query(-1)) { if (!other->isExists || other == this || other->orbitLayer != layer) continue; if (other->get_orbit_parent() != get_orbit_parent()) continue; isWide = isWide || is_wide_wisp(*other); orbiters.push_back(other); } float speedScale = isWide ? WIDE_WISP_SPEED : 1; if (player->HasCollectible(COLLECTIBLE_BIG_FAN)) speedScale = BIG_FAN_SPEED; if (change == ORBIT_JOIN) orbiters.push_back(this); const OrbitLayer& entry = orbit_layer(layer); for (size_t i = 0; i < orbiters.size(); i++) { Entity_Familiar& orbiter = *orbiters[i]; orbiter.orbitAngleOffset = (float)i * TWO_PI / (float)orbiters.size(); float speed = player->HasCollectible(COLLECTIBLE_GUARDIAN_ANGEL) ? entry.guardianAngelSpeed * entry.speed : entry.speed; orbiter.orbitSpeed = speed * speedScale; if (orbiter.variant == BEST_BUD) orbiter.orbitSpeed *= BEST_BUD_SPEED; } } // 006127b0 Moves it to another orbit layer. void Entity_Familiar::AddToOrbit(int layer) { if (orbitLayer >= 0) RecalculateOrbitOffset(orbitLayer, ORBIT_LEAVE); orbitLayer = layer; orbitDistance = GetOrbitDistance(layer); RecalculateOrbitOffset(layer, ORBIT_JOIN); } // 00612820 Leaves its orbit, the rest of the orbit closing the gap. void Entity_Familiar::RemoveFromOrbit() { if (orbitLayer < 0) return; RecalculateOrbitOffset(orbitLayer, ORBIT_LEAVE); orbitLayer = -1; } // 00612850 The ellipse of an orbit layer. Vector Entity_Familiar::GetOrbitDistance(int layer) { const OrbitLayer& entry = orbit_layer(layer); return vector_make(entry.distanceX, entry.distanceY); } // 006139b0 Its spot on its orbit around a centre this frame, half a step on between game frames. The Sacrificial Dagger // and Slipped Rib turn their sprite with it. Not orbiting, it stays on the centre. Vector Entity_Familiar::GetOrbitPosition(const Vector& center) { if (isSubtype55555) return formationOffset; if (player->HasTrinket(TRINKET_DUCT_TAPE) && variant != BLUE_FLY) return vector_add(UpdateFollowPosition(), center); if (!vector_is_zero(formationOffset)) reset_follow_target(); float phase = (float)(g_manager->framecount % 2) * 0.5f + (float)g_game->frameCount; float angle = std::fmod(orbitAngleOffset - phase * orbitSpeed * PI_F * 0.5f, TWO_PI); if (variant == SACRIFICIAL_DAGGER) { if (sprite.SetAnimation("Rotation", SWITCH_KEEP_FRAME) && sprite.animState.animData) { sprite.animState.SetFrameFloat(SACRIFICIAL_DAGGER_ROTATION_FRAME); sprite.animState.isPlaying = false; } sprite.GetLayer(0)->rotation = angle * 180 / PI_F; } if (variant == SLIPPED_RIB) sprite.GetLayer(0)->rotation = angle * 180 / PI_F; if (orbitLayer < 0) return center; float layerAngle = angle + orbit_layer(orbitLayer).angleOffset; return vector_make(std::cos(layerAngle) * orbitDistance.x + center.x, std::sin(layerAngle) * orbitDistance.y + center.y); } // 00634a40 After a change to the formation, makes the player's other formed-up followers take new places. void Entity_Familiar::ResolveOrbitConflict() { if (formationMode == FORMATION_FREE || is_formation_exempt()) return; for (Entity_Familiar* other : familiar_query(-1)) if (other != this && !other->is_formation_exempt() && other->player == player) other->formationMode = FORMATION_NONE; }