Advertisement
ZottelvonUrvieh

chopper

Aug 27th, 2013
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.17 KB | None | 0 0
  1. facing = 1 --facing:richtung von ursprung ausgehend NOSW 1234 aktuallisiert beim bewegen/drehen N/S ist xachse
  2. cords = vector.new(0,0,0)
  3.  
  4. farmlenght = 5 --farmlenght:amount of trees
  5. farmwidth = 8
  6. treedist = 2
  7. modus = null
  8. status = "notfull" -- stati: notfull,full,nosaplings
  9.  
  10. function loadcords()
  11.     local file = fs.open("cords.dat","r")
  12.     facing = tonumber(file.readLine())
  13.     cords.x = tonumber(file.readLine())
  14.     cords.y = tonumber(file.readLine())
  15.     cords.z = tonumber(file.readLine())
  16.     file.close()
  17. end
  18.  
  19. function updatecords()
  20.     local file = fs.open("cords.dat","w")
  21.     file.writeLine(facing)
  22.     file.writeLine(cords.x)
  23.     file.writeLine(cords.y)
  24.     file.writeLine(cords.z)
  25.     file.close()
  26.     loadcords()
  27. end
  28.  
  29.  
  30. function loadsettings()
  31.     local file = fs.open ("chopper.cfg", "r")
  32.     file.readLine(farmlenght)
  33.     file.readLine(farmwidht)
  34.     file.readLine(treedist)
  35.     file.readLine(modus)
  36.     file.close()
  37. end
  38.  
  39. function savesattings()
  40.     local file = fs.open ("chopper.cfg", "w")
  41.     file.writeLine (farmlenght)
  42.     file.writeLine (farmwidht)
  43.     file.writeLine (treedist)
  44.     file.writeLine (modus)
  45.     file.close()
  46.     loadsettings()
  47. end
  48.  
  49. function breakcheck()
  50.     if turtle.getItemCount(9) == 0 then
  51.         print("turtle was broken! reset to startpoint!")
  52.         cords.x,cords.y,cords.z = 0,0,0
  53.         facing = 1
  54.         updatecords()
  55.     else loadcords() loadsettings()
  56.     end
  57. end
  58.  
  59. function move(amount, direction) -- direction: 1, 2 or empty 1=left,2=right,3=up,4=down empty = forward
  60.     selectlogslot()
  61.     if direction == null then
  62.         direction = 0
  63.     elseif direction == 1 then
  64.         turtle.turnLeft()
  65.         facing = (facing+2)%4 + 1
  66.     elseif direction == 2 then
  67.         turtle.turnRight()
  68.         facing = (facing)%4 + 1
  69.     end
  70.     updatecords()
  71.     if amount == null then amount = 1 end
  72.     if amount ~= 0 then
  73.         if direction < 3 then
  74.             for i=1,amount do
  75.                 if turtle.detect() then turtle.dig() end
  76.                 while not turtle.forward() do sleep(0.1) end
  77.                 if facing == 1 then cords.x = cords.x + 1
  78.                 elseif facing == 2 then cords.y = cords.y + 1
  79.                 elseif facing == 3 then cords.x = cords.x - 1
  80.                 elseif facing == 4 then cords.y = cords.y - 1
  81.                 end
  82.                 updatecords()  
  83.             end
  84.         elseif direction == 3 then
  85.             for i=1,amount do
  86.                 if turtle.detectUp() then turtle.digUp() end
  87.                 while not turtle.up() do sleep(0.1) end
  88.                 cords.z = cords.z + 1
  89.                 updatecords()
  90.             end
  91.         elseif direction == 4 then
  92.             for i=1,amount do
  93.                 if turtle.detectDown() then turtle.digDown() end
  94.                 while not turtle.down() do sleep(0.1) end
  95.                 cords.z = cords.z - 1
  96.                 updatecords()
  97.             end
  98.         end
  99.     end
  100. end
  101.  
  102. function selectlogslot()
  103.     for i=9,16 do
  104.         if turtle.getItemCount(i) < 64 then
  105.             turtle.select(i)
  106.             status = "notfull"
  107.             return
  108.         end
  109.     end
  110.     status = "full"
  111. end
  112.  
  113. function selectsaplingslot()
  114.     for i=5,8 do
  115.         if turtle.getItemCount(i) > 0 then
  116.             turtle.select(i)
  117.             status = "notfull"
  118.             return
  119.         end
  120.     end
  121.     status = "nosaplings"
  122. end
  123.  
  124. function cut()
  125.     selectlogslot()
  126.     if turtle.compare() then
  127.         if status == "notfull" then move() end
  128.         selectlogslot()
  129.         if status == "notfull" then turtle.digDown()
  130.         selectsaplingslot() if status ~= "nosaplings" then turtle.placeDown() end end
  131.         selectlogslot()
  132.         while turtle.compareUp() do if status == "notfull" then move(1,3) selectlogslot() end end
  133.         temp = cords.z
  134.         for a=1, temp do move(1,4) end
  135.         selectlogslot()
  136.     else
  137.         move()
  138.         selectsaplingslot()
  139.         if not turtle.compareDown() then           
  140.             if turtle.detectDown() then
  141.                 selectlogslot() turtle.digDown() selectsaplingslot() turtle.placeDown()
  142.             else selectsaplingslot() turtle.placeDown()
  143.             end
  144.             selectlogslot()
  145.         end
  146.     end
  147. end
  148.  
  149. function empty()
  150.     if cords.x+cords.y+cords.z+facing == 1 then
  151.         move(0,2)
  152.         for i=9,16 do
  153.             if turtle.getItemCount(i) > 0 then
  154.                 selectsaplingslot()
  155.                 if turtle.compareTo(i) then
  156.                     turtle.select(i)
  157.                     turtle.transferTo(8)
  158.                     turtle.transferTo(7)
  159.                     turtle.transferTo(6)
  160.                 end
  161.                 turtle.select(i)
  162.                 if i == 9 then turtle.drop(turtle.getItemCount(i)-1)
  163.                 else turtle.drop() end
  164.             end
  165.         end
  166.         selectlogslot()
  167.         move(0,1)      
  168.     end
  169. end
  170.  
  171. function refill()
  172.     --sort dirt
  173.     if cords.x+cords.y+cords.z+facing == 1 then
  174.         move(0,1)
  175.         dirt,saplings = 0,0
  176.         for i=2,4 do
  177.             turtle.select(i)
  178.             turtle.transferTo(1)
  179.         end
  180.         for i=3,4 do           
  181.             turtle.select(i)
  182.             turtle.transferTo(2)
  183.         end
  184.         turtle.select(4)
  185.         turtle.transferTo(3)   
  186.     --sort saplings
  187.         for i=6,8 do
  188.             turtle.select(i)
  189.             turtle.transferTo(5)
  190.             end
  191.         for i=7,8 do           
  192.             turtle.select(i)
  193.             turtle.transferTo(6)
  194.         end
  195.         turtle.select(8)
  196.         turtle.transferTo(7)
  197.         for i=5,8 do
  198.             if turtle.getItemCount(i) < 64 then turtle.select(i) turtle.suck() end
  199.         end
  200.         nottoomuch = true
  201.         for i=10,16 do
  202.             if turtle.getItemCount(i) > 0 then
  203.                 turtle.select(i)
  204.                 nottoomuch = nottoomuch and turtle.drop()
  205.             end
  206.         end
  207.         if not nottoomuch then
  208.             move(0,2) move(0,2)
  209.             for i=10,16 do
  210.                 if turtle.getItemCount(i) > 0 then
  211.                     turtle.select(i)
  212.                     while not turtle.drop() do sleep(0.3) end
  213.                 end
  214.             end
  215.             move(0,1)
  216.         else
  217.             move(0,2)
  218.         end
  219.     end
  220. end
  221.  
  222. function gohome()
  223.     if cords.z ~= 0 then
  224.         selectlogslot()
  225.         while turtle.compareUp() do if status == "notfull" then move(1,3) selectlogslot() end end
  226.         temp = cords.z
  227.         for a=1, temp do move(1,4) end
  228.         selectlogslot()
  229.     end
  230.     if cords.z == 0 then
  231.         if facing == 1 then
  232.             move(1,2) move(0,2)
  233.         elseif facing == 2 then
  234.             move() move(0,2)
  235.         elseif facing == 3 then
  236.             move(1,2) move(0,1)
  237.         elseif facing == 4 then
  238.             move()
  239.             while facing ~= 3 do move(0,1) end
  240.         end
  241.     end
  242.  
  243.     while cords.x > 1 do move() end
  244.     move(0,2)
  245.     while cords.y > 0 do move() end
  246.     move(1,1) move(0,1) move(0,1)
  247. end
  248.  
  249. function work()
  250.     -- initial row
  251.     move(1)
  252.     cut()
  253.     while cords.x < ((farmlenght-1)*treedist + farmlenght) do      
  254.         move(treedist)
  255.         cut()
  256.     end
  257.     -- all other rows
  258.     while cords.y < ((farmwidth-1)*treedist + farmwidth)-1 do
  259.         if facing == 1 then
  260.             move(0,2) move(treedist) cut() move(0,2)
  261.             while cords.x > 3 do       
  262.             move(treedist)
  263.             cut()
  264.         end
  265.         else
  266.             move(0,1) move(treedist) cut() move(0,1)
  267.             while cords.x < ((farmlenght-1)*treedist + farmlenght) do      
  268.             move(treedist)
  269.             cut()
  270.         end
  271.         end    
  272.     end
  273. end
  274.  
  275. function startup()
  276.     breakcheck()
  277.     if cords.z ~= 0 then
  278.         print("finishing tree")
  279.         selectsaplingslot()
  280.         if turtle.compareDown() then
  281.             selectlogslot()
  282.             while turtle.compareUp() do
  283.                 move(1,3)              
  284.             end
  285.             temp = cords.z
  286.             for a=1, temp do move(1,4) end
  287.         end
  288.     end
  289.     if cords.x ~= 0 or cords.y ~= 0 then print("returning home coz restart") gohome() end
  290.     main()
  291. end
  292.  
  293. function everythingokcheck()
  294.     while turtle.getItemCount(5) < (farmlenght * farmwidth) do
  295.         turtle.select(5)
  296.         sleep(0.1)
  297.         term.clear()
  298.         term.setCursorPos(1,1)
  299.         a = (farmlenght * farmwidth) - turtle.getItemCount(5)
  300.         print("put at least ".. tostring(a) .. " saplings in slot 5")
  301.     end
  302.     while turtle.getItemCount(9) < 1 do
  303.         turtle.select(9)
  304.         sleep(0.1)
  305.         term.clear()
  306.         term.setCursorPos(1,1)
  307.         print("at least 1 log in slot 9")
  308.     end
  309.     term.clear()
  310.     print("going to work :-)")
  311. end
  312.  
  313. function main()
  314.     while true do
  315.         empty()
  316.         refill()
  317.         everythingokcheck()
  318.         work()
  319.         gohome()
  320.         sleep(300)
  321.     end
  322. end
  323.  
  324. startup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement