Advertisement
CaptainResu

elevator_service

Dec 25th, 2022 (edited)
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.79 KB | None | 0 0
  1. rednet.close();
  2. local modem = peripheral.find("modem", rednet.open);
  3.  
  4. local el_door = 0;
  5. local elevator = 0;
  6. local floors = {};
  7. local reqfloor = "";
  8. local currentfloorlevel = 0;
  9.  
  10. -- rednet.open(modem);
  11. rednet.host("elevator", "elevator.api");
  12. -- rednet.broadcast("ELEVATOR");
  13.  
  14. redstone.setOutput("bottom", false);
  15. redstone.setOutput("back", true);
  16.  
  17. function commanddoors(command, door)
  18.     print("Door action: " .. command .. " to " .. tostring(el_door));
  19.     rednet.send(el_door, command);
  20.     -- rednet.send(el_door, "close");
  21.     for it, val in pairs(floors) do
  22.         -- print(door == 0);
  23.         print(tostring(val.floorid) .. ": floor " .. val.floorname .. ": " .. tostring(val.floororder) .. " send " .. command);
  24.         if(door == 0) then
  25.             rednet.send(val.floorid, command);
  26.         elseif(door == val.floororder) then
  27.             -- write(" at level " .. val.floorname .. " ( " .. val.floororder .. ")\n");
  28.             rednet.send(val.floorid, command);
  29.         end
  30.     end
  31.     sleep(0.3);
  32. end
  33.  
  34. function elevatormove(action)
  35.     print("Elevator action: " .. action);
  36.  
  37.     if(action == "stop") then
  38.         redstone.setOutput("bottom", true);
  39.     elseif(action == "start") then
  40.         redstone.setOutput("bottom", false);
  41.     elseif(action == "up") then
  42.         redstone.setOutput("back", true);
  43.         redstone.setOutput("bottom", false);
  44.     elseif(action == "down") then
  45.         redstone.setOutput("back", false);
  46.         redstone.setOutput("bottom", false);
  47.     end
  48. end
  49.  
  50. function main()
  51.     com, message = rednet.receive(10);
  52.     -- print(("msg: %s"):format(tostring(message)));
  53.  
  54.     if (com == nil) then
  55.         return;
  56.     end;
  57.  
  58.     print(("computer %d sent %s, type: %s"):format(com, message, type(message)));
  59.     if (type(message) == "string" and string.sub(message, 1, 3) == "iam") then
  60.         -- print(("computer %d sent %s"):format(com, string.sub(message, 4, 8)));
  61.         if(string.sub(message, 4, 11) == "el_door") then
  62.             el_door = com;
  63.         elseif(string.sub(message, 4, 12) == "elevator") then
  64.             elevator = com;
  65.         elseif(string.sub(message, 4, 8) == "floor") then
  66.             -- print(("Vals %d, %d, %s"):format(com, tonumber(string.sub(message, 9, 10)), string.sub(message, 11, 24)));
  67.             floors[#floors + 1] = {
  68.                 floorid = com,
  69.                 floororder = tonumber(string.sub(message, 9, 10)),
  70.                 floorname = string.sub(message, 11, 24)
  71.             };
  72.             print("Floor " .. floors[#floors].floorname .. ": " .. tostring(floors[#floors].floororder));
  73.         end
  74.     end
  75.  
  76.     if (type(message) == "string" and string.sub(message, 1, 4) == "door") then
  77.         commanddoors(string.sub(message, 5, 10), currentfloorlevel);
  78.         -- rednet.send(el_door, string.sub(message, 5, 10));
  79.     end
  80.  
  81.     if (type(message) == "string" and string.sub(message, 1, 8) == "elevator") then
  82.         commanddoors("close");
  83.  
  84.         elevatormove(string.sub(message, 9, 20));
  85.        
  86.         commanddoors("close");
  87.     end
  88.  
  89.     if (type(message) == "string" and string.sub(message, 1, 2) == "at") then
  90.         -- currentfloorlevel = string.sub(message, 3, 16);
  91.  
  92.         for it, val in pairs(floors) do
  93.             if(val.floorname == string.sub(message, 3, 16)) then
  94.                 currentfloorlevel = val.floororder;
  95.             end
  96.         end
  97.  
  98.         if(reqfloor == string.sub(message, 3, 16)) then
  99.             redstone.setOutput("bottom", true);
  100.  
  101.             for it, val in pairs(floors) do
  102.                 if(val.floorname == reqfloor) then
  103.                     currentfloorlevel = val.floororder;
  104.                     rednet.send(val.floorid, "open");
  105.                 end
  106.             end
  107.         end
  108.     end
  109.  
  110.     if (type(message) == "string" and string.sub(message, 1, 7) == "request") then
  111.         for it, val in pairs(floors) do
  112.             print("Floor " .. tostring(string.sub(message, 8, 20) == val.floorname));
  113.             -- print("Order " .. tostring(val.floororder == currentfloorlevel));
  114.             print("Order " .. tostring(val.floororder) .. " == " .. tostring(currentfloorlevel));
  115.  
  116.             if(val.floorname == string.sub(message, 8, 20)) then
  117.                 -- Check if higher or lower
  118.                 -- open door if equal
  119.  
  120.                 if(val.floororder < currentfloorlevel) then
  121.                     elevatormove("up");
  122.                     return;
  123.                 elseif(val.floororder > currentfloorlevel) then
  124.                     elevatormove("down");
  125.                     return;
  126.                 elseif(val.floororder == currentfloorlevel) then
  127.                     commanddoors("open", currentfloorlevel);
  128.                     return;
  129.                 end
  130.             end
  131.         end
  132.     end
  133. end
  134.  
  135. while true do
  136.     main();
  137. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement