Guest User

Solar Panel Placer V1.1

a guest
Jul 7th, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.12 KB | None | 0 0
  1. --  
  2. --              Solar Panel Placer v1.1 by Joseph Jones
  3. --
  4. --              Instructions found at: http://www.computercraft.info/forums2/index.php?/topic/13942-solar-panel-placer/
  5. --
  6. --      Needs solar panels. Uses slots 1-8
  7. --      Needs cables.       Uses slots 9-15
  8. --      Needs fuel.     Uses slot 16
  9. --
  10. --              ChangeLog:
  11. --                      July 6, 2013: [v1.0] Finished initial release.
  12. --          July 7, 2013: [v1.1] More efficient when placing flowers.
  13. --
  14.  
  15. local solarPanelSlot
  16. local cableSlot
  17.  
  18. function checkFuel()
  19.     if(turtle.getFuelLevel() < 2) then
  20.         turtle.select(16)
  21.         if(turtle.refuel(1)==false) then
  22.             print("Turtle needs more fuel in slot 16. Press enter to continue")
  23.             io.read()
  24.             checkFuel()
  25.         end
  26.     end
  27. end
  28.  
  29. function move()
  30.     checkFuel()
  31.     while(turtle.forward()==false)do turtle.dig() end
  32. end
  33.  
  34. function moveBack()
  35.     checkFuel()
  36.     while(turtle.back()==false)do end
  37. end
  38.  
  39. function moveUp()
  40.     checkFuel()
  41.     while(turtle.up()==false)do turtle.digUp() end
  42. end
  43.  
  44. function moveDown()
  45.     checkFuel()
  46.     while(turtle.down()==false)do turtle.digDown() end
  47. end
  48.  
  49. --This places a solar panel in a specified direction
  50. --Direction: 2 is down, 1 is up, 0 is in front
  51. function placeSolarPanel(direction)
  52.     local lastSlot = solarPanelSlot
  53.  
  54.     while(turtle.getItemCount(solarPanelSlot)==0)do
  55.         solarPanelSlot = solarPanelSlot+1
  56.         if(solarPanelSlot == 9) then
  57.             solarPanelSlot = 1
  58.         end
  59.         if(solarPanelSlot == lastSlot) then
  60.             print("Turtle needs more solar panels in slots 1-8.")
  61.             print("Press enter to continue")
  62.             io.read()
  63.         end
  64.     end
  65.    
  66.     turtle.select(solarPanelSlot)
  67.     if direction==1 then
  68.         while(turtle.placeUp()==false) do turtle.digUp() end
  69.     else if direction ==2 then
  70.         while(turtle.placeDown()==false) do turtle.digDown() end
  71.     else
  72.         while(turtle.place()==false) do turtle.dig() end
  73.     end end
  74. end
  75.  
  76. --This places a cable in a specified direction
  77. --Direction: 2 is down, 0 is in front
  78. function placeCable(direction)
  79.     local lastSlot = cableSlot
  80.    
  81.     while(turtle.getItemCount(cableSlot)==0)do
  82.         cableSlot = cableSlot+1
  83.         if(cableSlot == 16) then
  84.             cableSlot = 9
  85.         end
  86.         if(cableSlot == lastSlot) then
  87.             print("Turtle needs more cables in slots 9-15.")
  88.             print("Press enter to continue")
  89.             io.read()
  90.         end
  91.     end
  92.    
  93.     turtle.select(cableSlot)
  94.     if(direction==2) then
  95.         while(turtle.placeDown()==false) do turtle.digDown() end
  96.     else
  97.         while(turtle.place()==false) do turtle.dig() end
  98.     end
  99.    
  100. end
  101.  
  102. function clearRow(length, digUp)
  103.     for tempLength = length, 1, -1 do
  104.         move()
  105.         if digUp then
  106.             turtle.digUp()
  107.         end
  108.     end
  109. end
  110.  
  111.  
  112. function placeSolarRow(length)
  113.     clearRow(length)
  114.     for tempLength = length, 1, -1 do
  115.         moveBack()
  116.         placeSolarPanel(0)
  117.     end
  118. end
  119.  
  120. function placeCableAndSolarRow(length)
  121.     clearRow(length,true)
  122.     for tempLength = length, 1, -1 do
  123.         placeSolarPanel(1)
  124.         moveBack()
  125.         placeCable(0)
  126.     end
  127. end
  128.  
  129. function goToNextRow()
  130.     turtle.turnRight()
  131.     move()
  132.     turtle.turnLeft()
  133. end
  134.  
  135. function goUpToNextRow()
  136.     goToNextRow()
  137.     moveUp()
  138.     moveUp()
  139. end
  140.  
  141. function goDownToNextRow()
  142.     goToNextRow()
  143.     moveDown()
  144.     moveDown()
  145. end
  146.  
  147. function placeGable(length, height)
  148.     state = true
  149.    
  150.     --This handles the left half of the gable
  151.     for tl = height-1, 1, -1 do
  152.         if state then
  153.             if(tl~=height-1) then goUpToNextRow() end
  154.             placeSolarRow(length)
  155.         else
  156.             goToNextRow()
  157.             placeCableAndSolarRow(length)
  158.         end
  159.         state = state==false
  160.     end
  161.    
  162.     --This handles the middle row of the gable
  163.     --Different depending on if the height is even or odd.
  164.     goToNextRow()
  165.     if(height%2==1) then moveUp() end
  166.     placeCableAndSolarRow(length)
  167.     goToNextRow()
  168.     if(height%2==1) then moveDown() end
  169.     state = state==false
  170.    
  171.     --This handles the right half of the gable
  172.     for tl = height-1, 1, -1 do
  173.         if state then
  174.             if(tl~=height-1) then goToNextRow() end
  175.             placeSolarRow(length)
  176.         else
  177.             if(tl~=height-1) then goDownToNextRow() end
  178.             placeCableAndSolarRow(length)
  179.         end
  180.         state = state==false
  181.     end
  182.    
  183. end
  184.  
  185. function placeLastFlowerRows()
  186.     moveUp()
  187.     placeSolarPanel(2)
  188.     move()
  189.     placeCable(2)
  190.     move()
  191.     placeSolarPanel(2)
  192.     moveBack()
  193.     turtle.turnRight()
  194.     move()
  195.     placeSolarPanel(2)
  196.     moveBack()
  197.     moveBack()
  198.     placeSolarPanel(2)
  199.     move()
  200.     moveUp()
  201.     placeSolarPanel(2)
  202. end
  203.  
  204. function placeFlower(height)
  205.     if(height==2) then
  206.         placeLastFlowerRows()
  207.         return
  208.     end
  209.    
  210.     for section=4, 1, -1 do
  211.        
  212.         move()
  213.         placeSolarPanel(2)
  214.         turtle.turnLeft()
  215.         move()
  216.         placeCable(2)
  217.         turtle.turnRight()
  218.         move()
  219.         placeSolarPanel(2)
  220.         moveBack()
  221.         turtle.turnLeft()
  222.        
  223.         for spib = height - 3, 1, -1 do
  224.             move()
  225.             placeSolarPanel(2)
  226.             turtle.turnLeft()
  227.             move()
  228.             placeCable(2)
  229.             move()
  230.             placeCable(2)
  231.             if(section~= 1 or spib ~= 1) then
  232.                 moveBack()
  233.                 turtle.turnRight()
  234.             end
  235.         end
  236.     end
  237.    
  238.     if(height==3) then
  239.         turtle.turnLeft()
  240.         move()
  241.         placeCable(2)
  242.         moveBack()
  243.     else
  244.         --moveBack()
  245.         --moveBack(
  246.         moveUp()
  247.     end
  248.    
  249.     placeFlower(height-1)
  250. end
  251.  
  252. function main()
  253.     solarPanelSlot = 1
  254.     cableSlot = 9
  255.    
  256.     local length
  257.     local height
  258.    
  259.     print("Type in the number of the style you wish to build.")
  260.     print("")
  261.     print("1.) Flower")
  262.     print("2.) Gable (Triangular Roof)")
  263.    
  264.     --Gets sytle for building and checks input
  265.     local input = tonumber(io.read())
  266.     while(input~=1 and input ~= 2) do
  267.         print("Invalid choice. The number must be between 1-2.")
  268.         input = tonumber(io.read())
  269.     end
  270.    
  271.     --Gets the length for the gable and checks input
  272.     if input == 2 then
  273.         print()
  274.         print("Length?")
  275.         length = tonumber(io.read())
  276.         while length<0 do
  277.             print("The length cannot be less than zero")
  278.             length = tonumber(io.read())
  279.         end
  280.     end
  281.    
  282.     --Gets the hieght and checks input
  283.     print()
  284.     print("Height?")
  285.     height = tonumber(io.read())
  286.     while height<2 do
  287.         print("The height cannot be less than two")
  288.         height = tonumber(io.read())
  289.     end
  290.    
  291.     --places the gable
  292.     if input == 2 then
  293.         placeGable(length, height)
  294.     else
  295.         if height==2 then
  296.             placeLastFlowerRows()
  297.             return
  298.         end
  299.         --Gets to the initial height needed for 3 layers or more.
  300.         moveUp()
  301.         move()
  302.         move()
  303.         turtle.turnLeft()
  304.         move()
  305.         turtle.turnLeft()
  306.         placeFlower(height)
  307.     end
  308. end
  309.  
  310. main()
Advertisement
Add Comment
Please, Sign In to add comment