#include "entity_effect.h" #include "entity_effect_combat.h" #include "entity_effect_trail.h" #include "entity_list.h" #include "entity_player.h" #include "entity_player_forgotten.h" #include "game.h" #include "kage_graphics.h" #include "log.h" #include "lua_engine.h" #include "manager.h" #include "room.h" #include "sfx_manager.h" #include "mines.h" #include #include #include // The creep variants the players leave (holy water trail, black, red, white, holy water, green, Lemon Mishap, Lemon // Party, 204, Black Powder, milk, Black Powder's pentagram). constexpr std::array PLAYER_CREEPS = {54, 45, 46, 44, 37, 53, 32, 78, 204, 92, 90, 93}; // The floor stains a crawlspace never shows: blood, craters, creep of every kind, footprints and puddles. constexpr std::array CRAWLSPACE_HIDDEN_EFFECTS = {7, 18, 22, 23, 24, 25, 26, 32, 37, 44, 45, 46, 53, 54, 56, 77, 78, 89, 90, 92, 93, 94, 100, 169, 195, 204}; // The creep a familiar's wave of creep (familiar variant 125) leaves, which keeps its chain going. constexpr std::array WAVE_CHAIN_CREEPS = {53, 44, 46, 45, 32, 204}; constexpr int FAMILIAR_CREEP_WAVE = 125; constexpr int EFFECT_HUSH_LASER = 96; constexpr int EFFECT_HUSH_LASER_UP = 101; constexpr int EFFECT_BLACK_POWDER = 92; constexpr int EFFECT_BOOMERANG = 60; constexpr int EFFECT_TIMER = 119; constexpr int EFFECT_ENEMY_SOUL = 179; constexpr int EFFECT_REDEMPTION = 185; // The Hourglass slows Hush's lasers from full speed to half as its effect's cooldown runs from 0 to 30. constexpr float HOURGLASS_COOLDOWN_SPAN = 30; constexpr float HOURGLASS_SLOWEST = 0.5f; constexpr float PAUSE_VELOCITY_DECAY = 0.6f; constexpr int SOUND_BOOMERANG_LOOP = 837; constexpr int SOUND_PORTAL_ENTITY_LOOP = 846; constexpr int SOUND_CHAIN_LOOP = 503; // The variants the behaviour tables can index, up to the last vanilla one. constexpr int EFFECT_VARIANT_LIMIT = 256; template static bool is_listed(const std::array& list, int value) { return std::find(list.begin(), list.end(), value) != list.end(); } // A timer's callback, how many frames apart it runs and how many more times. None runs once, then it goes. struct TimerData : Entity_Effect::ExtraData { void (*callback)(Entity_Effect* timer) = nullptr; uint32_t delay = 1; int times = 1; }; // The timer part of 005cdb20 (Init): a timer is invisible, never overwritten, and has nothing to run yet. static void timer_init(Entity_Effect& timer) { timer.isVisible = false; timer.extraData = std::make_shared(); timer.AddEntityFlags(FLAG_DONT_OVERWRITE); } // The timer part of 005d4380 (Update): runs the callback every delay frames of its life until its times run out, then // removes itself. Without a callback it goes at once. // A delay of 0 divided by zero in the original. Here it runs every frame. static EffectUpdateEnd timer_update(Entity_Effect& timer) { auto& data = static_cast(*timer.extraData); if (!data.callback) { timer.Remove(); return EFFECT_UPDATE_RETURN; } if (data.delay && (uint32_t)timer.FrameCount() % data.delay != 0) return EFFECT_UPDATE_FINISH; data.callback(&timer); if (data.times > 0) data.times--; if (data.times != 0) return EFFECT_UPDATE_FINISH; timer.Remove(); return EFFECT_UPDATE_RETURN; } constexpr EffectBehaviour TIMER_BEHAVIOURS[] = {{EFFECT_TIMER, timer_init, timer_update}}; // The behaviour of a variant one of the groups hands out, indexed once by variant, or none. static const EffectBehaviour* behaviour_find(int variant) { static const std::array TABLE = [] { std::array table{}; for (auto group : {ambient_effect_behaviours(), hazard_effect_behaviours(), floor_effect_behaviours(), beam_effect_behaviours(), player_effect_behaviours(), mines_effect_behaviours(), trail_effect_behaviours(), spike_effect_behaviours(), std::span(TIMER_BEHAVIOURS)}) for (const EffectBehaviour& behaviour : group) table[behaviour.variant] = behaviour; return table; }(); if (variant < 0 || variant >= EFFECT_VARIANT_LIMIT || TABLE[variant].variant != variant) return nullptr; return &TABLE[variant]; } // 005d4b7a (Update's shared poof case) Turns the sprite by the effect's rotation and removes the effect once its // animation ends. EffectUpdateEnd effect_poof_update(Entity_Effect& effect) { effect.sprite.rotation = effect.sprite.isFlipX ? effect.rotation : -effect.rotation; if (effect.sprite.IsFinished()) effect.Remove(); return EFFECT_UPDATE_FINISH; } // An angle in degrees wrapped into -180 to 180, as approach_angle wraps it. static float angle_wrapped(float angle) { if (std::fabs(angle) > DEGREES_PER_TURN) angle = std::fmod(angle, DEGREES_PER_TURN); if (angle > DEGREES_PER_TURN / 2) return angle - DEGREES_PER_TURN; if (angle < -DEGREES_PER_TURN / 2) return angle + DEGREES_PER_TURN; return angle; } // 00682a50 An angle in degrees turned toward a target by at most a step, reaching it when within the step. float angle_approach(float current, float target, float maxStep) { float delta = angle_wrapped(target - current); if (std::fabs(delta) <= maxStep) return target; return angle_wrapped(delta > 0 ? current + maxStep : current - maxStep); } // 00a15640 (assert_lerp_range, always clamped here) A value's place between two inputs, carried onto two outputs. float lerp_clamped(float value, float inFirst, float inLast, float outFirst, float outLast) { float t = (value - inFirst) / (inLast - inFirst); if (t <= 0) t = 0; if (t >= 1) t = 1; return (outLast - outFirst) * t + outFirst; } // BlendMode::SetMode 2 on a layer. void layer_additive_set(LayerState& layer) { KAGE::Graphics::BlendMode mode; mode.SetMode(KAGE::Graphics::BLEND_ADDITIVE); layer.blendMode = {mode.equation, mode.rgbSource, mode.rgbDest, mode.alphaSource, mode.alphaDest}; } // 0060c400 Only a boss spawn pentagram holds the doors shut. bool Entity_Effect::CanShutDoors() { return variant == EFFECT_SPAWN_PENTAGRAM; } // 005ccc20 Collides with nothing it reacts to, letting the collision through. bool Entity_Effect::HandleCollision(Entity*, bool) { return true; } // 00435e90 Sets how long it lasts, from now. void Entity_Effect::SetTimeout(int frames) { timeout = frames; lifespan = frames; } // 00435ef0 Sets the radius it grows between. void Entity_Effect::SetRadii(float newMinRadius, float newMaxRadius) { minRadius = newMinRadius; maxRadius = newMaxRadius; } // 005cd950 Whether two indices differ, the filter of the rift's pull. bool Entity_Effect::rift_attract_filter(int first, int second) { return first != second; } // 0060ac20 Whether the variant is creep or fire the players leave, which hurts only enemies. bool Entity_Effect::IsPlayerCreep(int effectVariant) { return std::find(PLAYER_CREEPS.begin(), PLAYER_CREEPS.end(), effectVariant) != PLAYER_CREEPS.end(); } // 005cdb20 Starts the effect from its config's sprite with no timeout, rotation or extra data, then hands it to its // variant. A crawlspace removes its floor stains at once, and a variant that removes itself skips its graphics and // Lua's callback. void Entity_Effect::Init(int entityType, int entityVariant, int entitySubType, uint32_t seed) { extraData.reset(); Entity::Init(entityType, entityVariant, entitySubType, seed); hitList.clear(); maxHitPoints = 0; if (config) { sprite.Load(config->anm2Path, GRAPHICS_DEFER); sprite.Play(sprite.GetDefaultAnimationName()); } state = 0; timeout = -1; lifespan = 1; parentOffset = {}; rotation = 0; isFollowing = false; scale = 1; minRadius = 0; maxRadius = 0; damageSource = 0; varData = {}; if (g_game->room->roomType == ROOM_DUNGEON && is_listed(CRAWLSPACE_HIDDEN_EFFECTS, variant)) { isVisible = false; return Remove(); } entityGridCollisionClass = variant == EFFECT_BLOOD_PARTICLE ? GRIDCOLL_WALLS : GRIDCOLL_NONE; entityCollisionClass = ENTCOLL_NONE; if (const EffectBehaviour* behaviour = behaviour_find(variant)) { if (behaviour->init) behaviour->init(*this); } else if (is_combat_effect(variant)) combat_effect_init(*this); else if (variant < EFFECT_VARIANT_LIMIT) reconstruction_missing("Entity_Effect::Init 005cdb20: this effect variant"); if (!isExists) return; sprite.LoadGraphics(); if (g_game->deathmatchManager.is_active()) reconstruction_missing("Entity_Effect::Init 005cdb20: the versus mode's entity registration"); lua_engine_get().PostEffectInit(this); } // 005d4380 Runs the effect for a frame at the room's time scale, held in place while paused and kept on its parent // while following it, then runs its variant and counts its timeout down to its death. void Entity_Effect::Update() { speedMultiplier = g_game->room->GetTimeScale(this); if (variant == EFFECT_HUSH_LASER || variant == EFFECT_HUSH_LASER_UP) { TemporaryEffect* hourglass = g_game->room->temporaryEffects.GetCollectibleEffect(COLLECTIBLE_HOURGLASS); if (hourglass) speedMultiplier *= lerp_clamped((float)hourglass->cooldown, 0, HOURGLASS_COOLDOWN_SPAN, 1, HOURGLASS_SLOWEST); } if (variant == EFFECT_BLACK_POWDER) pauseTimer = 0; if (pauseTimer != 0) { uint64_t savedFlags = flags; bool wasDead = isDead; if (pausePosition.x == 0 && pausePosition.y == 0) pausePosition = pos; if (pauseTimer > 0) pauseTimer--; pos = pausePosition; velocity = vector_make(velocity.x * PAUSE_VELOCITY_DECAY, velocity.y * PAUSE_VELOCITY_DECAY); AddEntityFlags(FLAG_NO_SPRITE_UPDATE); Entity::Update(); flags = savedFlags; if (wasDead == isDead) return; } Entity::Update(); if (isFollowing && parent) { pos = vector_make(parent->pos.x + parentOffset.x, parent->pos.y + parentOffset.y); velocity = parent->velocity; posOffset = parent->posOffset; } else if (isFollowing) { velocity = {}; isFollowing = false; if (variant == EFFECT_LIGHT && timeout < 0) SetTimeout(state); } EffectUpdateEnd end = EFFECT_UPDATE_FINISH; if (const EffectBehaviour* behaviour = behaviour_find(variant)) { if (behaviour->update) end = behaviour->update(*this); } else if (is_combat_effect(variant)) end = combat_effect_update(*this); else if (variant < EFFECT_VARIANT_LIMIT) reconstruction_missing("Entity_Effect::Update 005d4380: this effect variant"); if (end == EFFECT_UPDATE_RETURN) return; if (timeout > 0 && --timeout < 1) isDead = true; pausePosition = pos; if (spawnerType == ENTITY_FAMILIAR && spawnerVariant == FAMILIAR_CREEP_WAVE && is_listed(WAVE_CHAIN_CREEPS, variant)) update_wave_chain(); lua_engine_get().PostEffectUpdate(this); } // 005d2520 Moves the effect half a step between updates, keeping a following effect on its parent. void Entity_Effect::Interpolate() { if (pauseTimer != 0) return; Entity::Interpolate(); if (!isFollowing || !parent) return; pos = vector_make(parent->pos.x + parentOffset.x, parent->pos.y + parentOffset.y); posOffset = parent->posOffset; } // 00607620 Draws the effect at its place on screen through its variant, or its sprite. A light only lights the room. void Entity_Effect::Render(const Vector& offset) { if (variant == EFFECT_LIGHT || !isVisible) return; Vector screen = render_position_get(vector_make(pos.x + posOffset.x, pos.y + posOffset.y), RENDER_SNAPPED); renderPosition = vector_make(screen.x + offset.x, screen.y + offset.y); const EffectBehaviour* behaviour = behaviour_find(variant); if (behaviour && behaviour->render) behaviour->render(*this, offset); else sprite.Render(SpriteDraw{renderPosition, {}, {}}); lua_engine_get().PostEffectRender(this, offset); } // 005d2420 Stops the boomerang and portal loops unless another such effect still plays them, then removes it. void Entity_Effect::Remove() { bool isBoomerangShared = false; bool isPortalShared = false; for (Entity* entity : g_game->room->entityList.updateEL) { if (entity->type != ENTITY_EFFECT || entity == this) continue; if (entity->variant == EFFECT_BOOMERANG) isBoomerangShared = true; else if (entity->variant == EFFECT_PORTAL_TELEPORT) isPortalShared = true; } if (!isBoomerangShared) g_manager->sfxManager.Stop(SOUND_BOOMERANG_LOOP); if (!isPortalShared) g_manager->sfxManager.Stop(SOUND_PORTAL_ENTITY_LOOP); Entity::Remove(); } // 0060c370 Glowing Hourglass removes a timer, and sets Redemption back to idle. void Entity_Effect::TriggerGlowingHourglass() { if (variant == EFFECT_REDEMPTION) reconstruction_missing("Entity_Effect::TriggerGlowingHourglass 0060c370: Redemption's idle restart"); if (variant == EFFECT_TIMER) Remove(); } // 0060abb0 Gives an enemy soul to the player it flew to and stops a hidden Anima chain's sound, then lets go of its // links and extra data. void Entity_Effect::ClearReferences() { if (variant == EFFECT_ENEMY_SOUL && target && target->type == ENTITY_PLAYER) reconstruction_missing("Entity_Effect::ClearReferences 0060abb0: the Urn of Souls' soul"); if (variant == EFFECT_ANIMA_CHAIN && subtype == 0 && !isVisible) g_manager->sfxManager.Stop(SOUND_CHAIN_LOOP); Entity::ClearReferences(); extraData.reset(); } // 004564b0 Follows an entity, keeping the offset it has from it now. // The original reads a null entity's position and crashes. This only lets go of the parent. void Entity_Effect::FollowParent(Entity* newParent) { parent = newParent; if (!newParent) return; isFollowing = true; parentOffset = vector_make(pos.x - newParent->pos.x, pos.y - newParent->pos.y); } // The rest of Entity_Effect is not reconstructed yet. // 00435290 void Entity_Effect::render_player_target(const Vector&, int) { reconstruction_missing("Entity_Effect::render_player_target 00435290"); } // 005d2a70 void Entity_Effect::update_mr_me() { reconstruction_missing("Entity_Effect::update_mr_me 005d2a70"); } // 005d4010 void Entity_Effect::update_wave_chain() { reconstruction_missing("Entity_Effect::update_wave_chain 005d4010"); } // 006073f0 void Entity_Effect::moonlight_timer_callback(int) { reconstruction_missing("Entity_Effect::moonlight_timer_callback 006073f0"); } // 0060af00 Spawns a timer running a callback (taking the timer) every delay frames, times more times after the first // (0 for once), kept across rooms when persistent. Entity_Effect* Entity_Effect::CreateTimer(void* fn, uint32_t delay, uint32_t times, bool isPersistent) { auto* timer = static_cast( g_game->Spawn({.type = ENTITY_EFFECT, .variant = EFFECT_TIMER, .seed = Random()})); auto& data = static_cast(*timer->extraData); data.callback = reinterpret_cast(fn); data.delay = delay; data.times = (int)times; if (isPersistent) timer->AddEntityFlags(FLAG_PERSISTENT); return timer; } // 0060b0d0 void Entity_Effect::update_variant178_shooter(bool) { reconstruction_missing("Entity_Effect::update_variant178_shooter 0060b0d0"); } // 0060b7a0 Entity_Effect* Entity_Effect::CreateLootPreview(void*, const Vector&, Entity*, Entity_Effect*) { reconstruction_missing("Entity_Effect::CreateLootPreview 0060b7a0"); } // 0060bae0 void Entity_Effect::setup_loot_preview(void*) { reconstruction_missing("Entity_Effect::setup_loot_preview 0060bae0"); }