4jcraft/Minecraft.World/AI/Goals/PanicGoal.cpp
MatthewBeshay dfb0e3b03e refactor: replace NULL with nullptr across C++ codebase
Excludes vendored C libs (zlib, Miles, DirectXMath, boost, Iggy).
2026-03-30 16:25:52 +11:00

40 lines
1.4 KiB
C++

#include "../../Platform/stdafx.h"
#include "../../Headers/net.minecraft.world.entity.ai.control.h"
#include "../../Headers/net.minecraft.world.entity.ai.navigation.h"
#include "../../Headers/net.minecraft.world.entity.ai.util.h"
#include "../../Headers/net.minecraft.world.entity.h"
#include "../../Headers/net.minecraft.world.phys.h"
#include "PanicGoal.h"
PanicGoal::PanicGoal(PathfinderMob* mob, double speedModifier) {
this->mob = mob;
this->speedModifier = speedModifier;
setRequiredControlFlags(Control::MoveControlFlag);
}
bool PanicGoal::canUse() {
if (mob->getLastHurtByMob() == nullptr && !mob->isOnFire()) return false;
// 4jcraft: stop entities from being eternally scared (referenced from
// smartcmd/MinecraftConsoles #519)
const int hurtTimeout = mob->getLastHurtByMobTimestamp();
static thread_local Random random;
const int panicDuration = random.nextInt(41) + 60;
if (mob->tickCount - hurtTimeout > panicDuration) return false;
auto pos = RandomPos::getPos(
std::dynamic_pointer_cast<PathfinderMob>(mob->shared_from_this()), 5,
4);
if (!pos.has_value()) return false;
posX = pos->x;
posY = pos->y;
posZ = pos->z;
return true;
}
void PanicGoal::start() {
mob->getNavigation()->moveTo(posX, posY, posZ, speedModifier);
}
bool PanicGoal::canContinueToUse() { return !mob->getNavigation()->isDone(); }