local Wires = {};
local function AddConnection(Connector, targetConnection)
if (not Wires[Connector]) then
Wires[Connector] = {targetConnection};
elseif (not table.find(Wires[Connector], targetConnection)) then
table.insert(Wires[Connector], targetConnection);
end
if (not Wires[targetConnection]) then
Wires[targetConnection] = {Connector};
elseif (not table.find(Wires[targetConnection], Connector)) then
table.insert(Wires[targetConnection], Connector);
end
end
local function RemoveConnection(Connector, Connection)
if (Wires[Connector]) then
local Index = table.find(Wires[Connector], Connection);
if (Index) then
table.remove(Wires[Connector], Index);
end
end
if (Wires[Connection]) then
local Index = table.find(Wires[Connection], Connector);
if (Index) then
table.remove(Wires[Connection], Index);
end
end
end
local function FindConnections(Connection)
if (not Wires[Connection]) then return; end -- Connection is not connected to anything!
local Connections = {};
local Iterate; Iterate = function(Connector)
if (Wires[Connector]) then
for _,SubConnection in pairs(Wires[Connector]) do
if (not table.find(Connections, SubConnection)) then
table.insert(Connections, SubConnection);
Iterate(SubConnection);
end
end
end
end ; Iterate(Connection);
return Connections;
end
Something like this?
Essentially if you want to connect two ‘Connectors’ together (e.g. Model1 and Model2), do:
AddConnection(Model1, Model2);
The Wires
table will look like this after:
local Wires = {
[Model1] = {Model2};
[Model2] = {Model1};
}
The FindConnections
function will try to locate the object you’re referencing from the table, then iteratively loop through every connection and add it to a table. Then all you need to do is search through the returned table and check to see if any of them is a battery.
I’ve not tested it, so there may be some errors. There may be a more efficient way of doing this.
I’ve done it this way for expandability, so you should be able to reference any connection at any point on the circuit and get everything connected to it. It also means you can potentially have multiple things connects to the same thing and it will still work.
Edit: I’ve tested it now, it’s working as intended:
Utilise beams to visualise