Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Screen to choose level
- --CONST
- MODEM_SIDE = "top"
- MONITOR_SIDE = "right"
- CHANNEL = 1 --Channel to send levels to
- --Colors to use (see colors API)
- COLOR_1 = colors.lightGray
- COLOR_2 = colors.gray
- --Number off lines available on the screen
- MAX_LEVEL = 10 --10 is the max for a 1*2 screen
- MAX_LEVEL_PER_COLUMN = 5 --5 is the number off lines on one monitor
- COLUMN_SIZE = 9 --Size in char off one column on screen
- --
- --API
- --Loading home-made touch screen API
- os.loadAPI("screenAPI")
- --Getting a fonction from the API
- point = screenAPI.p
- --
- --File management
- --Sends back registered level table
- function getTable()
- local t = {}
- --If the file exists, we read the table from it, otherwise it'll stay empty
- if fs.exists("data/levels") then
- local f = fs.open("data/levels", 'r')
- t = textutils.unserialize(f.readLine())
- f.close()
- end
- return t
- end--getTable()
- --Writes the list in the file
- function writeTable(t)
- fs.makeDir("data")
- table.sort(t, function(a,b) return a.num>b.num end) --Sorts the table using user defined number (not actual level numbers)
- local f = fs.open("data/levels", 'w')
- f.writeLine(textutils.serialize(t))
- f.close()
- end --writeTable()
- --Adds level with number, label and corresponding level to the list
- -- Choose short labels if you have more than 5 levels, 8 char max for it to display correctly on screen
- function addLevel(num, label, level)
- local t = getTable()
- --We have too many registered levels to display them on the screen
- if #t >= MAX_LEVEL then print("too many levels !") return 1 end --The return is not used
- --Inserts a new table is the table and writes it
- table.insert(t, {["num"]=tonumber(num),
- ["label"]=label,
- ["level"]=tonumber(level)})
- writeTable(t)
- end--addLevel()
- --Deletes level from the list (uses actual level, the distance from level 0)
- --If lvl is used multiple times in the list, they will all be removed
- function delLevel(lvl)
- local t = getTable()
- --Reads the list
- for k,v in ipairs(t) do
- --Look for corresponding table
- if v.level == tonumber(lvl) then
- table.remove(t, k) --Deletes table whos level is lvl
- end
- end
- --Save changes
- writeTable(t)
- end--delLevel()
- --Return level (distance from level 0) off level number n
- function getLevel(n)
- --Reads table
- for k, v in ipairs(getTable()) do
- --Finds level
- if v.num == tonumber(n) then
- return v.level --Return level
- end
- end
- --If we can't find it, that's what will be displayed
- return "Can't find floor"
- end--getLevel()
- --Clears the table and save it
- function clearTable()
- writeTable({})
- end--clearTable()
- --
- --Display
- --Clears the computer's screen
- function clear()
- term.clear()
- term.setCursorPos(1,1)
- end--clear()
- --Clear's the monitor's screen given
- function clearMon(m)
- m.clear()
- m.setCursorPos(1,1)
- end--clearMon()
- --print(), designed for the screen. Requires monitor's handle
- function printMon(s, mon)
- local w, h = mon.getSize()
- local x, y = mon.getCursorPos()
- --Writes the string
- mon.write(s)
- --If we have enough space, we go to next line
- if y+1 <= h then
- mon.setCursorPos(1, y+1)
- else --Otherwise, we go back at pos (1;1)
- mon.setCursorPos(1, 1)
- end
- end--printMon()
- --[[
- Displays levels on the screen
- num-label
- ]]
- function draw()
- local t = getTable()
- -- t was sorted in writeTable()
- --Wrapping monitor, settings to default
- m = peripheral.wrap(MONITOR_SIDE)
- m.setBackgroundColor(colors.black)
- clearMon(m)
- --Clears the registered regions
- screenAPI.clear()
- --Reads the table
- for k, v in ipairs(t) do
- if k/2 == math.floor(k/2) then --Setting background color, one color per line
- m.setBackgroundColor(COLOR_1)
- else --Impair, gris
- m.setBackgroundColor(COLOR_2)
- end
- local x, y = m.getCursorPos()
- if k > MAX_LEVEL_PER_COLUMN then --If we are at the end off the screen, we start the second column
- m.setCursorPos(COLUMN_SIZE+1, y)
- end
- --We add the level to regions recognized by the screenAPI (see the file for more information)
- --The point() function makes it human readable
- x, y = m.getCursorPos()
- screenAPI.addReg(v.num, point(x, y), point(x+COLUMN_SIZE-1, y))
- --Printing, don't forget the monitor's handle 'm'
- printMon(tostring(v.num)..'-'..v.label, m)
- end--For
- end--draw()
- --
- --Splits the string per word
- function stringSplit(string)
- local t = {}
- for word in string.gmatch(string, "[^%s]+") do --Finds everything separated by a space, hard to explain, see lua documentation
- table.insert(t, word)
- end
- return t
- end--stringSplit()
- --MAIN
- --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
- main = function()
- local event = {}
- local modem = peripheral.wrap(MODEM_SIDE)
- --Clear the computer screen
- clear()
- print("Running")
- --We draw the screen
- draw()
- while true do
- event = {os.pullEvent()}
- if event[1] == "key" then
- if event[2] == keys.numPadAdd then --On veut ajouter un etage
- print("Add a floor:\n <number> \"label\" <level>")
- local params = stringSplit(read())
- if #params < 3 then
- print("Error")
- else
- addLevel(params[1], params[2], params[3])
- end--If
- elseif event[2] == keys.numPadSubtract then --On veut enlever un etage
- print("Remove a floor:\n <level>")
- local params = stringSplit(read())
- delLevel(params[1])
- elseif event[2] == keys.multiply then --On veut recuperer le niveau d'un etage
- print("Get level of floor:\n <number>")
- local params = stringSplit(read())
- print(getLevel(params[1]))
- end--If
- draw()
- elseif event[1] == "regions" then --Ecran, on envoi a l'ascensseur le niveau correspondant
- modem.transmit(CHANNEL, CHANNEL, tonumber(getLevel(event[2])))
- end--If
- end--While
- end--main()
- --We run main and the screenApi in parallel. They both run (almost) at the same time. See the API's file for more info
- parallel.waitForAny(main, screenAPI.run)
Advertisement
Add Comment
Please, Sign In to add comment