gps server record positions

This commit is contained in:
kepler155c@gmail.com
2019-01-05 00:40:43 -05:00
parent b05e614130
commit 2539fce815
2 changed files with 34 additions and 4 deletions

View File

@@ -25,7 +25,6 @@ local GRID = {
}
local HOME_PT = { x = 0, y = 0, z = 0, heading = 0 }
local HIGH_PT = { x = 0, y = 8, z = 0, heading = 0 }
local DIG_BLACKLIST = {
[ 'minecraft:furnace' ] = true,
@@ -457,6 +456,7 @@ local function countSaplings()
end
local function randomSapling()
countSaplings()
local sapling = ALL_SAPLINGS[math.random(1, #ALL_SAPLINGS)]
if sapling.count > 0 then

View File

@@ -1,4 +1,5 @@
local Config = require('config')
local GPS = require('gps')
local Util = require('util')
local args = { ... }
@@ -6,11 +7,14 @@ local gps = _G.gps
local os = _G.os
local peripheral = _G.peripheral
local turtle = _G.turtle
local vector = _G.vector
local WIRED_MODEM = 'computercraft:wired_modem_full'
local CABLE = 'computercraft:cable'
local ENDER_MODEM = 'computercraft:advanced_modem'
local positions = { }
local function build()
if not turtle.has(WIRED_MODEM, 8) or
not turtle.has(CABLE, 4) or
@@ -48,8 +52,18 @@ local function build()
end
end
local function memoize(t, k, fn)
local e = t[k]
if not e then
e = fn()
t[k] = e
end
return e
end
local function server()
local modems = Config.load('gpsServer')
local computers = { }
for k, modem in pairs(modems) do
Util.merge(modem, peripheral.wrap(k))
@@ -60,11 +74,27 @@ local function server()
local e, p1, p2, p3, p4, p5 = os.pullEvent( "modem_message" )
if e == "modem_message" then
-- We received a message from a modem
local sSide, sChannel, sReplyChannel, sMessage, nDistance = p1, p2, p3, p4, p5
if sChannel == gps.CHANNEL_GPS and sMessage == "PING" then
local sSide, channel, computerId, sMessage, distance = p1, p2, p3, p4, p5
if channel == gps.CHANNEL_GPS and sMessage == "PING" then
-- We received a ping message on the GPS channel, send a response
local computer = memoize(computers, computerId, function() return { } end)
local modem = modems[sSide]
table.insert(computer, {
position = vector.new(modem.x, modem.y, modem.z), distance = distance
})
if #computer == 4 then
local pt = GPS.trilaterate(computer)
if pt then
positions[computerId] = pt
term.clear()
for k,v in pairs(positions) do
Util.print('ID: %d: %s %s %s', k, v.x, v.y, v.z)
end
end
computers[computerId] = nil
end
for _, modem in pairs(modems) do
modem.transmit( sReplyChannel, gps.CHANNEL_GPS, { modem.x, modem.y, modem.z })
modem.transmit( computerId, gps.CHANNEL_GPS, { modem.x, modem.y, modem.z })
end
end
end