PeachGaming

Virtual Signal Block System

Jan 19th, 2020
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.66 KB | None | 0 0
  1. --Virtual Signal Block System
  2. -- Concept
  3. --Copyright 2020 PeachMaster
  4. --Just kidding no copywrite
  5.  
  6. local radio = peripheral.wrap("top")
  7. local signalBlocks = {TEST_1 = {
  8.         startXPos = -341,
  9.         startYPos = 63,
  10.         startZPos = -51,
  11.         endXPos = -352,
  12.         endYPos = 63,
  13.         endZPos = -51,
  14.         speedLimit = 20,
  15.         nextSpeedLimitLocation = "",
  16.         status = 0,
  17.     },
  18.     TEST_2 = {
  19.         startXPos = -353,
  20.         startYPos = 63,
  21.         startZPos = -51,
  22.         endXPos = -364,
  23.         endYPos = 63,
  24.         endZPos = -51,
  25.         speedLimit = 20,
  26.         nextSpeedLimitLocation = "",
  27.         status = 0,
  28.     },
  29.     TEST_3 = {
  30.         startXPos = -365,
  31.         startYPos = 63,
  32.         startZPos = -51,
  33.         endXPos = -376,
  34.         endYPos = 63,
  35.         endZPos = -51,
  36.         speedLimit = 20,
  37.         nextSpeedLimitLocation = "",
  38.         status = 0,
  39.     },
  40.     TEST_4 = {
  41.         startXPos = -377,
  42.         startYPos = 63,
  43.         startZPos = -51,
  44.         endXPos = -388,
  45.         endYPos = 63,
  46.         endZPos = -51,
  47.         speedLimit = 20,
  48.         nextSpeedLimitLocation = "",
  49.         status = 0,
  50.     },
  51.     TEST_5 = {
  52.         startXPos = -389,
  53.         startYPos = 63,
  54.         startZPos = -51,
  55.         endXPos = -394,
  56.         endYPos = 63,
  57.         endZPos = -51,
  58.         speedLimit = 20,
  59.         nextSpeedLimitLocation = "",
  60.         status = 0,
  61.     },
  62.     TEST_6 = {
  63.         startXPos = -399,
  64.         startYPos = 63,
  65.         startZPos = -55,
  66.         endXPos = -400,
  67.         endYPos = 63,
  68.         endZPos = -66,
  69.         speedLimit = 20,
  70.         nextSpeedLimitLocation = "",
  71.         status = 0,
  72.     },
  73.     TEST_7 = {
  74.         startXPos = -400,
  75.         startYPos = 63,
  76.         startZPos = -67,
  77.         endXPos = -400,
  78.         endYPos = 63,
  79.         endZPos = -78,
  80.         speedLimit = 20,
  81.         nextSpeedLimitLocation = "",
  82.         status = 0,
  83.     },
  84.     TEST_8 = {
  85.         startXPos = -400,
  86.         startYPos = 63,
  87.         startZPos = -79,
  88.         endXPos = -400,
  89.         endYPos = 63,
  90.         endZPos = -84,
  91.         speedLimit = 20,
  92.         nextSpeedLimitLocation = "",
  93.         status = 0,
  94.     },
  95.     TEST_9 = {
  96.         startXPos = -399,
  97.         startYPos = 63,
  98.         startZPos = -85,
  99.         endXPos = -400,
  100.         endYPos = 63,
  101.         endZPos = -91,
  102.         speedLimit = 20,
  103.         nextSpeedLimitLocation = "",
  104.         status = 0,
  105.     },
  106.     TEST_10 = {
  107.         startXPos = -399,
  108.         startYPos = 63,
  109.         startZPos = -92,
  110.         endXPos = -400,
  111.         endYPos = 63,
  112.         endZPos = -108,
  113.         speedLimit = 20,
  114.         nextSpeedLimitLocation = "",
  115.         status = 0,
  116.     },
  117. }
  118.  
  119. local connectedTrains = {
  120.  
  121. }
  122.  
  123. os.loadAPI("json")
  124. radio.activate()
  125. term.redirect(peripheral.find("monitor"))
  126.  
  127.  
  128. local function updateDisplay()
  129.     term.setBackgroundColor(colors.white)
  130.     term.setTextColor(colors.black)
  131.    term.clear()
  132.     term.setCursorPos(1,1)
  133.     print("Virtual Signal Block Demonstrative Example")
  134.     print("By PeachMaster")
  135.     paintutils.drawLine(2,9, 10, 9, colors.orange)
  136.    paintutils.drawLine(11,9, 18, 9, colors.magenta)
  137.    paintutils.drawLine(19,9, 26, 9, colors.purple)
  138.    paintutils.drawLine(27,9, 34, 9, colors.yellow)
  139.    paintutils.drawLine(35,9, 37, 9, colors.green)
  140.    paintutils.drawLine(38,9, 46, 9, colors.pink)
  141.    paintutils.drawLine(46,9, 55, 9, colors.brown)
  142.    paintutils.drawLine(56,9, 58, 9, colors.red)
  143.    paintutils.drawLine(59,9, 67, 9, colors.blue)
  144.    term.setTextColor(colors.black)
  145.    term.setTextColor(colors.black)
  146.    term.setCursorPos(1,13)
  147.    if signalBlocks["TEST_1"].status == 1 then
  148.     print("TEST_1 (Orange): Occupied")
  149.    else
  150.     print("TEST_1 (Orange): Open")
  151.    end
  152.  
  153.    if signalBlocks["TEST_2"].status == 1 then
  154.     print("TEST_2 (Magenta): Occupied")
  155.    else
  156.     print("TEST_2 (Magenta): Open")
  157.    end
  158.  
  159.    if signalBlocks["TEST_3"].status == 1 then
  160.     print("TEST_3 (Purple): Occupied")
  161.    else
  162.     print("TEST_3 (Purple): Open")
  163.    end
  164.  
  165.    if signalBlocks["TEST_4"].status == 1 then
  166.     print("TEST_4 (Yellow): Occupied")
  167.    else
  168.     print("TEST_4 (Yellow): Open")
  169.    end
  170.  
  171.    if signalBlocks["TEST_5"].status == 1 then
  172.     print("TEST_5 (Green): Occupied")
  173.    else
  174.     print("TEST_5 (Green): Open")
  175.    end
  176.  
  177.    if signalBlocks["TEST_6"].status == 1 then
  178.     print("TEST_6 (Pink): Occupied")
  179.    else
  180.     print("TEST_6 (Pink): Open")
  181.    end
  182.  
  183.    if signalBlocks["TEST_7"].status == 1 then
  184.     print("TEST_7 (Brown): Occupied")
  185.    else
  186.     print("TEST_7 (Brown): Open")
  187.    end
  188.    if signalBlocks["TEST_8"].status == 1 then
  189.     print("TEST_8 (Red): Occupied")
  190.    else
  191.     print("TEST_8 (Red): Open")
  192.    end
  193.    if signalBlocks["TEST_9"].status == 1 then
  194.     print("TEST_9 (Blue): Occupied")
  195.    else
  196.     print("TEST_9 (Blue): Open")
  197.    end
  198.    if signalBlocks["TEST_10"].status == 1 then
  199.     print("TEST_10 (Blue): Occupied")
  200.    else
  201.     print("TEST_10 (Blue): Open")
  202.    end
  203. end
  204.  
  205. local function compareCoords(coord1, coord2, thepoint)
  206.     if thepoint <= tonumber(coord1) and thepoint >= tonumber(coord2) then
  207.  
  208.         return true
  209.     else
  210.         return false
  211.     end
  212.  
  213. end
  214.  
  215. while true do
  216.     local event, side, id, uuid, message, system = os.pullEvent("radio_message")
  217.  
  218.     local theMessage = json.decode(message)
  219.  
  220.     if theMessage.funct == "attemptconnection" then
  221.         radio.sendMessage(id, json.encode({funct = "startlevel2", speedLimit = 20, nextSpeedLimit = 0, speedChangeSoon = false, endSoon = false, stationStopSoon = false, mtcStatus = 1}))
  222.        
  223.         radio.sendMessage(id, json.encode({funct = "message", themessage = "Train identified: "..id..",length ".. string.sub(theMessage.destination,8)..", connected to Virtual Signal Block system"}))
  224.  
  225.     end
  226.     if theMessage.funct == "update" then
  227.      radio.sendMessage(id, json.encode({funct = "response", speedLimit = 20, nextSpeedLimit = 0, speedChange = false, endSoon = false, stationStopSoon = false, atoStatus = 1, destintaion = ""}))
  228.      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
  229.             signalBlocks["TEST_1"].status = 1
  230.      else
  231.         signalBlocks["TEST_1"].status = 0
  232.         end
  233.         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
  234.             signalBlocks["TEST_2"].status = 1
  235.         else
  236.             signalBlocks["TEST_2"].status = 0
  237.         end
  238.         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
  239.             signalBlocks["TEST_3"].status = 1
  240.         else
  241.             signalBlocks["TEST_3"].status = 0
  242.         end
  243.         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
  244.             signalBlocks["TEST_4"].status = 1
  245.         else
  246.             signalBlocks["TEST_4"].status = 0
  247.         end
  248.         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
  249.             signalBlocks["TEST_5"].status = 1
  250.         else
  251.             signalBlocks["TEST_5"].status = 0
  252.         end
  253.           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
  254.             signalBlocks["TEST_6"].status = 1
  255.           else
  256.             signalBlocks["TEST_6"].status = 0
  257.         end
  258.         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
  259.             signalBlocks["TEST_7"].status = 1
  260.         else
  261.             signalBlocks["TEST_7"].status = 0
  262.         end
  263.         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
  264.             signalBlocks["TEST_8"].status = 1
  265.         else
  266.             signalBlocks["TEST_8"].status = 0
  267.         end
  268.         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
  269.             signalBlocks["TEST_9"].status = 1
  270.         else
  271.             signalBlocks["TEST_9"].status = 0
  272.         end
  273.         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
  274.             signalBlocks["TEST_10"].status = 1
  275.         else
  276.             signalBlocks["TEST_10"].status = 0
  277.         end
  278.     end
  279.     updateDisplay()
  280.  
  281. end
  282.  
  283. local function starts_with(str, start)
  284.    return str:sub(1, #start) == start
  285. end
Advertisement
Add Comment
Please, Sign In to add comment