the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 127 lines 3.7 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.util.h" 5#include "net.minecraft.world.entity.ai.village.h" 6#include "net.minecraft.world.entity.h" 7#include "net.minecraft.world.level.h" 8#include "BasicTypeContainers.h" 9#include "MoveThroughVillageGoal.h" 10#include "Path.h" 11 12MoveThroughVillageGoal::MoveThroughVillageGoal(PathfinderMob *mob, double speedModifier, bool onlyAtNight) 13{ 14 path = NULL; 15 doorInfo = weak_ptr<DoorInfo>(); 16 17 this->mob = mob; 18 this->speedModifier = speedModifier; 19 this->onlyAtNight = onlyAtNight; 20 setRequiredControlFlags(Control::MoveControlFlag); 21} 22 23MoveThroughVillageGoal::~MoveThroughVillageGoal() 24{ 25 if(path != NULL) delete path; 26} 27 28bool MoveThroughVillageGoal::canUse() 29{ 30 updateVisited(); 31 32 if (onlyAtNight && mob->level->isDay()) return false; 33 34 shared_ptr<Village> village = mob->level->villages->getClosestVillage(Mth::floor(mob->x), Mth::floor(mob->y), Mth::floor(mob->z), 0); 35 if (village == NULL) return false; 36 37 shared_ptr<DoorInfo> _doorInfo = getNextDoorInfo(village); 38 if (_doorInfo == NULL) return false; 39 doorInfo = _doorInfo; 40 41 bool oldCanOpenDoors = mob->getNavigation()->canOpenDoors(); 42 mob->getNavigation()->setCanOpenDoors(false); 43 delete path; 44 45 path = mob->getNavigation()->createPath(_doorInfo->x, _doorInfo->y, _doorInfo->z); 46 mob->getNavigation()->setCanOpenDoors(oldCanOpenDoors); 47 if (path != NULL) return true; 48 49 Vec3 *pos = RandomPos::getPosTowards(dynamic_pointer_cast<PathfinderMob>(mob->shared_from_this()), 10, 7, Vec3::newTemp(_doorInfo->x, _doorInfo->y, _doorInfo->z)); 50 if (pos == NULL) return false; 51 mob->getNavigation()->setCanOpenDoors(false); 52 delete path; 53 path = mob->getNavigation()->createPath(pos->x, pos->y, pos->z); 54 mob->getNavigation()->setCanOpenDoors(oldCanOpenDoors); 55 return path != NULL; 56} 57 58bool MoveThroughVillageGoal::canContinueToUse() 59{ 60 if (mob->getNavigation()->isDone()) return false; 61 float dist = mob->bbWidth + 4.f; 62 shared_ptr<DoorInfo> _doorInfo = doorInfo.lock(); 63 if( _doorInfo == NULL ) return false; 64 65 return mob->distanceToSqr(_doorInfo->x, _doorInfo->y, _doorInfo->z) > dist * dist; 66} 67 68void MoveThroughVillageGoal::start() 69{ 70 mob->getNavigation()->moveTo(path, speedModifier); 71 path = NULL; 72} 73 74void MoveThroughVillageGoal::stop() 75{ 76 shared_ptr<DoorInfo> _doorInfo = doorInfo.lock(); 77 if( _doorInfo == NULL ) return; 78 79 if (mob->getNavigation()->isDone() || mob->distanceToSqr(_doorInfo->x, _doorInfo->y, _doorInfo->z) < 4 * 4) 80 { 81 visited.push_back(doorInfo); 82 } 83} 84 85shared_ptr<DoorInfo> MoveThroughVillageGoal::getNextDoorInfo(shared_ptr<Village> village) 86{ 87 shared_ptr<DoorInfo> closest = nullptr; 88 int closestDistSqr = Integer::MAX_VALUE; 89 vector<shared_ptr<DoorInfo> > *doorInfos = village->getDoorInfos(); 90 //for (DoorInfo di : doorInfos) 91 for(AUTO_VAR(it, doorInfos->begin()); it != doorInfos->end(); ++it) 92 { 93 shared_ptr<DoorInfo> di = *it; 94 int distSqr = di->distanceToSqr(Mth::floor(mob->x), Mth::floor(mob->y), Mth::floor(mob->z)); 95 if (distSqr < closestDistSqr) 96 { 97 if (hasVisited(di)) continue; 98 closest = di; 99 closestDistSqr = distSqr; 100 } 101 } 102 return closest; 103} 104 105bool MoveThroughVillageGoal::hasVisited(shared_ptr<DoorInfo>di) 106{ 107 //for (DoorInfo di2 : visited) 108 for(AUTO_VAR(it, visited.begin()); it != visited.end(); ) 109 { 110 shared_ptr<DoorInfo> di2 = (*it).lock(); 111 if( di2 == NULL ) 112 { 113 it = visited.erase(it); 114 } 115 else 116 { 117 if (di->x == di2->x && di->y == di2->y && di->z == di2->z) return true; 118 ++it; 119 } 120 } 121 return false; 122} 123 124void MoveThroughVillageGoal::updateVisited() 125{ 126 if (visited.size() > 15) visited.erase(visited.begin()); 127}