draddy

CC multiEmitter for ae2 ME

Jan 11th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.51 KB | None | 0 0
  1. --Multi Emitter 1.0.2 for AE2
  2. --written by Draddy
  3.  
  4.  
  5. local me                    --ME System
  6. local monitor               --Monitor
  7. local modem = nil           --Modem for Remote Monitor
  8. local cablePos              --Redstone BundledCable
  9. local cols = 0              --set Redstone Colors
  10. local useRemote = true      --switch on/off Remoteservice
  11. local activ={}              --helping Array for emit status
  12. local file = "disk/emitList"--the List File
  13. local lastFileSize = 0      --size of list file on last read (so read can skipped it file isn't changed
  14.  
  15.  
  16. local meItems = {}          --workspace after file is read MOVED TO emitList!
  17.  
  18.  
  19. --get the peripherals
  20. function initPeripherals()
  21.     local per = peripheral.getNames()
  22.     for i=1,#per do
  23.         --Monitor
  24.         if peripheral.getType(per[i]) == "monitor" then
  25.             monitor = peripheral.wrap(per[i])
  26.         --Modem(s) 
  27.         elseif peripheral.getType(per[i]) == "modem" then
  28.             m = peripheral.wrap(per[i])
  29.             if m.isWireless() and useRemote then
  30.                 modem = peripheral.wrap(per[i])
  31.                 os.loadAPI("disk/wirelessSend.lua")
  32.             else
  33.                 cModem = peripheral.wrap(per[i])
  34.             end
  35.         --bundeld Redstone cable
  36.         elseif peripheral.getType(per[i]) == "ccmp:saved_multipart" then
  37.             cablePos = per[i]
  38.         --ME System
  39.         elseif string.find(peripheral.getType(per[i]), "appliedenergistics2:") then
  40.             me = peripheral.wrap(per[i])
  41.         end
  42.     end
  43. end
  44.  
  45.  
  46. --readFile if changed since last read
  47. function readFile ()
  48.     runs = true
  49.     running = true
  50.     while runs do
  51.         f = fs.open(file, "r")
  52.         if f then runs = false else err1="no file@ ".. getTime() end
  53.     end
  54.     if err1 then print(err1) end
  55.     err1 = nil
  56.     fileSize = fs.getSize(file)
  57.     if fileSize ~= lastFileSize then
  58.         lastFileSize = fileSize
  59.         meItems = {}
  60.         while running do
  61.             t = f.readLine()
  62.             if t == nil then
  63.                 running = false
  64.  
  65.             else
  66.                 tT = textutils.unserialise(t)
  67.                 table.insert(meItems, tT)
  68.             end
  69.            
  70.         end
  71.         print("file read@ " .. getTime())
  72.     end
  73. end
  74.  
  75.  
  76.  
  77. --clear Monitor(s)
  78. function clearMon()
  79.     if modem then wirelessSend.remoteColors(colors.black, colors.white) end
  80.     monitor.setBackgroundColor(colors.black)
  81.     monitor.clear()
  82.     monitor.setCursorPos(1,1)
  83.     centerT(" Multi Emitter ", 1, colors.gray, colors.purple, "head")
  84.     centerT("Count ", 2, colors.black, colors.purple, "left")
  85.     centerT("Item", 2, colors.black, colors.purple, "left2")
  86.     centerT("Signal", 2, colors.black,  colors.purple, "right")
  87. end
  88.  
  89.  
  90. --bring text to Monitor(s)
  91. function centerT(text, line, txtback, txtcolor, pos)
  92.     monX,monY = monitor.getSize()
  93.     monitor.setBackgroundColor(txtback)
  94.     monitor.setTextColor(txtcolor)
  95.     length = string.len(text)
  96.     dif = math.floor(monX-length)
  97.     x = math.floor(dif/2)
  98.     if modem then
  99.         remX, remY = wirelessSend.remoteSize()
  100.         rDif = math.floor(remX-length)
  101.         rX = math.floor(rDif/2)
  102.     end
  103.      
  104.     if  pos == "head" then
  105.         monitor.setCursorPos(x+1, line)
  106.         monitor.write(text)
  107.         if modem then wirelessSend.remoteOutput(text, txtback, txtcolor, rX+1, line, false) end
  108.     elseif pos == "left" then
  109.         monitor.setCursorPos(2, line)
  110.         monitor.clearLine()
  111.         monitor.write(text)
  112.         if modem then wirelessSend.remoteOutput(text, txtback, txtcolor, 2, line, true) end
  113.     elseif pos == "left2" then
  114.         monitor.setCursorPos(10, line)
  115.         monitor.write(text)
  116.         if modem then wirelessSend.remoteOutput(text, txtback, txtcolor, 10, line, false) end
  117.     elseif pos == "right" then
  118.         monitor.setCursorPos(monX-length, line)
  119.         monitor.write(text)
  120.         if modem then wirelessSend.remoteOutput(text, txtback, txtcolor, remX-length, line, false) end
  121.     end
  122. end
  123.  
  124.  
  125. --set new Redstone Outputs
  126. function setCable()
  127.   rs.setBundledOutput(cablePos, cols)
  128. end
  129.  
  130.  
  131. --get values from ME System and set outputs
  132. function checkMe()
  133.     local i = 3
  134.     cols = 0
  135.     tCol = colors.white
  136.     for a = 1, #meItems do
  137.         w = a
  138.         pcall(getItem) --pcall to prevent nullPointer error
  139.         if sItem == "null" then --if item not in ME System, set to emit/produce
  140.             if i%2==1 then tCol=colors.lightGray else tCol=colors.white end
  141.             centerT("0",i, colors.black, tCol, "left")
  142.             centerT(meItems[a].displayName, i, colors.black, tCol, "left2")
  143.             if meItems[a].emit == "low" then
  144.               cols = cols+colors[meItems[a].color]
  145.               activ[a] = true
  146.               centerT("activ", i, colors.black, colors[meItems[a].color], "right")
  147.             else
  148.               centerT("inactiv", i, colors.black, colors[meItems[a].color], "right")
  149.               activ[a] = false
  150.             end
  151.             i = i + 1
  152.         else --item is in ME System - check values ...
  153.             if i%2==1 then tCol=colors.lightGray else tCol=colors.white end
  154.             centerT(tostring(sItem.count),i, colors.black, tCol, "left")
  155.             centerT(meItems[a].displayName, i, colors.black, tCol, "left2")
  156.             if not activ[a] then
  157.                 if ((sItem.count < meItems[a].min) and (meItems[a].emit == "low")) or
  158.                  ((sItem.count > meItems[a].min) and (meItems[a].emit == "high")) then
  159.                     cols = cols+colors[meItems[a].color]
  160.                     centerT("activ", i, colors.black, colors[meItems[a].color], "right")
  161.                     activ[a] = true
  162.                 else
  163.                     centerT("inactiv", i, colors.black, colors[meItems[a].color], "right")
  164.                 end
  165.             else
  166.                 if ((sItem.count >= meItems[a].max) and (meItems[a].emit == "low")) or
  167.                    ((sItem.count <= meItems[a].max) and (meItems[a].emit == "high")) then              
  168.                     activ[a] = false
  169.                     centerT("inactiv", i, colors.black, colors[meItems[a].color], "right")
  170.                 else
  171.                     cols = cols+colors[meItems[a].color]
  172.                     centerT("activ", i, colors.black, colors[meItems[a].color], "right")
  173.                 end
  174.             end
  175.             i = i + 1
  176.         end
  177.     end
  178. end
  179.  
  180.  
  181. -- find Item in the ME System
  182. function getItem()
  183.     sItem = "null"
  184.     sItem = me.findItem(meItems[w].name .. "@" .. meItems[w].damage).getMetadata()
  185. end
  186.  
  187. --just get the ingame time
  188. function getTime()
  189.     local time = os.time()
  190.     local formattedTime = textutils.formatTime(time, false)
  191.     return formattedTime
  192. end
  193.  
  194. --let the magic begin
  195. function main()
  196.     print("Starting Emitter at: " .. getTime())
  197.     print("check for Items ... CTRL+T to abort")
  198.     clearMon()
  199.     for v=1, #meItems do activ[v] = meItems[v].initEmit end
  200.     while 1 do
  201.         os.startTimer(1)
  202.         local event = os.pullEventRaw()
  203.         if event == "terminate" then
  204.             cols = 0
  205.             setCable()
  206.             centerT("Stopped@ "..getTime() .. "cable set to " ..cols, 9, colors.red, colors.white, "head")
  207.             print("Emitter stop at: " .. getTime())
  208.             return
  209.         else
  210.             readFile()
  211.             checkMe()
  212.             setCable()
  213.             --sleep(1)
  214.         end
  215.     end
  216. end
  217.  
  218. initPeripherals() --init
  219. monitor.clear() --clear screen(s)
  220. main() -- do the magic
  221. --eof
Advertisement
Add Comment
Please, Sign In to add comment