# Turtle Survival AI

May 18th, 2017
198
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. local returning = false
2.
3. function tryRefuel()
4.   if getInvoItem("coal") then
5.     turtle.refuel(5)
6.     return true
7.   elseif getInvoItem("planks") then
8.     turtle.refuel(5)
9.     return true
10.   end
11.   return false
12. end
13.
14. function checkFuel(flag)
15.   if not flag then
16.     local fuel = turtle.getFuelLevel()
17.     local distance = math.abs(x) + math.abs(y) + math.abs(z)
18.     if fuel-1 <= distance and not tryRefuel() then
19.       home()
20.       error("Alert: Fuel level critical. Returning to home point...")
21.     end
22.   end
23. end
24.
25. function getFuel()
26.   faceMachine("craftChest")
27.   turtle.select(1)
28.   while turtle.suckUp() do
29.     if scanSlot(1, "coal") then
30.       turtle.transferTo(16)
31.     end
32.     turtle.drop()
33.   end
34.   while turtle.suck() do
35.     turtle.dropUp()
36.   end
37. end
38.
39. --     0 (-z)
40. -- 3 (-x)  1 (+x)
41. --     2 (+z)
42.
43. function forward(num)
44.   num = num or 1
45.   for i = 1, num do
46.     if turtle.forward() then
47.       if f == 0 then
48.         z = z - 1
49.         update("z",z)
50.       elseif f == 1 then
51.         x = x + 1
52.         update("x",x)
53.       elseif f == 2 then
54.         z = z + 1
55.         update("z",z)
56.       elseif f == 3 then
57.         x = x - 1
58.         update("x",x)
59.       end
60.       checkFuel(returning)
61.     else
62.       return false
63.     end
64.   end
65.   return true
66. end
67.
68. function back(num)
69.   num = num or 1
70.   for i = 1, num do
71.     if turtle.back() then
72.       if f == 0 then
73.         z = z + 1
74.         update("z",z)
75.       elseif f == 1 then
76.         x = x - 1
77.         update("x",x)
78.       elseif f == 2 then
79.         z = z - 1
80.         update("z",z)
81.       elseif f == 3 then
82.         x = x + 1
83.         update("x",x)
84.       end
85.       checkFuel(returning)
86.     else
87.       return false
88.     end
89.   end
90.   return true
91. end
92.
93. function up(num)
94.   num = num or 1
95.   for i = 1, num do
96.     if turtle.up() then
97.       y = y + 1
98.       update("y",y)
99.       checkFuel(returning)
100.     else
101.       return false
102.     end
103.   end
104.   return true
105. end
106.
107. function down(num)
108.   num = num or 1
109.   for i = 1, num do
110.     if turtle.down() then
111.       y = y - 1
112.       update("y",y)
113.       checkFuel(returning)
114.     else
115.       return false
116.     end
117.   end
118.   return true
119. end
120.
121. function left(num)
122.   num = num or 1
123.   for i = 1, num do
124.     turtle.turnLeft()
125.     f = (f+3)%4
126.     update("f",f)
127.   end
128. end
129.
130. function right(num)
131.   num = num or 1
132.   for i = 1, num do
133.     turtle.turnRight()
134.     f = (f+1)%4
135.     update("f",f)
136.   end
137. end
138.
139. function home()
140.   returning = true
141.   if y == homePos[3] and x == homePos[1] and z == homePos[2] then
142.     returning = false
143.     return
144.   end
145.   while y ~= homePos[3]-2 do
146.     if y < homePos[3]-2 then
147.       turtle.digUp()
148.       up()
149.     elseif y > homePos[3]-2 then
150.       turtle.digDown()
151.       down()
152.     end
153.   end
154.   if z > homePos[2] then
155.     while f ~= 0 do
156.       left()
157.     end
158.   elseif z < homePos[2] then
159.     while f ~= 2 do
160.       left()
161.     end
162.   end
163.   while z ~= homePos[2] do
164.     forward()
165.   end
166.   if x > homePos[1] then
167.     while f ~= 3 do
168.       left()
169.     end
170.   elseif x < homePos[1] then
171.     while f ~= 1 do
172.       left()
173.     end
174.   end
175.   while x ~= homePos[1] do
176.     forward()
177.   end
178.   for i = 1, 2 do
179.     up()
180.   end
181.   returning = false
182. end
183.
184. function travel(xPos, zPos, yPos)
185.   while y ~= yPos do
186.     if y < yPos then
187.       turtle.digUp()
188.       up()
189.     elseif y > yPos then
190.       turtle.digDown()
191.       down()
192.     end
193.   end
194.   if z > zPos then
195.     while f ~= 0 do
196.       left()
197.     end
198.   elseif z < zPos then
199.     while f ~= 2 do
200.       left()
201.     end
202.   end
203.   while z ~= zPos do
204.     turtle.dig()
205.     forward()
206.   end
207.   if x > xPos then
208.     while f ~= 3 do
209.       left()
210.     end
211.   elseif x < xPos then
212.     while f ~= 1 do
213.       left()
214.     end
215.   end
216.   while x ~= xPos do
217.     turtle.dig()
218.     forward()
219.   end
220. end
221.
222. function update(var,value)
223.   if not fs.exists("saveData") then
224.     local h = fs.open("saveData", "w")
225.     h.close()
226.   end
227.   if type(value) == "number" then
228.     value = tostring(value)
229.   elseif type(value) == "table" then
230.     local v = "{"
231.     for i = 1, #value-1 do
232.       v = v .. tostring(value[i]) .. ","
233.     end
234.     v = v .. tostring(value[#value]) .. "}"
235.     value = v
236.   elseif type(value) == "string" then
237.     value = "\""..value.."\""
238.   end
240.   if str:find(var) then
241.     str = str:gsub(var.."%=.-%;",var.."="..value..";")
242.   else
243.     str = str .."\n"..var.."="..value..";"
244.   end
245.   local h = fs.open("saveData","w")
246.   h.write(str)
247.   h.close()
248. end
249.
250. function initVars()
251.   if not fs.exists("saveData") then
252.     update("x", 0)
253.     update("z", 0)
254.     update("y", 0)
255.     update("f", 0)
256.     update("homePos", {0, 0, 0})
258.     update("version", 0)
259.   end
260.   shell.run("saveData")
261. end
262.
263. function setPos(a, b, c, d)
264.   update("x", a)
265.   update("z", b)
266.   update("y", c)
267.   update("f", d)
268.   shell.run("saveData")
269. end
270.
271. function setHome(homeX, homeZ, homeY)
272.   homePos = {homeX, homeZ, homeY}
273.   update("homePos", homePos)
274. end
275.
276. function log(data)
277.   local h = fs.open("log", "w")
278.   if type(data) == "string" then
279.     h.writeLine(data)
280.   elseif type(data) == "table" then
281.     for i = 1, #data do
282.       h.writeLine(data[i])
283.     end
284.   end
285.   h.close()
286. end
287.
288. function surface()
289.   while down() do
290.   end
291. end
292.
293. function bottom()
294.   turtle.digDown()
295.   while down() do
296.     turtle.digDown()
297.   end
298. end
299.
300. function scan(tStr)
301.   local s,d = turtle.inspect()
302.   if s then
303.     for i = 1, #tStr do
304.       if string.find(d.name, tStr[i]) then
305.         return true
306.       end
307.     end
308.     return false
309.   end
310.   return false
311. end
312. function scanUp(tStr)
313.   local s,d = turtle.inspectUp()
314.   if s then
315.     for i = 1, #tStr do
316.       if string.find(d.name, tStr[i]) then
317.         return true
318.       end
319.     end
320.     return false
321.   end
322.   return false
323. end
324. function scanDown(tStr)
325.   local s,d = turtle.inspectDown()
326.   if s then
327.     for i = 1, #tStr do
328.       if string.find(d.name, tStr[i]) then
329.         return true
330.       end
331.     end
332.     return false
333.   end
334.   return false
335. end
336.
337. local vFlag = false
338. function mineVein(tStr, lim, max)
339.   if not lim then
340.     lim = 128
341.     max = 128
342.   end
343.   if not max then
344.     max = lim
345.   end
346.   if lim == max then
347.     vFlag = false
348.   end
349.   lim = lim - 1
350.   if lim == 0 then
351.     vFlag = true
352.     return true
353.   end
354.   for i = 1, 4 do
355.     if scan(tStr) then
356.       turtle.dig()
357.       forward()
358.       mineVein(tStr, lim, max)
359.       if vFlag then return true end
360.       back()
361.     end
362.     left()
363.   end
364.   if scanUp(tStr) then
365.     turtle.digUp()
366.     up()
367.     mineVein(tStr, lim, max)
368.     if vFlag then return true end
369.     down()
370.   end
371.   if scanDown(tStr) then
372.     turtle.digDown()
373.     down()
374.     mineVein(tStr, lim, max)
375.     if vFlag then return true end
376.     up()
377.   end
378. end
379.
380. function drillDown(num)
381.   for i = 1, num do
382.     turtle.digDown()
383.     if not down() then
384.       break
385.     end
386.   end
387. end
388.
389. function checkFilled()
390.   for i = 1, 16 do
391.     if turtle.getItemSpace(i) == 64 then
392.       return true
393.     end
394.   end
395.   return false
396. end
397.
398. function stripMine(length, strips, template)
399.   for i = 1, strips do
400.     for i = 1, length do
401.       turtle.dig()
402.       if not checkFilled()then
403.         home()
404.         clearInvo()
405.         clearTrash()
406.       end
407.       forward()
408.       vFlag = false
409.       mineVein(template)
410.     end
411.     left((i%2)*2+1)
412.     for i = 1, 3 do
413.       turtle.dig()
414.       if not checkFilled()then
415.         home()
416.         clearInvo()
417.         clearTrash()
418.       end
419.       forward()
420.       vFlag = false
421.       mineVein(template)
422.     end
423.     left((i%2)*2+1)
424.   end
425. end
426.
427. function getInitialTree()
428.   turtle.dig()
429.   turtle.craft()
430.   turtle.refuel()
431.   forward()
432.   local n = 0
433.   while scanUp({"log"}) do
434.     turtle.digUp()
435.     up()
436.   end
437.   turtle.craft()
438.   turtle.refuel()
439.   for i = 2, n do
440.     down()
441.   end
442.   turtle.suckDown()    ---grab fallen saplings
443.   down()
444.
445. end
446.
447. function coast(length, strips, template, num)
448.   for i = 1, strips do
449.     for i = 1, length do
450.       while not forward() do
451.         turtle.digUp()
452.         up()
453.       end
454.       surface()
455.       vFlag = false
456.       if mineVein(template, num) then
457.         break
458.       end
459.     end
460.     left((i%2)*2+1)
461.     for i = 1, 3 do
462.       while not forward() do
463.         turtle.digUp()
464.         up()
465.       end
466.       surface()
467.       --[[vFlag = false
468.       if mineVein(template, num) then
469.         break
470.       end]]--
471.     end
472.     left((i%2)*2+1)
473.   end
474. end
475.
476. local rFlag = false
477. function recursiveBreak(tStr)
478.   if not scanDown(tStr) then
479.     turtle.digDown()
480.     down()
481.     recursiveBreak(tStr)
482.     rFlag = true
483.     --up()
484.   end
485.   if rFlag then return end
486.   if not scan(tStr) then
487.     turtle.dig()
488.     forward()
489.     recursiveBreak(tStr)
490.     if rFlag then return end
491.     back()
492.   end
493.   left()
494.   if not scan(tStr) then
495.     turtle.dig()
496.     forward()
497.     recursiveBreak(tStr)
498.     if rFlag then return end
499.     back()
500.   end
501.   right(2)
502.   if not scan(tStr) then
503.     turtle.dig()
504.     forward()
505.     recursiveBreak(tStr)
506.     if rFlag then return end
507.     back()
508.   end
509.   left()
510. end
511.
512. function setRelativeCoords()
513.   bottom()
514.   recursiveBreak({"bedrock"})
515.   local surface = y
516.   setPos(0, 0, 2, 0)
517.   setHome(0, 0, 20-surface)
518. end
519.
520. ----------------------------------------CRAFTING----------------------------------------
521.
522. local craftSlots = {4, 8, 12, 16, 15, 14, 13}
523. local craftTab = {
524.   ["plank"] = {{"log"}, {{1,0,0},{0,0,0},{0,0,0}}},
525.   ["stick"] = {{"plank"}, {{1,0,0},{1,0,0},{0,0,0}}},
526.   ["table"] = {{"plank"}, {{1,1,0},{1,1,0},{0,0,0}}},
527.   ["chest"] = {{"plank"}, {{1,1,1},{1,0,1},{1,1,1}}},
528.   ["furnace"] = {{"cobble"}, {{1,1,1},{1,0,1},{1,1,1}}},
529.   ["glass_pane"] = {{"glass"}, {{0,0,0},{1,1,1},{1,1,1}}},
530.   ["paper"] = {{"sugarcane"}, {{0,0,0},{0,0,0},{1,1,1}}},
531.   ["pickaxe"] = {{"stick", "diamond"}, {{2,2,2},{0,1,0},{0,1,0}}},
532.   ["computer"] = {{"stone", "redstone", "glass_pane"}, {{1,1,1},{1,2,1},{1,3,1}}},
533.   ["turtle"] = {{"iron_ingot", "CC%-Computer", "chest"}, {{1,1,1},{1,2,1},{1,3,1}}},
534.   ["mining_turtle"] = {{"CC%-Turtle", "diamond_pick", "crafting_table"}, {{0,0,0},{2,1,3},{0,0,0}}},
535.   ["disk_drive"] = {{"stone", "redstone"}, {{1,1,1},{1,2,1},{1,2,1}}},
536.   ["floppy_disk"] = {{"paper", "redstone"}, {{2,1,0},{0,0,0},{0,0,0}}},
537.
538.   ["iron_ingot"] = {"coal", "iron_ore"},
539.   ["gold_ingot"] = {"coal", "gold_ore"},
540.   ["stone"] = {"coal", "cobble"},
541.   ["charcoal"] = {"coal", "log"},
542.   ["glass"] = {"coal", "sand"}
543. }
544. local machines = {
545.   ["craftChest"] = 0,
546.   ["furnace"] = 1
547. }
548.
549. function getInvoItem(str)
550.   for i = 1, 16 do
551.     turtle.select(i)
552.     local d = turtle.getItemDetail()
553.     if d and string.find(d.name, str) then
554.       return true
555.     end
556.   end
557.   return false
558. end
559.
560. function scanSlot(slot, tStr)
561.   turtle.select(slot)
562.   local d = turtle.getItemDetail()
563.   if type(tStr) == "table" then
564.     for i = 1, #tStr do
565.       if d and string.find(d.name, ":"..tStr[i]) then
566.         return true
567.       end
568.     end
569.   elseif type(tStr) == "string" then
570.     if d then print(tStr .. "   " .. d.name) end
571.     if d and string.find(d.name, ":"..tStr) then
572.       return true
573.     end
574.   end
575.   return false
576. end
577.
578. function clearInvo(tStr)
579.   if not tStr then
580.     for i = 1, 16 do
581.       turtle.select(i)
582.       turtle.dropUp()
583.     end
584.   else
585.     for n = 1, 7 do
586.       for i = 1, #tStr do
587.         if scanSlot(craftSlots[n], tStr[i]) then
588.           turtle.transferTo(craftSlots[i])
589.         end
590.       end
591.       turtle.dropUp()
592.     end
593.     for row = 1, 3 do
594.       for col = 1, 3 do
595.         for i = 1, #tStr do
596.           if scanSlot((row-1)*4+col, tStr[i]) then
597.             turtle.transferTo(craftSlots[i])
598.           end
599.         end
600.         turtle.dropUp()
601.       end
602.     end
603.   end
604. end
605.
606. function buildFloor(slot)
607.   turtle.select(slot)
608.   back()
609.   left()
610.   back()
611.   for i = 1, 4 do
612.     for i = 1, 2 do
613.       forward()
614.       turtle.placeDown()
615.     end
616.     right()
617.   end
618.   forward()
619.   right()
620.   forward()
621. end
622.
623. function prepCraft()
624.   surface()
625.   turtle.digDown()
626.   down()
627.   getInvoItem("dirt")
628.   turtle.placeUp()
629.   turtle.digDown()
630.   down()
631. end
632.
633. function craftDirty(item, num)
634.   num = num or 64
635.   clearInvo(craftTab[item][1])
636.   for row = 1, 3 do
637.     for col = 1, 3 do
638.       if craftTab[item][2][row][col] > 0 then
639.         turtle.select(craftSlots[craftTab[item][2][row][col]])
640.         turtle.transferTo((row-1)*4+col, num)
641.       end
642.     end
643.   end
644.   for i = 1, #craftTab[item][1] do
645.     turtle.select(craftSlots[i])
646.     turtle.dropUp()
647.   end
648.   turtle.select(16)
649.   turtle.craft(num)
650.   up()
651.   while turtle.suckDown() do end
652.   down()
653. end
654.
655. function getCraftingItems(tStr)
656.   faceMachine("craftChest")
657.   turtle.select(1)
658.   while turtle.suckUp() do
659.     for i = 1, #tStr do
660.       if scanSlot(1, tStr[i]) then
661.         turtle.transferTo(craftSlots[i])
662.       end
663.     end
664.     turtle.drop()
665.   end
666.   while turtle.suck() do
667.     turtle.dropUp()
668.   end
669. end
670.
671. function craft(item, num)
672.   num = num or 64
673.   clearInvo()
674.   getCraftingItems(craftTab[item][1])
675.   for row = 1, 3 do
676.     for col = 1, 3 do
677.       if craftTab[item][2][row][col] > 0 then
678.         turtle.select(craftSlots[craftTab[item][2][row][col]])
679.         turtle.transferTo((row-1)*4+col, num)
680.       end
681.     end
682.   end
683.   for i = 1, #craftTab[item][1] do
684.     turtle.select(craftSlots[i])
685.     turtle.dropUp()
686.   end
687.   turtle.select(16)
688.   turtle.craft(num)
689. end
690.
691. local essentials = {"log", "plank", "stick", "cobble", "stone", "redstone", "glass", "sand", "iron", "gold", "coal"}
692.
693. function clearTrash()
694.   clearInvo()
695.   local s = 1
696.   turtle.select(1)
697.   while turtle.suckUp() do
698.     local d = turtle.getItemDetail()
699.     if not scanSlot(s, essentials) then
700.       turtle.dropDown()
701.     else
702.       s = s + 1
703.     end
704.   end
705.   clearInvo()
706. end
707.
708. function clearTrashDirty()
709.   for i = 1, 16 do
710.     turtle.select(i)
711.     if not scanSlot(i, essentials) then
712.       turtle.dropDown()
713.     end
714.   end
715. end
716.
717. function smelt(item, num)
718.   clearInvo()
719.   getCraftingItems(craftTab[item])
720.   faceMachine("furnace")
721.   forward()
722.   up()
723.   forward()
724.   turtle.select(craftSlots[2])
725.   num = num or turtle.getItemCount()
726.   turtle.dropDown(num)
727.   back()
728.   down(2)
729.   forward()
730.   turtle.select(craftSlots[1])
731.   turtle.dropUp(math.ceil(num/8))
732.   sleep(num*10)
733.   turtle.select(16)
734.   turtle.suckUp()
735.   back()
736.   up()
737.   back()
738. end
739.
740. function faceMachine(m)
741.   while f ~= machines[m] do
742.     left()
743.   end
744. end
745.
746. ----------------------------------------FILE TRANSFER----------------------------------------
747. function transferChildProgram()
748.   getInvoItem("CC%-Peripheral")
749.   up()
750.   turtle.place()
751.   getInvoItem("disk")
752.   turtle.drop()
753.   local h = fs.open("disk/startup", "w")
754.   local currentLabel = os.getComputerLabel()
755.   currentLabel = currentLabel:gsub("%-", "_")
756.   h.writeLine([[
757.     shell.run("pastebin get 7a4sZAXm run")
758.     local h = fs.open("startup", "w")
759.     h.writeLine("shell.run(\"run\")")
760.     h.close()
761.     os.setComputerLabel("]]..currentLabel.."-"..version..[[")
762.   ]])
763.   h.close()
764.   version = version + 1
765.   down()
766.   getInvoItem("CC%-Turtle")
767.   turtle.place()
768.   peripheral.wrap("front").turnOn()
769.   sleep(2)
770.   up()
771.   turtle.suck()
772.   turtle.dig()
773.   down()
774.   peripheral.wrap("front").reboot()
775.   back()
776. end
777. ----------------------------------------ACTUAL PROGRAM----------------------------------------
779.   if task == 0 then  --get initial wood
780.     getInitialTree()
781.     coast(5, 5, {"log"})
784.   elseif task == 1 then  --craft chests for base
785.     prepCraft()
786.     craftDirty("plank")
787.     craftDirty("chest", 2)
790.   elseif task == 2 then  --set relative coordinate system
791.     setRelativeCoords()
794.   elseif task == 3 then  --set up base
795.     home()
796.     if getInvoItem("cobble") and turtle.getItemCount() > 8 then
797.       buildFloor(turtle.getSelectedSlot())
798.     end
799.     getInvoItem("chest")
800.     turtle.placeUp()
801.     faceMachine("craftChest")
802.     turtle.place()
803.     craft("furnace", 1)
804.     faceMachine("furnace")
805.     forward()
806.     turtle.place()
807.     turtle.digDown()
808.     back()
809.     clearTrash()
812.   elseif task == 4 then  --mining (best range y = 6 - 13)
813.     getFuel()
814.     travel(0, 0, 10)
815.     stripMine(12, 4, {"iron", "gold", "redstone", "coal", "diamond", "sand"}) --30, 10
816.     home()
817.     clearTrash()
820.   elseif task == 5 then  --find sand
821.     getFuel()
822.     down(2)
823.     forward()
824.     surface()
825.     coast(100, 1, {"sand"}, 64)
826.     home()
827.     clearTrash()
830.   elseif task == 6 then  --craft turtle!!!
831.     smelt("iron_ingot", 7)
832.     smelt("stone", 7)
833.     smelt("glass", 6)
834.     craft("glass_pane", 1)
835.     craft("stick", 1)
836.     craft("table", 1)
837.     craft("chest", 1)
838.     craft("pickaxe", 1)
839.     craft("computer", 1)
840.     craft("turtle", 1)
841.     craft("mining_turtle", 1)
844.   elseif task == 7 then  --find a new land for the new turtle!
847.   elseif task == 8 then  --give the new turtle its software and start it up!
848.     transferChildProgram()
849.     error("Transfer Completed.")