Negasus

Untitled

Nov 24th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --roadBuilder
  2.  
  3. currentFacing = 0
  4. currentlySelectedSlot = 1
  5. fuelLevelToRefuelAt = 5
  6. initBlocksCount = 0
  7. minSlot = 1
  8. maxSlot = 16
  9.  
  10.  
  11. currentY = 0
  12.  
  13. args = { ... }
  14.  
  15. minimumDown = 2
  16.  
  17. BLOCK_PILLAR = 1
  18. BLOCK_GLASS = 3
  19. BLOCK_WALL = 1
  20. BLOCK_WALLSTAIRS = 2
  21. BLOCK_LAMP = 4
  22.  
  23. function main()
  24.   if #args > 0 then
  25.     local length = tonumber(args[1])
  26.     initBlocks()    
  27.     for i=1,length,1 do
  28.       makePart()  
  29.     end
  30.   else
  31.     print("Usage: road <length>")
  32.   end  
  33. end
  34.  
  35. function makePart()
  36.   for i=1,3,1 do
  37.     case(false)
  38.   end
  39.   go("forward",true)   
  40.   go("down",true)  
  41.   pillar()
  42.   go("up",true)
  43.   turnLeft()
  44.   turnLeft()
  45.   go("forward",true)
  46.   turnLeft()
  47.   turnLeft()
  48.   case(false)
  49.   case(true)
  50.   case(false)
  51.   for i=1,3,1 do
  52.     case(false)
  53.   end  
  54. end
  55.  
  56. function case(lamp)
  57.   if lamp then
  58.     replaceBlock(BLOCK_LAMP,"down")
  59.   else
  60.     replaceBlock(BLOCK_WALL,"down")
  61.   end  
  62.   turnRight()
  63.   go("forward",true)   
  64.   replaceBlock(BLOCK_WALL,"down")
  65.   go("forward",true)   
  66.  
  67.   go("up",true)
  68.   replaceBlock(BLOCK_WALL,"down")
  69.   go("forward",true)   
  70.   go("down",true)
  71.   go("down",true)
  72.   turnRight()
  73.   turnRight()
  74.   go("down",true)
  75.   go("forward",true)     
  76.   replaceBlock(BLOCK_WALLSTAIRS,"up")
  77.   turnRight()
  78.   turnRight()
  79.   go("forward",true)     
  80.   turnRight()
  81.   turnRight()
  82.   go("up",true)
  83.   go("up",true)
  84.   go("up",true)
  85.   go("forward",true)
  86.   go("up",true)
  87.   replaceBlock(BLOCK_GLASS,"down")
  88.   go("up",true)
  89.   replaceBlock(BLOCK_WALL,"down")
  90.   go("up",true)
  91.   replaceBlock(BLOCK_WALLSTAIRS,"down")
  92.   go("forward",true)   
  93.   replaceBlock(BLOCK_WALL,"down")
  94.   go("forward",true)   
  95.   replaceBlock(BLOCK_GLASS,"down")
  96.   go("forward",true)   
  97.   replaceBlock(BLOCK_WALL,"down")
  98.   go("forward",true)   
  99.   go("forward",true)   
  100.   turnRight()
  101.   turnRight()
  102.   go("down",true)  
  103.   replaceBlock(BLOCK_WALLSTAIRS,"forward")
  104.   go("down",true)  
  105.   replaceBlock(BLOCK_WALL,"forward")
  106.   go("down",true)  
  107.   replaceBlock(BLOCK_GLASS,"forward")
  108.   go("down",true)  
  109.   replaceBlock(BLOCK_WALL,"forward")
  110.   go("down",true)  
  111.   go("forward",true)   
  112.   replaceBlock(BLOCK_WALL,"forward")
  113.   turnRight()
  114.   turnRight()
  115.   go("forward",true)   
  116.   turnRight()
  117.   turnRight()
  118.   go("down",true)  
  119.   go("forward",true)   
  120.   replaceBlock(BLOCK_WALLSTAIRS,"up")
  121.   turnLeft()
  122.   turnLeft()
  123.   go("forward",true)   
  124.   go("up",true)
  125.   turnRight()    
  126.   go("forward",true)   
  127.   go("up",true)
  128.   turnRight()  
  129.   go("forward",true)   
  130.   go("forward",true)   
  131.   go("forward",true)     
  132.   turnLeft()
  133. end
  134.  
  135. function pillar()
  136.   for i=1,minimumDown,1 do
  137.     currentY  = currentY - 1
  138.     go("down",true)
  139.   end
  140.   repeat
  141.     currentY  = currentY - 1
  142.     go("down",true)
  143.   until not isEmpty("down")    
  144.   while currentY < 0 do
  145.     go("up",true)  
  146.     replaceBlock(BLOCK_PILLAR,"down")
  147.     turnLeft()
  148.     go("forward",true) 
  149.     replaceBlock(BLOCK_PILLAR,"down")
  150.     turnLeft()
  151.     turnLeft()
  152.     go("forward",true) 
  153.     go("forward",true) 
  154.     replaceBlock(BLOCK_PILLAR,"down")
  155.     turnRight()
  156.     turnRight()
  157.     go("forward",true) 
  158.     turnRight()
  159.     currentY = currentY + 1
  160.   end
  161.   turnRight()
  162.   go("forward",true)
  163.   turnLeft()
  164.   go("forward",true)
  165.   turnLeft()
  166.   replaceBlock(BLOCK_PILLAR,"down")
  167.   go("forward",true)
  168.   replaceBlock(BLOCK_PILLAR,"down")
  169.   go("forward",true)
  170.   replaceBlock(BLOCK_PILLAR,"down")
  171.   turnLeft()
  172.   go("forward",true)
  173.   go("forward",true)
  174.   turnLeft()
  175.   replaceBlock(BLOCK_PILLAR,"down")
  176.   go("forward",true)
  177.   replaceBlock(BLOCK_PILLAR,"down")
  178.   go("forward",true)
  179.   replaceBlock(BLOCK_PILLAR,"down")
  180.   turnLeft()
  181.   turnLeft()
  182.   go("forward",true)
  183.   turnRight()
  184.   go("forward",true)
  185. end
  186.  
  187.  
  188.  
  189. function replaceBlock(id, direction)
  190.   if not isEmpty(direction) then
  191.     dig(direction)
  192.   end
  193.   placeBlock(id, direction)
  194. end
  195.  
  196. function placeBlock(id, direction)
  197.   local slotNum = GetBlock(id)
  198.   local result = false
  199.   while slotNum == 0 do
  200.     selectSlot(id)
  201.     awaitFix("I have no blocks as in selected slot")
  202.     slotNum = GetBlock(id)
  203.   end
  204.   ensureFuel() 
  205.   selectSlot(slotNum)
  206.   if direction=="up" then
  207.      result = turtle.placeUp()
  208.   elseif direction=="down" then
  209.      result = turtle.placeDown()     
  210.   else
  211.      result = turtle.place()
  212.   end    
  213.   return result  
  214. end
  215.  
  216. function GetBlock(id)
  217.   if turtle.getItemCount(id) > 1 then
  218.     return id
  219.   else
  220.     selectSlot(id)
  221.     for i=minSlot,maxSlot,1 do
  222.       if turtle.getItemCount(i) > 0 then
  223.         if turtle.compareTo(i) then
  224.           return i
  225.         end
  226.       end
  227.     end
  228.   end  
  229.   return 0
  230. end
  231.  
  232.  
  233. function ___initBlocks()
  234.   if initBlocksCount==0 then
  235.     selectSlot(1)
  236.     while turtle.getItemCount(currentlySelectedSlot)~=0 do
  237.       initBlocksCount = initBlocksCount + 1
  238.       selectSlot(currentlySelectedSlot + 1)
  239.     end
  240.   end
  241.   if initBlocksCount==0 then
  242.     return false
  243.   end
  244.   minSlot = initBlocksCount + 1
  245.   maxSlot = 15
  246. end
  247.  
  248.  
  249. function initBlocks()
  250.   initBlocksCount = 4
  251.   minSlot = initBlocksCount
  252.   maxSlot = 15
  253. end
  254.  
  255. function isEmpty(side)
  256.     local result = false
  257.     if side=="up" then
  258.       result = not turtle.detectUp()
  259.     elseif side=="down" then
  260.       result = not turtle.detectDown()
  261.     else
  262.       result = not turtle.detect()
  263.     end
  264.     return result
  265. end
  266.  
  267. --select slot
  268. function selectSlot(x)
  269.   if x<1 then
  270.     x = 1
  271.   elseif x > 16 then
  272.     x = 16
  273.   end
  274.   turtle.select(x)
  275.   currentlySelectedSlot = x
  276. end
  277.  
  278.  
  279. --Check fuel, refueling from inventory special slot or not
  280. function ensureFuel()
  281.   -- Determine whether a refuel is required
  282.   local fuelLevel = turtle.getFuelLevel()
  283.   if (fuelLevel ~= "unlimited") then
  284.     if (fuelLevel < fuelLevelToRefuelAt) then
  285.       -- Need to refuel
  286.       selectSlot(16)
  287.       local fuelItems = turtle.getItemCount(16)
  288.       -- Do we need to impact the emergency fuel to continue? (always  
  289.       -- keep one fuel item in slot 16)
  290.       if (fuelItems == 0) then
  291.         awaitFix("I want more fuel!")
  292.       elseif (fuelItems == 1) then
  293.         --load fuel from another slot
  294.     local fueled = false
  295.     local fromSlot = initBlocksCount + 1
  296.         for i=fromSlot,15,1 do
  297.           selectSlot(i)
  298.       if (turtle.compareTo(16)) then
  299.         print("Refueling from not fuel slot: "+i)
  300.         turtle.refuel(1)
  301.         turtle.transferTo(16)          
  302.         fueled = true
  303.           end
  304.         end
  305.         if not fueled then
  306.           print("completely out of fuel!")
  307.         end
  308.       else --fuelItems > 1
  309.         print("Time to refueling!")
  310.         turtle.refuel(1)
  311.       end
  312.     end
  313.   end
  314. end
  315.  
  316.  
  317. --turns
  318. function turnFacing(facing)
  319.   while facing ~= currentFacing do
  320.     turnRight()
  321.   end
  322. end
  323.  
  324. --turns
  325. function turnLeft()
  326.   ensureFuel()
  327.   turtle.turnLeft()
  328.   currentFacing = currentFacing - 1
  329.   if currentFacing < 0 then
  330.     currentFacing = 3
  331.   end
  332. end
  333.  
  334. --turns
  335. function turnRight()
  336.   ensureFuel()
  337.   turtle.turnRight()
  338.   currentFacing = currentFacing + 1
  339.   if currentFacing >= 4 then
  340.     currentFacing = 0
  341.   end
  342. end
  343.  
  344. -- BOOL dig direction with retries
  345. function dig(direction)
  346.   local result = false
  347.   local tries = 0
  348.   selectSlot(1)
  349.   while (not result) and (tries < 10) do
  350.     ensureFuel()   
  351.     if direction=="up" then
  352.         result = turtle.digUp()
  353.     elseif direction=="down" then
  354.         result = turtle.digDown()    
  355.     else
  356.         result = turtle.dig()
  357.     end  
  358.     tries = tries + 1
  359.   end
  360.   if not result then
  361.     print ("can't dig in "..direction)
  362.   end
  363.   return result
  364. end
  365.  
  366. --go direction with breaking blocks and refueling
  367. function go(direction, allowDig)
  368.     if direction=="right" then
  369.       turnRight()
  370.       direction="forward"
  371.     end
  372.     if direction=="left" then
  373.       turnLeft()
  374.       direction="forward"
  375.     end
  376.     if direction=="forward" then      
  377.        while turtle.detect() do
  378.          if allowDig then
  379.            ensureFuel()
  380.            turtle.dig()
  381.          else
  382.            awaitFix("can't forward")
  383.          end
  384.        end
  385.        
  386.        while not turtle.forward() do
  387.          ensureFuel()
  388.          turtle.dig()      
  389.        end
  390.     end
  391.     if direction=="up" then
  392.        while turtle.detectUp() do
  393.          if allowDig then
  394.            ensureFuel()
  395.            turtle.digUp()
  396.          else
  397.            awaitFix("can't up")
  398.          end
  399.        end     
  400.        while not turtle.up() do
  401.            ensureFuel()
  402.            turtle.digUp()      
  403.        end
  404.     end
  405.     if direction=="down" then
  406.        while turtle.detectDown() do
  407.          if allowDig then
  408.            ensureFuel()
  409.            turtle.digDown()
  410.          else
  411.            awaitFix("can't down")
  412.          end
  413.        end     
  414.         while not turtle.down() do
  415.            ensureFuel()
  416.            turtle.digDown()    
  417.        end
  418.     end
  419. end
  420.  
  421. --wait for user action
  422. function awaitFix(message)
  423.     print("I have problem: " .. message)
  424.     print("Please fix it and press Enter to continue")
  425.     io.read()
  426. end
  427.  
  428.  
  429.  
  430. main() --execute ALL
Advertisement
Add Comment
Please, Sign In to add comment