Advertisement
ceribia

build

Jun 30th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.25 KB | None | 0 0
  1. --------------------------------------------------------
  2. --Position Management
  3. --------------------------------------------------------
  4. local depth = 0
  5. local unloaded = 0
  6. local collected = 0
  7. local xPos,zPos = 0,0
  8. local xDir,zDir = 0,1
  9.  
  10. movements = {}
  11. movements["forward"] = {}
  12. movements["forward"]["detect"] = turtle.detect
  13. movements["forward"]["dig"] = turtle.dig
  14. movements["forward"]["move"] = turtle.forward
  15. movements["forward"]["attack"] = turtle.attack
  16.  
  17. movements["up"] = {}
  18. movements["up"]["detect"] = turtle.detectUp
  19. movements["up"]["dig"] = turtle.digUp
  20. movements["up"]["move"] = turtle.up
  21. movements["up"]["attack"] = turtle.attackUp
  22.  
  23. movements["down"] = {}
  24. movements["down"]["detect"] = turtle.detectDown
  25. movements["down"]["dig"] = turtle.digDown
  26. movements["down"]["move"] = turtle.down
  27. movements["down"]["attack"] = turtle.attackDown
  28.  
  29. local function move(direction)
  30. --print("Attempting to move with " .. direction)
  31. while not movements[direction].move() do
  32. if movements[direction].detect() then
  33. if movements[direction].dig() == false then
  34. error("Failed to dig block")
  35. end
  36. elseif movements[direction].attack() then
  37. -- Do nothing, next loop after attacking
  38. else
  39. sleep( 0.5 )
  40. end
  41. end
  42.  
  43. if direction == "forward" then
  44. xPos = xPos + xDir
  45. zPos = zPos + zDir
  46. elseif direction == "up" then
  47. depth = depth - 1
  48. elseif direction == "down" then
  49. depth = depth + 1
  50. end
  51. end
  52.  
  53. local function turnLeft()
  54. turtle.turnLeft()
  55. xDir, zDir = -zDir, xDir
  56. end
  57.  
  58. local function turnRight()
  59. turtle.turnRight()
  60. xDir, zDir = zDir, -xDir
  61. end
  62.  
  63. function alignXDir( xGoalDir )
  64. print("Turning to " .. xGoalDir)
  65.  
  66. if xGoalDir == xDir then
  67. return
  68. elseif xGoalDir == zDir then
  69. turnRight()
  70. elseif xGoalDir == -zDir then
  71. turnLeft()
  72. else
  73. turnRight()
  74. turnRight()
  75. end
  76.  
  77. if xGoalDir ~= xDir then
  78. error("Failed to fix direction")
  79. end
  80. end
  81.  
  82. function alignZDir( zGoalDir )
  83. if zGoalDir == zDir then
  84. return
  85. elseif zGoalDir == xDir then
  86. turnLeft()
  87. elseif zGoalDir == -xDir then
  88. turnRight()
  89. else
  90. turnRight()
  91. turnRight()
  92. end
  93.  
  94. if zGoalDir ~= zDir then
  95. error("Failed to fix direction")
  96. end
  97. end
  98.  
  99. function doNothing ()
  100. -- Do nothing
  101. end
  102.  
  103. function goTo( x, y, z, atEachSqurare, xd, zd)
  104. print( "GoTo: " .. x .. "," .. y .. "," .. z .. " From: " .. xPos .. "," .. depth .. "," .. zPos)
  105.  
  106. if atEachSqurare == nil then
  107. atEachSqurare = doNothing
  108. end
  109.  
  110. while depth ~= y do
  111. -- print("Depth = " .. depth .. " | y = " .. y)
  112. if depth > y then
  113. move("up")
  114. elseif depth < y then
  115. move("down")
  116. end
  117. atEachSqurare()
  118. end
  119.  
  120. local xGoalDir = (x - xPos) / math.abs(x - xPos)
  121. while xPos ~= x do
  122. alignXDir(xGoalDir)
  123. move("forward")
  124. atEachSqurare()
  125. end
  126.  
  127. local zGoalDir = (z - zPos) / math.abs(z - zPos)
  128. while zPos ~= z do
  129. alignZDir(zGoalDir)
  130. move("forward")
  131. atEachSqurare()
  132. end
  133.  
  134. --Fix alignment
  135. if xd then
  136. alignXDir(xd)
  137. end
  138.  
  139. if zd then
  140. alignZDir(zd)
  141. end
  142. end
  143.  
  144. --------------------------------------------------------
  145. --Inventory Management
  146. --------------------------------------------------------
  147. local marble = {}
  148. marble['material'] = 1
  149. marble['manualReload'] = true
  150.  
  151. local firstSlot = marble['material']
  152.  
  153. local chestZDir = -1
  154. local chestXDir = 0
  155.  
  156. local preChestZDir;
  157. local preChestXDir;
  158.  
  159. local function placeChest(item)
  160. preChestXdir = xDir
  161. preChestZdir = zDir
  162. alignZDir(chestZDir)
  163. alignXDir(chestXDir)
  164.  
  165. if turtle.detect() then
  166. error("Block where chest should go")
  167. end
  168.  
  169. turtle.select(item.chest)
  170.  
  171. if turtle.place() == false then
  172. error("Failed to place chest from slot " .. item.chest)
  173. end
  174. end
  175.  
  176. local function getChest(item)
  177. turtle.select(item.chest)
  178.  
  179. if turtle.dig() == false then
  180. error("Failed to retreive chest to slot " .. item.chest)
  181. end
  182.  
  183. alignZDir(preChestZdir)
  184. alignXDir(preChestXdir)
  185. end
  186.  
  187. local function unload(item)
  188. placeChest(item)
  189.  
  190. if item.lastItem then
  191. for n=item.material,16 do
  192. turtle.select(n)
  193. turtle.drop()
  194. end
  195. else
  196. turtle.select(item.material)
  197. turtle.drop()
  198. end
  199.  
  200. getChest(item)
  201. end
  202.  
  203. local function reload(item)
  204. if turtle.getItemSpace(item.material) == 0 then
  205. return
  206. elseif item.manualReload then
  207. for s = firstSlot, 16 do
  208. turtle.select(s)
  209. turtle.transferTo(item.material)
  210. end
  211.  
  212. while turtle.getItemSpace(item.material) ~= 0 do
  213. print("Waiting for restock on " .. item.material)
  214. os.pullEvent("turtle_inventory")
  215.  
  216. for s = firstSlot, 16 do
  217. turtle.select(s)
  218. turtle.transferTo(item.material)
  219. end
  220. end
  221. else
  222. placeChest(item)
  223. turtle.select(item.material)
  224. while turtle.getItemSpace() > 0 do
  225. if turtle.suck(turtle.getItemSpace()) == false then
  226. print("Couldn't get an item for slot " .. item.material)
  227. sleep(30)
  228. end
  229. end
  230. getChest(item)
  231. end
  232. end
  233.  
  234. local function refuel(item, fuelLimit)
  235. if fuelLimit == nil then
  236. fuelLimit = turtle.getFuelLimit()
  237. end
  238.  
  239. while turtle.getFuelLevel() < fuelLimit do
  240. turtle.select(item.material)
  241. if turtle.getItemCount() > 1 then
  242. turtle.refuel(turtle.getItemCount() - 1)
  243. else
  244. reload(item)
  245. end
  246. end
  247. end
  248.  
  249. local Item = {}
  250. Item["detect"] = {}
  251. Item["detect"]["forward"] = turtle.detect
  252. Item["detect"]["up"] = turtle.detectUp
  253. Item["detect"]["down"] = turtle.detectDown
  254. Item["place"] = {}
  255. Item["place"]["forward"] = turtle.place
  256. Item["place"]["up"] = turtle.placeUp
  257. Item["place"]["down"] = turtle.placeDown
  258. Item["compare"] = {}
  259. Item["compare"]["forward"] = turtle.compare
  260. Item["compare"]["up"] = turtle.compareUp
  261. Item["compare"]["down"] = turtle.compareDown
  262. local function place(item, direction)
  263. if turtle.getItemCount(item.material) <= 1 then
  264. reload(item)
  265. end
  266.  
  267. turtle.select(item.material)
  268. while Item.detect[direction]() == true do
  269. if Item.compare[direction]() == false then
  270. if movements[direction]["dig"]() == false then
  271. error("Failed to dig a block different than item.material")
  272. end
  273. else
  274. break
  275. end
  276. end
  277.  
  278. if Item.detect[direction]() == false then
  279. while Item.place[direction]() == false do
  280. print("Failed to place an item from slot " .. item.material)
  281. movements[direction]["attack"]()
  282. end
  283. end
  284. end
  285.  
  286. local function isFull()
  287. for n=firstSlot,16 do
  288. if turtle.getItemCount(n) == 0 then
  289. --Empty slots
  290. return false;
  291. end
  292. end
  293. print( "No empty slots left." )
  294. return true;
  295. end
  296.  
  297. -----------------------------------------------------------------------------
  298. --Main Program Functions
  299. -----------------------------------------------------------------------------
  300.  
  301. function stock()
  302. if turtle.getFuelLimit() ~= 0 and turtle.getFuelLevel() < 10000 then
  303. refuel(fuel, 20000)
  304. end
  305.  
  306. if isFull() then
  307. unload(loot)
  308. end
  309. end
  310.  
  311. function fillBelow()
  312. place(marble, "down")
  313. end
  314.  
  315. function placeTorch()
  316. alignZDir(-1)
  317. place(torch, "forward")
  318. end
  319.  
  320.  
  321. function fillSquare(maxX, maxZ, yDepth)
  322. --Move to start
  323. goTo(0,yDepth,0,fillBelow)
  324. for z = 0, maxZ do
  325. --Move to the column
  326. goTo(xPos,yDepth,z,fillBelow)
  327.  
  328. --Move to the other end of the column
  329. if xPos == 0 then
  330. goTo(maxX,yDepth,z,fillBelow)
  331. elseif xPos == maxX then
  332. goTo(0,yDepth,z,fillBelow)
  333. else
  334. error("Wasn't at either end of the column. Xpos = " .. xPos)
  335. end
  336. end
  337.  
  338. --Move to start
  339. goTo(0,yDepth,0)
  340. end
  341.  
  342. function outlineSquare(maxX, maxZ, yDepth)
  343. goTo(0,yDepth,0,fillBelow)
  344.  
  345. goTo(maxX,yDepth,0,fillBelow)
  346. goTo(maxX,yDepth,maxZ,fillBelow)
  347. goTo(0,yDepth,maxZ,fillBelow)
  348. goTo(0,yDepth,0,fillBelow)
  349. end
  350.  
  351. -----------------------------------------------------------------------------
  352. --Main Program
  353. -----------------------------------------------------------------------------
  354.  
  355. local tArgs = { ... }
  356. if #tArgs < 4 then
  357. print( "My Version Usage: build shape length width height" )
  358. return
  359. end
  360.  
  361. local shape = tArgs[1]
  362. local length = tonumber( tArgs[2] ) -1
  363. local width = tonumber( tArgs[3] ) -1
  364. local height = tonumber( tArgs[4] ) - 1
  365.  
  366. print( "Building " .. shape )
  367. if shape == "fill" then
  368. for y = 0, height, 1 do
  369. fillSquare(length, width, y * -1)
  370. end
  371.  
  372. elseif shape == "outline" then
  373. for y = 0, height, 1 do
  374. outlineSquare(length, width, y * -1)
  375. end
  376. end
  377.  
  378. alignZDir(1)
  379. print("Done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement