the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 131 lines 3.5 kB view raw
1#include "stdafx.h" 2#include "net.minecraft.world.entity.h" 3#include "net.minecraft.world.entity.ai.attributes.h" 4#include "net.minecraft.world.entity.ai.navigation.h" 5#include "net.minecraft.world.entity.ai.sensing.h" 6#include "net.minecraft.world.entity.animal.h" 7#include "net.minecraft.world.entity.monster.h" 8#include "net.minecraft.world.entity.player.h" 9#include "net.minecraft.world.level.pathfinder.h" 10#include "net.minecraft.world.phys.h" 11#include "TargetGoal.h" 12 13void TargetGoal::_init(PathfinderMob *mob, bool mustSee, bool mustReach) 14{ 15 reachCache = EmptyReachCache; 16 reachCacheTime = 0; 17 unseenTicks = 0; 18 19 this->mob = mob; 20 this->mustSee = mustSee; 21 this->mustReach = mustReach; 22} 23 24TargetGoal::TargetGoal(PathfinderMob *mob, bool mustSee) 25{ 26 _init(mob, mustSee, false); 27} 28 29TargetGoal::TargetGoal(PathfinderMob *mob, bool mustSee, bool mustReach) 30{ 31 _init(mob,mustSee,mustReach); 32} 33 34bool TargetGoal::canContinueToUse() 35{ 36 shared_ptr<LivingEntity> target = mob->getTarget(); 37 if (target == NULL) return false; 38 if (!target->isAlive()) return false; 39 40 double within = getFollowDistance(); 41 if (mob->distanceToSqr(target) > within * within) return false; 42 if (mustSee) 43 { 44 if (mob->getSensing()->canSee(target)) 45 { 46 unseenTicks = 0; 47 } 48 else 49 { 50 if (++unseenTicks > UnseenMemoryTicks) return false; 51 } 52 } 53 return true; 54} 55 56double TargetGoal::getFollowDistance() 57{ 58 AttributeInstance *followRange = mob->getAttribute(SharedMonsterAttributes::FOLLOW_RANGE); 59 return followRange == NULL ? 16 : followRange->getValue(); 60} 61 62void TargetGoal::start() 63{ 64 reachCache = EmptyReachCache; 65 reachCacheTime = 0; 66 unseenTicks = 0; 67} 68 69void TargetGoal::stop() 70{ 71 mob->setTarget(nullptr); 72} 73 74bool TargetGoal::canAttack(shared_ptr<LivingEntity> target, bool allowInvulnerable) 75{ 76 if (target == NULL) return false; 77 if (target == mob->shared_from_this()) return false; 78 if (!target->isAlive()) return false; 79 if (!mob->canAttackType(target->GetType())) return false; 80 81 OwnableEntity *ownableMob = dynamic_cast<OwnableEntity *>(mob); 82 if (ownableMob != NULL && !ownableMob->getOwnerUUID().empty()) 83 { 84 shared_ptr<OwnableEntity> ownableTarget = dynamic_pointer_cast<OwnableEntity>(target); 85 if (ownableTarget != NULL && ownableMob->getOwnerUUID().compare(ownableTarget->getOwnerUUID()) == 0) 86 { 87 // We're attacking something owned by the same person... 88 return false; 89 } 90 91 if (target == ownableMob->getOwner()) 92 { 93 // We're attacking our owner 94 return false; 95 } 96 } 97 else if (target->instanceof(eTYPE_PLAYER)) 98 { 99 if (!allowInvulnerable && (dynamic_pointer_cast<Player>(target))->abilities.invulnerable) return false; 100 } 101 102 if (!mob->isWithinRestriction(Mth::floor(target->x), Mth::floor(target->y), Mth::floor(target->z))) return false; 103 104 if (mustSee && !mob->getSensing()->canSee(target)) return false; 105 106 if (mustReach) 107 { 108 if (--reachCacheTime <= 0) reachCache = EmptyReachCache; 109 if (reachCache == EmptyReachCache) reachCache = canReach(target) ? CanReachCache : CantReachCache; 110 if (reachCache == CantReachCache) return false; 111 } 112 113 return true; 114} 115 116bool TargetGoal::canReach(shared_ptr<LivingEntity> target) 117{ 118 reachCacheTime = 10 + mob->getRandom()->nextInt(5); 119 Path *path = mob->getNavigation()->createPath(target); 120 if (path == NULL) return false; 121 Node *last = path->last(); 122 if (last == NULL) 123 { 124 delete path; 125 return false; 126 } 127 int xx = last->x - Mth::floor(target->x); 128 int zz = last->z - Mth::floor(target->z); 129 delete path; 130 return xx * xx + zz * zz <= 1.5 * 1.5; 131}