Files
MinecraftConsoles/Minecraft.World/AvoidPlayerGoal.cpp
daoge b3feddfef3 feat: TU19 (Dec 2014) Features & Content (#155)
* try to resolve merge conflict

* feat: TU19 (Dec 2014) Features & Content (#32)

* December 2014 files

* Working release build

* Fix compilation issues

* Add sound to Windows64Media

* Add DLC content and force Tutorial DLC

* Revert "Add DLC content and force Tutorial DLC"

This reverts commit 97a4399472.

* Disable broken light packing

* Disable breakpoint during DLC texture map load

Allows DLC loading but the DLC textures are still broken

* Fix post build not working

* ...

* fix vs2022 build

* fix cmake build

---------

Co-authored-by: Loki <lokirautio@gmail.com>
2026-03-03 03:04:10 +08:00

96 lines
2.9 KiB
C++

#include "stdafx.h"
#include "net.minecraft.world.entity.ai.control.h"
#include "net.minecraft.world.entity.ai.navigation.h"
#include "net.minecraft.world.entity.ai.sensing.h"
#include "net.minecraft.world.entity.ai.util.h"
#include "net.minecraft.world.entity.h"
#include "net.minecraft.world.entity.animal.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.pathfinder.h"
#include "net.minecraft.world.phys.h"
#include "AvoidPlayerGoal.h"
AvoidPlayerGoalEntitySelector::AvoidPlayerGoalEntitySelector(AvoidPlayerGoal *parent)
{
m_parent = parent;
}
bool AvoidPlayerGoalEntitySelector::matches(shared_ptr<Entity> entity) const
{
return entity->isAlive() && m_parent->mob->getSensing()->canSee(entity);
}
AvoidPlayerGoal::AvoidPlayerGoal(PathfinderMob *mob, const type_info& avoidType, float maxDist, double walkSpeedModifier, double sprintSpeedModifier) : avoidType(avoidType)
{
this->mob = mob;
//this->avoidType = avoidType;
this->maxDist = maxDist;
this->walkSpeedModifier = walkSpeedModifier;
this->sprintSpeedModifier = sprintSpeedModifier;
this->pathNav = mob->getNavigation();
setRequiredControlFlags(Control::MoveControlFlag);
entitySelector = new AvoidPlayerGoalEntitySelector(this);
toAvoid = weak_ptr<Entity>();
path = NULL;
}
AvoidPlayerGoal::~AvoidPlayerGoal()
{
if(path != NULL) delete path;
delete entitySelector;
}
bool AvoidPlayerGoal::canUse()
{
if (avoidType == typeid(Player))
{
shared_ptr<TamableAnimal> tamableAnimal = dynamic_pointer_cast<TamableAnimal>(mob->shared_from_this());
if (tamableAnimal != NULL && tamableAnimal->isTame()) return false;
toAvoid = weak_ptr<Entity>(mob->level->getNearestPlayer(mob->shared_from_this(), maxDist));
if (toAvoid.lock() == NULL) return false;
}
else
{
vector<shared_ptr<Entity> > *entities = mob->level->getEntitiesOfClass(avoidType, mob->bb->grow(maxDist, 3, maxDist), entitySelector);
if (entities->empty())
{
delete entities;
return false;
}
toAvoid = weak_ptr<Entity>(entities->at(0));
delete entities;
}
Vec3 *pos = RandomPos::getPosAvoid(dynamic_pointer_cast<PathfinderMob>(mob->shared_from_this()), 16, 7, Vec3::newTemp(toAvoid.lock()->x, toAvoid.lock()->y, toAvoid.lock()->z));
if (pos == NULL) return false;
if (toAvoid.lock()->distanceToSqr(pos->x, pos->y, pos->z) < toAvoid.lock()->distanceToSqr(mob->shared_from_this())) return false;
delete path;
path = pathNav->createPath(pos->x, pos->y, pos->z);
if (path == NULL) return false;
if (!path->endsInXZ(pos)) return false;
return true;
}
bool AvoidPlayerGoal::canContinueToUse()
{
return toAvoid.lock() != NULL && !pathNav->isDone();
}
void AvoidPlayerGoal::start()
{
pathNav->moveTo(path, walkSpeedModifier);
path = NULL;
}
void AvoidPlayerGoal::stop()
{
toAvoid = weak_ptr<Entity>();
}
void AvoidPlayerGoal::tick()
{
if (mob->distanceToSqr(toAvoid.lock()) < 7 * 7) mob->getNavigation()->setSpeedModifier(sprintSpeedModifier);
else mob->getNavigation()->setSpeedModifier(walkSpeedModifier);
}