ferrybig

computercraft circle

Jan 30th, 2014
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.63 KB | None | 0 0
  1. local r = 0
  2. currSlot = 2 --reserve slot #1 for fuel all time!
  3. mfid = 0 --monitor computer's id
  4. osid = os.getComputerID() --turtle's computer id
  5. isSent = false --send all messages just once
  6. fReserve = 10 --fuel can't go below this
  7. print("What radius of circle would you like me to create?")
  8. r = tonumber(io.read())
  9. if r == 0 then
  10.   print("Radius 0 circle? Done!")
  11. end
  12. --send message to monitor computer(if any) and write it out locally
  13. function sendM(message)
  14.   if peripheral.isPresent("right") and peripheral.getType("right") == "modem" then
  15.         rednet.open("right")
  16.         rednet.send(mfid, "Turtle("..osid..":("..message, false)
  17.         rednet.close("right")
  18.   end
  19.   print(message)
  20. end
  21. --return gps location if it has a modem and can retrive turtles's location
  22. --if not then return x,y based on turtle's startup location
  23. function gpsLoc(fx,fy)
  24.   if peripheral.isPresent("right") and peripheral.getType("right") == "modem" then
  25.         rednet.open("right")
  26.         local x, y, z = gps.locate(5)
  27.         rednet.close("right")
  28.         if x == nil then
  29.           return "x:"..tostring(fx)..",y:"..tostring(fy).."(rel)"
  30.         else
  31.           return "x:"..tostring(x)..",y:"..tostring(y)..",z:"..tostring(z)
  32.         end
  33.   else
  34.         return "x:"..tostring(fx)..",y:"..tostring(fy).."(rel)"
  35.   end
  36. end
  37. function findBlock ()
  38.   if turtle.getItemCount(currSlot) ~= 0 then
  39.         return true
  40.   end
  41.  
  42.   local tmp = 8
  43.   turtle.select(tmp)
  44.  
  45.   while (turtle.getItemCount(tmp) == 0 and tmp <= 16) do
  46.         if tmp == 16 then
  47.           if isSent == false then --only print once
  48.                 sendM("Out of blocks! Turtle at "..gpsLoc(x,y))
  49.                 isSent = true
  50.           end
  51.           sleep(5)
  52.           tmp = 2
  53.           turtle.select(tmp)
  54.         else
  55.           tmp = tmp + 1
  56.           turtle.select(tmp)
  57.         end
  58.   end
  59.   isSent = false
  60.   currSlot = tmp
  61.   tmp = 2
  62.  
  63.   return true
  64. end
  65. function tryPlaceDown (x,y)
  66.   turtle.digDown()
  67.   if turtle.detectDown() == true then
  68.         if turtle.compareDown() == true then
  69.           return true
  70.         else
  71.           sendM("Can't place block("..gpsLoc(x,y)..")!")
  72.           return false
  73.         end
  74.   else
  75.         if turtle.placeDown() == true then
  76.           return true
  77.         else
  78.           sendM("Can't place block("..gpsLoc(x,y)..")!")
  79.           return false
  80.         end
  81.   end
  82. end
  83. function forward()
  84. local count = 0;
  85. while not turtle.forward() do
  86.   sleep(0) --Calls coroutine.yield to prevent errors
  87.   count = count + 1
  88.   if not turtle.dig() then
  89.     turtle.attack()
  90.   end
  91.   if count > 10 then
  92.     turtle.attack()
  93.     sleep(0.2)
  94.   end
  95.   if count > 50 then
  96.      sendM("error?");
  97.      sleep(2);
  98.   end
  99. end
  100. end
  101. round = function (inputN)
  102.   if inputN % 2 ~= 0.5 then
  103.         return math.floor(inputN + 0.5)
  104.   end
  105.   return inputN - 0.5
  106. end
  107. turtle.select(currSlot)
  108. local coords = {}
  109. local coordsCount = 0
  110. local f = 1 - r
  111. local ddF_x = 1
  112. local ddF_y = -2 * r
  113. x = 0
  114. y = r
  115. coords[0 .. "," .. round(0+r)] = true
  116. coords[0 .. "," .. round(0-r)] = true
  117. coords[0+r .. "," .. 0] = true
  118. coords[0-r .. "," .. 0] = true
  119. coordsCount = 4
  120. while (x < y) do
  121.   if (f >= 0) then
  122.         y = y - 1
  123.         ddF_y = ddF_y + 2
  124.         f = f + ddF_y
  125.   end
  126.   x = x + 1
  127.   ddF_x = ddF_x + 2
  128.   f = f + ddF_x
  129.   coords[round(0+x) .. "," .. round(0+y)] = true
  130.   coords[round(0-x) .. "," .. round(0+y)] = true
  131.   coords[round(0+x) .. "," .. round(0-y)] = true
  132.   coords[round(0-x) .. "," .. round(0-y)] = true
  133.   coords[round(0+y) .. "," .. round(0+x)] = true
  134.   coords[round(0-y) .. "," .. round(0+x)] = true
  135.   coords[round(0+y) .. "," .. round(0-x)] = true
  136.   coords[round(0-y) .. "," .. round(0-x)] = true
  137.   coordsCount = coordsCount + 8
  138. end
  139. checkCoords = function (x0,y0)
  140.   if coords[x0 .. "," .. y0] == true then
  141.         return true
  142.   else
  143.         return false
  144.   end
  145. end
  146. --watch fuel level and if smaller than the reserve level refuel from slot #1
  147. function fuelProc()
  148.   local flvl = turtle.getFuelLevel()
  149.   if flvl == "unlimited" then
  150.         return true
  151.   elseif flvl > fReserve then
  152.         return true
  153.   else
  154.         turtle.select(1)
  155.         while turtle.refuel(1) == false do
  156.           if isSent == false then
  157.                 sendM("Out of fuel! turtle at "..gpsLoc(x,y))
  158.                 print("Please insert fuel to slot #1 to resume!")
  159.                 isSent = true
  160.           end
  161.           sleep(5)
  162.         end
  163.         isSent = false
  164.         turtle.select(currSlot)
  165.   end
  166. end
  167. -- Move from the center to the first position it can find.
  168. local x = 0
  169. local y = 0
  170. while checkCoords(x,y) ~= true do
  171.   fuelProc()
  172.   forward()
  173.   x = x + 1
  174. end
  175. -- Place a block in the first position.
  176. --findBlock()
  177. --tryPlaceDown() <-block will be placed last so won't be any route blocking at multi lvl structures
  178. -- Keep track of which way the turtle is facing.
  179. facingX = 1
  180. facingY = 0
  181. -- Look clockwise from each block and find the next block in the space.
  182. spinsDone = 0
  183. while (coordsCount > 0 and spinsDone < 5) do
  184.   fuelProc()
  185.   cX = x + facingX
  186.   cY = y + facingY
  187.   if checkCoords(cX, cY) then
  188.         forward()
  189.         findBlock()
  190.         tryPlaceDown(x,y)
  191.         x = cX
  192.         y = cY
  193.         coords[x .. "," .. y] = nil
  194.         coordsCount = coordsCount - 1
  195.         spinsDone = 0
  196.   else
  197.         -- Check diagnals
  198.         if facingX == 1 then
  199.                   cY = y + 1
  200.                   newFacingX = 0
  201.                   newFacingY = 1
  202.         elseif facingX == -1 then
  203.                   cY = y - 1
  204.                   newFacingX = 0
  205.                   newFacingY = -1
  206.         elseif facingY == 1 then
  207.                   cX = x - 1
  208.                   newFacingX = -1
  209.                   newFacingY = 0
  210.         elseif facingY == -1 then
  211.                   cX = x + 1
  212.                   newFacingX = 1
  213.                   newFacingY = 0
  214.         end
  215.  
  216.         if checkCoords(cX,cY) then
  217.                   forward()
  218.                   turtle.turnRight()
  219.                   forward()
  220.                   turtle.turnLeft()
  221.                   findBlock()
  222.                   tryPlaceDown(x,y)
  223.                   x = cX
  224.                   y = cY
  225.                   coords[x .. "," .. y] = nil
  226.                   coordsCount = coordsCount - 1
  227.                   spinsDone = 0
  228.         else
  229.                   turtle.turnRight()
  230.                   facingX = newFacingX
  231.                   facingY = newFacingY
  232.                   spinsDone = spinsDone + 1
  233.         end
  234.   end
  235. end
  236. --return to starting point
  237. repeat
  238.   fuelProc()
  239.   forward()
  240.   x = x - 1
  241. until x == 0
  242. turtle.turnRight()
  243. turtle.turnRight()
  244. sendM("Work done!")
  245. x = nil
  246. y = nil
  247. r = nil
  248. coords = nil
  249. coordsCount = nil
Advertisement
Add Comment
Please, Sign In to add comment