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.projectile.h"
6#include "net.minecraft.world.entity.h"
7#include "net.minecraft.world.level.h"
8#include "net.minecraft.world.phys.h"
9#include "SoundTypes.h"
10#include "ArrowAttackGoal.h"
11
12ArrowAttackGoal::ArrowAttackGoal(Mob *mob, float speed, int projectileType, int attackInterval)
13{
14 // 4J Init
15 target = weak_ptr<Mob>();
16 attackTime = 0;
17 seeTime = 0;
18
19 this->mob = mob;
20 this->level = mob->level;
21 this->speed = speed;
22 this->projectileType = projectileType;
23 this->attackInterval = attackInterval;
24 setRequiredControlFlags(Control::MoveControlFlag | Control::LookControlFlag);
25}
26
27bool ArrowAttackGoal::canUse()
28{
29 shared_ptr<Mob> bestTarget = mob->getTarget();
30 if (bestTarget == NULL) return false;
31 target = weak_ptr<Mob>(bestTarget);
32 return true;
33}
34
35bool ArrowAttackGoal::canContinueToUse()
36{
37 return target.lock() != NULL && (canUse() || !mob->getNavigation()->isDone());
38}
39
40void ArrowAttackGoal::stop()
41{
42 target = weak_ptr<Mob>();
43}
44
45void ArrowAttackGoal::tick()
46{
47 double attackRadiusSqr = 10 * 10;
48 shared_ptr<Mob> tar = target.lock();
49 double targetDistSqr = mob->distanceToSqr(tar->x, tar->bb->y0, tar->z);
50 bool canSee = mob->getSensing()->canSee(tar);
51
52 if (canSee)
53 {
54 ++seeTime;
55 }
56 else seeTime = 0;
57
58 if (targetDistSqr > attackRadiusSqr || seeTime < 20) mob->getNavigation()->moveTo(tar, speed);
59 else mob->getNavigation()->stop();
60
61 mob->getLookControl()->setLookAt(tar, 30, 30);
62
63 attackTime = max(attackTime - 1, 0);
64 if (attackTime > 0) return;
65 if (targetDistSqr > attackRadiusSqr || !canSee) return;
66 fireAtTarget();
67 attackTime = attackInterval;
68}
69
70void ArrowAttackGoal::fireAtTarget()
71{
72 shared_ptr<Mob> tar = target.lock();
73 if (projectileType == ArrowType)
74 {
75 shared_ptr<Arrow> arrow = shared_ptr<Arrow>( new Arrow(level, dynamic_pointer_cast<Mob>(mob->shared_from_this()), tar, 1.60f, 12) );
76 level->playSound(mob->shared_from_this(), eSoundType_RANDOM_BOW, 1.0f, 1 / (mob->getRandom()->nextFloat() * 0.4f + 0.8f));
77 level->addEntity(arrow);
78 }
79 else if (projectileType == SnowballType)
80 {
81 shared_ptr<Snowball> snowball = shared_ptr<Snowball>( new Snowball(level, dynamic_pointer_cast<Mob>(mob->shared_from_this())) );
82 double xd = tar->x - mob->x;
83 double yd = (tar->y + tar->getHeadHeight() - 1.1f) - snowball->y;
84 double zd = tar->z - mob->z;
85 float yo = sqrt(xd * xd + zd * zd) * 0.2f;
86 snowball->shoot(xd, yd + yo, zd, 1.60f, 12);
87
88 level->playSound(mob->shared_from_this(), eSoundType_RANDOM_BOW, 1.0f, 1 / (mob->getRandom()->nextFloat() * 0.4f + 0.8f));
89 level->addEntity(snowball);
90 }
91}