Advertisement
psychic__panda

chopit v1.0

Nov 22nd, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.09 KB | None | 0 0
  1. -- **********************************************************************************
  2. -- **
  3. -- ** Minecraft Chopping Turtle v1.0 by Psychic__Panda
  4. -- ** ----------------------------------------------------
  5. -- **
  6. -- ** ************************************************************************
  7. -- ** *** THIS IS THE FIRST RELEASE OF chopit BY Psychic_Panda
  8. -- ** *** IT WORKS, IT MAY NOT BE VERY EFFICIENT YET
  9. -- ** *** THE NEXT VERSION IS IN DEVELOPMENT
  10. -- ** ************************************************************************
  11. -- **
  12. -- ** Recommend:
  13. -- ** 'label set myTurtle' on your turtle so that it saves state when you break it.
  14. -- **
  15. -- ** Get code on your turtle:
  16. -- ** pastebin get 1FdfpCPJ chopit
  17. -- ** chopit
  18. -- **
  19. -- ** Overview:
  20. -- ** A nearly perpetual logging turtle. This turtle will plant, fertilize, and chop down a specified number of trees,
  21. -- ** planted in a line in front of and extending to the right of the turtle.
  22. -- ** When your turtle runs low on fertilizer, saplings, or fuel, it will go get more from specified locations.
  23. -- ** When its inventory is full, it will dump its inventory into a chest at a specified location.
  24. -- ** You indicate how many trees to plant and chop down each iteration. After chopping down the trees, the
  25. -- ** turtle does a sweep of the area sucking up fallen saplings. This is very useful when when you are running a perpetual
  26. -- ** logging operation, to replenish saplings. If you are using a conveyor belt or slime channel to gather saplings,
  27. -- ** set enableSweep to false, but leave the sweep radius at its default, otherwise the chests will get in the
  28. -- ** way.
  29. -- **
  30. -- ** Usage:
  31. -- ** chopit help
  32. -- ** Print help and exit.
  33. -- **
  34. -- ** chopit [plantTrees] [sweepRadius]
  35. -- ** plantTarget is the target number of trees to plant.
  36. -- ** If it doesn't have enough saplings the turtle will attempt to get
  37. -- ** more saplings. It will then plant the number specified or
  38. -- ** the number of saplings you have (less one to save
  39. -- ** the place in its inventory) whichever is less. Default is 1 sapling.
  40. -- ** sweepRadius is the number of spaces around the planted trees the turtle will
  41. -- ** sweep for fallen saplings. Default is 4.
  42. -- **
  43. -- ** Set Up Example:
  44. -- ** In this example the turtle will plane 1 tree and sweep a radius of 4 around the tree
  45. -- ** chopping it down. Either of the following commands will work the same:
  46. -- **
  47. -- ** chopit
  48. -- **
  49. -- ** chopit 1 4
  50. -- **
  51. -- ** Inventory
  52. -- ** Start out with the following items in the following inventory
  53. -- ** slots:
  54. -- ** W: one block of wood. This saves the inventory spot for wood.
  55. -- ** B: A stack of Bonemeal or other fertilizer. The turtle will never use the last one.
  56. -- ** S: A stack of saplings (preferably Fir, Pine or Spruce that only grow up. (Not oak,
  57. -- ** eucalyptus, jungle wood, or sequoia!). The turtle will never use the last one.
  58. -- ** F: A stack of fuel, such as charcoal or coal, much more efficient than wood. The
  59. -- ** turtle will never use the last one. Recommend charcoal.
  60. -- **
  61. -- ** W . . .
  62. -- ** . . . .
  63. -- ** . . . .
  64. -- ** . B S F
  65. -- **
  66. -- **
  67. -- ** Map
  68. -- ** The turtle starts out at T. It checks for fuel and refuels if necessary. If there is no fuel in
  69. -- ** its inventory, it tries to get fuel from location F. Note that if the turtle starts out with no
  70. -- ** fuel in its inventory and no internal fuel it will not be able to get to the fuel barrel.
  71. -- ** The turtle then tries to plant a sapling. If it has 1 or 0 saplings it will try to get some from
  72. -- ** location S. It will plant the sapling and try to fertilize it. No fertilizer? It will try
  73. -- ** to get some from B (B for bonemeal, but it can use any fertilizer. Note that
  74. -- ** fertilizer doesn't work on rubber trees. If you want to try this with Rubber trees, set enableFertilizer
  75. -- ** to false.
  76. -- **
  77. -- ** . . . . . . . . . .
  78. -- ** . . . . . . . . . .
  79. -- ** . . . . . . . . . .
  80. -- ** . . . . . . . . . .
  81. -- ** . . . . . X . . . .
  82. -- ** S . . . . T . . . .
  83. -- ** B . . . . . . . . .
  84. -- ** C . . . . . . . . .
  85. -- ** W . . . . . . . . .
  86. -- ** F
  87. -- **
  88. -- ** Notes:
  89. -- ** You can set up a transport pipe system to sort the items in the Chest, sending the saplings to the
  90. -- ** Sapling barrel, and Wood blocks to a Wood Barrel. You can also set up a furnace to create more Charcoal from the
  91. -- ** Wood blocks, with an Emerald pipe to only take part of the wood, if you have another use for it.
  92. -- **
  93. -- ** Works with FTB TechWorld 2 and Minecraft 1.6.4 and later (probably).
  94. -- **
  95. -- ** You can change enableSweep in the variables below, as well as StorageX, SaplingY, FertilizerY, ChestY, WoodY, and FuelY
  96. -- **
  97. -- *********************************************************************************************************************
  98.  
  99. local args = { ... }
  100. local enableSweep = true
  101. local StorageX = -4
  102. local SaplingY = 0
  103. local FertilizerY = -1
  104. local ChestY = -2
  105. local WoodY = -3
  106. local FuelY = -4
  107. local FurnaceY = -5
  108. local FurnaceZ = 1
  109.  
  110. local enableFertilizer = true
  111. local maxTries
  112. local sleepTime
  113. if enableFertilizer then
  114. sleepTime=0.2
  115. maxTries=5
  116. else
  117. maxTries=20
  118. sleepTime=5
  119. end
  120.  
  121. local blockSlot=1
  122. local fertilizerSlot=14
  123. local treeSlot=15
  124. local fuelSlot=16
  125. local plantTarget=1
  126. local plantMax=8
  127. local fuelMin=500
  128. local flagFuel=false
  129. local flagFertilizer=false
  130. local flagTrees=false
  131. local sweepRadius=4
  132. local positionX=0
  133. local positionY=0
  134. local positionZ=0
  135.  
  136. right="right"
  137. left="left"
  138. up="up"
  139. down="down"
  140. forward="forward"
  141. back="back"
  142.  
  143. if (#args > 0) then
  144. if args[1]=="help" then
  145. print("Usage: chopit [numberTrees] [sweepRadius]");
  146. print("where [numberTrees} is the number of trees");
  147. print("to plant each time, default is 1.");
  148. print("and [sweepRadius} is the number of blocks");
  149. print("around the tree to sweep for saplings, default is 4.");
  150. else
  151. plantTarget = tonumber(args[1])
  152. if (plantTarget>plantMax) then plantTarget = plantMax end
  153. end
  154. end
  155. if (#args > 1) then sweepRadius= tonumber(args[2]) end
  156.  
  157. -- ********************************************************************************** --
  158. -- Check that turtle has fuel
  159. -- ********************************************************************************** --
  160. function doRefuel()
  161.  
  162. local fuelLevel = turtle.getFuelLevel()
  163. print("Fuel level at "..fuelLevel)
  164. fuelLeft=turtle.getItemCount(fuelSlot)-1
  165.  
  166. if ((fuelLevel ~= "unlimited") and (fuelLevel<fuelMin)) then
  167. if (fuelLeft <= 1) then
  168. print("Critically low on fuel")
  169. OK = getFuel()
  170. if not OK then return false end
  171. fuelLeft=turtle.getItemCount(fuelSlot)-1
  172. end
  173. print("Refuelling")
  174. turtle.select(fuelSlot)
  175. turtle.refuel(fuelLeft)
  176. OK = getFuel()
  177. else
  178. print("Plenty of fuel")
  179. end
  180.  
  181. return true
  182.  
  183. end
  184.  
  185. -- ********************************************************************************** --
  186. -- Plant trees in a straight line
  187. -- ********************************************************************************** --
  188. function doPlant()
  189.  
  190. --see if there are enough saplings
  191. treesLeft = turtle.getItemCount(treeSlot)-1
  192. if (treesLeft < plantTarget) then
  193. print("Not enough saplings: "..treesLeft..". Getting saplings")
  194. if not getTrees() then return false end
  195. end
  196.  
  197. treesLeft = turtle.getItemCount(treeSlot)-1
  198. if (treesLeft < plantTarget) then
  199. plantTrees = treesLeft
  200. else
  201. plantTrees = plantTarget
  202. end
  203.  
  204. print("Planting "..plantTrees)
  205. doReturn()
  206. for thisTree=1,plantTrees do
  207. turtle.select(treeSlot)
  208. turtle.place()
  209. doMove(right)
  210. end
  211. return true
  212.  
  213. end
  214.  
  215. -- ********************************************************************************** --
  216. -- Grow a Tree
  217. -- ********************************************************************************** --
  218. function doGrow()
  219.  
  220. for thisTry=1,maxTries do
  221.  
  222. if turtle.detectUp() then turtle.digUp() end
  223. doMove(up)
  224. if turtle.detect() then
  225. doMove(down)
  226. return true
  227. end
  228. doMove(down)
  229.  
  230. if enableFertilizer then
  231. fertilizerLeft = turtle.getItemCount(fertilizerSlot)-1
  232. if (fertilizerLeft<1) then
  233. print("Out of fertilizer: "..fertilizerLeft)
  234. print("Getting more.")
  235. if not getFertilizer(positionX,positionY,positionZ) then return false end
  236. end
  237. turtle.select(fertilizerSlot)
  238. turtle.place()
  239. end
  240. sleep(sleepTime)
  241. end
  242.  
  243. return true
  244.  
  245. end
  246.  
  247. -- ********************************************************************************** --
  248. -- Chop it down
  249. -- ********************************************************************************** --
  250. function doChop()
  251. --chop it down
  252. turtle.dig()
  253. doMove(forward)
  254. while turtle.detectUp() do
  255. turtle.digUp()
  256. doMove(up)
  257. end
  258.  
  259. --go back down to the ground
  260. doMove(down,positionZ)
  261. doMove(back)
  262. doMove(right)
  263. end
  264.  
  265. -- ********************************************************************************** --
  266. -- Sweep Up any floating saplings
  267. -- ********************************************************************************** --
  268. function doSweep()
  269. doReturn(plantTrees+sweepRadius-1,1-sweepRadius,0)
  270. thisDirection=-1
  271. for thisSweep=plantTrees+sweepRadius-1,0-sweepRadius,-1 do
  272. doReturn(thisSweep,1-(thisDirection*sweepRadius),0)
  273. thisDirection=thisDirection * -1
  274. end
  275. end
  276.  
  277. -- ********************************************************************************** --
  278. -- Get tree saplings from chest or barrel
  279. -- ********************************************************************************** --
  280. function getFertilizer(returnToX,returnToY,returnToZ)
  281. -- Fertilizer Chest should be sweepRadius to the left and back 1 from the origin
  282. doReturn(StorageX,FertilizerY,0)
  283.  
  284. --face the chest to extract
  285. turtle.turnLeft()
  286. turtle.select(fertilizerSlot)
  287. turtle.suck()
  288. turtle.turnRight()
  289.  
  290. doDropOff() -- in case we got too many
  291. doReturn(returnToX,returnToY,returnToZ)
  292. return (turtle.getItemCount(fertilizerSlot)>1)
  293.  
  294. end
  295.  
  296. -- ********************************************************************************** --
  297. -- Get tree saplings from chest or barrel
  298. -- ********************************************************************************** --
  299. function getTrees()
  300. -- Sapling Chest should be sweepRadius to the left from the origin
  301. doReturn(StorageX,SaplingY,0)
  302.  
  303. --face the chest to extract
  304. turtle.turnLeft()
  305. turtle.select(treeSlot)
  306. turtle.suck()
  307. turtle.turnRight()
  308.  
  309. doDropOff() -- in case we got too many
  310. doReturn()
  311. return (turtle.getItemCount(treeSlot)>1)
  312.  
  313. end
  314.  
  315. -- ********************************************************************************** --
  316. -- Get fuel from chest or barrel
  317. -- ********************************************************************************** --
  318. function getFuel()
  319. -- Fuel Chest should be sweepRadius to the left, and 6 back from the origin
  320. doReturn(StorageX,FuelY,0)
  321.  
  322. --face the chest to extract
  323. turtle.turnLeft()
  324. turtle.select(fuelSlot)
  325. turtle.suck()
  326. turtle.turnRight()
  327.  
  328. doDropOff() -- in case we got too many
  329. return (turtle.getItemCount(fuelSlot)>1)
  330.  
  331. end
  332.  
  333. -- ********************************************************************************** --
  334. -- Drop Off Stuff in Chest
  335. -- ********************************************************************************** --
  336. function doDropOff()
  337. -- Chest should be sweepRadius to the left, and 2 back from the origin
  338. doReturn(StorageX,ChestY,0)
  339.  
  340. --drop off all but one wood block (for comparison)
  341. turtle.turnLeft()
  342. lastSlot=fertilizerSlot
  343. if enableFertilizer then lastSlot=lastSlot-1 end
  344. for thisSlot=1,lastSlot do
  345. turtle.select(thisSlot)
  346. if (turtle.getItemCount(thisSlot) > 0) then
  347. if (thisSlot==blockSlot) then
  348. turtle.drop(turtle.getItemCount(thisSlot)-1)
  349. else
  350. turtle.drop()
  351. end
  352. end
  353. end
  354. turtle.turnRight()
  355.  
  356. end
  357.  
  358. -- ********************************************************************************** --
  359. -- Return to starting position
  360. -- ********************************************************************************** --
  361. function doReturn(toX,toY,toZ)
  362. toX=toX or 0
  363. toY=toY or 0
  364. toZ=toZ or 0
  365.  
  366. --print("At X: "..positionX..", Y: "..positionY..", Z: "..positionZ)
  367. --print("To: X: "..toX..", Y: "..toY..", Z: "..toZ)
  368. doMove(right,toX-positionX)
  369. doMove(forward,toY - positionY)
  370. doMove(up,toZ - positionZ)
  371. --print("At X: "..positionX..", Y: "..positionY..", Z: "..positionZ)
  372.  
  373. end
  374.  
  375. -- ********************************************************************************** --
  376. -- Move, and keep track or relative position
  377. -- ********************************************************************************** --
  378. function doMove(direction,distance)
  379.  
  380. -- set defaults
  381. if (distance==nil) then distance = 1 end
  382. if (distance==0) then return end
  383. direction=direction or "none"
  384.  
  385. --fix negative distances
  386. if (distance<0) then
  387. distance=distance * -1
  388. if (direction==right) then direction=left
  389. elseif (direction==left) then direction=right
  390. elseif (direction==forward) then direction=back
  391. elseif (direction==back) then direction=forward
  392. elseif (direction==up) then direction=down
  393. elseif (direction==down) then direction=up
  394. end
  395. end
  396.  
  397. -- increase or decrease position with increment
  398. if ((direction==right) or (direction==up) or (direction==forward)) then
  399. increment=1
  400. else
  401. increment=-1
  402. end
  403.  
  404. --always face forward so that suck works
  405. --set temporary location to update
  406. if (direction==right) then
  407. tmpLocation=positionX
  408. turtle.turnRight()
  409. elseif (direction==left) then
  410. tmpLocation=positionX
  411. turtle.turnLeft()
  412. elseif (direction==back) then
  413. turtle.turnLeft()
  414. turtle.turnLeft()
  415. tmpLocation=positionY
  416. elseif (direction==forward) then
  417. tmpLocation=positionY
  418. elseif ((direction==up) or (direction==down)) then
  419. tmpLocation=positionZ
  420. else
  421. print("unknown direction: "..direction)
  422. return
  423. end
  424.  
  425. --move and update position
  426. if (direction==up) then
  427. for d=1,distance do
  428. turtle.suckUp()
  429. if turtle.up() then tmpLocation=tmpLocation+increment end
  430. end
  431. elseif (direction==down) then
  432. for d=1,distance do
  433. turtle.suckDown()
  434. if turtle.down() then tmpLocation=tmpLocation+increment end
  435. end
  436. else
  437. for d=1,distance do
  438. turtle.suck()
  439. if turtle.forward() then tmpLocation=tmpLocation+increment end
  440. end
  441. end
  442.  
  443. --set the globalposition and face correct direction
  444. if (direction==right) then
  445. positionX=tmpLocation
  446. turtle.turnLeft()
  447. elseif (direction==left) then
  448. positionX=tmpLocation
  449. turtle.turnRight()
  450. elseif (direction==back) then
  451. positionY=tmpLocation
  452. turtle.turnLeft()
  453. turtle.turnLeft()
  454. elseif (direction==forward) then
  455. positionY=tmpLocation
  456. elseif ((direction==up) or (direction==down)) then
  457. positionZ=tmpLocation
  458. end
  459.  
  460.  
  461. end
  462.  
  463. -- ********************************************************************************** --
  464. -- The main loop
  465. -- ********************************************************************************** --
  466. OK = true
  467. while OK do
  468. OK = doRefuel()
  469. if OK then
  470. OK = doPlant()
  471. doReturn()
  472. if (OK) then
  473. for thisTree=1,plantTrees do
  474. OK = doGrow()
  475. doChop()
  476. end
  477. if enableSweep then doSweep() end
  478. end
  479. doDropOff()
  480. doReturn()
  481. end
  482. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement