immibis

"sandbox" (1.3 pr2)

Feb 10th, 2012
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.04 KB | None | 0 0
  1.  
  2. local myenv = getfenv()
  3. local turtle = turtle
  4. local env = {
  5.     turtle = {
  6.         select = turtle.select,
  7.         drop = turtle.drop
  8.     }
  9. }
  10.  
  11. setmetatable(env, {__index = myenv, __newindex = myenv})
  12.  
  13. local undolist = {}
  14.  
  15. -- initially: +X is forward, +Y is left, +Z is up
  16.  
  17. local dx, dy = 1, 0
  18. local x, y, z = 0, 0, 0
  19.  
  20. function env.turtle.gps()
  21.     return x, y, z
  22. end
  23.  
  24. function env.turtle.face(ndx, ndy)
  25.     if ndx == -dx and ndy == -dy then
  26.         env.turtle.turnLeft()
  27.         env.turtle.turnLeft()
  28.     elseif ndy == dx and ndx == -ndy then
  29.         env.turtle.turnLeft()
  30.     elseif ndy == -dx and ndx == ndy then
  31.         env.turtle.turnRight()
  32.     end
  33. end
  34.  
  35. local gpshook = {
  36.     [turtle.turnLeft] = function()
  37.         dy, dx = dx, -dy
  38.     end,
  39.     [turtle.turnRight] = function()
  40.         dy, dx = -dx, dy
  41.     end,
  42.     [turtle.forward] = function()
  43.         x = x + dx
  44.         y = y + dy
  45.     end,
  46.     [turtle.back] = function()
  47.         x = x - dx
  48.         y = y - dy
  49.     end,
  50.     [turtle.up] = function()
  51.         z = z + 1
  52.     end,
  53.     [turtle.down] = function()
  54.         z = z - 1
  55.     end,
  56. }
  57.  
  58. local function wrap(fn, rev)
  59.     return function(...)
  60.         table.insert(undolist, rev)
  61.         local a = {fn(...)}
  62.         if #a == 0 or a[1] then
  63.             if gpshook[fn] then gpshook[fn]() end
  64.         else
  65.             table.remove(undolist,#undolist)
  66.         end
  67.         return unpack(a)
  68.     end
  69. end
  70.  
  71. local function pair(a, b)
  72.     env.turtle[a] = wrap(turtle[a], turtle[b])
  73.     env.turtle[b] = wrap(turtle[b], turtle[a])
  74. end
  75.  
  76. pair("forward", "back")
  77. pair("up", "down")
  78. pair("turnLeft", "turnRight")
  79. pair("dig", "place")
  80. pair("digUp", "placeUp")
  81. pair("digDown", "placeDown")
  82.  
  83. local args = {...}
  84.  
  85. local noundo = (args[1] == "noundo")
  86. if noundo then table.remove(args, 1) end
  87.  
  88. local prog = shell.resolveProgram(table.remove(args, 1))
  89. print("Beginning sandboxed execution")
  90. local ok, result = pcall(os.run, env, prog, unpack(args))
  91. print("Ended sandboxed execution")
  92.  
  93. if not ok then print("Error: ",result) end
  94.  
  95. if noundo then
  96.     print("Not undoing ",#undolist," actions")
  97. else
  98.     print("Undoing ",#undolist," actions")
  99.  
  100.     for k = #undolist,1,-1 do
  101.         undolist[k]()
  102.     end
  103.  
  104.     print("Undo complete")
  105. end
Advertisement
Add Comment
Please, Sign In to add comment