Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 60 additions & 4 deletions source/AutomaticLabHousekeeper/ALH_Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void ProcessScienceForAllVessels()
{
ProtoPartModuleSnapshot labModule = protoPart.modules.FirstOrDefault(m => m.moduleName == "ModuleScienceLab");
ProtoPartModuleSnapshot alhModule = protoPart.modules.FirstOrDefault(m => m.moduleName == "Module_AutomaticLabHousekeeper");
ProtoPartModuleSnapshot converterModule = protoPart.modules.FirstOrDefault(m => m.moduleName == "ModuleScienceConverter");
ProtoPartModuleSnapshot converterModule = protoPart.modules.FirstOrDefault(m => m.moduleName == "ModuleScienceConverter" || m.moduleName == "CW_ModuleScienceConverter");

if (labModule == null || alhModule == null) continue;

Expand Down Expand Up @@ -305,7 +305,10 @@ void SimulateScienceProcessingForUnloadedLab(Vessel vessel, ProtoPartSnapshot pr
}

ConfigNode partConfig = PartLoader.getPartInfoByName(protoPart.partName).partConfig;
ConfigNode moduleNode = partConfig.GetNodes("MODULE").FirstOrDefault(n => n.GetValue("name") == "ModuleScienceConverter");
ConfigNode moduleNode = partConfig.GetNodes("MODULE")
.FirstOrDefault(n =>
n.GetValue("name") == "ModuleScienceConverter" ||
n.GetValue("name") == "CW_ModuleScienceConverter");

float scienceCap = float.Parse(moduleNode.GetValue("scienceCap"));
DebugLog($"[AutomaticLabHousekeeper] scienceCap {scienceCap}");
Expand Down Expand Up @@ -337,8 +340,19 @@ void SimulateScienceProcessingForUnloadedLab(Vessel vessel, ProtoPartSnapshot pr
double currentTime = Planetarium.GetUniversalTime();
double timeElapsed = currentTime - lastUpdateTime;

float totalScientistLevel = CalculateScientistLevel(protoPart, scientistBonus);
DebugLog($"[AutomaticLabHousekeeper] totalScientistValue {totalScientistLevel}");
float totalScientistLevel;
if (converterModule.moduleName == "CW_ModuleScienceConverter")
{
// Special handling for CW_ModuleScienceConverter
totalScientistLevel = GetAIScientistsLevel(vessel, protoPart, scientistBonus);
DebugLog($"[AutomaticLabHousekeeper] CW_ModuleScienceConverter détecté, totalScientistLevel : {totalScientistLevel}");
}
else
{
// Normal calculation based on crew
totalScientistLevel = CalculateScientistLevel(protoPart, scientistBonus);
DebugLog($"[AutomaticLabHousekeeper] ModuleScienceConverter détecté, totalScientistLevel calculé : {totalScientistLevel}");
}
double secondsPerDay = GameSettings.KERBIN_TIME ? 21600.0 : 86400.0;
float scienceRatePerDay = (float)(secondsPerDay * totalScientistLevel * dataStored * dataProcessingMultiplier * scienceMultiplier) / Mathf.Pow(10f, researchTime);
DebugLog($"[AutomaticLabHousekeeper] scienceRatePerDay {scienceRatePerDay}");
Expand Down Expand Up @@ -395,6 +409,48 @@ float CalculateScientistLevel(ProtoPartSnapshot protoPart, float scientistBonus)
return Mathf.Max(totalScientistLevel, 0.0f); // If no scientists, processing stops
}

float GetAIScientistsLevel(Vessel vessel, ProtoPartSnapshot converterPart, float scientistBonus)
{
float totalLevel = 0f;

// Find the parent index of the converter part
int parentIndex = converterPart.parentIdx;
if (parentIndex < 0 || parentIndex >= vessel.protoVessel.protoPartSnapshots.Count)
return 0f;

// Converter part's parent
ProtoPartSnapshot parentPart = vessel.protoVessel.protoPartSnapshots[parentIndex];

// All chips with this parent
var chips = vessel.protoVessel.protoPartSnapshots
.Where(p => p.parentIdx == parentIndex);

foreach (var chip in chips)
{
foreach (var module in chip.modules)
{
if (module.moduleName == "CW_AIScientists")
{
bool isActive = false;
float level = 0f;

if (module.moduleValues.HasValue("isActive"))
bool.TryParse(module.moduleValues.GetValue("isActive"), out isActive);

if (module.moduleValues.HasValue("level"))
float.TryParse(module.moduleValues.GetValue("level"), out level);

if (isActive)
{
DebugLog($"[AutomaticLabHousekeeper] Found active AI Scientist on {chip.partName}, level {level}");
totalLevel += (1.0f + scientistBonus * level);
}
}
}
}
return Mathf.Max(totalLevel, 0.0f);
}

void PullDataIntoLab(Vessel vessel, Part part, Module_AutomaticLabHousekeeper alhModule)
{
DebugLog($"[AutomaticLabHousekeeper] Attempting data pull for loaded lab {part.partInfo.name} on-board {vessel.vesselName}...");
Expand Down