Advertisement
augustclear

Main Turtle Program

Dec 18th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.23 KB | None | 0 0
  1. ---------------------------------------
  2. --------   GLOBAL VARIABLES    --------
  3. ---------------------------------------
  4.  
  5. height = 0
  6.  
  7. ---------------------------------------
  8. -- BASIC FUNCTIONS FOR TURTLE CONTROL -
  9. ---------------------------------------
  10.  
  11. local function gf(n)
  12.   if n==nil then
  13.     n=1
  14.   end
  15.   for i=1,n,1 do
  16.     turtle.forward()
  17.   end
  18. end
  19. local function gb(n)
  20.   if n==nil then
  21.     n=1
  22.   end
  23.   for i=1,n,1 do
  24.     turtle.back()
  25.   end
  26. end
  27. local function gu(n)
  28.   if n==nil then
  29.     n=1
  30.   end
  31.   for i=1,n,1 do
  32.     turtle.up()
  33.   end
  34. end
  35. local function gd(n)
  36.   if n==nil then
  37.     n=1
  38.   end
  39.   for i=1,n,1 do
  40.     turtle.down()
  41.   end
  42. end
  43. local function tl()
  44.   turtle.turnLeft()
  45. end
  46. local function tr()
  47.   turtle.turnRight()
  48. end
  49. local function pf()  turtle.place()       end
  50. local function pu()  turtle.placeUp()     end
  51. local function pd()  turtle.placeDown()   end
  52. local function df()  return turtle.dig()  end
  53. local function du()  turtle.digUp()       end
  54. local function dd()  turtle.digDown()     end
  55. local function sf()  turtle.suck()        end
  56. local function su(n)
  57.   if n==nil then
  58.     while turtle.suckUp() do end
  59.   else
  60.     for i=1,n do
  61.       turtle.suckUp()
  62.     end
  63.   end
  64. end
  65. local function sd(n)
  66.   if n==nil then
  67.     while turtle.suckDown() do end
  68.   else
  69.     for i=1,n do
  70.       turtle.suckDown()
  71.     end
  72.   end
  73. end
  74. local function Df()  turtle.drop()       end
  75. local function Du()  turtle.dropUp()     end
  76. local function Dd(n)
  77.   if n==nil then n=64 end
  78.   turtle.dropDown(n)
  79. end
  80. local function ss(s) turtle.select(s)    end
  81. local function cf() turtle.compare()     end
  82. local function cu() turtle.compareUp()   end
  83.  
  84. ---------------------------------------
  85. --          BUILDING BLOCKS          --
  86. ---------------------------------------
  87.  
  88. function messageHandler()
  89.     local event, senderId, message, protocol = os.pullEvent("rednet_message")
  90.     if protocol ~= "worker" then
  91.         return
  92.     elseif message == "location" then
  93.         print("Sending location to ID#"..tostring(senderId))
  94.         sendLocation(senderId)
  95.     elseif message == "dig" then
  96.         rednet.send(senderId,"Digging...","home")
  97.         print("Digging...")
  98.         clearArea(4,4)
  99.     elseif message == "reboot" then
  100.         rednet.send(senderId,"Rebooting...","home")
  101.         shell.run("reboot")
  102.     else
  103.         print("Invalid message:"..message)
  104.     end
  105. end
  106.  
  107. function sendLocation(host)
  108.     local x, y, z = gps.locate()
  109.     --print("ID:" .. homeid)
  110.     local message = tostring(x) .. " " .. tostring(y) .. " " .. tostring(z)
  111.     rednet.send(host,message,"home")
  112. end
  113.  
  114. function digColumn()
  115.     local height = 0
  116.     while turtle.inspect() do
  117.         df()
  118.     end
  119.     gf()
  120.     while turtle.inspectUp() do
  121.         while turtle.inspectUp() do
  122.             du()
  123.         end
  124.         gu()
  125.         height = height + 1
  126.     end
  127.     while height > 0 do
  128.         gd()
  129.         height = height - 1
  130.     end
  131. end
  132.  
  133. function clearArea(length, width)
  134.     local x1,y1,z1 = gps.locate()
  135.     local x2,y2,z2 = x1,y1,z1
  136.     digColumn()
  137.     x2,y2,z2 = gps.locate()
  138.    
  139. end
  140.  
  141. ---------------------------------------
  142. --           MAIN PROGRAM            --
  143. ---------------------------------------
  144.  
  145.  
  146. print("Starting main...")
  147.  
  148. --Register for Rednet
  149.  
  150. rednet.host("worker","ana")
  151. rednet.open("right")
  152.  
  153. --clearArea(4,4)
  154.  
  155. --while true do
  156.      --messageHandler()
  157. --end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement