Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- args={...}
- if #args>0 and args[1]=="update" then
- shell.run("delete togo.lua")
- shell.run("pastebin get psq7nt9X togo.lua")
- return
- end
- local Quit = false
- local tokens={}
- local nextToken = 1
- local recorded = {}
- local isRecording = false
- local macros={}
- local function peekToken()
- return tokens[nextToken]
- end
- local function popToken()
- nextToken=nextToken+1
- return tokens[nextToken-1]
- end
- local function tabSub(t,f,l)
- f=f or 1
- l=l or #t
- res={}
- for i=f,l do
- res[#res+1]=t[i]
- end
- return res
- end
- local function tabCat(t1,t2)
- for i=1,#t2 do
- t1[#t1+1]=t2[i]
- end
- end
- local actDirs = {
- up=true,
- down=true,
- front=true,
- }
- local function dir_f(ff, fu, fd,...)
- local t=peekToken()
- if actDirs[t] then
- nextToken=nextToken+1
- else
- t="front"
- end
- if t=="front" then
- ff(...)
- elseif t=="up" then
- fu(...)
- elseif t=="down" then
- fd(...)
- end
- end
- local function rep_f(f,...)
- local t=peekToken()
- local d=tonumber(t)
- if d then
- nextToken=nextToken+1
- else
- d=1
- end
- for i=1,d do
- if not f(...) then
- return false
- end
- i=i+1
- end
- return true
- end
- local instructions = {}
- function runTokens()
- for cmd in popToken do
- if instructions[cmd] then
- local p=nextToken
- instructions[cmd]()
- if isRecording and not nonRecordable[cmd] then
- --copy cmd and any args it consumed to recording
- for i=p,nextToken do
- recorded[#recorded+1] = tokens[i-1]
- end
- end
- else
- print("I don't know how to "..cmd..", aborting")
- if nextToken>2 then
- aborted=table.concat(tabSub(tokens,nextToken-1)," ")
- end
- break
- end
- end
- end
- function runMacro(name)
- local backup = tokens
- local backupNext = nextToken
- tokens=macros[name]
- nextToken=1
- runTokens()
- tokens=backup
- nextToken=backupNext
- end
- -- the basic instructions which are essentially passed to turtle
- instructions.forward = function (...) rep_f(turtle.forward,...) end
- instructions.back = function (...) rep_f(turtle.back,...) end
- instructions.up = function (...) rep_f(turtle.up,...) end
- instructions.down = function (...) rep_f(turtle.down,...) end
- instructions.left= turtle.turnLeft
- instructions.right= turtle.turnRight
- instructions.dig= function(...) dir_f(turtle.dig, turtle.digUp, turtle.digDown, ...) end
- instructions.attack= function(...) dir_f(turtle.attack, turtle.attackUp, turtle.attackDown, ...) end
- instructions.suck= function(...) dir_f(turtle.suck,turtle.suckUp, turtle.suckDown, ...) end
- instructions.drop= function(...) dir_f(turtle.drop, turtle.dropUp, turtle.dropDown, ...) end
- instructions.place= function(...) dir_f(turtle.place, turtle.placeUp, turtle.placeDown, ...) end
- instructions.exit= function() Quit=true end
- instructions.fd = instructions.forward
- instructions.bk = instructions.back
- instructions.dn = instructions.down
- instructions.lt= instructions.left
- instructions.rt= instructions.right
- instructions.dg= instructions.dig
- instructions.at= instructions.attack
- instructions.sk= instructions.suck
- instructions.dr= instructions.drop
- instructions.pl= instructions.place
- instructions.ex= instructions.exit
- --recording meta-instructions (these are never recorded!)
- instructions.record = function()
- if not isRecording then
- --new recording
- if #recorded==0 then
- print("recording begun")
- else
- print("recording resumed")
- end
- else
- print("recording stopped")
- end
- isRecording=not isRecording
- end
- instructions.rec = instructions.record
- instructions.save = function()
- if #recorded == 0 then
- print("no recording to save!")
- return
- end
- local name=popToken()
- if name==nil then
- print("must specify a name to save as!")
- return
- end
- if instructions[name] and not macros[name] then
- print("illegal macro name!")
- return
- end
- macros[name]=recorded
- recorded={}
- instructions[name]=function() run_f(runMacro,name) end
- end
- --the high-level instructions provided by this system
- instructions.goto = function(x,y,z, how) end -- move turtle to x,y,z; how can be "direct", "path", or "default"; default tries to path, and failing that, tries to go directly
- instructions.move = function(x,y,z) end --move to x,y,z relative to current position
- instructions.sethome = function() end --sets the current position to "home"
- instructions.home = function() end -- return to starting position, or position last set by sethome
- instructions.pushp = function() end --pushes the current position to the position stack
- instructions.popp = function() end --pops top position from stack and goto()'s it
- instructions.nameLoc = function(name) end --saves the current location with the specified name.
- instructions.navto = function(name) end --navigates to the specified named location.
- --special automatic variables...
- -- succ/fail (pair of bools, set based on success or failure of last instr)
- -- count - set by suck, drop, and by movement instructions if given a distance argument.
- -- px, py, pz; pos - position
- -- dir
- -- dx, dy, dz - relative position - relative to position of start, or of last call to sethome
- -- name, meta - set by inspect
- -- name, damage, count - set by getItemDetail
- --
- --
- nonRecordable = {
- record = true,
- rec = true,
- save = true,
- }
- --[[
- place -
- vector x,y,z
- ]]
- local function exportMacros()
- local text=textutils.serialize(macros)
- local file=fs.open(".macros","w")
- file.write(text)
- file.close()
- end
- local function importMacros()
- if fs.exists(".macros") then
- local file=fs.open(".macros",r)
- local text=file.readAll()
- file.close()
- macros=textutils.unserialize(text)
- for n,_ in macros do
- instructions[n]=function() runf(runMacro,n) end
- end
- end
- end
- history={}
- aborted=""
- importMacros()
- while not Quit do
- write("TOGO>")
- local cmd
- if aborted then
- os.queueEvent("key",200,false)
- local space=string.find(aborted," ")
- if space then
- os.queueEvent("key",199,false)
- for i=2,space do
- os.queueEvent("key",205,false)
- end
- end
- history[#history+1]=aborted
- cmd=read(nil,history)
- history[#history]=nil
- aborted=nil
- else
- cmd=read(nil,history)
- end
- history[#history+1]=cmd
- tokens={}
- nextToken=1
- for tok in string.gmatch(cmd,"%S+") do
- tokens[#tokens+1]=tok
- end
- runTokens()
- end
- exportMacros()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement