Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Init device
- local function initDevice()
- rednet.open("back")
- return peripheral.wrap("front")
- end
- -- Init floor
- local function initFloor()
- tab = {}
- tab[0] = "B1"
- tab[1] = "GF"
- return tab
- end
- local function numFloor(f, tab)
- result = -1
- for i = 0, #tab do
- if tab[i] == f then
- result = i
- end
- end
- return result
- end
- -- Etage
- local function getFloor()
- -- If test
- if rs.testBundledInput("right", 1) then
- return 1 -- White
- end
- if rs.testBundledInput("right", 2) then
- return 0 -- Orange
- end
- return -1
- end
- -- Nouvelle etage
- local function getNextFloor(tableau)
- return tableau[0]
- end
- -- Ajout
- local function addFloor(tableau, etage)
- local i = 0
- while tableau[i] ~= nil do
- i = i + 1
- end
- tableau[i] = etage
- return tableau
- end
- -- Suppression
- local function delFloor(tableau)
- local i = 1
- local j = 0
- local tab = {}
- while tableau[i] ~= nil do
- j = i - 1
- tab[j] = tableau[i]
- i = i + 1
- end
- print("Deleting ...")
- return tab
- end
- local function openDoor(fl)
- print("open door")
- for i = 1, 4 do
- if fl == 0 then
- rs.setBundledOutput("right", 8)
- elseif fl == 1 then
- rs.setBundledOutput("right", 32)
- end
- sleep(0.5)
- rs.setBundledOutput("right", 0)
- sleep(0.5)
- end
- end
- local function closeDoor(fl)
- print("close door")
- for i = 1, 4 do
- if fl == 0 then
- rs.setBundledOutput("right", 4)
- elseif fl == 1 then
- rs.setBundledOutput("right", 16)
- end
- sleep(0.5)
- rs.setBundledOutput("right", 0)
- sleep(0.5)
- end
- end
- -- Main function fields
- local actualFloor = "CF"
- local floorSelect = "CF"
- local floors = {}
- local etageTest = -1
- local etageActuel = 5
- local direction = 0
- local stateLift = 1
- local tickWait = 0
- local tickMax = 5
- m = initDevice()
- f = initFloor()
- while true do
- local s, msg, distance = rednet.receive(0.5)
- local copy = {}
- local bc = {}
- bc["floor"] = "CF"
- bc["f"] = "CF"
- bc["s"] = " "
- if msg ~= nil and s ~= nil then
- copy = textutils.unserialize(msg)
- end
- if copy["floor"] ~= nil then
- floors = addFloor(floors, numFloor(copy["floor"], f))
- print("Add floor : "..copy["floor"].." with id : "..numFloor(copy["floor"], f))
- rednet.send(s, "ok_call")
- end
- -- Add floor and prepare broadcast information
- etageTest = getFloor()
- if etageTest ~= -1 then
- etageActuel = etageTest
- end
- -- 1 = reset
- -- 2 = open door
- -- 3 = wait floor
- -- 4 = close door
- -- 5 = lift UP/DOWN
- if stateLift == 1 then
- stateLift = 2
- elseif stateLift == 2 then
- openDoor(etageActuel)
- stateLift = 3
- elseif stateLift == 3 then
- if tickWait > tickMax then
- direction = getNextFloor(floors)
- if direction ~= nil and direction ~= etageActuel then
- tickWait = 0
- stateLift = 4
- elseif direction ~= nil and direction == etageActuel then
- floors = delFloor(floors)
- direction = etageActuel
- else
- tickWait = 0
- end
- else
- tickWait = tickWait + 1
- end
- elseif stateLift == 4 then
- closeDoor(etageActuel)
- stateLift = 5
- elseif stateLift == 5 then
- -- If < Then up Else if > Then down Else arrived floor
- if etageActuel < direction then
- bc["s"] = "U"
- rs.setBundledOutput("left", 1)
- sleep(0.1)
- rs.setBundledOutput("left", 0)
- sleep(0.1)
- elseif etageActuel > direction then
- bc["s"] = "D"
- rs.setBundledOutput("left", 2)
- sleep(0.1)
- rs.setBundledOutput("left", 0)
- sleep(0.1)
- else
- floors = delFloor(floors)
- stateLift = 1
- end
- end
- bc["f"] = f[etageActuel]
- bc["floor"] = f[etageActuel]
- rednet.broadcast(textutils.serialize(bc))
- sleep(0.5)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement