Compare commits

..

2 Commits

2 changed files with 24 additions and 4 deletions

View File

@ -36,6 +36,9 @@ Packet format is uniform between master <-> slaves and is structured like so:
itemquant: int()
destination: int()
location: [int(),int(),int()]
// Dump for additional body data
body: any
}
```
@ -49,6 +52,7 @@ Packet format is uniform between master <-> slaves and is structured like so:
| `itemname` | false | string | The unlocalized name of an item. Used for querying, crafting, movement, etc. |
| `itemquant` | false | int | Quantity of the aforementioned item |
| `destination` | false | int | The CC ID of the intended recipient of the item |
| `body` | false | any | Any additional body data per the requirements of a query, such as a detailed list of inventory contents |
### Slave Node Initialization
@ -63,8 +67,6 @@ Packet format is uniform between master <-> slaves and is structured like so:
### Inventory Search (query)
* A sender node (master or slave) sends out a `ping`
* All available slaves respond with `pong`
* The sender node sends a packet with type `query`. Field `itemname` is populated with an item to query for.
* Each available slave responds with type `query`.
* `itemname` is populated with data from the previous packet

View File

@ -24,6 +24,7 @@ modem_side = peripheral.getName(modem)
-- Master globals
m_slaves = {}
-- Slave globals
s_chest = peripheral.wrap("top") or nil
-- Common functions
function c_mainLoop(loopfunc)
@ -33,7 +34,7 @@ function c_mainLoop(loopfunc)
end
end
function c_waitForMessage()
-- Waits for a message on the modem, timing out after 1 second
-- Waits for a message on the modem, timing out after some duration
local sender, message = rednet.receive(packet_magic, 1)
if message then
if
@ -74,7 +75,9 @@ function m_loop()
if msg["type"] == "pong" then
local source = msg["sourceid"]
print("Received pong from slave: " .. source)
m_slaves[source] = source
if (not m_slaves[source]) then
m_slaves[source] = {}
end
end
end
function m_ping()
@ -97,9 +100,21 @@ function s_loop()
-- Respond to pings with pongs
print("Received ping from master: " .. msg["sourceid"])
c_sendMessage({type="pong"})
elseif (msg["type"] == "query") then
-- Analyze the attached inventory and see if we have the item
else
print("Unknown message: " .. textutils.serialize(msg))
end
-- If we have any items, we should stow them away. Put them in our chest if
-- we can fit it, otherwise pass it along
for i=1,16 do
turtle.select(i)
while turtle.getItemCount(i) > 0 do
-- We have items in this slot -- push up until we can't anymore
if not turtle.dropUp() then break end
end
if turtle.getItemCount(i) > 0 then turtle.drop() end
end
end
-- Application entrypoint
@ -116,6 +131,9 @@ function main ()
elseif (mode == "slave") then
print("Beginning initialization as slave...")
rednet.open(modem_side)
if not s_chest then
error("No connected inventory. Place one above this node.", 0)
end
print("Entering main loop")
c_mainLoop(s_loop)