mc-scripts/storage-net/common.lua

67 lines
2.1 KiB
Lua
Raw Normal View History

2024-02-25 22:44:46 -06:00
-- 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
-- Default configuration values. Override in config.lua. DO NOT CHANGE HERE
2024-02-25 22:47:00 -06:00
mode = "ConfigureMe" -- The mode of function for this node
2024-02-25 22:44:46 -06:00
networkid = 0 -- Unique ID for this network
port_broadcast = 42914 -- Port for M->S traffic
port_return = 42915 -- Port for S->M traffic
-- This loads config.lua. See masterconfig.lua and slaveconfig.lua for example
-- configurations.
2024-02-25 22:22:02 -06:00
require "config"
2024-02-25 22:44:46 -06:00
-- Startup diagnostics
print("Salt's CC Storage Net")
print("Computer ID: " .. os.getComputerID())
print("Computer " .. os.getComputerID() .. " running as " .. mode)
2024-02-25 22:47:00 -06:00
-- Common globals
2024-02-25 23:01:37 -06:00
modem = peripheral.find("modem") or error("No modem attached", 0)
2024-02-25 22:47:00 -06:00
-- Master globals
-- Slave globals
2024-02-25 22:44:46 -06:00
-- Common functions
function c_mainLoop(loopfunc)
-- Loops a thing forever
while true do
loopfunc()
2024-02-25 22:47:00 -06:00
sleep(0.10)
2024-02-25 22:44:46 -06:00
end
end
-- Master functions
function m_loop()
-- The main loop of the master server
2024-02-25 23:01:37 -06:00
-- Listen for packets on the return net. This has a timeout for events and stuff
local timeout = os.startTimer(1)
local event, side, channel, replyChannel, message, distance = os.pullEvent()
if (event == "modem_message") then
print("Message received: " .. message)
end
2024-02-25 22:47:00 -06:00
sleep(1)
2024-02-25 22:44:46 -06:00
end
-- Slave functions
function s_loop()
-- The main loop of any slave nodes
2024-02-25 23:01:37 -06:00
-- Listen for packets from the master
local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
2024-02-25 22:47:00 -06:00
sleep(1)
2024-02-25 22:44:46 -06:00
end
-- Application entrypoint
function main ()
if (mode == "master") then
2024-02-25 23:01:37 -06:00
modem.open(port_return)
2024-02-25 22:44:46 -06:00
c_mainLoop(m_loop)
elseif (mode == "slave") then
2024-02-25 23:01:37 -06:00
modem.open(port_broadcast)
2024-02-25 22:44:46 -06:00
c_mainLoop(s_loop)
else
2024-02-25 23:01:37 -06:00
error("Invalid mode: " .. mode .. ", please configure this node appropriately", 0)
2024-02-25 22:44:46 -06:00
end
2024-02-25 23:01:37 -06:00
modem.closeAll()
2024-02-25 22:44:46 -06:00
end
main()