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.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.h"
6#include "net.minecraft.world.entity.monster.h"
7#include "net.minecraft.world.phys.h"
8#include "RangedAttackGoal.h"
9
10void RangedAttackGoal::_init(RangedAttackMob *rangedMob, Mob *mob, double speedModifier, int attackIntervalMin, int attackIntervalMax, float attackRadius)
11{
12 //if (!(mob instanceof LivingEntity))
13 //{
14 //throw new IllegalArgumentException("ArrowAttackGoal requires Mob implements RangedAttackMob");
15 //}
16 rangedAttackMob = rangedMob;
17 this->mob = mob;
18 this->speedModifier = speedModifier;
19 this->attackIntervalMin = attackIntervalMin;
20 this->attackIntervalMax = attackIntervalMax;
21 this->attackRadius = attackRadius;
22 attackRadiusSqr = attackRadius * attackRadius;
23 setRequiredControlFlags(Control::MoveControlFlag | Control::LookControlFlag);
24
25 target = weak_ptr<LivingEntity>();
26 attackTime = -1;
27 seeTime = 0;
28}
29
30RangedAttackGoal::RangedAttackGoal(RangedAttackMob *rangedMob, Mob *mob, double speedModifier, int attackInterval, float attackRadius)
31{
32 _init(rangedMob, mob, speedModifier, attackInterval, attackInterval, attackRadius);
33}
34
35RangedAttackGoal::RangedAttackGoal(RangedAttackMob *rangedMob, Mob *mob, double speedModifier, int attackIntervalMin, int attackIntervalMax, float attackRadius)
36{
37 _init(rangedMob, mob, speedModifier, attackIntervalMin, attackIntervalMax, attackRadius);
38}
39
40bool RangedAttackGoal::canUse()
41{
42 shared_ptr<LivingEntity> bestTarget = mob->getTarget();
43 if (bestTarget == NULL) return false;
44 target = weak_ptr<LivingEntity>(bestTarget);
45 return true;
46}
47
48bool RangedAttackGoal::canContinueToUse()
49{
50 return canUse() || !mob->getNavigation()->isDone();
51}
52
53void RangedAttackGoal::stop()
54{
55 target = weak_ptr<LivingEntity>();
56 seeTime = 0;
57 attackTime = -1;
58}
59
60void RangedAttackGoal::tick()
61{
62 // 4J: It's possible the target has gone since canUse selected it, don't do tick if target is null
63 if (target.lock() == NULL) return;
64
65 double targetDistSqr = mob->distanceToSqr(target.lock()->x, target.lock()->bb->y0, target.lock()->z);
66 bool canSee = mob->getSensing()->canSee(target.lock());
67
68 if (canSee)
69 {
70 seeTime++;
71 }
72 else
73 {
74 seeTime = 0;
75 }
76
77 if (targetDistSqr > attackRadiusSqr || seeTime < 20)
78 {
79 mob->getNavigation()->moveTo(target.lock(), speedModifier);
80 }
81 else
82 {
83 mob->getNavigation()->stop();
84 }
85
86 mob->getLookControl()->setLookAt(target.lock(), 30, 30);
87
88 if (--attackTime == 0)
89 {
90 if (targetDistSqr > attackRadiusSqr || !canSee) return;
91
92 float dist = Mth::sqrt(targetDistSqr) / attackRadius;
93 float power = dist;
94 if (power < 0.1f) power = 0.1f;
95 if (power > 1) power = 1;
96
97 rangedAttackMob->performRangedAttack(target.lock(), power);
98 attackTime = Mth::floor(dist * (attackIntervalMax - attackIntervalMin) + attackIntervalMin);
99 }
100 else if (attackTime < 0)
101 {
102 float dist = Mth::sqrt(targetDistSqr) / attackRadius;
103 attackTime = Mth::floor(dist * (attackIntervalMax - attackIntervalMin) + attackIntervalMin);
104 }
105}