the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 79 lines 2.4 kB view raw
1#include "stdafx.h" 2#include "net.minecraft.world.entity.h" 3#include "net.minecraft.world.level.h" 4#include "net.minecraft.world.phys.h" 5#include "NearestAttackableTargetGoal.h" 6 7SubselectEntitySelector::SubselectEntitySelector(NearestAttackableTargetGoal *parent, EntitySelector *subselector) 8{ 9 m_parent = parent; 10 m_subselector = subselector; 11} 12 13SubselectEntitySelector::~SubselectEntitySelector() 14{ 15 delete m_subselector; 16} 17 18bool SubselectEntitySelector::matches(shared_ptr<Entity> entity) const 19{ 20 if (!entity->instanceof(eTYPE_LIVINGENTITY)) return false; 21 if (m_subselector != NULL && !m_subselector->matches(entity)) return false; 22 return m_parent->canAttack(dynamic_pointer_cast<LivingEntity>(entity), false); 23} 24 25NearestAttackableTargetGoal::DistComp::DistComp(Entity *source) 26{ 27 this->source = source; 28} 29 30bool NearestAttackableTargetGoal::DistComp::operator() (shared_ptr<Entity> e1, shared_ptr<Entity> e2) 31{ 32 // Should return true if e1 comes before e2 in the sorted list 33 double distSqr1 = source->distanceToSqr(e1); 34 double distSqr2 = source->distanceToSqr(e2); 35 if (distSqr1 < distSqr2) return true; 36 if (distSqr1 > distSqr2) return false; 37 return true; 38} 39 40NearestAttackableTargetGoal::NearestAttackableTargetGoal(PathfinderMob *mob, const type_info& targetType, int randomInterval, bool mustSee, bool mustReach /*= false*/, EntitySelector *entitySelector /* =NULL */) 41 : TargetGoal(mob, mustSee, mustReach), targetType(targetType) 42{ 43 this->randomInterval = randomInterval; 44 this->distComp = new DistComp(mob); 45 setRequiredControlFlags(TargetGoal::TargetFlag); 46 47 this->selector = new SubselectEntitySelector(this, entitySelector); 48} 49 50NearestAttackableTargetGoal::~NearestAttackableTargetGoal() 51{ 52 delete distComp; 53 delete selector; 54} 55 56bool NearestAttackableTargetGoal::canUse() 57{ 58 if (randomInterval > 0 && mob->getRandom()->nextInt(randomInterval) != 0) return false; 59 double within = getFollowDistance(); 60 61 vector<shared_ptr<Entity> > *entities = mob->level->getEntitiesOfClass(targetType, mob->bb->grow(within, 4, within), selector); 62 63 bool result = false; 64 if(entities != NULL && !entities->empty() ) 65 { 66 std::sort(entities->begin(), entities->end(), *distComp); 67 target = weak_ptr<LivingEntity>(dynamic_pointer_cast<LivingEntity>(entities->at(0))); 68 result = true; 69 } 70 71 delete entities; 72 return result; 73} 74 75void NearestAttackableTargetGoal::start() 76{ 77 mob->setTarget(target.lock()); 78 TargetGoal::start(); 79}