Advertisement
HandieAndy

Rail Builder

Nov 13th, 2016
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.58 KB | None | 0 0
  1. print("Length?")
  2. local length = tonumber(read())
  3.  
  4. local segments = length/8
  5.  
  6. local brickItem = "MineFactoryReloaded:brick"
  7. local concreteItem = "Railcraft:cube"
  8. local railItem = "minecraft:rail"
  9. local lanternItem = "Railcraft:lantern.stone"
  10.  
  11. function selectItem(name)
  12.   local thisSlot = turtle.getItemDetail()
  13.   if (thisSlot ~= nil) then
  14.     if (thisSlot.name == name) then
  15.       return
  16.      end
  17.   end
  18.   for i=1,16 do
  19.     local data = turtle.getItemDetail(i)
  20.     if (data ~= nil) then
  21.       if (data.name == name) then
  22.         turtle.select(i)
  23.         return
  24.       end
  25.     end
  26.   end
  27.   return false
  28. end
  29.  
  30. function getItemCount(name)
  31.   local sum = 0
  32.   for i=1,16 do
  33.     local data = turtle.getItemDetail(i)
  34.     if (data ~= nil) then
  35.       if (data.name == name) then
  36.         sum = sum + data.count
  37.       end
  38.     end
  39.   end
  40.   return sum
  41. end
  42.  
  43. function clearInventory()
  44.     for i=1,16 do
  45.         local slot = turtle.getItemDetail(i)
  46.         if (slot ~= nil) then
  47.             if (not(slot.name == brickItem or slot.name == concreteItem or slot.name == railItem or slot.name == lanternItem)) then
  48.                 turtle.select(i)
  49.                 if (slot.name == "minecraft:log") then
  50.                     turtle.refuel()
  51.                 else
  52.                     turtle.dropDown()
  53.                 end
  54.             end
  55.         end
  56.     end
  57. end
  58.  
  59. function checkItem(name,count)
  60.   local c = getItemCount(name)
  61.   while (c < count) do
  62.     print("Add more ["..name.."] and press enter.")
  63.     read()
  64.     c = getItemCount(name)
  65.   end
  66. end
  67.  
  68. function checkFuel()
  69.     local fuelLevel = turtle.getFuelLevel()
  70.     local fuelEstimate = (length * 5) + (segments * 32)
  71.     print("Fuel Level: "..fuelLevel.." Estimated needed: "..fuelEstimate)
  72.     if (fuelLevel < fuelEstimate) then
  73.         print("Not enough fuel. Refuel and try again.")
  74.         os.sleep(2)
  75.         shell.exit()
  76.     end
  77. end
  78.  
  79. function buildColumn()
  80.   local depth = 0
  81.   while(true) do
  82.     local success, block = turtle.inspectDown()
  83.     if (success and (block.name == "minecraft:grass" or
  84.         block.name == "minecraft:dirt" or
  85.         block.name == "minecraft:stone" or
  86.         block.name == "minecraft:sand" or
  87.         block.name == "minecraft:sandstone" or
  88.         block.name == "minecraft:gravel" or
  89.         block.name == "minecraft:bedrock")) then
  90.         break
  91.     else
  92.         if (success) then
  93.             turtle.digDown()
  94.         end
  95.         turtle.down()
  96.         depth = depth + 1
  97.     end
  98.   end
  99.   for i=1,depth do
  100.     if (i == depth-1) then
  101.          turtle.turnRight()
  102.          selectItem(concreteItem)
  103.          turtle.place()
  104.          turtle.turnLeft()
  105.          turtle.turnLeft()
  106.          selectItem(concreteItem)
  107.          turtle.place()
  108.          turtle.turnRight()
  109.        end
  110.     turtle.up()
  111.     selectItem(concreteItem)
  112.     turtle.placeDown()
  113.   end
  114.   turtle.up()
  115.   selectItem(lanternItem)
  116.   turtle.placeDown()
  117.   turtle.forward()
  118.   turtle.down()
  119. end
  120.  
  121. function buildRails()
  122.     for i=1,8 do
  123.         turtle.down()
  124.         --Place bricks
  125.         turtle.turnRight()
  126.         if (turtle.detect()) then
  127.             turtle.dig()
  128.         end
  129.         selectItem(brickItem)
  130.         turtle.place()
  131.         turtle.up()
  132.         if (turtle.detect()) then
  133.             turtle.dig()
  134.         end
  135.         selectItem(railItem)
  136.         turtle.place()
  137.         turtle.turnLeft()
  138.         turtle.turnLeft()
  139.         turtle.down()
  140.         if (turtle.detect()) then
  141.             turtle.dig()
  142.         end
  143.         selectItem(brickItem)
  144.         turtle.place()
  145.         turtle.up()
  146.         if (turtle.detect()) then
  147.             turtle.dig()
  148.         end
  149.         selectItem(railItem)
  150.         turtle.place()
  151.         turtle.turnRight()
  152.         if (i ~= 8) then
  153.             turtle.forward()
  154.         end
  155.     end
  156. end
  157.  
  158. checkFuel()
  159.  
  160. --Official Loop
  161. for i=1,segments do
  162.   checkItem(brickItem, 16)
  163.   checkItem(railItem, 16)
  164.   checkItem(concreteItem, 64)
  165.   checkItem(lanternItem, 1)
  166.   print("Constructing segment "..i)
  167.   buildRails()
  168.   buildColumn()
  169.   clearInventory()
  170. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement