Advertisement
King0fGamesYami

Sync Program

Feb 2nd, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.85 KB | None | 0 0
  1. local tArgs = { ... }
  2. local program = table.remove( tArgs, #tArgs )
  3. local numThing
  4.  
  5. numThing = {
  6.     __add = function( left, right )
  7.         if type( left ) == "table" then
  8.             local new = {}
  9.             if type( right ) == "table" then
  10.                 for k, v in pairs( left ) do
  11.                     new[ k ] = v + right[ k ]
  12.                 end
  13.             elseif type( right ) == "number" then
  14.                 for k, v in pairs( left ) do
  15.                     new[ k ] = v + right
  16.                 end
  17.             end
  18.             return setmetatable( new, numThing )
  19.         else
  20.             return right + left
  21.         end
  22.     end,
  23.     __sub = function( left, right )
  24.         if type( left ) == "table" then
  25.             local new = {}
  26.             if type( right ) == "table" then
  27.                 for k, v in pairs( left ) do
  28.                     new[ k ] = v - right[ k ]
  29.                 end
  30.             elseif type( right ) == "number" then
  31.                 for k, v in pairs( left ) do
  32.                     new[ k ] = v - right
  33.                 end
  34.             end
  35.             return setmetatable( new, numThing )
  36.         else
  37.             return -right + left
  38.         end
  39.     end,
  40.     __mul = function( left, right )
  41.         if type( left ) == "table" then
  42.             local new = {}
  43.             if type( right ) == "table" then
  44.                 for k, v in pairs( left ) do
  45.                     new[ k ] = v * right[ k ]
  46.                 end
  47.             elseif type( right ) == "number" then
  48.                 for k, v in pairs( left ) do
  49.                     new[ k ] = v * right
  50.                 end
  51.             end
  52.             return setmetatable( new, numThing )
  53.         else
  54.             return right * left
  55.         end
  56.     end,
  57.     __div = function( left, right )
  58.         if type( left ) == "table" then
  59.             local new = {}
  60.             if type( right ) == "table" then
  61.                 for k, v in pairs( left ) do
  62.                     new[ k ] = v / right[ k ]
  63.                 end
  64.             elseif type( right ) == "number" then
  65.                 for k, v in pairs( left ) do
  66.                     new[ k ] = v / right
  67.                 end
  68.             end
  69.             return setmetatable( new, numThing )
  70.         else
  71.             local new = {}
  72.             if type( left ) == "number" then
  73.                 for k, v in pairs( right ) do
  74.                     new[ k ] = left / v
  75.                 end
  76.             end
  77.             return setmetatable( new, numThing )
  78.         end
  79.     end,
  80.     __concat = function( left, right )
  81.         return tostring( left ) .. tostring( right )
  82.     end,
  83.     __tostring = function( self )
  84.         return tostring( self.term )
  85.     end,
  86.     __eq = function( left, right )
  87.         if type( left ) == "table" then
  88.             if type( right ) == "table" then
  89.                 for k, v in pairs( left ) do
  90.                     if not right[ k ] == v then
  91.                         return false
  92.                     end
  93.                 end
  94.                 return true
  95.             elseif type( right ) == "number" then
  96.                 return left.term == right
  97.             end
  98.         else
  99.             return right == left
  100.         end
  101.     end,
  102.     __lt = function( left, right )
  103.         if type( left ) == "table" then
  104.             if type( right ) == "table" then
  105.                 for k, v in pairs( left ) do
  106.                     if not right[ k ] < v then
  107.                         return false
  108.                     end
  109.                 end
  110.                 return true
  111.             elseif type( right ) == "number" then
  112.                 return left.term < right
  113.             end
  114.         else
  115.             return right > left
  116.         end
  117.     end,
  118.     __le = function( left, right )
  119.         if type( left ) == "table" then
  120.             if type( right ) == "table" then
  121.                 for k, v in pairs( left ) do
  122.                     if not v <= right[ k ] then
  123.                         return false
  124.                     end
  125.                 end
  126.                 return true
  127.             elseif type( right ) == "number" then
  128.                 return left.term <= right
  129.             end
  130.         else
  131.             return right >= left
  132.         end
  133.     end,
  134.     __unm = function( self )
  135.         return 0 - self
  136.     end,
  137.     __len = function( self )
  138.         return #tostring( self )
  139.     end,
  140. }
  141.  
  142. local current = term.current()
  143.  
  144. local _redirect = {
  145.     setCursorPos = function( x, y )
  146.         if type( x ) == "table" and type( y ) == "table" then
  147.             for _, monitor in ipairs( tArgs ) do
  148.                 peripheral.call( monitor, "setCursorPos", x[ monitor ], y[ monitor ] )
  149.             end
  150.         elseif type( x ) == "table" and type( y ) == "number" then
  151.             for _, monitor in ipairs( tArgs ) do
  152.                 peripheral.call( monitor, "setCursorPos", x[ monitor ], y )
  153.             end
  154.         elseif type( x ) == "number" and type( y ) == "table" then
  155.             for _, monitor in ipairs( tArgs ) do
  156.                 peripheral.call( monitor, "setCursorPos", x, y[ monitor ] )
  157.             end
  158.         elseif type( x ) == "number" and type( y ) == "number" then
  159.             for _, monitor in ipairs( tArgs ) do
  160.                 peripheral.call( monitor, "setCursorPos", x, y )
  161.             end
  162.         end
  163.         current.setCursorPos( type( x ) == "number" and x or x.term, type( y ) == "number" and y or y.term )
  164.     end,
  165.     setTextColor = function( color )
  166.         for _, monitor in ipairs( tArgs ) do
  167.             peripheral.call( monitor, "setTextColor", color )
  168.         end
  169.         current.setTextColor( color )
  170.     end,
  171.     setTextColour = function( color )
  172.         for _, monitor in ipairs( tArgs ) do
  173.             peripheral.call( monitor, "setTextColor", color )
  174.         end
  175.         current.setTextColor( color )
  176.     end,
  177.     setBackgroundColor = function( color )
  178.         for _, monitor in ipairs( tArgs ) do
  179.             peripheral.call( monitor, "setBackgroundColor", color )
  180.         end
  181.         current.setBackgroundColor( color )
  182.     end,
  183.     setBackgroundColour = function( color )
  184.         for _, monitor in ipairs( tArgs ) do
  185.             peripheral.call( monitor, "setBackgroundColor", color )
  186.         end
  187.         current.setBackgroundColor( color )
  188.     end,
  189.     isColor = function()
  190.         for _, monitor in ipairs( tArgs ) do
  191.             if not peripheral.call( monitor, "isColor" ) then
  192.                 return false
  193.             end
  194.         end
  195.         return true
  196.     end,
  197.     isColour = function()
  198.         for _, monitor in ipairs( tArgs ) do
  199.             if not peripheral.call( monitor, "isColour" ) then
  200.                 return false
  201.             end
  202.         end
  203.         return true
  204.     end,
  205.     getSize = function()
  206.         local x, y = {}, {}
  207.         for _, monitor in ipairs( tArgs ) do
  208.             x[ monitor ], y[ monitor ] = peripheral.call( monitor, "getSize" )
  209.         end
  210.         x.term, y.term = current.getSize()
  211.         return setmetatable( x, numThing ), setmetatable( y, numThing )
  212.     end,
  213.     getCursorPos = function()
  214.         local x, y = {}, {}
  215.         for _, monitor in ipairs( tArgs ) do
  216.             x[ monitor ], y[ monitor ] = peripheral.call( monitor, "getCursoPos" )
  217.         end
  218.         x.term, y.term = current.getCursorPos()
  219.         return setmetatable( x, numThing ), setmetatable( y, numThing )
  220.     end,
  221.     clear = function()
  222.         for _, monitor in ipairs( tArgs ) do
  223.             peripheral.call( monitor, "clear" )
  224.         end
  225.         current.clear()
  226.     end,
  227. }
  228.  
  229. term.redirect( _redirect )
  230. term.clear()
  231. shell.run( program )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement