Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Virtual Signal Block System
- -- Concept
- --Copyright 2020 PeachMaster
- --Just kidding no copywrite
- local radio = peripheral.wrap("top")
- local signalBlocks = {TEST_1 = {
- startXPos = -341,
- startYPos = 63,
- startZPos = -51,
- endXPos = -352,
- endYPos = 63,
- endZPos = -51,
- speedLimit = 20,
- nextSpeedLimitLocation = "",
- status = 0,
- },
- TEST_2 = {
- startXPos = -353,
- startYPos = 63,
- startZPos = -51,
- endXPos = -364,
- endYPos = 63,
- endZPos = -51,
- speedLimit = 20,
- nextSpeedLimitLocation = "",
- status = 0,
- },
- TEST_3 = {
- startXPos = -365,
- startYPos = 63,
- startZPos = -51,
- endXPos = -376,
- endYPos = 63,
- endZPos = -51,
- speedLimit = 20,
- nextSpeedLimitLocation = "",
- status = 0,
- },
- TEST_4 = {
- startXPos = -377,
- startYPos = 63,
- startZPos = -51,
- endXPos = -388,
- endYPos = 63,
- endZPos = -51,
- speedLimit = 20,
- nextSpeedLimitLocation = "",
- status = 0,
- },
- TEST_5 = {
- startXPos = -389,
- startYPos = 63,
- startZPos = -51,
- endXPos = -394,
- endYPos = 63,
- endZPos = -51,
- speedLimit = 20,
- nextSpeedLimitLocation = "",
- status = 0,
- },
- TEST_6 = {
- startXPos = -399,
- startYPos = 63,
- startZPos = -55,
- endXPos = -400,
- endYPos = 63,
- endZPos = -66,
- speedLimit = 20,
- nextSpeedLimitLocation = "",
- status = 0,
- },
- TEST_7 = {
- startXPos = -400,
- startYPos = 63,
- startZPos = -67,
- endXPos = -400,
- endYPos = 63,
- endZPos = -78,
- speedLimit = 20,
- nextSpeedLimitLocation = "",
- status = 0,
- },
- TEST_8 = {
- startXPos = -400,
- startYPos = 63,
- startZPos = -79,
- endXPos = -400,
- endYPos = 63,
- endZPos = -84,
- speedLimit = 20,
- nextSpeedLimitLocation = "",
- status = 0,
- },
- TEST_9 = {
- startXPos = -399,
- startYPos = 63,
- startZPos = -85,
- endXPos = -400,
- endYPos = 63,
- endZPos = -91,
- speedLimit = 20,
- nextSpeedLimitLocation = "",
- status = 0,
- },
- TEST_10 = {
- startXPos = -399,
- startYPos = 63,
- startZPos = -92,
- endXPos = -400,
- endYPos = 63,
- endZPos = -108,
- speedLimit = 20,
- nextSpeedLimitLocation = "",
- status = 0,
- },
- }
- local connectedTrains = {
- }
- os.loadAPI("json")
- radio.activate()
- term.redirect(peripheral.find("monitor"))
- local function updateDisplay()
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- print("Virtual Signal Block Demonstrative Example")
- print("By PeachMaster")
- paintutils.drawLine(2,9, 10, 9, colors.orange)
- paintutils.drawLine(11,9, 18, 9, colors.magenta)
- paintutils.drawLine(19,9, 26, 9, colors.purple)
- paintutils.drawLine(27,9, 34, 9, colors.yellow)
- paintutils.drawLine(35,9, 37, 9, colors.green)
- paintutils.drawLine(38,9, 46, 9, colors.pink)
- paintutils.drawLine(46,9, 55, 9, colors.brown)
- paintutils.drawLine(56,9, 58, 9, colors.red)
- paintutils.drawLine(59,9, 67, 9, colors.blue)
- if signalBlocks["TEST_1"].status == 1 then
- paintutils.drawLine(2,9, 10, 9, colors.black)
- else
- paintutils.drawLine(2,9, 10, 9, colors.orange)
- end
- if signalBlocks["TEST_2"].status == 1 then
- paintutils.drawLine(11,9, 18, 9, colors.black)
- else
- paintutils.drawLine(11,9, 18, 9, colors.magenta)
- end
- if signalBlocks["TEST_3"].status == 1 then
- paintutils.drawLine(19,9, 26, 9, colors.black)
- else
- paintutils.drawLine(19,9, 26, 9, colors.purple)
- end
- if signalBlocks["TEST_4"].status == 1 then
- paintutils.drawLine(27,9, 34, 9, colors.black)
- else
- paintutils.drawLine(27,9, 34, 9, colors.yellow)
- end
- if signalBlocks["TEST_5"].status == 1 then
- paintutils.drawLine(35,9, 37, 9, colors.black)
- else
- paintutils.drawLine(35,9, 37, 9, colors.green)
- end
- if signalBlocks["TEST_6"].status == 1 then
- paintutils.drawLine(38,9, 46, 9, colors.black)
- else
- paintutils.drawLine(38,9, 46, 9, colors.pink)
- end
- if signalBlocks["TEST_7"].status == 1 then
- paintutils.drawLine(46,9, 55, 9, colors.black)
- else
- paintutils.drawLine(46,9, 55, 9, colors.brown)
- end
- if signalBlocks["TEST_8"].status == 1 then
- paintutils.drawLine(56,9, 58, 9, colors.black)
- else
- paintutils.drawLine(56,9, 58, 9, colors.red)
- end
- if signalBlocks["TEST_9"].status == 1 then
- paintutils.drawLine(59,9, 67, 9, colors.black)
- else
- paintutils.drawLine(59,9, 67, 9, colors.blue)
- end
- if signalBlocks["TEST_10"].status == 1 then
- paintutils.drawLine(59,9, 67, 9, colors.black)
- else
- paintutils.drawLine(59,9, 67, 9, colors.blue)
- end
- end
- local function compareCoords(coord1, coord2, thepoint)
- if thepoint <= tonumber(coord1) and thepoint >= tonumber(coord2) then
- return true
- else
- return false
- end
- end
- while true do
- local event, side, id, uuid, message, system = os.pullEvent("radio_message")
- local theMessage = json.decode(message)
- if theMessage.funct == "attemptconnection" then
- radio.sendMessage(id, json.encode({funct = "startlevel2", speedLimit = 20, nextSpeedLimit = 0, speedChangeSoon = false, endSoon = false, stationStopSoon = false, mtcStatus = 1}))
- radio.sendMessage(id, json.encode({funct = "message", themessage = "Train identified: "..id..",length ".. string.sub(theMessage.destination,8)..", connected to Virtual Signal Block system"}))
- end
- if theMessage.funct == "update" then
- radio.sendMessage(id, json.encode({funct = "response", speedLimit = 20, nextSpeedLimit = 0, speedChange = false, endSoon = false, stationStopSoon = false, atoStatus = 1, destintaion = ""}))
- if compareCoords(signalBlocks["TEST_1"].startZPos, signalBlocks["TEST_1"].endZPos, math.floor(theMessage.posZ)) and compareCoords(signalBlocks["TEST_1"].startXPos, signalBlocks["TEST_1"].endXPos, math.floor(theMessage.posX)) then
- signalBlocks["TEST_1"].status = 1
- else
- signalBlocks["TEST_1"].status = 0
- end
- if compareCoords(signalBlocks["TEST_2"].startZPos, signalBlocks["TEST_2"].endZPos, math.floor(theMessage.posZ)) and compareCoords(signalBlocks["TEST_2"].startXPos, signalBlocks["TEST_2"].endXPos, math.floor(theMessage.posX)) then
- signalBlocks["TEST_2"].status = 1
- else
- signalBlocks["TEST_2"].status = 0
- end
- if compareCoords(signalBlocks["TEST_3"].startZPos, signalBlocks["TEST_3"].endZPos, math.floor(theMessage.posZ)) and compareCoords(signalBlocks["TEST_3"].startXPos, signalBlocks["TEST_3"].endXPos, math.floor(theMessage.posX)) then
- signalBlocks["TEST_3"].status = 1
- else
- signalBlocks["TEST_3"].status = 0
- end
- if compareCoords(signalBlocks["TEST_4"].startZPos, signalBlocks["TEST_4"].endZPos, math.floor(theMessage.posZ)) and compareCoords(signalBlocks["TEST_4"].startXPos, signalBlocks["TEST_4"].endXPos, math.floor(theMessage.posX)) then
- signalBlocks["TEST_4"].status = 1
- else
- signalBlocks["TEST_4"].status = 0
- end
- if compareCoords(signalBlocks["TEST_5"].startZPos, signalBlocks["TEST_5"].endZPos, math.floor(theMessage.posZ)) and compareCoords(signalBlocks["TEST_5"].startXPos, signalBlocks["TEST_5"].endXPos, math.floor(theMessage.posX)) then
- signalBlocks["TEST_5"].status = 1
- else
- signalBlocks["TEST_5"].status = 0
- end
- if compareCoords(signalBlocks["TEST_6"].startZPos, signalBlocks["TEST_6"].endZPos, math.floor(theMessage.posZ)) and compareCoords(signalBlocks["TEST_6"].startXPos, signalBlocks["TEST_6"].endXPos, math.floor(theMessage.posX)) then
- signalBlocks["TEST_6"].status = 1
- else
- signalBlocks["TEST_6"].status = 0
- end
- if compareCoords(signalBlocks["TEST_7"].startZPos, signalBlocks["TEST_7"].endZPos, math.floor(theMessage.posZ)) and compareCoords(signalBlocks["TEST_7"].startXPos, signalBlocks["TEST_7"].endXPos, math.floor(theMessage.posX)) then
- signalBlocks["TEST_7"].status = 1
- else
- signalBlocks["TEST_7"].status = 0
- end
- if compareCoords(signalBlocks["TEST_8"].startZPos, signalBlocks["TEST_8"].endZPos, math.floor(theMessage.posZ)) and compareCoords(signalBlocks["TEST_8"].startXPos, signalBlocks["TEST_8"].endXPos, math.floor(theMessage.posX)) then
- signalBlocks["TEST_8"].status = 1
- else
- signalBlocks["TEST_8"].status = 0
- end
- if compareCoords(signalBlocks["TEST_9"].startZPos, signalBlocks["TEST_9"].endZPos, math.floor(theMessage.posZ)) and compareCoords(signalBlocks["TEST_9"].startXPos, signalBlocks["TEST_9"].endXPos, math.floor(theMessage.posX)) then
- signalBlocks["TEST_9"].status = 1
- else
- signalBlocks["TEST_9"].status = 0
- end
- if compareCoords(signalBlocks["TEST_10"].startZPos, signalBlocks["TEST_10"].endZPos, math.floor(theMessage.posZ)) and compareCoords(signalBlocks["TEST_10"].startXPos, signalBlocks["TEST_10"].endXPos, math.floor(theMessage.posX)) then
- signalBlocks["TEST_10"].status = 1
- else
- signalBlocks["TEST_10"].status = 0
- end
- end
- updateDisplay()
- end
- local function starts_with(str, start)
- return str:sub(1, #start) == start
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement