-- inizializzazione periferiche
modem = nil
for _, side in pairs(rs.getSides()) do
if peripheral.getType(side) == "modem" then
modem = side
break
end
end
monitor = nil
for _, side in pairs(rs.getSides()) do
if peripheral.getType(side) == "monitor" then
monitor = side
break
end
end
-- codice avvio
sleep(1)
print("< tragitti >")
sleep(1)
print("gestore")
sleep(1)
print("assegnazione")
sleep(1)
print("inizializzazione")
sleep(1)
print("connessione")
rednet.open(modem)
rednet.host("SCGIRC","gestore_tragitti")
rednet.broadcast({"avvio","gestore","tragitti"})
sleep(1)
print("funzionamento")
-- codice funzioni
function configura_versori(v)
q = nil
g = nil
w = nil
xd = nil
yd = nil
if v == ve then
q = "esistente"
g = C.de
w = N.we
end
if v == vs then
q = "esistente"
g = C.ds
w = N.ws
end
if v == vw then
q = "esistente"
g = C.dw
w = N.ww
end
if v == vn then
q = "esistente"
g = C.dn
w = N.wn
end
end
function configura_blocchi(q,g,v,d)
k = nil
xd = nil
yd = nil
if q == "esistente" and g == "gestita" then
k = v*N.cd+d
end
if C.cb == "gestita" then
xd = N.cx
yd = N.cy
end
if C.cb == "ingestita" then
xd = N.cz
yd = N.cz
end
end
function configura_fermata(v)
p = nil
l = nil
if v == N.ve then
p = N.bf+1
l = v*N.cd+c
end
if v == N.vs then
p = N.bf+1
l = v*N.cd+c
end
if v == N.vw then
p = N.bf+2
l = v*N.cd+c
end
if v == N.vn then
p = N.bf+2
l = v*N.cd+c
end
end
function tragitto_configura(v,d,c)
cnfg = nil
cnf1 = nil
cnf2 = nil
rednet.broadcast({"configura","tragitto",v,d,c})
while true do
local id, codice = rednet.receive()
if codice == {"tragitto",v,d,c,"deviatoi","configurato"} then
cnf1 = true
end
if codice == {"tragitto",v,d,c,"segnali","configurato"} then
cnf2 = true
end
if codice == {"tragitto",v,d,c,"deviatoi","indefinito"} then
cnf1 = false
end
if codice == {"tragitto",v,d,c,"segnali","indefinito"} then
cnf2 = false
end
if codice == {"tragitto",v,d,c,"deviatoi","errore"} then
cnf1 = false
end
if codice == {"tragitto",v,d,c,"segnali","errore"} then
cnf2 = false
end
if cnf1 == true and cnf2 == true then
cnfg = true
break
end
if cnf1 == false or cnf2 == false then
cnfg = false
break
end
end
end
function tragitto_transito(v1,v2,d1,d2,c,k)
tragitto_configura(v1,d1,c)
if cnfg == true then
cnc1 = true
end
if cnfg == false then
cnc1 = false
end
tragitto_configura(v2,d2,k)
if cnfg == true then
cnc2 = true
end
if cnfg == false then
cnc2 = false
end
if cnc1 == true and cnc2 == true then
cnct = true
end
if cnc1 == false or cnc2 == false then
cnct = false
end
end
function tragitto_binario(c)
erro = nil
rednet.broadcast({"stato","binario",c})
local id, codice = rednet.receive()
if codice == {"binario",c,"libero"} then
erro = false
end
if codice == {"binario",c,"indefinito"} then
erro = true
end
if codice == {"binario",c,"occupato"} then
erro = true
end
if codice == {"binario",c,"transito"} then
erro = true
end
end
function tragitto_elabora(v,b,x,y)
erro = nil
for i = 1 , b do
if v == N.ve then
j = i
u = y
end
if v == N.vs then
j = i
u = y
end
if v == N.vw then
j = b-i
u = x
end
if v == N.vn then
j = b-i
u = x
end
r = i
c = j+x
rednet.broadcast({"stato","binario",c})
local id, codice = rednet.receive()
if codice == {"binario",c,"libero"} then
erro = false
break
end
if codice == {"binario",c,"indefinito"} then
erro = true
end
if codice == {"binario",c,"occupato"} then
erro = true
end
if codice == {"binario",c,"transito"} then
erro = true
end
end
end
function tragitto_prepara(t,v,d,n,s,r,c,erro)
if erro == false then
tragitto_configura(v,d,c)
if cnfg == true then
rednet.broadcast({t,v,n,r,"preparato"})
end
if cnfg == false then
rednet.broadcast({t,v,n,s,"eccezione"})
end
end
if erro == true then
rednet.broadcast({t,v,n,s,"impossibile"})
end
end
function transito_binario(c,k)
ste1 = nil
ste2 = nil
tragitto_binario(c)
ste1 = erro
tragitto_binario(k)
ste2 = erro
end
function transito_elabora(v,d,b,x,y,k)
ste1 = nil
ste2 = nil
tragitto_elabora(v,ve,vs,vw,vn,d,b,x,y)
ste1 = erro
tragitto_binario(k)
ste2 = erro
end
function transito_prepara(v1,v2,d1,d2,n,s,r,c,k,ste1,ste2)
step = nil
if ste1 == false and ste2 == false then
step = false
end
if ste1 == true or ste2 == true then
step = true
end
if step == false then
tragitto_transito(v1,v2,d1,d2,c,k)
if cnct == true then
rednet.broadcast({"transito",v1,v2,n,r,"preparato"})
end
if cnct == false then
if cnc1 == false then
rednet.broadcast({"transito",v1,v2,n,s,"imprevisto","binario"})
end
if cnc2 == false then
rednet.broadcast({"transito",v1,v2,n,s,"imprevisto","linea"})
end
rednet.broadcast({"transito",v1,v2,n,s,"eccezione"})
end
end
if step == true then
if ste1 == true then
rednet.broadcast({"transito",v1,v2,n,s,"blocco_binario"})
end
if ste2 == true then
rednet.broadcast({"transito",v1,v2,n,s,"blocco_linea"})
end
rednet.broadcast({"transito",v1,v2,n,s,"impossibile"})
end
end
function transito_verifica(v,d,n,f,b,x,y)
if f == "ammesso" then
rednet.broadcast({"transito","eccezionale",n,f})
sleep(1)
tragitto_elabora(v,b,x,y)
er = r
ec = c
if erro == true then
rednet.broadcast({"transito","eccezionale",n,"impossibile"})
vrfy = nil
end
if erro == false then
rednet.broadcast({"transito","eccezionale",n,"possibile"})
vrfy = n
end
end
if f == "vietato" then
rednet.broadcast({"transito","eccezionale",n,f})
end
r = nil
c = nil
end
function transito_dedicato(v1,v2,d1,d2,n,b,s,r,c,k,x,y)
if b == 0 then
rednet.broadcast({"transito","funzione",n,"inesistente"})
end
if b ~= 0 then
if s == nil then
rednet.broadcast({"transito",v1,v2,n,"indefinito"})
end
if s == "elabora" then
transito_elabora(v1,d1,b,x,y,k)
transito_prepara(v1,v2,d1,d2,n,s,r,c,k,ste1,ste2)
t = nil
end
if r ~= nil then
c = r+x
if c > y then
rednet.broadcast({"transito",v1,v2,n,"binario","inesistente"})
end
if c < y then
transito_binario(c,k)
transito_prepara(v1,v2,d1,d2,n,s,r,c,k,ste1,ste2)
end
end
end
end
function transito_eccezionale(v1,v2,d1,d2,n,f,s,r,c,k)
if f == "vietato" then
rednet.broadcast({"transito","funzione",n,f})
end
if f == "ammesso" then
if s == nil then
rednet.broadcast({"transito",v1,v2,"eccezionale","binario","indefinito"})
end
if s == "elabora" then
vrfy = nil
if vrfy == nil then
n = "passeggeri"
transito_verifica(v,d,n,F.tp,C.bp,N.xp,N.yp)
ste1 = erro
end
if vrfy == nil then
n = "merci"
transito_verifica(v,d,n,F.tm,C.bm,N.xm,N.ym)
ste1 = erro
end
if vrfy == nil then
n = "servizi"
transito_verifica(v,d,n,F.ts,B.bs,N.xs,N.ys)
ste1 = erro
end
tragitto_binario(k)
ste2 = erro
transito_prepara(v1,v2,d1,d2,n,s,er,ec,k,ste1,ste2)
end
if r ~= nil then
c = r
if r > C.ls then
rednet.broadcast({"transito",v1,v2,"eccezionale",r,"limite","sistemi"})
end
if r < C.ls then
transito_binario(c,k)
transito_prepara(v1,v2,d1,d2,n,s,r,c,k,ste1,ste2)
end
end
end
end
function prepara_arrivo(v,d,n,b,s,r,k,x,y)
if b == 0 then
rednet.broadcast({"arrivo","funzione",n,"inesistente"})
end
if b ~= 0 then
if s == nil then
rednet.broadcast({"arrivo",v,n,"binario","indefinito"})
end
if s == "elabora" then
tragitto_elabora(v,b,x,y)
tragitto_prepara("arrivo",v,d,n,s,r,c,erro)
r = nil
end
if r ~= nil then
c = r+x
if c > y then
rednet.broadcast({"arrivo",v,n,"binario","inesistente"})
end
if c < y then
tragitto_binario(c)
tragitto_prepara("arrivo",v,d,n,s,r,c,erro)
end
end
end
end
function prepara_partenza(v,d,n,b,s,r,k,x,y)
if b == 0 then
rednet.broadcast({"partenza","funzione",n,"inesistente"})
end
if b ~= 0 then
if s == nil then
rednet.broadcast({"partenza",v,n,"binario","indefinito"})
end
if r ~= nil then
c = r+x
if c > y then
rednet.broadcast({"partenza",v,n,"binario","inesistente"})
end
if c < y then
tragitto_binario(k)
tragitto_prepara("partenza",v,d,n,s,r,c,erro)
end
end
end
end
function prepara_eccezionale(t,v,d,b,s,r,c,k)
if s == nil then
rednet.broadcast({t,v,"eccezionale","binario","indefinito"})
end
if s ~= nil then
c = r
if r > C.ls then
rednet.broadcast({t,v,"eccezionale","limite","sistemi"})
end
if r < C.ls then
if B.xp < r and r < B.yp or B.xm < r and r < B.ym or B.xs < r and r < B.ys then
tragitto_binario(k)
tragitto_prepara(t,v,d,n,s,r,c,erro)
else
rednet.broadcast({t,v,"eccezionale","binario","inesistente"})
end
end
end
end
function prepara_fermata(v,w,xd,yd,n,f,s,r,c,k)
if f == "vietato" then
rednet.broadcast({"fermata","funzione",n,f})
end
if f == "ammesso" then
if r == "arrivo" then
tragitto_binario(c)
tragitto_prepara("fermata",v,xd,n,s,r,c,erro)
end
if r == "partenza" then
tragitto_binario(k)
tragitto_prepara("fermata",w,yd,n,s,r,c,erro)
end
end
end
-- codice sistema
rednet.broadcast({"istruisci","N"})
id, N = rednet.receive()
rednet.broadcast({"istruisci","C"})
id, C = rednet.receive()
rednet.broadcast({"istruisci","B"})
id, B = rednet.receive()
while true do
local id, codice = rednet.receive()
if codice[1] == "prepara" then
if codice[2] == "arrivo" then
t = codice[2]
v = tonumber(codice[3])
n = codice[4]
s = codice[5]
r = tonumber(codice[5])
configura_versori(v)
configura_blocchi(q,g,v,xd)
if q == "esistente" and g == "gestita" then
if n == "passeggeri" then
prepara_arrivo(v,xd,n,C.bp,s,r,k,B.xp,B.yp)
end
if n == "merci" then
prepara_arrivo(v,xd,n,C.bm,s,r,k,B.xm,B.ym)
end
if n == "servizi" then
prepara_arrivo(v,xd,n,C.bs,s,r,k,B.xs,B.ys)
end
if n == "eccezionale" then
prepara_eccezionale(t,v,xd,n,s,r,c,k)
end
end
if q == "esistente" and g == "ingestita" then
rednet.broadcast({"arrivo",v,"cardinalita'",g})
end
if q == "inesistente" then
rednet.broadcast({"arrivo",v,"cardinalita'",q})
end
if v == nil then
rednet.broadcast({"arrivo","cardinalita'","indefinita"})
end
end
if codice[2] == "partenza" then
t = codice[2]
v = tonumber(codice[3])
n = codice[4]
s = codice[5]
r = tonumber(codice[5])
configura_versori(v)
configura_blocchi(q,g,v,yd)
if q == "esistente" and g == "gestita" then
if n == "passeggeri" then
prepara_partenza(v,yd,n,C.bp,s,r,k,B.xp,B.yp)
end
if n == "merci" then
prepara_partenza(v,yd,n,C.bm,s,r,k,B.xm,B.ym)
end
if n == "servizi" then
prepara_partenza(v,yd,n,C.bs,s,r,k,B.xs,B.ys)
end
if n == "eccezionale" then
prepara_eccezionale(t,v,yd,n,s,r,c,k)
end
end
if q == "esistente" and g == "ingestita" then
rednet.broadcast({"partenza",v,"cardinalita'",g})
end
if q == "inesistente" then
rednet.broadcast({"partenza",v,"cardinalita'",q})
end
if v == nil then
rednet.broadcast({"partenza","cardinalita'","indefinita"})
end
end
if codice[2] == "fermata" then
t = codice[2]
v = tonumber(codice[3])
n = codice[4]
s = codice[5]
r = codice[5]
configura_versori(v)
configura_fermata(v)
configura_blocchi(q,g,v,yd)
if q == "esistente" and g == "gestita" then
if n == "parallela" then
prepara_fermata(v,w,xd,yd,n,F.fp,s,r,p,k)
end
if n == "lineare" then
prepara_fermata(v,w,xd,yd,n,F.fl,s,r,p,k)
end
end
if q == "esistente" and g == "ingestita" then
rednet.broadcast({"fermata",v,"cardinalita'",g})
end
if q == "inesistente" then
rednet.broadcast({"fermata",v,"cardinalita'",q})
end
if v == nil then
rednet.broadcast({"fermata","cardinalita'","indefinita"})
end
end
if codice[2] == "transito" then
t = codice[2]
v1 = tonumber(codice[3])
v2 = tonumber(codice[4])
q1 = nil
q2 = nil
g1 = nil
g2 = nil
n = codice[5]
s = codice[6]
r = tonumber(codice[6])
configura_versori(v1)
q1 = q
g1 = g
configura_versori(v2)
q2 = q
g2 = g
configura_blocchi(q,g,v,yd)
d1 = xd
d2 = yd
if q1 == "esistente" and q2 == "esistente" then
q = "esistente"
end
if q1 == "inesistente" or q2 == "inesistente" then
q = "inesistente"
end
if g1 == "gestita" and g2 == "gestita" then
g = "gestita"
end
if g1 == "ingestita" or g2 == "ingestita" then
g = "ingestita"
end
if q == "esistente" and g == "gestita" then
if n == "dedicato" then
transito_dedicato(v1,v2,d1,d2,n,C.bt,s,r,c,k,B.xt,B.yt)
end
if n == "eccezionale" then
transito_eccezionale(v1,v2,d1,d2,n,F.te,s,r,c,k)
end
end
if q == "esistente" and g == "ingestita" then
rednet.broadcast({"transito",v1,v2,"cardinalita'",g})
end
if q == "inesistente" then
rednet.broadcast({"transito",v1,v2,"cardinalita'",q})
end
if v == nil then
rednet.broadcast({"transito","cardinalita'","indefinita"})
end
end
end
if codice == {"sistemi","gestore","tragitti","spegnimento"} then
rednet.broadcast({"spegnimento","gestore","tragitti"})
os.shutdown()
end
if codice == {"sistemi","gestore","tragitti","riavvio"} then
rednet.broadcast({"riavvio","gestore","tragitti"})
os.reboot()
end
end