Advertisement
MoonlightOwl

Dwarf v0.1.0

May 21st, 2017
695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.50 KB | None | 0 0
  1. -- Dwarf - THE BEST ULTIMATE DIGGER EVER
  2. --        ---   v0.1.0    ---
  3. --               * * *
  4. -- I am a dwarf, and I'm digging a hole!
  5. -- Diggy diggy hole, diggy diggy hole!
  6. --               * * *
  7.  
  8. local robot = require("robot")
  9. local sides = require("sides")
  10. local com = require("component")
  11. local r = com.robot
  12.  
  13. -- parse commandline arguments
  14. local args = {...}
  15. local WIDTH, HEIGHT, DEPTH = tonumber(args[1]), tonumber(args[2]), tonumber(args[3])
  16. if type(WIDTH) ~= "number" or type(HEIGHT) ~= "number" or type(DEPTH) ~= "number" then
  17.     print("SYNTAX: dwarf <width> <height> <depth>")
  18.   os.exit()
  19. end
  20.  
  21. -- failsafe navigation
  22. local pos = { x = 0, y = 0, z = 0, t = 0 }
  23. function pos.forward()
  24.   if pos.t == 0 then pos.x = pos.x + 1
  25.   elseif pos.t == 1 then pos.z = pos.z + 1
  26.   elseif pos.t == 2 then pos.x = pos.x - 1
  27.   else pos.z = pos.z - 1 end
  28. end
  29. function pos.up() pos.y = pos.y + 1 end
  30. function pos.down() pos.y = pos.y - 1 end
  31. function pos.move(side)
  32.   if side == sides.forward then pos.forward(); return true
  33.   elseif side == sides.up then pos.up(); return true
  34.   elseif side == sides.down then pos.down(); return true
  35.   else return false end
  36. end
  37. function pos.left() pos.t = (pos.t + 3) % 4 end
  38. function pos.around() pos.t = (pos.t + 2) % 4 end
  39. function pos.right() pos.t = (pos.t + 1) % 4 end
  40. function pos.reset() pos.x = 0; pos.y = 0; pos.z = 0; pos.t = 0 end
  41.  
  42. local function left()
  43.   robot.turnLeft()
  44.   pos.left()
  45. end
  46. local function around()
  47.   robot.turnAround()
  48.   pos.around()
  49. end
  50. local function right()
  51.   robot.turnRight()
  52.   pos.right()
  53. end
  54.  
  55. -- digging mode
  56. local mode, VOID, SOLID = nil, {}, {}
  57. function VOID.move(side)
  58.   if r.move(side) then pos.move(side); return true
  59.   else mode = SOLID end
  60.   return false
  61. end
  62. function SOLID.move(side)
  63.   if r.swing(side) then
  64.     if r.move(side) then pos.move(side); return true end
  65.   else mode = VOID end
  66.   return false
  67. end
  68.  
  69. local function forward()
  70.   while not mode.move(sides.forward) do end
  71. end
  72. local function up()
  73.   while not mode.move(sides.up) do end
  74. end
  75. local function down()
  76.   while not mode.move(sides.down) do end
  77. end
  78.  
  79. -- go!
  80. mode = SOLID
  81.  
  82. local a, b = true, true
  83. while true do
  84.   for x = 1, DEPTH - 1 do forward(); os.sleep(0.1) end
  85.   if (b and pos.z < WIDTH - 1) or (not b and pos.z > 0)  then
  86.     if a then right(); forward(); right()
  87.     else left(); forward(); left() end
  88.     a = not a
  89.   else
  90.     if pos.y > -HEIGHT + 1 then
  91.       down(); around(); b = not b
  92.     else break end
  93.   end
  94. end
  95.  
  96. -- the end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement