SHOW:
|
|
- or go back to the newest paste.
| 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 | local fuelcheck = function() | |
| 140 | fuellevel = turtle.getFuelLevel() | |
| 141 | dis = tArgs[2] | |
| 142 | if tArgs[2] > fuellevel then | |
| 143 | print("The turtle does not have enough fuel to move")
| |
| 144 | do return end | |
| 145 | end | |
| 146 | end | |
| 147 | ||
| 148 | --Argument Handling | |
| 149 | if tArgs[1] ~= nil then | |
| 150 | if #tArgs ~= 2 then | |
| 151 | printUsage(1) | |
| 152 | elseif not hasval(dirlist, tArgs[1]) then | |
| 153 | printUsage(2) | |
| 154 | elseif tArgs[2] == nil then | |
| 155 | printUsage(3) | |
| 156 | elseif not tonumber(tArgs[2]) then | |
| 157 | printUsage(4) | |
| 158 | elseif tonumber(tArgs[2]) < 0 then | |
| 159 | printUsage(5) | |
| 160 | else | |
| 161 | local Dir = tArgs[1] | |
| 162 | local Dis = tArgs[2] | |
| 163 | turtlemove(Dir,Dis) | |
| 164 | return | |
| 165 | end | |
| 166 | else | |
| 167 | printUsage(0) | |
| 168 | end | |
| 169 | -- |