Advertisement
PhilHibbs

cc_turtle_log

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