Advertisement
ktdm

ENDER_NETWORK

Jun 10th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.56 KB | None | 0 0
  1. local component = require("component")
  2. local str       = require("string")
  3. local sides     = require("sides")
  4. local math      = require("math")
  5. local table     = require("table")
  6.  
  7. local transposer = component.transposer
  8. local ender_chest = component.ender_chest
  9.  
  10. local DATA = sides.up
  11.  
  12.  
  13.  
  14. local bufferStart = 1
  15. local bufferEnd = 27
  16.  
  17. local messageIdSlot = 28
  18. local chestBlockerSlot = 29
  19. local msgCodeSlot = 30
  20. local dataTypeSlot = 31
  21. local dataStart = 32
  22. local dataEnd = 54
  23. local dataSize = dataEnd - dataStart + 1
  24.  
  25. local lastMessage = -1
  26.  
  27.  
  28.  
  29. local function moveItem(from, to, count) transposer.transferItem(DATA, DATA, count, from, to) end
  30.  
  31. local function clearData()
  32.     ender_chest.condenseItems()
  33. end
  34. local function readData(slot, data)
  35.     if data ~= nil then
  36.         local val = data[slot]
  37.         if val == nil then return 0 end
  38.         return val.basic().qty
  39.     end
  40.     return transposer.getSlotStackSize(DATA, slot)
  41. end
  42.  
  43. local function writeToSlot(slot, val) moveItem(slot - bufferEnd, slot, val) end
  44.  
  45. local function unblockChest() moveItem(chestBlockerSlot, 2, 64) end
  46. local function blockChest() writeToSlot(chestBlockerSlot, 1) end
  47. local function chestBlocked() return readData(chestBlockerSlot) > 0 end
  48.  
  49. local function incMessageId(counter)
  50.     if counter == 63 then counter = 0 end
  51.     writeToSlot(messageIdSlot, counter + 1)
  52.     return counter + 1
  53. end
  54.  
  55. local function convertNumberToMessage(number, base)
  56.     local msg, ch = {}, false
  57.     for i = 23, 1, -1 do
  58.         local k = base ^ i
  59.         if k <= number then do
  60.             ch = true
  61.             local n = math.floor(number / k)
  62.             number = number - n * k
  63.             table.insert(msg, n)
  64.         end
  65.         elseif ch then table.insert(msg, 0) end
  66.     end
  67.     table.insert(msg, number)
  68.     for i = 1, #msg do msg[i] = msg[i] + 1 end
  69.     return msg
  70. end
  71. local function convertMessageToNumber(m, base)
  72.     local n = 0
  73.     for i = 1, #m do m[i] = m[i] - 1 end
  74.     for i = 1, #m do n = n + base ^ (#m - i) * m[i] end
  75.     return n
  76. end
  77. local function writeData(data, msgCode)
  78.     writeToSlot(msgCodeSlot, msgCode)
  79.     if type(data) == "number" then
  80.         data = convertNumberToMessage(data, 64)
  81.         writeToSlot(dataTypeSlot, 1)
  82.     end
  83.     for i = 1, math.min(dataSize, #data) do writeToSlot(i + dataStart - 1, data[i]) end
  84. end
  85.  
  86. local function readMessage()
  87.     local data, message, num = ender_chest.getAllStacks(), {}
  88.  
  89.     local msgCode = readData(msgCodeSlot, data)
  90.     local dataType = readData(dataTypeSlot, data)
  91.     for i = dataStart, math.min(#data, dataEnd) do
  92.         num = readData(i, data)
  93.         message[i - dataStart + 1] = num
  94.     end
  95.     if dataType == 1 then message = convertMessageToNumber(message, 64) end
  96.     return msgCode, message
  97. end
  98.  
  99.  
  100. local function sendMessage(msgCode, data, timeout)
  101.     if timeout == nil then timeout = 0.75 end
  102.     while true do
  103.         if not chestBlocked() then
  104.             local lastMessageId = readData(messageIdSlot)
  105.             clearData()
  106.             blockChest()
  107.             writeData(data, msgCode)
  108.             lastMessage = incMessageId(lastMessageId)
  109.             os.sleep(timeout)
  110.             unblockChest()
  111.             break
  112.         end
  113.     end
  114. end
  115.  
  116. local function pullMessage()    
  117.     local messageId = readData(messageIdSlot)
  118.    
  119.     if lastMessage ~= messageId and messageId > 0 then
  120.         lastMessage = messageId
  121.         return true, readMessage()
  122.     end
  123.     return false
  124. end
  125.  
  126. return {["sendMessage"]=sendMessage, ["pullMessage"]=pullMessage, ["maxMessageSize"]=dataSize}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement