snirkimmington

Circle Maker

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