Skip to content

Commit

Permalink
Make the recycler a work-node
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
taniwha committed Dec 15, 2020
1 parent d69e210 commit ed9ab9b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 18 deletions.
1 change: 1 addition & 0 deletions Source/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
4 changes: 3 additions & 1 deletion Source/Recycler/Recycler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -191,6 +191,8 @@ public ModifierChangeWhen GetModuleMassChangeWhen ()
{
return ModifierChangeWhen.CONSTANTLY;
}

public ELVesselWorkNet workNet { get; set; }
}

}
3 changes: 1 addition & 2 deletions Source/Recycler/StateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public KFSMState FindState (string name)
Collider RecycleField;
RMResourceSet recycler_resources;
Part active_part;
ELVesselWorkNet workNet;
ELVesselWorkNet workNet => recycler.workNet;
HashSet<uint> recycle_parts;
List<BuildResource> part_resources;
int res_index;
Expand Down Expand Up @@ -252,7 +252,6 @@ public RecyclerFSM (ELRecycler recycler)

public void Start (Collider field)
{
workNet = recycler.vessel.FindVesselModuleImplementing<ELVesselWorkNet> ();
RecycleField = field;
GameEvents.onVesselWasModified.Add (onVesselWasModified);
recycler_resources = new RMResourceSet (recycler.vessel, recycle_parts);
Expand Down
29 changes: 18 additions & 11 deletions Source/Workshop/VesselWorkNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,19 @@ void BuildNetwork ()
{
Productivity = 0;
if (vessel.loaded) {
sources = vessel.FindPartModulesImplementing<ELWorkSource> ();
sinks = vessel.FindPartModulesImplementing<ELWorkSink> ();

foreach (var source in sources) {
source.workNet = this;
}
foreach (var sink in sinks) {
sink.workNet = this;
var nodes = vessel.FindPartModulesImplementing<ELWorkNode> ();
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);
Expand All @@ -86,7 +91,7 @@ protected override void OnLoad (ConfigNode node)
double.TryParse (s, out p);
Productivity = p;
}
sinks = new List<ELWorkSink> ();
sinks.Clear ();
protoSinks = new List<ELProtoWorkSink> ();
for (int i = 0; i < node.nodes.Count; i++) {
ConfigNode n = node.nodes[i];
Expand Down Expand Up @@ -129,6 +134,8 @@ void onVesselWasModified (Vessel v)
protected override void OnAwake ()
{
GameEvents.onVesselWasModified.Add (onVesselWasModified);
sources = new List<ELWorkSource> ();
sinks = new List<ELWorkSink> ();
}

void OnDestroy ()
Expand Down Expand Up @@ -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++) {
Expand All @@ -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
Expand Down
26 changes: 26 additions & 0 deletions Source/Workshop/WorkNode.cs
Original file line number Diff line number Diff line change
@@ -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
<http://www.gnu.org/licenses/>.
*/

namespace ExtraplanetaryLaunchpads {

public interface ELWorkNode
{
ELVesselWorkNet workNet { set; }
}

}
3 changes: 1 addition & 2 deletions Source/Workshop/WorkSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}

}
3 changes: 1 addition & 2 deletions Source/Workshop/WorkSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}

}

0 comments on commit ed9ab9b

Please sign in to comment.