Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ---------------------------------------------------------
- -- config
- -- ---------------------------------------------------------
- local HEIGHT = -4 -- even number only
- local DEPTH = 4 -- even number only
- -- ---------------------------------------------------------
- -- utilities
- -- ---------------------------------------------------------
- local function fileReadAll(filePath)
- local hFile = fs.open(filePath, "r")
- local txt = hFile.readAll()
- hFile.close()
- return txt
- end
- local function fileOverwrite(fileName, text)
- local hFile = fs.open(fileName, "w")
- hFile.writeLine(text)
- hFile.close()
- end
- -- ---------------------------------------------------------
- -- application
- -- ---------------------------------------------------------
- local ARMSG_INIT = 1
- local ARMSG_SIGNAL = 2
- local ARMSG_GO_HOME = 3
- local ARMSG_HALF = 4
- local status = { depth = 0, height = 0 }
- -- -----------------------------
- -- app utils
- -- -----------------------------
- local STATUS_FILENAME = "tinywm.status"
- local function loadStatus()
- local staTmp = fileReadAll(STATUS_FILENAME)
- status = textutils.unserialize(staTmp)
- end
- local function saveStatus()
- fileOverwrite(STATUS_FILENAME, textutils.serialize(status))
- end
- -- -----------------------------
- -- cutTail
- -- -----------------------------
- local function createCutTailFunc(holizontal, suckFunc, digFunc)
- return function()
- if holizontal then
- turtle.turnLeft()
- turtle.turnLeft()
- end
- for i = 1, 16 do
- suckFunc()
- end
- for i = 1, 16 do
- if 0 == turtle.getItemCount(i) then
- notFound = false
- turtle.select(1)
- turtle.transferTo(i)
- break
- end
- end
- if notFound then
- turtle.select(1)
- turtle.drop()
- end
- turtle.select(1)
- digFunc()
- if holizontal then
- turtle.turnLeft()
- turtle.turnLeft()
- end
- end
- end
- local cutTail = createCutTailFunc(true , turtle.suck , turtle.dig )
- local cutTailTop = createCutTailFunc(false, turtle.suckUp , turtle.digUp )
- local cutTailBottom = createCutTailFunc(false, turtle.suckDown, turtle.digDown)
- -- -----------------------------
- -- go
- -- -----------------------------
- local function createGoFunc(cutTailFunc, digFunc, dir, placeFunc, arMsg, debugMsg)
- return function(half)
- while digFunc() do end
- if half then
- rs.setAnalogOutput(dir, arMsg + ARMSG_HALF)
- else
- rs.setAnalogOutput(dir, arMsg)
- end
- turtle.select(1)
- while not placeFunc() do end
- peripheral.call(dir, "turnOn")
- end
- end
- local goForward = createGoFunc(cutTail , turtle.dig ,"front" , turtle.place , ARMSG_SIGNAL , "goForward()")
- local goDown = createGoFunc(cutTailTop , turtle.digDown,"bottom", turtle.placeDown, ARMSG_SIGNAL , "goDown()")
- local goUp = createGoFunc(cutTailBottom, turtle.digUp ,"top" , turtle.placeUp , ARMSG_SIGNAL , "goUp()")
- local goHome = createGoFunc(cutTail , turtle.dig ,"front" , turtle.place , ARMSG_GO_HOME, "goHome()")
- -- -----------------------------
- -- main
- -- -----------------------------
- local args = {...}
- if #args == 0 then
- local backInput = rs.getAnalogInput("back")
- local topInput = rs.getAnalogInput("top")
- local bottomInput = rs.getAnalogInput("bottom")
- local frontInput = rs.getAnalogInput("front")
- if 0 < frontInput then
- turtle.turnLeft()
- turtle.turnLeft()
- backInput = frontInput
- end
- if backInput == ARMSG_INIT then
- saveStatus()
- end
- if backInput == ARMSG_INIT or
- backInput == ARMSG_SIGNAL then
- loadStatus()
- if backInput == ARMSG_INIT then
- status.depth = 1
- elseif backInput == ARMSG_SIGNAL then
- status.depth = status.depth + 2
- end
- saveStatus()
- cutTail()
- if status.depth == DEPTH then
- goDown(true)
- else
- goForward(false)
- end
- elseif topInput == ARMSG_SIGNAL or
- topInput == ARMSG_SIGNAL + ARMSG_HALF then
- loadStatus()
- if topInput == ARMSG_SIGNAL + ARMSG_HALF then
- status.height = status.height - 1
- else
- status.height = status.height - 2
- end
- saveStatus()
- cutTailTop()
- for i = 1, 4 do
- turtle.dig()
- turtle.turnLeft()
- end
- if status.height == HEIGHT then
- goUp(true)
- else
- goDown(false)
- end
- elseif bottomInput == ARMSG_SIGNAL or
- bottomInput == ARMSG_SIGNAL + ARMSG_HALF then
- loadStatus()
- if topInput == ARMSG_SIGNAL + ARMSG_HALF then
- status.height = status.height + 1
- else
- status.height = status.height + 2
- end
- saveStatus()
- cutTailBottom()
- if status.height == 0 then
- turtle.turnLeft()
- turtle.turnLeft()
- goHome(true)
- else
- goUp(false)
- end
- elseif backInput == ARMSG_GO_HOME or
- backInput == ARMSG_GO_HOME + ARMSG_HALF then
- loadStatus()
- if topInput == ARMSG_GO_HOME + ARMSG_HALF then
- status.depth = status.depth - 1
- else
- status.depth = status.depth - 2
- end
- saveStatus()
- cutTail()
- if 0 == status.depth then
- turtle.turnLeft()
- turtle.turnLeft()
- print("finished.")
- else
- goHome(false)
- end
- else
- error("no signal.")
- end
- elseif #args == 1 and "-start" == args[1] then
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.select(1)
- turtle.dig()
- turtle.turnLeft()
- turtle.turnLeft()
- saveStatus()
- turtle.select(1)
- rs.setAnalogOutput("front", ARMSG_INIT)
- turtle.place()
- peripheral.call("front", "turnOn")
- else
- error("bad args")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement