Compare commits
2 Commits
5dd175bb00
...
1f593d0542
Author | SHA1 | Date | |
---|---|---|---|
1f593d0542 | |||
ba51252476 |
@ -36,6 +36,9 @@ Packet format is uniform between master <-> slaves and is structured like so:
|
|||||||
itemquant: int()
|
itemquant: int()
|
||||||
destination: int()
|
destination: int()
|
||||||
location: [int(),int(),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. |
|
| `itemname` | false | string | The unlocalized name of an item. Used for querying, crafting, movement, etc. |
|
||||||
| `itemquant` | false | int | Quantity of the aforementioned item |
|
| `itemquant` | false | int | Quantity of the aforementioned item |
|
||||||
| `destination` | false | int | The CC ID of the intended recipient of the 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
|
### Slave Node Initialization
|
||||||
|
|
||||||
@ -63,8 +67,6 @@ Packet format is uniform between master <-> slaves and is structured like so:
|
|||||||
|
|
||||||
### Inventory Search (query)
|
### 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.
|
* 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`.
|
* Each available slave responds with type `query`.
|
||||||
* `itemname` is populated with data from the previous packet
|
* `itemname` is populated with data from the previous packet
|
||||||
|
@ -24,6 +24,7 @@ modem_side = peripheral.getName(modem)
|
|||||||
-- Master globals
|
-- Master globals
|
||||||
m_slaves = {}
|
m_slaves = {}
|
||||||
-- Slave globals
|
-- Slave globals
|
||||||
|
s_chest = peripheral.wrap("top") or nil
|
||||||
|
|
||||||
-- Common functions
|
-- Common functions
|
||||||
function c_mainLoop(loopfunc)
|
function c_mainLoop(loopfunc)
|
||||||
@ -33,7 +34,7 @@ function c_mainLoop(loopfunc)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
function c_waitForMessage()
|
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)
|
local sender, message = rednet.receive(packet_magic, 1)
|
||||||
if message then
|
if message then
|
||||||
if
|
if
|
||||||
@ -74,7 +75,9 @@ function m_loop()
|
|||||||
if msg["type"] == "pong" then
|
if msg["type"] == "pong" then
|
||||||
local source = msg["sourceid"]
|
local source = msg["sourceid"]
|
||||||
print("Received pong from slave: " .. source)
|
print("Received pong from slave: " .. source)
|
||||||
m_slaves[source] = source
|
if (not m_slaves[source]) then
|
||||||
|
m_slaves[source] = {}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
function m_ping()
|
function m_ping()
|
||||||
@ -97,9 +100,21 @@ function s_loop()
|
|||||||
-- Respond to pings with pongs
|
-- Respond to pings with pongs
|
||||||
print("Received ping from master: " .. msg["sourceid"])
|
print("Received ping from master: " .. msg["sourceid"])
|
||||||
c_sendMessage({type="pong"})
|
c_sendMessage({type="pong"})
|
||||||
|
elseif (msg["type"] == "query") then
|
||||||
|
-- Analyze the attached inventory and see if we have the item
|
||||||
else
|
else
|
||||||
print("Unknown message: " .. textutils.serialize(msg))
|
print("Unknown message: " .. textutils.serialize(msg))
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
-- Application entrypoint
|
-- Application entrypoint
|
||||||
@ -116,6 +131,9 @@ function main ()
|
|||||||
elseif (mode == "slave") then
|
elseif (mode == "slave") then
|
||||||
print("Beginning initialization as slave...")
|
print("Beginning initialization as slave...")
|
||||||
rednet.open(modem_side)
|
rednet.open(modem_side)
|
||||||
|
if not s_chest then
|
||||||
|
error("No connected inventory. Place one above this node.", 0)
|
||||||
|
end
|
||||||
|
|
||||||
print("Entering main loop")
|
print("Entering main loop")
|
||||||
c_mainLoop(s_loop)
|
c_mainLoop(s_loop)
|
||||||
|
Loading…
Reference in New Issue
Block a user