Add .lua extensions, reorder file structure
This commit is contained in:
163
maps/remoteMap/remoteMap_server.lua
Normal file
163
maps/remoteMap/remoteMap_server.lua
Normal file
@@ -0,0 +1,163 @@
|
||||
local args = {...}
|
||||
|
||||
--===== OPEN REDNET =====--
|
||||
for _, side in ipairs(redstone.getSides()) do
|
||||
if peripheral.getType(side) == "modem" then
|
||||
rednet.open(side)
|
||||
end
|
||||
end
|
||||
|
||||
if not rednet.isOpen() then
|
||||
printError("could not open rednet")
|
||||
return
|
||||
end
|
||||
|
||||
--===== LOAD MAP =====--
|
||||
if not compactMap then
|
||||
if not os.loadAPI("compactMap") then
|
||||
error("could not load API: compactMap")
|
||||
end
|
||||
end
|
||||
local map = compactMap.new(args[1])
|
||||
|
||||
--===== SET REDNET PROTOCOL =====--
|
||||
local MAP_SHARE_PROTOCOL
|
||||
if args[2] and type(args[2]) == "string" then
|
||||
MAP_SHARE_PROTOCOL = "map_share:"..args[2]
|
||||
else
|
||||
MAP_SHARE_PROTOCOL = "map_share:"..fs.getName(args[1])
|
||||
end
|
||||
|
||||
--===== HOST AS SERVER =====--
|
||||
do
|
||||
local host = rednet.lookup(MAP_SHARE_PROTOCOL, "SERVER")
|
||||
if host and host ~= os.computerID() then
|
||||
printError("server already running for this map share")
|
||||
return
|
||||
end
|
||||
end
|
||||
rednet.host(MAP_SHARE_PROTOCOL, "SERVER")
|
||||
|
||||
--===== UTILS =====--
|
||||
local MESSAGE_TYPE = {
|
||||
GET = 0,
|
||||
SET = 1,
|
||||
}
|
||||
local receivedMessages = {}
|
||||
local receivedMessageTimeouts = {}
|
||||
|
||||
local function isValidCoord(coord)
|
||||
return type(coord) == "number" and coord % 1 == 0 and coord >= 0 and coord <= 15
|
||||
end
|
||||
|
||||
local function updateCoord(x, y, z, value)
|
||||
local coord = vector.new(x, y, z)
|
||||
local currValue = map:get(coord)
|
||||
if value == 1 then
|
||||
if currValue then
|
||||
map:set(coord, math.min(7, currValue + 1))
|
||||
else
|
||||
map:set(coord, 0)
|
||||
end
|
||||
elseif value == -1 then
|
||||
if currValue then
|
||||
if currValue == 0 then
|
||||
map:set(coord, nil)
|
||||
else
|
||||
map:set(coord, currValue - 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function updateMap(newData, gX, gY, gZ)
|
||||
if type(newData) == "table" then
|
||||
local currX, currY
|
||||
for x, gridYZ in pairs(newData) do
|
||||
if isValidCoord(x) and type(gridYZ) == "table" then
|
||||
currX = gX*16 + x
|
||||
for y, gridZ in pairs(gridYZ) do
|
||||
if isValidCoord(y) and type(gridZ) == "table" then
|
||||
currY = gY*16 + y
|
||||
for z, value in pairs(gridZ) do
|
||||
if isValidCoord(z) then
|
||||
updateCoord(currX, currY, gZ*16 + z, value)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
map:save(gX, gY, gZ)
|
||||
end
|
||||
end
|
||||
|
||||
local function checkGridCoordFormat(gridCoord)
|
||||
if type(gridCoord) == "table" and #gridCoord == 3 then
|
||||
for i = 1, 3 do
|
||||
local coord = gridCoord[i]
|
||||
if type(coord) ~= "number" or coord % 1 ~= 0 then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
local function newMessage(messageType, messageID, grid, data)
|
||||
return {
|
||||
type = messageType,
|
||||
ID = messageID,
|
||||
grid = grid,
|
||||
data = data,
|
||||
}
|
||||
end
|
||||
|
||||
--===== REPEATED MESSAGE HANDLING =====--
|
||||
local function clearOldMessages()
|
||||
while true do
|
||||
local event, timer = os.pullEvent("timer")
|
||||
local messageID = receivedMessageTimeouts[timer]
|
||||
if messageID then
|
||||
receivedMessageTimeouts[timer] = nil
|
||||
receivedMessages[messageID] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--===== MAIN =====--
|
||||
local function main()
|
||||
while true do
|
||||
local senderID, message = rednet.receive(MAP_SHARE_PROTOCOL)
|
||||
if type(message) == "table" and checkGridCoordFormat(message.grid) then
|
||||
if message.type == MESSAGE_TYPE.GET then
|
||||
local gridData = map:getGrid(unpack(message.grid))
|
||||
local replyMessage = newMessage(MESSAGE_TYPE.GET, message.ID, message.grid, gridData)
|
||||
rednet.send(senderID, replyMessage, MAP_SHARE_PROTOCOL)
|
||||
elseif message.type == MESSAGE_TYPE.SET then
|
||||
if not receivedMessages[message.ID] then
|
||||
updateMap(message.data, unpack(message.grid))
|
||||
receivedMessages[message.ID] = true
|
||||
receivedMessageTimeouts[os.startTimer(15)] = message.ID
|
||||
end
|
||||
local replyMessage = newMessage(MESSAGE_TYPE.SET, message.ID, message.grid, true)
|
||||
rednet.send(senderID, replyMessage, MAP_SHARE_PROTOCOL)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--===== USER INTERFACE =====--
|
||||
local function control()
|
||||
while true do
|
||||
local event, key = os.pullEvent("key")
|
||||
if key == keys.backspace then
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
parallel.waitForAny(main, clearOldMessages, control)
|
||||
|
||||
rednet.unhost(MAP_SHARE_PROTOCOL)
|
||||
170
maps/remoteMap/remoteMap_viewer.lua
Normal file
170
maps/remoteMap/remoteMap_viewer.lua
Normal file
@@ -0,0 +1,170 @@
|
||||
local tArgs = {...}
|
||||
|
||||
local function printUsage()
|
||||
print("Usage: viewRemoteMap <(string) mapName> <(int) x-coord> <(int) y-coord> <(int) z-coord>")
|
||||
end
|
||||
|
||||
if not remoteMap then
|
||||
if not os.loadAPI("remoteMap") then
|
||||
error("Could not load remoteMap API")
|
||||
end
|
||||
end
|
||||
|
||||
for _, side in ipairs(redstone.getSides()) do
|
||||
if peripheral.getType(side) == "modem" then
|
||||
rednet.open(side)
|
||||
end
|
||||
end
|
||||
|
||||
if not rednet.isOpen() then
|
||||
error("could not open rednet")
|
||||
end
|
||||
|
||||
if type(tArgs[1]) ~= "string" then
|
||||
printError("string expected for map name")
|
||||
printUsage()
|
||||
return
|
||||
end
|
||||
local map = remoteMap.new(tArgs[1], 5)
|
||||
|
||||
local currX, currY, currZ
|
||||
if #tArgs == 4 then
|
||||
for i = 2, 4 do
|
||||
local num = tArgs[i]
|
||||
if not tonumber(num) or num % 1 ~= 0 then
|
||||
printError("argument "..tostring(i).." is not a number")
|
||||
printUsage()
|
||||
return
|
||||
end
|
||||
end
|
||||
currX = tArgs[2]
|
||||
currY = tArgs[3]
|
||||
currZ = tArgs[4]
|
||||
else
|
||||
currX, currY, currZ = gps.locate(1)
|
||||
if tonumber(currX) then
|
||||
currX = math.floor(tonumber(currX))
|
||||
end
|
||||
if tonumber(currY) then
|
||||
currY = math.floor(tonumber(currY))
|
||||
end
|
||||
if tonumber(currZ) then
|
||||
currZ = math.floor(tonumber(currZ))
|
||||
end
|
||||
end
|
||||
if not (currX and currY and currZ) then
|
||||
printError("could not identify start coords")
|
||||
printUsage()
|
||||
return
|
||||
end
|
||||
|
||||
term.setCursorBlink(false)
|
||||
term.setTextColour(colours.white)
|
||||
term.setBackgroundColour(colours.black)
|
||||
term.clear()
|
||||
|
||||
local width, height = term.getSize()
|
||||
local currW, currH = 1, 1
|
||||
|
||||
local mainWindow = window.create(term.current(), 1, 1, width, math.max(0, height - 1), false)
|
||||
mainWindow.setTextColour(colours.red)
|
||||
|
||||
local toolbarWindow = window.create(term.current(), 1, height, width, 1, false)
|
||||
toolbarWindow.setBackgroundColour(colours.grey)
|
||||
toolbarWindow.setTextColour(colours.white)
|
||||
toolbarWindow.clear()
|
||||
|
||||
local function redrawMainWindow()
|
||||
mainWindow.setVisible(false)
|
||||
|
||||
mainWindow.setBackgroundColour(colours.black)
|
||||
mainWindow.clear()
|
||||
mainWindow.setBackgroundColour(colours.white)
|
||||
|
||||
local w, h = mainWindow.getSize()
|
||||
for x = 1, w do
|
||||
for z = 1, h do
|
||||
local value = map:get(vector.new(currX + x - 1, currY, currZ + z - 1))
|
||||
if value then
|
||||
mainWindow.setCursorPos(x, z)
|
||||
mainWindow.write(string.sub(value, 1, 1))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local cursorValue = map:get(vector.new(currX + currW - 1, currY, currZ + currH - 1))
|
||||
mainWindow.setBackgroundColour(colours.green)
|
||||
mainWindow.setCursorPos(currW, currH)
|
||||
if cursorValue then
|
||||
mainWindow.write(string.sub(cursorValue, 1, 1))
|
||||
else
|
||||
mainWindow.write(" ")
|
||||
end
|
||||
|
||||
mainWindow.setVisible(true)
|
||||
end
|
||||
|
||||
local function redrawToolbarWindow()
|
||||
toolbarWindow.setVisible(false)
|
||||
|
||||
toolbarWindow.setCursorPos(1, 1)
|
||||
toolbarWindow.clearLine()
|
||||
toolbarWindow.write(tostring(currX + currW - 1)..","..tostring(currY)..","..tostring(currZ + currH - 1))
|
||||
toolbarWindow.write(" -- ")
|
||||
toolbarWindow.write(tostring(math.floor( (currX + currW - 1)/16 )))
|
||||
toolbarWindow.write(",")
|
||||
toolbarWindow.write(tostring(math.floor( (currY)/16 )))
|
||||
toolbarWindow.write(",")
|
||||
toolbarWindow.write(tostring(math.floor( (currZ + currH - 1)/16 )))
|
||||
|
||||
toolbarWindow.setVisible(true)
|
||||
end
|
||||
|
||||
local cont = true
|
||||
local redraw = true
|
||||
while cont do
|
||||
if redraw then
|
||||
map:check()
|
||||
redrawToolbarWindow()
|
||||
redrawMainWindow()
|
||||
redraw = false
|
||||
end
|
||||
local event = {os.pullEvent()}
|
||||
if event[1] == "key" then
|
||||
local key = event[2]
|
||||
if key == keys.up then
|
||||
currZ = currZ - 1
|
||||
currH = math.min(height - 1, currH + 1)
|
||||
elseif key == keys.down then
|
||||
currZ = currZ + 1
|
||||
currH = math.max(1, currH - 1)
|
||||
elseif key == keys.left then
|
||||
currX = currX - 1
|
||||
currW = math.min(width, currW + 1)
|
||||
elseif key == keys.right then
|
||||
currX = currX + 1
|
||||
currW = math.max(1, currW - 1)
|
||||
elseif key == keys.numPadAdd then
|
||||
currY = currY + 1
|
||||
elseif key == keys.numPadSubtract then
|
||||
currY = currY - 1
|
||||
elseif key == keys.backspace then
|
||||
cont = false
|
||||
end
|
||||
redraw = true
|
||||
elseif event[1] == "mouse_click" then
|
||||
if event[4] < height then
|
||||
currW, currH = event[3], event[4]
|
||||
redraw = true
|
||||
end
|
||||
elseif event[1] == "term_resize" then
|
||||
width, height = term.getSize()
|
||||
mainWindow.reposition(1, 1, width, math.max(0, height - 1))
|
||||
toolbarWindow.reposition(1, height, width, height)
|
||||
redraw = true
|
||||
end
|
||||
end
|
||||
|
||||
term.setBackgroundColour(colours.black)
|
||||
term.setCursorPos(1, 1)
|
||||
term.clear()
|
||||
Reference in New Issue
Block a user