Advertisement
DigitalZilla

Untitled

Aug 19th, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.90 KB | None | 0 0
  1. WIDTH = 3;
  2. HEIGHT = 3;
  3. SIDE = "back";
  4.  
  5. Data = {};
  6.  
  7. -- -----------------------------------------------------------------------------------------------
  8. function Initialize()
  9.     for i = 1, (HEIGHT * WIDTH) do
  10.         Data[i] = false;
  11.     end
  12.  
  13.     Randomize();
  14. end
  15.  
  16. -- -----------------------------------------------------------------------------------------------
  17. function Randomize()
  18.     for i = 1, #Data do
  19.         Data[i] = math.random(2) == 1;
  20.     end
  21. end
  22.  
  23. -- -----------------------------------------------------------------------------------------------
  24. function GetIndex(x, y)
  25.     if (x > WIDTH) then
  26.         print(string.format("ERROR - GetIndex, X out of range: %d of %d", x, WIDTH));
  27.         return nil;
  28.     end
  29.  
  30.     if (y > HEIGHT) then
  31.         print(string.format("ERROR - GetIndex, Y out of range: %d of %d", y, HEIGHT));
  32.         return nil;
  33.     end
  34.  
  35.     return ((y - 1) * WIDTH) + x;
  36. end
  37.  
  38. -- -----------------------------------------------------------------------------------------------
  39. function GetCoords(index)
  40.     local x = math.floor(index / WIDTH) + 1;
  41.     local y = index % WIDTH;
  42.  
  43.     return x, y;
  44. end
  45.  
  46. -- -----------------------------------------------------------------------------------------------
  47. function IndexToBitFlag(index)
  48.     return bit.blshift(1, index - 1)
  49. end
  50.  
  51.  
  52.  
  53. -- -----------------------------------------------------------------------------------------------
  54. function Render()
  55.     local output = 0;
  56.  
  57.     for i = 1, #Data do
  58.         local value = Data[i];
  59.         local flag = IndexToBitFlag(i);
  60.  
  61.         if (value) then
  62.             output = bit.bor(output, bit);
  63.         else
  64.             output = bit.band(output, bit.bnot(flag));
  65.         end
  66.     end
  67.  
  68.     rs.setBundledOutput(SIDE, output);
  69. end
  70.  
  71.  
  72. -- -----------------------------------------------------------------------------------------------
  73. Initialize();
  74. -- -----------------------------------------------------------------------------------------------
  75.  
  76. while true do
  77.     Randomize();
  78.     Render();
  79.     os.sleep(1);
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement