Advertisement
harris4681

KrazDGClient

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