Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- DeadGlass Server, by Deadlock989
- -- August 2015
- -- Made for the Industrial Rage server, http://www.kbgmc.com/index.php?forums/
- version = "1.01"
- debug = true
- knownprotocols = {"handshake","eout","estor","eoutspeed","aecells","eheat","tank","istor","sensor","fout","rcboiler"}
- groups = {
- ["batteries"]={"estor"},
- ["generators"]={"eout","eheat","fout","eoutspeed","rcboiler"},
- ["items"]={"aecells","istor"},
- ["liquids"]={"tank"},
- }
- colourlist = {"white", "lightgrey", "lightgray", "grey", "gray", "black", "red", "orange", "yellow", "green", "lime", "lightblue", "cyan", "blue", "purple", "magenta", "pink", "brown"}
- toggles = {}
- hbtime = 1
- hbcount = 0
- displaycheckcount = 30
- tickerttl = 30
- decaytime = 60
- playerlist = {}
- ticker = {}
- refreshticker = true
- pcols = {
- ["eout"]={0xFFFF00,0xAA9900},
- ["eoutspeed"]={0x6666FF,0x0000AA},
- ["estor"]={0x33FF33,0x00AA00},
- ["eheat"]={0xFFAA33,0xAA6600},
- ["istor"]={0x990099,0x330033},
- }
- gap = 3
- x = gap
- firstgaugey = 16+gap*2
- y = firstgaugey
- mx = 100
- my = 16
- boxes = {}
- outlines = {}
- texts = {}
- gauges = {}
- icons = {}
- lastmessage = {}
- timestamp = {}
- blockidms = {
- ["BigReactors-Reactor"]={"BigReactors:BRReactorPart",1},
- ["BigReactors-Turbine"]={"BigReactors:BRTurbinePart",1},
- ["batbox"]={"IC2:blockElectric",0},
- ["cesu"]={"IC2:blockElectric",7},
- ["mfe"]={"IC2:blockElectric",1},
- ["mfsu"]={"IC2:blockElectric",2},
- ["nuclear_reactor"]={"IC2:blockGenerator",5},
- ["reactor_fluid_port"]={"IC2:blockReactorFluidPort",0},
- ["tilechest"]={"appliedenergistics2:tile.BlockChest",0},
- ["tiledrive"]={"appliedenergistics2:tile.BlockDrive",0},
- ["blockenergycell"]={"appliedenergistics2:tile.BlockEnergyCell",0},
- ["blockdenseenergycell"]={"appliedenergistics2:tile.BlockDenseEnergyCell",0},
- ["tilecontroller"]={"appliedenergistics2:tile.BlockController",0},
- ["tile_thermalexpansion_cell_basic_name"]={"ThermalExpansion:Cell",1},
- ["tile_thermalexpansion_cell_hardened_name"]={"ThermalExpansion:Cell",2},
- ["tile_thermalexpansion_cell_redstone_name"]={"ThermalExpansion:Cell",3},
- ["tile_thermalexpansion_cell_resonant_name"]={"ThermalExpansion:Cell",4},
- ["tile_thermalexpansion_dynamo_steam_name"]={"ThermalExpansion:Dynamo",0},
- ["tile_thermalexpansion_dynamo_magmatic_name"]={"ThermalExpansion:Dynamo",1},
- ["tile_thermalexpansion_dynamo_compression_name"]={"ThermalExpansion:Dynamo",2},
- ["tile_thermalexpansion_dynamo_reactant_name"]={"ThermalExpansion:Dynamo",3},
- ["tile_thermalexpansion_dynamo_enervation_name"]={"ThermalExpansion:Dynamo",4},
- ["net_minecraft_src_buildcraft_factory_tiletank"]={"BuildCraft|Factory:tankBlock",0},
- ["tileentitycertustank"]={"extracells:certustank",0},
- ["openblocks_tank"]={"OpenBlocks:tank",0},
- ["rcirontankvalvetile"]={"Railcraft:machine.beta",1},
- ["rcsteeltankvalvetile"]={"Railcraft:machine.beta",14},
- ["chest"]={"minecraft:chest",0},
- ["tileskychest"]={"appliedenergistics2:tile.BlockSkyChest",1},
- ["iron"]={"IronChest:BlockIronChest",0},
- ["gold"]={"IronChest:BlockIronChest",1},
- ["diamond"]={"IronChest:BlockIronChest",2},
- ["copper"]={"IronChest:BlockIronChest",3},
- ["silver"]={"IronChest:BlockIronChest",4},
- ["crystal"]={"IronChest:BlockIronChest",5},
- ["obsidian"]={"IronChest:BlockIronChest",6},
- ["drum"]={"ExtraUtilities:drum",0},
- ["openperipheral_sensor"]={"OpenPeripheral:sensor",0},
- ["solid_fueled_boiler_firebox"]={"Railcraft:machine.beta",5},
- ["liquid_fueled_boiler_firebox"]={"Railcraft:machine.beta",6},
- ["rcsteamturbinetile"]={"Railcraft:machine.alpha",1},
- }
- -- routines
- function printdebug(text)
- if not debug then return end
- if monitorfound then
- term.redirect(mymonitor)
- print(text)
- term.redirect(term.native())
- else print(text) end
- end
- function fancytext(xx,yy,text,colour,alpha)
- if colour==nil then colour=0xFFFFFF end
- if alpha==nil then alpha=1 end
- local t1 = glass.addText(xx,yy,text,colour)
- t1.setAlpha(alpha)
- t1.setZ(10)
- local t2 = glass.addText(xx+1,yy+1,text,0x000000)
- t2.setAlpha(alpha/3)
- t2.setZ(5)
- return {t1,t2}
- end
- function searchlist(searchfor,list)
- for i=1,#list do
- if list[i]==searchfor then return true end
- end
- return false
- end
- function mysplit(str, pat)
- local t = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- function pairsByKeys (t, f)
- local a = {}
- for n in pairs(t) do table.insert(a, n) end
- table.sort(a, f)
- local i = 0 -- iterator variable
- local iter = function () -- iterator function
- i = i + 1
- if a[i] == nil then return nil
- else return a[i], t[a[i]]
- end
- end
- return iter
- end
- function outlinebox(xx,yy,ww,hh,colour)
- if colour==nil then colour=0x000000 end
- local outlinetable = {}
- table.insert(outlinetable,glass.addBox(xx-1,yy-1,ww+2,1,colour,0.5))
- table.insert(outlinetable,glass.addBox(xx-1,yy+hh,ww+2,1,colour,0.5))
- table.insert(outlinetable,glass.addBox(xx-1,yy,1,my,colour,0.5))
- table.insert(outlinetable,glass.addBox(xx+ww,yy,1,my,colour,0.5))
- return outlinetable
- end
- function haligntext(halign,t)
- t.setObjectAnchor(halign,"top")
- end
- function haligntexts(halign,texttable)
- for i,v in pairs(texttable) do
- v.setObjectAnchor(halign,"top")
- end
- end
- function saligntext(halign,valign,t)
- t.setScreenAnchor(halign,valign)
- end
- function saligntexts(halign,valign,texttable)
- for i,v in pairs(texttable) do
- v.setScreenAnchor(halign,valign)
- end
- end
- function redraw()
- glass.clear()
- drawtitle()
- boxes = {}
- outlines = {}
- texts = {}
- gauges = {}
- icons = {}
- lastmessage = {}
- ticker = {}
- end
- function drawtitle()
- local bannertext = "DeadGlass "..version
- if mx < 100 then bannertext = "DG "..version end
- haligntexts("middle",fancytext((16+mx+gap*2)/2,gap+4,bannertext,0xFFFFFF))
- outlinebox(gap,gap,16+mx+gap,my)
- glass.addGradientBox(gap,gap,16+mx+gap,my,0x000000,0.25,0x000000,0.5,1)
- end
- function reorder()
- newy = firstgaugey
- index = 1
- for i,v in pairsByKeys(boxes) do
- index = index + 1
- for ii,vv in ipairs(gauges[i]) do
- diff = newy - boxes[i].getY()
- vv.setY(vv.getY() + diff)
- end
- for ii,vv in ipairs(outlines[i]) do
- diff = newy - boxes[i].getY()
- vv.setY(vv.getY() + diff)
- end
- for ii,vv in ipairs(texts[i]) do
- diff = newy - boxes[i].getY()
- vv.setY(vv.getY() + diff)
- end
- boxes[i].setY(newy)
- icons[i].setY(newy)
- newy = newy + gap + my
- end
- end
- function timenow()
- return os.day()+(os.time()/24)
- end
- function estimatetimediff(day,time)
- local daynow = timenow()
- local daythen = day+(time/24)
- return (daynow-daythen)*20
- end
- function timedifftoenglish(diff)
- if diff < 0.1 then return "Now"
- elseif diff < 1 then return "<1 min"
- elseif diff >= 1 and diff < 60 then return math.floor(diff).." min"..(diff>=2 and "s" or "")
- elseif diff >= 60 and diff < 1440 then return math.floor(diff/60).." hour"..(diff/60>=2 and "s" or "")
- elseif diff >= 1440 and diff < 40320 then return math.floor(diff/1440).." day"..(diff/1440>=2 and "s" or "")
- else return "Long ago" end
- end
- function filetoarray(file,array)
- local array = {}
- if fs.exists(file) then
- local f = fs.open(file,"r")
- repeat
- local l = f.readLine()
- if (l~=nil) then table.insert(array,l) end
- until l==nil
- end
- return array
- end
- function arraytofile(array,file)
- local f = fs.open(file,"w")
- for k,v in ipairs(array) do
- f.writeLine(v)
- end
- f.close()
- end
- function writeplayerlist()
- local f = fs.open("playerlist.txt","w")
- for k,v in pairs(playerlist) do
- f.writeLine(k..","..v.day..","..v.time)
- end
- f.close()
- end
- function readplayerlist()
- local pt = filetoarray("playerlist.txt")
- for i,v in pairs(pt) do
- local split = mysplit(v,",")
- playerlist[split[1]] = {["day"]=split[2],["time"]=split[3]}
- --printdebug("Read player "..split[1]..", "..split[2]..", "..split[3])
- end
- end
- function sense()
- printdebug(key.." "..message.." "..value)
- local players = mysplit(message,"!")
- if players[1]=="No-one" then return end
- for k,v in pairs(players) do
- playerlist[v] = {["day"]=os.day(),["time"]=os.time()}
- local colour = 0xFFFF00
- local notecommand = "random.orb#0.5"
- if searchlist(v,blacklist) then
- notecommand = "nuclearcontrol:alarm-default#1"
- colour = 0xFF0000
- end
- if not searchlist(v,whitelist) then
- rednet.broadcast("note "..notecommand,"krazserver")
- addtoticker("Sensor "..sender.."/"..pid..": "..v, colour)
- end
- end
- --updateticker()
- writeplayerlist()
- end
- function daystominutes(days)
- return days*20
- end
- function textsetalpha(t,a)
- --printdebug("Alpha "..a)
- t.setAlpha(a)
- end
- function updateticker()
- local s = false
- if refreshticker then
- local yy = -20
- for i,t in ipairs(ticker) do
- t["text"].setY(yy)
- yy = yy - 10
- end
- refreshticker = false
- s = true
- end
- local i = 1
- if #ticker > 0 then s = true end
- while i <= #ticker do
- -- too old? Eastbourne
- if ticker[i]["timestamp"] >= tickerttl then
- ticker[i]["text"].delete()
- table.remove(ticker,i)
- else
- local a = 0.11+((tickerttl-ticker[i]["timestamp"])*0.89/tickerttl)
- textsetalpha(ticker[i]["text"], a)
- ticker[i]["timestamp"] = ticker[i]["timestamp"] + 1
- i = i + 1
- end
- end
- if s then glass.sync() end
- end
- function addtoticker(text,colour)
- if colour==nil then colour=0xFFFFFF end
- table.insert(ticker, 1, {["timestamp"]=0, ["text"]=glass.addText(-10,-20,text,colour)})
- haligntext("right", ticker[1]["text"])
- saligntext("right", "bottom", ticker[1]["text"])
- refreshticker = true
- end
- function draw()
- if texts[key] == nil then
- -- add to the bottom
- boxes[key] = glass.addBox(x+16+gap,y,mx,my,0xFFFFFF,0.0)
- texts[key] = fancytext(x+20+gap,y+4,message)
- icons[key] = glass.addIcon(x,y,blockidms[id][1],blockidms[id][2])
- icons[key].setZ(10)
- gauges[key] = {}
- outlines[key] = outlinebox(x+16+gap,y,mx,my)
- for i,v in pairs(outlinebox(gap,y,16,16)) do table.insert(outlines[key],v) end
- table.insert(outlines[key],glass.addGradientBox(gap,y,16,16,0x000000,0.25,0x000000,0.5,1))
- if protocol=="estor" or protocol=="eout" or protocol=="eheat" or protocol=="eoutspeed" or protocol=="istor" or protocol == "fout" then
- for i=1,mx,2 do
- local line = glass.addBox(x+20+i,y,1,my,0x000000,0.2)
- line.setZ(2)
- table.insert(outlines[key],line)
- end
- end
- y = y + my + gap
- -- sort them into key order
- reorder()
- else
- for ii,vv in ipairs(texts[key]) do
- vv.setText(message)
- end
- end
- printdebug(key.." "..message.." "..value)
- --printdebug("Key: "..key)
- --printdebug("Message: "..message)
- --printdebug("Svalue: "..svalue)
- --printdebug("Value: "..value)
- bx = boxes[key].getX()
- bw = boxes[key].getWidth()
- bpw = bw*(value/100)
- bxp = bx + bpw
- by = boxes[key].getY()
- bh = boxes[key].getHeight()
- for i,v in pairs(gauges[key]) do
- v.delete()
- end
- gauges[key] = {}
- if protocol=="estor" or protocol=="eout" or protocol=="eoutspeed" or protocol=="eheat" or protocol=="istor" then
- if (value>0) then
- table.insert(gauges[key],glass.addGradientBox(bx,by,bpw,bh,pcols[protocol][2],0.7,pcols[protocol][1],0.7,1))
- end
- if (value<100) then
- table.insert(gauges[key],glass.addGradientBox(bxp,by,bw-bpw,bh,0x111111,0.7,0x333333,0.7,1))
- end
- elseif protocol=="aecells" then
- for ii,vv in ipairs(texts[key]) do
- vv.setText("")
- end
- for i=1,value do
- cw = (bw/value)
- cellcol = 0x333333
- if string.sub(message,i,i)=="g" then cellcol = 0x66FF66
- elseif string.sub(message,i,i)=="o" then cellcol = 0xFF9933
- elseif string.sub(message,i,i)=="r" then cellcol = 0xFF0000 end
- table.insert(gauges[key],glass.addGradientBox(bx+((i-1)*cw),by,cw,bh,cellcol,0.5,cellcol,0.7,2))
- end
- elseif protocol=="tank" or protocol=="fout" then
- if (value>0) then
- table.insert(gauges[key],glass.addLiquid(bx,by,bpw,bh,liquidname))
- gauges[key][1].setAlpha(0.7)
- end
- if (value<100) then table.insert(gauges[key],glass.addGradientBox(bxp,by,bw-bpw,bh,0x000000,0.25,0x000000,0.5,1)) end
- end
- glass.sync()
- end
- function serialised(array)
- local s = ""
- for _,v in pairs(array) do
- s = s .. "," .. v
- end
- return string.sub(s, 2)
- end
- function sendlist(sender,listname,list)
- rednet.send(sender,listname.." "..serialised(list),"krazserver")
- end
- function broadcastlist(listname,list)
- rednet.broadcast(listname.." "..serialised(list),"krazserver")
- end
- -- initialise
- term.clear()
- term.setCursorPos(1,1)
- print("Deadlock989's DeadGlass server "..version.." running!")
- if (os.getComputerLabel()==nil) then
- print("Setting computer label to 'krazserver'.")
- os.setComputerLabel("krazserver")
- end
- print("Loading whitelist ...")
- whitelist = filetoarray("whitelist.txt")
- print("Loading blacklist ...")
- blacklist = filetoarray("blacklist.txt")
- print("Loading detected players list ...")
- readplayerlist()
- modemfound = false
- tgbfound = false
- monitorfound = false
- sides = peripheral.getNames()
- for i=1, #sides do
- thistype = peripheral.getType(sides[i])
- if thistype=="modem" then
- print("Attaching to modem on the "..sides[i].." ...")
- rednet.open(sides[i])
- modemfound = true
- end
- if thistype=="monitor" then
- print("Attaching to monitor on the "..sides[i].." ...")
- mymonitor = peripheral.wrap(sides[i])
- monitorfound = true
- mymonitor.clear()
- mymonitor.setCursorPos(1,1)
- end
- if not tgbfound and thistype=="openperipheral_bridge" then
- print("Attaching to TGB on the "..sides[i].." ...")
- glass = peripheral.wrap(sides[i])
- tgbfound = true
- -- workaround text bug?
- end
- end
- if not modemfound then error("No modem attached. Aborting.") end
- if not tgbfound then error("No Terminal Glasses Bridge attached. Aborting.") end
- rednet.host("krazserver","krazserver")
- glass.clear()
- drawtitle()
- print("Running ...")
- -- network events
- function netevent(p1,p2,p3)
- sender = p1
- message = p2
- protocol = p3
- psplit = mysplit(protocol,"/")
- protocol = psplit[1]
- if not searchlist(protocol,knownprotocols) then return end
- pid = psplit[2]
- if protocol=="handshake" then
- printdebug("Received handshake request from client "..sender)
- sendlist(sender,"whitelist",whitelist)
- sendlist(sender,"blacklist",blacklist)
- return
- end
- id = ""
- value = 0
- split = mysplit(message,"|")
- message = split[1]
- id = split[2]
- value = tonumber(split[3])
- svalue = split[3]
- liquidname = split[4]
- key = protocol.."/"..blockidms[id][1].."/"..sender.."/"..pid
- timestamp[key] = os.clock()
- -- new info?
- if lastmessage[key]==nil or lastmessage[key]~=message then
- lastmessage[key] = message
- if protocol == "sensor" then sense()
- else draw() end
- end
- end
- function listcommand(array,file,commands)
- local colour = 0x00FF00
- if commands[1]=="blacklist" then
- colour = 0xFF0000
- end
- if commands[2]==nil and commands[3]==nil then
- if #array==0 then addtoticker("List is empty")
- else
- addtoticker(commands[1]:gsub("^%l", string.upper)..":")
- for i,v in ipairs(array) do addtoticker(v, colour) end
- end
- --updateticker()
- elseif commands[3]==nil or (commands[2]~="add" and commands[2]~="remove") then
- addtoticker("Syntax: $$"..commands[1].." [add/remove playername]")
- --updateticker()
- elseif commands[2]=="add" then
- local found = false
- if commands[1]=="whitelist" and searchlist(commands[3],blacklist) then
- addtoticker("Player is in blacklist")
- return array
- elseif commands[1]=="blacklist" and searchlist(commands[3],whitelist) then
- addtoticker("Player is in whitelist")
- return array
- end
- for i,v in ipairs(array) do
- if v==commands[3] then
- found = true
- break
- end
- end
- if found then
- addtoticker("Player "..commands[3].." is already in list")
- --updateticker()
- else
- table.insert(array,commands[3])
- arraytofile(array,file)
- addtoticker("Player "..commands[3].." added")
- --updateticker()
- broadcastlist(commands[1],array)
- end
- elseif commands[2]=="remove" then
- local i = 1
- local found = false
- while i <= #array do
- if array[i]==commands[3] then
- table.remove(array,i)
- found = true
- break
- end
- i = i + 1
- end
- if found then
- arraytofile(array,file)
- addtoticker("Player "..commands[3].." removed")
- --updateticker()
- broadcastlist(commands[1],array)
- else
- addtoticker("Player "..commands[3].." not found in list")
- --updateticker()
- end
- end
- return array
- end
- -- chat commands
- function chatcommand(side,player,key,command)
- printdebug("Command received: "..command)
- commands = mysplit(command," ")
- if command=="reset" then
- redraw()
- elseif command=="small" then
- mx=50
- redraw()
- elseif command=="medium" then
- mx=100
- redraw()
- elseif command=="large" then
- mx=200
- redraw()
- elseif command=="reboot" then
- os.reboot()
- elseif command=="players" then
- for k,v in pairs(playerlist) do
- local colour = 0xFFFF00
- if searchlist(v, whitelist) then colour = 0x00FF00
- elseif searchlist(v, blacklist) then colour = 0xFF0000
- end
- addtoticker(k..": "..timedifftoenglish(estimatetimediff(v["day"],v["time"])))
- end
- elseif commands[1]=="redstone" then
- if commands[2]==nil or (commands[2]~="on" and commands[2]~="off") then
- addtoticker("Syntax: $$redstone on/off [clientname]")
- return
- end
- printdebug("Broadcasting ...")
- rednet.broadcast(command,"krazserver")
- elseif commands[1]=="credstone" then
- if commands[2]==nil or not searchlist(commands[2],colourlist) or commands[3]==nil or (commands[3]~="on" and commands[3]~="off") then
- addtoticker("Syntax: $$credstone colour on/off [clientname]")
- return
- end
- printdebug("Broadcasting ...")
- rednet.broadcast(command,"krazserver")
- elseif commands[1]=="lamps" then
- if commands[2]==nil or string.find(commands[2],"^%x%x%x%x%x%x$")==nil then
- addtoticker("Syntax: $$lamps FFFFFF [clientname]")
- return
- end
- printdebug("Broadcasting ...")
- rednet.broadcast(command,"krazserver")
- elseif commands[1]=="note" then
- if commands[2]==nil or string.find(commands[2],"#")==nil then
- addtoticker("Syntax: $$note notename#pitch [clientname]")
- return
- end
- printdebug("Broadcasting ...")
- rednet.broadcast(command,"krazserver")
- elseif commands[1]=="whitelist" then
- whitelist = listcommand(whitelist,"whitelist.txt",commands)
- elseif commands[1]=="blacklist" then
- blacklist = listcommand(blacklist,"blacklist.txt",commands)
- elseif commands[1]=="brrods" then
- if commands[2]==nil or string.find(commands[2],"^%d%d?%d?$")==nil or tonumber(commands[2])<0 or tonumber(commands[2])>100 then
- addtoticker("Syntax: $$brrods 0-100 [clientname]")
- return
- end
- rednet.broadcast(command,"krazserver")
- elseif commands[1]=="brflow" then
- if commands[2]==nil or string.find(commands[2],"^%d%d?%d?%d?$")==nil or tonumber(commands[2])<0 or tonumber(commands[2])>2000 then
- addtoticker("Syntax: $$brrods 0-2000 [clientname]")
- return
- end
- rednet.broadcast(command,"krazserver")
- end
- end
- function getkeyset(t)
- ks = {}
- for k,v in pairs(t) do
- table.insert(ks,k)
- end
- return ks
- end
- function timerlistener(p1)
- if p1==heartbeat then
- heartbeat = os.startTimer(hbtime)
- printdebug("*Heartbeat*")
- updateticker()
- hbcount = hbcount + 1
- if (hbcount%displaycheckcount==0) then
- -- kill dead client displays
- local k = 1
- for i,k in ipairs(getkeyset(timestamp)) do
- if (os.clock()-timestamp[k])>=decaytime then
- printdebug("Removing "..k)
- if boxes[k] ~= nil then
- for ii,vv in ipairs(gauges[k]) do
- vv.delete()
- end
- for ii,vv in ipairs(outlines[k]) do
- vv.delete()
- end
- for ii,vv in ipairs(texts[k]) do
- vv.delete()
- end
- boxes[k].delete()
- icons[k].delete()
- end
- gauges[k] = nil
- outlines[k] = nil
- texts[k] = nil
- boxes[k] = nil
- icons[k] = nil
- timestamp[k] = nil
- lastmessage[k] = nil
- end
- end
- reorder()
- glass.sync()
- end
- end
- end
- function eventlistener()
- local event, p1, p2, p3, p4, p5 = os.pullEvent()
- if event=="rednet_message" then netevent(p1,p2,p3)
- elseif event=="timer" then timerlistener(p1)
- elseif event=="glasses_chat_command" then chatcommand(p1,p2,p3,p4) end
- end
- -- main loop
- heartbeat = os.startTimer(hbtime)
- while true do
- eventlistener()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement