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.phys.h"
4#include "RandomPos.h"
5
6Vec3 *RandomPos::tempDir = Vec3::newPermanent(0, 0, 0);
7
8Vec3 *RandomPos::getPos(shared_ptr<PathfinderMob> mob, int xzDist, int yDist, int quadrant/*=-1*/) // 4J - added quadrant
9{
10 return generateRandomPos(mob, xzDist, yDist, NULL, quadrant);
11}
12
13Vec3 *RandomPos::getPosTowards(shared_ptr<PathfinderMob> mob, int xzDist, int yDist, Vec3 *towardsPos)
14{
15 tempDir->x = towardsPos->x - mob->x;
16 tempDir->y = towardsPos->y - mob->y;
17 tempDir->z = towardsPos->z - mob->z;
18 return generateRandomPos(mob, xzDist, yDist, tempDir);
19}
20
21Vec3 *RandomPos::getPosAvoid(shared_ptr<PathfinderMob> mob, int xzDist, int yDist, Vec3 *avoidPos)
22{
23 tempDir->x = mob->x - avoidPos->x;
24 tempDir->y = mob->y - avoidPos->y;
25 tempDir->z = mob->z - avoidPos->z;
26 return generateRandomPos(mob, xzDist, yDist, tempDir);
27}
28
29Vec3 *RandomPos::generateRandomPos(shared_ptr<PathfinderMob> mob, int xzDist, int yDist, Vec3 *dir, int quadrant/*=-1*/) // 4J - added quadrant
30{
31 Random *random = mob->getRandom();
32 bool hasBest = false;
33 int xBest = 0, yBest = 0, zBest = 0;
34 float best = -99999;
35
36 // 4J Stu - restrict is a reserved keyword
37 bool bRestrict;
38 if (mob->hasRestriction())
39 {
40 double restDist = mob->getRestrictCenter()->distSqr(Mth::floor(mob->x), Mth::floor(mob->y), Mth::floor(mob->z)) + 4;
41 double radius = mob->getRestrictRadius() + xzDist;
42 bRestrict = restDist < radius * radius;
43 }
44 else bRestrict = false;
45
46 for (int i = 0; i < 10; i++)
47 {
48 int xt, yt, zt;
49 // 4J - added quadrant here so that we can choose to select positions only within the one quadrant. Passing a parameter of -1 will
50 // lead to normal java behaviour
51 if( quadrant == -1 )
52 {
53 xt = random->nextInt(2 * xzDist) - xzDist;
54 zt = random->nextInt(2 * xzDist) - xzDist;
55 }
56 else
57 {
58 int sx = ( ( quadrant & 1 ) ? -1 : 1 );
59 int sz = ( ( quadrant & 2 ) ? -1 : 1 );
60 xt = random->nextInt(xzDist) * sx;
61 zt = random->nextInt(xzDist) * sz;
62 }
63 yt = random->nextInt(2 * yDist) - yDist;
64
65 if (dir != NULL && xt * dir->x + zt * dir->z < 0) continue;
66
67 xt += Mth::floor(mob->x);
68 yt += Mth::floor(mob->y);
69 zt += Mth::floor(mob->z);
70
71 if (bRestrict && !mob->isWithinRestriction(xt, yt, zt)) continue;
72 float value = mob->getWalkTargetValue(xt, yt, zt);
73 if (value > best)
74 {
75 best = value;
76 xBest = xt;
77 yBest = yt;
78 zBest = zt;
79 hasBest = true;
80 }
81 }
82 if (hasBest)
83 {
84 return Vec3::newTemp(xBest, yBest, zBest);
85 }
86
87 return NULL;
88}