shiryavsky

roadBuilder

Nov 21st, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.53 KB | None | 0 0
  1. --roadBuilder
  2.  
  3. currentFacing = 0
  4. currentlySelectedSlot = 1
  5. fuelLevelToRefuelAt = 5
  6. initBlocksCount = 0
  7. minSlot = 1
  8. maxSlot = 16
  9.  
  10.  
  11.  
  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. BLOCK_FENCE = 3
  24. BLOCK_TORCH = 4
  25.  
  26. function main()
  27.   if #args > 1 then
  28.     local length = tonumber(args[1])
  29.     local roadType = args[2]  
  30.     initBlocks()    
  31.     for i=1,length,1 do
  32.       currentY = 0
  33.       print("Building part: "..i.." of "..length);
  34.       if roadType=="tun" then
  35.         tun_makePart()  
  36.       elseif roadType=="optun" then
  37.         optun_makePart()
  38.       elseif roadType=="village" then
  39.         village_makePart()
  40.       elseif roadType=="susp" then
  41.         susp_makePart()
  42.       else
  43.         print("Avail types: tun,optun,susp,village")      
  44.       end
  45.     end
  46.  
  47.   else
  48.     print("Usage: road <length> [type=tun|optun|susp|village]")
  49.   end  
  50. end
  51.  
  52. function susp_makePart()
  53.   currentY = 0
  54.   turnRight()
  55.   go("forward",true)
  56.   turnLeft()
  57.   for i=1,6 do
  58.     replaceBlock(BLOCK_WALL,"down")
  59.     go("forward",true)
  60.   end
  61.   replaceBlock(BLOCK_WALL,"down")
  62.   turnLeft()
  63.   go("forward",true)
  64.   turnLeft()
  65.   for i=1,6 do
  66.     replaceBlock(BLOCK_FENCE,"down")
  67.     go("forward",true)
  68.   end
  69.   replaceBlock(BLOCK_FENCE,"down")
  70.   turnRight()  
  71.   go("forward",true)
  72.   turnRight()  
  73.   for i=1,6 do
  74.     replaceBlock(BLOCK_WALL,"down")
  75.     go("forward",true)
  76.   end
  77.   replaceBlock(BLOCK_WALL,"down")
  78.   turnRight()
  79.   go("forward",true)
  80.   turnRight()
  81.   go("up",true)
  82.   for i=1,6 do    
  83.     if i==2 or i==6 then
  84.       replaceBlock(BLOCK_TORCH,"down")
  85.     end
  86.     if i==4 then
  87.       replaceBlock(BLOCK_FENCE,"down")
  88.     end  
  89.     go("forward",true)
  90.   end
  91.   turnLeft()
  92.   turnLeft()
  93.   go("up",true)
  94.   goX(3,"forward",true)
  95.   replaceBlock(BLOCK_FENCE,"down")
  96.   go("up",true)
  97.   replaceBlock(BLOCK_FENCE,"down")
  98.   go("up",true)
  99.   replaceBlock(BLOCK_PILLAR,"down")
  100.   turnLeft()
  101.   go("forward",true)
  102.   replaceBlock(BLOCK_PILLAR,"down")
  103.   go("forward",true)
  104.   go("forward",true)
  105.   turnLeft()
  106.   turnLeft()
  107.   go("down",true)
  108.   replaceBlock(BLOCK_WALLSTAIRS,"forward")
  109.   goX(3,"down",true)
  110.   for i=1,minimumDown,1 do
  111.     currentY  = currentY - 1
  112.     go("down",true)
  113.   end
  114.   repeat
  115.     currentY  = currentY - 1
  116.     go("down",true)
  117.   until not isEmpty("down")    
  118.   while currentY < (0 + 2) do
  119.     go("up",true)  
  120.     replaceBlock(BLOCK_PILLAR,"down")
  121.     currentY = currentY + 1
  122.   end
  123.   go("up",true)
  124.   replaceBlock(BLOCK_WALLSTAIRS,"down")
  125.   turnRight()
  126.   go("forward",true)
  127.   turnLeft()
  128.   go("forward",true)
  129.   go("down",true)
  130.   go("down",true)
  131.   turnLeft()
  132.   go("forward",true)
  133.   turnLeft()
  134.   replaceBlock(BLOCK_WALLSTAIRS,"up")
  135.   turnRight()
  136.   goX(4,"forward",true)
  137.   go("down",true)
  138.   turnRight()
  139.   goX(2,"forward",true)
  140.   turnLeft()
  141.   --next
  142. end
  143.  
  144. function village_makePart()
  145.   replaceBlock(BLOCK_WALL,"down")
  146.   go("forward",true)
  147.   go("forward",true)   
  148.   village_pillar()
  149.   go("forward",true)
  150.   go("forward",true)   
  151.   replaceBlock(BLOCK_WALL,"down")
  152.   turnLeft()
  153.   go("forward",true)   
  154.   turnLeft()
  155.   for j=1,2 do
  156.     for i=1,5,1 do
  157.       replaceBlock(BLOCK_FENCE,"down")
  158.       if i==3 then
  159.         go("up",true)  
  160.         replaceBlock(BLOCK_TORCH,"down")
  161.         go("forward",true)
  162.         go("down",true)    
  163.       else
  164.         go("forward",true) 
  165.       end  
  166.     end
  167.     turnLeft()
  168.     go("forward",true) 
  169.     if j==1 then    
  170.       go("forward",true)   
  171.       turnLeft()
  172.       go("forward",true)   
  173.     else      
  174.       turnRight()
  175.     end
  176.   end
  177. end
  178.  
  179. function optun_makePart()
  180.   for i=1,3,1 do
  181.     optun_case(false)
  182.   end
  183.   go("forward",true)   
  184.   go("down",true)  
  185.   tun_pillar()
  186.   go("up",true)
  187.   turnLeft()
  188.   turnLeft()
  189.   go("forward",true)
  190.   turnLeft()
  191.   turnLeft()
  192.   optun_case(false)
  193.   optun_case(true)
  194.   optun_case(false)
  195.   for i=1,3,1 do
  196.     optun_case(false)
  197.   end  
  198. end
  199.  
  200. function tun_makePart()
  201.   for i=1,3,1 do
  202.     tun_case(false)
  203.   end
  204.   go("forward",true)   
  205.   go("down",true)  
  206.   tun_pillar()
  207.   go("up",true)
  208.   turnLeft()
  209.   turnLeft()
  210.   go("forward",true)
  211.   turnLeft()
  212.   turnLeft()
  213.   tun_case(false)
  214.   tun_case(true)
  215.   tun_case(false)
  216.   for i=1,3,1 do
  217.     tun_case(false)
  218.   end  
  219. end
  220.  
  221. function optun_case(lamp)
  222.   turnLeft()
  223.   go("forward",true)     
  224.   turnRight()
  225.   replaceBlock(BLOCK_WALL,"down")
  226.   turnRight()
  227.   go("forward",true)
  228.   if lamp then
  229.     replaceBlock(BLOCK_LAMP,"down")
  230.   else
  231.     replaceBlock(BLOCK_WALL,"down")
  232.   end  
  233.   go("forward",true)   
  234.   replaceBlock(BLOCK_WALL,"down")
  235.   go("forward",true)     
  236.   go("up",true)
  237.   replaceBlock(BLOCK_WALL,"down")
  238.   go("forward",true)   
  239.   go("down",true)
  240.   go("down",true)
  241.   turnRight()
  242.   turnRight()
  243.   go("down",true)
  244.   go("forward",true)     
  245.   replaceBlock(BLOCK_WALLSTAIRS,"up")
  246.   turnRight()
  247.   turnRight()
  248.   go("forward",true)     
  249.   turnRight()
  250.   turnRight()
  251.   go("up",true)
  252.   go("up",true)
  253.   go("up",true)
  254.   go("forward",true)
  255.   go("forward",true)   
  256.   go("forward",true)   
  257.   go("forward",true)   
  258.   go("forward",true)   
  259.   replaceBlock(BLOCK_WALL,"down")
  260.   go("forward",true)   
  261.   turnRight()
  262.   turnRight()
  263.   go("down",true)  
  264.   go("down",true)  
  265.   go("down",true)  
  266.   go("forward",true)     
  267.   replaceBlock(BLOCK_WALLSTAIRS,"up")
  268.   turnLeft()
  269.   turnLeft()
  270.   go("forward",true)   
  271.   go("up",true)
  272.   turnRight()    
  273.   go("forward",true)   
  274.   go("up",true)
  275.   turnRight()  
  276.   go("forward",true)   
  277.   go("forward",true)   
  278.   go("forward",true)     
  279.   turnLeft()
  280. end
  281.  
  282. function tun_case(lamp)
  283.   if lamp then
  284.     replaceBlock(BLOCK_LAMP,"down")
  285.   else
  286.     replaceBlock(BLOCK_WALL,"down")
  287.   end  
  288.   turnRight()
  289.   go("forward",true)   
  290.   replaceBlock(BLOCK_WALL,"down")
  291.   go("forward",true)   
  292.  
  293.   go("up",true)
  294.   replaceBlock(BLOCK_WALL,"down")
  295.   go("forward",true)   
  296.   go("down",true)
  297.   go("down",true)
  298.   turnRight()
  299.   turnRight()
  300.   go("down",true)
  301.   go("forward",true)     
  302.   replaceBlock(BLOCK_WALLSTAIRS,"up")
  303.   turnRight()
  304.   turnRight()
  305.   go("forward",true)     
  306.   turnRight()
  307.   turnRight()
  308.   go("up",true)
  309.   go("up",true)
  310.   go("up",true)
  311.   go("forward",true)
  312.   go("up",true)
  313.   replaceBlock(BLOCK_GLASS,"down")
  314.   go("up",true)
  315.   replaceBlock(BLOCK_WALL,"down")
  316.   go("up",true)
  317.   replaceBlock(BLOCK_WALLSTAIRS,"down")
  318.   go("forward",true)   
  319.   replaceBlock(BLOCK_WALL,"down")
  320.   go("forward",true)   
  321.   replaceBlock(BLOCK_GLASS,"down")
  322.   go("forward",true)   
  323.   replaceBlock(BLOCK_WALL,"down")
  324.   go("forward",true)   
  325.   go("forward",true)   
  326.   turnRight()
  327.   turnRight()
  328.   go("down",true)  
  329.   replaceBlock(BLOCK_WALLSTAIRS,"forward")
  330.   go("down",true)  
  331.   replaceBlock(BLOCK_WALL,"forward")
  332.   go("down",true)  
  333.   replaceBlock(BLOCK_GLASS,"forward")
  334.   go("down",true)  
  335.   replaceBlock(BLOCK_WALL,"forward")
  336.   go("down",true)  
  337.   go("forward",true)   
  338.   replaceBlock(BLOCK_WALL,"forward")
  339.   turnRight()
  340.   turnRight()
  341.   go("forward",true)   
  342.   turnRight()
  343.   turnRight()
  344.   go("down",true)  
  345.   go("forward",true)   
  346.   replaceBlock(BLOCK_WALLSTAIRS,"up")
  347.   turnLeft()
  348.   turnLeft()
  349.   go("forward",true)   
  350.   go("up",true)
  351.   turnRight()    
  352.   go("forward",true)   
  353.   go("up",true)
  354.   turnRight()  
  355.   go("forward",true)   
  356.   go("forward",true)   
  357.   go("forward",true)     
  358.   turnLeft()
  359. end
  360.  
  361.  
  362. function tun_pillar()
  363.   for i=1,minimumDown,1 do
  364.     currentY  = currentY - 1
  365.     go("down",true)
  366.   end
  367.   repeat
  368.     currentY  = currentY - 1
  369.     go("down",true)
  370.   until not isEmpty("down")    
  371.   while currentY < 0 do
  372.     go("up",true)  
  373.     replaceBlock(BLOCK_PILLAR,"down")
  374.     turnLeft()
  375.     go("forward",true) 
  376.     replaceBlock(BLOCK_PILLAR,"down")
  377.     turnLeft()
  378.     turnLeft()
  379.     go("forward",true) 
  380.     go("forward",true) 
  381.     replaceBlock(BLOCK_PILLAR,"down")
  382.     turnRight()
  383.     turnRight()
  384.     go("forward",true) 
  385.     turnRight()
  386.     currentY = currentY + 1
  387.   end
  388.   turnRight()
  389.   go("forward",true)
  390.   turnLeft()
  391.   go("forward",true)
  392.   turnLeft()
  393.   replaceBlock(BLOCK_PILLAR,"down")
  394.   go("forward",true)
  395.   replaceBlock(BLOCK_PILLAR,"down")
  396.   go("forward",true)
  397.   replaceBlock(BLOCK_PILLAR,"down")
  398.   turnLeft()
  399.   go("forward",true)
  400.   go("forward",true)
  401.   turnLeft()
  402.   replaceBlock(BLOCK_PILLAR,"down")
  403.   go("forward",true)
  404.   replaceBlock(BLOCK_PILLAR,"down")
  405.   go("forward",true)
  406.   replaceBlock(BLOCK_PILLAR,"down")
  407.   turnLeft()
  408.   turnLeft()
  409.   go("forward",true)
  410.   turnRight()
  411.   go("forward",true)
  412. end
  413.  
  414.  
  415. function village_pillar()
  416.   for i=1,minimumDown,1 do
  417.     currentY  = currentY - 1
  418.     go("down",true)
  419.   end
  420.   repeat
  421.     currentY  = currentY - 1
  422.     go("down",true)
  423.   until not isEmpty("down")    
  424.   while currentY < 0 do
  425.     go("up",true)  
  426.     replaceBlock(BLOCK_PILLAR,"down")
  427.     currentY = currentY + 1
  428.   end
  429.   turnRight()
  430.   go("forward",true)   
  431.   go("down",true)
  432.   go("down",true)    
  433.   turnLeft()
  434.   go("forward",true)   
  435.   turnLeft()
  436.   go("forward",true)   
  437.   turnLeft()
  438.   replaceBlock(BLOCK_WALL,"up")
  439.   go("down",true)    
  440.   replaceBlock(BLOCK_WALLSTAIRS,"up")
  441.   turnLeft()
  442.   go("forward",true)
  443.   go("up",true)    
  444.   turnRight()
  445.   go("forward",true)
  446.   go("forward",true)
  447.   turnRight()
  448.   go("forward",true)
  449.   turnRight()
  450.   replaceBlock(BLOCK_WALL,"up")
  451.   go("down",true)    
  452.   replaceBlock(BLOCK_WALLSTAIRS,"up")
  453.   turnRight()
  454.   go("forward",true)
  455.   turnLeft()
  456.   go("forward",true)
  457.   go("up",true)    
  458.   go("up",true)    
  459.   go("up",true)    
  460.   turnLeft()
  461.   go("forward",true)   
  462.   turnRight()
  463. end
  464.  
  465.  
  466.  
  467. function replaceBlock(id, direction)
  468.   if not isEmpty(direction) then
  469.     dig(direction)
  470.   end
  471.   placeBlock(id, direction)
  472. end
  473.  
  474. function placeBlock(id, direction)
  475.   local slotNum = GetBlock(id)
  476.   local result = false
  477.   while slotNum == 0 do
  478.     selectSlot(id)
  479.     awaitFix("I have no blocks as in selected slot")
  480.     slotNum = GetBlock(id)
  481.   end
  482.   ensureFuel() 
  483.   selectSlot(slotNum)
  484.   if direction=="up" then
  485.      result = turtle.placeUp()
  486.   elseif direction=="down" then
  487.      result = turtle.placeDown()     
  488.   else
  489.      result = turtle.place()
  490.   end    
  491.   return result  
  492. end
  493.  
  494. function GetBlock(id)
  495.   if turtle.getItemCount(id) > 1 then
  496.     return id
  497.   else
  498.     selectSlot(id)
  499.     for i=minSlot,maxSlot,1 do
  500.       if turtle.getItemCount(i) > 0 then
  501.         if turtle.compareTo(i) then
  502.           return i
  503.         end
  504.       end
  505.     end
  506.   end  
  507.   return 0
  508. end
  509.  
  510.  
  511. function ___initBlocks()
  512.   if initBlocksCount==0 then
  513.     selectSlot(1)
  514.     while turtle.getItemCount(currentlySelectedSlot)~=0 do
  515.       initBlocksCount = initBlocksCount + 1
  516.       selectSlot(currentlySelectedSlot + 1)
  517.     end
  518.   end
  519.   if initBlocksCount==0 then
  520.     return false
  521.   end
  522.   minSlot = initBlocksCount + 1
  523.   maxSlot = 15
  524. end
  525.  
  526.  
  527. function initBlocks()
  528.   initBlocksCount = 4
  529.   minSlot = initBlocksCount
  530.   maxSlot = 15
  531. end
  532.  
  533. function isEmpty(side)
  534.     local result = false
  535.     if side=="up" then
  536.       result = not turtle.detectUp()
  537.     elseif side=="down" then
  538.       result = not turtle.detectDown()
  539.     else
  540.       result = not turtle.detect()
  541.     end
  542.     return result
  543. end
  544.  
  545. --select slot
  546. function selectSlot(x)
  547.   if x<1 then
  548.     x = 1
  549.   elseif x > 16 then
  550.     x = 16
  551.   end
  552.   turtle.select(x)
  553.   currentlySelectedSlot = x
  554. end
  555.  
  556.  
  557. --Check fuel, refueling from inventory special slot or not
  558. function ensureFuel()
  559.   -- Determine whether a refuel is required
  560.   local fuelLevel = turtle.getFuelLevel()
  561.   if (fuelLevel ~= "unlimited") then
  562.     if (fuelLevel < fuelLevelToRefuelAt) then
  563.       -- Need to refuel
  564.       selectSlot(16)
  565.       local fuelItems = turtle.getItemCount(16)
  566.       -- Do we need to impact the emergency fuel to continue? (always  
  567.       -- keep one fuel item in slot 16)
  568.       if (fuelItems == 0) then
  569.         awaitFix("I want more fuel!")
  570.       elseif (fuelItems == 1) then
  571.         --load fuel from another slot
  572.     local fueled = false
  573.     local fromSlot = initBlocksCount + 1
  574.         for i=fromSlot,15,1 do
  575.           selectSlot(i)
  576.       if (turtle.compareTo(16)) then
  577.         print("Refueling from not fuel slot: "+i)
  578.         turtle.refuel(1)
  579.         turtle.transferTo(16)          
  580.         fueled = true
  581.           end
  582.         end
  583.         if not fueled then
  584.           print("completely out of fuel!")
  585.         end
  586.       else --fuelItems > 1
  587.         print("Time to refueling!")
  588.         turtle.refuel(1)
  589.       end
  590.     end
  591.   end
  592. end
  593.  
  594.  
  595. --turns
  596. function turnFacing(facing)
  597.   while facing ~= currentFacing do
  598.     turnRight()
  599.   end
  600. end
  601.  
  602. --turns
  603. function turnLeft()
  604.   ensureFuel()
  605.   turtle.turnLeft()
  606.   currentFacing = currentFacing - 1
  607.   if currentFacing < 0 then
  608.     currentFacing = 3
  609.   end
  610. end
  611.  
  612. --turns
  613. function turnRight()
  614.   ensureFuel()
  615.   turtle.turnRight()
  616.   currentFacing = currentFacing + 1
  617.   if currentFacing >= 4 then
  618.     currentFacing = 0
  619.   end
  620. end
  621.  
  622. -- BOOL dig direction with retries
  623. function dig(direction)
  624.   local result = false
  625.   local tries = 0
  626.   selectSlot(1)
  627.   while (not result) and (tries < 10) do
  628.     ensureFuel()   
  629.     if direction=="up" then
  630.         result = turtle.digUp()
  631.     elseif direction=="down" then
  632.         result = turtle.digDown()    
  633.     else
  634.         result = turtle.dig()
  635.     end  
  636.     tries = tries + 1
  637.   end
  638.   if not result then
  639.     print ("can't dig in "..direction)
  640.   end
  641.   return result
  642. end
  643.  
  644. function goX(blocks,direction,allowDig)
  645.   for i=1,blocks do
  646.     go(direction,allowDig)
  647.   end
  648. end
  649.  
  650. --go direction with breaking blocks and refueling
  651. function go(direction, allowDig)
  652.     if direction=="right" then
  653.       turnRight()
  654.       direction="forward"
  655.     end
  656.     if direction=="left" then
  657.       turnLeft()
  658.       direction="forward"
  659.     end
  660.     if direction=="forward" then      
  661.        while turtle.detect() do
  662.          if allowDig then
  663.            ensureFuel()
  664.            turtle.dig()
  665.          else
  666.            awaitFix("can't forward")
  667.          end
  668.        end
  669.        
  670.        while not turtle.forward() do
  671.          ensureFuel()
  672.          turtle.dig()      
  673.        end
  674.     end
  675.     if direction=="up" then
  676.        while turtle.detectUp() do
  677.          if allowDig then
  678.            ensureFuel()
  679.            turtle.digUp()
  680.          else
  681.            awaitFix("can't up")
  682.          end
  683.        end     
  684.        while not turtle.up() do
  685.            ensureFuel()
  686.            turtle.digUp()      
  687.        end
  688.     end
  689.     if direction=="down" then
  690.        while turtle.detectDown() do
  691.          if allowDig then
  692.            ensureFuel()
  693.            turtle.digDown()
  694.          else
  695.            awaitFix("can't down")
  696.          end
  697.        end     
  698.         while not turtle.down() do
  699.            ensureFuel()
  700.            turtle.digDown()    
  701.        end
  702.     end
  703. end
  704.  
  705. --wait for user action
  706. function awaitFix(message)
  707.     print("I have problem: " .. message)
  708.     print("Please fix it and press Enter to continue")
  709.     io.read()
  710. end
  711.  
  712.  
  713.  
  714. main() --execute ALL
Advertisement
Add Comment
Please, Sign In to add comment