Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- ***********
- * woodCutter2
- *
- * To cut woods.
- * saplings slots are 1.
- **********
- ]]
- ----------
- -- Variables.
- ----------
- -- get arguments.
- local tArgs = { ... }
- -- define vars.
- local column = tonumber(tArgs[1])
- local row = tonumber(tArgs[2])
- -- check arguments.
- if type(column) ~= 'number' or type(row) ~= 'number' then
- print('Wrong arguments. Only numbers accepted.')
- return
- end
- -- define vars.
- local saplingSlot = 1
- local refuelLevel = 5000
- local waitInterval = 3
- local waitDif = 0
- -- create move instance.
- dofile("move")
- local moveObj, error_msg = Move.new()
- if moveObj == false then
- print("Something wrong. Can't create Move obj.")
- print(error_msg)
- return
- end
- ----------
- -- Functions.
- ----------
- function dig(dir)
- local currentDig = turtle.dig
- local currentDetect = turtle.detect
- if dir == 'up' then
- currentDig = turtle.digUp
- currentDetect = turtle.detectUp
- elseif dir == 'down' then
- currentDig = turtle.digDown
- currentDetect = turtle.detectDown
- end
- local times = 0
- while currentDetect() do
- currentDig()
- times = times + 1
- if times == 20 then
- return false
- end
- sleep(1)
- end
- return true
- end
- function digAndMove(times)
- for i=1, times, 1 do
- dig('up')
- dig('down')
- moveObj:moveSure('front')
- end
- end
- function cutSingle()
- moveObj:moveSure('up')
- moveObj:moveSure('up')
- digAndMove(1)
- moveObj:turnLeft()
- digAndMove(2)
- moveObj:turnLeft()
- digAndMove(2)
- moveObj:turnLeft()
- digAndMove(3)
- moveObj:turnLeft()
- digAndMove(3)
- moveObj:turnLeft()
- digAndMove(4)
- moveObj:turnLeft()
- digAndMove(4)
- moveObj:turnLeft()
- digAndMove(4)
- dig('down')
- moveObj:moveSure('up')
- moveObj:moveSure('up')
- moveObj:moveSure('up')
- moveObj:turnLeft()
- digAndMove(4)
- moveObj:turnLeft()
- digAndMove(4)
- moveObj:turnLeft()
- digAndMove(4)
- moveObj:turnLeft()
- digAndMove(3)
- moveObj:turnLeft()
- digAndMove(3)
- moveObj:turnLeft()
- digAndMove(2)
- moveObj:turnLeft()
- digAndMove(2)
- moveObj:turnLeft()
- digAndMove(1)
- moveObj:turnLeft()
- digAndMove(1)
- dig('up')
- for i=1, 5, 1 do
- moveObj:moveSure('down')
- end
- moveObj:turnRight()
- moveObj:moveSure('front')
- moveObj:turnRight()
- moveObj:turnRight()
- turtle.select(saplingSlot)
- local times = 0
- while turtle.place() == false do
- times = times + 1
- if times == 20 then
- break
- end
- sleep(1)
- end
- turtle.select(1)
- moveObj:turnRight()
- end
- function turn(dir)
- local currentFunc
- if dir%2 == 0 then
- moveObj:turnRight()
- else
- moveObj:turnLeft()
- end
- end
- local function boolToNum(bool)
- if bool == true then
- return 1
- else
- return 0
- end
- end
- function drop()
- moveObj:turnLeft()
- local res = 1
- for i=2, 16, 1 do
- if turtle.getItemCount(i) ~= 0 then
- turtle.select(i)
- res = res * boolToNum(turtle.drop())
- end
- end
- turtle.select(1)
- moveObj:turnRight()
- if res == 0 then
- return false
- else
- return true
- end
- end
- function refuel()
- if turtle.getFuelLevel() < refuelLevel then
- while turtle.suckDown() do
- end
- for i=2, 16, 1 do
- turtle.select(i)
- turtle.refuel()
- end
- turtle.select(1)
- if turtle.getFuelLevel() < refuelLevel then
- return false
- else
- return true
- end
- else
- return true
- end
- end
- function getTime()
- local hour, min = math.modf(os.time())
- min = math.floor( min * 60 )
- local time = os.day()..":"..hour..":"..min
- return time
- end
- function getWaitTime(interval, givenDif)
- local dif = givenDif or 0
- local hour, min = math.modf(os.time())
- local totalTime = os.day()*24 + hour + 12*dif
- local residue = (12*interval) - (totalTime % (12*interval))
- return residue * 50
- end
- function updateScreen(message, startTime)
- term.scroll(1)
- term.setCursorPos(1,1)
- term.clearLine()
- term.write("Wood Cutter 2")
- term.setCursorPos(1,2)
- term.clearLine()
- term.setCursorPos(1,3)
- term.clearLine()
- term.write("|------------------------------|")
- term.setCursorPos(1,11)
- term.clearLine()
- local str1 = "|"..startTime.." -> "..getTime()
- for i=1, 35 - string.len(str1), 1 do
- str1 = str1.." "
- end
- term.write(str1.."|")
- term.setCursorPos(1,12)
- term.clearLine()
- local str2 = "|"..message
- for i=1, 35 - string.len(str2), 1 do
- str2 = str2.." "
- end
- term.write(str2.."|")
- term.setCursorPos(1,13)
- term.clearLine()
- term.write("|------------------------------|")
- end
- ----------
- -- Main Thread.
- ----------
- moveObj:returnToIniPointSure()
- sleep(getWaitTime(waitInterval, waitDif))
- while true do
- local startTime = getTime()
- for i=1, 3, 1 do
- moveObj:moveSure('front')
- end
- for i=1, column-1, 1 do
- -- print('start line'..i..'.')
- for j=1, row-1, 1 do
- cutSingle()
- for k=1, 5, 1 do
- moveObj:moveSure('front')
- end
- end
- cutSingle()
- moveObj:moveSure('front')
- turn(i)
- for k=1, 3+(i%2*4), 1 do
- moveObj:moveSure('front')
- end
- turn(i)
- moveObj:moveSure('front')
- end
- for i=1, row-1, 1 do
- cutSingle()
- for j=1, 5, 1 do
- moveObj:moveSure('front')
- end
- end
- cutSingle()
- moveObj:moveSure('front')
- moveObj:returnToIniPointSure()
- while drop() == false do
- updateScreen("Can't drop. Wating.", getTime())
- sleep(3600)
- end
- while refuel() == false do
- updateScreen("Can't refuel. Wating.", getTime())
- sleep(3600)
- end
- updateScreen('Finish cutting.', startTime)
- sleep(getWaitTime(waitInterval, waitDif))
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement