Kirkq

Minecraft ComputerCraft Turtle

Jul 28th, 2019
2,243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. -- pastebin get aK9V8ZyP turtlescript
  2.  
  3. -- Assumptions
  4. -- +Z is towards the sky.
  5. -- +Y is the starting direction.
  6. -- The area is totally clear of blocks, mobs, and players.
  7. -- The Turtle starts above a chest or ender chest outside of the structure at (x,y,z) (0,0,+2)
  8. -- The Turtle always places downwards.
  9. -- Block orientation is as placed downwards.
  10. -- For consistency always face +y when placing a block.
  11.  
  12. -- The Turtle inventory is preloaded with slots 1 through 16 and never runs out from the chest.
  13. -- Slot 1 is fuel.
  14.  
  15. --XOO
  16. --XOO
  17. --TXX
  18.  
  19. xHome = 0
  20. yHome = 0
  21. zHome = 2
  22.  
  23. local xPos = xHome
  24. local yPos = yHome
  25. local zPos = zHome
  26. local xDir = 0
  27. local yDir = 1
  28.  
  29. local blockToPlace = 0
  30.  
  31. local debugFlag = 1
  32.  
  33. --------- Stuff you need to edit ----------
  34.  
  35. Array2d = {}
  36.  
  37. Array2d[1] = {
  38. {"2","3","4","4"},
  39. {"5","0","7","7"},
  40. {"8","9","0","0"}
  41. }
  42.  
  43. Array2d[2] = {
  44. {"3","4","5","5"},
  45. {"6","7","8","8"},
  46. {"9","0","0","0"}
  47. }
  48.  
  49. Array3d = {1,2}
  50.  
  51. local XSIZE = 4
  52. local YSIZE = 3
  53. local ZSIZE = table.getn(Array3d)
  54.  
  55. --------- End stuff you need to edit ----------------
  56.  
  57. --123456789abcdefg
  58. --0 denotes empty.
  59. function convertString(str)
  60. if (str == "0") then
  61. ret = 0
  62. elseif (str == "1") then
  63. ret = 1
  64. elseif (str == "2") then
  65. ret = 2
  66. elseif (str == "3") then
  67. ret = 3
  68. elseif (str == "4") then
  69. ret = 4
  70. elseif (str == "5") then
  71. ret = 5
  72. elseif (str == "6") then
  73. ret = 6
  74. elseif (str == "7") then
  75. ret = 7
  76. elseif (str == "8") then
  77. ret = 8
  78. elseif (str == "9") then
  79. ret = 9
  80. elseif (str == "a") then
  81. ret = 10
  82. elseif (str == "b") then
  83. ret = 11
  84. elseif (str == "c") then
  85. ret = 12
  86. elseif (str == "d") then
  87. ret = 13
  88. elseif (str == "e") then
  89. ret = 14
  90. elseif (str == "f") then
  91. ret = 15
  92. elseif (str == "g") then
  93. ret = 16
  94. else
  95. ret = 0
  96. end
  97. return ret
  98. end
  99.  
  100. function debugPrint(str)
  101. if (debugFlag==1) then
  102. print(str)
  103. end
  104. end
  105.  
  106. -- Dervied from: http://www.computercraft.info/wiki/Excavate
  107. local function turnLeft()
  108. turtle.turnLeft()
  109. xDir, yDir = -yDir, xDir
  110. end
  111.  
  112. local function turnRight()
  113. turtle.turnRight()
  114. xDir, yDir = yDir, -xDir
  115. end
  116.  
  117. function goTo(x,y,z)
  118. goToFacing(x,y,z,xDir,yDir)
  119. end
  120.  
  121. function goToFacing( x, y, z, xd, yd )
  122. debugPrint("Starting at " .. xPos .. " " .. yPos .. " " .. zPos)
  123. debugPrint("Going to " .. x .. " " .. y .. " " .. z)
  124. -- This function moves on the z-axis first.
  125. while zPos > z do
  126. if turtle.down() then
  127. debugPrint("Went Down")
  128. zPos = zPos - 1
  129. else
  130. debugPrint("Can't go Down")
  131. sleep( 0.5 )
  132. end
  133. end
  134.  
  135. while zPos < z do
  136. if turtle.up() then
  137. debugPrint("Went Up")
  138. zPos = zPos + 1
  139. else
  140. debugPrint("Can't go Up")
  141. sleep( 0.5 )
  142. end
  143. end
  144.  
  145. if xPos > x then
  146. while xDir ~= -1 do
  147. if (xDir == 0 and yDir == -1) then
  148. debugPrint("Turning Right xPos > x")
  149. turnRight()
  150. else
  151. debugPrint("Turning Left xPos > x")
  152. turnLeft()
  153. end
  154. end
  155. while xPos > x do
  156. if turtle.forward() then
  157. debugPrint("Went Forward xPos > x")
  158. xPos = xPos - 1
  159. else
  160. debugPrint("Can't go Forward xPos > x")
  161. sleep( 0.5 )
  162. end
  163. end
  164. elseif xPos < x then
  165. while xDir ~= 1 do
  166. if (xDir == 0 and yDir == 1) then
  167. debugPrint("Turning Right xPos < x")
  168. turnRight()
  169. else
  170. debugPrint("Turning Left xPos < x")
  171. turnLeft()
  172. end
  173. end
  174. while xPos < x do
  175. if turtle.forward() then
  176. debugPrint("Went Forward xPos < x")
  177. xPos = xPos + 1
  178. else
  179. debugPrint("Can't go Forward xPos < x")
  180. sleep( 0.5 )
  181. end
  182. end
  183. end
  184.  
  185. if yPos > y then
  186. while yDir ~= -1 do
  187. if ( xDir == 1 and yDir == 0) then
  188. debugPrint("Turning Right yPos > y")
  189. turnRight()
  190. else
  191. debugPrint("Turning Left yPos > y")
  192. turnLeft()
  193. end
  194. end
  195. while yPos > y do
  196. if turtle.forward() then
  197. debugPrint("Went Forward yPos > y")
  198. yPos = yPos - 1
  199. else
  200. debugPrint("Can't go Forward yPos > y")
  201. sleep( 0.5 )
  202. end
  203. end
  204. elseif yPos < y then
  205. while yDir ~= 1 do
  206. if ( xDir == -1 and yDir == 0) then
  207. debugPrint("Turning Right yPos < y")
  208. turnRight()
  209. else
  210. debugPrint("Turning Left yPos < y")
  211. turnLeft()
  212. end
  213. end
  214. while yPos < y do
  215. if turtle.forward() then
  216. debugPrint("Went Forward yPos < y")
  217. yPos = yPos + 1
  218. else
  219. debugPrint("Can't go Forward yPos < y")
  220. sleep( 0.5 )
  221. end
  222. end
  223. end
  224.  
  225. while yDir ~= yd or xDir ~= xd do
  226. if (xd == yDir and yd == -xDir) then
  227. debugPrint("Returning Facing Right")
  228. turnRight()
  229. else
  230. debugPrint("Returning Facing Left")
  231. turnLeft()
  232. end
  233. end
  234. end
  235.  
  236. -- Refill the inventory from the chest
  237. function refill()
  238. for i=1,16 do
  239. turtle.select(i)
  240. turtle.suckDown()
  241. end
  242. refuel()
  243. end
  244.  
  245. function refuel()
  246. turtle.select(1)
  247. if(turtle.refuel(63)) then
  248. debugPrint("Refueled")
  249. else
  250. debugPrint("Could not Refuel")
  251. end
  252. fuel=turtle.getFuelLevel()
  253. debugPrint("Fuel is " .. fuel)
  254. end
  255.  
  256. function returnHome()
  257. -- Goes to 0,0 first.
  258. goTo(xHome,yHome,zPos)
  259. goTo(xHome,yHome,zHome)
  260. end
  261.  
  262. function returnHomeFacing01()
  263. -- Goes to 0,0 first.
  264. goTo(xHome,yHome,zPos)
  265. goToFacing(xHome,yHome,zHome,0,1)
  266. end
  267.  
  268. debugPrint("XSIZE, YSIZE, ZSIZE" .. table.getn(Array2d[1][1]) .. " " .. table.getn(Array2d[1]) .. " " .. ZSIZE)
  269.  
  270. refill()
  271. for zIndex =1,ZSIZE do
  272. for yIndex=1,YSIZE do
  273. for xIndex=1,XSIZE do
  274. blockToPlace = convertString(Array2d[Array3d[zIndex]][1+YSIZE - yIndex][xIndex]);
  275. fuel=turtle.getFuelLevel()
  276. debugPrint("Prepare to Place " .. blockToPlace .. " at " .. xIndex .. " " .. yIndex .. " " .. " " .. zIndex)
  277.  
  278. -- If we don't have the material or are low on fuel, return home, refill, come back to the z-axis.
  279. if (blockToPlace ~= 0) then
  280. if (turtle.getItemCount(blockToPlace) < 2 or fuel < 200) then
  281. debugPrint("Returning Home")
  282. returnHome()
  283. debugPrint("Refilling")
  284. refill()
  285. end
  286. -- Go to and face +y
  287. debugPrint("goToFacing " .. xIndex .. " " .. yIndex .. " " .. zIndex + 1 .. " - - 0 1")
  288. goToFacing( xIndex, yIndex, zIndex+1, 0, 1 )
  289. debugPrint("Select Block")
  290. turtle.select(blockToPlace)
  291. turtle.placeDown()
  292. end
  293. end
  294. end
  295. end
  296. returnHomeFacing01()
  297. debugPrint("Done")
Add Comment
Please, Sign In to add comment