Niverton

Lift-Screen

Sep 12th, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.09 KB | None | 0 0
  1. --Screen to choose level
  2.  
  3. --CONST
  4.     MODEM_SIDE = "top"
  5.     MONITOR_SIDE = "right"
  6.     CHANNEL = 1 --Channel to send levels to
  7.    
  8.     --Colors to use (see colors API)
  9.     COLOR_1 = colors.lightGray
  10.     COLOR_2 = colors.gray
  11.    
  12.     --Number off lines available on the screen
  13.     MAX_LEVEL = 10 --10 is the max for a 1*2 screen
  14.     MAX_LEVEL_PER_COLUMN = 5 --5 is the number off lines on one monitor
  15.     COLUMN_SIZE = 9 --Size in char off one column on screen
  16. --
  17.  
  18. --API
  19. --Loading home-made touch screen API
  20. os.loadAPI("screenAPI")
  21. --Getting a fonction from the API
  22. point = screenAPI.p
  23. --
  24.  
  25. --File management
  26.     --Sends back registered level table
  27.     function getTable()
  28.         local t = {}
  29.         --If the file exists, we read the table from it, otherwise it'll stay empty
  30.         if fs.exists("data/levels") then
  31.             local f = fs.open("data/levels", 'r')
  32.             t = textutils.unserialize(f.readLine())
  33.             f.close()
  34.         end
  35.         return t
  36.     end--getTable()
  37.    
  38.     --Writes the list in the file
  39.     function writeTable(t)
  40.         fs.makeDir("data")
  41.         table.sort(t, function(a,b) return a.num>b.num end) --Sorts the table using user defined number (not actual level numbers)
  42.         local f = fs.open("data/levels", 'w')
  43.         f.writeLine(textutils.serialize(t))
  44.         f.close()
  45.     end --writeTable()
  46.  
  47.     --Adds level with number, label and corresponding level to the list
  48.         -- Choose short labels if you have more than 5 levels, 8 char max for it to display correctly on screen
  49.     function addLevel(num, label, level)
  50.         local t = getTable()
  51.         --We have too many registered levels to display them on the screen
  52.         if #t >= MAX_LEVEL then print("too many levels !") return 1 end --The return is not used
  53.    
  54.         --Inserts a new table is the table and writes it
  55.         table.insert(t, {["num"]=tonumber(num),
  56.             ["label"]=label,
  57.             ["level"]=tonumber(level)})
  58.    
  59.         writeTable(t)
  60.     end--addLevel()
  61.  
  62.     --Deletes level from the list (uses actual level, the distance from level 0)
  63.         --If lvl is used multiple times in the list, they will all be removed
  64.     function delLevel(lvl)
  65.         local t = getTable()
  66.         --Reads the list
  67.         for k,v in ipairs(t) do
  68.             --Look for corresponding table
  69.             if v.level == tonumber(lvl) then
  70.                 table.remove(t, k) --Deletes table whos level is lvl
  71.             end
  72.         end
  73.         --Save changes
  74.         writeTable(t)
  75.     end--delLevel()
  76.    
  77.     --Return level (distance from level 0) off level number n
  78.     function getLevel(n)
  79.         --Reads table
  80.         for k, v in ipairs(getTable()) do
  81.             --Finds level
  82.             if v.num == tonumber(n) then
  83.                 return v.level --Return level
  84.             end
  85.         end
  86.         --If we can't find it, that's what will be displayed
  87.         return "Can't find floor"
  88.     end--getLevel()
  89.  
  90.     --Clears the table and save it
  91.     function clearTable()
  92.         writeTable({})
  93.     end--clearTable()
  94. --
  95.  
  96. --Display
  97.     --Clears the computer's screen
  98.     function clear()
  99.         term.clear()
  100.         term.setCursorPos(1,1)
  101.     end--clear()
  102.    
  103.     --Clear's the monitor's screen given
  104.     function clearMon(m)
  105.         m.clear()
  106.         m.setCursorPos(1,1)
  107.     end--clearMon()
  108.    
  109.     --print(), designed for the screen. Requires monitor's handle
  110.     function printMon(s, mon)
  111.    
  112.         local w, h = mon.getSize()     
  113.         local x, y = mon.getCursorPos()
  114.        
  115.         --Writes the string
  116.         mon.write(s)
  117.        
  118.         --If we have enough space, we go to next line
  119.         if y+1 <= h then
  120.             mon.setCursorPos(1, y+1)
  121.         else --Otherwise, we go back at pos (1;1)
  122.             mon.setCursorPos(1, 1)
  123.         end
  124.     end--printMon()
  125.    
  126.     --[[
  127.         Displays levels on the screen
  128.         num-label
  129.     ]]
  130.     function draw()
  131.         local t = getTable()
  132.         -- t was sorted in writeTable()
  133.        
  134.         --Wrapping monitor, settings to default
  135.         m = peripheral.wrap(MONITOR_SIDE)
  136.         m.setBackgroundColor(colors.black)
  137.         clearMon(m)
  138.         --Clears the registered regions
  139.         screenAPI.clear()
  140.    
  141.         --Reads the table
  142.         for k, v in ipairs(t) do
  143.             if k/2 == math.floor(k/2) then --Setting background color, one color per line
  144.                 m.setBackgroundColor(COLOR_1)
  145.             else    --Impair, gris
  146.                 m.setBackgroundColor(COLOR_2)
  147.             end
  148.        
  149.             local x, y = m.getCursorPos()
  150.             if k > MAX_LEVEL_PER_COLUMN then --If we are at the end off the screen, we start the second column
  151.                 m.setCursorPos(COLUMN_SIZE+1, y)
  152.             end
  153.            
  154.             --We add the level to regions recognized by the screenAPI (see the file for more information)
  155.                 --The point() function makes it human readable
  156.             x, y = m.getCursorPos()
  157.             screenAPI.addReg(v.num, point(x, y), point(x+COLUMN_SIZE-1, y))
  158.             --Printing, don't forget the monitor's handle 'm'
  159.             printMon(tostring(v.num)..'-'..v.label, m)
  160.         end--For
  161.     end--draw()
  162. --
  163.  
  164. --Splits the string per word
  165. function stringSplit(string)
  166.     local t = {}
  167.     for word in string.gmatch(string, "[^%s]+") do --Finds everything separated by a space, hard to explain, see lua documentation
  168.         table.insert(t, word)
  169.     end
  170.     return t
  171. end--stringSplit()
  172.  
  173. --MAIN
  174. --This is actually a variable, like every function in lua. This syntax only makes it clearer that it's a variable, it doesn't change anything
  175. main = function()
  176.     local event = {}
  177.     local modem = peripheral.wrap(MODEM_SIDE)
  178.    
  179.     --Clear the computer screen
  180.     clear()
  181.     print("Running")
  182.    
  183.     --We draw the screen
  184.     draw()
  185.     while true do
  186.         event = {os.pullEvent()}
  187.         if event[1] == "key" then
  188.             if event[2] == keys.numPadAdd then  --On veut ajouter un etage
  189.                 print("Add a floor:\n <number> \"label\" <level>")
  190.                 local params = stringSplit(read())
  191.                 if #params < 3 then
  192.                     print("Error")
  193.                 else
  194.                     addLevel(params[1], params[2], params[3])
  195.                 end--If
  196.             elseif event[2] == keys.numPadSubtract then --On veut enlever un etage
  197.                 print("Remove a floor:\n <level>")
  198.                 local params = stringSplit(read())
  199.                 delLevel(params[1])
  200.             elseif event[2] == keys.multiply then --On veut recuperer le niveau d'un etage
  201.                 print("Get level of floor:\n <number>")
  202.                 local params = stringSplit(read())
  203.                 print(getLevel(params[1]))
  204.             end--If
  205.             draw()
  206.         elseif event[1] == "regions" then --Ecran, on envoi a l'ascensseur le niveau correspondant
  207.             modem.transmit(CHANNEL, CHANNEL, tonumber(getLevel(event[2])))
  208.         end--If
  209.     end--While
  210. end--main()
  211.  
  212. --We run main and the screenApi in parallel. They both run (almost) at the same time. See the API's file for more info
  213. parallel.waitForAny(main, screenAPI.run)
Advertisement
Add Comment
Please, Sign In to add comment