Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.close();
- local modem = peripheral.find("modem", rednet.open);
- local el_door = 0;
- local elevator = 0;
- local floors = {};
- local reqfloor = "";
- local currentfloorlevel = 0;
- -- rednet.open(modem);
- rednet.host("elevator", "elevator.api");
- -- rednet.broadcast("ELEVATOR");
- redstone.setOutput("bottom", false);
- redstone.setOutput("back", true);
- function commanddoors(command, door)
- print("Door action: " .. command .. " to " .. tostring(el_door));
- rednet.send(el_door, command);
- -- rednet.send(el_door, "close");
- for it, val in pairs(floors) do
- -- print(door == 0);
- print(tostring(val.floorid) .. ": floor " .. val.floorname .. ": " .. tostring(val.floororder) .. " send " .. command);
- if(door == 0) then
- rednet.send(val.floorid, command);
- elseif(door == val.floororder) then
- -- write(" at level " .. val.floorname .. " ( " .. val.floororder .. ")\n");
- rednet.send(val.floorid, command);
- end
- end
- sleep(0.3);
- end
- function elevatormove(action)
- print("Elevator action: " .. action);
- if(action == "stop") then
- redstone.setOutput("bottom", true);
- elseif(action == "start") then
- redstone.setOutput("bottom", false);
- elseif(action == "up") then
- redstone.setOutput("back", true);
- redstone.setOutput("bottom", false);
- elseif(action == "down") then
- redstone.setOutput("back", false);
- redstone.setOutput("bottom", false);
- end
- end
- function main()
- com, message = rednet.receive(10);
- -- print(("msg: %s"):format(tostring(message)));
- if (com == nil) then
- return;
- end;
- print(("computer %d sent %s, type: %s"):format(com, message, type(message)));
- if (type(message) == "string" and string.sub(message, 1, 3) == "iam") then
- -- print(("computer %d sent %s"):format(com, string.sub(message, 4, 8)));
- if(string.sub(message, 4, 11) == "el_door") then
- el_door = com;
- elseif(string.sub(message, 4, 12) == "elevator") then
- elevator = com;
- elseif(string.sub(message, 4, 8) == "floor") then
- -- print(("Vals %d, %d, %s"):format(com, tonumber(string.sub(message, 9, 10)), string.sub(message, 11, 24)));
- floors[#floors + 1] = {
- floorid = com,
- floororder = tonumber(string.sub(message, 9, 10)),
- floorname = string.sub(message, 11, 24)
- };
- print("Floor " .. floors[#floors].floorname .. ": " .. tostring(floors[#floors].floororder));
- end
- end
- if (type(message) == "string" and string.sub(message, 1, 4) == "door") then
- commanddoors(string.sub(message, 5, 10), currentfloorlevel);
- -- rednet.send(el_door, string.sub(message, 5, 10));
- end
- if (type(message) == "string" and string.sub(message, 1, 8) == "elevator") then
- commanddoors("close");
- elevatormove(string.sub(message, 9, 20));
- commanddoors("close");
- end
- if (type(message) == "string" and string.sub(message, 1, 2) == "at") then
- -- currentfloorlevel = string.sub(message, 3, 16);
- for it, val in pairs(floors) do
- if(val.floorname == string.sub(message, 3, 16)) then
- currentfloorlevel = val.floororder;
- end
- end
- if(reqfloor == string.sub(message, 3, 16)) then
- redstone.setOutput("bottom", true);
- for it, val in pairs(floors) do
- if(val.floorname == reqfloor) then
- currentfloorlevel = val.floororder;
- rednet.send(val.floorid, "open");
- end
- end
- end
- end
- if (type(message) == "string" and string.sub(message, 1, 7) == "request") then
- for it, val in pairs(floors) do
- print("Floor " .. tostring(string.sub(message, 8, 20) == val.floorname));
- -- print("Order " .. tostring(val.floororder == currentfloorlevel));
- print("Order " .. tostring(val.floororder) .. " == " .. tostring(currentfloorlevel));
- if(val.floorname == string.sub(message, 8, 20)) then
- -- Check if higher or lower
- -- open door if equal
- if(val.floororder < currentfloorlevel) then
- elevatormove("up");
- return;
- elseif(val.floororder > currentfloorlevel) then
- elevatormove("down");
- return;
- elseif(val.floororder == currentfloorlevel) then
- commanddoors("open", currentfloorlevel);
- return;
- end
- end
- end
- end
- end
- while true do
- main();
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement