RandomPositionGenerator .java

net.minecraft.entity.ai.RandomPositionGenerator (class, 130 lines): members: findRandomTargetBlockAwayFrom, findRandomTargetBlockTowards, findRandomTarget.

net.minecraft.entity.aiclass130 linescommon4.6 KB

API Surface (3)

Vec3 findRandomTarget(EntityCreature entitycreatureIn, int xz, int y)
Vec3 findRandomTargetBlockTowards(EntityCreature entitycreatureIn, int xz, int y, Vec3 targetVec3)
Vec3 findRandomTargetBlockAwayFrom(EntityCreature entitycreatureIn, int xz, int y, Vec3 targetVec3)

Fields & Constants (1)

staticVector

Code Graph: Dependencies (4)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

RandomPositionGenerator.java130 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.entity.ai;
2
3
import java.util.Random;
4
import net.minecraft.entity.EntityCreature;
5
import net.minecraft.util.BlockPos;
6
import net.minecraft.util.MathHelper;
7
import net.minecraft.util.Vec3;
8
9
public class RandomPositionGenerator
10
{
11
    /**
12
     * used to store a driection when the user passes a point to move towards or away from. WARNING: NEVER THREAD SAFE.
13
     * MULTIPLE findTowards and findAway calls, will share this var
14
     */
15
    private static Vec3 staticVector = new Vec3(0.0D, 0.0D, 0.0D);
16
17
    /**
18
     * finds a random target within par1(x,z) and par2 (y) blocks
19
     */
20
    public static Vec3 findRandomTarget(EntityCreature entitycreatureIn, int xz, int y)
21
    {
22
        return findRandomTargetBlock(entitycreatureIn, xz, y, (Vec3)null);
23
    }
24
25
    /**
26
     * finds a random target within par1(x,z) and par2 (y) blocks in the direction of the point par3
27
     */
28
    public static Vec3 findRandomTargetBlockTowards(EntityCreature entitycreatureIn, int xz, int y, Vec3 targetVec3)
29
    {
30
        staticVector = targetVec3.subtract(entitycreatureIn.posX, entitycreatureIn.posY, entitycreatureIn.posZ);
31
        return findRandomTargetBlock(entitycreatureIn, xz, y, staticVector);
32
    }
33
34
    /**
35
     * finds a random target within par1(x,z) and par2 (y) blocks in the reverse direction of the point par3
36
     */
37
    public static Vec3 findRandomTargetBlockAwayFrom(EntityCreature entitycreatureIn, int xz, int y, Vec3 targetVec3)
38
    {
39
        staticVector = (new Vec3(entitycreatureIn.posX, entitycreatureIn.posY, entitycreatureIn.posZ)).subtract(targetVec3);
40
        return findRandomTargetBlock(entitycreatureIn, xz, y, staticVector);
41
    }
42
43
    /**
44
     * searches 10 blocks at random in a within par1(x,z) and par2 (y) distance, ignores those not in the direction of
45
     * par3Vec3, then points to the tile for which creature.getBlockPathWeight returns the highest number
46
     */
47
    private static Vec3 findRandomTargetBlock(EntityCreature entitycreatureIn, int xz, int y, Vec3 targetVec3)
48
    {
49
        Random random = entitycreatureIn.getRNG();
50
        boolean flag = false;
51
        int i = 0;
52
        int j = 0;
53
        int k = 0;
54
        float f = -99999.0F;
55
        boolean flag1;
56
57
        if (entitycreatureIn.hasHome())
58
        {
59
            double d0 = entitycreatureIn.getHomePosition().distanceSq((double)MathHelper.floor_double(entitycreatureIn.posX), (double)MathHelper.floor_double(entitycreatureIn.posY), (double)MathHelper.floor_double(entitycreatureIn.posZ)) + 4.0D;
60
            double d1 = (double)(entitycreatureIn.getMaximumHomeDistance() + (float)xz);
61
            flag1 = d0 < d1 * d1;
62
        }
63
        else
64
        {
65
            flag1 = false;
66
        }
67
68
        for (int j1 = 0; j1 < 10; ++j1)
69
        {
70
            int l = random.nextInt(2 * xz + 1) - xz;
71
            int k1 = random.nextInt(2 * y + 1) - y;
72
            int i1 = random.nextInt(2 * xz + 1) - xz;
73
74
            if (targetVec3 == null || (double)l * targetVec3.xCoord + (double)i1 * targetVec3.zCoord >= 0.0D)
75
            {
76
                if (entitycreatureIn.hasHome() && xz > 1)
77
                {
78
                    BlockPos blockpos = entitycreatureIn.getHomePosition();
79
80
                    if (entitycreatureIn.posX > (double)blockpos.getX())
81
                    {
82
                        l -= random.nextInt(xz / 2);
83
                    }
84
                    else
85
                    {
86
                        l += random.nextInt(xz / 2);
87
                    }
88
89
                    if (entitycreatureIn.posZ > (double)blockpos.getZ())
90
                    {
91
                        i1 -= random.nextInt(xz / 2);
92
                    }
93
                    else
94
                    {
95
                        i1 += random.nextInt(xz / 2);
96
                    }
97
                }
98
99
                l = l + MathHelper.floor_double(entitycreatureIn.posX);
100
                k1 = k1 + MathHelper.floor_double(entitycreatureIn.posY);
101
                i1 = i1 + MathHelper.floor_double(entitycreatureIn.posZ);
102
                BlockPos blockpos1 = new BlockPos(l, k1, i1);
103
104
                if (!flag1 || entitycreatureIn.isWithinHomeDistanceFromPosition(blockpos1))
105
                {
106
                    float f1 = entitycreatureIn.getBlockPathWeight(blockpos1);
107
108
                    if (f1 > f)
109
                    {
110
                        f = f1;
111
                        i = l;
112
                        j = k1;
113
                        k = i1;
114
                        flag = true;
115
                    }
116
                }
117
            }
118
        }
119
120
        if (flag)
121
        {
122
            return new Vec3((double)i, (double)j, (double)k);
123
        }
124
        else
125
        {
126
            return null;
127
        }
128
    }
129
}
130

Deobfuscated MCP 1.8.9 source of RandomPositionGenerator from Marcelektro/MavenMCP-1.8.9 (net/minecraft/entity/ai/RandomPositionGenerator.java), stored verbatim.