From fdd00e3fb6a0494b0a8558c0af3d5724ba8bb701 Mon Sep 17 00:00:00 2001 From: Jack O'Sullivan Date: Wed, 17 Jan 2024 01:28:48 +0000 Subject: [PATCH] Add multi-dimension support --- dimServ.lua | 24 ++++++++++++++++++++++++ mine.lua | 19 +++++++++++++++++++ mineMonitor.lua | 45 +++++++++++++++++++++++++++++++++++++++------ mineRemote.lua | 45 +++++++++++++++++++++++++++++++++++---------- 4 files changed, 117 insertions(+), 16 deletions(-) create mode 100644 dimServ.lua diff --git a/dimServ.lua b/dimServ.lua new file mode 100644 index 0000000..d611eb5 --- /dev/null +++ b/dimServ.lua @@ -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 diff --git a/mine.lua b/mine.lua index 427fe06..5c2ffee 100644 --- a/mine.lua +++ b/mine.lua @@ -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) diff --git a/mineMonitor.lua b/mineMonitor.lua index fbab231..9931bc0 100644 --- a/mineMonitor.lua +++ b/mineMonitor.lua @@ -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({ diff --git a/mineRemote.lua b/mineRemote.lua index 7e524b8..08ae340 100644 --- a/mineRemote.lua +++ b/mineRemote.lua @@ -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] .. " ") end