Advertisement
PhilHibbs

cc_turtle_log

Jul 28th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.54 KB | None | 0 0
  1.     --[[To Do:
  2.     If interest is high enough:
  3.     1. Add rednet capability
  4.     2. Save time by going through rows to detect trees
  5.     ]]
  6.      
  7.     --Version 1.2.5
  8.      
  9.     --Config rows are to the right
  10.     --Columns are forward
  11.     -- rows > x  x  x  x  x
  12.     --        x  x  x  x  x
  13.     --        ^  ^  ^  ^
  14.     --        Columns
  15.     rows = 2
  16.     columns = 2
  17.     --Space between saplings
  18.     space = 2
  19.     --Turn on Fuel Check
  20.     doCheckFuel = true
  21.     --If how much to raise by when fuel low
  22.     raise = 1000
  23.      
  24.     tArgs = {...}
  25.     tArgsCheck = {}
  26.      
  27.     if tArgs[1] == "initial" then
  28.     failings = 0
  29.     initial = true
  30.     if tArgs[2] and tArgs[3] and tArgs[4] then
  31.     if not (tonumber(tArgs[2]) and tonumber(tArgs[3]) and tonumber(tArgs[4])) then
  32.     error([[Please restart with acutal numbers in the argument,
  33.     it will most definantly not work without numbers there]],0)
  34.     end
  35.     rows = tonumber(tArgs[2])
  36.     columns = tonumber(tArgs[3])
  37.     space = tonumber(tArgs[4])
  38.     end
  39.     else
  40.     initial = false
  41.     tArgsCheck = {}
  42.     for i=1, #tArgs do
  43.     if tonumber(tArgs[i]) then
  44.     tArgsCheck[i] = tonumber(tArgs[i])
  45.     end
  46.     end
  47.     if tArgsCheck[1] ~= nil and tArgsCheck[2] ~= nil then
  48.     rows = tArgsCheck[1]
  49.     columns = tArgsCheck[2]
  50.     end
  51.     if tArgsCheck[3] ~= nil then
  52.     space = tArgsCheck[3]
  53.     end
  54.     end
  55.      
  56.     if rows < 1 then rows = 1 end
  57.     if columns <1 then columns = 1 end
  58.     if space <1 then space = 1 end
  59.     rows = math.floor(rows)
  60.     columns = math.floor(columns)
  61.     space = math.floor(space)
  62.      
  63.      
  64.      
  65.     --Calculating Fuel Usage
  66.     --This will just keep it over the fuel value you set
  67.     fuelUsage = rows*columns*8 + --Assume an average of height 8 per tree
  68.     rows*columns*(space+1) +
  69.     columns*(space+1)
  70.     if columns/2 ~= math.floor(columns/2)
  71.     then
  72.     fuelUsage = fuelUsage + rows*(space+1)
  73.     end
  74.     if doCheckFuel == true then
  75.     function checkFuel()
  76.     local fuelLevel = turtle.getFuelLevel()
  77.     return fuelLevel
  78.     end
  79.      
  80.     if checkFuel() < fuelUsage then
  81.     term.clear()
  82.     term.setCursorPos(1,1)
  83.     print("More Fuel Needed")
  84.     print("Place in Bottom Right, press any key")
  85.     os.pullEvent("char")
  86.     turtle.select(16)
  87.     while checkFuel() < fuelUsage + raise do
  88.     if turtle.refuel(1) == false then
  89.     term.clearLine()
  90.     print("Still too little fuel")
  91.     term.clearLine()
  92.     print("Press a key to resume fueling")
  93.     os.pullEvent("char")
  94.     end
  95.     local x,y = term.getCursorPos()
  96.     print(checkFuel().." Fuel")
  97.     term.setCursorPos(x,y)
  98.     end
  99.     print(checkFuel().." Units of Fuel")
  100.     sleep(3)
  101.     turtle.select(1)
  102.     end
  103.      
  104.     end
  105.      
  106.      
  107.     --Defining Functions
  108.     function move()
  109.     while not turtle.forward() do
  110.     turtle.dig()
  111.     end
  112.     turtle.suckDown()
  113.     end
  114.      
  115.     function logger()
  116.     print("Fuel: "..checkFuel())
  117.     local dist = 0
  118.     while turtle.detect() do
  119.     turtle.dig()
  120.     up()
  121.     dist = dist+1
  122.     end
  123.     for i=0, dist-1 do
  124.     down()
  125.     end
  126.     end
  127.      
  128.      
  129.     function moveSaplings()
  130.     local movedSaps = false
  131.     if turtle.getItemCount(1) < 10 then
  132.     local itemsInInventory = false
  133.     for i=2, 15 do
  134.     if turtle.getItemCount(i) > 0 then
  135.     itemsInInventory = true
  136.     end
  137.     end
  138.      
  139.     if itemsInInventory then
  140.     for i=2, 15 do
  141.     turtle.select(i)
  142.     if turtle.compareTo(1) then
  143.     turtle.transferTo(1)
  144.     movedSaps = true
  145.     end
  146.     end
  147.     end
  148.     turtle.select(1)
  149.     end
  150.     return movedSaps
  151.     end
  152.      
  153.     function countSaplings()
  154.     local var = turtle.getItemCount(1)
  155.     return var
  156.     end
  157.      
  158.     function inCheck()
  159.     if turtle.detect() then
  160.     down()
  161.     logger()
  162.     if countSaplings() > 1 then
  163.     turtle.place()
  164.     elseif moveSaplings() == true then
  165.     turtle.place()
  166.     end
  167.     up()
  168.     end
  169.     if initial == true then -- If you specify it is initial running
  170.     if countSaplings() > 1 then
  171.     down()
  172.     turtle.dig()
  173.     if not turtle.place() then
  174.     failings = failings + 1
  175.     print("On Row "..cRow..", Column "..cColumn.." a place failed due to")
  176.     if countSaplings() < 1 then
  177.     print("lack of saplings")
  178.     else
  179.     print("inhospitable terrain")
  180.     end
  181.     end
  182.     up()
  183.     else
  184.     failings = failings + 1
  185.     print("I'M FAILNG YOU, MASTER, NO SAPLINGS")
  186.     end
  187.     end
  188.     end
  189.      
  190.     function treeCheck()
  191.     if rowCheck == "right" then
  192.     turtle.turnLeft()
  193.     inCheck()
  194.     turtle.turnRight()
  195.     else
  196.     turtle.turnRight()
  197.     inCheck()
  198.     turtle.turnLeft()
  199.     end
  200.     move()
  201.     end
  202.      
  203.     --Misc. Move Functions:
  204.     function up()
  205.     while not turtle.up() do turtle.digUp() end
  206.     end
  207.     function down()
  208.     while not turtle.down() do turtle.digDown() end
  209.     end
  210.      
  211.     --Initial Check for Saplings
  212.     if turtle.getItemCount(1) == 0 then
  213.     turtle.turnLeft()
  214.     turtle.suck()
  215.     turtle.turnRight()
  216.     if countSaplings() == 0 then
  217.     print("No Saplings, Continue (\"c\") or Quit (\"q\")?")
  218.     local _, key = os.pullEvent("char")
  219.     if key == "q" then
  220.     return
  221.     end
  222.     end
  223.     end
  224.      
  225.      
  226.     rowCheck = "right"
  227.      
  228.     --UserInterface
  229.     term.clear()
  230.     term.setCursorPos(1,1)
  231.     print("This is the Auto Log Harvester")
  232.     print("Made by Civilwargeeky","")
  233.     print("Your Current Dimensions:")
  234.     print("X: "..rows.." Y: "..columns.." Space: "..space,"")
  235.     print("This Job Will Take Up To "..fuelUsage.." fuel")
  236.      
  237.      
  238.     while fuelUsage > turtle.getFuelLevel() do
  239.     print("More Fuel Needed. Place in Bottom Right")
  240.     print("Then Press any Key to fuel")
  241.     os.pullEvent("char")
  242.     turtle.select(16)
  243.     for i=1, turtle.getItemCount(16) do
  244.     if fuelUsage > turtle.getFuelLevel() then
  245.     turtle.refuel(1)
  246.     end
  247.     end
  248.     turtle.select(1)
  249.     end
  250.      
  251.     print("Starting in 3")
  252.     sleep(3)
  253.      
  254.     --Starting it along
  255.     if initial and countSaplings() == 0 then
  256.     print("No saplings, please restart with proper amounts")
  257.     return false
  258.     end
  259.     up()
  260.     turtle.turnRight()
  261.      
  262.     cRow = 0
  263.     cColumn = 0
  264.      
  265.     --In the program, the singular "column" and "row" are the current count
  266.     --   while the plural "columns" and "rows" are the numbers from the config
  267.      
  268.     --Actual Loops
  269.     for column=1, columns do
  270.     cColumn = cColumn + 1 -- See cRow below...
  271.      
  272.     --Cutting Down Every Tree in Column
  273.     move()
  274.     for row=1, rows do
  275.     cRow = cRow + 1 -- cRow because the rows variable refuses to be nice in called functions
  276.     treeCheck()
  277.     if row ~= rows then
  278.     for b=1, space do
  279.     move()
  280.     end
  281.     end
  282.     end
  283.      
  284.     --Go to Next Column
  285.     if column ~= columns then
  286.     if rowCheck == "right" then
  287.     turtle.turnLeft()
  288.     for b=1, space+1 do
  289.     move()
  290.     end
  291.     turtle.turnLeft()
  292.     else
  293.     turtle.turnRight()
  294.     for b=1, space+1 do
  295.     move()
  296.     end
  297.     turtle.turnRight()
  298.     end
  299.      
  300.      
  301.     --Switching Row
  302.     if rowCheck == "right" then
  303.     rowCheck = "left"
  304.     else
  305.     rowCheck = "right"
  306.     end
  307.     end
  308.     end
  309.      
  310.      
  311.     --Getting back to home chest
  312.     if rowCheck == "right" then
  313.     turtle.turnLeft()
  314.     turtle.turnLeft()
  315.     for a=1, rows-1 do
  316.     for i=1, 3 do
  317.     move()
  318.     end
  319.     end
  320.     for i=1, 2 do
  321.     move()
  322.     end
  323.     end
  324.     turtle.turnLeft()
  325.      
  326.     for i=1, (columns-1) * (space+1) do
  327.     move()
  328.     end
  329.     down()
  330.      
  331.     --Resupply Saplings
  332.     if turtle.getItemCount(1) < 10 then
  333.     if not moveSaplings() then
  334.     turtle.turnRight()
  335.     if turtle.detect() then
  336.     turtle.suck()
  337.     else
  338.     print("No Supply Chest")
  339.     sleep(1)
  340.     end
  341.     turtle.turnLeft()
  342.     end
  343.     end
  344.      
  345.      
  346.     --Dropping Inventory
  347.     logs = 0
  348.     sleep(1)
  349.     if turtle.detect() then
  350.     for i=2, 15 do
  351.     turtle.select(i)
  352.     if not turtle.compareTo(1) then
  353.     logs = logs + turtle.getItemCount(i)
  354.     turtle.drop()
  355.     end
  356.     end
  357.     else
  358.     for i=2, 15 do
  359.     turtle.select(i)
  360.     if not turtle.compareTo(1) then
  361.     logs = logs + turtle.getItemCount(i)
  362.     end
  363.     end
  364.     end
  365.     turtle.select(1)
  366.      
  367.      
  368.     turtle.turnLeft()
  369.     turtle.turnLeft()
  370.      
  371.     term.clear()
  372.     term.setCursorPos(1,1)
  373.     print("Job Done!")
  374.     print("Wood Obtained: "..logs)
  375.     print("Current Fuel: "..turtle.getFuelLevel())
  376.     if initial == true and failings ~= 0 then
  377.     print("Placed: "..(columns*rows-failings).."/"..(columns*rows))
  378.     end
  379.      
  380.     moveSaplings()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement