Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local str = require("string")
- local sides = require("sides")
- local math = require("math")
- local table = require("table")
- local transposer = component.transposer
- local ender_chest = component.ender_chest
- local DATA = sides.up
- local bufferStart = 1
- local bufferEnd = 27
- local messageIdSlot = 28
- local chestBlockerSlot = 29
- local msgCodeSlot = 30
- local dataTypeSlot = 31
- local dataStart = 32
- local dataEnd = 54
- local dataSize = dataEnd - dataStart + 1
- local lastMessage = -1
- local function moveItem(from, to, count) transposer.transferItem(DATA, DATA, count, from, to) end
- local function clearData()
- ender_chest.condenseItems()
- end
- local function readData(slot, data)
- if data ~= nil then
- local val = data[slot]
- if val == nil then return 0 end
- return val.basic().qty
- end
- return transposer.getSlotStackSize(DATA, slot)
- end
- local function writeToSlot(slot, val) moveItem(slot - bufferEnd, slot, val) end
- local function unblockChest() moveItem(chestBlockerSlot, 2, 64) end
- local function blockChest() writeToSlot(chestBlockerSlot, 1) end
- local function chestBlocked() return readData(chestBlockerSlot) > 0 end
- local function incMessageId(counter)
- if counter == 63 then counter = 0 end
- writeToSlot(messageIdSlot, counter + 1)
- return counter + 1
- end
- local function convertNumberToMessage(number, base)
- local msg, ch = {}, false
- for i = 23, 1, -1 do
- local k = base ^ i
- if k <= number then do
- ch = true
- local n = math.floor(number / k)
- number = number - n * k
- table.insert(msg, n)
- end
- elseif ch then table.insert(msg, 0) end
- end
- table.insert(msg, number)
- for i = 1, #msg do msg[i] = msg[i] + 1 end
- return msg
- end
- local function convertMessageToNumber(m, base)
- local n = 0
- for i = 1, #m do m[i] = m[i] - 1 end
- for i = 1, #m do n = n + base ^ (#m - i) * m[i] end
- return n
- end
- local function writeData(data, msgCode)
- writeToSlot(msgCodeSlot, msgCode)
- if type(data) == "number" then
- data = convertNumberToMessage(data, 64)
- writeToSlot(dataTypeSlot, 1)
- end
- for i = 1, math.min(dataSize, #data) do writeToSlot(i + dataStart - 1, data[i]) end
- end
- local function readMessage()
- local data, message, num = ender_chest.getAllStacks(), {}
- local msgCode = readData(msgCodeSlot, data)
- local dataType = readData(dataTypeSlot, data)
- for i = dataStart, math.min(#data, dataEnd) do
- num = readData(i, data)
- message[i - dataStart + 1] = num
- end
- if dataType == 1 then message = convertMessageToNumber(message, 64) end
- return msgCode, message
- end
- local function sendMessage(msgCode, data, timeout)
- if timeout == nil then timeout = 0.75 end
- while true do
- if not chestBlocked() then
- local lastMessageId = readData(messageIdSlot)
- clearData()
- blockChest()
- writeData(data, msgCode)
- lastMessage = incMessageId(lastMessageId)
- os.sleep(timeout)
- unblockChest()
- break
- end
- end
- end
- local function pullMessage()
- local messageId = readData(messageIdSlot)
- if lastMessage ~= messageId and messageId > 0 then
- lastMessage = messageId
- return true, readMessage()
- end
- return false
- end
- return {["sendMessage"]=sendMessage, ["pullMessage"]=pullMessage, ["maxMessageSize"]=dataSize}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement