· 5 years ago · Jul 17, 2020, 02:16 PM
1fy/-- Replacement for code originally pasted in 2014
2-- selfReplicate2018.lua last modified 12/12/2018
3-- Using CC 1.65 to 1.79 on Minecraft 1.7.10 and 1.8.9
4-- Takes advantage of turtle.inspect() and turtle.getItemDetail()
5-- pastebin http://pastebin.com/2Zf5an8H
6-- multiline comments not used as Pastebin trashes the appearance of the code!
7-- functions in alphabetical order except main()
8-- classes declared before functions
9-- CraftOS 1.6 names: (altered in main() if using CraftOS 1.7)
10
11-- global variables
12local ccComputer = "ComputerCraft:CC-Computer"
13local ccDisk = "ComputerCraft:disk"
14local ccTurtle = "ComputerCraft:CC-Turtle"
15local ccCraftyMiningTurtle = "ComputerCraft:CC-TurtleExpanded"
16local ccDiskDrive = "ComputerCraft:CC-Peripheral"
17local pastebinCode = "2Zf5an8H"
18
19-- classes
20function classCoord(coordName)
21 -- 0 = go south (z increases)
22 -- 1 = go west (x decreases)
23 -- 2 = go north (z decreases
24 -- 3 = go east (x increases)
25
26 -- compass[0] = "south"
27 -- compass[1] = "west"
28 -- compass[2] = "north"
29 -- compass[3] = "east"
30
31 clsCoord = {} -- the table representing the class, which will double as the metatable for any instances
32 clsCoord.__index = clsCoord -- failed table lookups on the instances should fallback to the class table, to get methods
33
34 local self = setmetatable({}, clsCoord)
35 self.name = coordName
36 self.x = 0
37 self.y = 0
38 self.z = 0
39 self.facing = 0
40 self.compass = "south"
41
42 function clsCoord.getX(self)
43 return self.x
44 end
45 function clsCoord.setX(self, newVal) -- setter
46 self.x = newVal
47 end
48 function clsCoord.getY(self) -- getter
49 return self.y
50 end
51 function clsCoord.setY(self, newVal)
52 self.y = newVal
53 end
54 function clsCoord.getZ(self)
55 return self.z
56 end
57 function clsCoord.setZ(self, newVal)
58 self.z = newVal
59 end
60 function clsCoord.getFacing(self)
61 return self.facing
62 end
63 function clsCoord.setFacing(self, newVal) -- setter
64 self.facing = newVal
65 if self.facing < 0 then
66 self.facing = 3
67 elseif self.facing > 3 then
68 self.facing = 0
69 end
70 if self.facing == 0 then
71 self.compass = "south"
72 elseif self.facing == 1 then
73 self.compass = "west"
74 elseif self.facing == 2 then
75 self.compass = "north"
76 else
77 self.compass = "east"
78 end
79 end
80 function clsCoord.getCompass(self)
81 return self.compass
82 end
83 function clsCoord.setCompass(self, newVal) -- setter
84 self.compass = newVal
85
86 if self.compass == "south" then
87 self.facing = 0
88 elseif self.compass == "west" then
89 self.facing = 1
90 elseif self.compass == "north" then
91 self.facing = 2
92 elseif self.compass == "east" then
93 self.facing = 3
94 end
95 end
96 function clsCoord.goUp(blocks)
97 blocks = blocks or 1
98 self.y = self.y + blocks
99 end
100 function clsCoord.goDown(blocks)
101 blocks = blocks or 1
102 self.y = self.y - blocks
103 end
104 -- uses:
105 -- location:getX() get current turtle x coordinate
106 -- location:getY() get current turtle y coordinate
107 -- location:getZ() get current turtle z coordinate
108 -- location:setX(xCoord) set current turtle x coordinate eg location:setX(-235)
109 -- location:setY(yCoord) set current turtle y coordinate eg location:setY(66)
110 -- location:setZ(zCoord) set current turtle z coordinate eg location:setZ(125)
111 -- location:getFacing() returns a number 0 - 3 representing direction of player
112 -- location:setFacing(facing) sets direction eg location:setFacing(1) (West)
113 -- location:getCompass() returns direction as text eg "north"
114 -- location:setCompass("X") sets direction using text eg location:setCompass("south")
115 -- location:goUp(X) increases Y coord by X
116 -- location:goDown(X) decreases Y coord by X
117 return self
118end
119
120function classTreeFarm()
121 clsTreeFarm = {} -- the table representing the class, which will double as the metatable for any instances
122 clsTreeFarm.__index = clsTreeFarm -- failed table lookups on the instances should fallback to the class table, to get methods
123
124 local self = setmetatable({}, clsTreeFarm)
125
126 self.numSaplings = 0
127 self.timePlanted = 0
128 self.dayPlanted = 0
129 self.timeHarvested = 0
130 self.woodHarvested = 0
131 self.pondFilled = false
132 self.hopperPlaced = false
133 self.farmCreated = false
134 self.farmFlooded = false
135 self.waterFound = false
136
137 function clsTreeFarm.reset(self)
138 self.numSaplings = 0
139 self.timePlanted = 0
140 self.dayPlanted = 0
141 self.timeHarvested = 0
142 self.woodHarvested = 0
143 end
144
145 function clsTreeFarm.getPondFilled(self)
146 return self.pondFilled
147 end
148 function clsTreeFarm.setPondFilled(self, yesNo)
149 self.pondFilled = yesNo
150 end
151
152 function clsTreeFarm.getWaterFound(self)
153 return self.waterFound
154 end
155 function clsTreeFarm.setWaterFound(self, yesNo)
156 self.waterFound = yesNo
157 end
158
159 function clsTreeFarm.getHopperPlaced(self)
160 return self.hopperPlaced
161 end
162 function clsTreeFarm.setHopperPlaced(self, yesNo)
163 self.hopperPlaced = yesNo
164 end
165
166 function clsTreeFarm.getFarmCreated(self)
167 return self.farmCreated
168 end
169 function clsTreeFarm.setFarmCreated(self, yesNo)
170 self.farmCreated = yesNo
171 end
172
173 function clsTreeFarm.getFarmFlooded(self)
174 return self.farmFlooded
175 end
176 function clsTreeFarm.setFarmFlooded(self, yesNo)
177 self.farmFlooded = yesNo
178 end
179
180 function clsTreeFarm.getNumSaplings(self)
181 return self.numSaplings
182 end
183 function clsTreeFarm.setNumSaplings(self, num)
184 self.numSaplings = num
185 end
186 function clsTreeFarm.addSapling(self)
187 self.numSaplings = self.numSaplings + 1
188 end
189 function clsTreeFarm.getTimePlanted(self)
190 return self.timePlanted
191 end
192 function clsTreeFarm.setTimePlanted(self, num)
193 self.timePlanted = num
194 self.dayPlanted = os.day()
195 end
196 function clsTreeFarm.getTimeHarvested(self)
197 return self.timeHarvested
198 end
199 function clsTreeFarm.setTimeHarvested(self, num)
200 self.timeHarvested = num
201 end
202
203 function clsTreeFarm.getWoodHarvested(self)
204 return self.woodHarvested
205 end
206 function clsTreeFarm.setWoodHarvested(self, num)
207 self.woodHarvested = num
208 end
209
210 function clsTreeFarm.getMaxHarvest(self)
211 return self.numSaplings * 5
212 end
213
214 function clsTreeFarm.setDebug(self, saplings, trees)
215 --use if debugging and treeFarm already planted
216 self.numSaplings = saplings + trees
217 self.timePlanted = os.time()
218 if trees >= 6 then
219 self.dayPlanted = os.day() - 3
220 end
221 end
222
223 function clsTreeFarm.getPotentialHarvest(self)
224 local currentDay = os.day()
225 local currentTime = os.time()
226 local numDays = 0
227 local numHours = 0
228 local harvest = 0
229
230 --days measured from world creation
231 numDays = currentDay - self.dayPlanted
232
233 --time 0 - 24 then resets
234 if currentTime > self.timePlanted then -- eg 13.5 > 7.5 (13.5 - 7.5 = 6 hours)
235 numHours = currentTime - self.timePlanted
236 else -- eg 6.465 < 21.454
237 numHours = 24 - self.timePlanted + currentTime -- eg 24 - 21.500 + 6.500 = 9 hours
238 end
239 -- 8 trees planted, half done in 12-14 minecraft hours
240 -- 1 tree 4.5 hrs
241 -- 2 trees 7 hrs
242 -- 3 trees 8 hrs
243 -- 4 trees 13 hrs
244 -- 5 trees 20 hrs
245 -- 6 trees 30 hours
246 -- estimate as half no of saplings per 12 hours
247 if numDays == 0 then
248 harvest = math.ceil((5 * self.numSaplings) / 2)
249 elseif numDays == 1 then
250 harvest = math.ceil(((5 * self.numSaplings) * 2) / 3)
251 else -- > 2 days probably most trees * 5 wood
252 harvest = 5 * self.numSaplings
253 end
254
255 return harvest
256 end
257
258 return self
259end
260
261function classTurtle()
262 clsTurtle = {} -- the table representing the class, which will double as the metatable for any instances
263 clsTurtle.__index = clsTurtle -- failed table lookups on the instances should fallback to the class table, to get methods
264
265 local self = setmetatable({}, clsTurtle)
266 -- class scope variables
267 self.x = 0
268 self.y = 0
269 self.z = 0
270 self.facing = 0
271 self.compass = ""
272 self.equippedLeft = ""
273 self.equippedRight = ""
274 self.placeItem = ""
275 -- logging variables
276 self.useLog = false
277 self.logFileName = ""
278 self.logFileExists = false
279 self.mobAttack = false
280
281 -- helper function for iterating lists
282 function clsTurtle.values(self,t) -- general diy iterator
283 local i = 0
284 return function() i = i + 1; return t[i] end
285 end
286
287 -- logging methods
288 function clsTurtle.getUseLog(self)
289 return self.useLog
290 end
291 function clsTurtle.setUseLog(self, use)
292 self.useLog = use
293 return use
294 end
295 function clsTurtle.getLogExists(self)
296 exists = false
297 if fs.exists(self.logFileName) then
298 exists = true
299 end
300 return exists
301 end
302 function clsTurtle.getLogFileName(self)
303 return self.logFileName
304 end
305 function clsTurtle.setLogFileName(self, value)
306 self.logFileName = value
307 end
308 function clsTurtle.getCurrentFileSize(self)
309 if self.logFileExists then
310 return fs.getSize(self.logFileName)
311 else
312 return 0
313 end
314 end
315 function clsTurtle.deleteLog(self)
316 if fs.exists(self.logFileName) then
317 fs.delete(self.logFileName)
318 end
319 self.logFileExists = false
320
321 return true
322 end
323 function clsTurtle.appendLine(self, newText)
324 local handle = ""
325
326 if fs.exists(self.logFileName) then --logFile already created
327 handle = fs.open(self.logFileName, "a")
328 else
329 handle = fs.open(self.logFileName, "w") --create file
330 end
331 self.logFileExists = true
332 handle.writeLine(newText)
333 handle.close()
334 end
335 function clsTurtle.saveToLog(self, text, toScreen)
336 if toScreen == nil then
337 toScreen = true
338 end
339 if text ~= "" and text ~= nil then
340 if toScreen then
341 print(text)
342 end
343 if self.useLog then
344 clsTurtle.appendLine(self, text)
345 end
346 end
347 end
348
349 -- getters and setters
350 function clsTurtle.getX(self) return self.x end
351 function clsTurtle.setX(self, newVal) self.x = newVal end
352 function clsTurtle.getY(self) return self.y end
353 function clsTurtle.setY(self, newVal) self.y = newVal end
354 function clsTurtle.getZ(self) return self.z end
355 function clsTurtle.setZ(self, newVal) self.z = newVal end
356 function clsTurtle.getFacing(self) return self.facing end
357 function clsTurtle.setFacing(self, newVal)
358 local direction = {"south","west","north","east"}
359 self.facing = newVal
360 if self.facing < 0 then
361 self.facing = 3
362 elseif self.facing > 3 then
363 self.facing = 0
364 end
365 self.compass = direction[self.facing + 1]
366 end
367 function clsTurtle.getCompass(self) return self.compass end
368 function clsTurtle.getPlaceItem(self) return self.placeItem end
369 function clsTurtle.setPlaceItem(self, item, useDamage)
370 local success = false
371 local slot = clsTurtle.getItemSlot(self, item, useDamage)
372 if slot > 0 then
373 self.placeItem = item
374 end
375 end
376 function clsTurtle.getEquipped(self, side)
377 retValue = ""
378 if side == "left" then
379 retValue = self.equippedLeft
380 else
381 retValue = self.equippedRight
382 end
383 return retValue
384 end
385 function clsTurtle.setEquipped(self, side, value)
386 if side == "left" then
387 self.equippedLeft = value
388 elseif side == "right" then
389 self.equippedRight = value
390 end
391 end
392
393 -- change direction and movement methods
394 function clsTurtle.attack(self, direction)
395 direction = direction or "forward"
396 local slot = turtle.getSelectedSlot()
397 turtle.select(1)
398 local success = false
399 local attackLimit = 10 -- won't get in infinite loop attacking a minecart chest
400
401 if direction == "up" then
402 while turtle.attackUp() do --in case mob above
403 sleep(1.5)
404 attackLimit = attackLimit - 1
405 if attackLimit <= 0 then
406 break
407 end
408 end
409 elseif direction == "down" then
410 while turtle.attackDown() do --in case mob below
411 sleep(1.5)
412 attackLimit = attackLimit - 1
413 if attackLimit <= 0 then
414 break
415 end
416 end
417 else
418 while turtle.attack() do --in case mob in front
419 sleep(1.5)
420 attackLimit = attackLimit - 1
421 if attackLimit <= 0 then
422 break
423 end
424 end
425 end
426 if attackLimit > 0 then
427 success = true
428 end
429 turtle.select(slot)
430 return success
431 end
432
433 function clsTurtle.back(self, steps)
434 steps = steps or 1
435 local success = false
436
437 clsTurtle.refuel(self, steps)
438 turtle.select(1)
439 for i = 1, steps do
440 if not turtle.back() then --cant move back
441 clsTurtle.turnRight(self, 2) --face backward direction
442 if clsTurtle.forward(self, 1) then-- will also attack mobs if in the way
443 success = true
444 clsTurtle.changeCoords(self, "back")
445 end
446 clsTurtle.turnRight(self, 2)
447 end
448 end
449
450 return success
451 end
452
453 function clsTurtle.changeCoords(self, direction)
454 -- 0 = go south (z increases)
455 -- 1 = go west (x decreases)
456 -- 2 = go north (z decreases
457 -- 3 = go east (x increases)
458 if direction == "forward" then
459 if self.facing == 0 then
460 self.z = self.z + 1
461 elseif self.facing == 1 then
462 self.x = self.x - 1
463 elseif self.facing == 2 then
464 self.z = self.z - 1
465 else
466 self.x = self.x + 1
467 end
468 elseif direction == "back" then
469 if self.facing == 0 then
470 self.z = self.z - 1
471 elseif self.facing == 1 then
472 self.x = self.x + 1
473 elseif self.facing == 2 then
474 self.z = self.z + 1
475 else
476 self.x = self.x - 1
477 end
478 end
479 end
480
481 function clsTurtle.down(self, steps)
482 steps = steps or 1
483 local success = false
484
485 clsTurtle.refuel(self, steps)
486 turtle.select(1)
487 for i = 1, steps do
488 if turtle.detectDown() then -- block below
489 if clsTurtle.getBlockType(self, "down") == "minecraft:bedrock" then
490 break
491 else
492 turtle.digDown()
493 end
494 end
495 if turtle.down() then --move down unless mob in the way
496 success = true
497 else -- not bedrock, could be mob or minecart
498 if clsTurtle.attack(self, "down") then -- attack succeeded
499 if turtle.down() then
500 success = true
501 end
502 end
503 end
504 if success then
505 self.y = self.y - 1
506 end
507 end
508
509 return success
510 end
511
512 function clsTurtle.forward(self, steps)
513 steps = steps or 1
514 local result, data
515 local success = true
516 local retryMove = 10
517
518 clsTurtle.refuel(self, steps)
519 turtle.select(1)
520 for i = 1, steps do
521 while turtle.detect() do -- allow for sand/gravel falling
522 if not clsTurtle.dig(self, "forward") then-- cannot dig. either bedrock or in server protected area
523 success = false
524 if clsTurtle.getBlockType(self, "forward") == "minecraft:bedrock" then
525 print("Bedrock in front:function exit")
526 end
527 break
528 end
529 end
530 success = false
531 if turtle.forward() then
532 success = true
533 else
534 if self.mobAttack then
535 if clsTurtle.attack(self, "forward") then
536 if turtle.forward() then
537 success = true
538 end
539 else
540 result, data = turtle.forward()
541 print(data..":function exit")
542 break
543 end
544 else
545 while not turtle.forward() do
546 clsTurtle.saveToLog(self, "Waiting for mob to leave...", true)
547 sleep(5)
548 retryMove = retryMove - 1
549 if retryMove <= 0 then
550 break
551 end
552 end
553 if retryMove <= 0 then
554 if clsTurtle.attack(self, "forward") then
555 if turtle.forward() then
556 success = true
557 end
558 else
559 result, data = turtle.forward()
560 print(data..":function exit")
561 break
562 end
563 end
564 end
565 end
566 if success then
567 clsTurtle.changeCoords(self, "forward")
568 end
569 end
570 return success
571 end
572
573 function clsTurtle.turnLeft(self, steps)
574 steps = steps or 1
575 for i = 1, steps do
576 turtle.turnLeft()
577 self.facing = self.facing - 1
578 if self.facing < 0 then
579 self.facing = 3
580 end
581 end
582 end
583
584 function clsTurtle.turnRight(self, steps)
585 steps = steps or 1
586 for i = 1, steps do
587 turtle.turnRight()
588 self.facing = self.facing + 1
589 if self.facing > 3 then
590 self.facing = 0
591 end
592 end
593 end
594
595 function clsTurtle.up(self, steps)
596 steps = steps or 1
597 local success = false
598 clsTurtle.refuel(self, steps)
599 turtle.select(1)
600
601 for i = 1, steps do
602 if turtle.detectUp() then -- block above
603 if clsTurtle.getBlockType(self, "up") == "minecraft:bedrock" then
604 print("Bedrock above:function exit")
605 break
606 else
607 clsTurtle.dig(self, "up")
608 end
609 end
610 if turtle.up() then --move up unless mob in the way
611 success = true
612 else
613 if clsTurtle.attack(self, "up") then -- attack succeeded
614 if turtle.up() then
615 success = true
616 end
617 end
618 end
619 if success then
620 self.y = self.y + 1
621 end
622 end
623 return success
624 end
625
626 -- other methods
627 function clsTurtle.clear(self)
628 term.clear()
629 term.setCursorPos(1,1)
630 end
631
632 function clsTurtle.checkInventoryForItem(self, item, minQuantity, altItem, altMinQuantity)
633 -- request player to add items to turtle inventory
634 clsTurtle.clear(self)
635 minQuantity = minQuantity or 1
636 altItem = altItem or ""
637 altMinQuantity = altMinQuantity or 0
638 local gotItemSlot = 0
639 local gotAltItemSlot = 0
640 local damage = 0
641 local count = 0
642 print("Add "..item.." to any single slot "..minQuantity.." needed")
643 if altItem ~= "" then
644 print("Or add "..altItem.." to any slot "..altMinQuantity.." needed")
645 end
646 while (count < minQuantity + altMinQuantity) do
647 gotItemSlot, damage, count = clsTurtle.getItemSlot(self, item, -1) -- 0 if not present. return slotData.leastSlot, slotData.leastSlotDamage, total, slotData -- first slot no, integer, integer, table
648 if gotItemSlot > 0 and count >= minQuantity then
649 break
650 else
651 sleep(0.5)
652 end
653 if altItem ~= "" then
654 gotAltItemSlot, damage, count = clsTurtle.getItemSlot(self, altItem, -1)
655 if gotAltItemSlot > 0 and count >= altMinQuantity then
656 break
657 else
658 sleep(0.5)
659 end
660 end
661 coroutine.yield()
662 end
663 end
664
665 function clsTurtle.craft(self, craftItem, craftQuantity, sourceItem1, sourceItem2, sourceItem3, refuel)
666 --Examples:
667 --make planks: T:craft("minecraft:planks", 8, "minecraft:log", nil, nil, false)
668 --make planks: clsTurtle.craft(self, "minecraft:planks", 8, "minecraft:log", nil, nil, false)
669 --make planks: T:craft{craftItem = "minecraft:planks", craftQuantity = 8, sourceItem1 = "minecraft:log"}
670 --make 1 chest: T:craft{craftItem = "minecraft:chest", craftQuantity = 1, sourceItem1 = "minecraft:planks"}
671 --late stage: T:craft{craftItem = "ccComputer", craftQuantity = 1, sourceItem1 = "minecraft:glass_pane", sourceItem2 = "minecraft:redstone", sourceItem3 = "minecraft:stone"}
672
673 local sourceSlot1 = 0
674 local sourceSlot2 = 0
675 local sourceSlot3 = 0
676 local sourceSlot4 = 0
677 local success = false
678 local emptySlot = 0
679 local turns = 0
680 local doContinue = true
681 local blockType = ""
682 local stockData = {}
683 local slot = turtle.getSelectedSlot()
684 local facing = self.facing
685 local chestDirection = "forward"
686 turtle.select(1)
687 -- check if at least 1 spare slot
688 if clsTurtle.getFirstEmptySlot(self) == 0 then
689 clsTurtle.emptyTrash(self, "down")
690 if clsTurtle.getFirstEmptySlot(self) == 0 then
691 turtle.select(clsTurtle.getItemSlot(self, "minecraft:cobblestone"))
692 turtle.dropUp()
693 end
694 end
695
696 if clsTurtle.getItemSlot(self, "minecraft:chest", -1) == 0 then -- chest not found
697 if craftItem == "minecraft:chest" then --make a chest
698 sourceSlot1 = clsTurtle.getItemSlot(self, sourceItem1)
699 turtle.select(1)
700 if turtle.craft() then
701 turtle.transferTo(2, 1)
702 turtle.transferTo(3, 1)
703 turtle.transferTo(5, 1)
704 turtle.transferTo(7, 1)
705 turtle.transferTo(9, 1)
706 turtle.transferTo(10, 1)
707 turtle.transferTo(11, 1)
708 if turtle.craft() then
709 clsTurtle:saveToLog("First Chest crafted")
710 end
711 end
712 end
713 doContinue = false
714 end
715 if doContinue then
716 if sourceItem1 == "minecraft:log" or sourceItem1 == "minecraft:log2" then
717 stockData = clsTurtle.getLogData(self) --{.total, .mostSlot, .mostCount, .mostModifier, mostName, .leastSlot, .leastCount, .leastModifier, leastName}
718 clsTurtle:saveToLog("craft(): stock.mSlot="..stockData.mostSlot..", stock.mCount="..stockData.mostCount..", stock.lSlot="..stockData.leastSlot..", stock.lCount="..stockData.leastCount)
719 if refuel then
720 sourceSlot1 = stockData.leastSlot
721 clsTurtle:saveToLog("craft(): slot no: "..stockData.leastSlot..", log count:"..stockData.leastCount)
722 else
723 if craftItem == "minecraft:planks" then
724 if stockData.leastCount >= craftQuantity / 4 then -- check if enough in lowest slot
725 sourceSlot1 = stockData.leastSlot
726 clsTurtle.saveToLog(self, "craft(): slot no: "..stockData.leastSlot..", log count:"..stockData.leastCount)
727 else
728 sourceSlot1 = stockData.mostSlot
729 clsTurtle.saveToLog(self, "craft(): slot no: "..stockData.mostSlot..", log count:"..stockData.mostCount)
730 end
731 end
732 end
733 else
734 sourceSlot1 = clsTurtle.getItemSlot(self, sourceItem1)
735 end
736 if sourceSlot1 == 0 then
737 doContinue = false
738 end
739 if sourceItem2 == nil then -- no second recipe item needed
740 sourceItem2 = ""
741 sourceSlot2 = 0
742 else
743 sourceSlot2 = clsTurtle.getItemSlot(self, sourceItem2) -- find slot number of second recipe item
744 if sourceSlot2 == 0 then
745 doContinue = false
746 end
747 end
748 if sourceItem3 == nil then -- no third recipe item needed
749 sourceItem3 = ""
750 sourceSlot3 = 0
751 else
752 sourceSlot3 = clsTurtle.getItemSlot(self, sourceItem3) -- find slot number of third recipe item
753 if sourceSlot3 == 0 then
754 doContinue = false
755 end
756 end
757 end
758 if doContinue then
759 chestDirection = clsTurtle.getPlaceChestDirection(self)
760 blockType = clsTurtle.getBlockType(self, chestDirection)
761 while blockType ~= "minecraft:chest" do --check if chest placed eg mob in the way
762 clsTurtle.dig(self, chestDirection)
763 if not clsTurtle.place(self, "minecraft:chest", -1, chestDirection) then
764 sleep(1) -- wait for mob to move
765 chestDirection = clsTurtle.getPlaceChestDirection(self)
766 end
767 blockType = clsTurtle.getBlockType(self, chestDirection)
768 end
769 -- fill chest with everything except required items
770 for i = 1, 16 do
771 if i ~= sourceSlot1 and i ~= sourceSlot2 and i ~= sourceSlot3 then
772 clsTurtle.drop(self, i, chestDirection)
773 end
774 end
775
776 --turtle emptied out except for crafting items, so move them to slots 16,15,14,13. Start by moving them all to empty slots
777 emptySlot = clsTurtle.getFirstEmptySlot(self)
778 turtle.select(sourceSlot1)
779 turtle.transferTo(emptySlot) --move selected first item to empty slot
780 sourceSlot1 = emptySlot
781 if sourceSlot2 > 0 then
782 emptySlot = clsTurtle.getFirstEmptySlot(self)
783 turtle.select(sourceSlot2)
784 turtle.transferTo(emptySlot) --move selected first item to empty slot
785 sourceSlot2 = emptySlot
786 end
787 if sourceSlot3 > 0 then
788 emptySlot = clsTurtle.getFirstEmptySlot(self)
789 turtle.select(sourceSlot3)
790 turtle.transferTo(emptySlot) --move selected first item to empty slot
791 sourceSlot3 = emptySlot
792 end
793 if sourceSlot4 > 0 then
794 emptySlot = clsTurtle.getFirstEmptySlot(self)
795 turtle.select(sourceSlot4)
796 turtle.transferTo(emptySlot) --move selected first item to empty slot
797 sourceSlot4 = emptySlot
798 end
799 --now move to 16, (15,14,13)
800 turtle.select(sourceSlot1)
801 turtle.transferTo(16) --move selected first item to empty slot
802 sourceSlot1 = 16
803 if sourceSlot2 > 0 then
804 turtle.select(sourceSlot2)
805 turtle.transferTo(15) --move selected first item to empty slot
806 sourceSlot1 = 15
807 end
808 if sourceSlot3 > 0 then
809 turtle.select(sourceSlot3)
810 turtle.transferTo(14) --move selected first item to empty slot
811 sourceSlot1 = 14
812 end
813 if sourceSlot4 > 0 then
814 turtle.select(sourceSlot4)
815 turtle.transferTo(13) --move selected first item to empty slot
816 sourceSlot1 = 13
817 end
818 --ready to craft
819 turtle.select(16)
820 if craftItem == ccComputer then --T:craft{craftItem = ccComputer, craftQuantity = 1, sourceItem1 = "minecraft:glass_panes", sourceItem2 = "minecraft:redstone",sourceItem3 = "minecraft:stone"}
821 -- 1 glass_panes, 1 redstone, 7 stone
822 turtle.select(16)
823 turtle.transferTo(10, craftQuantity) --move glass panes to 10
824 turtle.select(15)
825 turtle.transferTo(6, craftQuantity) --move redstone to 6
826 turtle.select(14) --stone
827 turtle.transferTo(1, craftQuantity) --move stone to 6
828 turtle.transferTo(2, craftQuantity)
829 turtle.transferTo(3, craftQuantity)
830 turtle.transferTo(5, craftQuantity)
831 turtle.transferTo(7, craftQuantity)
832 turtle.transferTo(9, craftQuantity)
833 turtle.transferTo(11, craftQuantity)
834 elseif craftItem == ccDisk then --T:craft{craftItem = ccDisk, craftQuantity = 1, sourceItem1 = "minecraft:paper", sourceItem2 = "minecraft:redstone"}
835 -- 1 paper, 1 redstone
836 turtle.select(16)
837 turtle.transferTo(5, craftQuantity) --move paper to 5
838 turtle.select(15)
839 turtle.transferTo(1, craftQuantity) --move redstone to 1
840 elseif craftItem == ccDiskDrive then --T:craft{craftItem = ccDiskDrive, craftQuantity = 1, sourceItem1 = "minecraft:redstone", sourceItem2 = "minecraft:stone"}
841 -- 2 redstone, 7 stone
842 turtle.select(16)
843 turtle.transferTo(6, craftQuantity) --move to 6
844 turtle.transferTo(10, craftQuantity) --move to 10
845 turtle.select(15)
846 turtle.transferTo(1, craftQuantity) --move stone to 6
847 turtle.transferTo(2, craftQuantity)
848 turtle.transferTo(3, craftQuantity)
849 turtle.transferTo(5, craftQuantity)
850 turtle.transferTo(7, craftQuantity)
851 turtle.transferTo(9, craftQuantity)
852 turtle.transferTo(11, craftQuantity)
853 elseif craftItem == ccTurtle then --T:craft{craftItem = ccTurtle, craftQuantity = 1, sourceItem1 = "minecraft:chest", sourceItem2 = ccComputer,sourceItem3 = "minecraft:iron_ingot"}
854 -- 1 chest, 1 computer, 7 iron
855 if sourceSlot2 ~= 15 then
856 turtle.select(sourceSlot2) --computer
857 turtle.transferTo(15, craftQuantity)
858 end
859 if sourceSlot3 ~= 14 then
860 turtle.select(sourceSlot3) --iron
861 turtle.transferTo(14)
862 end
863 turtle.select(16)
864 turtle.transferTo(10, craftQuantity)
865 turtle.select(15) --computer
866 turtle.transferTo(6, craftQuantity)
867 turtle.select(14) --iron
868 turtle.transferTo(1, craftQuantity) --move iron
869 turtle.transferTo(2, craftQuantity)
870 turtle.transferTo(3, craftQuantity)
871 turtle.transferTo(5, craftQuantity)
872 turtle.transferTo(7, craftQuantity)
873 turtle.transferTo(9, craftQuantity)
874 turtle.transferTo(11, craftQuantity)
875 elseif craftItem == ccCraftyMiningTurtle then --T:craft{craftItem = ccCraftyMiningTurtle, craftQuantity = 1, sourceItem1 = "minecraft:crafting_table", sourceItem2 = "minecraft:diamond_pickaxe",sourceItem3 = ccTurtle}
876 -- 1 crafting table, 1 diamond pickaxe, 1 turtle
877 turtle.select(16)
878 turtle.transferTo(1, craftQuantity) --move crafting table to 1
879 turtle.select(15)
880 turtle.transferTo(3, craftQuantity) --move pickaxe to 3
881 turtle.select(14)
882 turtle.transferTo(2, craftQuantity) --move turtle to 2
883 elseif craftItem == "minecraft:bucket" then --T:craft{craftItem = "minecraft:bucket", craftQuantity = 1, sourceItem1 = "minecraft:iron_ingot"}
884 turtle.transferTo(1, craftQuantity)
885 turtle.transferTo(3, craftQuantity)
886 turtle.transferTo(6, craftQuantity)
887 elseif craftItem == "minecraft:chest" then --T:craft{craftItem = "minecraft:chest", craftQuantity = 1, sourceItem1 = "minecraft:planks"}
888 --8 planks = 1 chest
889 turtle.transferTo(1, craftQuantity)
890 turtle.transferTo(2, craftQuantity)
891 turtle.transferTo(3, craftQuantity)
892 turtle.transferTo(5, craftQuantity)
893 turtle.transferTo(7, craftQuantity)
894 turtle.transferTo(9, craftQuantity)
895 turtle.transferTo(10, craftQuantity)
896 turtle.transferTo(11, craftQuantity)
897 elseif craftItem == "minecraft:crafting_table" then --T:craft{craftItem = "minecraft:crafting_table", craftQuantity = 1, sourceItem1 = "minecraft:planks"}
898 -- 4 planks = 1 table
899 turtle.transferTo(1, craftQuantity)
900 turtle.transferTo(2, craftQuantity)
901 turtle.transferTo(5, craftQuantity)
902 turtle.transferTo(6, craftQuantity)
903 elseif craftItem == "minecraft:diamond_pickaxe" then --T:craft{craftItem = "minecraft:diamond_pickaxe", craftQuantity = 1, sourceItem1 = "minecraft:stick", sourceItem2 = "minecraft:diamond"}
904 --2 sticks + 3 diamonds = 1 axe
905 turtle.select(16)
906 turtle.transferTo(6, craftQuantity) --move stick to 6
907 turtle.transferTo(10, craftQuantity) --move stick to 10
908 turtle.select(15)
909 turtle.transferTo(1, craftQuantity) --move diamond to 1
910 turtle.transferTo(2, craftQuantity) --move diamond to 2
911 turtle.transferTo(3, craftQuantity) --move diamond to 3
912 elseif craftItem == "minecraft:fence" then --T:craft{craftItem = "minecraft:fence", craftQuantity = 2, sourceItem1 = "minecraft:stick"}
913 -- 6 stick = 2 fences
914 turtle.transferTo(5, craftQuantity / 2)
915 turtle.transferTo(6, craftQuantity / 2)
916 turtle.transferTo(7, craftQuantity / 2)
917 turtle.transferTo(9, craftQuantity / 2)
918 turtle.transferTo(10, craftQuantity / 2)
919 turtle.transferTo(11, craftQuantity / 2)
920 elseif craftItem == "minecraft:furnace" then --T:craft{craftItem = "minecraft:furnace", craftQuantity = 1, sourceItem1 = "minecraft:cobblestone"}
921 -- 8 cobblestone = 1 furnace
922 turtle.transferTo(1, craftQuantity)
923 turtle.transferTo(2, craftQuantity)
924 turtle.transferTo(3, craftQuantity)
925 turtle.transferTo(5, craftQuantity)
926 turtle.transferTo(7, craftQuantity)
927 turtle.transferTo(9, craftQuantity)
928 turtle.transferTo(10, craftQuantity)
929 turtle.transferTo(11, craftQuantity)
930 elseif craftItem == "minecraft:glass_pane" then --T:craft{craftItem = "minecraft:glass_pane", craftQuantity = 16, sourceItem1 = "minecraft:glass"}
931 -- 6 glass = 16 panes
932 turtle.transferTo(5, craftQuantity / 16)
933 turtle.transferTo(6, craftQuantity / 16)
934 turtle.transferTo(7, craftQuantity / 16)
935 turtle.transferTo(9, craftQuantity / 16)
936 turtle.transferTo(10, craftQuantity / 16)
937 turtle.transferTo(11, craftQuantity / 16)
938 elseif craftItem == "minecraft:hopper" then --T:craft{craftItem = "minecraft:hopper", craftQuantity = 1, sourceItem1 = "minecraft:iron_ingot", sourceItem2 = "minecraft:chest"}
939 -- 5 iron, 1 chest
940 turtle.select(16)
941 turtle.transferTo(1, craftQuantity) --move iron to 1
942 turtle.transferTo(3, craftQuantity)
943 turtle.transferTo(5, craftQuantity)
944 turtle.transferTo(7, craftQuantity)
945 turtle.transferTo(10, craftQuantity)
946 turtle.select(15)
947 turtle.transferTo(6, craftQuantity) --move chest to 6
948 elseif craftItem == "minecraft:ladder" then --T:craft{craftItem = "minecraft:ladder", craftQuantity = 3, sourceItem1 = "minecraft:stick"}
949 -- 7 stick = 3 ladder
950 turtle.transferTo(1, craftQuantity / 3)
951 turtle.transferTo(3, craftQuantity / 3)
952 turtle.transferTo(5, craftQuantity / 3)
953 turtle.transferTo(6, craftQuantity / 3)
954 turtle.transferTo(7, craftQuantity / 3)
955 turtle.transferTo(9, craftQuantity / 3)
956 turtle.transferTo(11, craftQuantity / 3)
957 elseif craftItem == "minecraft:paper" then --T:craft{craftItem = "minecraft:paper", craftQuantity = 3, sourceItem1 = "minecraft:reeds"}
958 --3 reeds = 3 paper
959 turtle.transferTo(1, craftQuantity / 3)
960 turtle.transferTo(2, craftQuantity / 3)
961 turtle.transferTo(3, craftQuantity / 3)
962 elseif craftItem == "minecraft:planks" then --T:craft{craftItem = "minecraft:planks", craftQuantity = 4, sourceItem1 = "minecraft:log"}
963 turtle.transferTo(1, craftQuantity / 4)
964 elseif craftItem == "minecraft:sign" then --T:craft{craftItem = "minecraft:sign", craftQuantity = 3, sourceItem1 = "minecraft:stick", sourceItem2 = "minecraft:planks"}
965 -- 1 stick + 6 planks = 3 sign
966 if sourceSlot2 ~= 15 then
967 turtle.select(sourceSlot2) -- move planks to 15
968 turtle.transferTo(15)
969 end
970 turtle.select(16)
971 turtle.transferTo(10, craftQuantity / 3) --move sticks to 5
972 turtle.select(15)
973 turtle.transferTo(1, craftQuantity / 3) --move planks to 1
974 turtle.transferTo(2, craftQuantity / 3) --move planks to 2
975 turtle.transferTo(3, craftQuantity / 3) --move planks to 3
976 turtle.transferTo(5, craftQuantity / 3) --move planks to 5
977 turtle.transferTo(6, craftQuantity / 3) --move planks to 6
978 turtle.transferTo(7, craftQuantity / 3) --move planks to 7
979 elseif craftItem == "minecraft:stair" then --T:craft{craftItem = "minecraft:stair", craftQuantity = 4, sourceItem1 = "minecraft:cobblestone"}
980 --6 cobblestone = 4 stair
981 turtle.transferTo(1, craftQuantity / 4)
982 turtle.transferTo(5, craftQuantity / 4)
983 turtle.transferTo(6, craftQuantity / 4)
984 turtle.transferTo(9, craftQuantity / 4)
985 turtle.transferTo(10, craftQuantity / 4)
986 turtle.transferTo(11, craftQuantity / 4)
987 elseif craftItem == "minecraft:stick" then --T:craft{craftItem = "stick", craftQuantity = 4, sourceItem1 = "minecraft:planks"}
988 -- 2 planks gives 4 sticks
989 turtle.transferTo(1, craftQuantity / 4)
990 turtle.transferTo(5, craftQuantity / 4)
991 elseif craftItem == "minecraft:torch" then --T:craft{craftItem = "minecraft:torch", craftQuantity = 4, sourceItem1 = "minecraft:stick", sourceItem2 = "minecraft:coal"}
992 -- 1 stick + 1 coal/charcoal = 4 torch
993 if sourceSlot2 ~= 15 then
994 turtle.select(sourceSlot2) -- move coal/charcoal to 15
995 turtle.transferTo(15)
996 end
997 turtle.select(16)
998 turtle.transferTo(5, craftQuantity / 4) --move sticks to 5
999 turtle.select(15)
1000 turtle.transferTo(1, craftQuantity / 4) --move coal/charcoal to
1001 end
1002 for i = 13, 16 do -- empty remaining recipe items into chest
1003 clsTurtle.drop(self, i, chestDirection)
1004 end
1005 -- Attempt to craft item into slot 16
1006 turtle.select(16)
1007 if turtle.craft() then
1008 success = true
1009 --now put crafted item in chest first, so will mix with any existing similar items
1010 if refuel then
1011 turtle.select(16)
1012 turtle.refuel()
1013 else
1014 clsTurtle.drop(self, 16, chestDirection)
1015 end
1016 else --crafting not successful, so empty out all items into chest
1017 for i = 1, 16 do
1018 if turtle.getItemCount(i) > 0 then
1019 clsTurtle.drop(self, i, chestDirection)
1020 end
1021 end
1022 end
1023 while clsTurtle.suck(self, chestDirection) do end
1024 clsTurtle.dig(self, chestDirection) -- collect chest
1025 while facing ~= self.facing do --return to original position
1026 clsTurtle.turnLeft(self, 1)
1027 end
1028 else
1029 clsTurtle.saveToLog(self, "Missing crafting ingredients.", true)
1030 success = false
1031 end
1032 turtle.select(slot)
1033 return success
1034 end
1035
1036 function clsTurtle.craftChests(self, quantity, orLess)
1037 local chestsCrafted = 0
1038 local stockData = {}
1039 stockData = clsTurtle.getLogData(self)--{.total, .mostSlot, .mostCount, .mostModifier, mostName, .leastSlot, .leastCount, .leastModifier, leastName}
1040 local logNum = stockData.total
1041
1042 if logNum >= 2 then -- at least one chest can be made
1043 if stockData.mostCount >= quantity * 2 then
1044 if quantity > 8 then
1045 clsTurtle.craft(self, "minecraft:planks", 64, stockData.mostName, nil, nil, false)
1046 clsTurtle.craft(self, "minecraft:chest", 8, "minecraft:planks", nil, nil, false)
1047 quantity = quantity - 8
1048 end
1049 stockData = clsTurtle.getLogData(self)
1050 clsTurtle.craft(self, "minecraft:planks", quantity * 8, stockData.mostName, nil, nil, false)
1051 clsTurtle.craft(self, "minecraft:chest", quantity, "minecraft:planks", nil, nil, false)
1052 chestsCrafted = quantity
1053 else -- less than required logs
1054 if orLess then
1055 logNum = (math.floor(stockData.mostCount / 2)) * 2
1056 logNeeded = quantity * 2 - logNum
1057 clsTurtle.craft(self, "minecraft:planks", logNum * 4, stockData.mostName, nil, nil, false)
1058 clsTurtle.craft(self, "minecraft:chest", logNum / 2, "minecraft:planks", nil, nil, false)
1059 stockData = clsTurtle.getLogData(self)
1060 logNum = (math.floor(stockData.mostCount / 2)) * 2
1061 if logNum >= logNeeded then
1062 clsTurtle.craft(self, "minecraft:planks", logNeeded * 4, stockData.mostName, nil, nil, false)
1063 clsTurtle.craft(self, "minecraft:chest", logNeeded / 2, "minecraft:planks", nil, nil, false)
1064 else
1065 clsTurtle.craft(self, "minecraft:planks", logNum * 4, stockData.mostName, nil, nil, false)
1066 clsTurtle.craft(self, "minecraft:chest", logNum / 2, "minecraft:planks", nil, nil, false)
1067 end
1068 chestsCrafted = logNeeded / 2
1069 end
1070 end
1071 end
1072
1073 return chestsCrafted
1074 end
1075
1076 function clsTurtle.dig(self, direction)
1077 direction = direction or "forward"
1078 local success = false
1079 turtle.select(1)
1080 if direction == "up" then
1081 while turtle.digUp() do
1082 sleep(0.7)
1083 success = true
1084 end
1085 elseif direction == "down" then
1086 if turtle.digDown() then
1087 success = true
1088 end
1089 else -- forward by default
1090 while turtle.dig() do
1091 sleep(0.7)
1092 success = true
1093 end
1094 end
1095 return success
1096 end
1097
1098 function clsTurtle.drop(self, slot, direction, amount)
1099 direction = direction or "forward"
1100 turtle.select(slot)
1101 local success = false
1102 if direction == "up" then
1103 if amount == nil then
1104 if turtle.dropUp() then
1105 success = true
1106 end
1107 else
1108 if turtle.dropUp(amount) then
1109 success = true
1110 end
1111 end
1112 elseif direction == "down" then
1113 if amount == nil then
1114 if turtle.dropDown() then
1115 success = true
1116 end
1117 else
1118 if turtle.dropDown(amount) then
1119 success = true
1120 end
1121 end
1122 else
1123 if amount == nil then
1124 if turtle.drop() then
1125 success = true
1126 end
1127 else
1128 if turtle.drop(amount) then
1129 success = true
1130 end
1131 end
1132 end
1133 return success
1134 end
1135
1136 function clsTurtle.dropItem(self, item, keepAmount, direction)
1137 keepAmount = keepAmount or 0
1138 direction = direction or "down"
1139 local itemSlot = 0
1140 local stockData = {}
1141
1142 if keepAmount <= 0 then -- drop everything
1143 itemSlot = clsTurtle.getItemSlot(self, item)
1144 while itemSlot > 0 do
1145 clsTurtle.drop(self, itemSlot, direction)
1146 itemSlot = clsTurtle.getItemSlot(self, item)
1147 end
1148 elseif keepAmount >= 64 then -- drop everything else
1149 stockData = clsTurtle.getStock(self, item)
1150 if item == "minecraft:log" then
1151 if stockData.mostSlot ~= stockData.leastSlot then
1152 clsTurtle.drop(self, stockData.leastSlot, direction)
1153 end
1154 else
1155 while stockData.total > keepAmount do
1156 if stockData.mostSlot ~= stockData.leastSlot then
1157 clsTurtle.drop(self, stockData.leastSlot, direction)
1158 else --only one slot but more than required in it
1159 clsTurtle.drop(self, stockData.mostSlot, direction)
1160 end
1161 stockData = clsTurtle.getStock(self, item)
1162 end
1163 end
1164 else --store specific amount
1165 itemSlot = clsTurtle.getItemSlot(self, item)
1166 local dropCount = turtle.getItemCount(itemSlot) - keepAmount
1167 if itemSlot > 0 then
1168 clsTurtle.drop(self, itemSlot, direction, dropCount)
1169 end
1170 end
1171 end
1172
1173 function clsTurtle.dumpRefuse(self)
1174 --dump dirt, cobble, sand, gravel
1175 local itemlist = {}
1176 local blockType = ""
1177 local blockModifier
1178 local slotCount
1179 local cobbleCount = 0
1180 local dirtCount = 0
1181
1182 itemlist[1] = "minecraft:gravel"
1183 itemlist[2] = "minecraft:stone" --includes andesite, diorite etc
1184 itemlist[3] = "minecraft:dirt"
1185 itemlist[4] = "minecraft:flint"
1186 for x = 1, 15 do
1187 for i = x + 1 , 16 do
1188 if turtle.getItemCount(i) > 0 then
1189 turtle.select(i)
1190 if turtle.compareTo(x) then
1191 turtle.transferTo(x)
1192 end
1193 end
1194 end
1195 end
1196 for i = 1, 16 do
1197 slotCount, blockType, blockModifier = clsTurtle.getSlotContains(self, i)
1198 if blockType == "minecraft:cobblestone" then
1199 if cobbleCount > 0 then
1200 turtle.select(i)
1201 turtle.drop()
1202 else
1203 cobbleCount = cobbleCount + 1
1204 end
1205 end
1206 for j = 1, 4 do
1207 if blockType == itemlist[j] then
1208 turtle.select(i)
1209 turtle.drop()
1210 break
1211 end
1212 end
1213 end
1214 turtle.select(1)
1215 end
1216
1217 function clsTurtle.emptyTrash(self, direction)
1218 direction = direction or "down"
1219 local slotData = {}
1220 local item = ""
1221 local move = false
1222 local keepItems = {"minecraft:cobblestone", "minecraft:redstone", "minecraft:sand",
1223 "minecraft:chest", "minecraft:log", "minecraft:log2", "minecraft:iron_ore", "minecraft:reeds", "minecraft:sapling",
1224 "minecraft:bucket", "minecraft:lava_bucket", "minecraft:water_bucket", "minecraft:torch", "minecraft:diamond",
1225 "minecraft:coal", "minecraft:iron_ingot"}
1226
1227 local keepit = false
1228 -- empty excess cobble, dirt, all gravel, unknown minerals
1229 --keep max of 1 stack
1230 clsTurtle.sortInventory(self)
1231 if direction == "down" then
1232 if clsTurtle.down(self, 1) then
1233 move = true
1234 end
1235 end
1236 for i = 1, 16 do
1237 keepit = false
1238 if turtle.getItemCount(i) > 0 then
1239 item = clsTurtle.getItemName(self, i)
1240 for _,v in pairs(keepItems) do
1241 if v == item then
1242 keepit = true
1243 break
1244 end
1245 end
1246 if not keepit then
1247 clsTurtle.saveToLog(self, "EmptyTrash():Dropping "..item, true)
1248 turtle.select(i)
1249 turtle.dropDown()
1250 end
1251 end
1252 end
1253 clsTurtle.sortInventory(self)
1254 clsTurtle.emptyTrashItem(self, "down", "minecraft:cobblestone", 128)
1255 clsTurtle.emptyTrashItem(self, "down", "minecraft:redstone", 64)
1256 slotData = clsTurtle.getStock(self, "minecraft:coal", 0)
1257 if slotData.total > 64 then
1258 if slotData.mostSlot ~= slotData.leastSlot and slotData.leastSlot ~= 0 then
1259 turtle.select(slotData.leastSlot)
1260 turtle.refuel()
1261 clsTurtle.saveToLog(self, "EmptyTrash():xs coal used for fuel", true)
1262 end
1263 end
1264 if direction == "down" and move then
1265 clsTurtle.up(self, 1)
1266 end
1267 turtle.select(1)
1268 end
1269
1270 function clsTurtle.emptyTrashItem(self, direction, item, maxAmount)
1271 local stockData = clsTurtle.getStock(self, item)
1272
1273 while stockData.total > maxAmount and stockData.mostSlot > 0 do
1274 turtle.select(stockData.mostSlot)
1275 if direction == "up" then
1276 if not turtle.dropUp() then
1277 if not turtle.dropDown() then
1278 clsTurtle.saveToLog(self, "EmptyTrashItem():error ", true)
1279 break
1280 end
1281 end
1282 else
1283 if not turtle.dropDown() then
1284 if not turtle.dropUp() then
1285 clsTurtle.saveToLog(self, "EmptyTrashItem():error ", true)
1286 break
1287 end
1288 end
1289 end
1290 stockData = clsTurtle.getStock(self, item)
1291 end
1292 end
1293
1294 function clsTurtle.equip(self, side, useItem, useDamage)
1295 useDamage = useDamage or 0
1296 local slot, damage = clsTurtle.getItemSlot(self, useItem)
1297 local currentSlot = turtle.getSelectedSlot()
1298 self.success = false
1299
1300 if slot > 0 and damage == useDamage then
1301 turtle.select(slot)
1302 if side == "right" then
1303 if turtle.equipRight() then
1304 self.success = true
1305 self.equippedRight = useItem
1306 end
1307 else
1308 if turtle.equipLeft() then
1309 self.success = true
1310 self.equippedLeft = useItem
1311 end
1312 end
1313 end
1314 turtle.select(currentSlot)
1315
1316 return self.success
1317 end
1318
1319 function clsTurtle.getBlockType(self, direction)
1320 -- turtle.inspect() returns two values
1321 -- 1) boolean (true/false) success
1322 -- 2) table with two values:
1323 -- .name (string) e.g. "minecraft:log"
1324 -- .metadata (integer) e.g. 0
1325 -- oak has metadata of 0, spruce 1, birch 2 etc
1326 local data = {} --initialise empty table variable
1327
1328 if direction == "up" then
1329 success, data = turtle.inspectUp() -- store information about the block above in a table
1330 elseif direction == "down" then
1331 success, data = turtle.inspectDown() -- store information about the block below in a table
1332 else
1333 success, data = turtle.inspect() -- store information about the block ahead in a table
1334 end
1335
1336 return data.name, data.metadata -- returns nil, nil if no block
1337 end
1338
1339 function clsTurtle.getFirstEmptySlot(self)
1340 local emptySlot = 0
1341 for i = 1,16 do
1342 if turtle.getItemCount(i) == 0 then
1343 emptySlot = i
1344 break
1345 end
1346 end
1347 return emptySlot
1348 end
1349
1350 function clsTurtle.getItemName(self, slot)
1351 local data = {} --initialise empty table variable
1352 data.name = ""
1353
1354 if turtle.getItemCount(slot) > 0 then
1355 data = turtle.getItemDetail(slot)
1356 end
1357
1358 return data.name
1359 end
1360
1361 function clsTurtle.getItemSlot(self, item, useDamage)
1362 -- return slot no with least count, damage(modifier) and total count available
1363 -- along with a table of mostSlot, mostCount, leastSlot, leastCount
1364 useDamage = useDamage or -1 -- -1 damage means is not relevant
1365 local data = {} --initialise empty table variable
1366 local slotData = {}
1367 local total = 0
1368 -- setup return table
1369 slotData.mostSlot = 0
1370 slotData.mostName = ""
1371 slotData.mostCount = 0
1372 slotData.mostModifier = 0
1373 slotData.leastSlot = 0
1374 slotData.leastName = ""
1375 slotData.leastCount = 0
1376 slotData.leastModifier = 0
1377 for i = 1, 16 do
1378 local count = turtle.getItemCount(i)
1379 if count > 0 then
1380 data = turtle.getItemDetail(i)
1381 if data.name == item and (data.damage == useDamage or useDamage == -1) then
1382 total = total + count
1383 if count > slotData.mostCount then--
1384 slotData.mostSlot = i
1385 slotData.mostName = data.name
1386 slotData.mostCount = count
1387 slotData.mostModifier = data.damage
1388 end
1389 if count < slotData.leastCount then--
1390 slotData.leastSlot = i
1391 slotData.leastName = data.name
1392 slotData.leastCount = count
1393 slotData.leastModifier = data.damage
1394 end
1395 end
1396 end
1397 end
1398 if slotData.mostSlot > 0 then
1399 if slotData.leastSlot == 0 then
1400 slotData.leastSlot = slotData.mostSlot
1401 slotData.leastName = slotData.mostName
1402 slotData.leastCount = slotData.mostCount
1403 slotData.leastModifier = slotData.mostModifier
1404 end
1405 end
1406
1407 return slotData.leastSlot, slotData.leastModifier, total, slotData -- first slot no, integer, integer, table
1408 end
1409
1410 function clsTurtle.getLogData(self)
1411 -- get the slot number and count of each type of log max and min only
1412 local leastSlot, leastDamage, total, total2
1413 local logData = {}
1414 local logData2 = {}
1415 local retData = {}
1416
1417 leastSlot, leastDamage, total, logData = clsTurtle.getItemSlot(self, "minecraft:log", -1)
1418 leastSlot, leastDamage, total2, logData2 = clsTurtle.getItemSlot(self, "minecraft:log2", -1)
1419 retData.total = total + total2
1420 if logData.mostCount > logData2.mostCount then
1421 retData.mostSlot = logData.mostSlot
1422 retData.mostCount = logData.mostCount
1423 retData.mostModifier = logData.mostModifier
1424 retData.mostName = logData.mostName
1425 if logData2.leastCount > 0 then
1426 retData.leastSlot = logData2.leastSlot
1427 retData.leastCount = logData2.leastCount
1428 retData.leastModifier = logData2.leastModifier
1429 retData.leastName = logData2.leastName
1430 else
1431 retData.leastSlot = logData.leastSlot
1432 retData.leastCount = logData.leastCount
1433 retData.leastModifier = logData.leastModifier
1434 retData.leastName = logData.leastName
1435 end
1436 else
1437 retData.mostSlot = logData2.mostSlot
1438 retData.mostCount = logData2.mostCount
1439 retData.mostModifier = logData2.mostModifier
1440 retData.mostName = logData2.mostName
1441 if logData.mostCount > 0 then
1442 retData.leastSlot = logData.leastSlot
1443 retData.leastCount = logData.leastCount
1444 retData.leastModifier = logData.leastModifier
1445 retData.leastName = logData.leastName
1446 else
1447 retData.leastSlot = logData2.leastSlot
1448 retData.leastCount = logData2.leastCount
1449 retData.leastModifier = logData2.leastModifier
1450 retData.leastName = logData2.leastName
1451 end
1452 end
1453
1454 return retData --{.total, .mostSlot, .mostCount, .mostModifier, mostName, .leastSlot, .leastCount, .leastModifier, leastName}
1455 end
1456
1457 function clsTurtle.getLogStock(self)
1458 local logAmount = 0
1459 local stockData = clsTurtle.getStock(self, "minecraft:log") --oak, spruce, birch, jungle
1460 logAmount = logAmount + stockData.total
1461 stockData = clsTurtle.getStock(self, "minecraft:log2") --acacia, dark oak
1462 logAmount = logAmount + stockData.total
1463
1464 return logAmount
1465 end
1466
1467 function clsTurtle.getPlaceChestDirection(self)
1468 local facing = self.facing
1469 local chestDirection = "forward"
1470 while turtle.detect() do --check for clear space to place chest
1471 clsTurtle.turnRight(self, 1)
1472 if facing == self.facing then -- full circle
1473 --check up and down
1474 if turtle.detectDown() then -- no space below
1475 if turtle.detectUp() then
1476 if clsTurtle.dig(self, "up") then
1477 chestDirection = "up"
1478 end
1479 else
1480 chestDirection = "up"
1481 end
1482 else
1483 chestDirection = "down"
1484 end
1485 break
1486 end
1487 end
1488 return chestDirection
1489 end
1490
1491 function clsTurtle.getSlotContains(self, slotNo)
1492 local data = {} --initialise empty table variable
1493
1494 if turtle.getItemCount(slotNo) > 0 then
1495 data = turtle.getItemDetail(slotNo)
1496 else
1497 data.count = 0
1498 data.name = ""
1499 data.damage = 0
1500 end
1501
1502 return data.count, data.name, data.damage
1503 end
1504
1505 function clsTurtle.getStock(self, item, modifier)
1506 -- return total units and slot numbers of max and min amounts
1507 local slot, damage, total, slotData = clsTurtle.getItemSlot(self, item, modifier) --return .leastSlot, .leastModifier, total, slotData
1508 local rt = {}
1509 rt.total = total
1510 rt.mostSlot = slotData.mostSlot
1511 rt.leastSlot = slotData.leastSlot
1512 rt.mostCount = slotData.mostCount
1513 rt.leastCount = slotData.leastCount
1514 if slot == 0 then
1515 if modifier == nil then
1516 clsTurtle.saveToLog(self, "getStock()"..tostring(item).."= not found", true)
1517 else
1518 clsTurtle.saveToLog(self, "getStock()"..tostring(item).."("..tostring(modifier)..")= not found")
1519 end
1520 end
1521
1522 return rt --{rt.total, rt.mostSlot, rt.leastSlot, rt.mostCount, rt.leastCount}
1523 end
1524
1525 function clsTurtle.go(self, path, useTorch, torchInterval)
1526 useTorch = useTorch or false -- used in m an M to place torches in mines
1527 torchInterval = torchInterval or 8
1528 local intervalList = {1, torchInterval * 1 + 1,
1529 torchInterval * 2 + 1,
1530 torchInterval * 3 + 1,
1531 torchInterval * 4 + 1,
1532 torchInterval * 5 + 1,
1533 torchInterval * 6 + 1}
1534 local slot = turtle.getSelectedSlot()
1535 turtle.select(1)
1536 local commandList = {}
1537 local command = ""
1538 -- make a list of commands from path string eg "x0F12U1" = x0, F12, U1
1539 for i = 1, string.len(path) do
1540 local character = string.sub(path, i, i) -- examine each character in the string
1541 if tonumber(character) == nil then -- char is NOT a number
1542 if command ~= "" then -- command is NOT empty eg "x0"
1543 table.insert(commandList, command) -- add to table eg "x0"
1544 end
1545 command = character -- replace command with new character eg "F"
1546 else -- char IS a number
1547 command = command..character -- add eg 1 to F = F1, 2 to F1 = F12
1548 if i == string.len(path) then -- last character in the string
1549 table.insert(commandList, command)
1550 end
1551 end
1552 end
1553 -- R# L# F# B# U# D# +0 -0 = Right, Left, Forward, Back, Up, Down, up while detect and return, down while not detect
1554 -- dig: x0,x1,x2 (up/fwd/down)
1555 -- suck: s0,s1,s2
1556 -- place chest: H0,H1,H2
1557 -- place sapling: S0,S1,S2
1558 -- place Torch: T0,T1,T2
1559 -- place Hopper: P0,P1,P2
1560 -- mine floor: m# = mine # blocks above and below, checking for valuable items below, and filling space with cobble or dirt
1561 -- mine ceiling: M# = mine # blocks, checking for valuable items above, and filling space with cobble or dirt
1562 -- mine ceiling: N# same as M but not mining block below unless valuable
1563 -- place: C,H,r,S,T,P = Cobble / cHest / DIrT / Sapling / Torch / hoPper in direction 0/1/2 (up/fwd/down) eg C2 = place cobble down
1564
1565 clsTurtle.refuel(self, 15)
1566 turtle.select(1)
1567 for cmd in clsTurtle.values(self, commandList) do -- eg F12 or x1
1568 local move = string.sub(cmd, 1, 1)
1569 local modifier = tonumber(string.sub(cmd, 2))
1570 if move == "R" then
1571 clsTurtle.turnRight(self, modifier)
1572 elseif move == "L" then
1573 clsTurtle.turnLeft(self, modifier)
1574 elseif move == "B" then
1575 clsTurtle.back(self, modifier)
1576 elseif move == "F" then
1577 clsTurtle.forward(self, modifier)
1578 elseif move == "U" then
1579 clsTurtle.up(self, modifier)
1580 elseif move == "D" then
1581 clsTurtle.down(self, modifier)
1582 elseif move == "+" then
1583 local height = 0
1584 while turtle.detectUp() do
1585 clsTurtle.up(self, 1)
1586 height = height + 1
1587 end
1588 clsTurtle.down(self, height)
1589 elseif move == "-" then
1590 while not turtle.inspectDown() do
1591 clsTurtle.down(self, 1)
1592 end
1593 elseif move == "x" then
1594 if modifier == 0 then
1595 clsTurtle.dig(self, "up")
1596 elseif modifier == 1 then
1597 clsTurtle.dig(self, "forward")
1598 elseif modifier == 2 then
1599 while turtle.detectDown() do
1600 turtle.digDown()
1601 end
1602 end
1603 elseif move == "s" then
1604 if modifier == 0 then
1605 while turtle.suckUp() do end
1606 elseif modifier == 1 then
1607 while turtle.suck() do end
1608 elseif modifier == 2 then
1609 while turtle.suckDown() do end
1610 end
1611 elseif move == "m" then --mine block below and/or fill void
1612 for i = 1, modifier + 1 do --eg m8 run loop 9 x
1613 turtle.select(1)
1614 if clsTurtle.isValuable(self, "down") then
1615 turtle.digDown() -- dig if valuable
1616 elseif clsTurtle.getBlockType(self, "down") == "minecraft:gravel" then
1617 turtle.digDown() -- dig if gravel
1618 else --check for lava
1619 if clsTurtle.getBlockType(self, "down") == "minecraft:lava" then
1620 clsTurtle.place(self, "minecraft:bucket", 0, "down")
1621 end
1622 end
1623 clsTurtle.dig(self, "up") -- create player coridoor
1624 if not turtle.detectDown() then
1625 if not clsTurtle.place(self, "minecraft:cobblestone", -1, "down") then
1626 clsTurtle.place(self, "minecraft:dirt", -1, "down")
1627 end
1628 end
1629 if i <= modifier then -- m8 = move forward 8x
1630 if useTorch then
1631 if i == intervalList[1] or
1632 i == intervalList[2] or
1633 i == intervalList[3] or
1634 i == intervalList[4] or
1635 i == intervalList[5] then
1636 clsTurtle.up(self, 1)
1637 clsTurtle.place(self, "minecraft:torch", -1, "down")
1638 clsTurtle.forward(self, 1)
1639 clsTurtle.down(self, 1)
1640 else
1641 clsTurtle.forward(self, 1)
1642 end
1643 else
1644 clsTurtle.forward(self, 1)
1645 end
1646 end
1647 end
1648 elseif move == "n" then --mine block below and/or fill void + check left side
1649 for i = 1, modifier + 1 do --eg m8 run loop 9 x
1650 turtle.select(1)
1651 if clsTurtle.isValuable(self, "down") then
1652 turtle.digDown() -- dig if valuable
1653 elseif clsTurtle.getBlockType(self, "down") == "minecraft:gravel" then
1654 turtle.digDown() -- dig if gravel
1655 else --check for lava
1656 if clsTurtle.getBlockType(self, "down") == "minecraft:lava" then
1657 clsTurtle.place(self, "minecraft:bucket", 0, "down")
1658 end
1659 end
1660 clsTurtle.dig(self, "up") -- create player coridoor
1661 if not turtle.detectDown() then
1662 if not clsTurtle.place(self, "minecraft:cobblestone", -1, "down") then
1663 clsTurtle.place(self, "minecraft:dirt", -1, "down")
1664 end
1665 end
1666 clsTurtle.turnLeft(self, 1)
1667 if clsTurtle.isValuable(self, "forward") then
1668 turtle.dig() -- dig if valuable
1669 end
1670 if not turtle.detect() then
1671 if not clsTurtle.place(self, "minecraft:cobblestone", -1, "forward") then
1672 clsTurtle.place(self, "minecraft:dirt", -1, "forward")
1673 end
1674 end
1675 clsTurtle.turnRight(self, 1)
1676 if i <= modifier then -- m8 = move forward 8x
1677 if useTorch then
1678 if i == intervalList[1] or
1679 i == intervalList[2] or
1680 i == intervalList[3] or
1681 i == intervalList[4] or
1682 i == intervalList[5] then
1683 clsTurtle.up(self, 1)
1684 clsTurtle.place(self, "minecraft:torch", -1, "down")
1685 clsTurtle.forward(self, 1)
1686 clsTurtle.down(self, 1)
1687 else
1688 clsTurtle.forward(self, 1)
1689 end
1690 else
1691 clsTurtle.forward(self, 1)
1692 end
1693 end
1694 end
1695 elseif move == "M" then --mine block above and/or fill void
1696 for i = 1, modifier + 1 do
1697 turtle.select(1)
1698 if clsTurtle.isValuable(self, "up") then
1699 clsTurtle.dig(self, "up")
1700 else --check for lava
1701 if clsTurtle.getBlockType(self, "up") == "minecraft:lava" then
1702 clsTurtle.place(self, "minecraft:bucket", 0, "up")
1703 end
1704 end
1705 if not turtle.detectUp() then
1706 if not clsTurtle.place(self, "minecraft:cobblestone", -1, "up") then
1707 clsTurtle.place(self, "minecraft:dirt", -1, "up")
1708 end
1709 end
1710 if i <= modifier then -- will not move forward if modifier = 0
1711 if useTorch then
1712 if i == intervalList[1] or
1713 i == intervalList[2] or
1714 i == intervalList[3] or
1715 i == intervalList[4] or
1716 i == intervalList[5] then
1717 clsTurtle.place(self, "minecraft:torch", 0, "down")
1718 end
1719 end
1720 clsTurtle.forward(self, 1)
1721 end
1722 end
1723 elseif move == "N" then --mine block above and/or fill void + mine block below if valuable
1724 for i = 1, modifier + 1 do
1725 turtle.select(1)
1726 if clsTurtle.isValuable(self, "up") then
1727 clsTurtle.dig(self, "up")
1728 else --check for lava
1729 if clsTurtle.getBlockType(self, "up") == "minecraft:lava" then
1730 clsTurtle.place(self, "minecraft:bucket", 0, "up")
1731 end
1732 end
1733 if not turtle.detectUp() then
1734 if not clsTurtle.place(self, "minecraft:cobblestone", -1, "up") then
1735 clsTurtle.place(self, "minecraft:dirt", -1, "up")
1736 end
1737 end
1738 turtle.select(1)
1739 if clsTurtle.isValuable(self, "down") then
1740 turtle.digDown()
1741 end
1742 if i <= modifier then
1743 clsTurtle.forward(self, 1)
1744 end
1745 end
1746 elseif move == "Q" then --mine block above and/or fill void + mine block below if valuable + left side
1747 for i = 1, modifier + 1 do
1748 turtle.select(1)
1749 if clsTurtle.isValuable(self, "up") then
1750 clsTurtle.dig(self, "up")
1751 else --check for lava
1752 if clsTurtle.getBlockType(self, "up") == "minecraft:lava" then
1753 clsTurtle.place(self, "minecraft:bucket", 0, "up")
1754 end
1755 end
1756 if not turtle.detectUp() then
1757 if not clsTurtle.place(self, "minecraft:cobblestone", -1, "up") then
1758 clsTurtle.place(self, "minecraft:dirt", -1, "up")
1759 end
1760 end
1761 clsTurtle.turnLeft(self, 1)
1762 if clsTurtle.isValuable(self, "forward") then
1763 turtle.dig() -- dig if valuable
1764 end
1765 if not turtle.detect() then
1766 if not clsTurtle.place(self, "minecraft:cobblestone", -1, "forward") then
1767 clsTurtle.place(self, "minecraft:dirt", -1, "forward")
1768 end
1769 end
1770 clsTurtle.turnRight(self, 1)
1771 if clsTurtle.isValuable(self, "down") then
1772 turtle.digDown()
1773 end
1774 if i <= modifier then
1775 clsTurtle.forward(self, 1)
1776 end
1777 end
1778 elseif move == "C" or move == "H" or move == "r" or move == "T" or move == "S" then --place item up/forward/down
1779 local placeItem = "minecraft:cobblestone"
1780 local direction = {"up", "forward", "down"}
1781 if move == "H" then
1782 placeItem = "minecraft:chest"
1783 elseif move == "r" then
1784 placeItem = "minecraft:dirt"
1785 elseif move == "T" then
1786 placeItem = "minecraft:torch"
1787 elseif move == "S" then
1788 placeItem = "minecraft:sapling"
1789 end
1790 if modifier == 0 then
1791 clsTurtle.dig(self, "up")
1792 elseif modifier == 1 then
1793 clsTurtle.dig(self, "forward")
1794 else
1795 turtle.digDown()
1796 end
1797 if move == "C" then
1798 if not clsTurtle.place(self, "minecraft:cobblestone", -1, direction[modifier + 1]) then
1799 clsTurtle.place(self, "minecraft:dirt", -1, direction[modifier + 1])
1800 end
1801 else
1802 clsTurtle.place(self, placeItem, -1, direction[modifier + 1])
1803 end
1804 elseif move == "*" then
1805 local goUp = 0
1806 while not turtle.inspectDown() do
1807 clsTurtle.down(self, 1)
1808 goUp = goUp + 1
1809 end
1810 if goUp > 0 then
1811 for i = 1, goUp do
1812 clsTurtle.up(self, 1)
1813 if not clsTurtle.place(self, "minecraft:cobblestone", -1, "down") then
1814 clsTurtle.place(self, "minecraft:dirt", -1, "down")
1815 end
1816 end
1817 goUp = 0
1818 else
1819 turtle.digDown()
1820 if not clsTurtle.place(self, "minecraft:cobblestone", -1, "down") then
1821 clsTurtle.place(self, "minecraft:dirt", -1, "down")
1822 end
1823 end
1824 elseif move == "c" then
1825 if turtle.detectDown() then
1826 --check if vegetation and remove
1827 data = clsTurtle.getBlockType(self, "down")
1828 if data.name ~= "minecraft:dirt" and data.name ~= "minecraft:stone" and data.name ~= "minecraft:cobblestone" and data.name ~= "minecraft:grass" then
1829 turtle.digDown()
1830 end
1831 end
1832 if not turtle.detectDown() then
1833 if not clsTurtle.place(self, "minecraft:cobblestone", -1, "down") then
1834 clsTurtle.place(self, "minecraft:dirt", -1, "down")
1835 end
1836 end
1837 elseif move == "Z" then -- mine to bedrock
1838 for i = 1, modifier + 1 do
1839 turtle.select(1)
1840 local goUp = 0
1841 while clsTurtle.down(self, 1) do
1842 goUp = goUp + 1
1843 end
1844 for j = goUp, 1, -1 do
1845 for k = 1, 4 do
1846 clsTurtle.turnRight(self, 1)
1847 if clsTurtle.isValuable(self, "forward") then
1848 clsTurtle.place(self, "minecraft:cobblestone", -1, "forward")
1849 end
1850 end
1851 clsTurtle.up(self, 1)
1852 clsTurtle.place(self, "minecraft:cobblestone", -1, "down")
1853 turtle.select(1)
1854 end
1855 if i <= modifier then
1856 clsTurtle.forward(self, 2)
1857 end
1858 end
1859 end
1860 end
1861 turtle.select(slot)
1862 end
1863
1864 function clsTurtle.harvestTree(self, extend)
1865 extend = extend or false
1866 local treeHeight = 0
1867 local addHeight = 0
1868 local onLeft = true
1869 local logType = clsTurtle.getBlockType(self, "forward")
1870 local slot = turtle.getSelectedSlot()
1871 clsTurtle.dig(self, "forward")
1872 clsTurtle.forward(self, 1) -- will force refuel if first tree
1873 clsTurtle.go(self, "L1") -- dig base of tree, go under tree
1874 -- check if on r or l of double width tree
1875 if clsTurtle.getBlockType(self, "forward", -1) == "minecraft:log" or clsTurtle.getBlockType(self, "forward", -1) == "minecraft:log2" then
1876 onLeft = false -- placed on right side of 2 block tree
1877 end
1878 clsTurtle.turnRight(self, 1)
1879 --craft chest if none onboard
1880 if clsTurtle.getItemSlot(self, "minecraft:chest", -1) == 0 then
1881 clsTurtle.go(self, "U2")
1882 clsTurtle.craft(self, "minecraft:planks", 8, logType, nil, nil, false)
1883 clsTurtle.craft(self, "minecraft:chest", 1, "minecraft:planks", nil, nil, false)
1884 else
1885 clsTurtle.go(self, "U2")
1886 end
1887 treeHeight = treeHeight + 2
1888 -- Loop to climb up tree and harvest trunk and surrounding leaves
1889 while clsTurtle.dig(self, "up") do -- continue loop while block detected above
1890 if clsTurtle.up(self, 1) then -- Move up
1891 treeHeight = treeHeight + 1
1892 end
1893 -- Inner loop to check for leaves
1894 for i = 1, 4 do
1895 if turtle.detect() then -- check if leaves in front
1896 turtle.dig() --Dig leaves
1897 end
1898 clsTurtle.turnRight(self, 1)
1899 end
1900 end
1901 -- At top of the tree. New loop to return to ground
1902 if extend then
1903 if onLeft then
1904 clsTurtle.go(self, "F1R1F1R2")
1905 else
1906 clsTurtle.go(self, "F1L1F1R2")
1907 end
1908 while turtle.detectUp() do
1909 clsTurtle.up(self, 1)
1910 addHeight = addHeight + 1
1911 end
1912 if addHeight > 0 then
1913 clsTurtle.down(self, addHeight)
1914 end
1915 end
1916 for i = 1, treeHeight do
1917 clsTurtle.down(self, 1)
1918 end
1919 if extend then
1920 if onLeft then
1921 clsTurtle.go(self, "F1L1F1R2")
1922 else
1923 clsTurtle.go(self, "F1R1F1R2")
1924 end
1925 end
1926 turtle.select(slot)
1927 end
1928
1929 function clsTurtle.initialise(self)
1930 clsTurtle.clear(self)
1931 if os.getComputerLabel() == nil then
1932 while name == "" do
1933 clsTurtle.clear(self)
1934 print("Type a name for this turtle")
1935 local name = read()
1936 end
1937 os.setComputerLabel(name)
1938 clsTurtle.clear(self)
1939 end
1940 print("Checking equipment...\nDO NOT REMOVE ANY ITEMS!")
1941 self.equippedRight, self.equippedLeft = clsTurtle.setEquipment(self) -- set in equipped items
1942 local message = os.getComputerLabel().." is equipped with:\n"
1943 if self.equippedLeft ~= "" and self.equippedRight ~= "" then
1944 message = message.."Left: "..self.equippedLeft.." and Right: "..self.equippedRight
1945 elseif self.equippedLeft ~= "" then
1946 message = message.."Left: "..self.equippedLeft
1947 elseif self.equippedRight ~= "" then
1948 message = message.."Right: "..self.equippedRight
1949 end
1950 end
1951
1952 function clsTurtle.isValuable(self, direction)
1953 local success = false
1954 local blockType = ""
1955 local blockModifier
1956
1957 local itemList = "minecraft:dirt,minecraft:grass,minecraft:stone,minecraft:gravel,minecraft:chest,"..
1958 "minecraft:cobblestone,minecraft:sand,minecraft:torch,minecraft:bedrock"
1959
1960 if direction == "up" then
1961 if turtle.detectUp() then
1962 blockType, blockModifier = clsTurtle.getBlockType(self, "up")
1963 end
1964 elseif direction == "down" then
1965 if turtle.detectDown() then
1966 blockType, blockModifier = clsTurtle.getBlockType(self, "down")
1967 end
1968 elseif direction == "forward" then
1969 if turtle.detect() then
1970 blockType, blockModifier = clsTurtle.getBlockType(self, "forward")
1971 end
1972 end
1973 if blockType ~= "" then --block found
1974 success = true
1975 if string.find(itemList, blockType) ~= nil then
1976 success = false
1977 end
1978 end
1979 return success
1980 end
1981
1982 function clsTurtle.isVegetation(self, blockName)
1983 blockName = blockName or ""
1984 local isVeg = false
1985 local vegList = {"minecraft:tallgrass", "minecraft:deadbush", "minecraft:cactus", "minecraft:leaves",
1986 "minecraft:pumpkin", "minecraft:melon_block", "minecraft:vine", "minecraft:mycelium", "minecraft:waterliliy",
1987 "minecraft:cocoa", "minecraft:double_plant", "minecraft:sponge", "minecraft:wheat"}
1988
1989 -- check for flower, mushroom
1990 if string.find(blockName, "flower") ~= nil or string.find(blockName, "mushroom") ~= nil then
1991 isVeg = true
1992 end
1993 if not isVeg then
1994 for _,v in pairs(vegList) do
1995 if v == blockName then
1996 isVeg = true
1997 break
1998 end
1999 end
2000 end
2001
2002 return isVeg
2003 end
2004
2005 function clsTurtle.place(self, blockType, damageNo, direction)
2006 local success = false
2007 local slot = slot or clsTurtle.getItemSlot(self, blockType, damageNo)
2008
2009 if slot > 0 then
2010 if direction == "down" then
2011 clsTurtle.dig(self, "down")
2012 turtle.select(slot)
2013 if turtle.placeDown() then
2014 if blockType == "minecraft:bucket" then
2015 if turtle.refuel() then
2016 clsTurtle.saveToLog("Refuelled with lava. Current fuel level: "..turtle.getFuelLevel())
2017 end
2018 end
2019 success = true
2020 else
2021 if not clsTurtle.attack(self, "down") then
2022 clsTurtle.saveToLog("Error placing "..blockType.." ? chest or minecart below")
2023 end
2024 end
2025 elseif direction == "up" then
2026 clsTurtle.dig(self, "up")
2027 turtle.select(slot)
2028 if turtle.placeUp() then
2029 if blockType == "minecraft:bucket" then
2030 if turtle.refuel() then
2031 clsTurtle.saveToLog("Refuelled with lava. Current fuel level: "..turtle.getFuelLevel())
2032 end
2033 end
2034 success = true
2035 else
2036 clsTurtle.saveToLog("Error placing "..blockType.." ? chest or minecart above")
2037 end
2038 else
2039 clsTurtle.dig(self, "forward")
2040 turtle.select(slot)
2041 if turtle.place() then
2042 if blockType == "minecraft:bucket" then
2043 if turtle.refuel() then
2044 clsTurtle.saveToLog("Refuelled with lava. Current fuel level: "..turtle.getFuelLevel())
2045 end
2046 end
2047 success = true
2048 else
2049 clsTurtle.saveToLog("Error placing "..blockType.." ? chest or minecart ahead")
2050 end
2051 end
2052 end
2053 turtle.select(1)
2054 return success
2055 end
2056
2057 function clsTurtle.refuel(self, minLevel)
2058 minLevel = minLevel or 15
2059 local itemSlot = 0
2060 local slot = turtle.getSelectedSlot()
2061 local count = 0
2062 local item = ""
2063 local damage = 0
2064 local success = false
2065 local data = {}
2066
2067 if turtle.getFuelLevel() < minLevel then
2068 -- check each slot for fuel item
2069 itemSlot = clsTurtle.getItemSlot(self, "minecraft:lava_bucket", -1) --return slotData.leastSlot, slotData.leastSlotDamage, total, slotData
2070 if itemSlot > 0 then
2071 turtle.select(itemSlot)
2072 if turtle.refuel() then
2073 clsTurtle.saveToLog(self, "refuel() lava used. level="..turtle.getFuelLevel(),true)
2074 success = true
2075 end
2076 end
2077 if not success then
2078 itemSlot = clsTurtle.getItemSlot(self, "minecraft:coal", 1) --charcoal
2079 if itemSlot > 0 then
2080 turtle.select(itemSlot)
2081 if turtle.refuel() then --use all charcoal
2082 clsTurtle.saveToLog(self, "refuel() all charcoal used. level="..turtle.getFuelLevel(),true)
2083 success = true
2084 end
2085 end
2086 end
2087 if not success then
2088 itemSlot = clsTurtle.getItemSlot(self, "minecraft:coal", 0)
2089 if itemSlot > 0 then
2090 turtle.select(itemSlot)
2091 if turtle.refuel(1) then
2092 clsTurtle.saveToLog(self, "refuel() 1 coal used. level="..turtle.getFuelLevel(),true)
2093 success = true
2094 end
2095 end
2096 end
2097 if not success then
2098 itemSlot = clsTurtle.getItemSlot(self, "minecraft:planks", -1)
2099 if itemSlot > 0 then
2100 turtle.select(itemSlot)
2101 if turtle.refuel() then
2102 clsTurtle.saveToLog(self, "refuel() planks used. level="..turtle.getFuelLevel(),true)
2103 success = true
2104 end
2105 end
2106 end
2107 if not success then
2108 itemSlot = clsTurtle.getItemSlot(self, "minecraft:log", -1)
2109 if itemSlot > 0 then --logs onboard
2110 clsTurtle.saveToLog(self, "Refuelling with log slot "..tostring(itemSlot)..", crafting planks", true)
2111 if turtle.getItemCount(itemSlot) == 1 then
2112 turtle.select(itemSlot)
2113 turtle.craft()
2114 if turtle.refuel() then
2115 clsTurtle.saveToLog(self, "refuel() planks used. level="..turtle.getFuelLevel(),true)
2116 success = true
2117 end
2118 else
2119 if clsTurtle.craft(self, "minecraft:planks", 4, "minecraft:log", nil, nil, true) then
2120 success = true
2121 else
2122 clsTurtle.saveToLog(self, "refuel() error crafting planks", true)
2123 end
2124 end
2125 end
2126 end
2127 if not success then
2128 itemSlot = clsTurtle.getItemSlot(self, "minecraft:log2", -1)
2129 if itemSlot > 0 then --logs onboard
2130 clsTurtle.saveToLog(self, "Refuelling with log2 slot "..tostring(itemSlot)..", crafting planks", true)
2131 if turtle.getItemCount(itemSlot) == 1 then
2132 turtle.select(itemSlot)
2133 turtle.craft()
2134 if turtle.refuel() then
2135 clsTurtle.saveToLog(self, "refuel() planks used. level="..turtle.getFuelLevel(),true)
2136 success = true
2137 end
2138 else
2139 if clsTurtle.craft(self, "minecraft:planks", 4, "minecraft:log2", nil, nil, true) then
2140 success = true
2141 else
2142 clsTurtle.saveToLog(self, "refuel() error crafting planks", true)
2143 end
2144 end
2145 end
2146 end
2147 if not success then
2148 term.clear()
2149 term.setCursorPos(1,1)
2150 print("Unable to refuel: "..turtle.getFuelLevel().." fuel remaining")
2151 error()
2152 end
2153 end
2154 if not recursiveCall then -- only runs once
2155 if not success then
2156 while turtle.getFuelLevel() < minLevel do
2157 term.clear()
2158 term.setCursorPos(1,1)
2159 print("Unable to refuel: "..turtle.getFuelLevel().." fuel remaining")
2160 print("Please add any fuel to any slot")
2161 clsTurtle.refuel(self, minLevel, true) -- recursive function, flag set
2162 sleep(1)
2163 end
2164 term.clear()
2165 term.setCursorPos(1,1)
2166 end
2167 end
2168 turtle.select(slot)
2169 return success
2170 end
2171
2172 function clsTurtle.setEquipment(self)
2173 -- if contains a crafting table, puts it on the right. Any other tool on the left
2174 local emptySlotR = clsTurtle:getFirstEmptySlot() -- first empty slot
2175 local emptySlotL = 0 -- used later
2176 local eqRight = ""
2177 local eqLeft = ""
2178 local count = 0
2179 local damage = 0
2180 if emptySlotR > 0 then -- empty slot found
2181 turtle.select(emptySlotR)
2182 if turtle.equipRight() then -- remove tool on the right
2183 count, eqRight, damage = clsTurtle.getSlotContains(self, emptySlotR) -- eqRight contains name of tool from right side
2184 emptySlotL = clsTurtle.getFirstEmptySlot(self) -- get next empty slot
2185 else
2186 emptySlotL = emptySlotR
2187 end
2188 if emptySlotL > 0 then -- empty slot found
2189 turtle.select(emptySlotL)
2190 if turtle.equipLeft() then
2191 count, eqLeft, damage = clsTurtle.getSlotContains(self, emptySlotL) -- eqLeft contains name of tool from left side
2192 end
2193 end
2194 if eqRight == "minecraft:crafting_table" then
2195 turtle.select(emptySlotR)
2196 turtle.equipRight()
2197 self.equippedRight = eqRight
2198 eqRight = ""
2199 elseif eqLeft == "minecraft:crafting_table" then
2200 turtle.select(emptySlotL)
2201 turtle.equipRight()
2202 self.equippedRight = eqLeft
2203 eqLeft = ""
2204 end
2205 if eqRight ~= "" then -- still contains a tool
2206 turtle.select(emptySlotR)
2207 turtle.equipLeft()
2208 self.equippedLeft = eqRight
2209 end
2210 if eqLeft ~= "" then
2211 turtle.select(emptySlotL)
2212 turtle.equipLeft()
2213 self.equippedLeft = eqLeft
2214 end
2215 end
2216 turtle.select(1)
2217 return self.equippedRight, self.equippedLeft
2218 end
2219
2220 function clsTurtle.sortInventory(self)
2221 local turns = 0
2222 local chestSlot = clsTurtle.getItemSlot(self, "minecraft:chest", -1) --get the slot number containing a chest
2223 local blockType, blockModifier
2224 local facing = self.facing
2225 clsTurtle.saveToLog(self, "clsTurtle:sortInventory() started chest found in slot "..chestSlot, true)
2226 if chestSlot > 0 then -- chest found
2227 -- find empty block to place it.
2228 local chestDirection = clsTurtle.getPlaceChestDirection(self)
2229 blockType, blockModifier = clsTurtle.getBlockType(self, chestDirection)
2230 clsTurtle.saveToLog(self, "clsTurtle:sortInventory() looking "..chestDirection.." for chest...", true)
2231 while blockType ~= "minecraft:chest" do --check if chest placed eg mob in the way
2232 if clsTurtle.place(self, "minecraft:chest", -1, chestDirection) then
2233 clsTurtle.saveToLog(self, "clsTurtle:sortInventory() chest placed:"..chestDirection, true)
2234 break
2235 else
2236 clsTurtle.saveToLog(self, "clsTurtle:sortInventory() chest NOT placed:"..chestDirection, true)
2237 clsTurtle.dig(self, chestDirection) -- will force wait for mob
2238 chestDirection = clsTurtle.getPlaceChestDirection(self)
2239 end
2240 blockType, blockModifier = clsTurtle.getBlockType(self, chestDirection)
2241 end
2242 -- fill chest with everything
2243 clsTurtle.saveToLog(self, "clsTurtle:sortInventory() emptying turtle:"..chestDirection, true)
2244 for i = 1, 16 do
2245 clsTurtle.drop(self, i, chestDirection)
2246 end
2247 -- remove everything
2248 clsTurtle.saveToLog(self, "clsTurtle:sortInventory() refilling turtle:"..chestDirection, true)
2249 while clsTurtle.suck(self, chestDirection) do end
2250 clsTurtle.dig(self, chestDirection) -- collect chest
2251 clsTurtle.saveToLog(self, "clsTurtle:sortInventory() chest collected("..chestDirection..")", true)
2252 while facing ~= self.facing do --return to original position
2253 clsTurtle.turnLeft(self, 1)
2254 end
2255 end
2256 end
2257
2258 function clsTurtle.suck(self, direction)
2259 direction = direction or "forward"
2260 turtle.select(1)
2261 local success = false
2262 if direction == "up" then
2263 if turtle.suckUp() then
2264 success = true
2265 end
2266 elseif direction == "down" then
2267 if turtle.suckDown() then
2268 success = true
2269 end
2270 else
2271 if turtle.suck() then
2272 success = true
2273 end
2274 end
2275 return success
2276 end
2277
2278 clsTurtle.initialise(self)
2279
2280 return self
2281end
2282
2283-- local functions
2284function checkTreeFarm()
2285 -- will only be called if bucket already onboard
2286 -- do not bother if no saplings
2287 if not treeFarm:getPondFilled() then --pond not filled yet
2288 fillPond() -- use iron ore from creating mine for bucket, fill pond with water
2289 end
2290 if treeFarm:getPondFilled() then --pond filled
2291 if not treeFarm:getFarmCreated() then --treefarm not yet made
2292 clearTreeFarm() -- clear 11 x 10 area to right of base and plant 4 saplings
2293 end
2294 if treeFarm:getFarmCreated() then -- treefarm built
2295 if not treeFarm:getHopperPlaced() then --hopper not yet built
2296 craftHopper()
2297 end
2298 end
2299 if treeFarm:getFarmCreated() and treeFarm:getHopperPlaced() then --treefarm built, hopper placed
2300 if not treeFarm:getFarmFlooded() then
2301 floodTreeFarm() -- Use bucket and pond to flood tree farm base ready for sapling collection
2302 end
2303 end
2304 end
2305end
2306
2307function checkWaterCoordsOK(x,z)
2308 local result = false
2309 -- 0 = go south (z increases)
2310 -- 1 = go west (x decreases)
2311 -- 2 = go north (z decreases
2312 -- 3 = go east (x increases)
2313 -- check if water coords are within storage placement area
2314 local relX = x - coordHome:getX() -- eg facing e -400 - -407 = +7
2315 local relZ = z - coordHome:getZ() -- eg facing e 270 - 278 = -8 (behind)
2316 if coordHome:getFacing() == 0 then
2317 --xCoord = xCoord + 1
2318 if relZ >= 3 or relZ <= -18 or relX >= 5 or relX <= -12 then --too far behind / in front to left/right
2319 result = true
2320 else
2321 if relZ < 3 or relZ > -18 then
2322 if relX >= 5 or relX <= -12 then
2323 result = true
2324 end
2325 elseif relX < 5 or relX > -12 then
2326 if relZ >= 3 or relZ <= -18 then
2327 result = true
2328 end
2329 end
2330 end
2331 elseif coordHome:getFacing() == 1 then --facing west
2332 --xCoord = xCoord - 1
2333 if relX <= -3 or relX >= 18 or relZ <= -5 or relZ >= 12 then --too far behind / in front to left/right
2334 result = true
2335 else
2336 if relX > -3 or relX < 18 then
2337 if relZ <= -5 or relZ >= 12 then
2338 result = true
2339 end
2340 elseif relZ > -5 or relZ < 12 then
2341 if relX <= -3 or relX >= 18 then
2342 result = true
2343 end
2344 end
2345 end
2346 elseif coordHome:getFacing() == 2 then
2347 --zCoord = zCoord - 1
2348 if relZ <= -3 or relZ >= 18 or relX <= -5 or relX >= 12 then --too far behind / in front to left/right
2349 result = true
2350 else
2351 if relZ > -3 or relZ < 18 then
2352 if relX <= -5 or relX >= 12 then
2353 result = true
2354 end
2355 elseif relX > -5 or relX < 12 then
2356 if relZ <= -3 or relZ >= 18 then
2357 result = true
2358 end
2359 end
2360 end
2361 elseif coordHome:getFacing() == 3 then --facing east
2362 --xCoord = xCoord + 1
2363 if relX >= 3 or relX <= -18 or relZ >= 5 or relZ <= -12 then --too far behind / in front to left/right
2364 result = true
2365 else
2366 if relX < 3 or relX > -18 then
2367 if relZ >= 5 or relZ <= -12 then
2368 result = true
2369 end
2370 elseif relZ < 5 or relZ > -12 then
2371 if relX >= 3 or relX <= -18 then
2372 result = true
2373 end
2374 end
2375 end
2376 end
2377
2378 return result
2379end
2380
2381function clearBase()
2382 local goUp = 0
2383 T:saveToLog("clearBase() Starting...", true)
2384 emptyAfterHarvest() --empty trash
2385 --craft furnace
2386 T:craft("minecraft:furnace", 1, "minecraft:cobblestone", nil, nil, false)
2387 --clear area around first tree 5 X 5 square
2388 T:go("F1x0C2L1")
2389 for i = 1, 3 do
2390 T:go("F1x2*0")
2391 end
2392 T:go("F1x0*2L1")
2393 for i = 1, 2 do
2394 T:go("F1x0*2")
2395 end
2396 T:go("F1x0*2L1")
2397 for i = 1, 5 do
2398 T:go("F1x0*2")
2399 end
2400 T:go("F1x0*2L1F1x0*2")
2401 T:go("F1x0*2D2x2C2U2")
2402 T:go("F1x0*2L1F1x0*2L1F1x0x2C2F1x0x2C2R1")
2403 T:go("F1x0C2F1x0C2")
2404 T:go("F1x0D1C2F1x0C2R1F1x0C2R1F1x0C2")
2405 T:go("F1x0C2U1C2F1L1")
2406 --put furnace above
2407 turtle.select(T:getItemSlot("minecraft:furnace"))
2408 turtle.placeUp()
2409end
2410
2411function clearTreeFarm()
2412 local stockData = {}
2413 stockData = T:getStock("minecraft:log", -1)
2414 local numLogs = stockData.total
2415 stockData = T:getStock("minecraft:torch")
2416 local numTorches = stockData.total
2417 local torchesNeeded = 10 - numTorches
2418
2419 T:saveToLog("clearTreeFarm() starting", true)
2420 --clear farm area
2421 treeFarm:reset()
2422 T:sortInventory()
2423 if torchesNeeded < 0 then
2424 torchesNeeded = 0
2425 else
2426 if numLogs > 4 then
2427 craftTorches(8) -- craft 6 torches for tree farm
2428 end
2429 end
2430 emptyTurtle(true)
2431 -- get saplings
2432 T:go("R1F2D1R2")
2433 while turtle.suckDown() do end
2434 stockData = T:getStock("minecraft:sapling", -1)
2435 if stockData.total > 0 then
2436 T:go("U1F2R1F4R2")
2437 --get cobble/dirt
2438 while turtle.suckDown() do end
2439 T:go("F4L1F2L1U1")-- on water enclosure wall
2440 -- build outer wall
2441 for i = 1, 9 do
2442 T:go("C2+0-0F1")
2443 end
2444 T:turnRight(1)
2445 for i = 1, 9 do
2446 T:go("C2+0-0F1")
2447 end
2448 T:turnRight(1)
2449 for i = 1, 10 do
2450 T:go("C2+0-0F1")
2451 end
2452 T:turnRight(1)
2453 for i = 1, 9 do
2454 T:go("C2+0-0F1")
2455 end
2456 T:turnRight(1)
2457 T:go("C2+0-0F1R1F1D1L1")
2458 --clear ground within outer wall
2459 for i = 1, 8 do -- left side
2460 T:go("c0+0-0F1")
2461 end -- col 2, row 2
2462 T:go("R1C2F1")
2463 for j = 1, 4 do
2464 for i = 1, 6 do
2465 T:go("c0+0-0F1")
2466 end
2467 T:go("R1c0+0-0F1R1")
2468 for i = 1, 6 do
2469 T:go("c0+0-0F1")
2470 end
2471 T:go("L1c0+0-0F1L1")
2472 end
2473 for i = 1, 6 do
2474 T:go("c0+0-0F1")
2475 end -- col 9, row 9 on right side of farm
2476 -- channel across end of farm
2477 T:go("c0+0-0L1D1") -- stay at bottom end
2478 T:go("R1x1C1R1C1R1C1C2F1C2L1C1R1F1C2L1C1R1F1C2L1C1R1F1C2L1C1R1F1C2L1C1R1F1C2L1C1R1F1C2L1C1R1F1x0C1R2F3U1L1F3")
2479 --place dirt and torches
2480 for i = 1, 4 do
2481 T:go("r0B1T0F4R1") --place dirt, Back, digUp, place Torch, forward 3, turn right
2482 end
2483 T:go("B2U3F2") --dodge torch on first block
2484 T:go("S2F3S2R1F3S2R1F3S2F3D3R1F8R1")
2485 treeFarm:setFarmCreated(true)
2486 treeFarm:setTimePlanted(os.time())
2487 saveStatus("treeFarm")
2488 emptyTurtle(false)
2489 else -- no saplings abandon tree farm
2490 T:go("U1F2R1")
2491 T:saveToLog("No saplings: treeFarm abandoned")
2492 end
2493
2494end
2495
2496function craftBucket()
2497 local stockData = {}
2498 local success = false
2499
2500 -- called only when under furnace
2501 stockData = T:getStock("minecraft:bucket", -1)
2502 if stockData.total == 0 then
2503 stockData = T:getStock("minecraft:iron_ore", -1)
2504 if stockData.total >= 3 then
2505 if smelt("minecraft:iron_ore", 3) then
2506 if T:craft("minecraft:bucket", 1, "minecraft:iron_ingot", nil, nil, false) then
2507 success = true
2508 end
2509 end
2510 end
2511 else
2512 success = true
2513 end
2514
2515 return success
2516end
2517
2518function craftHopper()
2519 local doContinue = false
2520 local stockData = {}
2521
2522 T:saveToLog("craftHopper() started")
2523 stockData = T:getStock("minecraft:iron_ore", -1)
2524 if stockData.total >= 5 then
2525 stockData = T:getStock("minecraft:chest")
2526 if stockData.total < 2 then
2527 if T:craftChests(1, false) >= 1 then
2528 doContinue = true
2529 end
2530 end
2531 if doContinue then --chests x 1 available
2532 doContinue = false
2533 if smelt("minecraft:iron_ore", 5) then
2534 doContinue = true
2535 end
2536 end
2537 if doContinue then -- chests x 1, and iron x 5 available
2538 doContinue = false
2539 if T:craft("minecraft:hopper", 1, "minecraft:iron_ingot", "minecraft:chest", nil, false) then
2540 doContinue = true
2541 end
2542 end
2543 if doContinue then -- hopper available
2544 T:go("R1F4R2D2")
2545 T:place("minecraft:hopper", -1, "forward")
2546 T:go("U1C2U1F4R1")
2547 treeFarm:setHopperPlaced(true)
2548 saveStatus("treeFarm")
2549 end
2550 end
2551 T:saveToLog("craftHopper() completed")
2552 return doContinue
2553end
2554
2555function craftMiningTurtle(numTurtles)
2556 local itemSlot = 0
2557 local keepNum = 0
2558 local logType = "minecraft:log"
2559 local logsNeeded = 0
2560 local numSticks = 0
2561 local sticksNeeded = 0
2562 local startFile = ""
2563 local itemInStock = 0
2564 local maxItemSlot = 0
2565 local minItemSlot = 0
2566 local stockData = {}
2567 local doContinue = false
2568
2569 -- chest1 = sticks
2570 -- chest2 = logs (oak/birch/jungle etc)
2571 -- chest3 = cobblestone, dirt
2572 -- chest4 = sand
2573 -- chest5 = iron_ore
2574 -- chest6 = redstone
2575 -- chest7 = diamond
2576 -- chest8 = gold_ore, lapis, mossy_cobblestone, obsidian
2577 -- chest9 = saplings (oak/birch,jungle)
2578
2579 -- Logs for Items needed:
2580 -- 1 turtle 2 turtles
2581 -- 6 sand 2 logs 2 logs
2582 -- disk dr: 7 cobble 2 logs 2 logs
2583 -- turtle: 7 cobble 2 logs 4 logs
2584 -- ironore: 7 iron ore 2 logs 4 logs
2585 -- crafting: 1 log 2 logs
2586 -- chests: 2 logs 4 logs
2587 -- pickaxe: 1 log 1 log
2588 -- Min 13 wood Max 21 wood
2589 T:saveToLog("craftMiningTurtle() started")
2590 -- start under furnace / over chest1
2591 if numTurtles == 0 then
2592 term.clear()
2593 T:saveToLog("Not enough diamonds in this area.\nMission has failed", true)
2594 error()
2595 elseif numTurtles == 1 then -- 0 - 2 diamonds + pickaxe
2596 sticksNeeded = 2
2597 logsNeeded = 20
2598 elseif numTurtles == 2 then
2599 sticksNeeded = 4
2600 logsNeeded = 28
2601 end
2602
2603 T:sortInventory()
2604 --use coal for fuel
2605 itemSlot = T:getItemSlot("minecraft:coal", -1)
2606 while itemSlot > 0 do
2607 turtle.select(itemSlot)
2608 turtle.refuel()
2609 itemSlot = T:getItemSlot("minecraft:coal", -1)
2610 end
2611 --empty turtle into correct storage
2612 T:dropItem("minecraft:stick", 0)
2613 T:forward(2) -- logs
2614 T:dropItem("minecraft:log", 0)
2615 T:dropItem("minecraft:log2", 0)
2616 T:forward(2) --dirt cobble
2617 T:dropItem("minecraft:cobblestone", 0)
2618 T:dropItem("minecraft:dirt", 0)
2619 T:forward(2) --sand reeds
2620 T:dropItem("minecraft:sand", 0)
2621 T:forward(2) --iron ore
2622 T:dropItem("minecraft:iron_ore", 0)
2623 T:dropItem("minecraft:bucket", 0)
2624 T:forward(2) --redstone
2625 T:dropItem("minecraft:redstone", 0)
2626 T:forward(2) -- diamond
2627 T:dropItem("minecraft:diamond", 0)
2628 T:forward(2)
2629 for i = 1, 16 do
2630 if turtle.getItemCount(i) > 0 then
2631 if T:getItemName(i) ~= "minecraft:chest" then
2632 T:dropItem(T:getItemName(i))
2633 end
2634 end
2635 end
2636
2637 -- only chest(s) left behind in turtle
2638 itemSlot = T:getItemSlot("minecraft:chest", 0)
2639 if itemSlot ~= 1 then
2640 turtle.select(itemSlot)
2641 turtle.transferTo(1)
2642 end
2643 -- go back and remove supplies
2644 T:go("R2F2") -- diamonds
2645 turtle.select(1)
2646 while turtle.suckDown() do end
2647 itemSlot = T:getItemSlot("minecraft:diamond", 0)
2648 keepNum = numTurtles * 3
2649 turtle.select(itemSlot)
2650 if turtle.getItemCount(itemSlot) > keepNum then
2651 turtle.dropDown(turtle.getItemCount(itemSlot) - keepNum)
2652 end
2653
2654 T:forward(2) --redstone
2655 turtle.select(1)
2656 turtle.suckDown()
2657 itemSlot = T:getItemSlot("minecraft:redstone")
2658 keepNum = numTurtles + 3
2659 turtle.select(itemSlot)
2660 if turtle.getItemCount(itemSlot) > keepNum then
2661 turtle.dropDown(turtle.getItemCount(itemSlot) - keepNum)
2662 end
2663
2664 T:forward(2) --iron ore
2665 turtle.select(1)
2666 turtle.suckDown()
2667 itemSlot = T:getItemSlot("minecraft:iron_ore")
2668 keepNum = numTurtles * 7
2669 turtle.select(itemSlot)
2670 if turtle.getItemCount(itemSlot) > keepNum then
2671 turtle.dropDown(turtle.getItemCount(itemSlot) - keepNum)
2672 end
2673
2674
2675 T:forward(2) --sand and reeds
2676 turtle.select(1)
2677 while turtle.suckDown() do end
2678 itemSlot = T:getItemSlot("minecraft:sand")
2679 keepNum = 6
2680 turtle.select(itemSlot)
2681 if turtle.getItemCount(itemSlot) > keepNum then
2682 turtle.dropDown(turtle.getItemCount(itemSlot) - keepNum)
2683 end
2684 itemSlot = T:getItemSlot("minecraft:reeds")
2685 turtle.select(itemSlot)
2686 keepNum = 3
2687 if turtle.getItemCount(itemSlot) > keepNum then
2688 turtle.dropDown(turtle.getItemCount(itemSlot) - keepNum)
2689 end
2690
2691 T:forward(2) --cobblestone
2692 keepNum = numTurtles * 7 + 7
2693 repeat
2694 turtle.suckDown()
2695 stockData = T:getStock("minecraft:cobblestone")
2696 until stockData.total >= keepNum
2697 itemSlot = T:getItemSlot("minecraft:cobblestone")
2698 T:dropItem("minecraft:cobblestone", turtle.getItemCount(itemSlot) - keepNum)
2699 T:dropItem("minecraft:dirt", 0)
2700
2701 T:forward(2) --logs
2702 while turtle.suckDown() do
2703 stockData = T:getStock("minecraft:log")
2704 if stockData.total >= logsNeeded then
2705 break
2706 end
2707 stockData = T:getStock("minecraft:log2")
2708 if stockData.total >= logsNeeded then
2709 break
2710 end
2711 end
2712 --empty furnace
2713 turtle.select(1)
2714 T:go("F2R2B1U1s1D1F1")
2715 itemSlot = T:getItemSlot("minecraft:planks")
2716 if itemSlot > 0 then
2717 stockData = T:getStock("minecraft:planks")
2718 if stockData.total >= 2 then
2719 craftSticks(4)
2720 end
2721 itemSlot = T:getItemSlot("minecraft:planks")
2722 if itemSlot > 0 then
2723 turtle.select(itemSlot)
2724 turtle.refuel()
2725 end
2726 end
2727 turtle.select(1)
2728 turtle.suckDown()
2729 itemSlot = T:getItemSlot("minecraft:stick")
2730 keepNum = sticksNeeded
2731 if itemSlot > 0 then
2732 if turtle.getItemCount(itemSlot) > keepNum then
2733 turtle.select(itemSlot)
2734 turtle.dropDown(turtle.getItemCount(itemSlot) - keepNum)
2735 sticksNeeded = 0
2736 end
2737 end
2738
2739 -- logs: crafting table = 2, sticks = 1, smelt
2740 -- planks: crafting table = 8, sticks = 2, smelt cobble = 21, smelt ironore = 14, smelt sand = 6, total 64
2741 stockData = T:getStock("minecraft:log")
2742 if stockData.total == 0 then
2743 stockData = T:getStock("minecraft:log2")
2744 logType = "minecraft:log2"
2745 end
2746 if stockData.total < logsNeeded then --not enough logs on board
2747 harvestTreeFarm()
2748 stockData = T:getStock("minecraft:log")
2749 logType = "minecraft:log"
2750 if stockData.total == 0 then
2751 stockData = T:getStock("minecraft:log2")
2752 logType = "minecraft:log2"
2753 end
2754 end
2755 if sticksNeeded > 0 then
2756 craftSticks(4)
2757 end
2758 T:craft("minecraft:diamond_pickaxe", 1, "minecraft:stick", "minecraft:diamond", nil, false)
2759 if numTurtles == 2 then --make 2 diamond pickaxe
2760 T:craft("minecraft:diamond_pickaxe", 1, "minecraft:stick", "minecraft:diamond", nil, false)
2761 end
2762 smelt("minecraft:cobblestone", 7 * numTurtles + 7)
2763 smelt("minecraft:iron_ore", 7 * numTurtles)
2764 smelt("minecraft:sand", 6)
2765 T:craft("minecraft:planks", 12 * numTurtles, logType, nil, nil, false)
2766 T:craftChests(numTurtles)
2767 T:craft("minecraft:crafting_table", numTurtles, "minecraft:planks", nil, nil, false)
2768 itemSlot = T:getItemSlot("minecraft:planks")
2769 if itemSlot > 0 then
2770 turtle.select(itemSlot)
2771 turtle.refuel()
2772 end
2773
2774 T:craft("minecraft:paper", 3, "minecraft:reeds", nil, nil, false)
2775 T:craft("minecraft:glass_pane", 16, "minecraft:glass", nil, nil, false)
2776 T:craft(ccDisk, 1, "minecraft:paper", "minecraft:redstone", nil, false)
2777 T:craft(ccComputer, numTurtles, "minecraft:glass_pane", "minecraft:redstone", "minecraft:stone", false)
2778 T:craft(ccTurtle, numTurtles, "minecraft:chest", ccComputer, "minecraft:iron_ingot", false)
2779 T:craft(ccCraftyMiningTurtle, 1, "minecraft:crafting_table", "minecraft:diamond_pickaxe", ccTurtle, false)
2780 T:craft(ccDiskDrive, 1, "minecraft:redstone", "minecraft:stone", nil, false)
2781 T:go("R2F3R2")
2782 programTurtles(numTurtles)
2783end
2784
2785function programTurtles(numTurtles)
2786 --program mining turtle
2787 turtle.select(T:getItemSlot(ccDiskDrive))
2788 turtle.place()
2789 turtle.select(T:getItemSlot(ccDisk))
2790 turtle.drop()
2791 T:go("U1F1") --now on top of disk drive
2792
2793
2794 if disk.isPresent("bottom") then
2795 local filepath = shell.getRunningProgram()
2796 local filename = fs.getName(filepath)
2797 if fs.getFreeSpace("/disk/") > 130000 then -- use filecopy
2798 fs.copy(filepath, "/disk/"..filename)
2799 T:saveToLog("selfReplicate.lua copied to floppy disk", true)
2800 else
2801 local text = "Config file not modified on this system."
2802 text = text.."\nUnable to copy 150kB file."
2803 text = text.."\nMy offspring will download files from"
2804 text = text.."\nPastebin if connected to the internet"
2805 text = text.."\nLast resort:\ncopy selfReplicate.lua\nusing Windows file explorer"
2806 T:saveToLog(text, true)
2807 end
2808
2809 --This script is a file called startup stored on the floppy disk
2810 --When a turtle is placed next to the disk drive, it reads this script
2811 --which opens 'minerList.txt' and sets the label to Miner2, (as '2' is stored in this file)
2812 --Either copies start.lua to the turtle then modifies 'minerList.txt' to '3'
2813 --or if on a server, requests the start.lua file via http from pastebin
2814 --so the name Miner3 given for the second turtle, (if constructed)
2815
2816 -- create/overwrite 'minerList.txt' on floppy disk
2817 startFile = fs.open("/disk/minerList.txt", "w")
2818 startFile.writeLine("2")
2819 startFile.close()
2820 -- create/overwrite startup
2821 startFile = fs.open("/disk/startup", "w")
2822 startFile.writeLine('function get(name, code)')
2823 startFile.writeLine(' local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(code))')
2824 startFile.writeLine(' if response then')
2825 startFile.writeLine(' local sCode = response.readAll()')
2826 startFile.writeLine(' if sCode ~= nil and sCode ~= "" then')
2827 startFile.writeLine(' local file = fs.open( name, "w" )')
2828 startFile.writeLine(' response.close()')
2829 startFile.writeLine(' file.write(sCode)')
2830 startFile.writeLine(' file.close()')
2831 startFile.writeLine(' return true')
2832 startFile.writeLine(' end')
2833 startFile.writeLine(' end')
2834 startFile.writeLine(' return false')
2835 startFile.writeLine('end')
2836 startFile.writeLine('if fs.exists("/disk/minerList.txt") then')
2837 startFile.writeLine(' textFile = fs.open("/disk/minerList.txt", "r")')
2838 startFile.writeLine(' textIn = textFile.readAll()')
2839 startFile.writeLine(' minerName = "Miner"..textIn')
2840 startFile.writeLine(' textFile.close()')
2841 startFile.writeLine(' textOut = tostring(tonumber(textIn) + 1)')
2842 startFile.writeLine('else')
2843 startFile.writeLine(' minerName = "Miner2"')
2844 startFile.writeLine(' textOut = "3"')
2845 startFile.writeLine('end')
2846 startFile.writeLine('textFile = fs.open("/disk/minerList.txt", "w")')
2847 startFile.writeLine('textFile.writeLine(textOut)')
2848 startFile.writeLine('textFile.close()')
2849 startFile.writeLine('if os.getComputerLabel() == nil or string.find(os.getComputerLabel(),"Miner") == nil then')
2850 startFile.writeLine(' os.setComputerLabel(minerName)')
2851 startFile.writeLine('end')
2852 startFile.writeLine('print("Hello, my name is "..os.getComputerLabel())')
2853 startFile.writeLine('if fs.exists("/disk/selfReplicate.lua") then')
2854 startFile.writeLine(' fs.copy("/disk/selfReplicate.lua", "selfReplicate.lua")')
2855 startFile.writeLine(' print("Replication program copied")')
2856 startFile.writeLine('else')
2857 startFile.writeLine(' get("selfReplicate.lua", "'..pastebinCode..'")')
2858 startFile.writeLine('end')
2859 startFile.writeLine('print("Break me and move to a tree > 33 blocks away")')
2860 startFile.writeLine('print("use selfReplicate.lua to begin self replicating ")')
2861 startFile.close()
2862 end
2863 T:go("B1D1R1F1L1")
2864 turtle.select(T:getItemSlot(ccCraftyMiningTurtle))
2865 turtle.place() --next to disk drive
2866 T:turnLeft(1)
2867 if numTurtles == 2 then
2868 T:craft(ccCraftyMiningTurtle, 1, "minecraft:crafting_table", "minecraft:diamond_pickaxe", ccTurtle, false)
2869 T:go("F1R1U1")
2870 turtle.select(T:getItemSlot(ccCraftyMiningTurtle))
2871 turtle.place() --next to disk drive
2872 T:down(1)
2873 else
2874 T:go("F1R1")
2875 end
2876 term.clear()
2877 if numTurtles == 2 then
2878 T:saveToLog("Mission successful.\nI have replicated myself twice.\n\nRight-Click on my offspring to check their status...")
2879 else
2880 T:saveToLog("Mission partially successful.\nI have replicated myself once\n\nRight-Click on my offspring to check its status...")
2881 end
2882end
2883
2884function craftSigns(signsNeeded)
2885 local success = false
2886 local woodNeeded = 0
2887 local woodInStock = 0
2888 local numPlanksNeeded = 0
2889 local logType = "minecraft:log"
2890 local stockData = {}
2891
2892 turtle.suckDown() --get any sticks in chest below
2893 signsNeeded = signsNeeded or 3
2894 if signsNeeded > 12 then
2895 signsNeeded = 12
2896 end
2897 --make 3 signs by default , need 8 planks, leaves 3 sticks
2898 -- signs planks sticks wood
2899 -- 3 6 + 2 1 2
2900 -- 6 12 + 2 2 4
2901 -- 9 18 + 2 3 5
2902 -- 12 24 + 2 4 7
2903
2904 if signsNeeded == 3 then
2905 woodNeeded = 2
2906 numPlanksNeeded = 8
2907 elseif signsNeeded == 6 then
2908 woodNeeded = 4
2909 numPlanksNeeded = 16
2910 elseif signsNeeded == 9 then
2911 woodNeeded = 5
2912 numPlanksNeeded = 20
2913 else
2914 woodNeeded = 7
2915 numPlanksNeeded = 28
2916 end
2917
2918 woodInStock = T:getLogStock()
2919 if woodInStock >= woodNeeded then
2920 stockData = T:getStock("minecraft:log")
2921 if stockData.total < woodNeeded then
2922 stockData = T:getStock("minecraft:log2")
2923 if stockData.total >= woodNeeded then
2924 logType = "minecraft:log2"
2925 end
2926 end
2927 T:craft("minecraft:planks", numPlanksNeeded, logType, nil, nil, false)
2928 stockData = T:getStock("minecraft:stick")
2929 if stockData.total < 1 then
2930 T:craft("minecraft:stick", 4, "minecraft:planks", nil, nil, false)
2931 end
2932 if T:craft("minecraft:sign", signsNeeded, "minecraft:stick", "minecraft:planks", nil, false) then
2933 success = true
2934 end
2935 stockData = T:getStock("minecraft:planks")
2936 if stockData.total > 0 then
2937 turtle.select(stockData.mostSlot)
2938 turtle.refuel()
2939 end
2940 stockData = T:getStock("minecraft:stick")
2941 if stockData.total > 0 then
2942 turtle.select(stockData.mostSlot)
2943 turtle.dropDown()
2944 end
2945 end
2946 return success
2947end
2948
2949function craftSticks(sticksNeeded)
2950 local success = false
2951 local makePlanks = false
2952 local woodNeeded = 0
2953 local planksNeeded = 0
2954 local doContinue = true
2955 local stockData = {}
2956
2957 if sticksNeeded <= 4 then
2958 sticksNeeded = 4
2959 planksNeeded = 4
2960 woodNeeded = 1
2961 elseif sticksNeeded <= 8 then
2962 sticksNeeded = 8
2963 planksNeeded = 4
2964 woodNeeded = 1
2965 elseif sticksNeeded <= 12 then
2966 sticksNeeded = 12
2967 planksNeeded = 8
2968 woodNeeded = 2
2969 else
2970 sticksNeeded = 16
2971 planksNeeded = 8
2972 woodNeeded = 2
2973 end
2974
2975 stockData = T:getStock("minecraft:planks")
2976 if stockData.total < planksNeeded then
2977 makePlanks = true
2978 else
2979 woodNeeded = 0 --reset
2980 end
2981
2982 if makePlanks then --need wood
2983 stockData = T:getLogData()
2984 if stockData.total >= woodNeeded then
2985 T:craft("minecraft:planks", planksNeeded, stockData.mostName, nil, nil, false)
2986 else
2987 doContinue = false
2988 end
2989 end
2990 if doContinue then
2991 if T:craft("minecraft:stick", sticksNeeded, "minecraft:planks", nil, nil, false) then
2992 success = true
2993 end
2994 end
2995
2996 return success
2997end
2998
2999function craftTorches(torchesNeeded)
3000 -- 4 torches min : 1 head + 1 stick = 4 torches. Min sticks = 4, min planks = 4
3001 -- torches head planks sticks
3002 -- 4 1 4 4
3003 -- 8 2 4 4
3004 -- 12 3 4 4
3005 -- 16 4 4 4
3006 -- 20 5 4 8
3007 -- 24 6 4 8
3008 -- 28 7 4 8
3009 -- 32 8 4 8
3010 -- 36 9 8 12
3011 -- 40 10 8 12
3012 -- 44 11 8 12
3013 -- 48 12 8 12
3014 -- 52 13 8 16
3015 -- 56 14 8 16
3016 -- 60 15 8 16
3017 -- 64 16 8 16
3018 local logType = "minecraft:log"
3019 local headModifier = 1 --charcoal
3020 local headQuantity = 0
3021 local makeCharcoal = false
3022 local makePlanks = false
3023 local success = false
3024 local doContinue = true
3025 local woodNeeded = 0
3026 local planksNeeded = 0
3027 local sticksNeeded = 0
3028 local coalInStock = 0
3029 local charcoalInStock = 0
3030 local sticksInStock = 0
3031 local planksInStock = 0
3032 local stockData = {}
3033
3034 T:saveToLog("craftTorches("..torchesNeeded..")")
3035 --turtle will be above storage chest to run this function
3036 turtle.select(1)
3037 --get sticks from storage
3038 while turtle.suckDown() do end
3039 stockData = T:getStock("minecraft:stick")
3040 sticksInStock = stockData.total
3041 torchesNeeded = math.floor(torchesNeeded / 4) * 4 -- correct torchesNeeded to multiple of 4
3042 headQuantity = torchesNeeded / 4 -- coal, charcoal
3043 stockData = T:getStock("minecraft:planks", -1)
3044 planksInStock = stockData.total
3045 -- calculate sticks/planks/logs needed
3046 if torchesNeeded == 0 then
3047 torchesNeeded = 4 -- torches min 4
3048 if sticksInStock < 4 then
3049 planksNeeded = 4 -- 1 wood
3050 sticksNeeded = 4 -- 2 planks
3051 woodNeeded = 1
3052 end
3053 elseif torchesNeeded <= 16 then-- 4, 8, 12, 16
3054 if sticksInStock < 4 then
3055 planksNeeded = 4 -- 8 planks = 16 sticks
3056 sticksNeeded = 4 -- 4 sticks
3057 woodNeeded = 1
3058 end
3059 elseif torchesNeeded <= 32 then-- 4, 8, 12, 16
3060 if sticksInStock < 8 then
3061 planksNeeded = 4 -- 8 planks = 16 sticks
3062 sticksNeeded = 8 -- 8 sticks
3063 woodNeeded = 2
3064 end
3065 elseif torchesNeeded <= 48 then-- 4, 8, 12, 16
3066 if sticksInStock < 12 then
3067 planksNeeded = 8 -- 8 planks = 16 sticks
3068 sticksNeeded = 12 -- 12 sticks
3069 woodNeeded = 2
3070 end
3071 else
3072 if sticksInStock < 16 then
3073 planksNeeded = 8 -- 8 planks = 16 sticks
3074 sticksNeeded = 16 -- 16 sticks
3075 woodNeeded = 2
3076 end
3077 end
3078 --need either coal or charcoal
3079 stockData = T:getStock("minecraft:coal", 0)
3080 coalInStock = stockData.total
3081 stockData = T:getStock("minecraft:coal", 1)
3082 charcoalInStock = stockData.total
3083
3084 if coalInStock >= headQuantity then
3085 headModifier = 0 --use coal
3086 else
3087 headModifier = 1 -- use charcoal
3088 end
3089 if headModifier == 1 then --use charcoal
3090 if charcoalInStock < headQuantity then
3091 makeCharcoal = true
3092 end
3093 -- extra logs needed for charcoal production
3094 woodNeeded = woodNeeded + headQuantity
3095 -- extra logs needed for charcoal fuel
3096 woodNeeded = woodNeeded + torchesNeeded / 4
3097 end
3098 T:saveToLog("craftTorches("..torchesNeeded..") coal="..coalInStock..", charcoal="..charcoalInStock..", planksNeeded="..planksNeeded..", woodNeeded="..woodNeeded)
3099 -- amount of logs needed known
3100 stockData = T:getStock("minecraft:log", -1)
3101 if stockData.total < woodNeeded then
3102 stockData = T:getStock("minecraft:log2")
3103 if stockData.total >= woodNeeded then
3104 logType = "minecraft:log2"
3105 else -- not enough log/log2 onboard
3106 getItemFromStorage("minecraft:log", true) -- get all types of log from storage
3107 stockData = T:getStock("minecraft:log")
3108 if stockData.total < woodNeeded then
3109 stockData = T:getStock("minecraft:log2")
3110 if stockData.total >= woodNeeded then
3111 logType = "minecraft:log2"
3112 else -- not enough logs to make torches
3113 doContinue = false
3114 end
3115 else
3116 logType = "minecraft:log"
3117 end
3118 end
3119 end
3120 T:saveToLog("craftTorches("..torchesNeeded..") using "..logType)
3121
3122 if doContinue then --enough raw materials onboard
3123 if sticksInStock == 0 or sticksInStock < headQuantity then
3124 stockData = T:getStock("minecraft:stick")
3125 if stockData.total < sticksNeeded then
3126 doContinue = false
3127 T:saveToLog("craftTorches("..torchesNeeded..") crafting sticks")
3128 if craftSticks(sticksNeeded) then
3129 doContinue = true
3130 end
3131 end
3132 end
3133 if doContinue then
3134 if makeCharcoal then
3135 doContinue = false
3136 T:saveToLog("craftTorches("..torchesNeeded..") smelting charcoal")
3137 if smelt(logType, headQuantity) then
3138 doContinue = true
3139 end
3140 end
3141 if doContinue then
3142 --make torches
3143 T:saveToLog("craftTorches("..torchesNeeded..") crafting torches...")
3144 if T:craft("minecraft:torch", torchesNeeded, "minecraft:stick", "minecraft:coal", nil, false) then
3145 success = true
3146 end
3147 end
3148 end
3149 stockData = T:getStock("minecraft:stick", -1)
3150 if stockData.total > 0 then
3151 T:saveToLog("craftTorches("..torchesNeeded..") storing sticks")
3152 turtle.select(stockData.mostSlot)
3153 turtle.dropDown()
3154 end
3155 end
3156
3157 return success
3158end
3159
3160function createMine(status)
3161 -- initial excavation in centre complete.
3162 T:saveToLog("createMine() Starting...", true)
3163 if status == 5 then
3164 createMinePrepare(14)
3165 T:saveToLog("createMine() Starting Level 14", true)
3166 createMineLevel(14)
3167 status = 6
3168 T:saveToLog("createMine() Level 14 complete. Saving Status '6'", true)
3169 end
3170 if status == 6 then
3171 createMinePrepare(11)
3172 T:saveToLog("createMine() Starting Level 11", true)
3173 createMineLevel(11)
3174 status = 7
3175 T:saveToLog("createMine() Level 11 complete. Saving Status '7'", true)
3176 end
3177 if status == 7 then
3178 createMinePrepare(8)
3179 T:saveToLog("createMine() Starting Level 8", true)
3180 createMineLevel(8)
3181 status = 8
3182 T:saveToLog("createMine() Level 8 complete. Saving Status '8'", true)
3183 end
3184 return status
3185end
3186
3187function createMinePrepare(level)
3188 local logsRequired = 0
3189 local stockData = T:getStock("minecraft:coal", -1) --either coal or charcoal
3190 local numCoal = stockData.total
3191 local numLogs = T:getLogStock()
3192
3193 emptyTurtle(false) -- keeps iron_ore, cobble, dirt, bucket, torches, signs, log, log2
3194 -- may have iron ore so attempt bucket crafting / check if bucket onboard
3195 if craftBucket() then -- checks or crafts a bucket
3196 checkTreeFarm()
3197 end
3198 -- wood needed for level 14 signs: 2 for signs
3199 -- wood needed for all levels: 5 for fuel, 3 for torches, 2 for chests
3200 if level == 14 then
3201 logsRequired = 12
3202 else
3203 logsRequired = 10
3204 end
3205 if numCoal >= 2 then
3206 logsRequired = logsRequired - 2
3207 end
3208 if numLogs < logsRequired then
3209 if treeFarm:getFarmCreated() then
3210 waitForTreeFarm(10)
3211 end
3212 end
3213 numLogs = T:getLogStock()
3214 local slot, modifier, total = T:getItemSlot("minecraft:chest", -1)
3215 if total < 2 and numLogs >= 2 then -- make 1 chest if 2 logs
3216 T:craftChests(1, true)
3217 numLogs = T:getLogStock()
3218 end
3219 slot, modifier, total = T:getItemSlot("minecraft:torch", -1)
3220 if total < 24 then
3221 if numLogs >= 4 or (numLogs >= 2 and numCoal >= 2) then
3222 craftTorches(24 - total) -- craft 24 torches 3 logs
3223 numLogs = T:getLogStock()
3224 end
3225 end
3226 if level == 14 then
3227 slot, modifier, total = T:getItemSlot("minecraft:sign", -1)
3228 if total < 1 and numLogs >= 2 then
3229 craftSigns(3) -- craft 3 signs 2 logs
3230 end
3231 end
3232 stockData = T:getStock("minecraft:log")
3233 if stockData.mostSlot ~= stockData.leastSlot then -- 2 types of log, so craft planks and refuel
3234 if stockData.leastCount <= 16 then
3235 T:craft("minecraft:planks", stockData.leastCount * 4, "minecraft:log", nil, nil, true)
3236 end
3237 end
3238 stockData = T:getStock("minecraft:sign")
3239 T:saveToLog("createMinePrepare("..level..") Signs avaiable: "..stockData.total, true)
3240 stockData = T:getStock("minecraft:torch")
3241 T:saveToLog("createMinePrepare("..level..") Torches avaiable: "..stockData.total, true)
3242 stockData = T:getStock("minecraft:chest")
3243 T:saveToLog("createMinePrepare("..level..") Chests avaiable: "..stockData.total - 1, true)
3244end
3245
3246function createMineLevel(level)
3247 local tempYcoord = T:getY()
3248 local doContinue = false
3249 local blockType = ""
3250 local stockData = {}
3251
3252 -- go down to level 14, 11, 8 and create + shape with firstTree at centre, 16 blocks length each arm
3253 -- torches at each end, and half way along
3254 T:forward(16) --now over mineshaft site
3255 if level == 14 then --first level so create mineshaft
3256 -- level out mine entrance and mark with a sign
3257 T:go("+0")
3258 while T:getY() >= tempYcoord - 1 do
3259 T:go("x1R1x1R1x1R1x1R1D1")
3260 end
3261 T:go("U1C1R1C1R1C1R1C1R1U1")
3262 if T:getItemSlot("minecraft:sign") > 0 then
3263 T:dig("forward")
3264 turtle.select(T:getItemSlot("minecraft:sign"))
3265 turtle.place("Diamond Mine\nPreparing for\nDeep Excavation\nLevel "..tostring(level))
3266 --dump remaining signs
3267 turtle.dropUp()
3268 end
3269 else
3270 T:dig("forward") -- dig existing sign
3271 if T:getItemSlot("minecraft:sign") > 0 then
3272 turtle.select(T:getItemSlot("minecraft:sign"))
3273 turtle.place("Diamond Mine\nPreparing for\nDeep Excavation\nLevel "..tostring(level))
3274 end
3275 end
3276 mineEntrance:setX(T:getX())
3277 mineEntrance:setY(T:getY())
3278 mineEntrance:setZ(T:getZ())
3279 mineEntrance:setFacing(T:getFacing())
3280 T:turnRight(2)
3281 while T:getY() > level do
3282 T:down(1)
3283 if level == 14 then -- shaft mining, check for valuables
3284 for i = 1, 4 do
3285 if T:isValuable("forward") then
3286 mineItem(true, "forward")
3287 else
3288 blockType = T:getBlockType("forward")
3289 if blockType == "minecraft:water" or
3290 blockType == "minecraft:flowing_water" or
3291 blockType == "minecraft:lava" or
3292 blockType == "minecraft:flowing_lava" then
3293 T:go("C1")
3294 end
3295 end
3296 T:turnRight(1)
3297 end
3298 end
3299 end
3300 T:go("x2R2C1D1C1R2")
3301 --ready to create mine at level 14 37x37 square
3302 T:go("m32U1R2M16D1", true, 8) -- mine ground level, go up, reverse and mine ceiling to mid-point, drop to ground
3303 T:place("minecraft:chest", -1, "down") --place chest in ground
3304 T:up(1) -- ready for emptyTrash() which moves down/up automatically
3305 T:emptyTrash("down")
3306 T:go("D1R1m16U1R2M16", true, 8) -- mine floor/ceiling of right side branch
3307 T:emptyTrash("down")
3308 T:go("D1m16U1R2M16", true, 8) -- mine floor/ceiling of left side branch
3309 T:emptyTrash("down")
3310 T:go("L1M15F1R1D1", true, 8) -- mine ceiling of entry coridoor, turn right
3311 T:go("n16R1n32R1n32R1n32R1n16U1", true, 8)-- mine floor of 36 x 36 square coridoor
3312 T:go("R1F16R2") --return to centre
3313 T:emptyTrash("down")
3314 T:go("F16R1") --return to entry shaft
3315 T:go("Q16R1Q32R1Q32R1Q32R1Q16x0F1R1", true, 8) --mine ceiling of 36x36 square coridoor. return to entry shaft + 1
3316 -- get rid of any remaining torches
3317 while T:getItemSlot("minecraft:torch", -1) > 0 do
3318 turtle.select(T:getItemSlot("minecraft:torch", -1))
3319 turtle.drop()
3320 end
3321 for i = 1, 8 do
3322 T:go("N32L1F1L1", true)
3323 T:dumpRefuse()
3324 T:go("N32R1F1R1", true)
3325 T:dumpRefuse()
3326 end
3327 T:go("R1F1R2C1R2F17L1") -- close gap in wall, return
3328 for i = 1, 8 do
3329 T:go("N32R1F1R1", true)
3330 T:dumpRefuse()
3331 T:go("N32L1F1L1", true)
3332 T:dumpRefuse()
3333 end
3334 T:go("L1F1R2C1R2F16R1") -- close gap in wall, return
3335 --return to surface
3336 while T:getY() < mineEntrance:getY() do
3337 T:up(1)
3338 end --at surface now
3339 T:forward(16)
3340 T:turnRight(2)
3341end
3342
3343function emptyAfterHarvest()
3344 local item = ""
3345 local keepit = false
3346 local keepItems = { "minecraft:dirt", "minecraft:cobblestone", "minecraft:chest", "minecraft:log", "minecraft:log2", "minecraft:coal",
3347 "minecraft:sapling", "minecraft:iron_ore", "minecraft:diamond", "minecraft:redstone", "minecraft:sign", "minecraft:torch",
3348 "minecraft:water_bucket", "minecraft:lava_bucket", "minecraft:bucket", "minecraft:planks",
3349 "minecraft:sand", "minecraft:reeds", "minecraft:iron_ingot", "minecraft:emerald"}
3350
3351 -- just finished harvesting all trees. get rid of apples, seeds any other junk
3352 -- or finished finding cobble after going into a disused mine
3353 for i = 1, 16 do
3354 keepit = false
3355 if turtle.getItemCount(i) > 0 then
3356 item = T:getItemName(i)
3357 for _,v in pairs(keepItems) do
3358 if v == item then
3359 keepit = true
3360 break
3361 end
3362 end
3363 if not keepit then
3364 turtle.select(i)
3365 turtle.dropDown()
3366 end
3367 end
3368 end
3369end
3370
3371function emptyTurtle(keepAll)
3372 -- empty out turtle except for logs, coal, torches, signs, bucket, cobble, dirt,diamonds
3373 -- chest1 = sticks
3374 -- chest2 = logs (oak/birch/jungle etc)
3375 -- chest3 = cobblestone, dirt
3376 -- chest4 = sand
3377 -- chest5 = iron_ore
3378 -- chest6 = redstone
3379 -- chest7 = diamond
3380 -- chest8 = gold_ore, lapis, mossy_cobblestone, obsidian
3381 -- chest9 = saplings (oak/birch,jungle)
3382
3383 local itemSlot = 0
3384 local stockData = {}
3385
3386 T:sortInventory()
3387 stockData = T:getStock("minecraft:planks", -1)
3388 if stockData.total > 0 then
3389 turtle.select(stockData.leastSlot)
3390 turtle.refuel()
3391 end
3392
3393 --sapling chest
3394 T:go("R1F2D1R2")
3395 while turtle.suckDown() do end --remove any junk in the sapling chest as well
3396 T:dropItem("minecraft:sapling", 0, "down")-- now only saplings in there
3397 T:go("U1F2R1")
3398 -- chest1 sticks only
3399 T:dropItem("minecraft:stick", 0, "down")
3400
3401 T:forward(2) -- chest2 logs of any type, keep 1 stack
3402 T:dropItem("minecraft:log", 64, "down")
3403 T:dropItem("minecraft:log2", 64, "down")
3404
3405 T:forward(2) -- chest3 dirt and cobble
3406 if not keepAll then -- keep max 1 stack cobble
3407 T:dropItem("minecraft:cobblestone", 64, "down")
3408 T:dropItem("minecraft:dirt", 64, "down")
3409 end
3410
3411 T:forward(2) -- chest4 sand and reeds
3412 T:dropItem("minecraft:sand", 0, "down")
3413 T:dropItem("minecraft:reeds", 0, "down")
3414
3415 T:forward(2) -- chest5 iron ore only
3416 T:dropItem("minecraft:iron_ore", 64, "down")
3417
3418 T:forward(2) -- redstone
3419 T:dropItem("minecraft:redstone", 0, "down")
3420 T:forward(4)
3421 -- chest8 gold ore, mossy cobblestone, obsidian, lapis, mob drops, misc items
3422 for i = 1, 16 do
3423 itemName = T:getItemName(i)
3424 if itemName ~= ""
3425 and itemName ~= "minecraft:chest"
3426 and itemName ~= "minecraft:torch"
3427 and itemName ~= "minecraft:iron_ore"
3428 and itemName ~= "minecraft:cobblestone"
3429 and itemName ~= "minecraft:dirt"
3430 and itemName ~= "minecraft:log"
3431 and itemName ~= "minecraft:log2"
3432 and itemName ~= "minecraft:sign"
3433 and itemName ~= "minecraft:coal"
3434 and itemName ~= "minecraft:diamond"
3435 and itemName ~= "minecraft:bucket" then
3436 turtle.select(i)
3437 turtle.dropDown()
3438 end
3439 end
3440 T:go("R2F14R2")
3441end
3442
3443function fillPond()
3444 local doContinue = false
3445 local fileHandle = ""
3446 local strText = ""
3447 local waterFound = false
3448 local numArms = 0
3449 local startFile = ""
3450 local itemSlot = 0
3451 local stockData = {}
3452 T:saveToLog("fillPond() started", true)
3453 -- reset turtle coordinates from file in case error has occurred with current location
3454 getCoords(true)
3455 -- read 'waterCoords.txt' from file
3456 if fs.exists("waterCoords.txt") then
3457 fileHandle = fs.open("waterCoords.txt", "r")
3458 strText = fileHandle.readLine()
3459 water:setX(tonumber(string.sub(strText, 3)))
3460 strText = fileHandle.readLine()
3461 water:setY(tonumber(string.sub(strText, 3)))
3462 strText = fileHandle.readLine()
3463 water:setZ(tonumber(string.sub(strText, 3)))
3464 fileHandle.close()
3465 T:saveToLog("Water coords from file x="..water:getX()..", y="..water:getY()..", z="..water:getZ(), true)
3466 end
3467 --T:sortInventory()
3468 stockData = T:getStock("minecraft:bucket", -1)
3469 if stockData.total == 0 then
3470 if craftBucket() then
3471 doContinue = true
3472 end
3473 else
3474 doContinue = true
3475 end
3476 if doContinue then -- bucket available
3477 -- fetch 2 buckets water and charge water pool
3478 -- place 4 buckets water in tree farm corners
3479 -- dig 4 corner blocks in treefarm to allow water flow down to hoppers
3480 goToWater() -- calls safeRun which constantly checks for water
3481 if T:getItemSlot("minecraft:water_bucket") == 0 then --no water found yet
3482 while not turtle.inspectDown() do -- must be air below
3483 T:down(1)
3484 end
3485 safeRun(1, "minecraft:water")
3486 T:turnRight(1)
3487 safeRun(1, "minecraft:water")
3488 T:turnRight(1) --now facing south, ready to continue spiral
3489 end
3490 if T:getItemSlot("minecraft:water_bucket") == 0 then
3491 numArms = 4
3492 for i = 2, numArms, 2 do
3493 safeRun(i, "minecraft:water")
3494 T:turnRight(1)
3495 safeRun(i, "minecraft:water")
3496 T:turnRight(1)
3497 safeRun(i + 1, "minecraft:water")
3498 T:turnRight(1)
3499 if i + 1 < numArms then
3500 safeRun(i + 1, "minecraft:water")
3501 T:turnRight(1)
3502 else
3503 --return to starting position
3504 safeRun(i / 2, "minecraft:water")
3505 T:turnRight(1)
3506 safeRun(i / 2 + 1, "minecraft:water")
3507 end
3508 end
3509 --changeDirection()
3510 if T:getItemSlot("minecraft:water_bucket") > 0 then
3511 startFile = fs.open("waterCoords.txt", "w")
3512 startFile.writeLine("x="..T:getX())
3513 startFile.writeLine("y="..T:getY())
3514 startFile.writeLine("z="..T:getZ())
3515 startFile.close()
3516 end
3517 end
3518 itemSlot = T:getItemSlot("minecraft:water_bucket", -1)
3519 if itemSlot > 0 then
3520 treeFarm:setWaterFound(true)
3521 returnHome()
3522 T:go("L1F3")
3523 turtle.select(T:getItemSlot("minecraft:water_bucket"))
3524 if turtle.placeDown() then
3525 T:saveToLog("Water added to pond at: x="..T:getX()..", y="..T:getY()..", z="..T:getZ(),true)
3526 T:go("R2F3L1")
3527 else
3528 T:go("L1F1L1F1")
3529 if turtle.placeDown() then
3530 T:saveToLog("Water added (after moving) to pond at: x="..T:getX()..", y="..T:getY()..", z="..T:getZ(),true)
3531 end
3532 T:go("L1F1R1F2L1")
3533 end
3534 goToWater()
3535 returnHome()
3536 T:go("L1F2L1F1")
3537 itemSlot = T:getItemSlot("minecraft:water_bucket")
3538 if itemSlot > 0 then
3539 turtle.select(itemSlot)
3540 if turtle.placeDown() then
3541 T:saveToLog("Water added to pond at: x="..T:getX()..", y="..T:getY()..", z="..T:getZ(),true)
3542 T:go("R2F1R1F2L1")
3543 else
3544 T:go("R1F1R1F1")
3545 if turtle.placeDown() then
3546 T:saveToLog("Water added (after moving) to pond at: x="..T:getX()..", y="..T:getY()..", z="..T:getZ(),true)
3547 end
3548 T:go("R1F3L1")
3549 end
3550 else
3551 returnHome()
3552 end
3553 else
3554 print("Water not found")
3555 error()
3556 end
3557 end
3558 treeFarm:setPondFilled(true)
3559 saveStatus("treeFarm")
3560end
3561
3562function findCobble()
3563 local atBedrock = false
3564 local blockType = ""
3565 local tempY = T:getY()
3566 T:saveToLog("findCobble() Starting...", true)
3567 --just finished harvesting all trees. get rid of apples, seeds any other junk
3568 emptyAfterHarvest()
3569 T:down(1)
3570 repeat
3571 turtle.select(1)
3572 if not T:down(1) then
3573 atBedrock = true
3574 end
3575 for i = 1, 4 do
3576 if T:isValuable("forward") then
3577 -- call recursive function!!!
3578 mineItem(true, "forward")
3579 else
3580 T:dig("forward")
3581 if T:getItemSlot("minecraft:gravel") > 0 then
3582 turtle.select(T:getItemSlot("minecraft:gravel"))
3583 turtle.drop()
3584 elseif T:getItemSlot("minecraft:flint") > 0 then
3585 turtle.select(T:getItemSlot("minecraft:flint"))
3586 turtle.drop()
3587 end
3588 end
3589 T:turnRight(1)
3590 turtle.select(1)
3591 end
3592 until atBedrock
3593 repeat
3594 T:up(1)
3595 until T:getY() == tempY - 4
3596 turtle.select(T:getItemSlot("minecraft:cobblestone"))
3597 for i = 1, 3 do
3598 for j = 1, 4 do
3599 turtle.select(T:getItemSlot("minecraft:cobblestone"))
3600 turtle.place()
3601 T:turnRight(1)
3602 end
3603 T:up(1)
3604 end
3605 T:up(1)
3606end
3607
3608function floodTreeFarm()
3609 local slot = 0
3610 local success = true
3611 T:saveToLog("floodTreeFarm() started", true)
3612 T:go("L1F2R2") -- go to pond
3613
3614 for i = 1, 9, 2 do --flood farm except for drainage channel
3615 slot = T:getItemSlot("minecraft:bucket")
3616 if slot > 0 then
3617 turtle.select(slot)
3618 turtle.placeDown() -- Fill 1
3619 end
3620 if i < 9 then
3621 T:forward(4 + i)
3622 else
3623 T:forward(12) -- far right side of farm
3624 end
3625 T:go("L1F8U1") --T:go("L FFFF FFFF U")
3626 slot = T:getItemSlot("minecraft:water_bucket")
3627 if slot > 0 then
3628 turtle.select(slot)
3629 turtle.placeDown()
3630 end
3631 T:turnLeft(1)
3632 if i < 9 then -- return to left wall of farm
3633 T:forward(i)
3634 else
3635 T:forward(8)
3636 end
3637 T:go("L1F8D1R1F4R2")
3638 -- ready for next bucket fill
3639 end
3640 slot = T:getItemSlot("minecraft:bucket")
3641 if slot > 0 then
3642 turtle.select(slot)
3643 turtle.placeDown()
3644 end
3645 T:forward(12)
3646 slot = T:getItemSlot("minecraft:water_bucket")
3647 if slot > 0 then
3648 turtle.select(slot)
3649 turtle.placeDown()
3650 T:saveToLog("floodTreeFarm() completed")
3651 else
3652 T:saveToLog("floodTreeFarm() failed: no water obtained", true)
3653 success = false
3654 end
3655 T:go("R2F10R1")
3656 treeFarm:setFarmFlooded(true)
3657 saveStatus("treeFarm")
3658
3659 return success
3660end
3661
3662function getCoords(fromFile)
3663 fromFile = fromFile or false
3664 --get world coordinates from player
3665 local coord = 0
3666 local response = ""
3667 local continue = true
3668 local event = ""
3669 local param1 = ""
3670
3671 term.clear()
3672 term.setCursorPos(1,1)
3673
3674 if fs.exists("homeCoords.txt") then
3675 fileHandle = fs.open("homeCoords.txt", "r")
3676 strText = fileHandle.readLine()
3677 T:setX(tonumber(string.sub(strText, 3)))
3678 coordHome:setX(T:getX())
3679 strText = fileHandle.readLine()
3680 T:setY(tonumber(string.sub(strText, 3)))
3681 coordHome:setY(T:getY())
3682 strText = fileHandle.readLine()
3683 T:setZ(tonumber(string.sub(strText, 3)))
3684 strText = fileHandle.readLine()
3685 coordHome:setZ(T:getZ())
3686 T:setFacing(tonumber(string.sub(strText, 3)))
3687 coordHome:setFacing(T:getFacing())
3688 fileHandle.close()
3689 T:saveToLog("Coordinates loaded from file:", true)
3690 T:saveToLog("x = "..T:getX()..", y = "..T:getY()..", z = "..T:getZ()..", f = "..T:getFacing(), true)
3691 if not fromFile then
3692 print()
3693 print(os.getComputerLabel().." assumed to be at Home Location")
3694 print()
3695 print("starting in 3 seconds")
3696 print("STAND CLEAR!")
3697 os.sleep(3)
3698 end
3699 else
3700 print("IMPORTANT! Stand directly behind turtle")
3701 print("Press F3 to read coordinates")
3702 print()
3703 continue = true
3704 while continue do
3705 print("Please enter your X coordinate")
3706 write(" x = ")
3707 coord = nil
3708 while coord == nil do
3709 coord = tonumber(read())
3710 if coord == nil then
3711 term.clear()
3712 term.setCursorPos(1,1)
3713 print("Incorrect input. Use numbers only!")
3714 print()
3715 print("Please enter your X coordinate")
3716 write(" x = ")
3717 end
3718 end
3719 T:setX(coord)
3720 term.clear()
3721 term.setCursorPos(1,1)
3722 print("Please enter your Y coordinate")
3723 write(" y = ")
3724 coord = nil
3725 while coord == nil do
3726 coord = tonumber(read())
3727 if coord == nil then
3728 term.clear()
3729 term.setCursorPos(1,1)
3730 print("Incorrect input. Use numbers only")
3731 print()
3732 print("Please enter your y coordinate")
3733 write(" y = ")
3734 end
3735 end
3736 T:setY(coord)
3737 term.clear()
3738 term.setCursorPos(1,1)
3739 print("Please enter your Z coordinate")
3740 write(" z = ")
3741 coord = nil
3742 while coord == nil do
3743 coord = tonumber(read())
3744 if coord == nil then
3745 term.clear()
3746 term.setCursorPos(1,1)
3747 print("Incorrect input. Use numbers only")
3748 print()
3749 print("Please enter your z coordinate")
3750 write(" z = ")
3751 end
3752 end
3753 T:setZ(coord)
3754 response = true
3755 while response do
3756 response = false
3757 term.clear()
3758 term.setCursorPos(1,1)
3759 print("Enter Direction you are facing:")
3760 print(" 0,1,2,3 (s,w,n,e)")
3761 print()
3762 print( " Direction = ")
3763 event, param1 = os.pullEvent ("char")
3764 if param1 == "s" or param1 == "S" then
3765 coord = 0
3766 elseif param1 == "w" or param1 == "W" then
3767 coord = 1
3768 elseif param1 == "n" or param1 == "N" then
3769 coord = 2
3770 elseif param1 == "e" or param1 == "E" then
3771 coord = 3
3772 elseif param1 == "0" or param1 == "1" or param1 == "2" or param1 == "3" then
3773 coord = tonumber(param1)
3774 else
3775 print()
3776 print("Incorrect input: "..param1)
3777 print()
3778 print("Use 0,1,2,3,n,s,w,e")
3779 sleep(2)
3780 response = true
3781 end
3782 end
3783 T:setFacing(coord)
3784 term.clear()
3785 term.setCursorPos(1,1)
3786 print("Your current location is:")
3787 print()
3788 print(" x = "..T:getX())
3789 print(" y = "..T:getY())
3790 print(" z = "..T:getZ())
3791 print(" facing "..T:getCompass().." ("..T:getFacing()..")")
3792 print()
3793 write("Is this correct? (y/n)")
3794 event, param1 = os.pullEvent ("char")
3795 if param1 == "y" or param1 == "Y" then
3796 continue = false
3797 end
3798 end
3799 -- correct coords to compensate for player standing position
3800 -- First tree is considered as point zero, on startup, turtle is in front of this tree
3801 -- Player is behind turtle, use 2 blocks to compensate
3802 -- facing: Change:
3803 -- 0 (S) z+1
3804 -- 1 (W) x-1
3805 -- 2 (N) z-1
3806 -- 3 (E) x+1
3807
3808 if T:getFacing() == 0 then
3809 T:setZ(T:getZ() + 2)
3810 elseif T:getFacing() == 1 then
3811 T:setX(T:getX() - 2)
3812 elseif T:getFacing() == 2 then
3813 T:setZ(T:getZ() - 2)
3814 elseif T:getFacing() == 3 then
3815 T:setX(T:getX() + 2)
3816 end
3817 coordHome:setX(T:getX())
3818 coordHome:setY(T:getY())
3819 coordHome:setZ(T:getZ())
3820 coordHome:setFacing(T:getFacing())
3821 -- create/overwrite 'homeCoords.txt'
3822 local fileHandle = fs.open("homeCoords.txt", "w")
3823 fileHandle.writeLine("x="..T:getX())
3824 fileHandle.writeLine("y="..T:getY())
3825 fileHandle.writeLine("z="..T:getZ())
3826 fileHandle.writeLine("f="..T:getFacing())
3827 fileHandle.close()
3828 T:saveToLog("homeCoords.txt file created", true)
3829 T:saveToLog("x = "..T:getX()..", y = "..T:getY()..", z = "..T:getZ()..", f = "..T:getFacing(), false)
3830 print()
3831 print("Press esc within 2 secs to watch the action!")
3832 sleep(2)
3833 end
3834 if not fromFile then
3835 term.clear()
3836 term.setCursorPos(1,1)
3837 print("Here we go...")
3838 sleep(2)
3839 term.clear()
3840 term.setCursorPos(1,1)
3841 end
3842end
3843
3844function getItemFromStorage(item, allItems)
3845 local success = false
3846 local moves = 0
3847 allItems = allItems or false
3848
3849 if item == "minecraft:log" or item == "minecraft:log2" then
3850 moves = 2
3851 elseif item == "minecraft:cobblestone" or item == "minecraft:dirt"then
3852 moves = 4
3853 elseif item == "minecraft:sand" or item == "minecraft:reeds" then
3854 moves = 6
3855 elseif item == "minecraft:iron_ore" then
3856 moves = 8
3857 numIronore = 0
3858 elseif item == "minecraft:redstone" then
3859 moves = 10
3860 elseif item == "minecraft:diamond" then
3861 moves = 12
3862 end
3863 T:forward(moves)
3864 turtle.select(1)
3865 if allItems then
3866 while turtle.suckDown() do -- all items
3867 success = true
3868 end
3869 if T:getFirstEmptySlot() == 0 then
3870 turtle.select(T:getItemSlot("minecraft:dirt"))
3871 turtle.dropDown()
3872 end
3873 else
3874 if turtle.suckDown() then -- item removed from chest
3875 success = true
3876 end
3877 end
3878 T:go("R2F"..moves.."R2")
3879
3880 return success
3881end
3882
3883function goToWater()
3884 local startX = T:getX()
3885 local startZ = T:getZ()
3886 local itemSlot = 0
3887 local waterCollected = false
3888 local nearHome = true
3889
3890 T:saveToLog("goToWater() started.", true)
3891 if T:getX() < water:getX() then
3892 while T:getFacing() ~= 3 do
3893 T:turnRight(1)
3894 end
3895 while T:getX() < water:getX() do
3896 if nearHome then
3897 T:forward(5)
3898 nearHome = false
3899 else
3900 safeRun(1, "minecraft:water")
3901 if T:getItemSlot("minecraft:water_bucket", -1) > 0 then
3902 waterCollected = true
3903 T:saveToLog("Water collected at: x="..T:getX()..", y="..T:getY()..", z="..T:getZ(),true)
3904 end
3905 if T:getX() < startX then --going wrong way
3906 T:turnRight(2)
3907 end
3908 end
3909 end
3910 elseif T:getX() > water:getX() then
3911 while T:getFacing() ~= 1 do
3912 T:turnRight(1)
3913 end
3914 while T:getX() > water:getX() do
3915 if nearHome then
3916 T:forward(5)
3917 nearHome = false
3918 else
3919 safeRun(1, "minecraft:water")
3920 if T:getItemSlot("minecraft:water_bucket", -1) > 0 then
3921 waterCollected = true
3922 T:saveToLog("Water collected at: x="..T:getX()..", y="..T:getY()..", z="..T:getZ(),true)
3923 end
3924 if T:getX() > startX then --going wrong way
3925 T:turnRight(2)
3926 end
3927 end
3928 end
3929 end
3930 if T:getZ() > water:getZ() then -- go north
3931 while T:getFacing() ~= 2 do
3932 T:turnRight(1)
3933 end
3934 while T:getZ() > water:getZ() do
3935 if nearHome then
3936 T:forward(5)
3937 nearHome = false
3938 else
3939 safeRun(1, "minecraft:water")
3940 if T:getItemSlot("minecraft:water_bucket", -1) > 0 then
3941 waterCollected = true
3942 T:saveToLog("Water collected at: x="..T:getX()..", y="..T:getY()..", z="..T:getZ(),true)
3943 end
3944 if T:getZ() > startZ then --going wrong way
3945 T:turnRight(2)
3946 end
3947 end
3948 end
3949 elseif T:getZ() < water:getZ() then
3950 while T:getFacing() ~= 0 do
3951 T:turnRight(1)
3952 end
3953 while T:getZ() < water:getZ() do
3954 if nearHome then
3955 T:forward(5)
3956 nearHome = false
3957 else
3958 safeRun(1, "minecraft:water")
3959 if T:getItemSlot("minecraft:water_bucket", -1) > 0 then
3960 waterCollected = true
3961 T:saveToLog("Water collected at: x="..T:getX()..", y="..T:getY()..", z="..T:getZ(),true)
3962 end
3963 if T:getZ() < startZ then --going wrong way
3964 T:turnRight(2)
3965 end
3966 end
3967 end
3968 end
3969 if not waterCollected then
3970 T:saveToLog("goToWater() completed: no water", true)
3971 else
3972 T:saveToLog("goToWater() completed: water collected", true)
3973 end
3974end
3975
3976function harvestAllTrees()
3977 -- create a 33 x 33 square with base camp in the centre
3978 -- - make sure on solid ground
3979 -- - If wood in front harvest tree.
3980 -- - else if leaves in front dig then move
3981 -- - else any other block climb up and over
3982 -- move in a spiral
3983 local numReeds = 0
3984 local numSand = 0
3985 local numLogs = 0
3986 local numSaplings = 0
3987 local waterFound = false
3988 local stockData = {}
3989 local doContinue = true
3990
3991 T:saveToLog("harvestAllTrees() Starting...", true)
3992 harvestRun(1) -- move 1 blocks
3993 T:turnRight(1)
3994 harvestRun(1, true)
3995 T:turnRight(1) --now facing south, ready to continue spiral
3996 numArms = 32 --32 for full version 4 for testing
3997 for i = 2, numArms, 2 do -- 2,4,6,8...32
3998 if i < 5 then
3999 harvestRun(i, true)
4000 else
4001 harvestRun(i)
4002 end
4003 T:turnRight(1)
4004 if i < 5 then
4005 harvestRun(i, true)
4006 else
4007 harvestRun(i)
4008 end
4009 T:turnRight(1)
4010 if i < 5 then
4011 harvestRun(i + 1, true)
4012 else
4013 harvestRun(i + 1)
4014 end
4015 T:turnRight(1) --full square completed. Can return if supplies sufficient
4016 if i + 1 < numArms then
4017 -- toDo
4018 -- choose point to break
4019 T:saveToLog("harvestAllTrees() spiral arm no: "..i, true)
4020 stockData = T:getStock("minecraft:reeds")
4021 numReeds = stockData.total
4022 stockData = T:getStock("minecraft:sand")
4023 numSand = stockData.total
4024 stockData = T:getStock("minecraft:sapling")
4025 numSaplings = stockData.total
4026 numLogs = T:getLogStock()
4027 waterFound = treeFarm:getWaterFound()
4028 T:saveToLog("reeds = "..numReeds..", sand = "..numSand..", logs = "..numLogs..", Water found = "..tostring(waterFound),true)
4029 print("harvestAllTrees() spiral arm no: "..i)
4030 if numReeds >= 3 and numSand >= 6 and numLogs >= 40 and treeFarm:getWaterFound() then
4031 if (numLogs >= 30 and numSaplings >=4) or (numLogs > 60 and numSaplings == 0)then
4032 doContinue = false
4033 T:saveToLog("harvestAllTrees() Abandoned after "..i.." spiral arms: Supplies sufficient", true)
4034 --return to starting position
4035 harvestRun(i / 2)
4036 T:turnRight(1)
4037 harvestRun(i / 2 + 1)
4038 T:turnRight(2)
4039 break
4040 end
4041 end
4042 if i < 2 then
4043 harvestRun(i + 1, true)
4044 else
4045 harvestRun(i + 1)
4046 end
4047 T:turnRight(1)
4048 else -- i + 1 >= 32 (i = 32)
4049 --return to starting position
4050 harvestRun(i / 2)
4051 T:turnRight(1)
4052 harvestRun(i / 2 + 1)
4053 end
4054 end
4055 if doContinue then
4056 T:turnRight(2)
4057 end
4058end
4059
4060function harvestRun(runLength, digDirt)
4061 local itemSlot = 0
4062 local logType = ""
4063 local startFile = ""
4064 local success = false
4065 local blockType = ""
4066 local blockModifier
4067
4068 turtle.select(1)
4069 for i = 1, runLength do
4070 while not turtle.inspectDown() do -- must be air below
4071 turtle.suckDown() --pick any saplings
4072 T:down(1)
4073 end
4074 blockType, blockModifier = T:getBlockType("down")
4075 if blockType == "minecraft:sand" then
4076 itemSlot, blockModifier, total = T:getItemSlot("minecraft:sand", -1)
4077 if total < 6 then
4078 harvestSand()
4079 end
4080 elseif blockType == "minecraft:flowing_water" or blockType == "minecraft:water" then
4081 if blockModifier == 0 then -- source block
4082 if water:getX() == 0 then -- water coords not yet found
4083 --T:saveToLog("Water source found: checking suitability", true)
4084 --local relX = math.abs(math.abs(T:getX()) - math.abs(coordHome:getX()))
4085 --local relZ = math.abs(math.abs(T:getZ()) - math.abs(coordHome:getZ()))
4086 T:saveToLog("Water found, checking suitability: x="..T:getX()..", z="..T:getZ(), true)
4087 --if relX > 5 or relZ > 5 then
4088 if checkWaterCoordsOK(T:getX(),T:getZ()) then
4089 water:setX(T:getX())
4090 water:setY(T:getY())
4091 water:setZ(T:getZ())
4092 water:setFacing(T:getFacing())
4093 -- create/overwrite 'waterCoords.txt'
4094 startFile = fs.open("waterCoords.txt", "w")
4095 startFile.writeLine("x="..T:getX())
4096 startFile.writeLine("y="..T:getY())
4097 startFile.writeLine("z="..T:getZ())
4098 startFile.close()
4099 treeFarm:setWaterFound(true)
4100 T:saveToLog("Water confirmed and saved to file x="..water:getX()..", y="..water:getY()..", z="..water:getZ(), true)
4101 else
4102 T:saveToLog("Water too close to base camp x="..water:getX()..", y="..water:getY()..", z="..water:getZ(), true)
4103 end
4104 --else
4105 --T:saveToLog("Water found: too close to base camp (rel X="..relX..", rel Z="..relZ..")", true)
4106 --end
4107 end
4108 end
4109 end
4110 blockType, blockModifier = T:getBlockType("up")
4111 if blockType == "minecraft:log" or blockType == "minecraft:log2" then
4112 T:back(1)
4113 T:harvestTree()
4114 end
4115 if digDirt and i < 5 then
4116 turtle.select(1)
4117 if T:getY() == coordHome:getY() then
4118 T:dig("up")
4119 T:dig("down")
4120 end
4121 end
4122 turtle.suck()
4123
4124 if turtle.detect() then --can't move forward
4125 blockType, blockModifier = T:getBlockType("forward")
4126 if blockType == "minecraft:log" then
4127 T:harvestTree()
4128 T:back(1)
4129 elseif blockType == "minecraft:reeds" then
4130 T:saveToLog("Reeds found", true)
4131 while blockType == "minecraft:reeds" do -- continue loop while reeds detected in front
4132 T:up(1) -- Move up
4133 blockType, blockModifier = T:getBlockType("forward")
4134 end
4135 -- At top of the Reeds/Sugar Cane.
4136 T:forward(1)
4137 -- New loop to return to ground
4138 blockType, blockModifier = T:getBlockType("down")
4139 while blockType == "minecraft:reeds" do -- While sugar cane detected below
4140 T:down(1)
4141 blockType, blockModifier = T:getBlockType("down")
4142 end
4143 T:back(1)
4144 -- check if leaves or grass in front, dig through
4145 else --any block except log or reeds
4146 if T:isVegetation(blockType) then -- tallgrass,flower, vine etc
4147 T:dig("forward")
4148 else
4149 while turtle.detect() do
4150 T:up(1)
4151 blockType, blockModifier = T:getBlockType("forward")
4152 if T:isVegetation(blockType) then -- tallgrass,flower, vine etc
4153 break
4154 elseif blockType == "minecraft:log" or blockType == "minecraft:log2" then
4155 T:harvestTree()
4156 T:back(1)
4157 break
4158 end
4159 end
4160 end
4161 end
4162 end
4163 T:forward(1)
4164 -- check if wood below eg tree harvest started mid - trunk
4165 blockType, blockModifier = T:getBlockType("down")
4166 while blockType == "minecraft:log" or blockType == "minecraft:log2" do
4167 T:down(1)
4168 blockType, blockModifier = T:getBlockType("down")
4169 end
4170 end
4171end
4172
4173function harvestSand()
4174 local depth = 0
4175
4176 T:saveToLog("harvestSand() started", true)
4177 local data = T:getBlockType("down")
4178 while data == "minecraft:sand" do -- While sand detected below
4179 T:down(1)
4180 depth = depth - 1
4181 data = T:getBlockType("down")
4182 end
4183
4184 while depth < 0 do
4185 T:up(1)
4186 depth = depth + 1
4187 T:place("minecraft:dirt", -1, "down")
4188 end
4189 T:saveToLog("harvestSand() completed", true)
4190end
4191
4192function harvestAndReplant()
4193 if T:isValuable("forward") then -- log in front
4194 T:harvestTree()
4195 else
4196 turtle.select(1)
4197 T:forward(1) --sapling harvested if still present
4198 end
4199 itemSlot = T:getItemSlot("minecraft:sapling", -1)
4200 if itemSlot > 0 then -- move up, plant sapling, move forward and down
4201 T:up(1)
4202 turtle.select(itemSlot)
4203 turtle.placeDown()
4204 T:forward(1)
4205 T:down(1)
4206 else
4207 T:forward(1) --no sapling so simply move forward
4208 end
4209end
4210
4211function harvestTreeFarm()
4212 local itemSlot = 0
4213
4214 T:saveToLog("harvestTreeFarm() started", true)
4215 treeFarm:reset()
4216 T:go("R1F2D1")
4217 turtle.select(1)
4218 while turtle.suckDown() do end-- get saplings from chest
4219 T:go("U3F3L1F2")
4220 harvestAndReplant()
4221 T:forward(1)
4222 harvestAndReplant()
4223 T:go("R1F3R1")
4224 harvestAndReplant()
4225 T:forward(1)
4226 harvestAndReplant()
4227 T:go("F2R1F6D3")
4228 --store any remaining saplings
4229 T:dropItem("minecraft:sapling", 0)
4230 T:go("U1F2R1")
4231 --get rid of any apples
4232 if T:getItemSlot("minecraft:apple") > 0 then
4233 turtle.select(T:getItemSlot("minecraft:apple"))
4234 turtle.drop()
4235 end
4236 treeFarm:setTimePlanted(os.time())
4237 saveStatus("treeFarm")
4238 T:saveToLog("harvestTreeFarm() completed", true)
4239end
4240
4241function initialise()
4242 -- create classes/objects
4243 T = classTurtle()
4244 coordHome = classCoord("homeLocation")
4245 mineEntrance = classCoord("mineEntrance")
4246 water = classCoord("water")
4247 treeFarm = classTreeFarm()
4248 loadStatus("treeFarm")
4249end
4250
4251function loadStatus(obj)
4252 local fileHandle = ""
4253 local strText = ""
4254
4255 if obj == "treeFarm" then
4256 if fs.exists("treeFarm.txt") then
4257 fileHandle = fs.open("treeFarm.txt", "r")
4258 strText = fileHandle.readLine()
4259 if strText == "true" then
4260 treeFarm:setPondFilled(true)
4261 end
4262 strText = fileHandle.readLine()
4263 if strText == "true" then
4264 treeFarm:setFarmCreated(true)
4265 end
4266 strText = fileHandle.readLine()
4267 if strText == "true" then
4268 treeFarm:setHopperPlaced(true)
4269 end
4270 strText = fileHandle.readLine()
4271 if strText == "true" then
4272 treeFarm:setFarmFlooded(true)
4273 end
4274 strText = fileHandle.readLine()
4275 treeFarm:setTimePlanted(tonumber(strText))
4276 fileHandle.close()
4277 end
4278 else
4279 strText = "0"
4280 if fs.exists(obj) then
4281 fileHandle = fs.open(obj, "r")
4282 strText = fileHandle.readLine()
4283 fileHandle.close()
4284 end
4285 end
4286
4287 return strText
4288end
4289
4290function mineItem(do3D, direction)
4291 local doContinue = true
4292 local mineIt = false
4293 local blockType = ""
4294
4295 --RECURSIVE FUNCTION - BEWARE!
4296
4297 -- check if block in front is valuable. If so mine it
4298 -- direction only up/down if already called recursively
4299 if direction == "up" then
4300 mineIt, blockType = T:isValuable("up")
4301 if mineIt then
4302 T:dig("up")
4303 end
4304 -- move up into space dug out
4305 T:up(1)
4306 -- check if item in front is valuable
4307 mineIt, blockType = T:isValuable("forward")
4308 if mineIt then
4309 mineItem(do3D, "forward")
4310 end
4311 --check right side
4312 T:turnRight(1)
4313 mineIt, blockType = T:isValuable("forward")
4314 if mineIt then
4315 mineItem(do3D, "forward")
4316 end
4317 --check behind
4318 T:turnRight(1)
4319 mineIt, blockType = T:isValuable("forward")
4320 if mineIt then
4321 mineItem(do3D, "forward")
4322 end
4323 --check left side
4324 T:turnRight(1)
4325 mineIt, blockType = T:isValuable("forward")
4326 if mineIt then
4327 mineItem(do3D, "forward")
4328 end
4329 --return to front
4330 T:turnRight(1)
4331 T:down(1)
4332 end
4333 if direction == "down" then
4334 mineIt, blockType = T:isValuable("down")
4335 if mineIt then
4336 turtle.select(1)
4337 turtle.digDown()
4338 end
4339 -- move down into space dug out
4340 T:down(1)
4341 -- check if item in front is valuable
4342 mineIt, blockType = T:isValuable("forward")
4343 if mineIt then
4344 mineItem(do3D, "forward")
4345 end
4346 --check right side
4347 T:turnRight(1)
4348 mineIt, blockType = T:isValuable("forward")
4349 if mineIt then
4350 mineItem(do3D, "forward")
4351 end
4352 --check behind
4353 T:turnRight(1)
4354 mineIt, blockType = T:isValuable("forward")
4355 if mineIt then
4356 mineItem(do3D, "forward")
4357 end
4358 --check left side
4359 T:turnRight(1)
4360 mineIt, blockType = T:isValuable("forward")
4361 if mineIt then
4362 mineItem(do3D, "forward")
4363 end
4364 --return to front
4365 T:turnRight(1)
4366 T:up(1)
4367 end
4368 if direction == "forward" then
4369 mineIt, blockType = T:isValuable("forward")
4370 if mineIt then
4371 T:forward(1)
4372 mineIt, blockType = T:isValuable("up")
4373 if mineIt then
4374 if do3D then
4375 mineItem(do3D, "up")
4376 else
4377 T:dig("up")
4378 end
4379 end
4380 mineIt, blockType = T:isValuable("down")
4381 if mineIt then
4382 if do3D then
4383 mineItem(do3D, "down")
4384 else
4385 turtle.select(1)
4386 turtle.digDown()
4387 end
4388 end
4389 -- check if item in front is valuable
4390 mineIt, blockType = T:isValuable("forward")
4391 if mineIt then
4392 mineItem(do3D, "forward")
4393 end
4394 --check left side
4395 T:turnLeft(1)
4396 mineIt, blockType = T:isValuable("forward")
4397 if mineIt then
4398 mineItem(do3D, "forward")
4399 end
4400 -- check right side
4401 T:turnRight(2)
4402 mineIt, blockType = T:isValuable("forward")
4403 if mineIt then
4404 mineItem(do3D, "forward")
4405 end
4406 T:turnLeft(1)
4407 T:back(1)
4408 end
4409 end
4410end
4411
4412function mineToBedrock(status)
4413 local returnPath = ""
4414 local stockData = {}
4415 T:saveToLog("mineToBedrock() started", true)
4416 -- start at furnace
4417 T:sortInventory()
4418 emptyTurtle(false)
4419 T:forward(16)
4420 turtle.dig()
4421 if T:getItemSlot("minecraft:sign") > 0 then
4422 turtle.select(T:getItemSlot("minecraft:sign"))
4423 turtle.place("Diamond Mine\nMining for\nDiamonds\nTo Bedrock\nSector "..status)
4424 turtle.select(1)
4425 end
4426 mineEntrance:setX(T:getX())
4427 mineEntrance:setY(T:getY())
4428 mineEntrance:setZ(T:getZ())
4429 mineEntrance:setFacing(T:getFacing())
4430 T:turnRight(2)
4431 while T:getY() > 8 do
4432 T:down(1)
4433 end
4434 -- now at base of shaft, above torch, level 7
4435 while status < 12 do -- break when status = 12 or enough diamonds are found
4436 stockData = T:getStock("minecraft:diamond", 0) -- check numbers
4437 if stockData.total >= 6 then
4438 break
4439 end
4440 if status <= 8 then -- no sectors mined
4441 returnPath = "L1U1F15L1"
4442 T:go("F1L1F1R1D1")
4443 status = 9
4444 elseif status <= 9 then -- lower left sector mined: do upper left
4445 returnPath = "L1U1F15R1F16R2"
4446 T:go("F17L1F1R1D1")
4447 status = 10
4448 elseif status <= 10 then -- both left sectors mined: do lower right
4449 returnPath = "R1U1F1R1"
4450 T:go("R1F15L1F1D1")
4451 status = 11
4452 elseif status <= 11 then -- both left + lower right sectors mined: do upper right
4453 returnPath = "U1F16R1F1R1"
4454 T:go("F16R1F15L1F1D1")
4455 status = 12
4456 end
4457 for i = 1, 4 do
4458 T:go("Z7U1L1F2L1F1D1Z7")
4459 if i < 4 then
4460 T:go("U1R1F2R1F1D1") --ready for next run
4461 else
4462 T:go(returnPath) -- return to shaft
4463 end
4464 T:dumpRefuse()
4465 end
4466 end
4467 -- go home
4468 while T:getY() < mineEntrance:getY() do
4469 T:up(1)
4470 end --at surface now
4471 T:forward(16)
4472 T:turnRight(2)
4473 return status
4474end
4475
4476function placeStorage()
4477 -- Chest1 sticks
4478 -- Chest2 logs
4479 -- chest3 cobblestone
4480 -- chest4 sand, reeds
4481 -- chest5 iron ore
4482 -- chest6 redstone
4483 -- chest7 diamond
4484 -- chest8 dirt, misc
4485 -- chest9 saplings (placed right side of furnace)
4486
4487 -- make 9 chests need 18 logs
4488 -- this function runs when all trees harvested locally
4489 T:saveToLog("placeStorage() Starting...", true)
4490 T:craftChests(9, false)
4491 --remove furnace at start
4492 T:go("x0R1F2D1x2H2U1R2F2R1")
4493 T:go("H2F1x0C2")
4494 for i = 2, 8 do
4495 T:go("F1x0H2F1x0C2")
4496 end
4497 T:go("F1x0C2R1F1x0C2R1")
4498 for i = 1, 14 do
4499 T:go("F1x0C2")
4500 end
4501 T:go("R1F2R1x0C2")
4502 for i = 1, 14 do
4503 T:go("F1x0C2")
4504 end
4505 T:go("R1F1L1R2F16R2")
4506 --put furnace back
4507 turtle.select(T:getItemSlot("minecraft:furnace"))
4508 turtle.placeUp()
4509end
4510
4511function relocateHome()
4512 local numArms = 4
4513 local gotHome = false
4514 for i = 2, numArms, 2 do
4515 if safeRun(i, "minecraft:furnace") then --furnace found
4516 gotHome = true
4517 break
4518 else
4519 T:turnRight(1)
4520 if safeRun(i, "minecraft:furnace") then
4521 gotHome = true
4522 break
4523 else
4524 T:turnRight(1)
4525 if safeRun(i + 1, "minecraft:furnace") then
4526 gotHome = true
4527 break
4528 else
4529 T:turnRight(1)
4530 if i + 1 < numArms then
4531 if safeRun(i + 1, "minecraft:furnace") then
4532 gotHome = true
4533 break
4534 else
4535 T:turnRight(1)
4536 end
4537 else
4538 --return to starting position
4539 safeRun(i / 2, "minecraft:furnace")
4540 T:turnRight(1)
4541 safeRun(i / 2 + 1, "minecraft:furnace")
4542 end
4543 end
4544 end
4545 end
4546 end
4547
4548 return gotHome
4549end
4550
4551function returnHome()
4552 local startX = T:getX()
4553 local startZ = T:getZ()
4554
4555 T:saveToLog("returnHome() started", true)
4556 if T:getX() <= coordHome:getX() then
4557 while T:getFacing() ~= 3 do
4558 T:turnRight(1)
4559 end
4560 while T:getX() < coordHome:getX() do
4561 safeRun(1, "minecraft:furnace")
4562 end
4563 elseif T:getX() > coordHome:getX() then -- eg -200 current -211 home
4564 while T:getFacing() ~= 1 do
4565 T:turnRight(1)
4566 end
4567 while T:getX() > coordHome:getX() do
4568 safeRun(1, "minecraft:furnace")
4569 end
4570 end
4571 if T:getZ() >= coordHome:getZ() then -- go north
4572 while T:getFacing() ~= 2 do
4573 T:turnRight(1)
4574 end
4575 while T:getZ() > coordHome:getZ() do
4576 safeRun(1, "minecraft:furnace")
4577 end
4578 elseif T:getZ() < coordHome:getZ() then
4579 while T:getFacing() ~= 0 do
4580 T:turnRight(1)
4581 end
4582 while T:getZ() < coordHome:getZ() do
4583 safeRun(1, "minecraft:furnace")
4584 end
4585 end
4586 while T:getFacing() ~= coordHome:getFacing() do
4587 T:turnRight(1)
4588 end
4589
4590 if T:getBlockType("up") == "minecraft:furnace" then
4591 T:saveToLog("returnHome() completed", true)
4592 getCoords(true)
4593 else
4594 if relocateHome() then
4595 T:saveToLog("returnHome() completed", true)
4596 getCoords(true)
4597 else
4598 T:saveToLog("returnHome() failed: I am lost!", true)
4599 error()
4600 end
4601 end
4602end
4603
4604function safeRun(runLength, actionBlock)
4605 actionBlock = actionBlock or ""
4606 local itemSlot = 0
4607 local success = false
4608 local doContinue = true
4609 local blockType, modifier
4610 turtle.select(1)
4611 for i = 1, runLength do
4612 if actionBlock == "minecraft:furnace" then
4613 blockType, modifier = T:getBlockType("up")
4614 if blockType == "minecraft:furnace" then
4615 success = true
4616 doContinue = false
4617 break
4618 end
4619 elseif actionBlock == "minecraft:water" then
4620 blockType, modifier = T:getBlockType("down")
4621 if blockType == "minecraft:flowing_water" or blockType == "minecraft:water" then
4622 if T:getItemSlot("minecraft:bucket", -1) > 0 then --water not yet found
4623 if T:place("minecraft:bucket", -1, "down") then
4624 T:saveToLog("Water bucket filled x="..T:getX()..", y="..T:getY()..", z="..T:getZ(), true)
4625 success = true
4626 else
4627 T:saveToLog("Water found, but not source block "..T:getX()..", y="..T:getY()..", z="..T:getZ(), true)
4628 end
4629 end
4630 end
4631 end
4632 if doContinue then
4633 if turtle.detect() then --can't move forward
4634 blockType, modifier = T:getBlockType("forward")
4635 if blockType == "minecraft:leaves" or blockType == "minecraft:tallgrass" or blockType == "minecraft:double_plant" then
4636 turtle.dig()
4637 else -- not veg so climb up
4638 while turtle.detect() do
4639 T:up(1)
4640 blockType, modifier = T:getBlockType("forward")
4641 if blockType == "minecraft:leaves"
4642 or blockType == "minecraft:tallgrass"
4643 or blockType == "minecraft:double_plant" then
4644 turtle.dig()
4645 break
4646 end
4647 end
4648 end
4649 end
4650 T:forward(1)
4651 while not turtle.inspectDown() do -- must be air below
4652 T:down(1)
4653 end
4654 if actionBlock == "minecraft:furnace" then
4655 blockType, modifier = T:getBlockType("up")
4656 if blockType == actionBlock then
4657 success = true
4658 break
4659 end
4660 elseif actionBlock == "minecraft:water" then
4661 blockType, modifier = T:getBlockType("down")
4662 if blockType == "minecraft:flowing_water" or blockType == "minecraft:water" then
4663 if T:getItemSlot("minecraft:bucket", -1) > 0 then --water not yet found
4664 if T:place("minecraft:bucket", -1, "down") then
4665 T:saveToLog("Water bucket filled x="..T:getX()..", y="..T:getY()..", z="..T:getZ(), true)
4666 success = true
4667 else
4668 T:saveToLog("Water found, but not source block "..T:getX()..", y="..T:getY()..", z="..T:getZ(), true)
4669 end
4670 end
4671 end
4672 end
4673 end
4674 end
4675
4676 return success
4677end
4678
4679function saveStatus(text)
4680 local fileHandle = ""
4681 T:saveToLog("Saving Status: '"..text.."'", true)
4682 if text == "treeFarm" then
4683 fileHandle = fs.open("treeFarm.txt", "w")
4684 fileHandle.writeLine(tostring(treeFarm:getPondFilled()))
4685 fileHandle.writeLine(tostring(treeFarm:getFarmCreated()))
4686 fileHandle.writeLine(tostring(treeFarm:getHopperPlaced()))
4687 fileHandle.writeLine(tostring(treeFarm:getFarmFlooded()))
4688 fileHandle.writeLine(tostring(treeFarm:getTimePlanted()))
4689 else
4690 -- from e.g. saveStatus("harvestFirstTree")
4691 fileHandle = fs.open("currentStatus.txt", "w")
4692 fileHandle.writeLine(text)
4693 end
4694 fileHandle.close()
4695end
4696
4697function smelt(oreType, quantity)
4698 --log->charcoal, iron_ore->iron, cobblestone->stone, sand->glass
4699 --assume function called with turtle under furnace, at ground level
4700 --move next to furnace to place planks
4701 --move to top of furnace to place wood
4702 --move under furnace to remove charcoal, stone, glass, iron
4703 T:saveToLog("smelt("..oreType..", "..quantity..") started", true)
4704 local smeltType = ""
4705 local continue = true
4706 local waitTime = 0
4707 local planksNeeded = 0 --total needed for smelting
4708 local woodNeeded = 0
4709 local woodAvailable = 0
4710 local success = false
4711 local stockData = {}
4712 local logData = T:getLogData() --{.total, .mostSlot, .mostCount, .mostModifier, mostName, .leastSlot, .leastCount, .leastModifier, leastName}
4713
4714 stockData = T:getStock("minecraft:planks", -1)
4715 if stockData.total > 0 then
4716 turtle.select(stockData.mostSlot)
4717 turtle.refuel()
4718 if stockData.leastSlot ~= stockData.mostSlot then
4719 turtle.select(stockData.leastSlot)
4720 turtle.refuel()
4721 end
4722 end
4723 woodAvailable = logData.total
4724 planksNeeded = quantity
4725 if oreType == "minecraft:log" then
4726 woodNeeded = quantity + math.ceil(quantity / 4)
4727 end
4728 if woodNeeded > woodAvailable then
4729 getItemFromStorage("minecraft:log", false)
4730 logData = T:getLogData()
4731 woodAvailable = logData.total
4732 end
4733 T:go("B1U2F1") --now on top
4734
4735 if oreType == "minecraft:log" or oreType == "minecraft:log2" then --eg quantity = 2, needs 2 wood + 2 planks ASSUME only called if wood in stock
4736 smeltType = "minecraft:coal"
4737 elseif oreType == "minecraft:cobblestone"then
4738 smeltType = "minecraft:stone"
4739 elseif oreType == "minecraft:iron_ore" then
4740 smeltType = "minecraft:iron_ingot"
4741 elseif oreType == "minecraft:sand" then
4742 smeltType = "minecraft:glass"
4743 end
4744 turtle.select(T:getItemSlot(oreType))
4745 turtle.dropDown(quantity)
4746 T:go("B1D1") --in front of furnace, remove any existing planks and refuel
4747 turtle.select(16)
4748 if turtle.suck() then
4749 turtle.refuel()
4750 end
4751 planksNeeded = math.ceil(planksNeeded / 4) * 4 --eg 1/4 = 1 ,*4 = 4 planks
4752 T:turnRight(1) --side on to furnace
4753 T:craft("minecraft:planks", planksNeeded, logData.mostName, nil, nil, false)
4754 T:turnLeft(1)
4755 turtle.select(T:getItemSlot("minecraft:planks"))
4756 turtle.drop() --drop planks into furnace
4757 T:go("D1F1") --back under furnace
4758 turtle.select(1)
4759 repeat
4760 waitTime = waitTime + 1
4761 sleep(1)
4762 if waitTime == 10 then --every 10 secs check if any output
4763 if turtle.suckUp() then --continue to wait
4764 continue = true
4765 waitTime = 0
4766 else --either no product or all done
4767 continue = false
4768 end
4769 end
4770 until not continue
4771
4772 if T:getItemSlot(smeltType, -1) > 0 then
4773 success = true
4774 T:saveToLog("smelt("..oreType..", "..quantity..") completed", true)
4775 end
4776 return success
4777end
4778
4779function waitForTreeFarm(woodNeeded)
4780 local woodGrowing = 0
4781 local maxWood = 0
4782
4783 if woodNeeded > 0 and treeFarm:getFarmCreated() then
4784 woodGrowing = treeFarm:getPotentialHarvest()
4785 treeFarm:getMaxHarvest()
4786 while woodGrowing < woodNeeded do
4787 sleep(10)
4788 -- 10 secs = 12 minecraft minutes
4789 -- 1 min = 1 hour 12 minutes
4790 -- 20 mins = 1 minecraft day
4791 woodGrowing = treeFarm:getPotentialHarvest()
4792 --will be equal to maxHarvest after 2 days
4793 T:saveToLog("waiting for tree farm to grow. potential harvest = "..woodGrowing.." from "..maxWood, true)
4794 if woodGrowing >= maxWood then
4795 break
4796 end
4797 end
4798 harvestTreeFarm()
4799 end
4800end
4801
4802function main()
4803 local functionList = {"T:harvestFirstTree()", "harvestAllTrees()", "findCobble()", "clearBase()", "placeStorage()",
4804 "createMine(14)", "createMine(11)", "createMine(8)",
4805 "mineToBedrock(1/4)", "mineToBedrock(2/4)", "mineToBedrock(3/4)",
4806 "mineToBedrock(4/4)", "craftMiningTurtle()"}
4807
4808 if os.version() == "CraftOS 1.7" then
4809 ccComputer = "computercraft:CC-Computer"
4810 ccDisk = "computercraft:disk"
4811 ccTurtle = "computercraft:CC-Turtle"
4812 ccCraftyMiningTurtle = "computercraft:CC-TurtleExpanded"
4813 ccDiskDrive = "computercraft:CC-Peripheral"
4814 end
4815 initialise() -- Set up global variables and create objects
4816 getCoords(false) -- Get/load current coordinates from player
4817 local status = tonumber(loadStatus("currentStatus.txt")) -- if continuing from previous run (debugging aid)
4818 T:saveToLog("Starting at function "..functionList[status + 1], true)
4819
4820 if status == 0 then
4821 T:harvestTree(true) -- harvest first tree and make chest
4822 T:saveToLog("harvestFirstTree() Completed.", true)
4823 status = 1
4824 saveStatus(tostring(status))
4825 end
4826 if status <= 1 then
4827 harvestAllTrees() -- harvest remaining trees in 33 x 33 square
4828 T:saveToLog("harvestAllTrees() Completed.", true)
4829 status = 2
4830 saveStatus(tostring(status))
4831 end
4832 if status <= 2 then
4833 findCobble() -- get cobble and any incidental coal or iron ore
4834 T:saveToLog("findCobble() Completed.", true)
4835 status = 3
4836 saveStatus(tostring(status))
4837 end
4838 if status <= 3 then
4839 clearBase() -- create cobble base station including 2x2 pond area
4840 T:saveToLog("clearBase() Completed.", true)
4841 status = 4
4842 saveStatus(tostring(status))
4843 end
4844 if status <= 4 then
4845 placeStorage() -- craft chests and place in ground for storage
4846 T:saveToLog("placeStorage() Completed.", true)
4847 status = 5
4848 saveStatus(tostring(status))
4849 end
4850 -- status 5 = no mine yet; status 6 = mined level 14/13; status 7 = mined level 11/10; status 8 = mined level 8/7
4851 if status <= 7 then
4852 status = createMine(status) -- create basic 3 layer mine minimal structure and collect iron ore
4853 saveStatus(tostring(status))
4854 end
4855 local stockData = T:getStock("minecraft:iron_ore") -- check enough iron ore found
4856 local numIronOre = stockData.total
4857 stockData = T:getStock("minecraft:diamond", 0)
4858 if stockData.total < 6 or numIronOre < 14 then
4859 if status <= 11 then
4860 if stockData.total < 6 then -- 3 levels mined out, need more
4861 status = mineToBedrock(status) -- 4 stages: status 9, 10, 11, 12
4862 saveStatus(tostring(status))
4863 end
4864 end
4865 end
4866 if status <= 12 then
4867 stockData = T:getStock("minecraft:diamond", 0)
4868 if stockData.total < 3 then
4869 craftMiningTurtle(0)
4870 elseif stockData.total < 6 then
4871 craftMiningTurtle(1)
4872 else
4873 craftMiningTurtle(2)
4874 end
4875 end
4876end
4877
4878--*********************Program runs from here*****************
4879main()