-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorkTreePlan.java
More file actions
460 lines (400 loc) · 11.5 KB
/
Copy pathWorkTreePlan.java
File metadata and controls
460 lines (400 loc) · 11.5 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
package com.custommods.ai;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Vec3;
public class WorkTreePlan {
public static enum Type {smeltStart, smeltEnd, craft, tool, moveTo, nothing};
private Type type;
private Object todo;
private WorkTreePlan[] childs;
private WorkTreePlan parent = null;
private List invetoryChange;
private static AIWorld world = null;
private static boolean craftCraftingTable = false;
private static boolean craftFurnace = false;
private static List locationList;
private final boolean USE_ITEM_PRINT = false;
private final boolean OBJECT_PRINT = true;
private final int NOT_FOUND = -1;
private final int MAX_NUMBER_OF_CHILDES = 10;
///Init the WorkTreePlan
public void init(){
initLocationList();
initInveChange();
}
///Init the location list
public void initLocationList(){
locationList = new ArrayList<Vec3>();
}
///Get the type of the object
public Type getType() {
return type;
}
///Get the object that need to do
public Object getTodo() {
return todo;
}
///Check if there is a crafting table
public static boolean isCraftCraftingTable() {
return craftCraftingTable;
}
///Call when is get a crafting table
public static void craftCraftingTable() {
WorkTreePlan.craftCraftingTable = true;
}
///Check if there is a furnace
public static boolean isCraftFurnace() {
return craftFurnace;
}
///Call when is get a furnace
public static void craftFurnace() {
WorkTreePlan.craftFurnace = true;
}
///Constructor
public WorkTreePlan(Object todo, Type type){
set(todo,type);
initLists();
}
///Check if there is items in the inventory
public void CheckItems(AIinventory inve){
ItemStack craftingTable = new ItemStack(Item.getItemById(UserSetting.CraftingTableId), 1);
ItemStack furnance = new ItemStack(Item.getItemById(UserSetting.FurnaceId), 1);
craftCraftingTable = haveItem(inve, craftingTable) >= 0;
craftFurnace = haveItem(inve, furnance) >= 0;
}
///Constructor
public WorkTreePlan(Object todo, Type type, WorkTreePlan parent){
set(todo,type);
initLists();
setParent(parent);
}
///Constructor
public WorkTreePlan(){
set(Type.nothing);
this.todo = null;
initLists();
}
///Constructor
public WorkTreePlan(String text){
set(Type.nothing);
set(text);
initLists();
}
///Initialize the Lists
private void initLists(){
this.childs = new WorkTreePlan[MAX_NUMBER_OF_CHILDES];
initInveChange();
}
///Initialize the inventory Change list
public void initInveChange(){
this.invetoryChange = new ArrayList<ItemStack>();
}
///Add a item that need to be used
public void AddUseItem(ItemStack item){
boolean found = false;
for (Object object : invetoryChange) {
if (Util.idItemEqual(item, (ItemStack)object)){
((ItemStack)object).stackSize += item.stackSize;
found = true;
/*if (((ItemStack)object).stackSize == 0){
invetoryChange.remove(object);
}*/
}
}
if (!found){
invetoryChange.add(item.copy());
}
}
///Add a item that need to be used
public void AddUseItem(ItemStack item, int stack){
ItemStack tempItem = new ItemStack(item.getItem());
tempItem.stackSize = stack;
AddUseItem(tempItem);
}
///Get the parent
public WorkTreePlan getParent(){
return parent;
}
///Check if already have the item
public int haveItem(AIinventory inve, ItemStack item){
int inventoryStack = inve.stackSize(item);
int treeStack = stackInTree(item);
Logger.debug("StackTree: " + treeStack + ", inveStack: " + inventoryStack + ", " + item.getDisplayName() + " whit " + item.stackSize, Logger.LOG);
return (inventoryStack + treeStack - item.stackSize);
}
private int stackInTree2(ItemStack item){
int toReturn = 0;
for (Object object : invetoryChange) {
if (Util.idItemEqual(item, (ItemStack)object)){
toReturn += ((ItemStack)object).stackSize;
}
}
return toReturn;
}
///Stack Size of an item in all the tree
private int stackInTree(ItemStack item){
int toReturn = 0;
WorkTreePlan tempParent = this;
while (tempParent.getParent() != null){
tempParent = tempParent.getParent();
}
return stackSize(tempParent, item);
}
///Get stack form the tree
private int stackSize(WorkTreePlan plan, ItemStack item){
int toReturn = 0;
//Logger.debug(plan.printByType(),Logger.LOG);
for (Object object : plan.invetoryChange) {
//Logger.debug("looking for " + item.getDisplayName() + " and this is " + ((ItemStack)object).getDisplayName(), Logger.LOG);
if (Util.idItemEqual(item, (ItemStack)object)){
//Logger.debug("got one " + item.getDisplayName() , Logger.LOG);
toReturn += ((ItemStack)object).stackSize;
}
}
for (int i = 0; i < plan.childs.length ; i++){
if (plan.childs[i]!=null){
toReturn += stackSize((WorkTreePlan)plan.childs[i], item);
}
}
/*for (Object object : plan.childs) {
toReturn += stackSize((WorkTreePlan)object, item);
}*/
return toReturn;
}
///Set the world
public void setWorld(AIWorld world){
Logger.debug(world.toString(), Logger.LOG);
this.world = world;
}
///Set the parent of this
public void setParent(WorkTreePlan parent){
this.parent = parent;
}
///Add child, return the child if succeed otherwise null
public int addChild(Object todo, Type type){
WorkTreePlan child = new WorkTreePlan(todo, type);
return addChild(child);
}
///Add child, return the child if succeed otherwise null
public int addChild(WorkTreePlan treePlan){
for (int i=0 ; i < childs.length ; i++){
if (childs[i] == null){
childs[i] = treePlan;
return i;
}
}
return NOT_FOUND;
}
///Get the number of children
public int childrenLenght(){
return childs.length;
}
///Get child in location
public WorkTreePlan getChild(int index){
return (WorkTreePlan) childs[index];
}
///Set the object
public void set(Object todo){
this.todo = todo;
}
///Set the type
public void set(Type type){
this.type = type;
}
///Set the object and type
public void set(Object todo, Type type){
set(type);
set(todo);
}
///Remove the current node
public void remove(){
}
///Remove a child
public void removeChild(int index){
childs[index].setParent(null);
childs[index] = null;
}
///Return a string with the tree to the children
public String toString(){
return "\n" + print("", true, OBJECT_PRINT) + "\n" + print("", true, USE_ITEM_PRINT);
}
///Return the id of the block in the todo, otherwise return Util.CANT_GET
public int blockId(){
if (todo instanceof Vec3[] && world != null){
return world.dropBlockId(((Vec3[])todo)[0]);
}
return Util.CANT_GET;
}
///Return the string of for the top node
public String printFromParent(){
WorkTreePlan tempNode = this;
String toReturn = "";
while (tempNode.getParent() != null){
toReturn += tempNode.printByType();
tempNode = tempNode.getParent();
}
return tempNode.toString();
}
//Return a string of a tree
private String print(String indent, boolean lastChild, boolean whoToPrint){
String toReturn = "";
toReturn += indent;
if (lastChild)
{
toReturn += "\\-";
indent += " ";
}
else
{
toReturn += "|-";
indent += "| ";
}
if (whoToPrint){
toReturn += printByType() + "\n";
}
else{
toReturn += printUseItem() + "\n";
}
for (int i = 0; i < childs.length ; i++){
if (childs[i] != null){
toReturn += ((WorkTreePlan) childs[i]).print(indent, i == childs.length - 1, whoToPrint);
}
}
return toReturn;
}
///Return one insteps in the tree
private String printByType(){
if (todo == null){
return "the object is null";
}
switch (type) {
case nothing:
if(todo == null){
return "Nothing";
}
else{
return "Get - " + todo;
}
case moveTo:
Vec3[] loctions = (Vec3[])todo;
int blockid = blockId();
String toReturn = "";
if (blockid != Util.CANT_GET){
toReturn = "Moving to a points to get " + Util.getItemStack(blockid).getDisplayName() + ": ";
}
else{
toReturn = "Moving to a points: ";
}
for (Vec3 vec3 : loctions) {
toReturn += vec3.toString() + " ";
}
return toReturn;
case tool:
switch ((Integer)todo) {
case AIinventory.AXE:
return "Swhich to tool- axe";
case AIinventory.PICKAXE:
return "Swhich to tool- picaxe";
case AIinventory.SHOVEL:
return "Swhich to tool- shovel";
default:
return "Swhich to tool- ERROR: no tool";
}
case craft:
return "Craft- " + ((ItemStack) todo).getDisplayName();
case smeltEnd:
return "End smelting- " + ((ItemStack) todo).getDisplayName();
case smeltStart:
return "Start smelting- " + ((ItemStack) todo).getDisplayName();
default:
return "Error: Type is worng";
}
}
///Print the used item
public String printUseItem(){
String toReturn = "";
for (Object object : invetoryChange) {
toReturn += ((ItemStack)object).getDisplayName() + "- Size:" + ((ItemStack)object).stackSize + ", ";
}
return toReturn;
}
///Union a similar node in the tree
public void unionTree(){
List items = new ArrayList<WorkTreePlan>();
union(this,items);
}
///Do the recursive union, return true if need to remove him, otherwise false
private boolean union(WorkTreePlan plan,List items){
if (plan != null){
if (plan.getType() == Type.moveTo){
int index;
if ((index = blockInList(items, plan)) == NOT_FOUND){
items.add(plan);
}
else{
union((WorkTreePlan)items.get(index), plan);
plan.remove();
plan = null;
return true;
}
}
if (plan != null){
for (int i = 0; i < childrenLenght(); i++) {
if (union((WorkTreePlan)plan.getChild(i), items)){
plan.removeChild(i);
}
}
}
}
return false;
}
///Check if the block is the list, if found return the number in the list otherwise return NOT_FOUND
private int blockInList(List items, WorkTreePlan plan){
for (int i = 0; i < items.size() ; i++) {
if (((WorkTreePlan)items.get(i)).blockId() == plan.blockId()){
return i;
}
}
return NOT_FOUND;
}
///Union two locations
private void union(WorkTreePlan plan1, WorkTreePlan plan2){
plan1.set(ArrayUtils.addAll((Vec3[])plan1.getTodo(), (Vec3[])plan2.getTodo()));
}
///Add location to the locationList
public void addLoc(Vec3 loc){
locationList.add(loc);
}
///Peek location from the locationList
public Vec3 peekLoc(){
return (Vec3) locationList.get(locationList.size()-1);
}
///Pull location from the locationList
public Vec3 pullLoc(){
Vec3 toReturn = peekLoc();
removeLoc();
return toReturn;
}
///Remove the last form the locationList
public void removeLoc(){
locationList.remove(locationList.size() - 1);
}
///Count the number of location
public int countLoc(){
return locationList.size();
}
///Return array of all the location in the location list
public Vec3[] GetLoctionArr(){
Vec3[] toReturn = new Vec3[locationList.size()];
int conter = 0;
for (Object object : locationList) {
if (object instanceof Vec3){
toReturn[conter++] = (Vec3)object;
}
}
return toReturn;
}
}