Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.11 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.  
  262.  
  263. elseif searchlist(ptype,types["brt"]) and pblock.getConnected() then
  264. local energy = pblock.getEnergyStored()
  265. table.insert(iabstracts, {["ptype"]=ptype,
  266. ["ctype"]="brt",
  267. ["energyproduced"]=pblock.getEnergyProducedLastTick(),
  268. ["energystored"]=energy,
  269. ["maxenergystored"]=10000000,
  270. })
  271. table.insert(iabstracts, {["ptype"]=ptype,
  272. ["ctype"]="brtr",
  273. ["rotorspeed"]=pblock.getRotorSpeed(),
  274. })
  275. elseif searchlist(ptype,types["ic2estor"]) then
  276. table.insert(iabstracts, {["ptype"]=ptype,
  277. ["ctype"]="ic2estor",
  278. ["energystored"]=pblock.getEUStored(),
  279. ["maxenergystored"]=pblock.getEUCapacity(),
  280. })
  281. elseif searchlist(ptype,types["ic2nuclear"]) then
  282. table.insert(iabstracts, {["ptype"]=ptype,
  283. ["ctype"]="ic2nuclear",
  284. ["energyproduced"]=pblock.getEUOutput()*5.0,
  285. ["heatstored"]=pblock.getHeat(),
  286. ["heatcapacity"]=pblock.getMaxHeat(),
  287. })
  288. elseif searchlist(ptype,types["rct"]) then
  289. table.insert(iabstracts, {["ptype"]=ptype,
  290. ["ctype"]="rct",
  291. ["energyproduced"]=pblock.getTurbineOutput()*2
  292. })
  293. elseif searchlist(ptype,types["ae2estor"]) then
  294. table.insert(iabstracts, {["ptype"]=ptype,
  295. ["ctype"]="ae2estor",
  296. ["energystored"]=pblock.getStoredPower(),
  297. ["maxenergystored"]=pblock.getMaxStoredPower(),
  298. })
  299. elseif searchlist(ptype,types["ae2istor"]) then
  300. cells = ""
  301. c = pblock.getCellCount()
  302. for i=1,c do
  303. slot, status = pblock.getCellStatus(i)
  304. cells = cells .. string.sub(status,1,1)
  305. end
  306. table.insert(iabstracts, {["ptype"]=ptype,
  307. ["ctype"]="ae2istor",
  308. ["cellcount"]=c,
  309. ["cellstatus"]=cells,
  310. })
  311. elseif searchlist(ptype,types["rfestor"]) then
  312. table.insert(iabstracts, {["ptype"]=ptype,
  313. ["ctype"]="rfestor",
  314. ["energystored"]="123",
  315. ["maxenergystored"]="123",
  316. })
  317. elseif searchlist(ptype,types["rfgen"]) then
  318. table.insert(iabstracts, {["ptype"]=ptype,
  319. ["ctype"]="rfgen",
  320. ["energyproduced"]=pblock.getEnergyPerTickInfo(),
  321. })
  322. elseif searchlist(ptype,types["tanks"]) then
  323. info = pblock.getTankInfo()
  324. for i,v in ipairs(info) do
  325. cap = v.capacity
  326. if v.contents == nil then
  327. amount = 0
  328. name = "air"
  329. else
  330. amount = v.contents.amount
  331. name = v.contents.name
  332. end
  333. table.insert(iabstracts, {["ptype"]=ptype,
  334. ["ctype"]="tanks",
  335. ["liquidcapacity"]=cap,
  336. ["liquidstored"]=amount,
  337. ["liquidname"]=name,
  338. })
  339. end
  340. elseif searchlist(ptype,types["istor"]) then
  341. local size = pblock.getInventorySize()
  342. pblock.condenseItems()
  343. local su = 100/size
  344. local full = 0.0
  345. local count = 0
  346. for i=1,size do
  347. local stack = pblock.getStackInSlot(i)
  348. if stack~= nil then
  349. full = full + (stack.qty/stack.max_size)*su
  350. count = count + stack.qty
  351. end
  352. end
  353. table.insert(iabstracts, {["ptype"]=ptype,
  354. ["ctype"]="istor",
  355. ["inventorysize"]=size,
  356. ["fullness"]=full,
  357. ["itemcount"]=count,
  358. })
  359. elseif searchlist(ptype,types["ibarrel"]) then
  360. local size = pblock.getMaxStoredItems()
  361. local su = 100/size
  362. local full = 0.0
  363. local count = 0
  364. local stack = pblock.getStoredItems()
  365. local name = "air"
  366. if stack~= nil then
  367. full = full + (stack.qty*su)
  368. count = count + stack.qty
  369. name = stack.id..";"..stack.dmg
  370. end
  371. table.insert(iabstracts, {["ptype"]=ptype,
  372. ["ctype"]="ibarrel",
  373. ["inventorysize"]=size,
  374. ["fullness"]=full,
  375. ["itemcount"]=count,
  376. ["itemname"]=name,
  377. })
  378. elseif searchlist(ptype,types["sensor"]) then
  379. table.insert(iabstracts, {["ptype"]=ptype,["ctype"]="sensor",["block"]=pblock})
  380. --printdebug("abstracted sensor "..ptype)
  381. end
  382. if searchlist(ptype,types["rcboiler"]) then
  383. table.insert(iabstracts, {["ptype"]=ptype,
  384. ["ctype"]="rcboiler",
  385. ["heatstored"]=pblock.getTemperature(),
  386. })
  387. end
  388. end
  389.  
  390. -- condense the abstractions
  391. abstracts = {}
  392. for ai,at in pairs(iabstracts) do
  393. --printdebug(at["ctype"].." "..at["ptype"])
  394. found = false
  395. 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
  396. for aii,att in pairs(abstracts) do
  397. if att["ctype"]==at["ctype"] and (at["ctype"]~="tanks" or at["ptype"]==att["ptype"]) and att["liquidname"]==at["liquidname"] then
  398. --printdebug("Found previous "..at["ctype"])
  399. for k,v in pairs(att) do
  400. if k~="ctype" and k~="ptype" and k~="liquidname" then att[k] = att[k] + at[k] end
  401. -- BUG WITH FULLNESS
  402. end
  403. found = true
  404. break
  405. end
  406. end
  407. end
  408. if at["ctype"]=="ae2istor" then
  409. for aii,att in pairs(abstracts) do
  410. if att["ctype"]==at["ctype"] then
  411. for k,v in pairs(att) do
  412. if k~="ctype" and k~="ptype" and k~="cellstatus" then att[k] = att[k] + at[k]
  413. elseif k=="cellstatus" then att[k] = att[k] .. at[k] end
  414. end
  415. found = true
  416. break
  417. end
  418. end
  419. end
  420. if not found then
  421. table.insert(abstracts,at)
  422. end
  423. end
  424.  
  425. -- create messages
  426. for ai,abstraction in pairs(abstracts) do
  427. ctype = abstraction["ctype"]
  428. ptype = abstraction["ptype"]
  429. --printdebug(ctype)
  430.  
  431. -- BR reactor
  432. if ctype=="br" then
  433. if laste[ctype]==nil then laste[ctype]=1 end
  434. if abstraction["energyproduced"]>laste[ctype] then laste[ctype] = abstraction["energyproduced"] end
  435. ppercent = math.floor(((abstraction["energyproduced"]/laste[ctype])*100)+0.5)
  436. spercent = math.floor((abstraction["energystored"]*100/abstraction["maxenergystored"])+0.5)
  437. updatemessage(ai,"eout", readablenumber(abstraction["energyproduced"]).."RF/t|"..ptype.."|"..ppercent)
  438. --updatemessage(ai,"estor", readablenumber(abstraction["energystored"]).."RF|"..ptype.."|"..spercent)
  439. sleepytime = max(sleepytime,2)
  440. end
  441.  
  442. -- BR actively cooled reactor
  443. if ctype=="brac" then
  444. if laste[ctype]==nil then laste[ctype]=1 end
  445. if abstraction["fluidproduced"]>laste[ctype] then laste[ctype] = abstraction["fluidproduced"] end
  446. percent = math.floor(((abstraction["fluidproduced"]/laste[ctype])*100)+0.5)
  447. updatemessage(ai,"fout", readablenumber(abstraction["fluidproduced"]/1000).."B/t|"..ptype.."|"..percent.."|"..abstraction["fluidname"])
  448. sleepytime = max(sleepytime,2)
  449. end
  450.  
  451. -- BR turbine
  452. if ctype=="brt" then
  453. if laste[ctype]==nil then laste[ctype]=1 end
  454. if abstraction["energyproduced"]>laste[ctype] then laste[ctype] = abstraction["energyproduced"] end
  455. ppercent = math.floor(((abstraction["energyproduced"]/laste[ctype])*100)+0.5)
  456. --spercent = math.floor((abstraction["energystored"]*100/abstraction["maxenergystored"])+0.5)
  457. updatemessage(ai,"eout", readablenumber(abstraction["energyproduced"]).."RF/t|"..ptype.."|"..ppercent)
  458. sleepytime = max(sleepytime,2)
  459. end
  460. if ctype=="brtr" then
  461. if lasts[ctype]==nil then lasts[ctype]=1 end
  462. if abstraction["rotorspeed"]>lasts[ctype] then lasts[ctype] = abstraction["rotorspeed"] end
  463. rpercent = math.floor(((abstraction["rotorspeed"]/lasts[ctype])*100)+0.5)
  464. updatemessage(ai,"eoutspeed", math.floor(abstraction["rotorspeed"]).." RPM|"..ptype.."|"..rpercent)
  465. sleepytime = max(sleepytime,2)
  466. end
  467.  
  468. -- Batteries
  469. if ctype=="ic2estor" or ctype=="ae2estor" or ctype=="rfestor" then
  470. percent = math.floor((abstraction["energystored"]*100/abstraction["maxenergystored"])+0.5)
  471. updatemessage(ai,"estor", readablenumber(abstraction["energystored"])..units[ctype].."|"..ptype.."|"..percent)
  472. sleepytime = max(sleepytime,2)
  473. end
  474.  
  475. -- EU generators - RC turbine, IC2 nuclear
  476. if ctype=="rct" or ctype=="ic2nuclear" then
  477. if laste[ctype]==nil then laste[ctype]=1 end
  478. if abstraction["energyproduced"]>laste[ctype] then laste[ctype] = abstraction["energyproduced"] end
  479. percent = math.floor(((abstraction["energyproduced"]/laste[ctype])*100)+0.5)
  480. updatemessage(ai,"eout", readablenumber(abstraction["energyproduced"]).."EU/t|"..ptype.."|"..percent)
  481. sleepytime = max(sleepytime,2)
  482. end
  483.  
  484. -- IC2 nuclear
  485. if ctype=="ic2nuclear" then
  486. hpercent = math.floor((abstraction["heatstored"]*100/abstraction["heatcapacity"])+0.5)
  487. updatemessage(ai,"eheat", math.floor(abstraction["heatstored"]/100).."%|"..ptype.."|"..hpercent)
  488. sleepytime = max(sleepytime,2)
  489. end
  490.  
  491. -- RC boiler
  492. if ctype=="rcboiler" then
  493. if laste[ctype]==nil then laste[ctype]=1 end
  494. hpercent = math.floor((abstraction["heatstored"]/10)+0.5)
  495. updatemessage(ai,"eheat", math.floor(abstraction["heatstored"]).."C|"..ptype.."|"..hpercent)
  496. sleepytime = max(sleepytime,2)
  497. end
  498.  
  499. -- AE storage
  500. if ctype=="ae2istor" then
  501. updatemessage(ai,"aecells",abstraction["cellstatus"].."|"..ptype.."|"..abstraction["cellcount"])
  502. sleepytime = max(sleepytime,10)
  503. end
  504.  
  505. -- TE dynamos
  506. if ctype=="rfgen" then
  507. if laste[ctype]==nil then laste[ctype]=1 end
  508. if abstraction["energyproduced"]>laste[ctype] then laste[ctype] = abstraction["energyproduced"] end
  509. percent = math.floor(((abstraction["energyproduced"]/laste[ctype])*100)+0.5)
  510. updatemessage(ai,"eout", readablenumber(abstraction["energyproduced"]).."RF/t|"..ptype.."|"..percent)
  511. sleepytime = max(sleepytime,2)
  512. end
  513.  
  514. -- Tanks
  515. if ctype=="tanks" then
  516. percent = math.floor((abstraction["liquidstored"]*100/abstraction["liquidcapacity"])+0.5)
  517. buckets = math.floor(abstraction["liquidstored"]/100)/10
  518. updatemessage(ai,"tank", readablenumber(buckets).."B|"..ptype.."|"..percent.."|"..abstraction["liquidname"])
  519. sleepytime = max(sleepytime,2)
  520. end
  521.  
  522. -- storage
  523. if ctype=="istor" then
  524. updatemessage(ai,"istor", readablenumber(abstraction["itemcount"]).."|"..ptype.."|"..abstraction["fullness"])
  525. sleepytime = max(sleepytime, 15 * (abstraction["inventorysize"]/54))
  526. end
  527.  
  528. if ctype=="ibarrel" then
  529. updatemessage(ai,"ibarrel", readablenumber(abstraction["itemcount"]).."|"..ptype.."|"..abstraction["fullness"].."|"..abstraction["itemname"])
  530. sleepytime = max(sleepytime, 5)
  531. end
  532.  
  533. -- sensors
  534. if ctype=="sensor" then
  535. local players = abstraction["block"].getPlayers()
  536. local message = ""
  537. local count = 0
  538. local wcount = 0
  539. local ncount = 0
  540. local bcount = 0
  541. for i,v in ipairs(players) do
  542. message = message .. v.name .. "!"
  543. count = count + 1
  544. local whitelisted = searchlist(v.name,whitelist)
  545. local blacklisted = searchlist(v.name,blacklist)
  546. if whitelisted then
  547. printdebug("Whitelisted player detected")
  548. wcount = wcount + 1
  549. end
  550. if blacklisted then
  551. printdebug("Blacklisted player detected")
  552. bcount = bcount + 1
  553. end
  554. if not whitelisted and not blacklisted then
  555. printdebug("Neutral player detected")
  556. ncount = ncount + 1
  557. end
  558. end
  559. colouredredstone(colors.green, wcount > 0)
  560. colouredredstone(colors.yellow, ncount > 0)
  561. colouredredstone(colors.red, bcount > 0)
  562. if count==0 then message = "No-one"
  563. else message = string.sub(message,1,-2) end
  564. updatemessage(ai,"sensor",message.."|"..ptype.."|"..count)
  565. sleepytime = max(sleepytime,2)
  566. end
  567. end
  568.  
  569. -- send messages
  570. for i,v in pairs(messages) do
  571. rednet.send(dgserver,v,i)
  572. os.sleep(0.05)
  573. end
  574.  
  575. return sleepytime
  576. end
  577.  
  578. function paddlistener()
  579. local event, side = os.pullEvent("peripheral")
  580. printdebug("Peripheral added: "..side)
  581. addperipheral(side)
  582. end
  583.  
  584. function premovelistener()
  585. local event, side = os.pullEvent("peripheral_detach")
  586. printdebug("Peripheral removed: "..side)
  587. for k,v in pairs(pblocks) do
  588. if v[3]==side then
  589. table.remove(pblocks,k)
  590. print(v[2].." removed from active list")
  591. break
  592. end
  593. end
  594. end
  595.  
  596. function lamps(commands)
  597. printdebug("Lamps")
  598. if commands[3]~=nil and commands[3]~=os.getComputerLabel() then return end
  599. local value = tonumber(commands[2],16)
  600. for pbk,pbv in pairs(pblocks) do
  601. if searchlist(pbv[2],types["light"]) then
  602. pbv[1].setColor(value)
  603. end
  604. end
  605. end
  606.  
  607. function note(commands)
  608. printdebug("Note")
  609. if commands[3]~=nil and commands[3]~=os.getComputerLabel() then return end
  610. local note = mysplit(commands[2],"#")
  611. for pbk,pbv in pairs(pblocks) do
  612. if searchlist(pbv[2],types["note"]) then
  613. pbv[1].playSound(note[1],note[2],1,0,0,0)
  614. end
  615. end
  616. end
  617.  
  618. function brrod(commands)
  619. printdebug("BRrods")
  620. if commands[3]~=nil and commands[3]~=os.getComputerLabel() then return end
  621. local value = tonumber(commands[2])
  622. for pbk,pbv in pairs(pblocks) do
  623. if searchlist(pbv[2],types["br"]) then
  624. pbv[1].setAllControlRodLevels(value)
  625. end
  626. end
  627. end
  628.  
  629. function brflow(commands)
  630. printdebug("BRflow")
  631. if commands[3]~=nil and commands[3]~=os.getComputerLabel() then return end
  632. local value = tonumber(commands[2])
  633. for pbk,pbv in pairs(pblocks) do
  634. if searchlist(pbv[2],types["brt"]) then
  635. pbv[1].setFluidFlowRateMax(value)
  636. end
  637. end
  638. end
  639.  
  640. function redstone(commands)
  641. if commands[3]~=nil and commands[3]~=os.getComputerLabel() then return end
  642. local red = 0
  643. if commands[2]=="on" then red = 15 end
  644. printdebug("Redstone")
  645. for k,v in pairs(rs.getSides()) do
  646. rs.setAnalogOutput(v,red)
  647. end
  648. end
  649.  
  650. function credstone(commands)
  651. if commands[4]~=nil and commands[4]~=os.getComputerLabel() then return end
  652. local bits = colours[commands[2]]
  653. printdebug("CRedstone")
  654. for k,v in pairs(rs.getSides()) do
  655. local current = rs.getBundledOutput(v)
  656. if (commands[3]=="on") then rs.setBundledOutput(v,colors.combine(current,bits))
  657. elseif colors.test(current,bits) then rs.setBundledOutput(v,current-bits) end
  658. end
  659. end
  660.  
  661. function updatewhitelist(commands)
  662. if commands[2]==nil then return end
  663. whitelist = mysplit(commands[2],",")
  664. end
  665.  
  666. function updateblacklist(commands)
  667. if commands[2]==nil then return end
  668. blacklist = mysplit(commands[2],",")
  669. end
  670.  
  671. function eventlistener()
  672. local event, p1, p2, p3 = os.pullEvent()
  673.  
  674. -- broadcasts from server
  675. if event=="rednet_message" and p3=="dgserver" then
  676.  
  677. local sender = p1
  678. local message = p2
  679. if sender==nil then return end
  680. printdebug("Received command "..message)
  681. local commands = mysplit(message," ")
  682. if commands[1]=="lamps" then lamps(commands)
  683. elseif commands[1]=="note" then note(commands)
  684. elseif commands[1]=="redstone" then redstone(commands)
  685. elseif commands[1]=="credstone" then credstone(commands)
  686. elseif commands[1]=="whitelist" then updatewhitelist(commands)
  687. elseif commands[1]=="blacklist" then updateblacklist(commands)
  688. elseif commands[1]=="brrods" then brrod(commands)
  689. elseif commands[1]=="brflow" then brflow(commands)
  690. end
  691.  
  692. -- main loop
  693. elseif event=="timer" and p1==timedmessage then
  694.  
  695. timedmessage = os.startTimer(timedmessages())
  696.  
  697. elseif event=="slot_click" then
  698.  
  699. local slot = p1
  700. local side = p2
  701. local p = peripheral.wrap(side)
  702. local item = p.getSlot(slot)
  703. printdebug("Item selector clicked: "..item.id)
  704. if (searchlist(item.id,colourblocks)) then
  705. local bits = 2^item.dmg
  706. for k,v in pairs(rs.getSides()) do
  707. local current = rs.getBundledOutput(v)
  708. if colors.test(current,bits) then rs.setBundledOutput(v,current-bits)
  709. else rs.setBundledOutput(v,colors.combine(current,bits)) end
  710. end
  711. end
  712. if (searchlist(item.id,redstoneblocks)) then
  713. for k,v in pairs(rs.getSides()) do
  714. rs.setOutput(v, not rs.getOutput(v))
  715. end
  716. end
  717. end
  718.  
  719. end
  720.  
  721. -- the loop
  722. timedmessage = os.startTimer(5)
  723. while true do
  724. eventlistener()
  725. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement