Advertisement
peregrin5

spleefArena

Sep 21st, 2019
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.39 KB | None | 0 0
  1. --[[
  2.  
  3. Spleef Arena Rebuilder
  4.  
  5.   ]]--
  6.  
  7. local W = 1
  8. local D = 2
  9. local even
  10. local arg = { ... }
  11. local currentSlot
  12.  
  13.  
  14. -- Argument checking
  15. if (#arg ~= 2) then
  16. error("Usage: "..shell.getRunningProgram().." <width> <depth>")
  17. end
  18.  
  19. -- Set dimensions of plane
  20. local planeSize = {tonumber(arg[W]), tonumber(arg[D])}
  21.  
  22. -- See if the plane width is even or odd.
  23. --   This is for seeing if the turtle ends in the front or back.
  24. if (planeSize[W] % 2 == 0) then
  25.     even = 1
  26. else
  27.     even = 0
  28. end
  29.  
  30. -- ------- Functions ------- --
  31.  
  32. -- Determine if the turtle has enough fuel to perform the requested job.
  33. function fuelUp()  -- Let's make sure we have all the fuel needed to complete the job.
  34.     turtle.select(16) -- Fuel will always be in slot 16
  35.  
  36.     currentFuelAmount = turtle.getFuelLevel()
  37.     currentCoalAmount = turtle.getItemCount(16)
  38.  
  39.     totalFuelRequired = ((4*planeSize[D])*planeSize[W])+(2*planeSize[D])+(2*planeSize[W])
  40.     remainingFuelRequired = totalFuelRequired-currentFuelAmount
  41.     remainingCoalRequired = math.ceil((totalFuelRequired-currentFuelAmount)/80)
  42.  
  43.     if (remainingCoalRequired < 0) then
  44.         remainingCoalRequired = 0
  45.     end
  46.  
  47.     if ((remainingFuelRequired > 0 ) and (remainingCoalRequired < currentCoalAmount+1)) then
  48.         turtle.refuel(remainingCoalRequired)
  49.     elseif (currentFuelAmount < totalFuelRequired) then
  50.         if (remainingCoalRequired < 64) then error("I'm sorry, but I do not have enough fuel.  "..remainingCoalRequired.." coal in slot 16 should do it.")
  51.         elseif (remainingCoalRequired > 64) then
  52.             error("I'm sorry, but I don't have enough fuel to begin, and the required fuel is beyond what slot 16 can handle.  Maybe if you manually refuel first.  I will need a fuel level of approximately "..totalFuelRequired..".  I'd say that "..math.ceil(math.ceil((totalFuelRequired-currentFuelAmount)/80)/64).." stacks should do it.")
  53.         end
  54.     end
  55.  
  56. end
  57.  
  58. -- count items in inventory
  59.  
  60. function countItems()
  61.     local totalMaterials = 0
  62.     for currentSlot = 1, 15, 1 do
  63.                     totalMaterials = totalMaterials + turtle.getItemCount(currentSlot)
  64.     end
  65. return totalMaterials
  66. end
  67.  
  68. -- Determine if the turtle has enough material to perform the requested job.
  69. function takeStock()
  70.  
  71.         local totalMaterials = countItems()
  72.  
  73.         totalMaterialsRequired = planeSize[D]*planeSize[W]
  74.  
  75.         if (totalMaterialsRequired > totalMaterials) and (totalMaterialsRequired > 960) then
  76.                 error ("I'm sorry, but there are not enough material slots to make a plane that large.")
  77.         elseif (totalMaterialsRequired > totalMaterials) and (totalMaterialsRequired < 960) then
  78.                                 print("Filling from chest above.")
  79.                                 suckAmount = totalMaterialsRequired - totalMaterials
  80.                                 stackNo = math.ceil(suckAmount/64)
  81.  
  82.                                 for slotFill = 1, stackNo, 1 do
  83.                                     turtle.select(slotFill)
  84.                                     turtle.suckUp()
  85.                                     sleep(5)
  86.                                 end
  87.  
  88.                                 totalMaterials = countItems()
  89.                                 turtle.select(stackNo)
  90.                                 turtle.dropUp(totalMaterials - totalMaterialsRequired)
  91.                                 if (totalMaterialsRequired > totalMaterials) then
  92.                                     error ("Unable to refill inventory from chest above.")
  93.                                 end
  94.         end
  95.  
  96. end
  97.  
  98. -- -- Basic Movements -- --
  99.  
  100. function diggingMoveUp() -- Going up...
  101.     if (turtle.detectUp()) then
  102.         turtle.digUp()
  103.     end
  104.     turtle.up()
  105. end
  106.  
  107. function diggingMoveDown() -- Going down...
  108.     if (turtle.detectDown()) then
  109.         turtle.digDown()
  110.     end
  111.     turtle.down()
  112. end
  113.  
  114. function diggingMoveForward() -- Going forward...
  115.     if (turtle.detect()) then
  116.         turtle.dig()
  117.     end
  118.     turtle.forward()
  119. end
  120.  
  121. function turnBackRow() -- Turning around at the back of one row...
  122.     turtle.turnRight()
  123.     diggingMoveForward()
  124.     turtle.turnRight()
  125. end
  126.  
  127. function turnFrontRow() -- Turning around at the front of the next...
  128.     turtle.turnLeft()
  129.     diggingMoveForward()
  130.     turtle.turnLeft()
  131. end
  132.  
  133. function turnReverse() -- 180 degree turn...
  134.     turtle.turnLeft()
  135.     turtle.turnLeft()
  136. end
  137.  
  138. -- Block Placement is always under the turtle.  If the current slot is empty, move to the next one.
  139.  
  140. function blockPlace()
  141.     while (turtle.getItemCount(currentSlot) < 1) do
  142.         currentSlot = currentSlot + 1
  143.     end
  144.  
  145.     turtle.select(currentSlot)
  146.  
  147.     if (turtle.detectDown()~=1) then
  148.         turtle.placeDown()
  149.     end
  150. end
  151.  
  152. -- -- Advanced Movements -- --
  153.  
  154.  
  155.  
  156. function planeStrip() -- Makes one row.
  157.     for spot = 1, planeSize[D], 1 do
  158.         blockPlace()
  159.         if (spot < planeSize[D]) then
  160.             diggingMoveForward()
  161.         end
  162.     end
  163. end
  164.  
  165. function turtleReturn()  -- Return to starting position after work is done.
  166.  
  167.     if (even ~= 1) then
  168.         turnReverse()
  169.         for spot = 1, planeSize[D]-1, 1 do
  170.             diggingMoveForward()
  171.  
  172.         end
  173.     end
  174.  
  175.     turtle.turnRight()
  176.  
  177.     for spot = 1, (planeSize[W]-1), 1 do
  178.         diggingMoveForward()
  179.     end
  180.  
  181.     turtle.turnLeft()
  182.     turtle.forward()
  183.     turtle.turnRight()
  184.     turtle.turnRight()
  185. end
  186.  
  187. -- ------- Main Script ------- --
  188.  
  189. print("Width: "..planeSize[W])
  190. print("Depth: "..planeSize[D])
  191.  
  192.  
  193. -- First, check the fuel.
  194. fuelUp()
  195.  
  196. -- Next, check the stock of materials to build with.
  197. takeStock()
  198.  
  199. -- Reset the selected row before beginning.
  200. currentSlot = 1
  201. turtle.select(currentSlot)
  202.  
  203. -- Move Forward 1 block
  204. turtle.forward()
  205.  
  206. -- Now, make the rows.
  207. for row = 1, planeSize[W], 1 do
  208.     planeStrip()
  209.     if (row % 2 == 0) and (row < planeSize[W]) then
  210.         turnFrontRow()
  211.     elseif (row % 2 ~= 0) and (row < planeSize[W]) then
  212.         turnBackRow()
  213.     end
  214. end
  215.  
  216. -- Finally, let's go back to the starting position.
  217. turtleReturn()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement