mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-10-27 11:57:38 +00:00
Handle duplicate pings received by gps.locate
Co-authored-by: Wojbie <Wojbie@gmail.com>
This commit is contained in:
@@ -16,4 +16,90 @@ describe("The gps library", function()
|
||||
expect.error(gps.locate, 1, ""):eq("bad argument #2 (boolean expected, got string)")
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("on fake computers", function()
|
||||
local fake_computer = require "support.fake_computer"
|
||||
|
||||
local function gps_reciever(x, y, z, fn)
|
||||
local computer = fake_computer.make_computer(1, fn)
|
||||
computer.position = vector.new(x, y, z)
|
||||
fake_computer.add_api(computer, "rom/apis/gps.lua")
|
||||
fake_computer.add_api(computer, "rom/apis/vector.lua")
|
||||
|
||||
local modem = fake_computer.add_modem(computer, "back")
|
||||
return computer, modem
|
||||
end
|
||||
|
||||
local function gps_hosts(computer, modem, positions)
|
||||
local computers = {}
|
||||
for _, position in pairs(positions) do
|
||||
local position = vector.new(position[1], position[2], position[3])
|
||||
local host = fake_computer.make_computer(99, function(env)
|
||||
env.loadfile("/rom/programs/gps.lua")("host", position.x, position.y, position.z)
|
||||
end)
|
||||
host.position = position
|
||||
fake_computer.add_api(host, "rom/apis/gps.lua")
|
||||
fake_computer.add_api(host, "rom/apis/vector.lua")
|
||||
|
||||
local host_modem = fake_computer.add_modem(host, "back")
|
||||
fake_computer.add_modem_edge(modem, host_modem)
|
||||
|
||||
computers[#computers + 1] = host
|
||||
end
|
||||
|
||||
computers[#computers + 1] = computer -- Run the computer after hosts have started
|
||||
return computers
|
||||
end
|
||||
|
||||
it("locates a computer", function()
|
||||
local computer, modem = gps_reciever(12, 23, 52, function(env)
|
||||
local x, y, z = env.gps.locate()
|
||||
expect({ x, y, z }):same { 12, 23, 52 }
|
||||
end)
|
||||
local computers = gps_hosts(computer, modem, {
|
||||
{ 5, 5, 5 },
|
||||
{ 10, 5, 5 },
|
||||
{ 5, 10, 5 },
|
||||
{ 5, 5, 10 },
|
||||
})
|
||||
|
||||
fake_computer.run_all(computers, false)
|
||||
fake_computer.advance_all(computers, 2)
|
||||
fake_computer.run_all(computers, { computer })
|
||||
end)
|
||||
|
||||
it("fails to locate a computer with insufficient hosts", function()
|
||||
local computer, modem = gps_reciever(12, 23, 52, function(env)
|
||||
local x, y, z = env.gps.locate()
|
||||
expect({ x, y, z }):same { nil, nil, nil }
|
||||
end)
|
||||
local computers = gps_hosts(computer, modem, {
|
||||
{ 5, 5, 5 },
|
||||
{ 10, 5, 5 },
|
||||
{ 5, 10, 5 },
|
||||
})
|
||||
|
||||
fake_computer.run_all(computers, false)
|
||||
fake_computer.advance_all(computers, 2)
|
||||
fake_computer.run_all(computers, { computer })
|
||||
end)
|
||||
|
||||
it("doesn't fail on duplicate hosts", function()
|
||||
local computer, modem = gps_reciever(12, 23, 52, function(env)
|
||||
local x, y, z = env.gps.locate()
|
||||
expect({ x, y, z }):same { 12, 23, 52 }
|
||||
end)
|
||||
local computers = gps_hosts(computer, modem, {
|
||||
{ 5, 5, 5 },
|
||||
{ 5, 5, 5 },
|
||||
{ 10, 5, 5 },
|
||||
{ 5, 10, 5 },
|
||||
{ 5, 5, 10 },
|
||||
})
|
||||
|
||||
fake_computer.run_all(computers, false)
|
||||
fake_computer.advance_all(computers, 2)
|
||||
fake_computer.run_all(computers, { computer })
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
@@ -56,7 +56,12 @@ local function make_computer(id, fn)
|
||||
repeat local _, id = env.os.pullEvent("timer") until id == timer
|
||||
end,
|
||||
}
|
||||
env.redstone = {
|
||||
getSides = redstone.getSides,
|
||||
}
|
||||
env.rs = env.redstone
|
||||
env.sleep = env.os.sleep
|
||||
env.loadfile = function(path, mode, new_env) return loadfile(path, mode, new_env or env) end
|
||||
env.dofile = function(path)
|
||||
local fn, err = loadfile(path, nil, env)
|
||||
if fn then return fn() else error(err, 2) end
|
||||
@@ -91,7 +96,9 @@ local function make_computer(id, fn)
|
||||
end
|
||||
end
|
||||
|
||||
return { env = env, peripherals = peripherals, queue_event = queue_event, step = step, co = co, advance = advance }
|
||||
local position = vector.new(0, 0, 0)
|
||||
|
||||
return { env = env, peripherals = peripherals, queue_event = queue_event, step = step, co = co, advance = advance, position = position }
|
||||
end
|
||||
|
||||
local function parse_channel(c)
|
||||
@@ -112,10 +119,13 @@ local function add_modem(owner, side)
|
||||
|
||||
for _, adjacent in pairs(adjacent) do
|
||||
if adjacent.open[channel] then
|
||||
adjacent.owner.queue_event("modem_message", adjacent.side, channel, reply_channel, payload, 123)
|
||||
local distance = (adjacent.owner.position - owner.position):length()
|
||||
print(("Distance %s .. %s => %d"):format(adjacent.owner.position, owner.position, distance))
|
||||
adjacent.owner.queue_event("modem_message", adjacent.side, channel, reply_channel, payload, distance)
|
||||
end
|
||||
end
|
||||
end,
|
||||
isWireless = function() return true end,
|
||||
}, { type = "modem" })
|
||||
owner.peripherals[side] = peripheral
|
||||
return { adjacent = adjacent, side = side, owner = owner, open = open }
|
||||
|
||||
Reference in New Issue
Block a user