Guest User

FullLogger version 2.2

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