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.navigation.h"
3#include "net.minecraft.world.entity.h"
4#include "net.minecraft.world.phys.h"
5#include "LookControl.h"
6
7LookControl::LookControl(Mob *mob)
8{
9 yMax = xMax = 0.0f;
10 hasWanted = false;
11 wantedX = wantedY = wantedZ = 0.0;
12
13 this->mob = mob;
14}
15
16void LookControl::setLookAt(shared_ptr<Entity> target, float yMax, float xMax)
17{
18 wantedX = target->x;
19 if ( target->instanceof(eTYPE_LIVINGENTITY) ) wantedY = target->y + dynamic_pointer_cast<LivingEntity>(target)->getHeadHeight();
20 else wantedY = (target->bb->y0 + target->bb->y1) / 2;
21 wantedZ = target->z;
22 this->yMax = yMax;
23 this->xMax = xMax;
24 hasWanted = true;
25}
26
27void LookControl::setLookAt(double x, double y, double z, float yMax, float xMax)
28{
29 wantedX = x;
30 wantedY = y;
31 wantedZ = z;
32 this->yMax = yMax;
33 this->xMax = xMax;
34 hasWanted = true;
35}
36
37void LookControl::tick()
38{
39 mob->xRot = 0;
40
41 if (hasWanted)
42 {
43 hasWanted = false;
44
45 double xd = wantedX - mob->x;
46 double yd = wantedY - (mob->y + mob->getHeadHeight());
47 double zd = wantedZ - mob->z;
48 double sd = sqrt(xd * xd + zd * zd);
49
50 float yRotD = (float) (atan2(zd, xd) * 180 / PI) - 90;
51 float xRotD = (float) -(atan2(yd, sd) * 180 / PI);
52 mob->xRot = rotlerp(mob->xRot, xRotD, xMax);
53 mob->yHeadRot = rotlerp(mob->yHeadRot, yRotD, yMax);
54 }
55 else
56 {
57 mob->yHeadRot = rotlerp(mob->yHeadRot, mob->yBodyRot, 10);
58 }
59
60 float headDiffBody = Mth::wrapDegrees(mob->yHeadRot - mob->yBodyRot);
61
62 if (!mob->getNavigation()->isDone())
63 {
64 // head clamped to body
65 if (headDiffBody < -75) mob->yHeadRot = mob->yBodyRot - 75;
66 if (headDiffBody > 75) mob->yHeadRot = mob->yBodyRot + 75;
67 }
68}
69
70float LookControl::rotlerp(float a, float b, float max)
71{
72 float diff = b - a;
73 while (diff < -180)
74 diff += 360;
75 while (diff >= 180)
76 diff -= 360;
77 if (diff > max)
78 {
79 diff = max;
80 }
81 if (diff < -max)
82 {
83 diff = -max;
84 }
85 return a + diff;
86}
87
88bool LookControl::isHasWanted()
89{
90 return hasWanted;
91}
92
93float LookControl::getYMax()
94{
95 return yMax;
96}
97
98float LookControl::getXMax()
99{
100 return xMax;
101}
102
103double LookControl::getWantedX()
104{
105 return wantedX;
106}
107
108double LookControl::getWantedY()
109{
110 return wantedY;
111}
112
113double LookControl::getWantedZ()
114{
115 return wantedZ;
116}