Advertisement
civilwargeeky

TreeFeller

Dec 29th, 2013
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.58 KB | None | 0 0
  1. --Single Tree-Cutter
  2. --Version 0.1.0
  3. --Made by Civilwargeeky, at the request of Mr. Hohenheim
  4. --Idea, if mod that lets see inv, then check if things are bonemeal/saplings
  5. --[[
  6. More ideas:
  7.   If wanting to save progress, you need these:
  8.     slotTypes
  9.     facing
  10.     atHome
  11.   On startup, if not at home, go down until you are level, then back one
  12.   Also on startup, turnTo 0
  13.  
  14.   MAKE IT SO ON ITEM RESTOCK, MATCHING ITEMS AND MATCHING ITEMS ONLY ARE PROPERLY MARKED
  15.   ]]
  16.  
  17. local insistOnStock = false --Whether or not it will force a certain amount of things to be in inventory
  18. local facing = 0 --0 is front, 1 is right, 2 is back, 3 is left
  19. local numCut = 0 --Trees cut
  20. local numDropped = 0 --Wood/things dropped off
  21. local slotTypes = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
  22. local typeTable = {sapling = 1, bonemeal = 2, wood = 0}
  23. local numTypes = 2 --Used in assign types function
  24. local materialsTable = {sapling = "left", bonemeal = "back", wood = "right"}
  25. local restock = {sapling = 64 * 2, bonemeal = 64*4}
  26. local keepOpen = 5
  27. local atHome = true --Whether or not its in its home spot
  28.  
  29. --Misc functions
  30. function getInvTable()
  31.   local toRet = {}
  32.   for i=1, 16 do
  33.     toRet[i] = turtle.getItemCount(i)
  34.   end
  35.   return toRet
  36. end
  37.  
  38. function getChangedSlots(input1, input2)
  39.   local toRet = {}
  40.   for i=1, math.min(#input1, #input2) do
  41.     if input1[i] ~= input2[i] then
  42.       table.insert(toRet,i)
  43.     end
  44.   end
  45.   return toRet
  46. end
  47. function countChange(func, num)
  48.   local snapshot, changed = getInvTable(), 0
  49.   func(num)
  50.   local ending = getInvTable()
  51.   for i=1,16 do
  52.     if snapshot[i] ~= ending[i] then
  53.       changed = changed + math.abs(snapshot[i]-ending[i])
  54.     end
  55.   end
  56.   return changed
  57. end
  58. function screenSet(x, y)
  59.   x, y = x or 1, y or 1
  60.   term.clear()
  61.   term.setCursorPos(x,y)
  62. end
  63. function display()
  64.   screenSet(1,1)
  65.   print("Fuel: ",turtle.getFuelLevel())
  66.   print("I couldn't really think of anything else to put here...")
  67. end
  68. --Custom movement related local functions
  69. function fromBoolean(input) --Like a calculator
  70. if input then return 1 end
  71. return 0
  72. end
  73. function coterminal(num, limit) --I knew this would come in handy :D TI-83 FTW!
  74. limit = limit or 4 --This is for facing
  75. return math.abs((limit*fromBoolean(num < 0))-(math.abs(num)%limit))
  76. end
  77. function genericTurn(func, toAdd)
  78.   local toRet = func()
  79.     facing = coterminal(facing + toAdd)
  80.   return toRet
  81. end
  82. function right()
  83.   return genericTurn(turtle.turnRight, 1)
  84. end
  85. function left()
  86.   return genericTurn(turtle.turnLeft, -1)
  87. end
  88. function turnTo(toTurn)
  89.   toTurn = coterminal(toTurn) or facing
  90.   local func = right
  91.   if coterminal(facing-toTurn) == 1 then func = left end --0 - 1 = -3, 1 - 0 = 1, 2 - 1 = 1
  92.   while facing ~= toTurn do          --The above is used to smartly turn
  93.     func()
  94.   end
  95. end
  96. function turnAround()
  97.   return turnTo(facing + 2) --Works because input is coterminaled
  98. end
  99. function genericDig(func, doAdd)
  100.   if func() then
  101.     if doAdd then
  102.       numCut = numCut + 1
  103.     end
  104.     display()
  105.     return true
  106.   end
  107.   return false
  108. end
  109. function dig(doAdd) return genericDig(turtle.dig, doAdd) end
  110. function digUp(doAdd) return genericDig(turtle.digUp, doAdd) end
  111. function digDown(doAdd) return genericDig(turtle.digDown, doAdd) end
  112. function genericMove(move, dig, attack, force)
  113.   force = force or true
  114.   while not move() do
  115.     if force then
  116.       if not dig() then
  117.         attack()
  118.        end
  119.     else print("Move Failed"); sleep(1)
  120.     end
  121.   end
  122.   return true
  123. end
  124. function forward(force)
  125.   return genericMove(turtle.forward, dig, turtle.attack, force)
  126. end
  127. local function returnNil() return nil end --Used in back below
  128. function back()
  129.   return genericMove(turtle.back, returnNil, returnNil, false)
  130. end
  131. function up(force)
  132.   return genericMove(turtle.up, digUp, turtle.attackUp, force)
  133. end
  134. function down(force)
  135.   return genericMove(turtle.down, digDown, turtle.attackDown, force)
  136. end
  137. --Specific local functions
  138. function getRep(which, fromBack)
  139.   local first, start, finish, step = false
  140.   if fromBack then
  141.     start, finish, step = 16, 1, -1
  142.   else
  143.     start, finish, step = 1, 16, 1
  144.   end
  145.   for i=start, finish, step do --Goes backward because slots are often taken/dropped off
  146.     --[[if turtle.getItemCount(i) > 0 and not first then --If not a rep, will return first slot with items
  147.       first = i
  148.     end]]
  149.     if slotTypes[i] == which then
  150.       return i
  151.     end
  152.   end
  153.   return first
  154. end
  155. function assignTypes(initial) --This gives all items names, if not initial, will not give newTypes
  156.   local currType --Types: 1 = saplings, 2 = bonemeal, >= 3 = other/wood
  157.   if initial then currType = 0 else currType = numTypes end
  158.   for i=1, 16 do
  159.     turtle.select(i)
  160.     local compares = false
  161.     for a=1, currType do
  162.       if turtle.compareTo(getRep(a, true) or 1) then --There should always be a representative, unless first slot
  163.         slotTypes[i] = a
  164.         print("Compares to ",a)
  165.         compares = true
  166.         break
  167.       end
  168.     end
  169.     if turtle.getItemCount(i) > 0 and initial and not compares and currType < 2 then --I don't care about the slot if its not a sapling/bonemeal
  170.       currType = currType + 1
  171.       slotTypes[i] = currType
  172.       print("New Item ",currType)
  173.     end
  174.     if not initial and not compares then
  175.       slotTypes[i] = 0
  176.     end
  177.   end
  178.   turtle.select(1)
  179.   --types = currType
  180. end
  181. function getNumType(which)
  182.   local num = 0
  183.   turtle.select(getRep(typeTable[which] or which))
  184.   for i=1, 16 do
  185.     if turtle.compareTo(i) then
  186.       num = num + turtle.getItemCount(i)
  187.     end
  188.   end
  189.   return num
  190. end
  191.  
  192.  
  193. function mineTree()
  194.   local moveDown = 0
  195.   forward()
  196.   atHome = false
  197.   while turtle.detectUp() do
  198.     up()
  199.     moveDown = moveDown + 1
  200.   end
  201.   for i=1, moveDown do
  202.     down()
  203.   end
  204.   back()
  205.   atHome = true
  206. end
  207. function placeSapling()
  208.   local currSlot = getRep(typeTable.sapling) or getMaterials("sapling")
  209.   turtle.select(currSlot) --If no saplings, get some saplings/wait
  210.   if not turtle.place() then
  211.     local k = not(dig(false)) or turtle.place() or print("Cannot place sapling, please fix") --Unexpected symbol crap --Digs without adding tries again, then prints that place failed
  212.     if turtle.getItemCount(currSlot) == 0 then
  213.       slotTypes[currSlot] = 0
  214.     end
  215.   end
  216. end
  217. function useBonemeal()
  218.   while true do
  219.     for i=1, 10 do
  220.       local currSlot = getRep(typeTable.bonemeal) or getMaterials("bonemeal")
  221.       turtle.select(currSlot)
  222.       turtle.place()
  223.       if turtle.getItemCount(currSlot) == 0 then
  224.         slotTypes[currSlot] = 0
  225.       end
  226.     end
  227.     return true
  228.     --[[turtle.select(getRep(typeTable.sapling)) --This doesn't work because turtle.compare doesn't work with bonemealed saplings
  229.     if not turtle.compare() then return true end]]
  230.   end
  231. end
  232. local facingTable = {}
  233. do --This will be so I have all the info I need for dropping and sucking.
  234.   local function newEntry (side, number, dropFunc, suckFunc, detectFunc)
  235.     facingTable[side] = {number = number, drop = dropFunc, suck = suckFunc, detect = detectFunc}
  236.   end
  237.   newEntry("forward", 0, turtle.drop, turtle.suck, turtle.detect)
  238.   newEntry("top", 0, turtle.dropUp, turtle.suckUp, turtle.detectUp)
  239.   newEntry("bottom", 0, turtle.dropDown, turtle.suckDown, turtle.detectDown)
  240.   newEntry("left", 3, turtle.drop, turtle.suck, turtle.detect)
  241.   newEntry("right", 1, turtle.drop, turtle.suck, turtle.detect)
  242.   newEntry("back", 2, turtle.drop, turtle.suck, turtle.detect)
  243. end
  244. function getMaterials(what, forceWait) --This function will get materials from a certain place
  245.   turtle.select(1) --So materials go in in the first available spot
  246.   local toFace, forceWait = facing, forceWait or insistOnStock
  247.   local facingInfo = facingTable[materialsTable[what]] --This table contains direction specific functions, since use is the same
  248.   turnTo(facingInfo.number) --Eg: facingTable[materialsTable["sapling"]].number --> facingTable["left"].number --> 3
  249.   local doWait = false
  250.   while not facingInfo.detect() do
  251.     doWait = true
  252.     screenSet()
  253.     print("Waiting for ",what," chest to be placed on ", materialsTable[what])
  254.     sleep(2)
  255.   end
  256.   if doWait then
  257.     print("Waiting for key press when all materials in chest")
  258.     os.pullEvent("char")
  259.   end
  260.   local snapshot = getInvTable() --Done to compare inventory
  261.   local numObtained = 0
  262.   while numObtained < restock[what] do
  263.     local a = countChange(facingInfo.suck)
  264.     if a ~= 0 then
  265.       numObtained = numObtained + a
  266.       print("Obtained ",numObtained,"/",restock[what]," ",what)
  267.     else
  268.       if forceWait then
  269.         print("Suck failed, no more ",what,"?")
  270.         sleep(4)
  271.       else
  272.         break
  273.       end
  274.     end
  275.   end
  276.   local currType = typeTable[what]
  277.   for a,b in ipairs(getChangedSlots(getInvTable(), snapshot)) do
  278.     slotTypes[b] = currType
  279.   end
  280.   assignTypes(false)
  281.   turnTo(toFace)
  282.   return (getRep(typeTable[what]) or getMaterials(what, true))
  283. end
  284. function dropMaterials(what, doAdd)
  285.   local toFace = facing
  286.   doAdd = doAdd or true
  287.   local facingInfo = facingTable[materialsTable[what]] --This table contains direction specific functions, since use is the same
  288.   turnTo(facingInfo.number) --Eg: facingTable[materialsTable["sapling"]].number --> facingTable["left"].number --> 3
  289.   while not facingInfo.detect() do
  290.     screenSet()
  291.     print("Waiting for ",what," chest to be placed on ", materialsTable[what])
  292.     sleep(2)
  293.   end
  294.   for i=1, 16 do
  295.     if slotTypes[i] == typeTable[what] and turtle.getItemCount(i) > 0 then
  296.       turtle.select(i)
  297.       local dropped = false
  298.       repeat
  299.         local curr = turtle.getItemCount(i)
  300.         local amount = countChange(facingInfo.drop, curr)
  301.         if doAdd then
  302.           numDropped = numDropped + amount --This is the global
  303.         end
  304.         print("Dropped ",amount," ",what)
  305.         if amount >= curr then
  306.           dropped = true
  307.         else
  308.           screenSet(1,1)
  309.           print("Cannot drop ",what," on ",materialsTable[what], " side")
  310.           sleep(2)
  311.         end
  312.       until dropped
  313.       slotTypes[i] = 0
  314.     end
  315.   end
  316.   turnTo(toFace)
  317.   turtle.select(1) --Its just nicer
  318.   return true
  319. end
  320.  
  321. function isFull()
  322.   local areFull = 0
  323.   local currInv = getInvTable()
  324.   for i=1, #currInv do
  325.     if currInv[i] > 0 then
  326.       areFull = areFull + 1
  327.     end
  328.   end
  329.   return areFull >= 16-keepOpen
  330. end
  331.  
  332. --Initial
  333. assignTypes(true) --Initial assign types
  334.  
  335. --Main Loop
  336. while true do
  337. if turtle.detect() then mineTree() end --Dig out the tree
  338.   placeSapling() --Place a sapling
  339.   useBonemeal() --Use the bonemeal
  340.   if isFull() then --If inventory is full, drop it
  341.     dropMaterials("wood")
  342.   end
  343. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement