Posted 16 March 2013 - 03:02 PM
I have been trying to figure out how to get monitors for a lift system (using arcane levitators activated with redstone) to sync between floors. As shown by the (bad) code when a button is selected it turns green showing which floor has been selected. When another floor is selected the button turns back red and the selected turns green. I have been trying to figure out a way to get each monitor to show which floor has been selected and to stop outputting any signal it may have already been emitting. I am looking for a way to run similar code on each computer for each floor with only minor changes.
Any help with this or even suggestions on how to tidy up the code would be very much appreciated.
Here is my ugly code stitched together using other peoples code and some of my own.
P.S.This is the first thing I have pretty much ever coded, feel free to rip me a new one :D/>
Any help with this or even suggestions on how to tidy up the code would be very much appreciated.
Here is my ugly code stitched together using other peoples code and some of my own.
Spoiler
local side = "right"
local mon = peripheral.wrap("left")
mon.setTextScale(1)
mon.setTextColor(colors.white)
local button = {}
mon.setBackgroundColor(colors.black)
function setTable(name, func, xmin, xmax, ymin, ymax)
button[name] = {}
button[name]["func"] = func
button[name]["active"] = false
button[name]["xmin"] = xmin
button[name]["ymin"] = ymin
button[name]["xmax"] = xmax
button[name]["ymax"] = ymax
end
function resetAll()
rs.setBundledOutput(side, 0)
button["1"]["active"] = false
button["2"]["active"] = false
button["3"]["active"] = false
button["4"]["active"] = false
button["5"]["active"] = false
end
function set1()
resetAll()
end
function set2()
resetAll()
rs.setBundledOutput(side, colors.purple)
end
function set3()
resetAll()
rs.setBundledOutput(side, colors.lime)
end
function set4()
resetAll()
rs.setBundledOutput(side, colors.blue)
end
function set5()
resetAll()
rs.setBundledOutput(side, colors.red)
end
function fillTable()
setTable("1", set1, 2, 6, 3, 5)
setTable("2", set2, 2, 6, 8, 10)
setTable("3", set3, 2, 6, 13, 15)
setTable("4", set4, 2, 6, 18, 20)
setTable("5", set5, 2, 6, 23, 25)
end
function fill(text, color, bData)
mon.setBackgroundColor(color)
local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
for j = bData["ymin"], bData["ymax"] do
mon.setCursorPos(bData["xmin"], j)
if j == yspot then
for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
if k == xspot then
mon.write(text)
else
mon.write(" ")
end
end
else
for i = bData["xmin"], bData["xmax"] do
mon.write(" ")
end
end
end
mon.setBackgroundColor(colors.black)
end
function screen()
local currColor
for name,data in pairs(button) do
local on = data["active"]
if on == true then currColor = colors.lime else currColor = colors.red end
fill(name, currColor, data)
end
end
function checkxy(x, y)
for name, data in pairs(button) do
if y>=data["ymin"] and y <= data["ymax"] then
if x>=data["xmin"] and x<= data["xmax"] then
data["func"]()
data["active"] = not data["active"]
print(name)
end
end
end
end
function heading(text)
w, h = mon.getSize()
mon.setCursorPos((w-string.len(text))/2+1, 1)
mon.write(text)
end
fillTable()
while true do
mon.clear()
heading("Floor 1")
screen()
local e,side,x,y = os.pullEvent("monitor_touch")
print(x..":"..y)
checkxy(x,y)
sleep(.1)
end
P.S.This is the first thing I have pretty much ever coded, feel free to rip me a new one :D/>