Skip to content

Commit

Permalink
Cache relevant partmodules in RACommNetVessel
Browse files Browse the repository at this point in the history
  • Loading branch information
siimav committed Oct 20, 2024
1 parent b5f4e25 commit e493ea4
Showing 1 changed file with 78 additions and 57 deletions.
135 changes: 78 additions & 57 deletions src/RealAntennasProject/RACommNetVessel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ public class RACommNetVessel : CommNet.CommNetVessel
protected const string ModTag = "[RealAntennasCommNetVessel]";
readonly List<RealAntenna> antennaList = new List<RealAntenna>();
readonly List<RealAntenna> inactiveAntennas = new List<RealAntenna>();
readonly List<CommNet.ModuleProbeControlPoint> probeControlPoints = new List<CommNet.ModuleProbeControlPoint>();
private PartResourceDefinition electricChargeDef;
public override IScienceDataTransmitter GetBestTransmitter() =>
(IsConnected && Comm is RACommNode node && node.AntennaTowardsHome() is RealAntenna toHome) ? toHome.Parent : null;

[KSPField(isPersistant = true)] public bool powered = true;

public override IScienceDataTransmitter GetBestTransmitter() =>
(IsConnected && Comm is RACommNode node && node.AntennaTowardsHome() is RealAntenna toHome) ? toHome.Parent : null;

public override void OnNetworkPreUpdate()
{
base.OnNetworkPreUpdate();
Expand Down Expand Up @@ -67,14 +69,14 @@ protected override void OnStart()
ParentVessel = Vessel,
};
(comm as RACommNode).RAAntennaList = DiscoverAntennas();
DiscoverProbeControlPoints();
vessel.connection = this;
networkInitialised = false;
if (CommNet.CommNetNetwork.Initialized)
OnNetworkInitialized();
GameEvents.CommNet.OnNetworkInitialized.Add(OnNetworkInitialized);
if (HighLogic.LoadedScene == GameScenes.TRACKSTATION)
GameEvents.onPlanetariumTargetChanged.Add(OnMapFocusChange);
GameEvents.onVesselWasModified.Add(OnVesselModified);
foreach (ModuleDeployablePart mdp in Vessel.FindPartModulesImplementing<ModuleDeployablePart>())
{
mdp.OnMoving.Add(OnMoving);
Expand All @@ -85,12 +87,32 @@ protected override void OnStart()
}
}

protected override void Update()
{
if (vessel.loaded)
{
int count = vessel.Parts.Count;
if (count != partCountCache)
{
partCountCache = count;
Profiler.BeginSample("RA DiscoverPartModules");
FindCommandSources();
if (Comm is RACommNode)
{
DiscoverAntennas();
}
DiscoverProbeControlPoints();
Profiler.EndSample();
}
UpdateControlState();
}
}

private void OnMoving(float f1, float f2) => DiscoverAntennas();
private void OnStop(float f1) => DiscoverAntennas();

protected override void OnDestroy()
{
GameEvents.onVesselWasModified.Remove(OnVesselModified);
GameEvents.CommNet.OnNetworkInitialized.Remove(OnNetworkInitialized);
GameEvents.onPlanetariumTargetChanged.Remove(OnMapFocusChange);
base.OnDestroy();
Expand All @@ -101,15 +123,46 @@ protected override void OnDestroy()

protected override void UpdateComm()
{
if (comm is RACommNode)
if (comm is RACommNode raComm)
{
raComm.name = gameObject.name;
raComm.displayName = vessel.GetDisplayName();
raComm.isControlSource = false;
raComm.isControlSourceMultiHop = false;
raComm.antennaRelay.power = raComm.antennaTransmit.power = 0.0;
hasScienceAntenna = raComm.RAAntennaList.Count > 0;
DetermineControl();
}
}

private void DiscoverProbeControlPoints()
{
probeControlPoints.Clear();

if (vessel.loaded)
{
foreach (Part part in vessel.Parts)
{
if (part.FindModuleImplementingFast<CommNet.ModuleProbeControlPoint>() is CommNet.ModuleProbeControlPoint pcp &&

Check failure on line 146 in src/RealAntennasProject/RACommNetVessel.cs

View workflow job for this annotation

GitHub Actions / build

'Part' does not contain a definition for 'FindModuleImplementingFast' and no accessible extension method 'FindModuleImplementingFast' accepting a first argument of type 'Part' could be found (are you missing a using directive or an assembly reference?)
pcp.CanControl())
{
probeControlPoints.Add(pcp);
}
}
}
else
{
comm.name = gameObject.name;
comm.displayName = vessel.GetDisplayName();
comm.isControlSource = false;
comm.isControlSourceMultiHop = false;
comm.antennaRelay.power = comm.antennaTransmit.power = 0.0;
hasScienceAntenna = (comm as RACommNode).RAAntennaList.Count > 0;
if (vessel.loaded) DetermineControlLoaded(); else DetermineControlUnloaded();
int index = 0;
foreach (ProtoPartSnapshot protoPartSnapshot in vessel.protoVessel.protoPartSnapshots)
{
index++;
Part part = protoPartSnapshot.partInfo.partPrefab;
if (part.FindModuleImplementingFast<CommNet.ModuleProbeControlPoint>() is CommNet.ModuleProbeControlPoint pcp &&

Check failure on line 160 in src/RealAntennasProject/RACommNetVessel.cs

View workflow job for this annotation

GitHub Actions / build

'Part' does not contain a definition for 'FindModuleImplementingFast' and no accessible extension method 'FindModuleImplementingFast' accepting a first argument of type 'Part' could be found (are you missing a using directive or an assembly reference?)
pcp.CanControlUnloaded(protoPartSnapshot.FindModule(pcp, index)))
{
probeControlPoints.Add(pcp);
}
}
}
}

Expand All @@ -124,60 +177,27 @@ private int CountControllingCrew()
return numControl;
}

private void DetermineControlUnloaded()
private void DetermineControl()
{
Profiler.BeginSample("DetermineControlUnloaded");
int numControl = CountControllingCrew();
int index = 0;
foreach (ProtoPartSnapshot protoPartSnapshot in vessel.protoVessel.protoPartSnapshots)
{
index++;
Part part = protoPartSnapshot.partInfo.partPrefab;
if (part.FindModuleImplementingFast<CommNet.ModuleProbeControlPoint>() is CommNet.ModuleProbeControlPoint pcp &&
pcp.CanControlUnloaded(protoPartSnapshot.FindModule(pcp, index)))
{
if (numControl >= pcp.minimumCrew || pcp.minimumCrew <= 0)
comm.isControlSource = true;
if (pcp.multiHop)
comm.isControlSourceMultiHop = true;
if (probeControlPoints.Count == 0) return;

// Assume that control parts are closer to root than leaf nodes.
// If both fields are true then no point in looking any further.
if (comm.isControlSource && comm.isControlSourceMultiHop)
break;
}
}
Profiler.EndSample();
}

private void DetermineControlLoaded()
{
Profiler.BeginSample("DetermineControlLoaded");
Profiler.BeginSample("RA DetermineControl");
int numControl = CountControllingCrew();
foreach (Part part in vessel.Parts)
foreach (CommNet.ModuleProbeControlPoint pcp in probeControlPoints)
{
if (part.FindModuleImplementingFast<CommNet.ModuleProbeControlPoint>() is CommNet.ModuleProbeControlPoint pcp &&
pcp.CanControl())
{
if (numControl >= pcp.minimumCrew || pcp.minimumCrew <= 0)
comm.isControlSource = true;
if (pcp.multiHop)
comm.isControlSourceMultiHop = true;
if (numControl >= pcp.minimumCrew || pcp.minimumCrew <= 0)
comm.isControlSource = true;
if (pcp.multiHop)
comm.isControlSourceMultiHop = true;

// Assume that control parts are closer to root than leaf nodes.
// If both fields are true then no point in looking any further.
if (comm.isControlSource && comm.isControlSourceMultiHop)
break;
}
// Assume that control parts are closer to root than leaf nodes.
// If both fields are true then no point in looking any further.
if (comm.isControlSource && comm.isControlSourceMultiHop)
break;
}
Profiler.EndSample();
}

protected void OnVesselModified(Vessel data)
{
if (this != null && data == Vessel && Comm is RACommNode) DiscoverAntennas();
}

protected List<RealAntenna> DiscoverAntennas()
{
antennaList.Clear();
Expand All @@ -202,6 +222,7 @@ protected List<RealAntenna> DiscoverAntennas()
}
return antennaList;
}

if (Vessel.protoVessel != null)
{
foreach (ProtoPartSnapshot part in Vessel.protoVessel.protoPartSnapshots)
Expand Down

0 comments on commit e493ea4

Please sign in to comment.