Guest User

Untitled

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