Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- searches for a given peripheral type on the computer sides and returns the side if found
- local function findPeripheral( kind )
- for _,v in pairs( rs.getSides() ) do
- if peripheral.getType( v ) == kind then
- return v
- end
- end
- return nil
- end
- local monitor -- empty variable waiting for peripheral.wrap
- local monitorSide = findPeripheral( "monitor" ) -- find the monitor
- local w, h = term.getSize() -- get the pixel size of the screen ( currently the computers screen )
- if not monitorSide then
- print( "No monitor found! :(")
- error() -- exit the program here
- else
- monitor = peripheral.wrap(monitorSide)
- end
- local function cwrite( msg, y )
- local w, h = term.getSize() -- get the pixel size of the current screen
- 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
- write( msg ) -- draw the message
- end
- -- the characters that draw over the top of the standard map
- local chars = {
- station = "*", -- train at station character
- onTrack = "+", -- train inbetween station character
- }
- -- 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
- local function drawChar( char, x, y, col )
- term.setCursorPos( x, y )
- term.setTextColor( col )
- write( char )
- end
- local function drawScreen()
- term.clear() -- clear the screen
- term.setTextColor( colors.white ) -- just incase we have any stray colors around
- monitor.setTextScale(3) -- make the text size bigger
- local w, h = term.getSize() -- gets the width and height in cc pixels of the screen, currently the screen is the monitor
- -- first we draw the default path on the screen
- for i = 1, h do -- for each number thats within the screen height
- if i == 1 or i == h then -- if the number is at the top or the bottom of the screen
- -- call the center write with the following parameters
- -- 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
- -- 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
- -- 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
- -- current number in the loop
- cwrite( ( i == 1 and "." or "'" )..string.rep( "-", w-2 )..( i == 1 and "." or "'" ), i )
- else -- else if its anywhere else
- -- call the center write with the following parameters
- -- 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
- -- this line is then drawn at the i position where i is the current number in the loop
- cwrite( "|"..string.rep( " ", w - 2 ).."|", i )
- end
- end
- -- write in the center of the top line the station plaeholders ( for no trains at station )
- cwrite( "@@@@", 1 )
- cwrite( "@@@@", h )
- -- now we draw on the train progress
- local rsInput = rs.getBundledInput("left") -- get the left redstone input
- if colors.test( rsInput, colors.red ) then -- if the red wire is turned on
- -- 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)
- drawChar( chars.station, w/2-1, 1, colors.red )
- end
- if colors.test( rsInput, colors.blue ) then -- if the blue wire is turned on
- -- 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)
- drawChar( chars.station, w/2, 1, colors.blue )
- end
- if colors.test( rsInput, colors.lime ) then -- if the lime wire is turned on
- -- 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)
- drawChar( chars.station, w/2+1, 1, colors.lime )
- end
- if colors.test( rsInput, colors.orange ) then -- if the orange wire is turned on
- -- 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)
- drawChar( chars.station, w/2+2, 1, colors.orange )
- end
- if colors.test( rsInput, colors.black ) or colors.test( rsInput, colors.brown ) then -- if the black or brown wire is turned on
- -- 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 )
- drawChar( string.rep( chars.onTrack, w/2-2), (w/2)+3, 1, colors.white )
- end
- if colors.test( rsInput, colors.green ) or colors.test( rsInput, colors.white ) then -- if the green or white wire is turned on
- -- draw the track character to the repetition width of half the screen width - 3 and at the x position of 2 ( stops station overwriting )
- drawChar( string.rep( chars.onTrack, w/2-3), 2, 1, colors.white )
- end
- if colors.test( rsInput, colors.pink ) or colors.test( rsInput, colors.yellow ) then -- if the pink or yellow wire is turned on
- term.setTextColor( colors.white )
- -- fot the heights between y 2 and the screens height - 1 ( to allow for corner chars )
- for i = 2, h - 1 do
- term.setCursorPos( w, i ) -- set the cursor position to the screens width and the current y position
- write( chars.onTrack ) -- write the between stations character
- end
- end
- -- all the code below is exactly the same as above except on the opposide sides of the screen,
- local rsInput = rs.getBundledInput("right")
- if colors.test( rsInput, colors.red ) then
- drawChar( chars.station, w/2-1, h, colors.red )
- end
- if colors.test( rsInput, colors.blue ) then
- drawChar( chars.station, w/2, h, colors.blue )
- end
- if colors.test( rsInput, colors.lime ) then
- drawChar( chars.station, w/2+1, h, colors.lime )
- end
- if colors.test( rsInput, colors.orange ) then
- drawChar( chars.station, w/2+2, h, colors.orange )
- end
- if colors.test( rsInput, colors.black ) or colors.test( rsInput, colors.brown ) then
- drawChar( string.rep( chars.onTrack, w/2-2), (w/2)+3, h, colors.white )
- end
- if colors.test( rsInput, colors.green ) or colors.test( rsInput, colors.white ) then
- drawChar( string.rep( chars.onTrack, w/2-3), 2, h, colors.white )
- end
- if colors.test( rsInput, colors.pink ) or colors.test( rsInput, colors.yellow ) then
- term.setTextColor( colors.white )
- for i = 2, h - 1 do
- term.setCursorPos( 1, i )
- write( chars.onTrack )
- end
- end
- term.setTextColor( colors.red )
- 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
- end
- term.clear() -- clear the terminal screen
- cwrite( "Monitor has focus" ) -- print in the middle of the screen that the monitor has control
- term.redirect( monitor ) -- give control of term functions to the monitor object meaning term. print and write will all show on the monitor
- 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 )
- while true do
- drawScreen() -- call the function that will draw the screen
- os.pullEvent( "timer" ) -- the program will wait here until the timer finishes
- refresh = os.startTimer( 1 ) -- restart the refresh timer
- end
- term.restore() -- give term control back to the computer screen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement