Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.85 KB | None | 0 0
  1. --Monitors energy levels on the left side.
  2. --Outputs generator control signal on the back.
  3. --Sends tank procentage with command "tankUpdate".
  4.  
  5. local r = component.proxy(component.list("redstone")());
  6. local m = component.proxy(component.list("modem")());
  7. local t = component.proxy(component.list("tank_controller")());
  8.  
  9. local port = 8000;
  10. local address = "lol";
  11.  
  12. function sleep(time)
  13.   local t = computer.uptime();
  14.   while(computer.uptime() - t <= time) do
  15.     computer.pushSignal("wait");
  16.     computer.pullSignal();
  17.   end
  18. end
  19.  
  20. function beep()
  21.   computer.beep();
  22. end
  23.  
  24. function errorBeep()
  25.   computer.beep(1000, 0.2);
  26.   sleep(0.2);
  27.   computer.beep(1000, 0.8);
  28. end
  29.  
  30. function init()
  31.   if(m) then
  32.     if(r) then
  33.       local portOpen = false;
  34.       if(m.isOpen(port)) then
  35.         portOpen = true;
  36.       else
  37.         portOpen = m.open(port);
  38.       end
  39.  
  40.       if(portOpen) then
  41.         beep();
  42.         return true;
  43.       end
  44.     end
  45.   end
  46.  
  47.   errorBeep();
  48.   return false;
  49. end
  50.  
  51. function getTankInfo()
  52.   local cap = 0;
  53.   local amount = 0;
  54.   for i = 0, 5, 1 do
  55.     local data = t.getFluidInTank(i);
  56.     if(data ~= nil) then
  57.       cap = tonumber(data[1]["capacity"]);
  58.       amount = tonumber(data[1]["amount"]);
  59.     end
  60.   end
  61.  
  62.   local info = {};
  63.   info["max"] = cap;
  64.   info["amount"] = amount;
  65.  
  66.   return info;
  67. end
  68.  
  69.  
  70. --Main loop.
  71. if(init()) then
  72.   while(true) do
  73.     sleep(10);
  74.  
  75.     --Check state of energy cell.
  76.     if(r.getInput(5) < 15) then
  77.       --Energy cell is not fully charged.
  78.       r.setOutput(2, 15);
  79.     else
  80.       --Energy cell is fully charged.
  81.       r.setOutput(2, 0);
  82.     end
  83.  
  84.     --Check lava levels and send it to the mainframe.
  85.     local info = getTankInfo();
  86.     local procentage = math.floor((info["amount"] / info["max"]) * 100);
  87.  
  88.     m.send(address, port, "tankUpdate", procentage);
  89.   end
  90. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement