-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAIPlayer.java
More file actions
465 lines (414 loc) · 15.1 KB
/
Copy pathAIPlayer.java
File metadata and controls
465 lines (414 loc) · 15.1 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package com.custommods.ai;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
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 com.custommods.walkmod.WalkMod;
import com.mojang.realmsclient.dto.McoServer.WorldType;
import com.sun.corba.se.spi.orbutil.threadpool.Work;
import com.sun.javafx.event.EventHandlerManager;
import com.sun.xml.internal.ws.api.config.management.policy.ManagementAssertion.Setting;
import cpw.mods.fml.common.eventhandler.Event;
import cpw.mods.fml.common.eventhandler.EventPriority;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import cpw.mods.fml.relauncher.Side;
import net.minecraft.block.Block;
import net.minecraft.block.BlockFurnace;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemAxe;
import net.minecraft.item.ItemPickaxe;
import net.minecraft.item.ItemSpade;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemTool;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.event.entity.minecart.MinecartEvent;
import net.minecraftforge.event.entity.minecart.MinecartInteractEvent;
import net.minecraftforge.event.terraingen.DecorateBiomeEvent.Decorate.EventType;
import scala.remote;
import scala.annotation.meta.param;
public class AIPlayer {
public static int CANT_HARVEST = -1;
public static int CANT_BREAK = -2;
private EntityPlayer player;
private boolean doneWalk = false;
///Constructor
public AIPlayer(EntityPlayer player){
this.player = player;
}
///Copy Constructor
public AIPlayer(AIPlayer player){
this.player.clonePlayer(player.getPlayer(), true);
}
///Get the player
public EntityPlayer getPlayer() {
return player;
}
///Set the player
public void setPlayer(EntityPlayer player) {
this.player = player;
}
///Get entity ID
public int getEntityID(){
return player.getEntityId();
}
///Get the time to dig the block by the player
public double getTimeToDig(Vec3 blockLoc,Block block, World world){
double blockHardness;
if (player.canHarvestBlock(block)){
if ((blockHardness = Util.getBlockHardness(blockLoc, block, world)) != -1){
double strVsBlock = player.getCurrentPlayerStrVsBlock(block, false);
return (blockHardness / strVsBlock);
}
else{
return CANT_BREAK;
}
}
else{
return CANT_HARVEST;
}
}
///Place a block in the world
public boolean placeBlock(AIWorld world, Vec3 place){
return world.getWorld().canMineBlock(player, (int)place.xCoord, (int)place.yCoord, (int)place.zCoord);
}
///Get the Vec3 of the position of the eyes
public Vec3 eyesLoc(){
return player.getPosition(0).addVector(0, player.getEyeHeight(), 0);
}
///Find the point that the player look it
public Vec3 getLookPoint(AIWorld world){
Vec3 loc = eyesLoc();
Vec3 look = player.getLookVec();
while (world.isBlockAir(loc)){
if (loc.distanceTo(eyesLoc()) > UserSetting.MaxDistanceLook){
return null;
}
loc = loc.addVector(look.xCoord, look.yCoord, look.zCoord);
}
return loc;
}
///Find the location of the empty block that the player look at
public Vec3 getLookEmptyBlock(AIWorld world){
Vec3 loc = eyesLoc();
Vec3 look = player.getLookVec();
while (world.isBlockAir(loc)){
loc = loc.addVector(look.xCoord, look.yCoord, look.zCoord);
}
loc = loc.addVector(-look.xCoord/2, -look.yCoord/2, -look.zCoord/2);
if (!world.isBlockAir(loc)){
loc = loc.addVector(-look.xCoord/2, -look.yCoord/2, -look.zCoord/2);
}
return loc;
}
///Get the location of the player
public Vec3 getLocation(){
return player.getPosition(0);
}
///Move the player to a point in the world
public boolean moveToPoint(Vec3 dest, AIWorld world){
Logger.debug("Start the Path Finding");
Logger.debug("b: " + dest);
Vec3 newDest = MinecraftWorldInfo.checkFirstNotAirBlock(dest);
Logger.debug("a: " + newDest + " and dist: " + newDest.distanceTo(dest));
// if (newDest.distanceTo(dest) != 0){
// moveToPoint(newDest, world);
// }
PathFinder pathFinder = new PathFinder(getLocation(), dest, world.getWorldInfo());
Queue<Step> stepsToGoal = pathFinder.findPath();
if (null == stepsToGoal || stepsToGoal.size() <= 0){
return false;
}
PathSmoother.getInstance().smoothPath(stepsToGoal);
Logger.debug("Start waking to: " + dest);
walkOnPath(stepsToGoal);
return true;
}
@SubscribeEvent
public void onPlayerTick(TickEvent.PlayerTickEvent event) {
Logger.debug("tick");
if (event.side == Side.SERVER){
Logger.debug("server side");
doneWalk = false;
}
else{
doneWalk = WalkMod.pathNavigator.isRun();
}
Logger.debug("doneWalk: " + doneWalk);
}
///Do the path
public void walkOnPath(Queue<Step> steps){
WalkMod.pathNavigator.setStepsQueue(steps);
WalkMod.pathNavigator.run();
while (WalkMod.pathNavigator.isRun()){
Util.waitAndTick();
}
for (int i = 0 ; i < 10 ; i++){
Util.waitAndTick();
}
WalkMod.pathNavigator.stop();
Logger.debug("Finished run");
}
///Move the player next to a block
public boolean standNextTo(Vec3 blockLoc, AIWorld world){
Vec3 gotoLoc = world.findNearestBlock(blockLoc, UserSetting.AirBlockId, (int)UserSetting.rechDistance);
if (gotoLoc == null){
return false;
}
moveToPoint(gotoLoc, world);
return true;
}
///Craft an item
public boolean craftItem (AIinventory inve, ItemStack item, AIWorld world){
if (!Objective.canCraft(item, inve)){
return false;
}
return inve.craftItem(item);
}
///Send a message to the player
public void sendMessage(String msg){
player.addChatComponentMessage(new ChatComponentText(msg));
}
///Smelt an item
public boolean smeltItem (AIinventory inve, ItemStack item, AIWorld world){
String furnacerBlock = "furnace";
if (!Objective.canSmelt(item, inve)){
return false;
}
return inve.smeltItem(item);
}
///Return the tool type
private int getToolType(ItemStack item){
if (item.getItem() instanceof ItemPickaxe){
return AIinventory.PICKAXE;
}
else if (item.getItem() instanceof ItemAxe){
return AIinventory.AXE;
}
else if (item.getItem() instanceof ItemSpade){
return AIinventory.SHOVEL;
}
else{
return AIinventory.NOT_FOUND;
}
}
///Get an item
public boolean getItem(ItemStack item, AIWorld world, AIinventory inve){
WorkTreePlan workTreePlan = new WorkTreePlan(item.getDisplayName());
workTreePlan.setWorld(world);
workTreePlan.init();
workTreePlan.CheckItems(inve);
workTreePlan.addLoc(getLocation());
if (planTree (item, world, inve, workTreePlan) == Util.Max){
Logger.debug("Cant get this item", Logger.LOG);
return false;
}
Logger.debug(workTreePlan.toString(), Logger.LOG);
//workTreePlan.unionTree();
Logger.debug(workTreePlan.toString(), Logger.LOG);
return doWorkTreePlan(workTreePlan, inve, world);
}
///The tree of the plan
private double planTree(ItemStack item,AIWorld world, AIinventory inve, WorkTreePlan workTreePlan){
List<ItemStack> craftInger = RecipesList.getIngredientList(item);
ItemStack smeltInger = RecipesList.getSmeltingItem(item);
double craftHeur = 0;
double goGetHeur = 0;
ItemStack tempTool;
ItemStack tempBlock;
Vec3[] blocksLoc = null;
int gotoNum = 0;
WorkTreePlan craftTree = new WorkTreePlan(item, WorkTreePlan.Type.craft, workTreePlan);
WorkTreePlan togoTree = new WorkTreePlan(null, WorkTreePlan.Type.moveTo, workTreePlan);
WorkTreePlan smeltTree = new WorkTreePlan(item, WorkTreePlan.Type.smeltStart, workTreePlan);
WorkTreePlan toolTree = new WorkTreePlan(null, WorkTreePlan.Type.tool, togoTree);
boolean needTool = false;
int toolKind = -1;
boolean needToSmelt = false;
int smeltNum;
int craftNum;
int togoNum;
//Check if the player already have the item
if ((item.stackSize = workTreePlan.haveItem(inve, item)) >= 0){
Logger.debug("PlanTree: allready have " + item.getDisplayName());
return 0;
}
item.stackSize *= -1;
//Check if the item can made by melting
if (smeltInger !=null){
gotoNum = workTreePlan.countLoc();
smeltNum = workTreePlan.addChild(smeltTree);
//Check if need to craft a furnace
ItemStack furnance = new ItemStack(Item.getItemById(UserSetting.FurnaceId), 1);
craftHeur += planTree(furnance, world, inve, smeltTree);
Logger.debug("PlanTree: can use smelt to get " + item.getDisplayName());
smeltInger.stackSize = item.stackSize;
craftHeur += planTree(smeltInger.copy(), world, inve, smeltTree);
needToSmelt = true;
workTreePlan.removeChild(smeltNum);
gotoNum = gotoNum - workTreePlan.countLoc();
}
//Check if the item can made by crafting
else if (craftInger == null){
Logger.debug("PlanTree: no craft for " + item.getDisplayName());
craftHeur = Util.Max;
}
else{
craftNum = workTreePlan.addChild(craftTree);
gotoNum = workTreePlan.countLoc();
//Check if need to craft a crafting table
if (workTreePlan.getType() != WorkTreePlan.Type.craft){
ItemStack craftingTable = new ItemStack(Item.getItemById(UserSetting.CraftingTableId), 1);
craftHeur = planTree(craftingTable, world, inve, craftTree);
}
for (ItemStack itemStack : craftInger) {
double tempHeur = planTree(itemStack.copy(), world, inve, craftTree);
craftHeur += tempHeur;
workTreePlan.AddUseItem(itemStack, -itemStack.stackSize);
}
gotoNum = gotoNum - workTreePlan.countLoc();
workTreePlan.removeChild(craftNum);
}
//Check if need an tool to mine
if ((tempTool = Util.getMinToolToCraft(item) ) == null){
goGetHeur = Util.Max;
}
else{
togoNum = workTreePlan.addChild(togoTree);
//Logger.debug("Tool is: " + tempTool.getDisplayName() + " Need Tool: " + Util.idItemEqual(tempTool, Util.getItemStack(Util.EMPTY_ID)) + " " + inve.betterTool(tempTool.copy()), Logger.LOG);
if (!(Util.idItemEqual(tempTool, Util.getItemStack(Util.EMPTY_ID)) || inve.betterTool(tempTool.copy()))){
Logger.debug("PlanTree: tool need to mine " + item.getDisplayName() + " is: " + tempTool.getDisplayName());
togoTree.addChild(toolTree);
goGetHeur = planTree( tempTool.copy(), world, inve, toolTree);
toolKind = getToolType(tempTool);
toolTree.set(toolKind);
needTool = true;
}
else {
needTool = false;
}
//Find the nearest blocks of this kind
blocksLoc = world.findNearestBlocks(workTreePlan.peekLoc(), Item.getIdFromItem(item.getItem()), item.stackSize, UserSetting.BLOCK_SEARCH_SIZE, workTreePlan.GetLoctionArr());
//Check if there is a block near the player
if (blocksLoc ==null){
Logger.debug("PlanTree: there is no block for " + item.getDisplayName());
goGetHeur = Util.Max;
}
else{
//second check for the tool need to the mining
if (!needTool){
tempTool = Util.getMinToolToCraft(world.getBlock(blocksLoc[0]));
if (tempTool.getItem() != null){
if (!inve.betterTool(tempTool)){
Logger.debug("PlanTree: second check - tool need to mine " + item.getDisplayName() + " is: " + tempTool.getDisplayName());
togoTree.addChild(toolTree);
goGetHeur = planTree( tempTool.copy(), world, inve, toolTree);
toolTree.set(getToolType(tempTool));
}
}
}
Logger.debug("planTree: blocks ammunt: " + blocksLoc.length);
for (int i = 0 ; i < blocksLoc.length ; i++){
Logger.debug("planTree: goto block to " + blocksLoc[i]);
}
goGetHeur += Util.getHeuristic(workTreePlan.peekLoc(), blocksLoc[0], world);
for (int i = 0 ; i < blocksLoc.length -1 ; i++){
goGetHeur += Util.getHeuristic(blocksLoc[i], blocksLoc[i+1], world);
}
}
workTreePlan.removeChild(togoNum);
}
Logger.debug(item.getDisplayName() + ": craftHeur: " + craftHeur + " goGetHeur: " + goGetHeur);
if (craftHeur >= Util.Max && goGetHeur >= Util.Max){
Logger.debug("planTree: can't find a way to get: " + item.getDisplayName());
return Util.Max;
}
//Check if the item need to craft or to get
if (craftHeur < goGetHeur){
if (needToSmelt){
Logger.debug("planTree: need to smelt for: " + item.getDisplayName());
workTreePlan.addChild(smeltTree);
workTreePlan.AddUseItem(smeltInger, -smeltInger.stackSize);
workTreePlan.AddUseItem(RecipesList.getSmeltingResult(smeltInger), smeltInger.stackSize);
}
else{
Logger.debug("planTree: need to craft for: " + item.getDisplayName());
workTreePlan.addChild(craftTree);
ItemStack newItem = item.copy();
newItem.stackSize = 1;
workTreePlan.AddUseItem(RecipesList.getRecipes(newItem).getRecipeOutput());
}
for (int i = 0 ; i < gotoNum ; i++){
workTreePlan.removeLoc();
}
return craftHeur;
}
else{
Logger.debug("planTree: need to go get: " + item.getDisplayName());
togoTree.set(blocksLoc);
workTreePlan.addChild(togoTree);
workTreePlan.AddUseItem(item, blocksLoc.length);
//workTreePlan.addLoc(blocksLoc[blocksLoc.length - 1]);
for (int i = 0; i < blocksLoc.length ; i++){
workTreePlan.addLoc(blocksLoc[i]);
}
return goGetHeur;
}
}
///Do the WorkTreePlan
private boolean doWorkTreePlan(WorkTreePlan plan, AIinventory inve, AIWorld world){
boolean succeded = true;
for (int i = 0; i < plan.childrenLenght() ; i++){
if (plan.getChild(i) != null){
succeded = succeded && doWorkTreePlan(plan.getChild(i), inve, world);
}
}
succeded = succeded && doNodeInWorkTree(plan.getTodo(), plan.getType(), inve, world);
return succeded;
}
///Do one node in the WorkTreePlan
private boolean doNodeInWorkTree(Object obj, WorkTreePlan.Type type, AIinventory inve, AIWorld world){
switch (type) {
case nothing:
return true;
case smeltEnd:
return true;
case smeltStart:
Logger.debug("doWorkPlan: smelt:" + ((ItemStack)obj).getDisplayName() );
return smeltItem(inve, (ItemStack)obj, world);
case craft:
Logger.debug("craft - " + ((ItemStack)obj).getDisplayName());
((ItemStack)obj).stackSize = 1;
return craftItem(inve, (ItemStack)obj, world);
case tool:
Logger.debug("use tool: " + obj, Logger.LOG);
if ((Integer)obj != AIinventory.NOT_FOUND){
inve.useTool((Integer)obj);
}
return true;
case moveTo:
Vec3[] loctaion = (Vec3[])obj;
boolean succeeded = true;
for (Vec3 vec3 : loctaion) {
Logger.debug("Move to: " + vec3, Logger.LOG);
succeeded = succeeded && moveToPoint(vec3, world);
}
return succeeded;
default:
break;
}
return false;
}
}