Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Program that runs a script..
- term.clear()
- term.setCursorPos(1,1)
- local Program = {...}
- local Logging = true
- local Dir = nil
- local Rep = nil
- if Program[2] == "logging" and Program[3] == "off" then
- Logging = false
- end
- Program = Program[1]
- local function Log(stuff)
- if Logging then
- print(stuff)
- end
- end
- --Checks if file has no errors
- local i = 0
- local b = 0
- Log("Checking if file has errors")
- if Program == nil or not fs.exists(Program) then
- Log("Script does not exist!")
- error()
- end
- Log("To disable this logging, start the program like this")
- Log("\""..shell.getRunningProgram().." "..Program.." logging off\"")
- local File = io.open(Program)
- for line in File:lines() do
- i = i+1
- b = i
- local function Wrong()
- print("Line "..i.." is wrong: "..line)
- File:close()
- error()
- end
- if string.find(line, " ") then
- local Type = string.lower(string.sub(line, 1, string.find(line, " ")-1))
- if Type == "print" then
- Dir = string.lower(string.sub(line, string.find(line, " ")+1, string.len(line)))
- elseif string.find(line, " ", (string.find(line, " ")+1)) and tonumber(string.sub(line, string.find(line, " ", string.find(line, " ")+1), string.len(line))) > 0 then -- Detected 2 spaces! Which means a error or repeation!
- Rep = string.sub(line, string.find(line, " ", string.find(line, " ")+1), string.len(line))
- Dir = string.lower(string.sub(line, string.find(line, " ")+1, (string.len(line)-string.len(Rep))))
- Rep = tonumber(Rep)
- else
- Dir = string.lower(string.sub(line, string.find(line, " ")+1, string.len(line)))
- end
- local function Check(...)
- local Bla = {...}
- for n=1,#Bla do
- if Dir == Bla[n] then
- return true
- end
- end
- Wrong()
- end
- if Dir == "" then
- Wrong()
- end
- if string.find(line, " ") then
- Wrong()
- end
- if Type == "turn" then
- Check("left", "right")
- elseif Type == "dig" then
- Check("up", "down", "forwards")
- elseif Type == "print" then
- -- Its right.. How can it be wrong
- elseif Type == "move" then
- Check("forwards", "back", "down", "up")
- elseif Type == "place" then
- Check("up", "down", "forwards")
- elseif Type == "select" then
- if tonumber(Dir) > 0 and tonumber(Dir) < 10 then
- -- Yay..
- else
- Wrong()
- end
- elseif Type == "place" then
- Check("up", "down", "forwards")
- else
- Wrong()
- end
- end
- end
- File:close()
- Log("File check completed")
- Log("Running script")
- local Dont = false
- local i = 0
- local File = io.open(Program)
- for line in File:lines() do
- i = i+1
- Rep = 1
- local function Log(stuff)
- if Logging and not Dont then
- print(i.." of "..b..": "..stuff)
- end
- end
- local Type = string.lower(string.sub(line, 1, string.find(line, " ")-1))
- if Type == "print" then
- print(line)
- Dir = string.lower(string.sub(line, string.find(line, " ")+1, string.len(line)))
- elseif string.find(line, " ", string.find(line, " ")+1) and tonumber(string.sub(line, string.find(line, " ", string.find(line, " ")+1), string.len(line))) > 0 then -- Detected 2 spaces! Which means a error or repeation!
- Rep = string.sub(line, string.find(line, " ", string.find(line, " ")+1), string.len(line))
- Dir = string.lower(string.sub(line, string.find(line, " ")+1, (string.len(line)-string.len(Rep))))
- Rep = tonumber(Rep)
- else
- Dir = string.lower(string.sub(line, string.find(line, " ")+1, string.len(line)))
- end
- if Rep > 1 then
- Log("Repeating \""..Type.." "..Dir.."\" "..Rep.." times")
- Dont = true
- end
- repeat
- Rep = Rep-1
- if Type == "turn" then
- Log("Turning turtle "..Dir)
- if Dir == "left" then
- turtle.turnLeft()
- elseif Dir == "right" then
- turtle.turnRight()
- end
- elseif Type == "print" then
- print(Dir)
- elseif Type == "dig" then
- Log("Turtle is now digging "..Dir)
- if Dir == "up" then
- turtle.digUp()
- elseif Dir == "down" then
- turtle.digDown()
- elseif Dir == "forwards" then
- turtle.dig()
- end
- elseif Type == "move" then
- Log("Turtle is now moving "..Dir)
- if Dir == "forwards" then
- turtle.forward()
- elseif Dir == "back" then
- turtle.back()
- elseif Dir == "up" then
- turtle.up()
- elseif Dir == "down" then
- turtle.down()
- end
- elseif Type == "select" then
- Log("Turtle is selecting slot "..tonumber(Dir))
- turtle.select(tonumber(Dir))
- elseif Type == "place" then
- Log("Placing block "..Dir)
- if Dir == "forwards" then
- turtle.place()
- elseif Dir == "up" then
- turtle.placeUp()
- elseif Dir == "down" then
- turtle.placeDown()
- end
- end
- until Rep < 1
- Dont = false
- end
- Log("Script completed")
- File:close()
Advertisement
Add Comment
Please, Sign In to add comment