Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- revision = "0.0.2b"
- -- logging
- function dump(o)
- if type(o) == 'table' then
- local s = '{ \n';
- for k,v in pairs(o) do
- s = s .. " " .. k ..': ' .. dump(v) .. ',\n';
- end
- return s .. '}';
- else
- return tostring(o);
- end
- end
- logToFile = false -- indicates the neccessarity to log to file
- function err(s)
- if logToFile then
- file.write(os.date() .. " [ERR] " .. s)
- end
- print(os.date() .. " [ERR] " .. s)
- end
- function info(s)
- if logToFile then
- file.write(os.date() .. " [INFO] " .. s)
- end
- print(os.date() .. " [INFO] " .. s)
- end
- function warn(s)
- if logToFile then
- file.write(os.date() .. " [WARN] " .. s)
- end
- print(os.date() .. " [WARN] " .. s)
- end
- function debug(s)
- if logToFile then
- file.write(os.date() .. " [DEBUG] " .. s)
- end
- print(os.date() .. " [DEBUG] " .. s)
- end
- -- common functions
- function try(--[[required]]fn, --[[optional]]fnArgs, --[[optional]]fnName, --[[optional]]args, --[[optional]]maySkip, --[[optional]]errFn)
- if fnName == nil then
- fnName = "unknown()"
- end
- fnName = fnName .. ": "
- if maySkip == nil then
- maySkip = false
- end
- if errFn == nil then
- errFn = err
- end
- local scs = false
- repeat
- scs = fn(fnArgs)
- if not scs then
- errFn(fnName .. ", fnArgs: " .. dump(fnArgs) .. ", args: " .. dump(args))
- end
- until(scs or maySkip)
- end
- -- program itself
- function digRoom(length, width, height)
- for h=1, height do
- digPlane(length, width)
- returnToStartPoint(length, width)
- if needToDrop() then
- for hh = 1, h do
- turtle.up()
- end
- try(dropUpLoot, nil, "dropUpLoot()", nil, true, warn)
- for hh = 1, h do
- turtle.down()
- end
- end
- if h < height then
- turtle.digDown()
- turtle.down()
- end
- end
- end
- function digPlane(length, width)
- for w = 1, width do
- for l = 1, length-1 do
- -- turtle.dig()
- try(turtle.dig, nil, "turtle.dig()", {currL = l, currW = w}, true, warn)
- try(turtle.forward, nil, "turtle.forward()", {currL = l, currW = w})
- end
- if (w % 2 ~= 0) and (w < width) then
- turtle.turnLeft()
- -- turtle.dig()
- try(turtle.dig, nil, "turtle.dig(turning)", {currW = w}, true, warn)
- try(turtle.forward, nil, "turtle.forward(turning)", {currW = w})
- turtle.turnLeft()
- elseif (w < width) then
- turtle.turnRight()
- -- turtle.dig()
- try(turtle.dig, nil, "turtle.dig(turning)", {currW = w}, true, warn)
- try(turtle.forward, nil, "turtle.forward(turning)", {currW = w})
- turtle.turnRight()
- end
- end
- end
- function dropUpLoot()
- if not turtle.detectUp() then
- return false
- end
- for i = 1, 16 do
- turtle.select(i)
- turtle.dropUp()
- end
- return true
- end
- function needToDrop()
- sum = 0
- for i = 1, 16 do
- turtle.select(i)
- if turtle.getItemCount() <= 0 then
- sum = sum + 1
- end
- end
- return sum <= 3
- end
- -- todo: rewrite
- function returnToStartPoint(length, width)
- if width % 2 == 0 then
- turtle.turnLeft()
- for w = 1, width-1 do
- try(turtle.forward, nil, "turtle.forward(returning)", {currW = w})
- end
- turtle.turnLeft()
- return
- end
- turtle.turnRight()
- for w = 1, width-1 do
- try(turtle.forward, nil, "turtle.forward(returning)", {currW = w})
- end
- turtle.turnRight()
- for l = 1, length-1 do
- try(turtle.forward, nil, "turtle.forward(returning)", {currL = l})
- end
- turtle.turnRight()
- turtle.turnRight()
- end
- function usage()
- print("roomdigger by @semior001 https://github.com/semior001")
- print("usage: rd length width height [logToFile]")
- end
- function main(args)
- if #args < 3 then
- usage()
- return
- end
- if #args == 4 then
- logToFile = args[4] == "true"
- end
- logToFile = true
- if logToFile then
- file = fs.open("roomdigger.log","w")
- end
- -- procedures call
- info("roomdigger, revision " .. revision .. " - started program execution")
- local length = tonumber(args[1])
- local width = tonumber(args[2])
- local height = tonumber(args[3])
- -- checking fuel
- initFuel = turtle.getFuelLevel()
- requiredFuelAmount = length * width * height + (width-1 + length-1) * height
- if initFuel < requiredFuelAmount then
- neededFuel = requiredFuelAmount - initFuel
- print("not enough fuel, needed " .. neededFuel .. " more fuel")
- return
- end
- digRoom(length, width, height)
- currFuel = turtle.getFuelLevel()
- spentFuel = initFuel - currFuel
- print("spent " .. spentFuel)
- print(currFuel .. " fuel left")
- if logToFile then
- file.close()
- end
- end
- local args = { ... }
- main(args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement