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.goal.h"
5#include "net.minecraft.world.entity.animal.h"
6#include "net.minecraft.world.level.h"
7#include "net.minecraft.world.level.tile.h"
8#include "net.minecraft.world.level.tile.entity.h"
9#include "BasicTypeContainers.h"
10#include "Arrays.h"
11#include "OcelotSitOnTileGoal.h"
12
13const int OcelotSitOnTileGoal::GIVE_UP_TICKS = 3 * SharedConstants::TICKS_PER_SECOND;
14const int OcelotSitOnTileGoal::SIT_TICKS = 60 * SharedConstants::TICKS_PER_SECOND;
15const int OcelotSitOnTileGoal::SEARCH_RANGE = 8;
16const double OcelotSitOnTileGoal::SIT_CHANCE = 0.0065f;
17
18OcelotSitOnTileGoal::OcelotSitOnTileGoal(Ocelot *ocelot, double speedModifier)
19{
20 _tick = 0;
21 tryTicks = 0;
22 maxTicks = 0;
23 tileX = 0;
24 tileY = 0;
25 tileZ = 0;
26
27 this->ocelot = ocelot;
28 this->speedModifier = speedModifier;
29 setRequiredControlFlags(Control::MoveControlFlag | Control::JumpControlFlag);
30}
31
32bool OcelotSitOnTileGoal::canUse()
33{
34 return ocelot->isTame() && !ocelot->isSitting() && ocelot->getRandom()->nextDouble() <= SIT_CHANCE && findNearestTile();
35}
36
37bool OcelotSitOnTileGoal::canContinueToUse()
38{
39 return _tick <= maxTicks && tryTicks <= GIVE_UP_TICKS && isValidTarget(ocelot->level, tileX, tileY, tileZ);
40}
41
42void OcelotSitOnTileGoal::start()
43{
44 ocelot->getNavigation()->moveTo((float) tileX + 0.5, tileY + 1, (float) tileZ + 0.5, speedModifier);
45 _tick = 0;
46 tryTicks = 0;
47 maxTicks = ocelot->getRandom()->nextInt(ocelot->getRandom()->nextInt(SIT_TICKS) + SIT_TICKS) + SIT_TICKS;
48 ocelot->getSitGoal()->wantToSit(false);
49
50 ocelot->setSittingOnTile(true); // 4J-Added.
51}
52
53void OcelotSitOnTileGoal::stop()
54{
55 ocelot->setSitting(false);
56
57 ocelot->setSittingOnTile(false); // 4J-Added.
58}
59
60void OcelotSitOnTileGoal::tick()
61{
62 _tick++;
63 ocelot->getSitGoal()->wantToSit(false);
64 if (ocelot->distanceToSqr(tileX, tileY + 1, tileZ) > 1)
65 {
66 ocelot->setSitting(false);
67 ocelot->getNavigation()->moveTo((float) tileX + 0.5, tileY + 1, (float) tileZ + 0.5, speedModifier);
68 tryTicks++;
69 }
70 else if (!ocelot->isSitting())
71 {
72 ocelot->setSitting(true);
73 }
74 else
75 {
76 tryTicks--;
77 }
78}
79
80bool OcelotSitOnTileGoal::findNearestTile()
81{
82 int y = (int) ocelot->y;
83 double distSqr = Integer::MAX_VALUE;
84
85 for (int x = (int) ocelot->x - SEARCH_RANGE; x < ocelot->x + SEARCH_RANGE; x++)
86 {
87 for (int z = (int) ocelot->z - SEARCH_RANGE; z < ocelot->z + SEARCH_RANGE; z++)
88 {
89 if (isValidTarget(ocelot->level, x, y, z) && ocelot->level->isEmptyTile(x, y + 1, z))
90 {
91 double dist = ocelot->distanceToSqr(x, y, z);
92
93 if (dist < distSqr)
94 {
95 tileX = x;
96 tileY = y;
97 tileZ = z;
98 distSqr = dist;
99 }
100 }
101 }
102 }
103
104 return distSqr < Integer::MAX_VALUE;
105}
106
107bool OcelotSitOnTileGoal::isValidTarget(Level *level, int x, int y, int z)
108{
109 int tile = level->getTile(x, y, z);
110 int data = level->getData(x, y, z);
111
112 if (tile == Tile::chest_Id)
113 {
114 shared_ptr<ChestTileEntity> chest = dynamic_pointer_cast<ChestTileEntity>(level->getTileEntity(x, y, z));
115
116 if (chest->openCount < 1)
117 {
118 return true;
119 }
120 }
121 else if (tile == Tile::furnace_lit_Id)
122 {
123 return true;
124 }
125 else if (tile == Tile::bed_Id && !BedTile::isHeadPiece(data))
126 {
127 return true;
128 }
129
130 return false;
131}