the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 96 lines 3.0 kB view raw
1#include "stdafx.h" 2#include "net.minecraft.world.entity.ai.control.h" 3#include "net.minecraft.world.entity.ai.navigation.h" 4#include "net.minecraft.world.entity.ai.sensing.h" 5#include "net.minecraft.world.entity.ai.util.h" 6#include "net.minecraft.world.entity.h" 7#include "net.minecraft.world.entity.animal.h" 8#include "net.minecraft.world.level.h" 9#include "net.minecraft.world.level.pathfinder.h" 10#include "net.minecraft.world.phys.h" 11#include "AvoidPlayerGoal.h" 12 13AvoidPlayerGoalEntitySelector::AvoidPlayerGoalEntitySelector(AvoidPlayerGoal *parent) 14{ 15 m_parent = parent; 16} 17 18bool AvoidPlayerGoalEntitySelector::matches(shared_ptr<Entity> entity) const 19{ 20 return entity->isAlive() && m_parent->mob->getSensing()->canSee(entity); 21} 22 23AvoidPlayerGoal::AvoidPlayerGoal(PathfinderMob *mob, const type_info& avoidType, float maxDist, double walkSpeedModifier, double sprintSpeedModifier) : avoidType(avoidType) 24{ 25 this->mob = mob; 26 //this->avoidType = avoidType; 27 this->maxDist = maxDist; 28 this->walkSpeedModifier = walkSpeedModifier; 29 this->sprintSpeedModifier = sprintSpeedModifier; 30 this->pathNav = mob->getNavigation(); 31 setRequiredControlFlags(Control::MoveControlFlag); 32 33 entitySelector = new AvoidPlayerGoalEntitySelector(this); 34 35 toAvoid = weak_ptr<Entity>(); 36 path = NULL; 37} 38 39AvoidPlayerGoal::~AvoidPlayerGoal() 40{ 41 if(path != NULL) delete path; 42 delete entitySelector; 43} 44 45bool AvoidPlayerGoal::canUse() 46{ 47 if (avoidType == typeid(Player)) 48 { 49 shared_ptr<TamableAnimal> tamableAnimal = dynamic_pointer_cast<TamableAnimal>(mob->shared_from_this()); 50 if (tamableAnimal != NULL && tamableAnimal->isTame()) return false; 51 toAvoid = weak_ptr<Entity>(mob->level->getNearestPlayer(mob->shared_from_this(), maxDist)); 52 if (toAvoid.lock() == NULL) return false; 53 } 54 else 55 { 56 vector<shared_ptr<Entity> > *entities = mob->level->getEntitiesOfClass(avoidType, mob->bb->grow(maxDist, 3, maxDist), entitySelector); 57 if (entities->empty()) 58 { 59 delete entities; 60 return false; 61 } 62 toAvoid = weak_ptr<Entity>(entities->at(0)); 63 delete entities; 64 } 65 66 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)); 67 if (pos == NULL) return false; 68 if (toAvoid.lock()->distanceToSqr(pos->x, pos->y, pos->z) < toAvoid.lock()->distanceToSqr(mob->shared_from_this())) return false; 69 delete path; 70 path = pathNav->createPath(pos->x, pos->y, pos->z); 71 if (path == NULL) return false; 72 if (!path->endsInXZ(pos)) return false; 73 return true; 74} 75 76bool AvoidPlayerGoal::canContinueToUse() 77{ 78 return toAvoid.lock() != NULL && !pathNav->isDone(); 79} 80 81void AvoidPlayerGoal::start() 82{ 83 pathNav->moveTo(path, walkSpeedModifier); 84 path = NULL; 85} 86 87void AvoidPlayerGoal::stop() 88{ 89 toAvoid = weak_ptr<Entity>(); 90} 91 92void AvoidPlayerGoal::tick() 93{ 94 if (mob->distanceToSqr(toAvoid.lock()) < 7 * 7) mob->getNavigation()->setSpeedModifier(sprintSpeedModifier); 95 else mob->getNavigation()->setSpeedModifier(walkSpeedModifier); 96}