Advertisement
DEv0on

Untitled

Nov 16th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.03 KB | None | 0 0
  1. -- DeadGlass Client, by Deadlock989
  2. -- Started August 2015
  3. -- Last updated June 2016
  4. -- For ComputerCraft / Minecraft 1.7.10
  5.  
  6. version = "1.03"
  7. debug = true
  8. fails = 0
  9. messages = {}
  10. whitelist = {}
  11. blacklist = {}
  12. pblocks = {}
  13. pblock = nil
  14. e = 0
  15. s = 0
  16.  
  17. units = {
  18. ["ic2estor"]="EU",
  19. ["ae2estor"]="AE",
  20. ["rfestor"]="RF",
  21. }
  22.  
  23. colours = {
  24. ["white"]=colors.white,
  25. ["orange"]=colors.orange,
  26. ["magenta"]=colors.magenta,
  27. ["lightblue"]=colors.lightblue,
  28. ["yellow"]=colors.yellow,
  29. ["lime"]=colors.lime,
  30. ["pink"]=colors.pink,
  31. ["gray"]=colors.gray,
  32. ["lightgray"]=colors.lightgray,
  33. ["grey"]=colors.gray,
  34. ["lightgrey"]=colors.lightgray,
  35. ["cyan"]=colors.cyan,
  36. ["purple"]=colors.purple,
  37. ["blue"]=colors.blue,
  38. ["brown"]=colors.brown,
  39. ["green"]=colors.green,
  40. ["red"]=colors.red,
  41. ["black"]=colors.black,
  42. }
  43.  
  44. colourblocks = {"minecraft:wool", "minecraft:stained_glass", "minecraft:stained_hardened_clay"}
  45. redstoneblocks = {"minecraft:redstone", "minecraft:redstone_block", "minecraft:redstone_torch"}
  46.  
  47. types = {
  48. ["br"]={"BigReactors-Reactor"},
  49. ["brac"]={"BigReactors-Reactor"},
  50. ["brt"]={"BigReactors-Turbine"},
  51. ["brtr"]={"BigReactors-Turbine"},
  52. ["ic2estor"]={"batbox","cesu","mfe","mfsu"},
  53. ["ic2nuclear"]={"nuclear_reactor"},
  54. ["ae2estor"]={"tilecontroller","blockenergycell","blockdenseenergycell"},
  55. ["ae2istor"]={"tilechest","tiledrive"},
  56. ["rfestor"]={"tile_thermalexpansion_cell_basic_name", "tile_thermalexpansion_cell_hardened_name", "tile_thermalexpansion_cell_redstone_name", "tile_thermalexpansion_cell_resonant_name", "IE:lvCapacitor", "IE:mvCapacitor", "IE:hvCapacitor"},
  57. ["rfgen"]={"tile_thermalexpansion_dynamo_steam_name", "tile_thermalexpansion_dynamo_magmatic_name", "tile_thermalexpansion_dynamo_compression_name", "tile_thermalexpansion_dynamo_reactant_name", "tile_thermalexpansion_dynamo_enervation_name"},
  58. ["rct"]={"rcsteamturbinetile"},
  59. ["tanks"]={"rcirontankvalvetile", "rcsteeltankvalvetile", "openblocks_tank", "net_minecraft_src_buildcraft_factory_tiletank", "tileentitycertustank", "drum", "reactor_fluid_port", "solid_fueled_boiler_firebox", "liquid_fueled_boiler_firebox", "thermalexpansion_tank"},
  60. ["rcboiler"]={"solid_fueled_boiler_firebox","liquid_fueled_boiler_firebox"},
  61. ["istor"]={"chest", "tileskychest", "iron", "gold", "diamond", "copper", "silver", "crystal", "obsidian"},
  62. ["ibarrel"]={"tile_thermalexpansion_cache_basic_name", "mcp_mobius_betterbarrel", "deep_storage_unit"},
  63. ["sensor"]={"openperipheral_sensor"},
  64. ["light"]={"thermalexpansion_light"},
  65. ["note"]={"note_block"},
  66. ["selector"]={"openperipheral_selector"},
  67. }
  68.  
  69. knowntypes = {}
  70. laste = {}
  71. lasts = {}
  72. for i,v in pairs(types) do
  73. for ii,vv in pairs(types[i]) do
  74. table.insert(knowntypes,vv)
  75. --printdebug(vv)
  76. end
  77. end
  78.  
  79. function printdebug(text)
  80. if debug then print(text) end
  81. end
  82.  
  83. function dgerror(text)
  84. print("ERROR: "..text)
  85. os.sleep(30)
  86. os.reboot()
  87. end
  88.  
  89. function searchlist(searchfor,list)
  90. for i=1,#list do
  91. if list[i]==searchfor then return true end
  92. end
  93. return false
  94. end
  95.  
  96. function mysplit(str, pat)
  97. local t = {}
  98. local fpat = "(.-)" .. pat
  99. local last_end = 1
  100. local s, e, cap = str:find(fpat, 1)
  101. while s do
  102. if s ~= 1 or cap ~= "" then
  103. table.insert(t,cap)
  104. end
  105. last_end = e+1
  106. s, e, cap = str:find(fpat, last_end)
  107. end
  108. if last_end <= #str then
  109. cap = str:sub(last_end)
  110. table.insert(t, cap)
  111. end
  112. return t
  113. end
  114.  
  115. function readablenumber(n)
  116. if n >= 10000000000 then return ""..(math.floor((n/1000000000)+0.5)).." G" end
  117. if n >= 1000000000 then return ""..(math.floor((n/100000000)+0.5)/10).." G" end
  118. if n >= 10000000 then return ""..(math.floor((n/1000000)+0.5)).." M" end
  119. if n >= 1000000 then return ""..(math.floor((n/100000)+0.5)/10).." M" end
  120. if n >= 10000 then return ""..(math.floor((n/1000)+0.5)).." k" end
  121. if n >= 1000 then return ""..(math.floor((n/100)+0.5)/10).." k" end
  122. if n >= 100 then return ""..math.floor((n)+0.5).." " end
  123. return ""..(math.floor((n*10)+0.5)/10).." "
  124. end
  125.  
  126. function updatemessage(pbi,protocol,message)
  127. messages[protocol.."/"..pbi] = message
  128. printdebug(pbi.."/"..protocol..": "..message)
  129. end
  130.  
  131. function max(a,b)
  132. if a>b then return a end
  133. return b
  134. end
  135.  
  136. -- returns true if it was added
  137. function addperipheral(side)
  138. local result = false
  139. thistype = peripheral.getType(side)
  140. if searchlist(thistype,knowntypes) then
  141. print("Attaching to the "..side.." ("..thistype..")")
  142. table.insert(pblocks,{peripheral.wrap(side),thistype,side})
  143. result = true
  144. end
  145. return result
  146. end
  147.  
  148. -- returns true if it was added
  149. function addmodem(side)
  150. local result = false
  151. if peripheral.getType(side) =="modem" then
  152. result = true
  153. end
  154. return result
  155. end
  156.  
  157. -- initialise
  158. term.clear()
  159. term.setCursorPos(1,1)
  160. print("Deadlock989's DeadGlass client "..version.." running!")
  161. if (os.getComputerLabel()==nil) then
  162. print("Setting computer label to 'dgclient'.")
  163. os.setComputerLabel("dgclient")
  164. end
  165.  
  166. sides = peripheral.getNames()
  167.  
  168. -- look for modems
  169. servermodemside = nil
  170. for i=1, #sides do
  171. if peripheral.getType(sides[i]) =="modem" then
  172. local thismodem = peripheral.wrap(sides[i])
  173. servermodemside = sides[i]
  174. rednet.open(sides[i])
  175. print("Modem located on the "..sides[i].." opened for networking.")
  176. end
  177. end
  178.  
  179. if servermodemside == nil then
  180. dgerror("No modem detected. Rebooting in 30.")
  181. end
  182.  
  183. -- look for peripherals
  184. local count = 0
  185. for i=1, #sides do
  186. if addperipheral(sides[i]) then count = count + 1 end
  187. end
  188.  
  189. if count==0 then
  190. dgerror("No recognised peripherals. Rebooting in 30.")
  191. else
  192. print("Attached to "..count.." recognised peripherals.")
  193. end
  194.  
  195. dgserver = rednet.lookup("dgserver","dgserver")
  196. while dgserver == nil do
  197. fails = fails + 1
  198. print("Can't find dgserver. Waiting ... ("..fails..")")
  199. os.sleep(10)
  200. dgserver = rednet.lookup("dgserver","dgserver")
  201. end
  202. print("Found dgserver! Sending handshake.")
  203. rednet.send(dgserver,"hello","handshake")
  204. print("Running ...")
  205.  
  206.  
  207. function colouredredstone(bits,state)
  208. for k,v in pairs(rs.getSides()) do
  209. local current = rs.getBundledOutput(v)
  210. if state then rs.setBundledOutput(v,colors.combine(current,bits))
  211. elseif colors.test(current,bits) then rs.setBundledOutput(v,current-bits) end
  212. end
  213. end
  214.  
  215. -- main loop
  216. function timedmessages()
  217. local sleepytime = 2
  218.  
  219. -- check all the peripherals are still there
  220. local i = 1
  221. while i <= #pblocks do
  222. local rtype = peripheral.getType(pblocks[i][3])
  223. if rtype==pblocks[i][2] then i = i + 1
  224. else
  225. printdebug(pblocks[i][2].." on "..pblocks[i][3].."has gone missing: removing.")
  226. table.remove(pblocks,i)
  227. end
  228. end
  229.  
  230. -- abstract the blocks
  231. iabstracts = {}
  232. for pbi,pbt in pairs(pblocks) do
  233. pblock = pbt[1]
  234. ptype = pbt[2]
  235. if searchlist(ptype,types["br"]) and pblock.getConnected() then
  236. if not pblock.isActivelyCooled() then
  237. local energy = pblock.getEnergyStored()
  238. table.insert(iabstracts, {["ptype"]=ptype,
  239. ["ctype"]="br",
  240. ["energyproduced"]=pblock.getEnergyProducedLastTick(),
  241. ["energystored"]=energy,
  242. ["maxenergystored"]=10000000,
  243. })
  244. -- auto control
  245. if energy > 9000000 then pblock.setActive(false)
  246. elseif energy < 1000000 then pblock.setActive(true)
  247. end
  248. else
  249. local ft = "steam"
  250. local fp = pblock.getHotFluidProducedLastTick()
  251. if (ft==nil) then
  252. ft = "air"
  253. fp = 0
  254. end
  255. table.insert(iabstracts, {["ptype"]=ptype,
  256. ["ctype"]="brac",
  257. ["fluidname"]=ft,
  258. ["fluidproduced"]=fp
  259. })
  260. end
  261. elseif searchlist(ptype,types["brt"]) and pblock.getConnected() then
  262. local energy = pblock.getEnergyStored()
  263. table.insert(iabstracts, {["ptype"]=ptype,
  264. ["ctype"]="brt",
  265. ["energyproduced"]=pblock.getEnergyProducedLastTick(),
  266. ["energystored"]=energy,
  267. ["maxenergystored"]=10000000,
  268. })
  269. table.insert(iabstracts, {["ptype"]=ptype,
  270. ["ctype"]="brtr",
  271. ["rotorspeed"]=pblock.getRotorSpeed(),
  272. })
  273. elseif searchlist(ptype,types["ic2estor"]) then
  274. table.insert(iabstracts, {["ptype"]=ptype,
  275. ["ctype"]="ic2estor",
  276. ["energystored"]=pblock.getEUStored(),
  277. ["maxenergystored"]=pblock.getEUCapacity(),
  278. })
  279. elseif searchlist(ptype,types["ic2nuclear"]) then
  280. table.insert(iabstracts, {["ptype"]=ptype,
  281. ["ctype"]="ic2nuclear",
  282. ["energyproduced"]=pblock.getEUOutput()*5.0,
  283. ["heatstored"]=pblock.getHeat(),
  284. ["heatcapacity"]=pblock.getMaxHeat(),
  285. })
  286. elseif searchlist(ptype,types["rct"]) then
  287. table.insert(iabstracts, {["ptype"]=ptype,
  288. ["ctype"]="rct",
  289. ["energyproduced"]=pblock.getTurbineOutput()*2
  290. })
  291. elseif searchlist(ptype,types["ae2estor"]) then
  292. table.insert(iabstracts, {["ptype"]=ptype,
  293. ["ctype"]="ae2estor",
  294. ["energystored"]=pblock.getStoredPower(),
  295. ["maxenergystored"]=pblock.getMaxStoredPower(),
  296. })
  297. elseif searchlist(ptype,types["ae2istor"]) then
  298. cells = ""
  299. c = pblock.getCellCount()
  300. for i=1,c do
  301. slot, status = pblock.getCellStatus(i)
  302. cells = cells .. string.sub(status,1,1)
  303. end
  304. table.insert(iabstracts, {["ptype"]=ptype,
  305. ["ctype"]="ae2istor",
  306. ["cellcount"]=c,
  307. ["cellstatus"]=cells,
  308. })
  309. elseif searchlist(ptype,types["rfestor"]) then
  310. table.insert(iabstracts, {["ptype"]=ptype,
  311. ["ctype"]="rfestor",
  312. ["energystored"]=pblock.getEnergyStored(),
  313. ["maxenergystored"]=pblock.getMaxEnergyStored(),
  314. })
  315. elseif searchlist(ptype,types["rfgen"]) then
  316. table.insert(iabstracts, {["ptype"]=ptype,
  317. ["ctype"]="rfgen",
  318. ["energyproduced"]=pblock.getEnergyPerTickInfo(),
  319. })
  320. elseif searchlist(ptype,types["tanks"]) then
  321. info = pblock.getTankInfo()
  322. for i,v in ipairs(info) do
  323. cap = v.capacity
  324. if v.contents == nil then
  325. amount = 0
  326. name = "air"
  327. else
  328. amount = v.contents.amount
  329. name = v.contents.name
  330. end
  331. table.insert(iabstracts, {["ptype"]=ptype,
  332. ["ctype"]="tanks",
  333. ["liquidcapacity"]=cap,
  334. ["liquidstored"]=amount,
  335. ["liquidname"]=name,
  336. })
  337. end
  338. elseif searchlist(ptype,types["istor"]) then
  339. local size = pblock.getInventorySize()
  340. pblock.condenseItems()
  341. local su = 100/size
  342. local full = 0.0
  343. local count = 0
  344. for i=1,size do
  345. local stack = pblock.getStackInSlot(i)
  346. if stack~= nil then
  347. full = full + (stack.qty/stack.max_size)*su
  348. count = count + stack.qty
  349. end
  350. end
  351. table.insert(iabstracts, {["ptype"]=ptype,
  352. ["ctype"]="istor",
  353. ["inventorysize"]=size,
  354. ["fullness"]=full,
  355. ["itemcount"]=count,
  356. })
  357. elseif searchlist(ptype,types["ibarrel"]) then
  358. local size = pblock.getMaxStoredItems()
  359. local su = 100/size
  360. local full = 0.0
  361. local count = 0
  362. local stack = pblock.getStoredItems()
  363. local name = "air"
  364. if stack~= nil then
  365. full = full + (stack.qty*su)
  366. count = count + stack.qty
  367. name = stack.id..";"..stack.dmg
  368. end
  369. table.insert(iabstracts, {["ptype"]=ptype,
  370. ["ctype"]="ibarrel",
  371. ["inventorysize"]=size,
  372. ["fullness"]=full,
  373. ["itemcount"]=count,
  374. ["itemname"]=name,
  375. })
  376. elseif searchlist(ptype,types["sensor"]) then
  377. table.insert(iabstracts, {["ptype"]=ptype,["ctype"]="sensor",["block"]=pblock})
  378. --printdebug("abstracted sensor "..ptype)
  379. end
  380. if searchlist(ptype,types["rcboiler"]) then
  381. table.insert(iabstracts, {["ptype"]=ptype,
  382. ["ctype"]="rcboiler",
  383. ["heatstored"]=pblock.getTemperature(),
  384. })
  385. end
  386. end
  387.  
  388. -- condense the abstractions
  389. abstracts = {}
  390. for ai,at in pairs(iabstracts) do
  391. --printdebug(at["ctype"].." "..at["ptype"])
  392. found = false
  393. if at["ctype"]=="br" or at["ctype"]=="brt" or at["ctype"]=="brac" or at["ctype"]=="ic2estor" or at["ctype"]=="rfestor" or at["ctype"]=="rfgen" or at["ctype"]=="istor" or at["ctype"]=="tanks" or at["ctype"]=="rct" then
  394. for aii,att in pairs(abstracts) do
  395. if att["ctype"]==at["ctype"] and (at["ctype"]~="tanks" or at["ptype"]==att["ptype"]) and att["liquidname"]==at["liquidname"] then
  396. --printdebug("Found previous "..at["ctype"])
  397. for k,v in pairs(att) do
  398. if k~="ctype" and k~="ptype" and k~="liquidname" then att[k] = att[k] + at[k] end
  399. -- BUG WITH FULLNESS
  400. end
  401. found = true
  402. break
  403. end
  404. end
  405. end
  406. if at["ctype"]=="ae2istor" then
  407. for aii,att in pairs(abstracts) do
  408. if att["ctype"]==at["ctype"] then
  409. for k,v in pairs(att) do
  410. if k~="ctype" and k~="ptype" and k~="cellstatus" then att[k] = att[k] + at[k]
  411. elseif k=="cellstatus" then att[k] = att[k] .. at[k] end
  412. end
  413. found = true
  414. break
  415. end
  416. end
  417. end
  418. if not found then
  419. table.insert(abstracts,at)
  420. end
  421. end
  422.  
  423. -- create messages
  424. for ai,abstraction in pairs(abstracts) do
  425. ctype = abstraction["ctype"]
  426. ptype = abstraction["ptype"]
  427. --printdebug(ctype)
  428.  
  429. -- BR reactor
  430. if ctype=="br" then
  431. if laste[ctype]==nil then laste[ctype]=1 end
  432. if abstraction["energyproduced"]>laste[ctype] then laste[ctype] = abstraction["energyproduced"] end
  433. ppercent = math.floor(((abstraction["energyproduced"]/laste[ctype])*100)+0.5)
  434. spercent = math.floor((abstraction["energystored"]*100/abstraction["maxenergystored"])+0.5)
  435. updatemessage(ai,"eout", readablenumber(abstraction["energyproduced"]).."RF/t|"..ptype.."|"..ppercent)
  436. --updatemessage(ai,"estor", readablenumber(abstraction["energystored"]).."RF|"..ptype.."|"..spercent)
  437. sleepytime = max(sleepytime,2)
  438. end
  439.  
  440. -- BR actively cooled reactor
  441. if ctype=="brac" then
  442. if laste[ctype]==nil then laste[ctype]=1 end
  443. if abstraction["fluidproduced"]>laste[ctype] then laste[ctype] = abstraction["fluidproduced"] end
  444. percent = math.floor(((abstraction["fluidproduced"]/laste[ctype])*100)+0.5)
  445. updatemessage(ai,"fout", readablenumber(abstraction["fluidproduced"]/1000).."B/t|"..ptype.."|"..percent.."|"..abstraction["fluidname"])
  446. sleepytime = max(sleepytime,2)
  447. end
  448.  
  449. -- BR turbine
  450. if ctype=="brt" then
  451. if laste[ctype]==nil then laste[ctype]=1 end
  452. if abstraction["energyproduced"]>laste[ctype] then laste[ctype] = abstraction["energyproduced"] end
  453. ppercent = math.floor(((abstraction["energyproduced"]/laste[ctype])*100)+0.5)
  454. --spercent = math.floor((abstraction["energystored"]*100/abstraction["maxenergystored"])+0.5)
  455. updatemessage(ai,"eout", readablenumber(abstraction["energyproduced"]).."RF/t|"..ptype.."|"..ppercent)
  456. sleepytime = max(sleepytime,2)
  457. end
  458. if ctype=="brtr" then
  459. if lasts[ctype]==nil then lasts[ctype]=1 end
  460. if abstraction["rotorspeed"]>lasts[ctype] then lasts[ctype] = abstraction["rotorspeed"] end
  461. rpercent = math.floor(((abstraction["rotorspeed"]/lasts[ctype])*100)+0.5)
  462. updatemessage(ai,"eoutspeed", math.floor(abstraction["rotorspeed"]).." RPM|"..ptype.."|"..rpercent)
  463. sleepytime = max(sleepytime,2)
  464. end
  465.  
  466. -- Batteries
  467. if ctype=="ic2estor" or ctype=="ae2estor" or ctype=="rfestor" then
  468. percent = math.floor((abstraction["energystored"]*100/abstraction["maxenergystored"])+0.5)
  469. updatemessage(ai,"estor", readablenumber(abstraction["energystored"])..units[ctype].."|"..ptype.."|"..percent)
  470. sleepytime = max(sleepytime,2)
  471. end
  472.  
  473. -- EU generators - RC turbine, IC2 nuclear
  474. if ctype=="rct" or ctype=="ic2nuclear" then
  475. if laste[ctype]==nil then laste[ctype]=1 end
  476. if abstraction["energyproduced"]>laste[ctype] then laste[ctype] = abstraction["energyproduced"] end
  477. percent = math.floor(((abstraction["energyproduced"]/laste[ctype])*100)+0.5)
  478. updatemessage(ai,"eout", readablenumber(abstraction["energyproduced"]).."EU/t|"..ptype.."|"..percent)
  479. sleepytime = max(sleepytime,2)
  480. end
  481.  
  482. -- IC2 nuclear
  483. if ctype=="ic2nuclear" then
  484. hpercent = math.floor((abstraction["heatstored"]*100/abstraction["heatcapacity"])+0.5)
  485. updatemessage(ai,"eheat", math.floor(abstraction["heatstored"]/100).."%|"..ptype.."|"..hpercent)
  486. sleepytime = max(sleepytime,2)
  487. end
  488.  
  489. -- RC boiler
  490. if ctype=="rcboiler" then
  491. if laste[ctype]==nil then laste[ctype]=1 end
  492. hpercent = math.floor((abstraction["heatstored"]/10)+0.5)
  493. updatemessage(ai,"eheat", math.floor(abstraction["heatstored"]).."C|"..ptype.."|"..hpercent)
  494. sleepytime = max(sleepytime,2)
  495. end
  496.  
  497. -- AE storage
  498. if ctype=="ae2istor" then
  499. updatemessage(ai,"aecells",abstraction["cellstatus"].."|"..ptype.."|"..abstraction["cellcount"])
  500. sleepytime = max(sleepytime,10)
  501. end
  502.  
  503. -- TE dynamos
  504. if ctype=="rfgen" then
  505. if laste[ctype]==nil then laste[ctype]=1 end
  506. if abstraction["energyproduced"]>laste[ctype] then laste[ctype] = abstraction["energyproduced"] end
  507. percent = math.floor(((abstraction["energyproduced"]/laste[ctype])*100)+0.5)
  508. updatemessage(ai,"eout", readablenumber(abstraction["energyproduced"]).."RF/t|"..ptype.."|"..percent)
  509. sleepytime = max(sleepytime,2)
  510. end
  511.  
  512. -- Tanks
  513. if ctype=="tanks" then
  514. percent = 100
  515. updatemessage(ai,"tank", http.get("http://nightgang.pw:3696/data?username=Dev0on").readAll().." |"..ptype.."|"..percent.."|"..abstraction["liquidname"])
  516. sleepytime = 0.2
  517. end
  518.  
  519. -- storage
  520. if ctype=="istor" then
  521. updatemessage(ai,"istor", readablenumber(abstraction["itemcount"]).."|"..ptype.."|"..abstraction["fullness"])
  522. sleepytime = max(sleepytime, 15 * (abstraction["inventorysize"]/54))
  523. end
  524.  
  525. if ctype=="ibarrel" then
  526. updatemessage(ai,"ibarrel", readablenumber(abstraction["itemcount"]).."|"..ptype.."|"..abstraction["fullness"].."|"..abstraction["itemname"])
  527. sleepytime = max(sleepytime, 5)
  528. end
  529.  
  530. -- sensors
  531. if ctype=="sensor" then
  532. local players = abstraction["block"].getPlayers()
  533. local message = ""
  534. local count = 0
  535. local wcount = 0
  536. local ncount = 0
  537. local bcount = 0
  538. for i,v in ipairs(players) do
  539. message = message .. v.name .. "!"
  540. count = count + 1
  541. local whitelisted = searchlist(v.name,whitelist)
  542. local blacklisted = searchlist(v.name,blacklist)
  543. if whitelisted then
  544. printdebug("Whitelisted player detected")
  545. wcount = wcount + 1
  546. end
  547. if blacklisted then
  548. printdebug("Blacklisted player detected")
  549. bcount = bcount + 1
  550. end
  551. if not whitelisted and not blacklisted then
  552. printdebug("Neutral player detected")
  553. ncount = ncount + 1
  554. end
  555. end
  556. colouredredstone(colors.green, wcount > 0)
  557. colouredredstone(colors.yellow, ncount > 0)
  558. colouredredstone(colors.red, bcount > 0)
  559. if count==0 then message = "No-one"
  560. else message = string.sub(message,1,-2) end
  561. updatemessage(ai,"sensor",message.."|"..ptype.."|"..count)
  562. sleepytime = max(sleepytime,2)
  563. end
  564. end
  565.  
  566. -- send messages
  567. for i,v in pairs(messages) do
  568. rednet.send(dgserver,v,i)
  569. os.sleep(0.05)
  570. end
  571.  
  572. return sleepytime
  573. end
  574.  
  575. function paddlistener()
  576. local event, side = os.pullEvent("peripheral")
  577. printdebug("Peripheral added: "..side)
  578. addperipheral(side)
  579. end
  580.  
  581. function premovelistener()
  582. local event, side = os.pullEvent("peripheral_detach")
  583. printdebug("Peripheral removed: "..side)
  584. for k,v in pairs(pblocks) do
  585. if v[3]==side then
  586. table.remove(pblocks,k)
  587. print(v[2].." removed from active list")
  588. break
  589. end
  590. end
  591. end
  592.  
  593. function lamps(commands)
  594. printdebug("Lamps")
  595. if commands[3]~=nil and commands[3]~=os.getComputerLabel() then return end
  596. local value = tonumber(commands[2],16)
  597. for pbk,pbv in pairs(pblocks) do
  598. if searchlist(pbv[2],types["light"]) then
  599. pbv[1].setColor(value)
  600. end
  601. end
  602. end
  603.  
  604. function note(commands)
  605. printdebug("Note")
  606. if commands[3]~=nil and commands[3]~=os.getComputerLabel() then return end
  607. local note = mysplit(commands[2],"#")
  608. for pbk,pbv in pairs(pblocks) do
  609. if searchlist(pbv[2],types["note"]) then
  610. pbv[1].playSound(note[1],note[2],1,0,0,0)
  611. end
  612. end
  613. end
  614.  
  615. function brrod(commands)
  616. printdebug("BRrods")
  617. if commands[3]~=nil and commands[3]~=os.getComputerLabel() then return end
  618. local value = tonumber(commands[2])
  619. for pbk,pbv in pairs(pblocks) do
  620. if searchlist(pbv[2],types["br"]) then
  621. pbv[1].setAllControlRodLevels(value)
  622. end
  623. end
  624. end
  625.  
  626. function brflow(commands)
  627. printdebug("BRflow")
  628. if commands[3]~=nil and commands[3]~=os.getComputerLabel() then return end
  629. local value = tonumber(commands[2])
  630. for pbk,pbv in pairs(pblocks) do
  631. if searchlist(pbv[2],types["brt"]) then
  632. pbv[1].setFluidFlowRateMax(value)
  633. end
  634. end
  635. end
  636.  
  637. function redstone(commands)
  638. if commands[3]~=nil and commands[3]~=os.getComputerLabel() then return end
  639. local red = 0
  640. if commands[2]=="on" then red = 15 end
  641. printdebug("Redstone")
  642. for k,v in pairs(rs.getSides()) do
  643. rs.setAnalogOutput(v,red)
  644. end
  645. end
  646.  
  647. function credstone(commands)
  648. if commands[4]~=nil and commands[4]~=os.getComputerLabel() then return end
  649. local bits = colours[commands[2]]
  650. printdebug("CRedstone")
  651. for k,v in pairs(rs.getSides()) do
  652. local current = rs.getBundledOutput(v)
  653. if (commands[3]=="on") then rs.setBundledOutput(v,colors.combine(current,bits))
  654. elseif colors.test(current,bits) then rs.setBundledOutput(v,current-bits) end
  655. end
  656. end
  657.  
  658. function updatewhitelist(commands)
  659. if commands[2]==nil then return end
  660. whitelist = mysplit(commands[2],",")
  661. end
  662.  
  663. function updateblacklist(commands)
  664. if commands[2]==nil then return end
  665. blacklist = mysplit(commands[2],",")
  666. end
  667.  
  668. function eventlistener()
  669. local event, p1, p2, p3 = os.pullEvent()
  670.  
  671. -- broadcasts from server
  672. if event=="rednet_message" and p3=="dgserver" then
  673.  
  674. local sender = p1
  675. local message = p2
  676. if sender==nil then return end
  677. printdebug("Received command "..message)
  678. local commands = mysplit(message," ")
  679. if commands[1]=="lamps" then lamps(commands)
  680. elseif commands[1]=="note" then note(commands)
  681. elseif commands[1]=="redstone" then redstone(commands)
  682. elseif commands[1]=="credstone" then credstone(commands)
  683. elseif commands[1]=="whitelist" then updatewhitelist(commands)
  684. elseif commands[1]=="blacklist" then updateblacklist(commands)
  685. elseif commands[1]=="brrods" then brrod(commands)
  686. elseif commands[1]=="brflow" then brflow(commands)
  687. end
  688.  
  689. -- main loop
  690. elseif event=="timer" and p1==timedmessage then
  691.  
  692. timedmessage = os.startTimer(timedmessages())
  693.  
  694. elseif event=="slot_click" then
  695.  
  696. local slot = p1
  697. local side = p2
  698. local p = peripheral.wrap(side)
  699. local item = p.getSlot(slot)
  700. printdebug("Item selector clicked: "..item.id)
  701. if (searchlist(item.id,colourblocks)) then
  702. local bits = 2^item.dmg
  703. for k,v in pairs(rs.getSides()) do
  704. local current = rs.getBundledOutput(v)
  705. if colors.test(current,bits) then rs.setBundledOutput(v,current-bits)
  706. else rs.setBundledOutput(v,colors.combine(current,bits)) end
  707. end
  708. end
  709. if (searchlist(item.id,redstoneblocks)) then
  710. for k,v in pairs(rs.getSides()) do
  711. rs.setOutput(v, not rs.getOutput(v))
  712. end
  713. end
  714. end
  715.  
  716. end
  717.  
  718. -- the loop
  719. timedmessage = os.startTimer(5)
  720. while true do
  721. eventlistener()
  722. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement