Advertisement
paramus

Gray Goo

Feb 28th, 2025
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.92 KB | Gaming | 0 0
  1. -- Self replicating Turtle
  2.  
  3. local x,y,z = 0,0,0 --Relative to First position
  4. local Orien = 0 --0:Forward; 1:Right; 2:Back; 3:Left
  5. local Heading = {x=0,y=0,z=0}
  6. local Query
  7. local IsFull,CanReplicate = false,false
  8.  
  9. function MoveUp(amount)
  10.     if (amount==0) then return end
  11.     if (amount<0) then amount=amount*-1 end
  12.    
  13.     for i=1,amount,1 do
  14.         turtle.digUp()
  15.         if (turtle.up()) then
  16.             y=y+1
  17.             term.setCursorPos(1,2)
  18.             term.write("I am at: " .. x .. " " .. y .. " " .. z)
  19.         end
  20.     end
  21. end
  22. function MoveDown(amount)
  23.     if (amount==0) then return end
  24.     if (amount<0) then amount=amount*-1 end
  25.    
  26.     for i=1,amount,1 do
  27.         turtle.digDown()
  28.         if (turtle.down()) then
  29.             y=y-1
  30.             term.setCursorPos(1,2)
  31.             term.write("I am at: " .. x .. " " .. y .. " " .. z)
  32.         end
  33.     end
  34. end
  35.  
  36. function MoveForward(amount)
  37.     if (amount==0) then return end
  38.     if (amount<0) then amount=amount*-1 end
  39.    
  40.     for i=1,amount,1 do
  41.         turtle.dig()
  42.         turtle.digDown()
  43.         if (turtle.forward()) then
  44.             if (Orien==0)then
  45.                 z=z-1
  46.             elseif (Orien==1)then
  47.                 x=x+1
  48.             elseif (Orien==2)then
  49.                 z=z+1
  50.             elseif (Orien==3)then
  51.                 x=x-1
  52.             end
  53.             term.setCursorPos(1,2)
  54.             term.write("I am at: " .. x .. " " .. y .. " " .. z)
  55.         end
  56.     end
  57. end
  58.  
  59. function SetFacing(direction)
  60.     if (direction==3 and Orien==0) then
  61.         turtle.turnLeft()
  62.         Orien=Orien-1
  63.         return
  64.     end
  65.    
  66.     while(true) do
  67.         if (direction==Orien) then
  68.             break
  69.         end
  70.        
  71.         if (direction>Orien) then
  72.             if (turtle.turnRight()) then
  73.                 Orien = Orien+1
  74.                 if (Orien>3) then Orien=0 end
  75.             end
  76.         elseif (direction<Orien) then
  77.             if (turtle.turnLeft()) then
  78.                 Orien = Orien-1
  79.                 if (Orien<-1) then Orien=3 end
  80.             end
  81.         end
  82.     end
  83. end
  84.  
  85. function Movement()
  86.     --X
  87.     if (x>Heading.x) then
  88.         SetFacing(3)
  89.     elseif (x<Heading.x) then
  90.         SetFacing(1)
  91.     end
  92.     MoveForward(Heading.x - x)
  93.    
  94.     --Y
  95.     if (y>Heading.y) then
  96.         MoveDown(Heading.y - y)
  97.     elseif (y<Heading.y) then
  98.         MoveUp(Heading.y - y)
  99.     end
  100.    
  101.     --Z
  102.     if (z>Heading.z) then
  103.         SetFacing(0)
  104.     elseif (z<Heading.z) then
  105.         SetFacing(2)
  106.     end
  107.     MoveForward(Heading.z - z)
  108. end
  109.  
  110. function Search()
  111.     turtle.select(2)
  112.     turtle.equipRight() --Equip scanner
  113.    
  114.     local Scanner = peripheral.wrap("right")
  115.     local scan = Scanner.scan(5)
  116.     if (scan ~= nil) then
  117.         for i, data in ipairs(scan) do
  118.             if (string.match(data.name, Query)) then
  119.                 Heading.x = data.x+x
  120.                 Heading.y = data.y+y
  121.                 Heading.z = data.z+z
  122.                 turtle.equipRight(2) --UnEquip scanner
  123.                 return
  124.             end
  125.         end
  126.         --Wander North
  127.         Heading.x = x
  128.         Heading.y = 0
  129.         Heading.z = z-5
  130.         turtle.equipRight(2) --UnEquip scanner
  131.     else
  132.         --Wander North
  133.         Heading.x = x
  134.         Heading.y = 0
  135.         Heading.z = z-5
  136.         turtle.equipRight(2) --UnEquip scanner
  137.     end
  138. end
  139.  
  140. function Refuel()
  141.     turtle.select(1) --Select bucket
  142.    
  143.     local success, data = turtle.inspect()
  144.     if (success and data.name=="minecraft:lava") then
  145.         turtle.place()
  146.     end
  147.    
  148.     success, data = turtle.inspectUp()
  149.     if (success and data.name=="minecraft:lava") then
  150.         turtle.placeUp()
  151.     end
  152.    
  153.     success, data = turtle.inspectDown()
  154.     if (success and data.name=="minecraft:lava") then
  155.         turtle.placeDown()
  156.     end
  157. end
  158.  
  159. function Inventory()
  160.     local Gold,Wood,Redstone,Glass = false,false,false,false
  161.    
  162.     --1 and 2 are the bucket and the scanner, so DON'T drop them
  163.     IsFull = true
  164.     for i=3,16,1 do
  165.         local data = turtle.getItemDetail(i)
  166.         if (data~=nil) then
  167.             if (string.match(data.name, Query)~=false) then -- Missing to count if items are full stacks
  168.                 turtle.select(i)
  169.                 turtle.drop()
  170.                 IsFull = false
  171.             end
  172.            
  173.             if (string.match(data.name, "gold") and data.count>=14) then Gold=true end
  174.             if (string.match(data.name, "log") and data.count>=2) then Wood=true end
  175.             if (string.match(data.name, "redstone") and data.count>=1) then Redstone=true end
  176.             if (string.match(data.name, "glass") and data.count>=6) then Glass=true end
  177.         end
  178.     end
  179.    
  180.     if (Gold and Wood and Redstone and Glass) then CanReplicate=true end
  181. end
  182.  
  183. function GoHome()
  184.     local FuelNeeded = ((x+y+z)*2)+100 -- Imagine is a straight line and then think it has to dig all the way
  185.                                        -- (+100 just in case there is gravel)
  186.    
  187.     if (turtle.getFuelLevel()<=FuelNeeded or ISFull==true) then
  188.         Heading.x=0
  189.         Heading.y=0
  190.         Heading.z=0
  191.         Movement()
  192.         return true
  193.     end
  194.    
  195.     return false
  196. end
  197.  
  198. function Replicate()
  199.    
  200. end
  201.  
  202. -- Main --
  203. term.setTextColor(colors.white)
  204. term.setCursorPos(1,1)
  205. term.write("Place a bucket in the first slot,")
  206. term.setCursorPos(1,2)
  207. term.write("then a geo scanner in the seccond,")
  208. term.setCursorPos(1,3)
  209. term.write("then equip a pickaxe on the right side")
  210. term.setCursorPos(1,4)
  211. term.write("and a chunk loader on the left.")
  212.  
  213. term.setTextColor(colors.green)
  214. term.setCursorPos(1,6)
  215. term.write("Search for: ")
  216. Query = read()
  217. while(true) do
  218.     term.clear()
  219.     term.setCursorPos(1,1)
  220.     term.write(Query .. " at: " .. Heading.x .. " " .. Heading.y .. " " .. Heading.z)
  221.    
  222.     if (GoHome()==false) then
  223.         Search()
  224.         Movement()
  225.         Refuel()
  226.         Inventory()
  227.         Replicate()
  228.     else
  229.         term.clear()
  230.         term.setCursorPos(1,1)
  231.         term.write("Heading home.")
  232.         break
  233.     end
  234. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement