Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Dwarf - THE BEST ULTIMATE DIGGER EVER
- -- --- v0.1.0 ---
- -- * * *
- -- I am a dwarf, and I'm digging a hole!
- -- Diggy diggy hole, diggy diggy hole!
- -- * * *
- local robot = require("robot")
- local sides = require("sides")
- local com = require("component")
- local r = com.robot
- -- parse commandline arguments
- local args = {...}
- local WIDTH, HEIGHT, DEPTH = tonumber(args[1]), tonumber(args[2]), tonumber(args[3])
- if type(WIDTH) ~= "number" or type(HEIGHT) ~= "number" or type(DEPTH) ~= "number" then
- print("SYNTAX: dwarf <width> <height> <depth>")
- os.exit()
- end
- -- failsafe navigation
- local pos = { x = 0, y = 0, z = 0, t = 0 }
- function pos.forward()
- if pos.t == 0 then pos.x = pos.x + 1
- elseif pos.t == 1 then pos.z = pos.z + 1
- elseif pos.t == 2 then pos.x = pos.x - 1
- else pos.z = pos.z - 1 end
- end
- function pos.up() pos.y = pos.y + 1 end
- function pos.down() pos.y = pos.y - 1 end
- function pos.move(side)
- if side == sides.forward then pos.forward(); return true
- elseif side == sides.up then pos.up(); return true
- elseif side == sides.down then pos.down(); return true
- else return false end
- end
- function pos.left() pos.t = (pos.t + 3) % 4 end
- function pos.around() pos.t = (pos.t + 2) % 4 end
- function pos.right() pos.t = (pos.t + 1) % 4 end
- function pos.reset() pos.x = 0; pos.y = 0; pos.z = 0; pos.t = 0 end
- local function left()
- robot.turnLeft()
- pos.left()
- end
- local function around()
- robot.turnAround()
- pos.around()
- end
- local function right()
- robot.turnRight()
- pos.right()
- end
- -- digging mode
- local mode, VOID, SOLID = nil, {}, {}
- function VOID.move(side)
- if r.move(side) then pos.move(side); return true
- else mode = SOLID end
- return false
- end
- function SOLID.move(side)
- if r.swing(side) then
- if r.move(side) then pos.move(side); return true end
- else mode = VOID end
- return false
- end
- local function forward()
- while not mode.move(sides.forward) do end
- end
- local function up()
- while not mode.move(sides.up) do end
- end
- local function down()
- while not mode.move(sides.down) do end
- end
- -- go!
- mode = SOLID
- local a, b = true, true
- while true do
- for x = 1, DEPTH - 1 do forward(); os.sleep(0.1) end
- if (b and pos.z < WIDTH - 1) or (not b and pos.z > 0) then
- if a then right(); forward(); right()
- else left(); forward(); left() end
- a = not a
- else
- if pos.y > -HEIGHT + 1 then
- down(); around(); b = not b
- else break end
- end
- end
- -- the end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement