-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAIWorld.java
More file actions
313 lines (279 loc) · 10.9 KB
/
Copy pathAIWorld.java
File metadata and controls
313 lines (279 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package com.custommods.ai;
import java.util.ArrayList;
import java.util.Queue;
import java.util.Random;
import com.custommods.walkmod.IWorldInfo;
import com.custommods.walkmod.MinecraftWorldInfo;
import com.custommods.walkmod.NeighborCollector;
import com.custommods.walkmod.PathFinder;
import com.custommods.walkmod.PathSmoother;
import com.custommods.walkmod.Step;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.stats.StatBase;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
import net.minecraft.world.storage.WorldInfo;
public class AIWorld{
private World world;
private IWorldInfo worldInfo;
///Constructor
public AIWorld(World world){
this.world = world;
MinecraftWorldInfo minecraftWorldInfo = MinecraftWorldInfo.getInstance();
minecraftWorldInfo.init();
worldInfo = new NeighborCollector(minecraftWorldInfo);
}
///Return the Minimal Distance for two point in the world
public double getMinimalDistance(Vec3 pos, Vec3 goal) {
return worldInfo.getMinimalDistance(pos, goal);
}
///Return the nearest block by name
public Vec3 findNearestBlock(Vec3 startLoc, String Blockname, int max){
return findNearestBlock(startLoc, Block.getIdFromBlock(Block.getBlockFromName(Blockname)), max);
}
///Return the nearest block by block
public Vec3 findNearestBlock(Vec3 startLoc, Block block, int max){
return findNearestBlock(startLoc, Block.getIdFromBlock(block), max);
}
///Return the nearest block by ID
public Vec3 findNearestBlock(Vec3 startLoc, int blockId, int max){
int x = (int) startLoc.xCoord;
int y = (int) startLoc.yCoord;
int z = (int) startLoc.zCoord;
for (int i = 0 ; i < max ; i++){
for (int j = -i ; j < i + 1 ; j++){
for (int k = -i ; k < i + 1 ; k++){
if (dropBlockId(x + i, y + k, z + j) == blockId){
return Vec3.createVectorHelper(x + i, y + k, z + j);
}
if (dropBlockId(x - i, y + k, z + j) == blockId){
return Vec3.createVectorHelper(x - i, y + k, z + j);
}
if (dropBlockId(x + k, y + i, z + j) == blockId){
return Vec3.createVectorHelper(x + k, y + i, z + j);
}
if (dropBlockId(x + k, y - i, z + j) == blockId){
return Vec3.createVectorHelper(x + k, y - i, z + j);
}
if (dropBlockId(x + k, y + j, z + i) == blockId){
return Vec3.createVectorHelper(x + k, y + j, z + i);
}
if (dropBlockId(x + k, y + j, z - i) == blockId){
return Vec3.createVectorHelper(x + k, y + j, z - i);
}
}
}
}
return null;
}
///Return the nearest block by array of ids
public Vec3 findNearestBlock(Vec3 startLoc, int[] blocksId, int max){
int x = (int) startLoc.xCoord;
int y = (int) startLoc.yCoord;
int z = (int) startLoc.zCoord;
for (int i = 0 ; i < max ; i++){
for (int j = -i ; j < i + 1 ; j++){
for (int k = -i ; k < i + 1 ; k++){
if (arrayEqual(dropBlockId(x + i, y + k, z + j), blocksId)){
return Vec3.createVectorHelper(x + i, y + k, z + j);
}
if (arrayEqual(dropBlockId(x - i, y + k, z + j), blocksId)){
return Vec3.createVectorHelper(x - i, y + k, z + j);
}
if (arrayEqual(dropBlockId(x + k, y + i, z + j), blocksId)){
return Vec3.createVectorHelper(x + k, y + i, z + j);
}
if (arrayEqual(dropBlockId(x + k, y - i, z + j), blocksId)){
return Vec3.createVectorHelper(x + k, y - i, z + j);
}
if (arrayEqual(dropBlockId(x + k, y + j, z + i), blocksId)){
return Vec3.createVectorHelper(x + k, y + j, z + i);
}
if (arrayEqual(dropBlockId(x + k, y + j, z - i), blocksId)){
return Vec3.createVectorHelper(x + k, y + j, z - i);
}
}
}
}
return null;
}
///Return the nearest block by array of id without skip block in the skipList
public Vec3[] findNearestBlocks(Vec3 startLoc, int blockId,int stackSize, int max, Vec3[] skipList){
int[] blocksId = {blockId};
return findNearestBlocks(startLoc, blocksId, stackSize, max, skipList);
}
///Return the nearest block by array of ids without skip block in the skipList
public Vec3[] findNearestBlocks(Vec3 startLoc, int[] blocksId,int stackSize, int max, Vec3[] skipList){
int skipListSize = skipList.length;
Vec3[] tempLocation = new Vec3[stackSize + skipListSize];
Vec3[] toReturn = new Vec3[stackSize];
for (int i = 0 ; i < skipListSize ; i++){
tempLocation[i] = skipList[i];
}
findNearestBlocks(tempLocation, startLoc, blocksId, skipListSize, max);
for (Vec3 vec3 : tempLocation) {
if (vec3 == null){
return null;
}
}
for (int i = 0 ; i < stackSize ; i++){
toReturn[i] = tempLocation [skipListSize + i];
}
return toReturn;
}
///Return the nearest block by array of id
public Vec3[] findNearestBlocks(Vec3 startLoc, int blockId,int stackSize, int max){
int[] blocksId = {blockId};
return findNearestBlocks(startLoc, blocksId, stackSize, max);
}
///Return the nearest block by array of ids
public Vec3[] findNearestBlocks(Vec3 startLoc, int[] blocksId,int stackSize, int max){
Vec3[] toReturn = new Vec3[stackSize];
findNearestBlocks(toReturn, startLoc, blocksId, 0, max);
for (Vec3 vec3 : toReturn) {
if (vec3 == null){
return null;
}
}
return toReturn;
}
///Find a number of blocks near the other block
private int findNearestBlocks(Vec3[] list, Vec3 startLoc, int[] blocksId,int stackSize, int max){
int x = (int) startLoc.xCoord;
int y = (int) startLoc.yCoord;
int z = (int) startLoc.zCoord;
if (stackSize == list.length){
return stackSize;
}
for (int i = 0 ; i < max ; i++){
for (int j = -i ; j < i + 1 ; j++){
for (int k = -i ; k < i + 1 ; k++){
if ((arrayEqual(dropBlockId(x + i, y + k, z + j), blocksId)) && !arrayEqual(Vec3.createVectorHelper(x + i, y + k, z + j), list)){
list[stackSize++] = Vec3.createVectorHelper(x + i, y + k, z + j);
return (findNearestBlocks(list, startLoc, blocksId, stackSize, max));
}
if ((arrayEqual(dropBlockId(x - i, y + k, z + j), blocksId)) && !arrayEqual(Vec3.createVectorHelper(x - i, y + k, z + j), list)){
list[stackSize++] = Vec3.createVectorHelper(x - i, y + k, z + j);
return (findNearestBlocks(list, startLoc, blocksId, stackSize, max));
}
if ((arrayEqual(dropBlockId(x + k, y + i, z + j), blocksId)) && !arrayEqual(Vec3.createVectorHelper(x + k, y + i, z + j), list)){
list[stackSize++] = Vec3.createVectorHelper(x + k, y + i, z + j);
return (findNearestBlocks(list, startLoc, blocksId, stackSize, max));
}
if ((arrayEqual(dropBlockId(x + k, y - i, z + j), blocksId)) && !arrayEqual(Vec3.createVectorHelper(x + k, y - i, z + j), list)){
list[stackSize++] = Vec3.createVectorHelper(x + k, y - i, z + j);
return (findNearestBlocks(list, startLoc, blocksId, stackSize, max));
}
if ((arrayEqual(dropBlockId(x + k, y + j, z + i), blocksId)) && !arrayEqual(Vec3.createVectorHelper(x + k, y + j, z + i), list)){
list[stackSize++] = Vec3.createVectorHelper(x + k, y + j, z + i);
return (findNearestBlocks(list, startLoc, blocksId, stackSize, max));
}
if ((arrayEqual(dropBlockId(x + k, y + j, z - i), blocksId)) && !arrayEqual(Vec3.createVectorHelper(x + k, y + j, z - i), list)){
list[stackSize++] = Vec3.createVectorHelper(x + k, y + j, z - i);
return (findNearestBlocks(list, startLoc, blocksId, stackSize, max));
}
}
}
}
return stackSize;
}
///Check if one of the Vector is equal of the array
private boolean arrayEqual(Vec3 v, Vec3[] arr){
for (int i = 0 ; i < arr.length ; i++){
if (arr[i]!=null){
if (v.distanceTo(arr[i]) == 0){
return true;
}
}
}
return false;
}
///Check if one number is equal to array
private boolean arrayEqual (int id, int[] arr){
for (int i = 0 ; i < arr.length ; i++){
if (id == arr[i]){
return true;
}
}
return false;
}
///Get the world
public World getWorld() {
return world;
}
///Set the world
public void setWorld(World world) {
this.world = world;
}
///Get the World Info
public IWorldInfo getWorldInfo() {
return worldInfo;
}
///Set the World Info
public void setWorldInfo(IWorldInfo worldInfo) {
this.worldInfo = worldInfo;
}
///Get list of block of some kind
public ArrayList<Vec3> getBlockList(Vec3 StartPos, double distance, String name){
ArrayList<Vec3> toReturn = new ArrayList<Vec3>();
for (int i = (int)(StartPos.xCoord - distance) ; i < StartPos.xCoord + distance ; i++){
for (int j = (int)(StartPos.zCoord - distance) ; j < StartPos.zCoord + distance ; j++){
int startY = (int) StartPos.yCoord;
for (int k = (int)(startY - distance) ; k < startY + distance ; k++){
if (world.getBlock(i, k, j).getLocalizedName().equals(name))
toReturn.add(Vec3.createVectorHelper(i, k, j));
}
}
}
return toReturn;
}
///Check if the block is air
public boolean isBlockAir(Vec3 loc){
return world.isAirBlock((int)loc.xCoord, (int)loc.yCoord, (int)loc.zCoord);
}
///Get the id of the block by vector
public int blockId(Vec3 loc){
return Block.getIdFromBlock(world.getBlock((int)loc.xCoord, (int)loc.yCoord, (int)loc.zCoord));
}
///Get the id of the block by integers
public int blockId(int x, int y, int z){
return Block.getIdFromBlock(world.getBlock(x, y, z));
}
///Get the id of the block drop
public int dropBlockId(Vec3 loc){
return dropBlockId((int)loc.xCoord, (int)loc.yCoord, (int)loc.zCoord);
}
///Get the id of the block drop
public int dropBlockId(int x, int y, int z){
return Item.getIdFromItem(world.getBlock(x, y, z).getItemDropped(0, new Random(), 0));
//return Item.getIdFromItem(Item.getItemFromBlock(world.getBlock(x, y, z)));
}
///Get the block by vector
public Block getBlock(Vec3 loc){
return world.getBlock((int)loc.xCoord, (int)loc.yCoord, (int)loc.zCoord);
}
///Get the block by integers
public Block getBlock(int x, int y, int z){
return world.getBlock(x, y, z);
}
///Get the TileEntityFurnace form the world
public TileEntityFurnace getFurnaceEntity(Vec3 loc){
return (TileEntityFurnace)world.getTileEntity((int)loc.xCoord, (int)loc.yCoord, (int)loc.zCoord);
}
///Harvest a block in the world
public void harvestBlock(int entityId, Vec3 loc){
world.destroyBlockInWorldPartially(entityId, (int)loc.xCoord, (int)loc.yCoord, (int)loc.zCoord, -1);
}
///Find a path form one point to other point
public Queue<Step> findPath(Vec3 startLoc, Vec3 endLoc){
PathFinder pathFinder = new PathFinder(startLoc, endLoc, worldInfo);
Queue<Step> stepsToGoal = pathFinder.findPath();
if (null == stepsToGoal || stepsToGoal.size() <= 0){
return null;
}
PathSmoother.getInstance().smoothPath(stepsToGoal);
return stepsToGoal;
}
}