aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/NearestAttackableTargetGoal.cpp
diff options
context:
space:
mode:
authorLoki Rautio <lokirautio@gmail.com>2026-03-04 03:56:03 -0600
committerLoki Rautio <lokirautio@gmail.com>2026-03-04 03:56:03 -0600
commit42aec6dac53dffa6afe072560a7e1d4986112538 (patch)
tree0836426857391df1b6a83f6368a183f83ec9b104 /Minecraft.World/NearestAttackableTargetGoal.cpp
parentc9d58eeac7c72f0b3038e084667b4d89a6249fce (diff)
parentef9b6fd500dfabd9463267b0dd9e29577eea8a2b (diff)
Merge branch 'main' into pr/win64-world-saves
# Conflicts: # Minecraft.Client/MinecraftServer.cpp # README.md
Diffstat (limited to 'Minecraft.World/NearestAttackableTargetGoal.cpp')
-rw-r--r--Minecraft.World/NearestAttackableTargetGoal.cpp60
1 files changed, 34 insertions, 26 deletions
diff --git a/Minecraft.World/NearestAttackableTargetGoal.cpp b/Minecraft.World/NearestAttackableTargetGoal.cpp
index 5ed9f4f1..1dbf587e 100644
--- a/Minecraft.World/NearestAttackableTargetGoal.cpp
+++ b/Minecraft.World/NearestAttackableTargetGoal.cpp
@@ -4,6 +4,24 @@
#include "net.minecraft.world.phys.h"
#include "NearestAttackableTargetGoal.h"
+SubselectEntitySelector::SubselectEntitySelector(NearestAttackableTargetGoal *parent, EntitySelector *subselector)
+{
+ m_parent = parent;
+ m_subselector = subselector;
+}
+
+SubselectEntitySelector::~SubselectEntitySelector()
+{
+ delete m_subselector;
+}
+
+bool SubselectEntitySelector::matches(shared_ptr<Entity> entity) const
+{
+ if (!entity->instanceof(eTYPE_LIVINGENTITY)) return false;
+ if (m_subselector != NULL && !m_subselector->matches(entity)) return false;
+ return m_parent->canAttack(dynamic_pointer_cast<LivingEntity>(entity), false);
+}
+
NearestAttackableTargetGoal::DistComp::DistComp(Entity *source)
{
this->source = source;
@@ -19,49 +37,39 @@ bool NearestAttackableTargetGoal::DistComp::operator() (shared_ptr<Entity> e1, s
return true;
}
-NearestAttackableTargetGoal::NearestAttackableTargetGoal(Mob *mob, const type_info& targetType, float within, int randomInterval, bool mustSee, bool mustReach /*= false*/) : TargetGoal(mob, within, mustSee, mustReach), targetType(targetType)
+NearestAttackableTargetGoal::NearestAttackableTargetGoal(PathfinderMob *mob, const type_info& targetType, int randomInterval, bool mustSee, bool mustReach /*= false*/, EntitySelector *entitySelector /* =NULL */)
+ : TargetGoal(mob, mustSee, mustReach), targetType(targetType)
{
- //this->targetType = targetType;
- this->within = within;
this->randomInterval = randomInterval;
this->distComp = new DistComp(mob);
setRequiredControlFlags(TargetGoal::TargetFlag);
+
+ this->selector = new SubselectEntitySelector(this, entitySelector);
}
NearestAttackableTargetGoal::~NearestAttackableTargetGoal()
{
delete distComp;
+ delete selector;
}
bool NearestAttackableTargetGoal::canUse()
{
if (randomInterval > 0 && mob->getRandom()->nextInt(randomInterval) != 0) return false;
- if (targetType == typeid(Player))
- {
- shared_ptr<Mob> potentialTarget = mob->level->getNearestAttackablePlayer(mob->shared_from_this(), within);
- if (canAttack(potentialTarget, false))
- {
- target = weak_ptr<Mob>(potentialTarget);
- return true;
- }
- }
- else
+ double within = getFollowDistance();
+
+ vector<shared_ptr<Entity> > *entities = mob->level->getEntitiesOfClass(targetType, mob->bb->grow(within, 4, within), selector);
+
+ bool result = false;
+ if(entities != NULL && !entities->empty() )
{
- vector<shared_ptr<Entity> > *entities = mob->level->getEntitiesOfClass(targetType, mob->bb->grow(within, 4, within));
- //Collections.sort(entities, distComp);
std::sort(entities->begin(), entities->end(), *distComp);
- for(AUTO_VAR(it, entities->begin()); it != entities->end(); ++it)
- {
- shared_ptr<Mob> potTarget = dynamic_pointer_cast<Mob>(*it);
- if (canAttack(potTarget, false))
- {
- target = weak_ptr<Mob>(potTarget);
- return true;
- }
- }
- delete entities;
+ target = weak_ptr<LivingEntity>(dynamic_pointer_cast<LivingEntity>(entities->at(0)));
+ result = true;
}
- return false;
+
+ delete entities;
+ return result;
}
void NearestAttackableTargetGoal::start()