Add multi-dimension support

This commit is contained in:
Jack O'Sullivan 2024-01-17 01:28:48 +00:00
parent 2a53a3c4c6
commit fdd00e3fb6
4 changed files with 117 additions and 16 deletions

24
dimServ.lua Normal file
View File

@ -0,0 +1,24 @@
local CHANNEL = 666
settings.define("dimension", {
description = "Dimension to serve",
default = "overworld",
type = "string",
})
local dim = settings.get("dimension")
local modem = peripheral.find("modem")
assert(modem)
modem.open(CHANNEL)
print("Serving requests for dimension '" .. dim .. "'")
while true do
local event, side, channel, replyChannel, reply, distance = os.pullEvent("modem_message")
assert(channel == CHANNEL)
-- AKA we are in the same dimension
if distance then
print("Responding to request from computer #" .. replyChannel - CHANNEL)
modem.transmit(replyChannel, channel, dim)
end
end

View File

@ -325,6 +325,7 @@ function Miner.new()
self:equip("computercraft:wireless_modem_advanced", "right")
rednet.open("right")
self:findDimension()
self.pos = vector.new(0, 0, 0)
self:doGPS(true)
@ -351,6 +352,7 @@ function Miner:sendMessage(type, msg)
rednet.open("right")
msg["type"] = type
msg["name"] = self.name
msg["dimension"] = self.dimension
msg["pos"] = self.absolutePos
rednet.broadcast(msg, PROTOCOL)
end
@ -444,6 +446,23 @@ function Miner:safeMove(dir)
end
end
function Miner:findDimension()
self:equip("computercraft:wireless_modem_advanced", "right")
local m = peripheral.wrap("right")
local replyChannel = 666 + os.computerID()
m.open(replyChannel)
m.transmit(666, replyChannel)
local event, side, channel, _, reply, distance = os.pullEvent("modem_message")
assert(distance)
assert(channel == replyChannel)
self.dimension = reply
m.close(replyChannel)
print("Looks like my dimension is " .. self.dimension)
end
function Miner:doGPS(orientation)
local x, y, z = gps.locate()
assert(x)

View File

@ -1,5 +1,12 @@
local PROTOCOL = "automine"
local TARGET = "devplayer0"
local DIM_MAP = {
overworld = "Overworld",
the_nether = "Nether",
the_end = "End",
}
local ICON_BASE = "https://p.nul.ie/cc/icon/"
local ORE_ICONS = {
["minecraft:coal_ore"] = "coal",
@ -91,6 +98,29 @@ function MineMonitor.new(chat)
return self
end
function MineMonitor:prepareMap(msg)
local map = DIM_MAP[msg.dimension]
assert(map)
assert(self.cart.setCurrentMap(map))
local id = markerSet(msg)
local sets = self.cart.getMarkerSets()
assert(sets)
local found = false
for _, s in ipairs(sets) do
if s == id then
found = true
break
end
end
if not found then
assert(self.cart.addMarkerSet(id, "AutoMiner " .. msg.name))
end
return id
end
function MineMonitor:setPosMarker(msg)
local id = markerSet(msg)
local mId = id .. "_pos"
@ -106,6 +136,8 @@ function MineMonitor:handleStart(msg)
{text = msg.name, color = "green"},
{text = " @ ", color = "white"},
{text = vecToStr(msg.pos), color = "aqua"},
{text = " in ", color = "white"},
{text = msg.dimension, color = "aqua"},
{text = " found ", color = "white"},
{text = tostring(msg.oreCount), color = "red"},
{text = " ores (", color = "white"},
@ -115,10 +147,8 @@ function MineMonitor:handleStart(msg)
{text = "!", color = "white"},
})
local id = markerSet(msg)
self.cart.addMarkerSet(id, "AutoMiner " .. msg.name)
local id = self:prepareMap(msg)
self.cart.clearMarkerSet(id)
self:setPosMarker(msg)
local veinPoints = {}
@ -143,6 +173,8 @@ function MineMonitor:handleVein(msg)
{text = msg.name, color = "green"},
{text = " @ ", color = "white"},
{text = vecToStr(msg.pos), color = "aqua"},
{text = " in ", color = "white"},
{text = msg.dimension, color = "aqua"},
{text = " is mining ", color = "white"},
{text = tostring(msg.oreCount), color = "red"},
{text = " ", color = "white"},
@ -154,7 +186,7 @@ function MineMonitor:handleVein(msg)
{text = ")!", color = "white"},
}, TARGET)
local id = markerSet(msg)
local id = self:prepareMap(msg)
self:setPosMarker(msg)
if msg.i ~= 1 then
self.cart.removeMarker(id, id .. "_vein_" .. (msg.i - 1))
@ -166,13 +198,14 @@ function MineMonitor:handleRecall(msg)
{text = msg.name, color = "green"},
{text = " @ ", color = "white"},
{text = vecToStr(msg.pos), color = "aqua"},
{text = " in ", color = "white"},
{text = msg.dimension, color = "aqua"},
{text = " is recalling to ", color = "white"},
{text = vecToStr(msg.to), color = "green"},
{text = "!", color = "white"},
})
local id = markerSet(msg)
self.cart.addMarkerSet(id, "AutoMiner " .. msg.name)
local id = self:prepareMap(msg)
self.cart.clearMarkerSet(id)
self:setPosMarker({

View File

@ -7,23 +7,48 @@ function Set(list)
return set
end
function awaitMessage(types, target)
MineRemote = {}
function MineRemote.new(target)
local self = setmetatable({}, { __index = MineRemote })
self.target = target
self:loadDimension()
rednet.open("back")
return self
end
function MineRemote:loadDimension()
local m = peripheral.wrap("back")
local replyChannel = 666 + os.computerID()
m.open(replyChannel)
m.transmit(666, replyChannel)
local event, side, channel, _, reply, distance = os.pullEvent("modem_message")
assert(distance)
assert(channel == replyChannel)
m.close(replyChannel)
self.dimension = reply
end
function MineRemote:awaitMessage(types)
while true do
local targetId, msg = rednet.receive(PROTOCOL)
if types[msg.type] and msg.name == target then
if types[msg.type] and msg.name == self.target and msg.dimension == self.dimension then
return targetId, msg
end
end
end
function run(target)
function MineRemote:run()
local x, y, z = gps.locate()
assert(x, "Couldn't run GPS")
local to = vector.new(math.floor(x), math.floor(y), math.floor(z))
print(target .. " will recall to " .. to:tostring())
print(self.target .. " will recall to " .. to:tostring())
print("Waiting for " .. target .. "...")
local targetId, msg = awaitMessage(Set{"iterationStart", "veinStart"}, target)
print("Waiting for " .. self.target .. "...")
local targetId, msg = self:awaitMessage(Set{"iterationStart", "veinStart"}, self.target)
assert(targetId)
print("Sending recall request")
@ -32,14 +57,14 @@ function run(target)
to = to,
}, PROTOCOL_CTRL)
local _, msg = awaitMessage(Set{"ackRecall"}, target)
local _, msg = self:awaitMessage(Set{"ackRecall"}, self.target)
assert(msg)
print("Received confirmation of recall from " .. target )
print("Received confirmation of recall from " .. self.target )
end
if arg[1] then
rednet.open("back")
run(arg[1])
local r = MineRemote.new(arg[1])
r:run()
else
print("usage: " .. arg[0] .. " <target>")
end