the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2#include "net.minecraft.world.entity.h"
3#include "net.minecraft.world.entity.monster.h"
4#include "net.minecraft.world.level.h"
5#include "net.minecraft.world.entity.ai.control.h"
6#include "net.minecraft.world.entity.ai.navigation.h"
7#include "net.minecraft.world.entity.ai.sensing.h"
8#include "net.minecraft.world.phys.h"
9#include "MeleeAttackGoal.h"
10
11void MeleeAttackGoal::_init(PathfinderMob *mob, double speedModifier, bool trackTarget)
12{
13 this->attackType = eTYPE_NOTSET;
14 this->mob = mob;
15 level = mob->level;
16 this->speedModifier = speedModifier;
17 this->trackTarget = trackTarget;
18 setRequiredControlFlags(Control::MoveControlFlag | Control::LookControlFlag);
19
20
21 attackTime = 0;
22 path = NULL;
23 timeToRecalcPath = 0;
24}
25
26MeleeAttackGoal::MeleeAttackGoal(PathfinderMob *mob, eINSTANCEOF attackType, double speedModifier, bool trackTarget)
27{
28 _init(mob, speedModifier, trackTarget);
29 this->attackType = attackType;
30}
31
32MeleeAttackGoal::MeleeAttackGoal(PathfinderMob *mob, double speedModifier, bool trackTarget)
33{
34 _init(mob,speedModifier,trackTarget);
35}
36
37MeleeAttackGoal::~MeleeAttackGoal()
38{
39 if(path != NULL) delete path;
40}
41
42bool MeleeAttackGoal::canUse()
43{
44 shared_ptr<LivingEntity> target = mob->getTarget();
45 if (target == NULL) return false;
46 if (!target->isAlive()) return false;
47 if (attackType != NULL && !target->instanceof(attackType)) return false;
48 delete path;
49 path = mob->getNavigation()->createPath(target);
50 return path != NULL;
51}
52
53bool MeleeAttackGoal::canContinueToUse()
54{
55 shared_ptr<LivingEntity> target = mob->getTarget();
56 if (target == NULL) return false;
57 if (!target->isAlive()) return false;
58 if (!trackTarget) return !mob->getNavigation()->isDone();
59 if (!mob->isWithinRestriction(Mth::floor(target->x), Mth::floor(target->y), Mth::floor(target->z))) return false;
60 return true;
61}
62
63void MeleeAttackGoal::start()
64{
65 mob->getNavigation()->moveTo(path, speedModifier);
66 path = NULL;
67 timeToRecalcPath = 0;
68}
69
70void MeleeAttackGoal::stop()
71{
72 mob->getNavigation()->stop();
73}
74
75void MeleeAttackGoal::tick()
76{
77 shared_ptr<LivingEntity> target = mob->getTarget();
78 mob->getLookControl()->setLookAt(target, 30, 30);
79 if (trackTarget || mob->getSensing()->canSee(target))
80 {
81 if (--timeToRecalcPath <= 0)
82 {
83 timeToRecalcPath = 4 + mob->getRandom()->nextInt(7);
84 mob->getNavigation()->moveTo(target, speedModifier);
85 }
86 }
87
88 attackTime = max(attackTime - 1, 0);
89
90 double meleeRadiusSqr = (mob->bbWidth * 2) * (mob->bbWidth * 2) + target->bbWidth;
91 if (mob->distanceToSqr(target->x, target->bb->y0, target->z) > meleeRadiusSqr) return;
92 if (attackTime > 0) return;
93 attackTime = 20;
94 if (mob->getCarriedItem() != NULL) mob->swing();
95 mob->doHurtTarget(target);
96}