Skip to content

Commit 8f281a4

Browse files
committed
fix element counting parts multiple times
1 parent f53bdd6 commit 8f281a4

1 file changed

Lines changed: 30 additions & 27 deletions

File tree

src/kOS/Suffixed/ElementValue.cs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,23 @@ public static ListValue PartsToList(IEnumerable<global::Part> parts, SharedObjec
6767
return ListValue.CreateList(elements.Values.ToList());
6868
}
6969

70+
private static DockedVesselInfo GetDockedVesselInfo(global::Part part)
71+
{
72+
for (int moduleIndex = 0; moduleIndex < part.Modules.Count; moduleIndex++)
73+
{
74+
var module = part.Modules[moduleIndex];
75+
if (module is ModuleDockingNode dockingNode)
76+
{
77+
return dockingNode.vesselInfo;
78+
}
79+
else if (module is ModuleGrappleNode grappleNode)
80+
{
81+
return grappleNode.vesselInfo;
82+
}
83+
}
84+
return null;
85+
}
86+
7087
private static void WorkQueue(Queue<ElementPair> queue, Dictionary<uint, ElementValue> elements, SharedObjects shared)
7188
{
7289
var visitedFlightIds = new HashSet<uint>();
@@ -76,35 +93,21 @@ private static void WorkQueue(Queue<ElementPair> queue, Dictionary<uint, Element
7693
ElementPair pair = queue.Dequeue();
7794
if (AlreadyVisited(pair.Part, visitedFlightIds)) continue;
7895

79-
for (int moduleIndex = 0; moduleIndex < pair.Part.Modules.Count; moduleIndex++)
96+
DockedVesselInfo info = GetDockedVesselInfo(pair.Part);
97+
ElementValue element;
98+
99+
if (info == null)
80100
{
81-
var module = pair.Part.Modules[moduleIndex];
82-
83-
ElementValue element;
84-
DockedVesselInfo info = null;
85-
86-
if (module is ModuleDockingNode dockingNode)
87-
{
88-
info = dockingNode.vesselInfo;
89-
}
90-
else if (module is ModuleGrappleNode grappleNode)
91-
{
92-
info = grappleNode.vesselInfo;
93-
}
94-
95-
if (info == null)
96-
{
97-
element = pair.Element;
98-
}
99-
else if (!elements.TryGetValue(info.rootPartUId, out element))
100-
{
101-
element = new ElementValue(info, shared);
102-
elements.Add(info.rootPartUId, element);
103-
}
104-
105-
element.AddPart(pair.Part);
106-
EnqueueChildren(queue, element, pair.Part);
101+
element = pair.Element;
107102
}
103+
else if (!elements.TryGetValue(info.rootPartUId, out element))
104+
{
105+
element = new ElementValue(info, shared);
106+
elements.Add(info.rootPartUId, element);
107+
}
108+
109+
element.AddPart(pair.Part);
110+
EnqueueChildren(queue, element, pair.Part);
108111
}
109112
}
110113

0 commit comments

Comments
 (0)