Advertisement
redeye83

RedBIT Train Network Map

Jan 25th, 2013
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.60 KB | None | 0 0
  1. -- searches for a given peripheral type on the computer sides and returns the side if found
  2. local function findPeripheral( kind )
  3.     for _,v in pairs( rs.getSides() ) do
  4.         if peripheral.getType( v ) == kind then
  5.             return v
  6.         end
  7.     end
  8.  
  9.     return nil
  10. end
  11.  
  12. local monitor  -- empty variable waiting for peripheral.wrap
  13. local monitorSide = findPeripheral( "monitor" ) -- find the monitor
  14. local w, h = term.getSize() -- get the pixel size of the screen ( currently the computers screen )
  15.  
  16. if not monitorSide then
  17.     print( "No monitor found! :(")
  18.     error() -- exit the program here
  19. else
  20.     monitor = peripheral.wrap(monitorSide)
  21. end
  22.  
  23. local function cwrite( msg, y )
  24.     local w, h = term.getSize() -- get the pixel size of the current screen
  25.     term.setCursorPos( w/2-#msg/2+1, y or h/2 ) -- set the cursor position so the supplied text will be in the middle of the screen
  26.     write( msg ) -- draw the message
  27. end
  28.  
  29. -- the characters that draw over the top of the standard map
  30. local chars = {
  31.     station = "*", -- train at station character
  32.     onTrack = "+", -- train inbetween station character
  33. }
  34.  
  35. -- The following function is designed to draw the supplied character(s) at the supplied x and y position on the screen, setting the color appropriately
  36. local function drawChar( char, x, y, col )
  37.     term.setCursorPos( x, y )
  38.     term.setTextColor( col )
  39.     write( char )
  40. end
  41.  
  42. local function drawScreen()
  43.     term.clear() -- clear the screen
  44.     term.setTextColor( colors.white ) -- just incase we have any stray colors around
  45.     monitor.setTextScale(3) -- make the text size bigger
  46.  
  47.     local w, h = term.getSize() -- gets the width and height in cc pixels of the screen, currently the screen is the monitor
  48.  
  49.     -- first we draw the default path on the screen
  50.  
  51.     for i = 1, h do -- for each number thats within the screen height
  52.         if i == 1 or i == h then -- if the number is at the top or the bottom of the screen
  53.  
  54.             -- call the center write with the following parameters
  55.             -- the string to draw is calculated based on the screen size, it will either have . or ' in the corners based on if it is at the top or the bottom with
  56.             -- the use of Lua's equvalent of the ternary operator ( <condition> and <if true> or <if false> ), then using string rep it creates a repetition of - for
  57.             -- the screens width minus 2 ( which are the corners ) then the last corner character is added. This line is then drawn at the i position where i is the
  58.             -- current number in the loop
  59.             cwrite( ( i == 1 and "." or "'" )..string.rep( "-", w-2 )..( i == 1 and "." or "'" ), i )
  60.  
  61.         else -- else if its anywhere else
  62.            
  63.             -- call the center write with the following parameters
  64.             -- the | to indicate the left and right tracks, with a string repetition of a space in the middle for the length of the screen width minus 2
  65.             -- this line is then drawn at the i position where i is the current number in the loop
  66.             cwrite( "|"..string.rep( " ", w - 2 ).."|", i )
  67.         end
  68.     end
  69.  
  70.     -- write in the center of the top line the station plaeholders ( for no trains at station )
  71.     cwrite( "@@@@", 1 )
  72.     cwrite( "@@@@", h )
  73.  
  74.  
  75.     -- now we draw on the train progress
  76.  
  77.     local rsInput = rs.getBundledInput("left") -- get the left redstone input
  78.  
  79.     if colors.test( rsInput, colors.red ) then -- if the red wire is turned on
  80.  
  81.         -- draw the station character at the x and y position and the colour where x position is the middle of the screen -1 (where the station placeholder for this one is)
  82.         drawChar( chars.station, w/2-1, 1, colors.red )
  83.     end
  84.  
  85.     if colors.test( rsInput, colors.blue ) then -- if the blue wire is turned on
  86.         -- draw the station character at the x and y position and the colour where x position is the middle of the screen (where the station placeholder for this one is)
  87.         drawChar( chars.station, w/2, 1, colors.blue )
  88.     end
  89.  
  90.     if colors.test( rsInput, colors.lime ) then -- if the lime wire is turned on
  91.         -- draw the station character at the x and y position and the colour where x position is the middle of the screen +1 (where the station placeholder for this one is)
  92.         drawChar( chars.station, w/2+1, 1, colors.lime )
  93.     end
  94.  
  95.     if colors.test( rsInput, colors.orange ) then -- if the orange wire is turned on
  96.         -- draw the station character at the x and y position and the colour where x position is the middle of the screen +2 (where the station placeholder for this one is)
  97.         drawChar( chars.station, w/2+2, 1, colors.orange )
  98.     end
  99.    
  100.     if colors.test( rsInput, colors.black ) or colors.test( rsInput, colors.brown ) then -- if the black or brown wire is turned on
  101.         -- draw the track character to the repetition width of half the screen width - 2 and at the x position of the screen width divided by 2 and added 3 ( stops station overwriting )
  102.         drawChar( string.rep( chars.onTrack, w/2-2), (w/2)+3, 1, colors.white )
  103.     end
  104.  
  105.     if colors.test( rsInput, colors.green ) or colors.test( rsInput, colors.white ) then -- if the green or white wire is turned on
  106.         -- draw the track character to the repetition width of half the screen width - 3 and at the x position of 2 ( stops station overwriting )
  107.         drawChar( string.rep( chars.onTrack, w/2-3), 2, 1, colors.white )
  108.     end
  109.  
  110.     if colors.test( rsInput, colors.pink ) or colors.test( rsInput, colors.yellow ) then -- if the pink or yellow wire is turned on
  111.         term.setTextColor( colors.white )
  112.         -- fot the heights between y 2 and the screens height - 1 ( to allow for corner chars )
  113.         for i = 2, h - 1 do
  114.             term.setCursorPos( w, i ) -- set the cursor position to the screens width and the current y position
  115.             write( chars.onTrack ) -- write the between stations character
  116.         end
  117.     end
  118.  
  119.     -- all the code below is exactly the same as above except on the opposide sides of the screen,
  120.     local rsInput = rs.getBundledInput("right")
  121.  
  122.     if colors.test( rsInput, colors.red ) then
  123.         drawChar( chars.station, w/2-1, h, colors.red )
  124.     end
  125.  
  126.     if colors.test( rsInput, colors.blue ) then
  127.         drawChar( chars.station, w/2, h, colors.blue )
  128.     end
  129.  
  130.     if colors.test( rsInput, colors.lime ) then
  131.         drawChar( chars.station, w/2+1, h, colors.lime )
  132.     end
  133.  
  134.     if colors.test( rsInput, colors.orange ) then
  135.         drawChar( chars.station, w/2+2, h, colors.orange )
  136.     end
  137.    
  138.     if colors.test( rsInput, colors.black ) or colors.test( rsInput, colors.brown ) then
  139.         drawChar( string.rep( chars.onTrack, w/2-2), (w/2)+3, h, colors.white )
  140.     end
  141.  
  142.     if colors.test( rsInput, colors.green ) or colors.test( rsInput, colors.white ) then
  143.         drawChar( string.rep( chars.onTrack, w/2-3), 2, h, colors.white )
  144.     end
  145.  
  146.     if colors.test( rsInput, colors.pink ) or colors.test( rsInput, colors.yellow ) then
  147.         term.setTextColor( colors.white )
  148.         for i = 2, h - 1 do
  149.             term.setCursorPos( 1, i )
  150.             write( chars.onTrack )
  151.         end
  152.     end
  153.  
  154.  
  155.     term.setTextColor( colors.red )
  156.     cwrite("RedBIT Train Networks", h/2+1) -- call the center write function, with a message to display and the y position to display it, in this case the middle of the screen using h from before
  157. end
  158.  
  159. term.clear() -- clear the terminal screen
  160. cwrite( "Monitor has focus" ) -- print in the middle of the screen that the monitor has control
  161.  
  162. term.redirect( monitor ) -- give control of term functions to the monitor object meaning term. print and write will all show on the monitor
  163.  
  164. local refresh = os.startTimer( 1 ) -- this starts a timer that takes 1 second to complete, used for refreshing ( its better than sleep but requires event loops )
  165.  
  166. while true do
  167.     drawScreen() -- call the function that will draw the screen
  168.  
  169.     os.pullEvent( "timer" ) -- the program will wait here until the timer finishes
  170.    
  171.     refresh = os.startTimer( 1 ) -- restart the refresh timer
  172. end
  173.  
  174. term.restore() -- give term control back to the computer screen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement