From ed9ab9b873a2f16fad1ba8b93343953eabb211c4 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 15 Dec 2020 18:09:18 +0900 Subject: [PATCH] Make the recycler a work-node Work-sinks and work-sources are now both work-nodes. Any module implementing ELWorkNode will be updated with the current work-net. This fixes the recycler losing track of the work-net in some situations. --- Source/Makefile | 1 + Source/Recycler/Recycler.cs | 4 +++- Source/Recycler/StateMachine.cs | 3 +-- Source/Workshop/VesselWorkNet.cs | 29 ++++++++++++++++++----------- Source/Workshop/WorkNode.cs | 26 ++++++++++++++++++++++++++ Source/Workshop/WorkSink.cs | 3 +-- Source/Workshop/WorkSource.cs | 3 +-- 7 files changed, 51 insertions(+), 18 deletions(-) create mode 100644 Source/Workshop/WorkNode.cs diff --git a/Source/Makefile b/Source/Makefile index 8421f515..ca6d9a8e 100644 --- a/Source/Makefile +++ b/Source/Makefile @@ -77,6 +77,7 @@ EL_FILES := \ Workshop/ConstructionSkill.cs \ Workshop/ProtoWorkSink.cs \ Workshop/VesselWorkNet.cs \ + Workshop/WorkNode.cs \ Workshop/WorkSink.cs \ Workshop/WorkSource.cs \ Workshop/Workshop.cs \ diff --git a/Source/Recycler/Recycler.cs b/Source/Recycler/Recycler.cs index e36459b8..c4c9707a 100644 --- a/Source/Recycler/Recycler.cs +++ b/Source/Recycler/Recycler.cs @@ -25,7 +25,7 @@ You should have received a copy of the GNU General Public License namespace ExtraplanetaryLaunchpads { -public class ELRecycler : PartModule, IModuleInfo, IPartMassModifier, ELControlInterface +public class ELRecycler : PartModule, IModuleInfo, IPartMassModifier, ELControlInterface, ELWorkNode { [KSPField] public float RecycleRate = 1.0f; [KSPField] public string RecycleField_name = "RecycleField"; @@ -191,6 +191,8 @@ public ModifierChangeWhen GetModuleMassChangeWhen () { return ModifierChangeWhen.CONSTANTLY; } + + public ELVesselWorkNet workNet { get; set; } } } diff --git a/Source/Recycler/StateMachine.cs b/Source/Recycler/StateMachine.cs index b89d961c..8ca48d6f 100644 --- a/Source/Recycler/StateMachine.cs +++ b/Source/Recycler/StateMachine.cs @@ -61,7 +61,7 @@ public KFSMState FindState (string name) Collider RecycleField; RMResourceSet recycler_resources; Part active_part; - ELVesselWorkNet workNet; + ELVesselWorkNet workNet => recycler.workNet; HashSet recycle_parts; List part_resources; int res_index; @@ -252,7 +252,6 @@ public RecyclerFSM (ELRecycler recycler) public void Start (Collider field) { - workNet = recycler.vessel.FindVesselModuleImplementing (); RecycleField = field; GameEvents.onVesselWasModified.Add (onVesselWasModified); recycler_resources = new RMResourceSet (recycler.vessel, recycle_parts); diff --git a/Source/Workshop/VesselWorkNet.cs b/Source/Workshop/VesselWorkNet.cs index c2056853..16158d41 100644 --- a/Source/Workshop/VesselWorkNet.cs +++ b/Source/Workshop/VesselWorkNet.cs @@ -54,14 +54,19 @@ void BuildNetwork () { Productivity = 0; if (vessel.loaded) { - sources = vessel.FindPartModulesImplementing (); - sinks = vessel.FindPartModulesImplementing (); - - foreach (var source in sources) { - source.workNet = this; - } - foreach (var sink in sinks) { - sink.workNet = this; + var nodes = vessel.FindPartModulesImplementing (); + sources.Clear(); + sinks.Clear(); + + for (int i = nodes.Count; i-- > 0; ) { + var node = nodes[i]; + node.workNet = this; + if (node is ELWorkSink sink) { + sinks.Add (sink); + } + if (node is ELWorkSource source) { + sources.Add (source); + } } UpdateProductivity (true); @@ -86,7 +91,7 @@ protected override void OnLoad (ConfigNode node) double.TryParse (s, out p); Productivity = p; } - sinks = new List (); + sinks.Clear (); protoSinks = new List (); for (int i = 0; i < node.nodes.Count; i++) { ConfigNode n = node.nodes[i]; @@ -129,6 +134,8 @@ void onVesselWasModified (Vessel v) protected override void OnAwake () { GameEvents.onVesselWasModified.Add (onVesselWasModified); + sources = new List (); + sinks = new List (); } void OnDestroy () @@ -281,7 +288,7 @@ void FixedUpdate () num_sinks++; } } - //Debug.LogFormat ("[EL Workshop] num_sinks: {0}", num_sinks); + //Debug.LogFormat ("[ELVesselWorkNet] num_sinks: {0}", num_sinks); if (num_sinks > 0) { double work = hours / num_sinks; for (int i = 0; i < sinks.Count; i++) { @@ -291,7 +298,7 @@ void FixedUpdate () } } } else { - //Debug.LogFormat ("[EL Workshop] loaded: {0}", vessel.loaded); + //Debug.LogFormat ("[ELVesselWorkNet] loaded: {0}", vessel.loaded); if (!vessel.loaded) { // run out of work to do, so shut down until the vessel is next // loaded diff --git a/Source/Workshop/WorkNode.cs b/Source/Workshop/WorkNode.cs new file mode 100644 index 00000000..382d2859 --- /dev/null +++ b/Source/Workshop/WorkNode.cs @@ -0,0 +1,26 @@ +/* +This file is part of Extraplanetary Launchpads. + +Extraplanetary Launchpads is free software: you can redistribute it and/or +modify it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Extraplanetary Launchpads is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Extraplanetary Launchpads. If not, see +. +*/ + +namespace ExtraplanetaryLaunchpads { + +public interface ELWorkNode +{ + ELVesselWorkNet workNet { set; } +} + +} diff --git a/Source/Workshop/WorkSink.cs b/Source/Workshop/WorkSink.cs index 968abe62..2ff95ce4 100644 --- a/Source/Workshop/WorkSink.cs +++ b/Source/Workshop/WorkSink.cs @@ -18,12 +18,11 @@ You should have received a copy of the GNU General Public License namespace ExtraplanetaryLaunchpads { -public interface ELWorkSink +public interface ELWorkSink : ELWorkNode { void DoWork (double kerbalHours); bool isActive { get; } double CalculateWork (); - ELVesselWorkNet workNet { set; } } } diff --git a/Source/Workshop/WorkSource.cs b/Source/Workshop/WorkSource.cs index e016f2fb..f6b18ebd 100644 --- a/Source/Workshop/WorkSource.cs +++ b/Source/Workshop/WorkSource.cs @@ -18,12 +18,11 @@ You should have received a copy of the GNU General Public License namespace ExtraplanetaryLaunchpads { -public interface ELWorkSource +public interface ELWorkSource : ELWorkNode { void UpdateProductivity (); double Productivity { get; } bool isActive { get; } - ELVesselWorkNet workNet { set; } } }