Advertisement
VADemon

Railcraft tank builder

Jul 2nd, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.50 KB | None | 0 0
  1. tankSize = 3*3
  2. tankHeight = 4
  3.  
  4. function refill(targetSlot, targetItems, fromSlot, toSlot)
  5.     if turtle.getItemCount( targetSlot )<targetItems then
  6.         for i=fromSlot, toSlot do
  7.             if turtle.getItemCount ( i )~=0 then
  8.                 turtle.select( i )
  9.                 turtle.transferTo(targetSlot, turtle.getItemCount ( i ))
  10.            
  11.                 if turtle.getItemCount( targetSlot )>=targetItems then
  12.                     turtle.select(targetSlot)
  13.                     return true
  14.                 end
  15.             end
  16.         end
  17.     else
  18.         return true
  19.     end
  20.    
  21.     turtle.select(targetSlot)
  22.     return false --if not enough items in the targetSlot
  23. end
  24.  
  25. function countItemsInSlots(fromSlot, toSlot)
  26.     local count = 0
  27.     for i = fromSlot, toSlot do
  28.         count = count + turtle.getItemCount(i)
  29.     end
  30.    
  31.     return count
  32. end
  33.  
  34. function enoughIronTankWallBlocks()
  35.     if countItemsInSlots(1,4) < frameBlocks + lidBlocks then
  36.         print("I don't have enough 'Iron Tank Wall' blocks!")
  37.         print("I need ".. (frameBlocks + lidBlocks) - countItemsInSlots(1,4) .." more blocks!")
  38.         return false
  39.     end
  40.     return true
  41. end
  42.  
  43. function enoughIronTankGaugeBlocks()
  44.     if countItemsInSlots(5,8) < sideBlocks then
  45.         print("I don't have enough 'Iron Tank Gauge' blocks!")
  46.         print("I need ".. (sideBlocks) - countItemsInSlots(5,8) .." more blocks!")
  47.         return false
  48.     end
  49.     return true
  50. end
  51.  
  52. function enoughBlocks()
  53.     return (enoughIronTankWallBlocks() and enoughIronTankGaugeBlocks())
  54. end
  55.  
  56. function OddOrEven(num, odd, even)
  57.     if num%2 == 1 then
  58.         return odd
  59.     end
  60.     return even
  61. end
  62.  
  63. function even(num)
  64.     return num%2 == 0
  65. end
  66.  
  67. tankSize = math.sqrt(tankSize)
  68.  
  69. function calculateSideBlocks()
  70.     return (tankSize - 2) * (tankHeight - 2) * 4
  71. end
  72.  
  73. function calculateLids()
  74.     return (tankSize - 2)^2 * 2
  75. end
  76.  
  77. function calculateFrame()
  78.     return (tankHeight - 2) * 4 + (tankSize - 2) * 8 + 8
  79. end
  80.  
  81. frameBlocks = calculateFrame()
  82. sideBlocks = calculateSideBlocks()
  83. lidBlocks = calculateLids()
  84.  
  85. print("Tank size: "..tankSize.." | Tank height: "..tankHeight)
  86. print("Frame: " ..frameBlocks.. " blocks")
  87. print("Side: " ..sideBlocks.. " blocks")
  88. print("Lids: " ..lidBlocks.. " blocks")
  89. print("Total volume: ".. tankSize * tankHeight * tankSize * 16 .."mB")
  90. sleep(1)
  91. print("\nPut ".. frameBlocks + lidBlocks .." 'Iron Tank Wall' blocks in slots 1-4")
  92. print("Put ".. sideBlocks .." 'Iron Tank Gauge' blocks in slots 5-8")
  93.  
  94. print("\nPress Enter to continue")
  95. while true do
  96.     local event, key = os.pullEventRaw("key")
  97.     if key == 28 then -- Enter
  98.         break
  99.     elseif key == 211 or key == 14 then -- Delete
  100.         print("Exit...")
  101.         error()
  102.     end
  103. end
  104.  
  105. enoughBlocks()
  106. while true do --true is necessary
  107.     print("\nPress Enter to count blocks again")
  108.     local event, key = os.pullEvent("key")
  109.     if key == 28 then -- Enter
  110.         if enoughBlocks() then break end
  111.     elseif key == 211 or key == 14 then -- Delete
  112.         print("Exit...")
  113.         error()
  114.     end
  115. end
  116.  
  117. print("\n\n\n\n\n\n\n\n\n\nI have enough blocks, starting building the "..tankSize.."x"..tankHeight.."x"..tankSize.." tank!")
  118.  
  119. function buildRow(buildDirection, moveDirection)
  120.     for z = 1, tankSize do
  121.         turtle[buildDirection]()
  122.         if z~= tankSize then turtle[moveDirection]() end --dont outbreak from border
  123.     end
  124. end
  125.  
  126. function buildSurface(slot)
  127.     turtle.select(slot)
  128.    
  129.     for x = 1, tankSize do
  130.         buildRow("placeDown", OddOrEven(x, "forward", "back")) --switch movement direction every iteration
  131.         if x ~= tankSize then
  132.             turtle.turnRight()
  133.             turtle.forward()
  134.             turtle.turnLeft()
  135.             refill(slot, 32, slot + 1, slot + 3)
  136.         end
  137.     end
  138. end
  139.  
  140. function buildColumn(slot, buildDirection, moveDirection)
  141.     turtle.select(slot)
  142.    
  143.     for y = 2, tankHeight - 2 do --1 block is already below us, the top will be built later
  144.         turtle[moveDirection]()
  145.         turtle[buildDirection]()
  146.         refill(slot, 32, slot + 1, slot + 3)
  147.     end
  148. end
  149.  
  150. function buildWall(stay)
  151.     buildColumn(1, "placeDown", "up")
  152.     turtle.turnRight()
  153.  
  154.     for z = 2, tankSize - 1 do
  155.         turtle.turnLeft()
  156.         turtle.back()
  157.         turtle.place()
  158.         turtle.turnRight()
  159.         buildColumn(5, OddOrEven(z, "placeDown", "placeUp"), OddOrEven(z, "up", "down"))
  160.     end
  161.     if not stay then --nil or false
  162.         --place a block where the turtle currently is and move to block on the right
  163.         turtle.turnLeft()
  164.         turtle.back()
  165.         turtle.place()
  166.         turtle.turnRight()
  167.     end
  168. end
  169.  
  170. function buildTank()
  171.     buildSurface(1)
  172.    
  173.     buildWall()
  174.     buildWall()
  175.     buildWall()
  176.    
  177.     buildWall(true)
  178.     --go to the top
  179.     turtle.back()
  180.     turtle.place()
  181.     for i = 2, tankHeight do
  182.         turtle.up()
  183.     end
  184.     turtle.forward()
  185.     turtle.turnRight()
  186.     turtle.forward()
  187.     turtle.turnRight()
  188.    
  189.     buildSurface(1)
  190. end
  191.  
  192. buildTank()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement