Advertisement
GopherAtl

togo (computercraft turtle program)

Apr 14th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.98 KB | None | 0 0
  1.  
  2.  
  3. args={...}
  4. if #args>0 and args[1]=="update" then
  5.     shell.run("delete togo.lua")
  6.     shell.run("pastebin get psq7nt9X togo.lua")
  7.     return
  8. end
  9.  
  10. local Quit = false
  11. local tokens={}
  12. local nextToken = 1
  13.  
  14. local recorded = {}
  15. local isRecording = false
  16.  
  17. local macros={}
  18.  
  19. local function peekToken()
  20.     return tokens[nextToken]
  21. end
  22.  
  23. local function popToken()
  24.     nextToken=nextToken+1
  25.     return tokens[nextToken-1]
  26. end
  27.  
  28. local function tabSub(t,f,l)
  29.     f=f or 1
  30.     l=l or #t
  31.     res={}
  32.     for i=f,l do
  33.         res[#res+1]=t[i]
  34.     end
  35.     return res
  36. end
  37.  
  38. local function tabCat(t1,t2)
  39.     for i=1,#t2 do
  40.         t1[#t1+1]=t2[i]
  41.     end
  42. end
  43.  
  44. local actDirs = {
  45.     up=true,
  46.     down=true,
  47.     front=true,
  48. }
  49.  
  50. local function dir_f(ff, fu, fd,...)
  51.     local t=peekToken()
  52.     if actDirs[t] then
  53.         nextToken=nextToken+1
  54.     else
  55.         t="front"        
  56.     end
  57.     if t=="front" then
  58.         ff(...)
  59.     elseif t=="up" then
  60.         fu(...)
  61.     elseif t=="down" then
  62.         fd(...)
  63.     end
  64. end
  65.  
  66. local function rep_f(f,...)
  67.     local t=peekToken()
  68.     local d=tonumber(t)
  69.     if  d then
  70.         nextToken=nextToken+1  
  71.     else
  72.         d=1
  73.     end
  74.  
  75.     for i=1,d do
  76.         if not f(...) then
  77.             return false
  78.         end
  79.         i=i+1
  80.     end
  81.     return true
  82. end
  83.  
  84. local instructions = {}
  85.  
  86. function runTokens()
  87.     for cmd in popToken do
  88.         if instructions[cmd] then
  89.             local p=nextToken
  90.             instructions[cmd]()
  91.             if isRecording and not nonRecordable[cmd] then
  92.                 --copy cmd and any args it consumed to recording
  93.                 for i=p,nextToken do
  94.                     recorded[#recorded+1] = tokens[i-1]
  95.                 end
  96.             end
  97.         else
  98.             print("I don't know how to "..cmd..", aborting")
  99.             if nextToken>2 then
  100.                 aborted=table.concat(tabSub(tokens,nextToken-1)," ")
  101.             end
  102.             break        
  103.         end
  104.     end
  105. end
  106.  
  107. function runMacro(name)
  108.     local backup = tokens
  109.     local backupNext = nextToken
  110.     tokens=macros[name]
  111.     nextToken=1
  112.     runTokens()
  113.     tokens=backup
  114.     nextToken=backupNext
  115. end
  116.  
  117. -- the basic instructions which are essentially passed to turtle
  118. instructions.forward = function (...) rep_f(turtle.forward,...) end
  119. instructions.back = function (...) rep_f(turtle.back,...) end
  120. instructions.up = function (...) rep_f(turtle.up,...) end
  121. instructions.down = function (...) rep_f(turtle.down,...) end
  122. instructions.left= turtle.turnLeft
  123. instructions.right= turtle.turnRight
  124. instructions.dig= function(...) dir_f(turtle.dig, turtle.digUp, turtle.digDown, ...) end
  125. instructions.attack= function(...) dir_f(turtle.attack, turtle.attackUp, turtle.attackDown, ...) end
  126. instructions.suck= function(...) dir_f(turtle.suck,turtle.suckUp, turtle.suckDown, ...) end
  127. instructions.drop= function(...) dir_f(turtle.drop, turtle.dropUp, turtle.dropDown, ...) end
  128. instructions.place= function(...) dir_f(turtle.place, turtle.placeUp, turtle.placeDown, ...) end
  129. instructions.exit= function() Quit=true end
  130.  
  131. instructions.fd = instructions.forward
  132. instructions.bk = instructions.back
  133. instructions.dn = instructions.down
  134. instructions.lt= instructions.left
  135. instructions.rt= instructions.right
  136. instructions.dg= instructions.dig
  137. instructions.at= instructions.attack
  138. instructions.sk= instructions.suck
  139. instructions.dr= instructions.drop
  140. instructions.pl= instructions.place
  141. instructions.ex= instructions.exit
  142.  
  143. --recording meta-instructions (these are never recorded!)
  144. instructions.record = function()
  145.     if not isRecording then
  146.         --new recording
  147.         if #recorded==0 then
  148.             print("recording begun")
  149.         else
  150.             print("recording resumed")
  151.         end        
  152.     else
  153.         print("recording stopped")
  154.     end
  155.     isRecording=not isRecording
  156. end
  157. instructions.rec = instructions.record
  158.  
  159. instructions.save = function()
  160.     if #recorded == 0 then
  161.         print("no recording to save!")
  162.         return
  163.     end
  164.     local name=popToken()
  165.     if name==nil then
  166.         print("must specify a name to save as!")
  167.         return
  168.     end
  169.     if instructions[name] and not macros[name] then
  170.         print("illegal macro name!")
  171.         return
  172.     end
  173.     macros[name]=recorded
  174.     recorded={}
  175.     instructions[name]=function() run_f(runMacro,name) end
  176. end
  177.    
  178. --the high-level instructions provided by this system
  179. 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
  180. instructions.move = function(x,y,z) end      --move to x,y,z relative to current position
  181. instructions.sethome = function() end    --sets the current position to "home"
  182. instructions.home = function() end       -- return to starting position, or position last set by sethome
  183. instructions.pushp = function() end      --pushes the current position to the position stack
  184. instructions.popp = function() end   --pops top position from stack and goto()'s it
  185. instructions.nameLoc = function(name) end --saves the current location with the specified name.
  186. instructions.navto = function(name) end --navigates to the specified named location.
  187.  
  188.  
  189.  
  190. --special automatic variables...
  191. -- succ/fail (pair of bools, set based on success or failure of last instr)
  192. -- count - set by suck, drop, and by movement instructions if given a distance argument.
  193. -- px, py, pz; pos - position
  194. -- dir
  195. -- dx, dy, dz - relative position - relative to position of start, or of last call to sethome
  196. -- name, meta - set by inspect  
  197. -- name, damage, count - set by getItemDetail
  198. --
  199. --
  200.  
  201. nonRecordable = {
  202.     record = true,
  203.     rec = true,
  204.     save = true,
  205.  
  206. }
  207. --[[
  208.   place -
  209.     vector x,y,z
  210.  
  211. ]]
  212.  
  213. local function exportMacros()    
  214.     local text=textutils.serialize(macros)
  215.     local file=fs.open(".macros","w")
  216.     file.write(text)
  217.     file.close()
  218. end
  219.  
  220. local function importMacros()
  221.     if fs.exists(".macros") then
  222.         local file=fs.open(".macros",r)
  223.         local text=file.readAll()
  224.         file.close()
  225.         macros=textutils.unserialize(text)
  226.         for n,_ in macros do
  227.             instructions[n]=function() runf(runMacro,n) end
  228.         end
  229.     end
  230. end
  231.  
  232. history={}
  233. aborted=""
  234.  
  235. importMacros()
  236.  
  237. while not Quit do
  238.     write("TOGO>")
  239.     local cmd
  240.     if aborted then
  241.         os.queueEvent("key",200,false)
  242.         local space=string.find(aborted," ")
  243.         if space then
  244.             os.queueEvent("key",199,false)
  245.             for i=2,space do
  246.                 os.queueEvent("key",205,false)
  247.             end
  248.         end
  249.        
  250.         history[#history+1]=aborted
  251.         cmd=read(nil,history)
  252.         history[#history]=nil
  253.         aborted=nil
  254.     else
  255.         cmd=read(nil,history)
  256.     end
  257.     history[#history+1]=cmd
  258.     tokens={}
  259.     nextToken=1
  260.     for tok in string.gmatch(cmd,"%S+") do
  261.         tokens[#tokens+1]=tok
  262.     end
  263.     runTokens()
  264. end
  265.  
  266. exportMacros()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement