Advertisement
Paul19988

Untitled

Jan 20th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.    if signalBlocks["TEST_1"].status == 1 then
  145.     paintutils.drawLine(2,9, 10, 9, colors.black)
  146.    else
  147.     paintutils.drawLine(2,9, 10, 9, colors.orange)
  148.    end
  149.  
  150.    if signalBlocks["TEST_2"].status == 1 then
  151.     paintutils.drawLine(11,9, 18, 9, colors.black)
  152.    else
  153.     paintutils.drawLine(11,9, 18, 9, colors.magenta)
  154.    end
  155.  
  156.    if signalBlocks["TEST_3"].status == 1 then
  157.     paintutils.drawLine(19,9, 26, 9, colors.black)
  158.    else
  159.     paintutils.drawLine(19,9, 26, 9, colors.purple)
  160.    end
  161.  
  162.    if signalBlocks["TEST_4"].status == 1 then
  163.     paintutils.drawLine(27,9, 34, 9, colors.black)
  164.    else
  165.     paintutils.drawLine(27,9, 34, 9, colors.yellow)
  166.    end
  167.  
  168.    if signalBlocks["TEST_5"].status == 1 then
  169.     paintutils.drawLine(35,9, 37, 9, colors.black)
  170.    else
  171.     paintutils.drawLine(35,9, 37, 9, colors.green)
  172.    end
  173.  
  174.    if signalBlocks["TEST_6"].status == 1 then
  175.     paintutils.drawLine(38,9, 46, 9, colors.black)
  176.    else
  177.     paintutils.drawLine(38,9, 46, 9, colors.pink)
  178.    end
  179.  
  180.    if signalBlocks["TEST_7"].status == 1 then
  181.     paintutils.drawLine(46,9, 55, 9, colors.black)
  182.    else
  183.     paintutils.drawLine(46,9, 55, 9, colors.brown)
  184.    end
  185.    if signalBlocks["TEST_8"].status == 1 then
  186.     paintutils.drawLine(56,9, 58, 9, colors.black)
  187.    else
  188.     paintutils.drawLine(56,9, 58, 9, colors.red)
  189.    end
  190.    if signalBlocks["TEST_9"].status == 1 then
  191.     paintutils.drawLine(59,9, 67, 9, colors.black)
  192.    else
  193.     paintutils.drawLine(59,9, 67, 9, colors.blue)
  194.    end
  195.    if signalBlocks["TEST_10"].status == 1 then
  196.     paintutils.drawLine(59,9, 67, 9, colors.black)
  197.    else
  198.     paintutils.drawLine(59,9, 67, 9, colors.blue)
  199.    end
  200. end
  201.  
  202. local function compareCoords(coord1, coord2, thepoint)
  203.     if thepoint <= tonumber(coord1) and thepoint >= tonumber(coord2) then
  204.  
  205.         return true
  206.     else
  207.         return false
  208.     end
  209.  
  210. end
  211.  
  212. while true do
  213.     local event, side, id, uuid, message, system = os.pullEvent("radio_message")
  214.  
  215.     local theMessage = json.decode(message)
  216.  
  217.     if theMessage.funct == "attemptconnection" then
  218.         radio.sendMessage(id, json.encode({funct = "startlevel2", speedLimit = 20, nextSpeedLimit = 0, speedChangeSoon = false, endSoon = false, stationStopSoon = false, mtcStatus = 1}))
  219.        
  220.         radio.sendMessage(id, json.encode({funct = "message", themessage = "Train identified: "..id..",length ".. string.sub(theMessage.destination,8)..", connected to Virtual Signal Block system"}))
  221.  
  222.     end
  223.     if theMessage.funct == "update" then
  224.      radio.sendMessage(id, json.encode({funct = "response", speedLimit = 20, nextSpeedLimit = 0, speedChange = false, endSoon = false, stationStopSoon = false, atoStatus = 1, destintaion = ""}))
  225.      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
  226.             signalBlocks["TEST_1"].status = 1
  227.      else
  228.         signalBlocks["TEST_1"].status = 0
  229.         end
  230.         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
  231.             signalBlocks["TEST_2"].status = 1
  232.         else
  233.             signalBlocks["TEST_2"].status = 0
  234.         end
  235.         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
  236.             signalBlocks["TEST_3"].status = 1
  237.         else
  238.             signalBlocks["TEST_3"].status = 0
  239.         end
  240.         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
  241.             signalBlocks["TEST_4"].status = 1
  242.         else
  243.             signalBlocks["TEST_4"].status = 0
  244.         end
  245.         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
  246.             signalBlocks["TEST_5"].status = 1
  247.         else
  248.             signalBlocks["TEST_5"].status = 0
  249.         end
  250.           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
  251.             signalBlocks["TEST_6"].status = 1
  252.           else
  253.             signalBlocks["TEST_6"].status = 0
  254.         end
  255.         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
  256.             signalBlocks["TEST_7"].status = 1
  257.         else
  258.             signalBlocks["TEST_7"].status = 0
  259.         end
  260.         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
  261.             signalBlocks["TEST_8"].status = 1
  262.         else
  263.             signalBlocks["TEST_8"].status = 0
  264.         end
  265.         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
  266.             signalBlocks["TEST_9"].status = 1
  267.         else
  268.             signalBlocks["TEST_9"].status = 0
  269.         end
  270.         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
  271.             signalBlocks["TEST_10"].status = 1
  272.         else
  273.             signalBlocks["TEST_10"].status = 0
  274.         end
  275.     end
  276.     updateDisplay()
  277.  
  278. end
  279.  
  280. local function starts_with(str, start)
  281.    return str:sub(1, #start) == start
  282. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement