Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- TURTLE v0.13 Beta
- --
- -- By Galbi3000
- --
- -- A command prompt to directly control turtles or run scripts of the turtle commands.
- --
- -- Usage:
- --
- -- Type 'turtle' at the TurtleOS command prompt to enter the turtle prompt mode.
- --
- -- Alternatively type 'turtle <name>' to run a turtle script and return to the TurtleOS
- -- prompt when it finishes.
- --
- local version = "0.13 beta"
- local tArgs = { ... }
- if #tArgs > 1 then
- print( "Usage: turtle [scriptName]" )
- return
- end
- -- Variables used by all functions:
- local tab = string.char(9)
- local tCommandHistory = {} -- For repeating commands already typed
- local advanced = false -- Indicates an advanced turtle for colour display
- local running = false -- Set to true before the main loop and stays true until the exit command is used
- local inScript = false -- Set to true if a script is being run
- local errorText = "" -- Used for command errors in a script
- local tCommandValue = {
- ["forward"] = 1,
- ["forwards"] = 1,
- ["for"] = 1,
- ["fwd"] = 1,
- ["fd"] = 1,
- ["fo"] = 1,
- ["f"] = 1,
- ["back"] = 2,
- ["backward"] = 2,
- ["backwards"] = 2,
- ["bk"] = 2,
- ["ba"] = 2,
- ["b"] = 2,
- ["up"] = 3,
- ["upward"] = 3,
- ["upwards"] = 3,
- ["u"] = 3,
- ["rise"] = 3,
- ["down"] = 4,
- ["downward"] = 4,
- ["downwards"] = 4,
- ["dn"] = 4,
- ["do"] = 4,
- ["d"] = 4,
- ["lower"] = 4,
- ["lwr"] = 4,
- ["fall"] = 4,
- ["left"] = 5,
- ["lft"] = 5,
- ["lt"] = 5,
- ["le"] = 5,
- ["l"] = 5,
- ["right"] = 6,
- ["rt"] = 6,
- ["ri"] = 6,
- ["r"] = 6,
- ["dig"] = 7,
- ["dg"] = 7,
- ["di"] = 7,
- ["place"] = 8,
- ["pl"] = 8,
- ["p"] = 8,
- ["drop"] = 9,
- ["drp"] = 9,
- ["dp"] = 9,
- ["dr"] = 9,
- ["suck"] = 10,
- ["sk"] = 10,
- ["su"] = 10,
- ["get"] = 10,
- ["gt"] = 10,
- ["ge"] = 10,
- ["g"] = 10,
- ["select"] = 11,
- ["slct"] = 11,
- ["sel"] = 11,
- ["sl"] = 11,
- ["se"] = 11,
- ["s"] = 11,
- ["inventory"] = 11,
- ["inv"] = 11,
- ["in"] = 11,
- ["i"] = 11,
- ["detect"] = 12,
- ["dtct"] = 12,
- ["det"] = 12,
- ["dt"] = 12,
- ["de"] = 12,
- ["sense"] = 12,
- ["sen"] = 12,
- ["attack"] = 13,
- ["atk"] = 13,
- ["att"] = 13,
- ["at"] = 13,
- ["a"] = 13,
- ["hit"] = 13,
- ["ht"] = 13,
- ["h"] = 13,
- ["equip"] = 14,
- ["eqp"] = 14,
- ["eq"] = 14,
- ["e"] = 14,
- ["use"] = 14,
- ["refuel"] = 15,
- ["ref"] = 15,
- ["rfl"] = 15,
- ["rf"] = 15,
- ["fuel"] = 16,
- ["fl"] = 16,
- ["fu"] = 16,
- ["run"] = 17,
- ["exit"] = 18,
- ["quit"] = 18,
- ["ex"] = 18,
- ["x"] = 18,
- ["qt"] = 18,
- ["q"] = 18,
- ["end"] = 18,
- ["edit"] = 19,
- ["ed"] = 19,
- ["list"] = 20,
- ["ls"] = 20,
- ["dir"] = 20,
- ["files"] = 20,
- ["scripts"] = 20,
- ["repeat"] = 21,
- ["rpt"] = 21,
- ["rp"] = 21,
- ["re"] = 21,
- ["print"] = 22,
- ["echo"] = 22,
- ["write"] = 22,
- ["say"] = 22,
- ["pr"] = 22,
- ["ec"] = 22,
- ["wr"] = 22,
- }
- local tDirection = {
- ["ahead"] = 1,
- ["forward"] = 1,
- ["forwards"] = 1,
- ["for"] = 1,
- ["fwd"] = 1,
- ["fd"] = 1,
- ["fo"] = 1,
- ["front"] = 1,
- ["fr"] = 1,
- ["a"] = 1,
- ["f"] = 1,
- ["up"] = 2,
- ["u"] = 2,
- ["above"] = 2,
- ["top"] = 2,
- ["t"] = 2,
- ["down"] = 3,
- ["d"] = 3,
- ["below"] = 3,
- ["bottom"] = 3,
- ["b"] = 3,
- }
- local tSide = {
- ["left"] = 1,
- ["lft"] = 1,
- ["lt"] = 1,
- ["le"] = 1,
- ["l"] = 1,
- ["right"] = 2,
- ["rt"] = 2,
- ["ri"] = 2,
- ["r"] = 2,
- }
- -- Functions:
- local function separate(line)
- -- This function splits the line into 2 lines at the first space.
- -- Usually for separating the command from it's parameter but also in the DROP command to get multiple
- -- parameters
- local i = 1
- while string.sub(line, i, i) ~= " " and i < string.len(line) do
- i = i + 1
- end
- local first = ""
- local second = ""
- if i == string.len(line) then
- -- No spaces so the whole line is returned as the first part
- first = line..""
- else
- -- Space found so copy the first part
- first = string.sub(line, 1, i - 1)..""
- -- Now make sure there are no leading spaces for the parameter
- while string.sub(line, i, i) == " " do
- i = i + 1
- end
- -- Now copy the second part as the rest of the line
- second = string.sub(line, i)..""
- end
- return first, second
- end
- local function strip(line)
- -- This function strips a string of leading/trailing spaces or tabs.
- -- Tabs are checked in case a script file was created outside of Minecraft with a text editor.
- local nStart = 1
- local nEnd = string.len(line)
- while string.sub(line, nStart, nStart) == " " or string.sub(line, nStart, nStart) == tab do
- nStart = nStart + 1
- end
- while string.sub(line, nEnd, nEnd) == " " or string.sub(line, nEnd, nEnd) == tab do
- nEnd = nEnd - 1
- end
- return string.sub(line, nStart, nEnd)..""
- end
- local function forward(dist)
- -- This function moves the turtle forward by 1 or by the distance specified.
- if dist == nil or dist == 1 then return turtle.forward() end
- for i = 1, dist do
- if not turtle.forward() then
- return false
- end
- end
- return true
- end
- local function back(dist)
- -- This function moves the turtle back by 1 or by the distance specified.
- if dist == nil or dist == 1 then return turtle.back() end
- for i = 1, dist do
- if not turtle.back() then
- return false
- end
- end
- return true
- end
- local function up(dist)
- -- This function moves the turtle up by 1 or by the distance specified.
- if dist == nil or dist == 1 then return turtle.up() end
- for i = 1, dist do
- if not turtle.up() then
- return false
- end
- end
- return true
- end
- local function down(dist)
- -- This function moves the turtle down by 1 or by the distance specified.
- if dist == nil or dist == 1 then return turtle.down() end
- for i = 1, dist do
- if not turtle.down() then
- return false
- end
- end
- return true
- end
- local function left(turns)
- -- This function turns the turtle left by 1 or by the number specified.
- if turns == nil or turns == 1 then return turtle.turnLeft() end
- for i = 1, turns do
- turtle.turnLeft()
- end
- return true
- end
- local function right(turns)
- -- This function turns the turtle right by 1 or by the number specified.
- if turns == nil or turns == 1 then return turtle.turnRight() end
- for i = 1, turns do
- turtle.turnRight()
- end
- return true
- end
- local function runCommand(command, param)
- -- This function will run a command given to it by either parse or runScript
- -- 'command' is the command number (currently 1-17)
- -- 'param' is the string entered after the command in the parsed line
- --
- -- There are 2 types of error. Critical errors are shown in red on an advanced turtle.
- -- Non-critical are orange.
- -- When in a script the non-critical are ignored but the critical errors stop the script.
- -- An alternative less friendly error message is displayed in script errors!
- -- FORWARD command
- if command == tCommandValue["forward"] then
- if param == "" then param = "1" end
- if tonumber(param) == nil then
- -- Critical error
- if inScript then
- errorText = "Non numeric parameter given"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("Please specify how far numerically!")
- else
- local dist = tonumber(param)
- if turtle.getFuelLevel() < dist then
- -- Critical error
- if inScript then
- errorText = "Not enough fuel"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("Not enough fuel. Please refuel at least "..param.." units")
- else
- if not forward(dist) and not inScript then
- -- Non-critical error
- if advanced then term.setTextColour(colours.orange) end
- print("I seem to have run into something!")
- end
- end
- end
- -- BACK command
- elseif command == tCommandValue["back"] then
- if param == "" then param = "1" end
- if tonumber(param) == nil then
- -- Critical error
- if inScript then
- errorText = "Non numeric parameter given"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("Please specify how far numerically!")
- else
- local dist = tonumber(param)
- if turtle.getFuelLevel() < dist then
- -- Critical error
- if inScript then
- errorText = "Not enough fuel"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("Not enough fuel. Please refuel at least "..param.." units")
- else
- if not back(dist) and not inScript then
- -- Non-critical error
- if advanced then term.setTextColour(colours.orange) end
- print("I seem to have run into something!")
- end
- end
- end
- -- UP command
- elseif command == tCommandValue["up"] then
- if param == "" then param = "1" end
- if tonumber(param) == nil then
- -- Critical error
- if inScript then
- errorText = "Non numeric parameter given"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("Please specify how far numerically!")
- else
- local dist = tonumber(param)
- if turtle.getFuelLevel() < dist then
- -- Critical error
- if inScript then
- errorText = "Not enough fuel"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("Not enough fuel. Please refuel at least "..param.." units")
- else
- if not up(dist) and not inScript then
- -- Non-critical error
- if advanced then term.setTextColour(colours.orange) end
- print("I seem to have run into something!")
- end
- end
- end
- -- DOWN command
- elseif command == tCommandValue["down"] then
- if param == "" then param = "1" end
- if tonumber(param) == nil then
- -- Critical error
- if inScript then
- errorText = "Non numeric parameter given"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("Please specify how far numerically!")
- else
- local dist = tonumber(param)
- if turtle.getFuelLevel() < dist then
- -- Critical error
- if inScript then
- errorText = "Not enough fuel"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("Not enough fuel. Please refuel at least "..param.." units")
- else
- if not down(dist) and not inScript then
- -- Non-critical error
- if advanced then term.setTextColour(colours.orange) end
- print("I seem to have run into something!")
- end
- end
- end
- -- LEFT command
- elseif command == tCommandValue["left"] then
- if param == "" then param = "1" end
- if tonumber(param) == nil then
- -- Critical error
- if inScript then
- errorText = "Non numeric parameter given"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("Please specify how many turns numerically!")
- else
- left(tonumber(param))
- end
- -- RIGHT command
- elseif command == tCommandValue["right"] then
- if param == "" then param = "1" end
- if tonumber(param) == nil then
- -- Critical error
- if inScript then
- errorText = "Non numeric parameter given"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("Please specify how many turns numerically!")
- else
- right(tonumber(param))
- end
- -- DIG command
- elseif command == tCommandValue["dig"] then
- if param == "" then param = "ahead" end
- local ret = false
- if tDirection[param] == nil then
- -- Critical error
- if inScript then
- errorText = "Invalid direction provided"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("Please use a valid direction.")
- else
- if tDirection[param] == tDirection["ahead"] then
- ret = turtle.dig()
- elseif tDirection[param] == tDirection["up"] then
- ret = turtle.digUp()
- elseif tDirection[param] == tDirection["down"] then
- ret = turtle.digDown()
- else
- -- Critical error
- if inScript then
- errorText = "Invalid parameter in DROP"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("You used an invalid parameter!")
- end
- if not ret and not inScript then
- -- Non-critical error
- if advanced then term.setTextColour(colours.orange) end
- print("It seems I can't dig that up!")
- end
- end
- -- PLACE command
- elseif command == tCommandValue["place"] then
- if param == "" then param = "ahead" end
- local ret = false
- if tDirection[param] == nil then
- -- Critical error
- if inScript then
- errorText = "Invalid direction provided"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("Please use a valid direction.")
- else
- if tDirection[param] == tDirection["ahead"] then
- ret = turtle.place()
- elseif tDirection[param] == tDirection["up"] then
- ret = turtle.placeUp()
- elseif tDirection[param] == tDirection["down"] then
- ret = turtle.placeDown()
- else
- -- Critical error
- if inScript then
- errorText = "Invalid parameter in DROP"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("You used an invalid parameter!")
- end
- if not ret and not inScript then
- -- Non-critical error
- if advanced then term.setTextColour(colours.orange) end
- print("It seems I can't put that there!")
- end
- end
- -- DROP command
- -- This command was implemented late because it has the possibility of 2 parameters and I needed to think
- -- it through carefully how to proceed. The answer came when I created the separate function :)
- -- It is still more complex because it needs to be user friendly. The parameters do not have to be
- -- provided in a specific order! E.G. DROP DOWN ALL is the same as DROP ALL DOWN
- elseif command == tCommandValue["drop"] then
- local param1, param2 = separate(param)
- local direction = "ahead" -- Default direction if none specified
- local number = 0 -- This will default to 'all in current slot'
- local ret = false
- -- First get a direction if specified
- if tDirection[param2] ~= nil then direction = param2 end
- if tDirection[param1] ~= nil then
- if tDirection[param2] and not inSCript then
- -- Non-critical error
- if advanced then term.setTextColour(colours.orange) end
- print("Only one direction can be specified, using the first only!")
- end
- direction = param1
- end
- -- Now get how many items to drop, if specified
- local nParam1 = tonumber(param1)
- local nParam2 = tonumber(param2)
- if nParam2 ~= nil then number = nParam2 end
- if nParam1 ~= nil then
- if nParam2 and not inScript then
- -- Non-critical error
- if advanced then term.setTextColour(colours.orange) end
- print("Only one number can be specified, using the first only!")
- end
- number = nParam1
- end
- -- Now if ALL was used as a parameter
- if param1 == "all" or param2 == "all" then
- if number > 0 then
- -- Critical error
- if inScript then
- errorText = "'ALL' specified with a number in DROP!"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("If you specify a number as well as ALL then I do not know what to do!")
- return false
- else
- local curSel = turtle.getSelectedSlot()
- for i = 1, 16 do
- local count = 0
- turtle.select(i)
- if tDirection[direction] == tDirection["ahead"] then
- if turtle.drop() then count = count + 1 end
- elseif tDirection[direction] == tDirection["up"] then
- if turtle.dropUp() then count = count + 1 end
- elseif tDirection[direction] == tDirection["down"] then
- if turtle.dropDown() then count = count + 1 end
- else
- -- Critical error
- if inScript then
- errorText = "Invalid parameter in DROP"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("You used an invalid parameter!")
- return false
- end
- if count > 0 then ret = true end
- end -- End of for loop
- turtle.select(curSel)
- end -- End of else
- else
- -- If ALL was not specified
- if tDirection[direction] == tDirection["ahead"] then
- if number > 0 then
- ret = turtle.drop(number)
- else
- ret = turtle.drop()
- end
- elseif tDirection[direction] == tDirection["up"] then
- if number > 0 then
- ret = turtle.dropUp(number)
- else
- ret = turtle.dropUp()
- end
- elseif tDirection[direction] == tDirection["down"] then
- if number > 0 then
- ret = turtle.dropDown(number)
- else
- ret = turtle.dropDown()
- end
- else
- -- Critical error
- if inScript then
- errorText = "Invalid parameter in DROP"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("You used an invalid parameter!")
- return false
- end
- end -- End of if ALL specified
- if ret == false and not inScript then
- -- Non-critical error
- if advanced then term.setTextColour(colours.orange) end
- print("Either there was nothing to suck or an inventory you are trying to suck from is empty!")
- end
- return ret
- -- SUCK command
- elseif command == tCommandValue["suck"] then
- local param1, param2 = separate(param)
- local direction = "ahead" -- Default direction if none specified
- local number = 0 -- This will default to 'all in current slot'
- local ret = false
- -- First get a direction if specified
- if tDirection[param2] ~= nil then direction = param2 end
- if tDirection[param1] ~= nil then
- if tDirection[param2] and not inSCript then
- -- Non-critical error
- if advanced then term.setTextColour(colours.orange) end
- print("Only one direction can be specified, using the first only!")
- end
- direction = param1
- end
- -- Now get how many items to suck, if specified
- local nParam1 = tonumber(param1)
- local nParam2 = tonumber(param2)
- if nParam2 ~= nil then number = nParam2 end
- if nParam1 ~= nil then
- if nParam2 and not inScript then
- -- Non-critical error
- if advanced then term.setTextColour(colours.orange) end
- print("Only one number can be specified, using the first only!")
- end
- number = nParam1
- end
- -- Now if ALL was used as a parameter
- -- If ALL was not specified
- if tDirection[direction] == tDirection["ahead"] then
- if number > 0 then
- ret = turtle.suck(number)
- else
- ret = turtle.suck()
- end
- elseif tDirection[direction] == tDirection["up"] then
- if number > 0 then
- ret = turtle.suckUp(number)
- else
- ret = turtle.suckUp()
- end
- elseif tDirection[direction] == tDirection["down"] then
- if number > 0 then
- ret = turtle.suckDown(number)
- else
- ret = turtle.suckDown()
- end
- else
- -- Critical error
- if inScript then
- errorText = "Invalid parameter in SUCK"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("You used an invalid parameter!")
- return false
- end
- if ret == false and not inScript then
- -- Non-critical error
- if advanced then term.setTextColour(colours.orange) end
- print("Either there was nothing to suck or an inventory you are trying to suck from is empty!")
- end
- return ret
- -- SELECT command
- elseif command == tCommandValue["select"] then
- local slot = tonumber(param)
- if slot >= 1 and slot <= 16 then
- turtle.select(slot)
- else
- -- Critical error
- if inScript then
- errorText = "Invalid number. Only values 1 to 16"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("Please provide an inventory box number between 1 and 16 only.")
- end
- -- DETECT command
- elseif command == tCommandValue["detect"] then
- if param == "" then param = "ahead" end
- local ret = false
- if tDirection[param] == nil then
- -- Critical error
- if inScript then
- errorText = "Invalid direction provided"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("Please use a valid direction.")
- else
- if tDirection[param] == tDirection["ahead"] then
- ret = turtle.dig()
- elseif tDirection[param] == tDirection["up"] then
- ret = turtle.digUp()
- elseif tDirection[param] == tDirection["down"] then
- ret = turtle.detectgDown()
- else
- -- VERY critical error! This should never happen but best be safe than sorry :)
- if inScript then
- errorText = "Invalid parameter got through checks!"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("It seems an invalid parameter got through my checks!")
- end
- if not inScript then
- -- Info not shown in a script
- if not ret then
- if advanced then term.setTextColour(colours.lightGrey) end
- print("That direction is clear.")
- else
- if advanced then term.setTextColour(colours.lightGrey) end
- print("That direction is blocked.")
- end
- end
- -- We got this far ok so there are no critical errors.
- -- First make sure the errorText is empty so runScript knows a return of false is
- -- to show there was nothing to detect. This is for when IF is implemented
- errorText = ""
- return ret
- end
- -- ATTACK command
- elseif command == tCommandValue["attack"] then
- if param == "" then param = "ahead" end
- local ret = false
- if tDirection[param] == nil then
- -- Critical error
- if inScript then
- errorText = "Invalid direction provided"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("Please use a valid direction.")
- else
- if tDirection[param] == tDirection["ahead"] then
- ret = turtle.attack()
- elseif tDirection[param] == tDirection["up"] then
- ret = turtle.attackUp()
- elseif tDirection[param] == tDirection["down"] then
- ret = turtle.attackDown()
- else
- -- VERY critical error! This should never happen but best be safe than sorry :)
- if inScript then
- errorText = "Invalid parameter got through checks!"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("It seems an invalid parameter got through my checks!")
- end
- if not ret and not inScript then
- -- Non-critical error
- if advanced then term.setTextColour(colours.orange) end
- print("I was unable to attack!")
- end
- end
- -- EQUIP command
- elseif command == tCommandValue["equip"] then
- local ret = false
- if tSide[param] == nil then
- -- Critical error
- if inScript then
- errorText = "No side specified for EQUIP"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("Please specify either the left or right side to equip.")
- elseif tSide[param] == tSide["left"] then
- ret = turtle.equipLeft()
- elseif tSide[param] == tSide["right"] then
- ret = turtle.equipRight()
- else
- -- VERY critical error! This should never happen but best be safe than sorry :)
- if inScript then
- errorText = "Invalid parameter got through checks!"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("It seems an invalid parameter got through my checks!")
- end
- if not ret then
- if turtle.getItemCount() > 0 then
- -- Critical error
- if inScript then
- errorText = "Tried to equip an invalid item!"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("I could not equip that! Please make sure it's a valid item to equip")
- elseif not inScript then
- -- Non-critical error
- if advanced then term.setTextColour(colours.orange) end
- print("There is nothing there to equip!")
- end
- end
- -- REFUEL command
- elseif command == tCommandValue["refuel"] then -- REFUEL
- local valid = false
- if param == "" then
- -- Refuel from all the items in the current inventory slot
- valid = turtle.refuel(0)
- if valid then
- turtle.refuel()
- if not inScript then
- -- Info not shown in a script
- if advanced then term.setTextColour(colours.lightGrey) end
- print("I now have "..tonumber(turtle.getFuelLevel()).." units of fuel")
- end
- end
- elseif param == "all" then
- valid = true
- local curSel = turtle.getSelectedSlot()
- local curFuel = turtle.getFuelLevel()
- for i =1, 16 do
- turtle.select(i)
- turtle.refuel()
- end
- turtle.select(curSel)
- if turtle.getFuelLevel() == curFuel and not inScript then
- -- Non-critical error
- if advanced then term.setTextColour(colours.orange) end
- print("There was nothing for me to use as fuel!")
- elseif not inScript then
- -- Info not shown in a script
- if advanced then term.setTextColour(colours.lightGrey) end
- print("I now have "..tonumber(turtle.getFuelLevel()).." units of fuel")
- end
- elseif tonumber(param) == nil then
- -- Critical error
- if inScript then
- errorText = "Invalid parameter provided"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("Please provide a number of items to use, or ALL to use all combustibles in the inventory")
- else
- local num = tonumber(param)
- valid = turtle.refuel(0)
- if valid then
- turtle.refuel(num)
- if not inScript then
- -- Info not shown in a script
- if advanced then term.setTextColour(colours.lightGrey) end
- print("I now have "..tonumber(turtle.getFuelLevel()).." units of fuel")
- end
- end
- end
- if not valid and not inScript then
- if advanced then term.setTextColour(colours.orange) end
- if turtle.getItemCount() > 0 then
- print("I can't use those items for fuel!")
- else
- print("There is nothing for me to use!")
- end
- end
- -- FUEL command
- elseif command == tCommandValue["fuel"] then
- if inScript then
- -- Critical error, can't use this command in scripts! (YET, Maybe)
- errorText = "Invalid use of FUEL in a script."
- return false
- end
- if advanced then term.setTextColour(colours.lightGrey) end
- print("I currently have "..tonumber(turtle.getFuelLevel()).." units of fuel")
- -- LIST command
- elseif command == tCommandValue["list"] then
- if inScript then
- -- Critical error, can't use this command in scripts
- errorText = "Can not list scripts from inside a script!"
- return false
- end
- local dir = shell.dir()
- local tAll = fs.list(dir)
- local tFiles = {}
- for n, sItem in pairs(tAll) do
- if string.sub( sItem, 1, 1 ) ~= "." then
- local sPath = fs.combine(dir, sItem)
- if not fs.isDir(sPath) then
- if string.sub(sItem, 1, 7) == "turtle." then
- table.insert(tFiles, string.sub(sItem, 8))
- end
- end
- end
- end
- table.sort(tFiles)
- if advanced then term.setTextColour(colours.green) end
- textutils.pagedTabulate(tFiles)
- -- EXIT command
- elseif command == tCommandValue["exit"] then
- if inScript then
- -- Critical error, can't use this command in scripts (YET, Maybe!)
- -- I might make scripts able to exit if a condition is met in the IF command
- -- but that is most likely going to be processed in the runScript function.
- errorText = "Invalid use of EXIT in a script."
- return false
- end
- running = false
- else
- -- VERY critical error! This should never happen but best be safe than sorry :)
- if inScript then
- errorText = "Unknown command value passed to runCommand!"
- return false
- end
- if advanced then term.setTextColour(colours.red) end
- print("Somehow an unknown command value was passed to runCommand!")
- end
- return true
- end
- local function runScript(script)
- -- This function will load a script file and parse each line calling
- -- runCommand for each line that is valid. This function will also deal
- -- with command blocks marked with '[' and ']'.
- -- The IF command is also processed here.
- --
- -- If an error is found in the script then script processing will stop
- -- with a message indicating which line has an error and what it is.
- -- print("Script support is not implemented yet!") return
- local curLine = 1
- local blockStart = 0 -- Marks the start of a block of commands
- -- First create the actual script file name from the given script name
- local fileName = "turtle."..script
- -- Now check if the file exists
- if not fs.exists(fileName) then
- if advanced then term.setTextColour(colours.red) end
- print("Script "..script.." does not exist!")
- return
- end
- -- Load the script into a table
- local tScript = {}
- local numLines = 0
- local file = fs.open(fileName, "r")
- local line = file:readLine()
- while line do
- table.insert(tScript, line)
- numLines = numLines + 1
- line = file:readLine()
- end
- file:close()
- -- Process the script line by line
- for curLine = 1, numLines do
- -- Check if the line is not empty
- if tScript[curLine] ~= "" then
- -- Strip any spaces or tabs from the start and end of the line
- line = string.lower(strip(tScript[curLine]))
- -- This next bit is similar to the parse function but with differences.
- -- The first being that the whole line is already made lower case. EDIT is not supported inside
- -- scripts so case sensitivity is not needed for the parameter.
- -- Also processing of the REPEAT command along with command blocks is done in here. REPEAT and
- -- command blocks are not supported in prompt mode.
- -- Now separate the command from it's parameters.
- local command, param = separate(line)
- -- Now process the commands
- if command == tCommandValue["run"] or command == tCommandValue["edit"] then
- if advanced then term.setTextColour(colours.red) end
- print("Line "..tostring(curLine)..": Invalid use of "..string.upper(command).." in a script.")
- return
- -- REPEAT command
- elseif command == tCommandValue["repeat"] then
- local iterations = tonumber(param)
- if iterations == nil then
- if advanced then term.setTextColour(colours.red) end
- print("Line "..tostring(curLine)..": Non numeric parameter used for REPEAT.")
- return
- else
- curLine = curLine+1
- local inBlock = false
- while iterations > 1 do
- line = string.lower(strip(tScript[curLine]))
- command, param = separate(line)
- if command == "[" then
- blockStart = curLine
- curLine = curLine+1
- inBlock = true
- elseif command == "]" then
- if not inBlock then
- if advanced then term.setTextColour(colours.red) end
- print("Line "..tostring(curLine)..": ']' used without '['")
- return
- else
- curLine = blockStart
- inBlock = false
- end
- else
- if not runCommand(tCommandValue[command], param) then
- if advanced then term.setTextColour(colours.red) end
- print("Line "..curLine..": "..errorText)
- return
- end
- curLine = curLine+1
- end
- if not inBlock then
- iterations = iterations - 1
- end
- end
- end
- -- Ignore block markers if no REPEAT was used
- elseif command == "[" or command == "]" then
- -- PRINT command
- elseif command == tCommandValue["print"] then
- if advanced then term.setTextColour(colours.white) end
- print(param)
- -- Process all other commands
- else
- if not runCommand(tCommandValue[command], param) then
- if advanced then term.setTextColour(colours.red) end
- print("Line "..curLine..": "..errorText)
- return
- end
- end
- end
- end
- end
- local function parse(line)
- -- This function will parse the line entered by the user and calls runCommand
- -- if the command is valid. Otherwise it will give an unknown command error.
- -- First strip any spaces or tabs from the start and end of the line
- line = strip(line)
- -- Now separate the command from it's parameters
- local command, param = separate(line)
- if tCommandValue[command] == nil then
- print("That is not a recognised command!")
- return
- -- RUN, EDIT and PRINT are processed here so that the parameter can be case sensitive
- -- RUN command
- elseif tCommandValue[command] == tCommandValue["run"] then
- inScript = true
- runScript(param)
- inScript = false
- -- EDIT command
- elseif tCommandValue[command] == tCommandValue["edit"] then
- local fileName = "turtle."..param
- shell.run("edit", fileName)
- -- PRINT command
- elseif command == tCommandValue["print"] then
- if advanced then term.setTextColour(colours.white) end
- print(param)
- -- All other commands
- else
- runCommand(tCommandValue[command], string.lower(param))
- end
- end
- -- Now the main program
- if term.isColour() then advanced = true end
- if not turtle then
- if advanced then term.setTextColour(colours.red) end
- print("This program can only be run in a turtle!")
- return false
- end
- if #tArgs == 1 then
- inScript = true
- return runScript(tArgs[1])
- end
- running = true
- term.clear()
- term.setCursorPos(1,1)
- if advanced then
- term.setTextColour(colours.lightGrey)
- write("Welcome to ")
- term.setTextColour(colours.green)
- write("Turtle")
- term.setTextColour(colours.grey)
- print(" version "..version)
- term.setTextColour(colours.lightGrey)
- write("Written by ")
- term.setTextColour(colours.white)
- print("Galbi3000")
- term.setTextColour(colours.lightGrey)
- else
- print("Welcome to Turtle version "..version)
- print("Written by Galbi3000")
- end
- print("")
- print("Type EXIT or QUIT to return to TurtleOS")
- print("")
- while running do
- -- Display the prompt:
- if advanced then term.setTextColour(colours.yellow) end
- write("# ")
- term.setTextColour(colours.white)
- local s = read(nil, tCommandHistory)
- table.insert(tCommandHistory, s)
- parse(s)
- end
- if advanced then
- term.setTextColour(colours.lightGrey)
- write("Thank you for using ")
- term.setTextColour(colours.green)
- print("Turtle")
- term.setTextColour(colours.white)
- else
- print("Thank you for using Turtle")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement