Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- FILLEDBOX
- local function drawPixelInternal(xPos, yPos)
- term.setCursorPos(xPos, yPos)
- term.write(" ")
- end
- local tColourLookup = {}
- for n = 1, 16 do
- tColourLookup[string.byte("0123456789abcdef", n, n)] = 2 ^ (n - 1)
- end
- function drawFilledBox(startX, startY, endX, endY, nColour)
- if type(startX) ~= "number" or type(startX) ~= "number" or type(endX) ~=
- "number" or type(endY) ~= "number" or
- (nColour ~= nil and type(nColour) ~= "number") then
- error("Expected startX, startY, endX, endY, colour", 2)
- end
- startX = math.floor(startX)
- startY = math.floor(startY)
- endX = math.floor(endX)
- endY = math.floor(endY)
- if nColour then term.setBackgroundColor(nColour) end
- if startX == endX and startY == endY then
- drawPixelInternal(startX, startY)
- return
- end
- local minX = math.min(startX, endX)
- if minX == startX then
- minY = startY
- maxX = endX
- maxY = endY
- else
- minY = endY
- maxX = startX
- maxY = startY
- end
- for x = minX, maxX do for y = minY, maxY do drawPixelInternal(x, y) end end
- end
- -- Importa enti
- local h = fs.open("qlvdata-published", "r")
- enti = textutils.unserialize(h.readAll())
- h.close()
- -- Importa itinerari
- local h2 = fs.open("qlvd", "r")
- itinerari = textutils.unserialize(h2.readAll())
- h2.close()
- -- Importa scritte
- local h3 = fs.open("qlvtxt", "r")
- txt = textutils.unserialize(h3.readAll())
- h3.close()
- -- Importa pedali
- local h4 = fs.open("qlvpedali", "r")
- pedali = textutils.unserialize(h4.readAll())
- h4.close()
- -- Importa nome
- local h5 = fs.open("qlvname", "r")
- nome = h5.readAll()
- h5.close()
- -- Variabili da popolare
- output = {}
- clicktoit = {}
- status = {}
- locked = {}
- active = {}
- _active = {}
- pedaletoit = {}
- statuspedali = {}
- itnametoitkey = {}
- auta = false
- canRotate = true
- statusinfo = colors.gray
- tm = nil
- scenery = 69
- blwait = false
- ccshut = false
- apt = 0
- -- Variabili statiche
- bundled = "bottom"
- pedaliSide = "left"
- seq = {"-","\\","|","/"}
- lasts = 0
- autax = 44
- autay = 3
- autatext = " AutA "
- allowedchanges = {}
- -- Funzioni
- function ac()
- allowedchanges[3] = {5}
- allowedchanges[2] = {5}
- allowedchanges[1] = {2,3,5}
- allowedchanges[5] = {0,4}
- end
- function checkAutA()
- if fs.exists(".auta") == true then
- auta = true
- handleAutAOutput()
- return
- end
- auta = false
- handleAutAOutput()
- end
- function handleAutAOutput()
- if auta == true then
- rs.setBundledOutput(pedaliSide, colors.blue)
- return
- end
- rs.setBundledOutput(pedaliSide, 0)
- end
- function switchAutA()
- if auta == true then
- fs.delete(".auta")
- else
- local h = fs.open(".auta", "w")
- h.write("on")
- h.close()
- end
- checkAutA()
- end
- function bitand(a, b)
- local result = 0
- local bitval = 1
- while a > 0 and b > 0 do
- if a % 2 == 1 and b % 2 == 1 then -- test the rightmost bits
- result = result + bitval -- set the current bit
- end
- bitval = bitval * 2 -- shift left
- a = math.floor(a/2) -- shift right
- b = math.floor(b/2)
- end
- return result
- end
- function associatePedale()
- for k, v in ipairs(itinerari) do
- itnametoitkey[v.name] = k
- local pedale = pedali[v.pedale]
- if pedaletoit[pedale.color] == nil then
- pedaletoit[pedale.color] = {}
- end
- table.insert(pedaletoit[pedale.color], k)
- end
- end
- function getGiragira()
- if canRotate == true then
- lasts = lasts+1
- if lasts > 4 then
- lasts = 1
- end
- end
- return seq[lasts]
- end
- function contains(table, element)
- for k, v in pairs(table) do
- if v == element then
- return true
- end
- end
- return false
- end
- function containsString(table, element)
- for k, v in pairs(table) do
- if tostring(v) == tostring(element) then
- --print("ELEMENT: "..k)
- return true
- end
- end
- return false
- end
- function removeSingolo(tb, element)
- local newt = tb
- for k, v in ipairs(tb) do
- if v == element then
- table.remove(newt, k)
- return newt
- end
- end
- return newt
- end
- function proposeTimer()
- tm = os.startTimer(1)
- end
- function titolo(testo)
- local maxw = 51
- drawFilledBox(1, 1, maxw, 1, colors.blue)
- term.setCursorPos((maxw - #testo) / 2, 1)
- term.setTextColor(colors.white)
- term.write(testo)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- end
- function qlv()
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- titolo(nome.." - ACC")
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- local qlv = paintutils.loadImage("qlv")
- paintutils.drawImage(qlv, 2, 1)
- drawEnti()
- drawItinerari()
- drawTesti()
- end
- function drawEnti()
- for k, v in ipairs(enti) do
- local c = colors.lightGray
- if v.type == "d" then
- c = colors.gray
- end
- if v.type == "s" then
- c = colors[loadStato(v)]
- term.setBackgroundColor(c)
- drawPixelInternal(v.position.x, v.position.y)
- else
- local visual = v.def
- if status[v.color] == true then visual = v.active end
- term.setBackgroundColor(c)
- term.setCursorPos(v.position.x, v.position.y)
- term.setTextColor(colors.white)
- term.write(visual)
- end
- end
- end
- function drawTesti()
- for k, v in ipairs(txt) do
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- term.setCursorPos(v.x, v.y)
- term.write(v.text)
- end
- end
- function drawItinerari()
- for k, v in ipairs(itinerari) do
- local y = 16
- local x = k-1
- if k > 6 then
- y = 18
- x = k-8
- end
- drawItinerarioBox(k, v, x, y)
- end
- end
- function drawItinerarioBox(k, v, x, y)
- local length = 8
- local p = x*length
- local cr = colors.blue
- if auta == true then
- cr = colors.gray
- end
- if active[k] ~= nil then
- cr = colors.red
- end
- drawFilledBox(p+2, y, p+length-1, y, cr)
- term.setBackgroundColor(cr)
- term.setTextColor(colors.white)
- term.setCursorPos(p+2, y)
- term.write(" "..v.name)
- for i=p+1,p+length,1 do
- clicktoit[i.."_"..y] = k
- end
- end
- function drawAutAButton()
- local color = colors.red
- if auta == true then
- color = colors.green
- end
- --drawFilledBox(autax, autay, autax + #autatext, autay, color)
- term.setTextColor(colors.white)
- term.setBackgroundColor(color)
- term.setCursorPos(autax, autay)
- term.write(autatext)
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- end
- function loadStato(element)
- if status[element.color] == true then
- local c = element.active
- if c == "off" then return "lightGray" end
- return c
- else
- local c = element.def
- if c == "off" then return "lightGray" end
- return c
- end
- end
- function comandaItinerario(id, internal)
- if internal == false then
- if auta == true then return end
- setStatusInfo(colors.yellow)
- sleep(1)
- proposeTimer()
- end
- local itinerario = itinerari[id]
- if active[id] ~= nil then
- if internal == false then erore() end
- return
- end
- if canCreateItinerario(itinerario) ~= true then
- if internal == false then erore() end
- return
- end
- active[id] = true
- table.insert(_active, itinerario)
- -- Attiva gli enti necessari
- for k, v in ipairs(itinerario.enti) do
- table.insert(output, v)
- status[v] = true
- end
- -- Blocca gli enti non necessari
- for k, v in ipairs(itinerario.lock) do
- table.insert(locked, v)
- end
- refreshBundledOutput()
- if internal == false then
- setStatusInfo(colors.green)
- end
- end
- function erore()
- setStatusInfo(colors.red)
- sleep(0.5)
- setStatusInfo(colors.gray)
- end
- function distruggiItinerario(id, internal)
- if internal == false then
- if auta == true then return end
- setStatusInfo(colors.orange)
- sleep(1)
- proposeTimer()
- end
- local itinerario = itinerari[id]
- if active[id] == nil then
- if internal == false then erore() end
- return
- end
- -- Disable all
- for k, v in ipairs(itinerario.enti) do
- output = removeSingolo(output, v)
- status[v] = false
- end
- -- Unlock
- for k, v in ipairs(itinerario.lock) do
- locked = removeSingolo(locked, v)
- end
- active[id] = nil
- removeSingolo(_active, itinerario)
- refreshBundledOutput()
- if internal == false then
- setStatusInfo(colors.green)
- end
- end
- function refreshBundledOutput()
- local used = {}
- local color = 0
- for k, v in ipairs(output) do
- if used[v] == nil then
- used[v] = true
- color = color + colors[v]
- end
- end
- rs.setBundledOutput(bundled, color)
- end
- function setStatusInfo(color)
- statusinfo = color
- drawStatusInfo()
- end
- function drawStatusInfo()
- term.setBackgroundColor(statusinfo)
- term.setTextColor(colors.white)
- term.setCursorPos(51, 19)
- term.write(getGiragira())
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- end
- function canCreateItinerario(it)
- if #_active >= 2 then
- return false
- end
- if #_active == 0 then
- return true
- end
- local act = _active[1]
- --error(textutils.serialize(_active))
- local common = 0
- for k, v in ipairs(it.lock) do
- if contains(locked, v) == true then
- common = common + 1
- end
- end
- --print(act.direction.."_"..it.direction)
- --error(common)
- if act.direction ~= it.direction then
- if common > 3 then
- return false
- end
- else
- if common > 3 then
- return false
- end
- end
- return true
- end
- function handleRedstoneVariation()
- apt = apt + 1
- local input = rs.getBundledInput(pedaliSide)
- --error(textutils.serialize(ipairs(pedaletoit)))
- for k, v in pairs(pedaletoit) do
- local status = bitand(input, colors[k])
- if status == 0 then
- statuspedali[k] = false
- else
- statuspedali[k] = true
- end
- if status ~= 0 then
- -- Is active
- for ka, va in ipairs(v) do
- distruggiItinerario(va, true)
- end
- end
- end
- statuspedali["c"] = apt
- --print(textutils.serialize(statuspedali))
- --exit()
- end
- function AutALogic()
- if auta == false then
- return
- end
- local newscenery = nil
- if containsString(statuspedali, true) == false then
- if scenery == 69 then
- newscenery = 0
- else
- newscenery = scenery
- end
- --print(textutils.serialize(statuspedali))
- if blwait == true then
- ccshut = true
- end
- end
- if statuspedali["black"] == true and statuspedali["red"] == false and statuspedali["cyan"] == false and statuspedali["green"] == false then
- -- Libero transito da Grottalupara
- newscenery = 0
- end
- if statuspedali["cyan"] == true and statuspedali["black"] == true then
- -- Treno in arrivo da entrambi i lati
- newscenery = 1
- end
- if statuspedali["cyan"] == true and statuspedali["red"] == true then
- -- Treno in arrivo da thwolle e treno da Grottalupara fermo al bin 2
- if blwait == true then
- newscenery = 5
- ccshut = true
- else
- newscenery = 2
- end
- end
- if statuspedali["black"] == true and statuspedali["green"] == true then
- -- Treno in arrivo da Grottalupara e treno da THwolle fermo al bin 1
- if blwait == true then
- newscenery = 5
- ccshut = true
- else
- newscenery = 3
- end
- end
- if statuspedali["black"] == false and statuspedali["red"] == false and statuspedali["cyan"] == true and statuspedali["green"] == false then
- -- Libero transito da Thwolle
- newscenery = 4
- end
- if statuspedali["black"] == false and statuspedali["cyan"] == false and statuspedali["red"] == true and statuspedali["green"] == true then
- -- DUe treni fermi ai 2 bin per incrocio i guess
- newscenery = 5
- blwait = true
- end
- if statuspedali["red"] == true and statuspedali["black"] == false and statuspedali["cyan"] == false then
- -- Treno di un libero transito entrato in stazione
- if scenery == 4 or scenery == 6 then
- newscenery = 6
- elseif scenery == 0 or scenery == 7 then
- newscenery = 7
- end
- end
- if allowedchanges[scenery] ~= nil and contains(allowedchanges[scenery], newscenery) == false then
- newscenery = scenery
- end
- if ccshut == true then
- newscenery = 0
- ccshut = false
- blwait = false
- end
- --print(textutils.serialize(statuspedali))
- if newscenery ~= scenery then
- --print(containsString(statuspedali, true))
- --print(tostring(blwait))
- --print(tostring(ccshut))
- print(newscenery)
- --print(textutils.serialize(statuspedali))
- destroyAll()
- destroyAll()
- scenery = newscenery
- if scenery == nil then
- return
- end
- AutAScenery()
- end
- end
- function AutAScenery()
- if scenery == 0 then
- comandaItinerario(itnametoitkey["1-II"], true)
- comandaItinerario(itnametoitkey["II-2"], true)
- elseif scenery == 1 then
- comandaItinerario(itnametoitkey["1-II"], true)
- comandaItinerario(itnametoitkey["2-I"], true)
- elseif scenery == 2 then
- comandaItinerario(itnametoitkey["2-I"], true)
- elseif scenery == 3 then
- comandaItinerario(itnametoitkey["1-II"], true)
- elseif scenery == 4 then
- comandaItinerario(itnametoitkey["2-II"], true)
- comandaItinerario(itnametoitkey["II-1"], true)
- elseif scenery == 5 then
- comandaItinerario(itnametoitkey["I-1"], true)
- comandaItinerario(itnametoitkey["II-2"], true)
- elseif scenery == 6 then
- comandaItinerario(itnametoitkey["II-1"], true)
- elseif scenery == 7 then
- comandaItinerario(itnametoitkey["II-2"], true)
- else
- error("CANNOT HANDLE SCENERY "..scenery)
- end
- end
- function destroyAll()
- for k, v in ipairs(itinerari) do
- distruggiItinerario(k, true)
- end
- if #_active > 0 then
- error("NON HO CANCELLATO TUTTO . "..textutils.serialize(active))
- end
- end
- function drawLogo()
- term.setCursorPos(1, 2)
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- term.write("ALST M")
- term.setTextColor(colors.red)
- term.setCursorPos(5, 2)
- term.write("O")
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- end
- -- DISPLAY
- ac()
- associatePedale()
- checkAutA()
- term.setBackgroundColor(colors.white)
- term.clear()
- proposeTimer()
- handleRedstoneVariation()
- while true do
- qlv()
- drawStatusInfo()
- drawAutAButton()
- drawLogo()
- AutALogic()
- local ev, b, x, y = os.pullEvent() -- mouse_click AND redstone
- handleRedstoneVariation()
- if ev == "timer" and b == tm then
- proposeTimer()
- --handleRedstoneVariation()
- canRotate = true
- else
- canRotate = false
- end
- if ev == "redstone" then
- --handleRedstoneVariation()
- elseif ev == "mouse_click" then
- local qid = clicktoit[x.."_"..y]
- if qid ~= nil then
- -- Comanda itinerario
- if active[qid] == nil then
- comandaItinerario(qid, false)
- else
- distruggiItinerario(qid, false)
- end
- end
- if y == autay and x >= autax and x <= autax+#autatext then
- -- Handle AutA ONLY IF THE CONDITIONS ARE OK
- setStatusInfo(colors.yellow)
- sleep(1)
- --proposeTimer()
- if contains(statuspedali, true) == false then
- switchAutA()
- setStatusInfo(colors.green)
- else
- setStatusInfo(colors.red)
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment