Advertisement
DigitalZilla

FluidControl

Aug 24th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.21 KB | None | 0 0
  1. -- --------------------------------------------------------------------------
  2. -- Constants
  3. -- --------------------------------------------------------------------------
  4. INPUT           = "bottom";
  5. OUTPUT          = "left";
  6. MB_LOW          = 1000;
  7. MB_CRITICAL     = 5000;
  8. PROCESS_TIME    = 30;
  9. POLL            = 1;
  10. MON             = peripheral.wrap("top");
  11. MON2            = peripheral.wrap("monitor_1");
  12.  
  13. Monitors = { MON, MON2 }
  14.  
  15. Data = {
  16.     {
  17.         Name        = "Redstone",
  18.         Color       = colors.red,
  19.         Low         = colors.white,
  20.         Critical    = colors.orange,
  21.         Item        = colors.magenta,
  22.         mbPerItem   = 900,
  23.         Output      = colors.white,
  24.     },
  25.  
  26.     {
  27.         Name        = "Glowstone",
  28.         Color       = colors.yellow,
  29.         Low         = colors.lightBlue,
  30.         Critical    = colors.yellow,
  31.         Item        = colors.lime,
  32.         mbPerItem   = 1000,
  33.         Output      = colors.orange,
  34.     },
  35.  
  36.     {
  37.         Name        = "Enderium",
  38.         Color       = colors.green,
  39.         Low         = colors.pink,
  40.         Critical    = colors.gray,
  41.         Item        = colors.lightGray,
  42.         mbPerItem   = 250,
  43.         Output      = colors.magenta,
  44.     },
  45.  
  46.     {
  47.         Name        = "Pyrotheum",
  48.         Color       = colors.orange,
  49.         Low         = colors.cyan,
  50.         Critical    = colors.purple,
  51.         Item        = colors.blue,
  52.         mbPerItem   = 100,
  53.         Output      = colors.lightBlue,
  54.     },
  55.  
  56.     {
  57.         Name        = "Cryotheum",
  58.         Color       = colors.cyan,
  59.         Low         = colors.brown,
  60.         Critical    = colors.green,
  61.         Item        = colors.red,
  62.         mbPerItem   = 100,
  63.         Output      = colors.yellow,
  64.     },
  65. }
  66.  
  67. Status = {
  68.     Good        = "Good",
  69.     Low         = "Low",
  70.     Critical    = "Critical",
  71. }
  72.  
  73. MonData = {
  74.     Columns = { 1, 11, 20, 28 };
  75.     Colors = {
  76.         Good        = colors.lime;
  77.         Low         = colors.orange;
  78.         Critical    = colors.red;
  79.     }
  80. }
  81.  
  82.  
  83.  
  84. -- --------------------------------------------------------------------------
  85. -- Variables
  86. -- --------------------------------------------------------------------------
  87. Timer = {};
  88.  
  89.  
  90. -- --------------------------------------------------------------------------
  91. -- Methods
  92. -- --------------------------------------------------------------------------
  93. function GetFlag(data, flag)
  94.     return bit.band(data, flag) ~= 0;
  95. end
  96.  
  97. -- --------------------------------------------------------------------------
  98. function GetNumToSend(mbPerBucket, isCritical)
  99.     local target = MB_LOW;
  100.     if (isCritical) then
  101.         target = MB_CRITICAL;
  102.     end
  103.  
  104.     local count = target / mbPerBucket;
  105.     return math.ceil(count);
  106. end
  107.  
  108. -- --------------------------------------------------------------------------
  109. function Pulse(channel, count)
  110.     for i = 1, count do
  111.         rs.setBundledOutput(OUTPUT, channel);
  112.         os.sleep(0.1);
  113.         rs.setBundledOutput(OUTPUT, 0);
  114.         os.sleep(0.1);
  115.     end
  116. end
  117.  
  118. -- --------------------------------------------------------------------------
  119. function SetDelay(id)
  120.     Timer[id] = os.clock() + PROCESS_TIME;
  121. end
  122.  
  123. -- --------------------------------------------------------------------------
  124. function MON_Clear()
  125.     for i = 1, #Monitors do
  126.         local m = Monitors[i];
  127.         m.clear();
  128.         m.setTextColor(colors.white);
  129.     end
  130. end
  131.  
  132. -- --------------------------------------------------------------------------
  133. function MON_Write(text, x, y, color)
  134.     color = color or colors.white;
  135.     x = x or 1;
  136.     y = y or 1;
  137.  
  138.     for i = 1, #Monitors do
  139.         local m = Monitors[i];
  140.         m.setCursorPos(x, y);
  141.         m.setTextColor(color);
  142.         m.write(text);
  143.     end
  144. end
  145.  
  146. -- --------------------------------------------------------------------------
  147. function UpdateMonitor(monitorData)
  148.     MON_Clear();
  149.  
  150.     for i, data in ipairs(monitorData) do
  151.        
  152.         -- Col 1: Name
  153.         MON_Write(data.Name, MonData.Columns[1], i, data.Color);
  154.  
  155.         -- Col 2: Status
  156.         MON_Write(data.Status, MonData.Columns[2], i, MonData.Colors[data.Status]);
  157.  
  158.         -- Col 3: Stock
  159.         local text = "Stocked";
  160.         local color = MonData.Colors.Good;
  161.         if (not data.HasStock) then
  162.             text = "Empty";
  163.             color = MonData.Colors.Critical;
  164.         end
  165.         MON_Write(text, MonData.Columns[3], i, color);
  166.  
  167.         -- Col 4: Processing Time
  168.         if(data.Time > 0) then
  169.             MON_Write(string.format("%d", data.Time), MonData.Columns[4], i, colors.white);
  170.         end
  171.     end
  172. end
  173.  
  174. -- --------------------------------------------------------------------------
  175. -- Program
  176. -- --------------------------------------------------------------------------
  177.  
  178. while true do
  179.     local raw = rs.getBundledInput(INPUT);
  180.     local drawNewLine = true;
  181.  
  182.     local monitorData = {};
  183.  
  184.     for i, data in ipairs(Data) do
  185.         local isLow         = GetFlag(raw, data.Low);
  186.         local isCritical    = GetFlag(raw, data.Critical);
  187.         local hasStock      = GetFlag(raw, data.Item);
  188.         local time          = os.clock();
  189.         local waitTilTime   = Timer[data.Name] or 0;
  190.  
  191.         if (isLow and (time >= waitTilTime))then
  192.             if (drawNewLine) then
  193.                 print("\n------------------------------------------------")
  194.                 drawNewLine = false;
  195.             end
  196.  
  197.             if (hasStock) then
  198.                 local count = GetNumToSend(data.mbPerItem, isCritical);
  199.                 print(string.format("[%d] Sending %d %s", time, count, data.Name));
  200.                
  201.                 Pulse(data.Output, count);
  202.                 SetDelay(data.Name);
  203.             else
  204.                 print(string.format("[%d] %s is low but Items are not available!", time, data.Name))
  205.                 SetDelay(data.Name);
  206.             end
  207.         end
  208.  
  209.         table.insert(monitorData, {
  210.                 Name        = data.Name;
  211.                 Color       = data.Color;
  212.                 Status      = (isCritical and Status.Critical) or (isLow and Status.Low) or Status.Good;
  213.                 HasStock    = hasStock;
  214.                 Time        = waitTilTime - time;
  215.             });
  216.     end
  217.  
  218.     UpdateMonitor(monitorData);
  219.  
  220.     os.sleep(POLL);
  221. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement