Compare commits

...

20 Commits

Author SHA1 Message Date
b47a8ad4fa Update shit 2024-02-26 02:58:42 -06:00
a1bb89a4d2 Remove extraneous code 2024-02-26 02:30:48 -06:00
53bfc85164 Improve algo for sorting items away 2024-02-26 02:25:39 -06:00
bcbb419179 Working on a bunch of things 2024-02-26 01:46:12 -06:00
e45b1ea995 Remove extraneous configs 2024-02-26 01:37:38 -06:00
4b4c656af1 Rework the script into something much simpler holy shit 2024-02-26 01:37:03 -06:00
1f593d0542 Add some specs idk it doesn't matter i just found out that a wired net does this a million times better lol 2024-02-26 01:03:04 -06:00
ba51252476 Minor spec revision 2024-02-26 00:41:44 -06:00
5dd175bb00 Minor cleanup 2024-02-26 00:40:16 -06:00
9f2b3a8050 Work on README, add duplicated logic for targeting 2024-02-26 00:30:44 -06:00
c8806be02d Remove sleep from loop func
This solves all our rednet issues, seemingly
2024-02-26 00:24:40 -06:00
bc46c8adef Remove sleeps from loops, as waitformessage will do that for us 2024-02-26 00:22:10 -06:00
78be72c4b1 Move to rednet
This sucks.
It has broadcast, which is required for the project, but if you're not listening when a packet ships then you drop it
2024-02-26 00:20:08 -06:00
a72cd8bef5 Work on init code 2024-02-25 23:33:32 -06:00
164006f4b0 Remove requirement for a GPS 2024-02-25 23:01:57 -06:00
b5ea97b5d9 More boilerplate 2024-02-25 23:01:37 -06:00
75b9634ea9 Minor polish 2024-02-25 22:47:00 -06:00
ff5ff86794 Add a bunch of proto functionality 2024-02-25 22:44:46 -06:00
144b2b9ebb Add initial stuff 2024-02-25 22:22:02 -06:00
7853e6174e Update deploy script 2024-02-25 22:15:30 -06:00
4 changed files with 103 additions and 84 deletions

1
storage-net/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.vscode

View File

@@ -4,78 +4,10 @@ This is an implementation of a centralized storage network architecture that use
## Deployment
A GPS will need to be established and be accurate within range of the entire network to the block.
First, consider using [someone else's project](https://github.com/lewark/inv.lua) instead of this one. It's liable to be a lot better than mine.
Slave nodes must have a modem attached and be within range of the GPS and master.
The master node must have a modem attached and be within range of all slave nodes.
1. Deploy at least one slave node
* The inventory it is to monitor must be above or in front of the unit
* The unit should be able to drop below itself to push items to the master node
## Communication Protocol
Port 42914 is used for Master => Slave broadcasting
Port 42915 is used for Slave => Master return broadcasting
### Packet Format
Packet format is uniform between master <-> slaves and is structured like so:
```
{
// Metadata
magic: str("ccstoragenet")
networkid: int()
sourceid: int()
destid: int()
// Payload
type: str()
// Optional Arguments
itemname: str()
itemquant: int()
destination: int()
location: [int(),int(),int()]
}
```
| Name | Required? | Type | Description
| :-- | :--: | :-- | :--
| `magic` | true | string | Must be `ccstoragenet`. If it is not, the packet is immediately discarded |
| `networkid` | true | int | The ID of the network |
| `sourceid` | true | int | The CC ID of the machine sending the packet |
| `destid` | true | int | The CC ID of the intended recipient of the packet |
| `type` | true | string | An arbitrary string literal corresponding to the type of the request. Common examplse include `ping`, `query`, etc. |
| `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 |
### Slave Node Initialization
* The slave starts up
* The slave receives a `ping` packet from a master server. The slave compares it to any stored `/pairednetwork` file in the root of the filesystem:
* If the file exists and the ID does not match, processing is halted
* The `networkid` of the packet is stored as `/pairednetwork`
* The slave responds with a `pong` packet.
### Master Node Initialization
* The master node starts up
* The master node sends a packet with type `ping` and `networkid` initialized to its CC ID.
* The master node listens for `pong` packets for a configurable timeout.
* The `sourceid` of all `pong` packets is recorded for statistics displays. This cached data is only used for user display. Node availability is evaluated at request time.
### 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
* `itemquantity` is populated with the quantity of that item attached to storages the slave has access to.
* The requester waits for a response from each slave that responded to the `ping` or until a configurable timeout is reached.
* Results are consumed
1. Deploy a Smart Computer
2. Connect the computer to at least 1 chest and 1 hopper via physical modem lines. Chests need to be attached from the bottom and hoppers from their output direction
3. Put a chest on top of the Smart Computer
4. Install Basalt (`wget run https://basalt.madefor.cc/install.lua release latest.lua`)
5. Deploy the script (as `startup`, of course)

90
storage-net/common.lua Normal file
View File

@@ -0,0 +1,90 @@
-- Salt's ComputerCraft Storage Network Script
--
-- For information on this script, physical in-world setup, and configuration,
-- see: https://git.desu.ltd/salt/mc-scripts/src/branch/master/storage-net
-- Startup diagnostics
print("Salt's CC Storage Net")
-- Global scope locals
local output = peripheral.wrap("top") or error("Put a chest on top of this terminal for output items", 0)
-- Helper functions
function getDepositHoppers()
-- Get all connected inventories that push items into the system
return {peripheral.find("inventory", function(name,i)
return string.find(name, "minecraft:hopper")
end)}
end
function getConnectedChests()
-- Get all connected inventories that store items
return {peripheral.find("inventory", function(name,i)
return string.find(name, "minecraft:chest")
end)}
end
-- Common functions
function pushDepositsToChests()
-- Take all the contents of all connected hoppers and stuff them into any other connected inventory
-- These things are wrapped into tables because that's how you take multiple return values and stuff them in a table, apparently
local hoppers = getDepositHoppers()
local chests = getConnectedChests()
-- For each hopper connected to the network...
for k,hopper in ipairs(hoppers) do
-- For each item in that hopper's inventory...
for hslot,hitem in pairs(hopper.list()) do
-- For each connected "chest"...
for k,chest in ipairs(chests) do
-- First, make an attempt to find slots that we can shove the item into
for cslot,citem in pairs(chest.list()) do
if
citem["name"] == hitem["name"] and -- We have the same item
citem["count"] < chest.getItemLimit(cslot) -- There's space in this slot
then
hopper.pushItems(peripheral.getName(chest),hslot,hitem["count"],cslot)
end
end
end
-- We've fallen through trying to fill up existing stacks. Fragmentation is not a concern, put it wherever
if hopper.getItemDetail(hslot) then
for k,chest in ipairs(chests) do
hopper.pushItems(peripheral.getName(chest),hslot)
end
end
end
end
end
function requestItem(name, count)
-- Pull a number of an item into the output chest above the machine
remaining = count
local chests = getConnectedChests()
-- For each chest on the network...
for k,chest in ipairs(chests) do
-- For each slot in that chest...
for cslot,citem in pairs(chest.list()) do
if
citem["name"] == name and
citem["count"] > 0
then
desired = math.max(remaining, citem["count"])
output.pullItems(peripheral.getName(chest),cslot,desired)
remaining = remaining - desired
end
if remaining <= 0 then return true end
end
end
if remaining > 0 then return false end
end
-- Application entrypoint
function main()
requestItem("minecraft:sandstone", 12)
while true do
-- Manage inventory cleanup tasks
pushDepositsToChests() -- Take all deposit terminals and push them into the inventory network
-- Sleep for a tick
sleep(0.05) -- This is to stop CC from killing us
end
end
main()

View File

@@ -10,15 +10,11 @@ mcroot="$HOME/.var/app/org.prismlauncher.PrismLauncher/data/PrismLauncher/instan
world="New World"
masterid="0"
slaveid="1"
_ccroot="$mcroot/.minecraft/saves/$world/computercraft/computer"
ls -al "$_ccroot"
if [ -f master.lua ]; then
cp master.lua "$_ccroot/$masterid/autostart.lua"
fi
if [ -f slave.lua ]; then
cp slave.lua "$_ccroot/$slaveid/autostart.lua"
fi
echo "Root: $_ccroot"
for computer in "$masterid"; do
echo "Deploying to: $computer"
mkdir -p "$_ccroot/$computer"
cp common.lua "$_ccroot/$computer/startup"
done