Advertisement
Guest User

ComputerCraft Platform Builder

a guest
Jul 5th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.76 KB | None | 0 0
  1. --[[
  2. Credit to Zach Dyer for the original program that I based this on
  3. 'Easy Platform Builder'.
  4.  
  5. Current version: 2.0
  6. Pastebin: M0i8fDU5
  7.  
  8. Customized by MMaster
  9.  
  10. Changelog:
  11. 5/20/2014 - added torch placing, block detection
  12. 5/31/2014 - added mob attacking, better block detection,
  13. inventory requests when empty, resume on keypress.
  14. --]]
  15.  
  16. local tArgs = {...}
  17.  
  18. local length = tonumber(tArgs[1])
  19. local width = tonumber(tArgs[2])
  20. local torch_interval = tonumber(tArgs[3])
  21. local go_down = tonumber(tArgs[4])
  22.  
  23. if torch_interval == nil then
  24.     torch_interval = 0
  25. end
  26. if go_down == nil then
  27.     go_down = 0
  28. end
  29.  
  30. local turnRight = true
  31. local slot = 1
  32. local interval = 0
  33.  
  34. function pauseAnyKey()
  35.    -- if event == "key" and p1 == keys.q then
  36.    while true do
  37.         local event, p1,p2,p3 = os.pullEvent()
  38.         if event == "key" then
  39.             return p1
  40.         end
  41.         sleep(0.5)
  42.    end
  43. end
  44.  
  45. -- Kudos to BlockSmith on ComputerCraft.info forums for the suggestion!
  46. function refuel(dim1,dim2)
  47.         dim1 = dim1 or 1
  48.         dim2 = dim2 or 1
  49.         fuelReq = dim1 * dim2
  50.         turtle.select(15)  
  51.         while (turtle.getFuelLevel() < fuelReq) do
  52.                 --inform the user that the turtle is out of fuel
  53.                 print("Waiting for fuel in slot 15...")
  54.                 print("Press any key to continue.")
  55.                 pauseAnyKey()
  56.                 -- use all of the fuel in slot 15
  57.                 turtle.refuel()
  58.                 -- then go back and check the level again
  59.         end
  60.        
  61.         -- select the active slot
  62.         turtle.select(slot)
  63. end
  64.  
  65. function flip()
  66.     turtle.turnRight()
  67.     turtle.turnRight()
  68. end
  69.  
  70. function digDetect()
  71.     while turtle.detect() do
  72.         turtle.dig()
  73.     end
  74. end
  75.  
  76. function psycho()
  77.     turtle.attack()
  78.     turtle.attackUp()
  79.     turtle.attackDown()
  80.     turtle.turnLeft()
  81.     turtle.attack()
  82.     turtle.turnRight()
  83.     turtle.turnRight()
  84.     turtle.attack()
  85.     turtle.turnLeft()
  86. end
  87.  
  88. function digDetectUp()
  89.     while turtle.detectUp() do
  90.         turtle.digUp()
  91.     end
  92. end
  93.  
  94. function tryForward()
  95.     while not turtle.forward() do
  96.         psycho() --turtle.attack()
  97.         turtle.dig()
  98.     end
  99. end
  100.  
  101. function tryPlaceDown(mode)
  102.     mode = mode or "ignore me"
  103.     while not turtle.placeDown() do
  104.         if mode == "dig" then
  105.             turtle.digDown()
  106.         end
  107.         --turtle.attackDown()
  108.         psycho()
  109.         turtle.attack()
  110.     end
  111. end
  112.  
  113. function findInventory()
  114.     for i=slot,14 do
  115.         if turtle.getItemCount(i) > 0 then
  116.             return i
  117.         end
  118.     end
  119.     for i=1,slot-1 do
  120.         if turtle.getItemCount(i) > 0 then
  121.             return i
  122.         end
  123.     end
  124.     return -1
  125. end
  126.  
  127. function torchPlacer()
  128.     turtle.select(16)
  129.     if turtle.getItemCount(16) == 0 then
  130.         print("I need more torches!")
  131.         print("Put more torches in slot 16 and press any key to make me resume!")
  132.         pauseAnyKey()
  133.     end
  134.     turtle.placeBack()
  135. end
  136.  
  137. if width == nil then
  138.     print("1. Place turtle facing direction of said platform on left side.")
  139.     print("2. Load turtle with materials for the platform. If placing torches, add light source blocks to slot 16.")
  140.     print("3. Type 'platform <length> <width> [torch_interval]'")
  141.     return
  142. end
  143.  
  144. turtle.select(slot)
  145.  
  146.  
  147. -- set the initial fuel load
  148. refuel(length,width)
  149. tryForward()
  150. if turtle.getItemCount(slot) == 0 then
  151. x = findInventory()
  152. while x == -1 do    
  153.   if x == -1 then
  154.       print("I ran out of materials!")
  155.       print("Put mats in slots 1 to 14 then")
  156.       print("press any key to continue.")
  157.       pauseAnyKey()
  158.       x = findInventory()
  159.   end
  160. end
  161. slot = x
  162. end
  163. for i = 1, go_down, 1 do
  164.   turtle.down()
  165. end
  166. for j = 1, width, 1 do
  167.   for i = 1, length, 1 do
  168.     if turtle.getItemCount(slot) == 0 then
  169.         x = findInventory()
  170.       while x == -1 do    
  171.           if x == -1 then
  172.               print("I ran out of materials!")
  173.               print("Put mats in slots 1 to 14 then")
  174.               print("press any key to continue.")
  175.               pauseAnyKey()
  176.               x = findInventory()
  177.           end
  178.      end
  179.      slot = x
  180.     end
  181.  
  182.     turtle.select(slot)
  183.     tryPlaceDown("dig")
  184.    
  185.     if i < length then
  186.       digDetect()
  187.       tryForward()
  188.     end
  189.  
  190.     if (torch_interval > 0) and (j % torch_interval == 0) then
  191.         interval = interval + 1
  192.         if interval == torch_interval then
  193.           interval = 0
  194.           torchPlacer()
  195.         end
  196.     end
  197.    
  198.   end
  199.  
  200.   if j < width then
  201.  
  202.     if turnRight == true then
  203.       turtle.turnRight()
  204.       digDetect()
  205.       tryForward()
  206.       turtle.turnRight()
  207.       turnRight = false
  208.     else
  209.       turtle.turnLeft()
  210.       digDetect()
  211.       tryForward()
  212.       turtle.turnLeft()
  213.       turnRight = true
  214.     end
  215.    
  216.   end
  217.  
  218.   refuel(5) -- cursory check for fuel
  219. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement