Artkek

tutel

Mar 26th, 2023
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 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 = 2
  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. if turtle.detectDown() == true then
  67. if turtle.compareDown() == true then
  68. return true
  69. else
  70. sendM("Can't place block("..gpsLoc(x,y)..")!")
  71. return false
  72. end
  73. else
  74. if turtle.placeDown() == true then
  75. return true
  76. else
  77. sendM("Can't place block("..gpsLoc(x,y)..")!")
  78. return false
  79. end
  80. end
  81. end
  82. round = function (inputN)
  83. if inputN % 2 ~= 0.5 then
  84. return math.floor(inputN + 0.5)
  85. end
  86. return inputN - 0.5
  87. end
  88. turtle.select(currSlot)
  89. local coords = {}
  90. local coordsCount = 0
  91. local f = 1 - r
  92. local ddF_x = 1
  93. local ddF_y = -2 * r
  94. x = 0
  95. y = r
  96. coords[0 .. "," .. round(0+r)] = true
  97. coords[0 .. "," .. round(0-r)] = true
  98. coords[0+r .. "," .. 0] = true
  99. coords[0-r .. "," .. 0] = true
  100. coordsCount = 4
  101. while (x < y) do
  102. if (f >= 0) then
  103. y = y - 1
  104. ddF_y = ddF_y + 2
  105. f = f + ddF_y
  106. end
  107. x = x + 1
  108. ddF_x = ddF_x + 2
  109. f = f + ddF_x
  110. coords[round(0+x) .. "," .. round(0+y)] = true
  111. coords[round(0-x) .. "," .. round(0+y)] = true
  112. coords[round(0+x) .. "," .. round(0-y)] = true
  113. coords[round(0-x) .. "," .. round(0-y)] = true
  114. coords[round(0+y) .. "," .. round(0+x)] = true
  115. coords[round(0-y) .. "," .. round(0+x)] = true
  116. coords[round(0+y) .. "," .. round(0-x)] = true
  117. coords[round(0-y) .. "," .. round(0-x)] = true
  118. coordsCount = coordsCount + 8
  119. end
  120. checkCoords = function (x0,y0)
  121. if coords[x0 .. "," .. y0] == true then
  122. return true
  123. else
  124. return false
  125. end
  126. end
  127. --watch fuel level and if smaller than the reserve level refuel from slot #1
  128. function fuelProc()
  129. local flvl = turtle.getFuelLevel()
  130. if flvl == "unlimited" then
  131. return true
  132. elseif flvl > fReserve then
  133. return true
  134. else
  135. turtle.select(1)
  136. while turtle.refuel(1) == false do
  137. if isSent == false then
  138. sendM("Out of fuel! turtle at "..gpsLoc(x,y))
  139. print("Please insert fuel to slot #1 to resume!")
  140. isSent = true
  141. end
  142. sleep(5)
  143. end
  144. isSent = false
  145. turtle.select(currSlot)
  146. end
  147. end
  148. -- Move from the center to the first position it can find.
  149. local x = 0
  150. local y = 0
  151. while checkCoords(x,y) ~= true do
  152. fuelProc()
  153. turtle.forward()
  154. x = x + 1
  155. end
  156. -- Place a block in the first position.
  157. --findBlock()
  158. --tryPlaceDown() <-block will be placed last so won't be any route blocking at multi lvl structures
  159. -- Keep track of which way the turtle is facing.
  160. facingX = 1
  161. facingY = 0
  162. -- Look clockwise from each block and find the next block in the space.
  163. spinsDone = 0
  164. while (coordsCount > 0 and spinsDone < 5) do
  165. fuelProc()
  166. cX = x + facingX
  167. cY = y + facingY
  168. if checkCoords(cX, cY) then
  169. turtle.forward()
  170. findBlock()
  171. tryPlaceDown(x,y)
  172. x = cX
  173. y = cY
  174. coords[x .. "," .. y] = nil
  175. coordsCount = coordsCount - 1
  176. spinsDone = 0
  177. else
  178. -- Check diagnals
  179. if facingX == 1 then
  180. cY = y + 1
  181. newFacingX = 0
  182. newFacingY = 1
  183. elseif facingX == -1 then
  184. cY = y - 1
  185. newFacingX = 0
  186. newFacingY = -1
  187. elseif facingY == 1 then
  188. cX = x - 1
  189. newFacingX = -1
  190. newFacingY = 0
  191. elseif facingY == -1 then
  192. cX = x + 1
  193. newFacingX = 1
  194. newFacingY = 0
  195. end
  196.  
  197. if checkCoords(cX,cY) then
  198. turtle.forward()
  199. turtle.turnRight()
  200. turtle.forward()
  201. turtle.turnLeft()
  202. findBlock()
  203. tryPlaceDown(x,y)
  204. x = cX
  205. y = cY
  206. coords[x .. "," .. y] = nil
  207. coordsCount = coordsCount - 1
  208. spinsDone = 0
  209. else
  210. turtle.turnRight()
  211. facingX = newFacingX
  212. facingY = newFacingY
  213. spinsDone = spinsDone + 1
  214. end
  215. end
  216. end
  217. --return to starting point
  218. repeat
  219. fuelProc()
  220. turtle.forward()
  221. x = x - 1
  222. until x == 0
  223. turtle.turnRight()
  224. turtle.turnRight()
  225. sendM("Work done!")
  226. x = nil
  227. y = nil
  228. r = nil
  229. coords = nil
  230. coordsCount = nil
  231.  
Add Comment
Please, Sign In to add comment