Advertisement
Nokiyen

woodCutter2

Jun 15th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.36 KB | None | 0 0
  1. --[[
  2. ***********
  3.  * woodCutter2
  4.  *
  5.  * To cut woods.
  6.  * saplings slots are 1.
  7. **********
  8. ]]
  9.  
  10.  
  11. ----------
  12. -- Variables.
  13. ----------
  14. -- get arguments.
  15. local tArgs = { ... }
  16. -- define vars.
  17. local column = tonumber(tArgs[1])
  18. local row = tonumber(tArgs[2])
  19. -- check arguments.
  20. if type(column) ~= 'number' or type(row) ~= 'number' then
  21.     print('Wrong arguments. Only numbers accepted.')
  22.     return
  23. end
  24. -- define vars.
  25. local saplingSlot = 1
  26. local refuelLevel = 5000
  27. local waitInterval = 3
  28. local waitDif = 0
  29.  
  30. -- create move instance.
  31. dofile("move")
  32. local moveObj, error_msg = Move.new()
  33. if moveObj == false then
  34.     print("Something wrong. Can't create Move obj.")
  35.     print(error_msg)
  36.     return
  37. end
  38.  
  39.  
  40. ----------
  41. -- Functions.
  42. ----------
  43. function dig(dir)
  44.     local currentDig = turtle.dig
  45.     local currentDetect = turtle.detect
  46.     if dir == 'up' then
  47.         currentDig = turtle.digUp
  48.         currentDetect = turtle.detectUp
  49.     elseif dir == 'down' then
  50.         currentDig = turtle.digDown
  51.         currentDetect = turtle.detectDown
  52.     end
  53.  
  54.     local times = 0
  55.     while currentDetect() do
  56.         currentDig()
  57.         times = times + 1
  58.         if times == 20 then
  59.             return false
  60.         end
  61.         sleep(1)
  62.     end
  63.  
  64.     return true
  65. end
  66.  
  67. function digAndMove(times)
  68.     for i=1, times, 1 do
  69.         dig('up')
  70.         dig('down')
  71.         moveObj:moveSure('front')
  72.     end
  73. end
  74.  
  75. function cutSingle()
  76.     moveObj:moveSure('up')
  77.     moveObj:moveSure('up')
  78.  
  79.     digAndMove(1)
  80.     moveObj:turnLeft()
  81.     digAndMove(2)
  82.     moveObj:turnLeft()
  83.     digAndMove(2)
  84.     moveObj:turnLeft()
  85.     digAndMove(3)
  86.     moveObj:turnLeft()
  87.     digAndMove(3)
  88.     moveObj:turnLeft()
  89.     digAndMove(4)
  90.     moveObj:turnLeft()
  91.     digAndMove(4)
  92.     moveObj:turnLeft()
  93.     digAndMove(4)
  94.     dig('down')
  95.  
  96.     moveObj:moveSure('up')
  97.     moveObj:moveSure('up')
  98.     moveObj:moveSure('up')
  99.     moveObj:turnLeft()
  100.  
  101.     digAndMove(4)
  102.     moveObj:turnLeft()
  103.     digAndMove(4)
  104.     moveObj:turnLeft()
  105.     digAndMove(4)
  106.     moveObj:turnLeft()
  107.     digAndMove(3)
  108.     moveObj:turnLeft()
  109.     digAndMove(3)
  110.     moveObj:turnLeft()
  111.     digAndMove(2)
  112.     moveObj:turnLeft()
  113.     digAndMove(2)
  114.     moveObj:turnLeft()
  115.     digAndMove(1)
  116.     moveObj:turnLeft()
  117.     digAndMove(1)
  118.     dig('up')
  119.  
  120.     for i=1, 5, 1 do
  121.         moveObj:moveSure('down')
  122.     end
  123.     moveObj:turnRight()
  124.     moveObj:moveSure('front')
  125.     moveObj:turnRight()
  126.     moveObj:turnRight()
  127.     turtle.select(saplingSlot)
  128.     local times = 0
  129.     while turtle.place() == false do
  130.         times = times + 1
  131.         if times == 20 then
  132.             break
  133.         end
  134.         sleep(1)
  135.     end
  136.     turtle.select(1)
  137.  
  138.     moveObj:turnRight()
  139. end
  140.  
  141. function turn(dir)
  142.     local currentFunc
  143.     if dir%2 == 0 then
  144.         moveObj:turnRight()
  145.     else
  146.         moveObj:turnLeft()
  147.     end
  148. end
  149.  
  150. local function boolToNum(bool)
  151.     if bool == true then
  152.         return 1
  153.     else
  154.         return 0
  155.     end
  156. end
  157.  
  158. function drop()
  159.     moveObj:turnLeft()
  160.     local res = 1
  161.     for i=2, 16, 1 do
  162.         if turtle.getItemCount(i) ~= 0 then
  163.             turtle.select(i)
  164.             res = res * boolToNum(turtle.drop())
  165.         end
  166.     end
  167.     turtle.select(1)
  168.     moveObj:turnRight()
  169.    
  170.     if res == 0 then
  171.         return false
  172.     else
  173.         return true
  174.     end
  175. end
  176.  
  177. function refuel()
  178.     if turtle.getFuelLevel() < refuelLevel then
  179.         while turtle.suckDown() do
  180.         end
  181.         for i=2, 16, 1 do
  182.             turtle.select(i)
  183.             turtle.refuel()
  184.         end
  185.         turtle.select(1)
  186.        
  187.         if turtle.getFuelLevel() < refuelLevel then
  188.             return false
  189.         else
  190.             return true
  191.         end
  192.     else
  193.         return true
  194.     end
  195. end
  196.  
  197. function getTime()
  198.     local hour, min = math.modf(os.time())
  199.     min = math.floor( min * 60 )
  200.    
  201.     local time = os.day()..":"..hour..":"..min
  202.     return time
  203. end
  204.  
  205. function getWaitTime(interval, givenDif)
  206.     local dif = givenDif or 0
  207.  
  208.     local hour, min = math.modf(os.time())
  209.     local totalTime = os.day()*24 + hour + 12*dif
  210.     local residue = (12*interval) - (totalTime % (12*interval))
  211.  
  212.     return residue * 50
  213. end
  214.  
  215. function updateScreen(message, startTime)
  216.     term.scroll(1)
  217.     term.setCursorPos(1,1)
  218.     term.clearLine()
  219.     term.write("Wood Cutter 2")
  220.     term.setCursorPos(1,2)
  221.     term.clearLine()
  222.     term.setCursorPos(1,3)
  223.     term.clearLine()
  224.     term.write("|------------------------------|")
  225.     term.setCursorPos(1,11)
  226.     term.clearLine()
  227.     local str1 = "|"..startTime.." -> "..getTime()
  228.     for i=1, 35 - string.len(str1), 1 do
  229.         str1 = str1.." "
  230.     end
  231.     term.write(str1.."|")
  232.     term.setCursorPos(1,12)
  233.     term.clearLine()
  234.     local str2 = "|"..message
  235.     for i=1, 35 - string.len(str2), 1 do
  236.         str2 = str2.." "
  237.     end
  238.     term.write(str2.."|")
  239.     term.setCursorPos(1,13)
  240.     term.clearLine()
  241.     term.write("|------------------------------|")
  242. end
  243.  
  244.  
  245. ----------
  246. -- Main Thread.
  247. ----------
  248. moveObj:returnToIniPointSure()
  249. sleep(getWaitTime(waitInterval, waitDif))
  250. while true do
  251.     local startTime = getTime()
  252.  
  253.     for i=1, 3, 1 do
  254.         moveObj:moveSure('front')
  255.     end
  256.  
  257.     for i=1, column-1, 1 do
  258. --      print('start line'..i..'.')
  259.  
  260.         for j=1, row-1, 1 do
  261.             cutSingle()
  262.             for k=1, 5, 1 do
  263.                 moveObj:moveSure('front')
  264.             end
  265.         end
  266.         cutSingle()
  267.  
  268.         moveObj:moveSure('front')
  269.         turn(i)
  270.         for k=1, 3+(i%2*4), 1 do
  271.             moveObj:moveSure('front')
  272.         end
  273.         turn(i)
  274.         moveObj:moveSure('front')
  275.     end
  276.  
  277.     for i=1, row-1, 1 do
  278.         cutSingle()
  279.         for j=1, 5, 1 do
  280.             moveObj:moveSure('front')
  281.         end
  282.     end
  283.     cutSingle()
  284.     moveObj:moveSure('front')
  285.  
  286.     moveObj:returnToIniPointSure()
  287.     while drop() == false do
  288.         updateScreen("Can't drop. Wating.", getTime())
  289.         sleep(3600)
  290.     end
  291.     while refuel() == false do
  292.         updateScreen("Can't refuel. Wating.", getTime())
  293.         sleep(3600)
  294.     end
  295.    
  296.     updateScreen('Finish cutting.', startTime)
  297.     sleep(getWaitTime(waitInterval, waitDif))
  298. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement