141 lines
6.4 KiB
C++
141 lines
6.4 KiB
C++
#include "npc_ai.h"
|
|
#include "active_items.h"
|
|
#include "entity_effect.h"
|
|
#include "entity_familiar.h"
|
|
#include "entity_player.h"
|
|
#include "game.h"
|
|
#include "menu_manager.h"
|
|
#include "room.h"
|
|
|
|
constexpr int DIP_BIG_CORN = 3;
|
|
constexpr int FAMILIAR_DIP_CORN = 2;
|
|
constexpr int FAMILIAR_DIP_BROWNIE = 20;
|
|
constexpr int COLLECTIBLE_DIRTY_MIND_ID = 576;
|
|
constexpr int TRINKET_DIPPING_TRINKET_ID = 163;
|
|
|
|
// How a Dip hops: one chance in ten a frame from rest, at a random speed (doubled for the corn dip), and how it
|
|
// slows, splats and squeaks.
|
|
constexpr int HOP_ODDS = 10;
|
|
constexpr float HOP_SPEED_MIN = 5;
|
|
constexpr float HOP_SPEED_RANGE = 5;
|
|
constexpr float HOP_FRICTION = 0.8f;
|
|
constexpr float SLIDE_FRICTION = 0.95f;
|
|
constexpr float SPLAT_SIZE_MIN = 0.2f;
|
|
constexpr float SPLAT_SIZE_RANGE = 0.5f;
|
|
constexpr int SPLAT_ODDS = 10;
|
|
constexpr float HURT_VOLUME = 0.8f;
|
|
constexpr int HURT_SOUND_DELAY = 15;
|
|
constexpr float CORN_AIM_LEAD = 0.1f;
|
|
constexpr float CORN_SHOT_SPEED = 10;
|
|
|
|
// The Drip hops slower (5 to 8), one time in three at its target, pushed back by the room's current, slides with
|
|
// less friction and may hop again from frame 10 of its hop, one time in three.
|
|
constexpr float DRIP_HOP_SPEED_RANGE = 3;
|
|
constexpr int DRIP_AIM_ODDS = 3;
|
|
constexpr float DRIP_CURRENT_PUSH = 8;
|
|
constexpr float DRIP_FRICTION = 0.9f;
|
|
constexpr int DRIP_REHOP_FRAME = 10;
|
|
constexpr int DRIP_REHOP_ODDS = 3;
|
|
constexpr float CONTROLLER_DEADZONE = 0.1f;
|
|
|
|
// A hop: a random way (away from its target while it fears it), faster for the corn dips. The Drip hops at its
|
|
// target one time in three and against the room's current.
|
|
static void dip_hop(Entity_NPC& npc)
|
|
{
|
|
npc.state = NPC_STATE_MOVE;
|
|
bool isDrip = npc.type == ENTITY_DRIP;
|
|
bool isFleeing = npc.HasEntityFlags(FLAG_FEAR | FLAG_SHRINK) && npc.controllerID == -1;
|
|
Vector direction;
|
|
if (isFleeing) direction = vector_normalized(vector_sub(npc.pos, npc.GetPlayerTarget()->pos));
|
|
else
|
|
{
|
|
bool isAimed = isDrip && Random() % DRIP_AIM_ODDS == 0;
|
|
direction = isAimed ? vector_normalized(vector_sub(npc.GetPlayerTarget()->pos, npc.pos)) : random_direction();
|
|
if (npc.controllerID != -1) direction = npc.GetControllerInput(0);
|
|
}
|
|
|
|
float speed = random_unit() * (isDrip ? DRIP_HOP_SPEED_RANGE : HOP_SPEED_RANGE) + HOP_SPEED_MIN;
|
|
bool isDoubled = !isDrip && (npc.variant == DIP_CORN || (isFleeing && npc.variant == DIP_BROWNIE));
|
|
Vector hop = vector_scale(direction, isDoubled ? speed * 2 : speed);
|
|
Vector current = g_game->room->waterCurrent;
|
|
if (isDrip && !isFleeing && !vector_is_zero(current)) hop = vector_sub(hop, vector_scale(current, DRIP_CURRENT_PUSH));
|
|
if (npc.friction != 0)
|
|
npc.velocity = vector_add(npc.velocity, vector_scale(hop, npc.speedMultiplier / npc.friction));
|
|
}
|
|
|
|
// The big corn dip's dying shot, aimed where its target is heading.
|
|
static void dip_corn_shoot(Entity_NPC& npc)
|
|
{
|
|
Entity* target = npc.GetPlayerTarget();
|
|
float distance = vector_length(vector_sub(npc.pos, target->pos));
|
|
Vector lead = vector_add(target->pos, vector_scale(target->velocity, CORN_AIM_LEAD * distance));
|
|
ProjectileParams params;
|
|
params.variant = PROJECTILE_CORN;
|
|
npc.FireProjectiles(npc.pos, vector_scale(vector_normalized(vector_sub(lead, npc.pos)), CORN_SHOT_SPEED),
|
|
PROJECTILES_SINGLE, params);
|
|
}
|
|
|
|
// 00534860 The Dip's and the Drip's AI: it rests, hops about (fleeing what it fears), slides to a stop and leaves
|
|
// poop splats. Dirty Mind and the Dipping trinket turn it into the players' own dip.
|
|
void npc_dip_update(Entity_NPC& npc)
|
|
{
|
|
bool isDrip = npc.type == ENTITY_DRIP;
|
|
PlayerManager& players = g_game->playerManager;
|
|
bool isTamed = players.FirstCollectibleOwner(COLLECTIBLE_DIRTY_MIND_ID, LAZARUS_SHARE_CHECK) ||
|
|
players.AnyoneHasTrinket(TRINKET_DIPPING_TRINKET_ID);
|
|
if (!npc.isDead && isTamed)
|
|
{
|
|
Entity* owner = players.RandomCollectibleOwner(COLLECTIBLE_DIRTY_MIND_ID, npc.initSeed, nullptr);
|
|
if (!owner) owner = players.RandomTrinketOwner(TRINKET_DIPPING_TRINKET_ID, npc.initSeed, nullptr);
|
|
if (owner)
|
|
{
|
|
npc.entityCollisionClass = ENTCOLL_NONE;
|
|
npc.Remove();
|
|
bool isCorn = !isDrip && (npc.variant == DIP_CORN || npc.variant == DIP_BIG_CORN);
|
|
int dip = !isDrip && npc.variant == DIP_BROWNIE ? FAMILIAR_DIP_BROWNIE : isCorn ? FAMILIAR_DIP_CORN : 0;
|
|
Entity_Familiar::ThrowDip(dip, npc.pos, owner);
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (npc.state == NPC_STATE_INIT)
|
|
{
|
|
npc.state = NPC_STATE_IDLE;
|
|
npc.splatColor = npc.variant == DIP_BROWNIE ? LIGHT_POOP_SPLAT_COLOR : POOP_SPLAT_COLOR;
|
|
return;
|
|
}
|
|
bool isBigCorn = npc.type == ENTITY_DIP && npc.variant == DIP_BIG_CORN;
|
|
bool isFloating = (isDrip || isBigCorn) && g_game->room->waterAmount > 0;
|
|
npc.entityGridCollisionClass = isFloating ? GRIDCOLL_NOPITS : GRIDCOLL_GROUND;
|
|
|
|
if (npc.state == NPC_STATE_IDLE || npc.state == NPC_STATE_SPECIAL)
|
|
{
|
|
npc.sprite.Play("Idle");
|
|
bool isHopping = npc.state == NPC_STATE_SPECIAL ||
|
|
(npc.controllerID == -1 ? Random() % HOP_ODDS == 0
|
|
: !vector_is_zero(npc.GetControllerInput(CONTROLLER_DEADZONE)));
|
|
if (isHopping) dip_hop(npc);
|
|
npc.friction *= isDrip ? DRIP_FRICTION : HOP_FRICTION;
|
|
}
|
|
else if (npc.state == NPC_STATE_MOVE)
|
|
{
|
|
npc.sprite.Play("Move");
|
|
npc.friction *= SLIDE_FRICTION;
|
|
bool isRehop = isDrip && npc.sprite.GetFrame() == DRIP_REHOP_FRAME &&
|
|
(Random() % DRIP_REHOP_ODDS == 0 || npc.controllerID != -1);
|
|
if (isRehop) npc.state = NPC_STATE_SPECIAL;
|
|
else if (isDrip ? !npc.sprite.IsPlaying() : vector_length_squared(npc.velocity) < 1)
|
|
npc.state = NPC_STATE_IDLE;
|
|
}
|
|
|
|
npc.sprite.isFlipX = npc.velocity.x < 0;
|
|
if (Random() % SPLAT_ODDS == 0)
|
|
npc.MakeSplat(random_unit() * SPLAT_SIZE_RANGE + SPLAT_SIZE_MIN)
|
|
->SetColor(npc.splatColor, -1, COLOR_PRIORITY_DEFAULT, false, true);
|
|
if (npc.pendingDamage > 0 && (Random() & 1) == 0)
|
|
npc.PlaySound(SOUND_DIP_HURT, HURT_VOLUME, HURT_SOUND_DELAY, false, 1);
|
|
if (isBigCorn && npc.controllerID != -1 && input_is_action_pressed(ACTION_BOMB, npc.controllerID, &npc))
|
|
npc.Kill(EntityRef());
|
|
if (npc.isDead && isBigCorn && !npc.HasEntityFlags(FLAG_SHRINK)) dip_corn_shoot(npc);
|
|
}
|