Guest User

move.lua

a guest
Feb 23rd, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.10 KB | None | 0 0
  1. --Movement Program by Galygious
  2. --Variables
  3.     local tArgs = { ... }  
  4.     local dirlist = {'f','b','d','u','l','r'}
  5. --Functions
  6.    
  7.     local tturn = function(tdir,tcount)
  8.         shell.run('/progs/tturn',tdir)
  9.     end
  10.    
  11.     local printUsage = function(ecode)
  12.         if ecode == 0 then
  13.             print('Error Code 0\nThis Program Requires these variables:\narg1 - direction (f,b,d,u,l,r)\narg2 - distance (positive number)')
  14.             do return end
  15.         elseif ecode == 1 then
  16.             print('Error Code 1\nThis Program Requires these variables:\narg1 - direction (f,b,d,u,l,r)\narg2 - distance (positive number)')
  17.             do return end
  18.         elseif ecode == 2 then
  19.             print('Error Code 2\nFirst argument must represent a direction (f,b,d,u,l,r)')
  20.             do return end
  21.         elseif ecode == 3 then
  22.             print('Error Code 3\nSecond argument must represent a distance (positive number)')
  23.             do return end
  24.         elseif ecode == 4 then
  25.             print('Error Code 4\nSecond argument must represent a distance (positive number)')
  26.             do return end
  27.         elseif ecode == 5 then
  28.             print('Error Code 4\nSecond argument must represent a distance (positive number)')
  29.             do return end
  30.         end
  31.     end
  32.    
  33.     local function moveForward()
  34.       unbreakable = false
  35.       while not turtle.forward() do
  36.             --# check if a block was the cause
  37.             if turtle.detect() then
  38.               if not turtle.dig() then
  39.                 --unbreakable
  40.                print("An unbreakable block has been encountered, bedrock is that you?")
  41.                error()
  42.               elseif turtle.detect() then
  43.                 --Gravel
  44.                     sleep(0.8)
  45.               end
  46.             --# check if fuel was the cause
  47.             elseif turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() == 0 then
  48.               print("I am out of fuel... HELP!")
  49.               while turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() == 0 do
  50.                 turtle.refuel()
  51.                 sleep(1)
  52.               end
  53.             elseif turtle.attack() then
  54.                 --hit mob
  55.             end
  56.       end
  57.     end
  58.    
  59.     local function moveUp()
  60.       while not turtle.up() do
  61.             --# check if a block was the cause
  62.             if turtle.detectUp() then
  63.               if not turtle.digUp() then
  64.               elseif turtle.detectUp() then
  65.                     print("Sand/Gravel is falling!")
  66.                     sleep(0.8)
  67.               else
  68.                     print("A block was in my way")
  69.               end
  70.             --# check if fuel was the cause
  71.             elseif turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() == 0 then
  72.               print("I am out of fuel... HELP!")
  73.             elseif turtle.attackUp() then
  74.               print("Get out of my way mob/player!")
  75.             end
  76.       end
  77.     end
  78.    
  79.     local function moveDown()
  80.       while not turtle.down() do
  81.             --# check if a block was the cause
  82.             if turtle.detectDown() then
  83.               if not turtle.digDown() then
  84.               elseif turtle.detectDown() then
  85.                     print("Sand/Gravel is falling!")
  86.                     sleep(0.8)
  87.               else
  88.                     print("A block was in my way")
  89.               end
  90.             --# check if fuel was the cause
  91.             elseif turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() == 0 then
  92.               print("I am out of fuel... HELP!")
  93.             elseif turtle.attackDown() then
  94.               print("Get out of my way mob/player!")
  95.             end
  96.       end
  97.     end
  98.    
  99.     local turtlemove = function(Dir,Dis)
  100.         if Dir == 'f' then
  101.             for i=1,Dis do
  102.                 moveForward()
  103.             end
  104.         elseif Dir == 'l' then
  105.             tturn('l')
  106.             for i=1,Dis do
  107.                 moveForward()
  108.             end
  109.         elseif Dir == 'r' then
  110.             tturn('r')
  111.             for i=1,Dis do
  112.                 moveForward()
  113.             end
  114.         elseif Dir == 'b' then
  115.             tturn('u')
  116.             for i=1,Dis do
  117.                 moveForward()
  118.             end
  119.         elseif Dir == 'u' then
  120.             for i=1,Dis do
  121.                 moveUp()
  122.             end
  123.         elseif Dir == 'd' then
  124.             for i=1,Dis do
  125.                 moveDown()
  126.             end
  127.         end
  128.     end
  129.    
  130.     local hasval = function(tab,val)
  131.         for i, value in ipairs(tab) do
  132.             if value == val then
  133.                 return true
  134.             end
  135.         end
  136.         return false
  137.     end
  138.    
  139. --Argument Handling
  140.     if tArgs[1] ~= nil then
  141.         if #tArgs ~= 2 then
  142.             printUsage(1)
  143.         elseif not hasval(dirlist, tArgs[1]) then
  144.             printUsage(2)
  145.         elseif tArgs[2] == nil then
  146.             printUsage(3)
  147.         elseif not tonumber(tArgs[2]) then
  148.             printUsage(4)
  149.         elseif tonumber(tArgs[2]) < 0 then
  150.             printUsage(5)
  151.         else
  152.             local Dir = tArgs[1]
  153.             local Dis = tArgs[2]
  154.             turtlemove(Dir,Dis)
  155.             return
  156.         end
  157.     else
  158.         printUsage(0)
  159.     end
  160. --
Advertisement
Add Comment
Please, Sign In to add comment