Jump to content

First time plugin making, let's get some help.


Recommended Posts

Hmmm, now to stretch goals.

Fuel Cell is almost there, but without water and heat produced, it's not a real fuel cell.

Water will be easy, as I just have to add water to my resource list. Heat will be a problem.

I want to simulate fuel cells using water as cooling method.

Haven't seen any overheat variables within the editor.

Edited by CSX_Ind
Link to comment
Share on other sites

Think I'm going to have to separate the fuel cell into Hydrogen/Oxygen as separate part and reactor/water as another. Since KSP has this werid bug when you add more than 3 resources to the part.cfg.

Means I need to think of a way to manage resources throughout the vessel.

Link to comment
Share on other sites

Are you sure about that? Because many mods add more than 3 resources. What bug are you having exactly? Also, in C# everything is always a reference type, that's why the above code works (it creates a reference to the resource in the part).

Link to comment
Share on other sites

Are you sure about that? Because many mods add more than 3 resources. What bug are you having exactly?

Last time I had three resources (Liquid fuel, Oxidizer and Electric charge) in my fuel cell part, I load it, and it shows up in the VAB but when I hover my mouse over it, icon grows infinitely and debug window tells me there is an exception thrown(Something to do with index).

Link to comment
Share on other sites

Day Four, been searching the mysterious bug of infinitely growing in VAB parts.

Searched through bug report site and it looks like if you have more than three resources in .cfg file and have PartModule in the part, it should work.

Maybe I can try and add another resource onto my fuel cell again then.

Link to comment
Share on other sites

Well, got it to work, just added a dummy PartModule thing to the fuel cell and now it's working.

(Had it to be partless PartModule)

Part.cfg


RESOURCE
{
name = Oxygen
amount = 330
maxAmount = 330
}

RESOURCE
{
name = Hydrogen
amount = 660
maxAmount = 660
}

RESOURCE
{
name = Water
amount = 0
maxAmount = 330
}

MODULE
{
name=PowerGenerator
}

MODULE
{
name = DummyPartModule
}

Link to comment
Share on other sites

Alright, now to stretch goal.

Like I mentioned last time, I want to add an overheat gauge on the stage display, so players can see if the fuel cell is overheating or not.

Means I need to display fuel cell icon on the stage display, but can't find any class/method that does it.

Think I can just display it by right-click option?

Link to comment
Share on other sites

Added two new resources(Water and Heat), tested them out and looks like it works nicely.

Also added fuel cell's temperature gauge on right-click menu.

PowerGenerator.cs


using UnityEngine;
using KSP.IO;
using System.Collections.Generic;

namespace PowerGenerator
{
public class PowerGenerator : PartModule
{
PartResource hydrogen, oxygen, water, heat;

[KSPField(guiActive = true, guiName = "Cell Temp.", guiUnits = "C")]
public float cellTemp = 1000;

public override void OnStart(PartModule.StartState state)
{
this.hydrogen = this.part.Resources["Hydrogen"];
this.oxygen = this.part.Resources["Oxygen"];
this.water = this.part.Resources["Water"];
this.heat = this.part.Resources["Heat"];

this.part.temperature = cellTemp;
}

public override void OnUpdate()
{
if(this.vessel == FlightGlobals.ActiveVessel && this.part.isPrimary(this.vessel.parts, this.ClassID))
{
if (ActivateGenerator(Events["DeactivateGenerator"].active))
{
double parts = CountChargeRequired();

foreach (Part part in this.vessel.parts)
foreach (PartResource resource in part.Resources)
if (resource.resourceName == "ElectricCharge" && (resource.amount < resource.maxAmount))
resource.amount += (0.24 / parts) * Time.deltaTime;

if(this.water.amount < this.water.maxAmount)
this.water.amount += 0.4 * Time.deltaTime;

if (!Events["CoolingOff"].active)
{
if (this.heat.amount == this.heat.maxAmount)
cellTemp += 0.16f * Time.deltaTime;
else this.heat.amount += 0.16 * Time.deltaTime;
}
else if(Events["CoolingOff"].active && this.water.amount > 0)
{
if(cellTemp > 1000f)
cellTemp -= 0.16f * Time.deltaTime;
this.water.amount -= 0.16;
}
}

this.part.temperature = cellTemp;
}
}

private bool ActivateGenerator(bool isActivated)
{
if (isActivated)
{
if (this.oxygen.amount > 0)
{
this.oxygen.amount -= 0.1 * Time.deltaTime;
if (this.hydrogen.amount > 0)
{
this.hydrogen.amount -= 0.2 * Time.deltaTime;
return true;
}
else return false;
}
else return false;
}
return false;
}

private void UpdateResources()
{
foreach(PartResource resource in part.Resources)
{
if (resource.resourceName == this.hydrogen.resourceName)
resource.amount = this.hydrogen.amount;
if (resource.resourceName == this.oxygen.resourceName)
resource.amount = this.oxygen.amount;
}
}

private double CountChargeRequired()
{
double count = 0;
foreach (Part part in this.vessel.parts)
foreach (PartResource resource in part.Resources)
if (resource.resourceName == "ElectricCharge" && (resource.amount < resource.maxAmount))
count++;

return count;
}

[KSPEvent(guiActive = true, guiName = "Activate Fuel Cell")]
public void ActivateGenerator()
{
ScreenMessages.PostScreenMessage("Generator : On", 5.0f, ScreenMessageStyle.UPPER_CENTER);

Events["ActivateGenerator"].active = false;
Events["DeactivateGenerator"].active = true;
}

[KSPEvent(guiActive = true, guiName = "Deactivate Fuel Cell", active = false)]
public void DeactivateGenerator()
{
ScreenMessages.PostScreenMessage("Generator : Off", 5.0f, ScreenMessageStyle.UPPER_CENTER);

Events["ActivateGenerator"].active = true;
Events["DeactivateGenerator"].active = false;
}

[KSPEvent(guiActive = true, guiName = "Activate Cooling")]
public void CoolingOn()
{
Events["CoolingOn"].active = false;
Events["CoolingOff"].active = true;
}

[KSPEvent(guiActive = true, guiName = "Deativate Cooling", active = false)]
public void CoolingOff()
{
Events["CoolingOn"].active = true;
Events["CoolingOff"].active = false;
}

[KSPAction("Toggle Fuel Cell")]
public void ToggleFuelCellAction(KSPActionParam param)
{
if (param.type == KSPActionType.Activate)
ActivateGenerator();
else if (param.type == KSPActionType.Deactivate)
DeactivateGenerator();
}

[KSPAction("Toggle Cooling")]
public void ToggleCoolingAction(KSPActionParam param)
{
if (param.type == KSPActionType.Activate)
CoolingOn();
else if (param.type == KSPActionType.Deactivate)
CoolingOff();
}
}
}


Edited by CSX_Ind
Link to comment
Share on other sites

Day Five with modding, been cleaning and catching bugs last night.

Fixed & Working Fuel Cell Part+Plugin

PowerGenerator.cs


using UnityEngine;
using KSP.IO;
using System.Collections.Generic;

namespace PowerGenerator
{
public class PowerGenerator : PartModule
{
private PartResource hydrogen, oxygen, water, heat;

[KSPField(guiActive = true, guiName = "Cell Temperature ", guiUnits = "C")]
public float cellTemp = 1000;

public override void OnStart(PartModule.StartState state)
{
this.hydrogen = this.part.Resources["Hydrogen"];
this.oxygen = this.part.Resources["Oxygen"];
this.water = this.part.Resources["Water"];
this.heat = this.part.Resources["Heat"];

this.part.temperature = cellTemp;
}

public override void OnUpdate()
{
if(this.vessel == FlightGlobals.ActiveVessel)
{
if(UpdateGenerator())
{
double parts = CountChargeRequired();

foreach (Part part in this.vessel.parts)
foreach (PartResource resource in part.Resources)
if (resource.resourceName == "ElectricCharge" && (resource.amount < resource.maxAmount))
resource.amount += (0.24 / parts) * Time.deltaTime;

if(water.amount < water.maxAmount)
water.amount += 1 * Time.deltaTime;

if (heat.amount < heat.maxAmount)
heat.amount += 16.0 * Time.deltaTime;
else cellTemp += 16f * Time.deltaTime;
}
else
{
if (cellTemp > 1000f)
cellTemp -= 1f * Time.deltaTime;
}

if(UpdateCooling())
{
if (cellTemp > 1000f)
cellTemp -= 32f * Time.deltaTime;
}

if (this.oxygen.amount < 0 || this.hydrogen.amount < 0)
DeactivateGenerator();
if (this.water.amount < 0)
{
CoolingOff();
this.water.amount = 0;
}
if (cellTemp <= 1000f)
CoolingOff();

this.part.temperature = cellTemp; // Set Cell's temperature
}
}

private bool UpdateGenerator()
{
if(Events["DeactivateGenerator"].active)
{
if (oxygen.amount > 0 && hydrogen.amount > 0)
{
oxygen.amount -= 1 * Time.deltaTime;
hydrogen.amount -= 2 * Time.deltaTime;
return true;
}
else return false;
}

return false;
}

private bool UpdateCooling()
{
if(Events["CoolingOff"].active)
{
if (water.amount > 0)
{
water.amount -= 2 * Time.deltaTime;
return true;
}
else return false;
}

return false;
}

private void UpdateResources()
{
foreach(PartResource resource in part.Resources)
{
if (resource.resourceName == this.hydrogen.resourceName)
resource.amount = this.hydrogen.amount;
if (resource.resourceName == this.oxygen.resourceName)
resource.amount = this.oxygen.amount;
}
}

private double CountChargeRequired()
{
double count = 0;
foreach (Part part in this.vessel.parts)
foreach (PartResource resource in part.Resources)
if (resource.resourceName == "ElectricCharge" && (resource.amount < resource.maxAmount))
count++;

return count;
}

[KSPEvent(guiActive = true, guiName = "Activate Fuel Cell")]
public void ActivateGenerator()
{
ScreenMessages.PostScreenMessage("Generator : On", 5.0f, ScreenMessageStyle.UPPER_CENTER);

Events["ActivateGenerator"].active = false;
Events["DeactivateGenerator"].active = true;
}

[KSPEvent(guiActive = true, guiName = "Deactivate Fuel Cell", active = false)]
public void DeactivateGenerator()
{
ScreenMessages.PostScreenMessage("Generator : Off", 5.0f, ScreenMessageStyle.UPPER_CENTER);

Events["ActivateGenerator"].active = true;
Events["DeactivateGenerator"].active = false;
}

[KSPEvent(guiActive = true, guiName = "Activate Cooling")]
public void CoolingOn()
{
Events["CoolingOn"].active = false;
Events["CoolingOff"].active = true;
}

[KSPEvent(guiActive = true, guiName = "Deativate Cooling", active = false)]
public void CoolingOff()
{
Events["CoolingOn"].active = true;
Events["CoolingOff"].active = false;
}

[KSPAction("Toggle Fuel Cell")]
public void ToggleFuelCellAction(KSPActionParam param)
{
if (param.type == KSPActionType.Activate)
ActivateGenerator();
else if (param.type == KSPActionType.Deactivate)
DeactivateGenerator();
}

[KSPAction("Toggle Cooling")]
public void ToggleCoolingAction(KSPActionParam param)
{
if (param.type == KSPActionType.Activate)
CoolingOn();
else if (param.type == KSPActionType.Deactivate)
CoolingOff();
}
}
}

Time to Stretch goals and move onto other features that might add up to this part.

Link to comment
Share on other sites

Moving things, optimizing, and added two more module to control resource and their type. Also added lots of comments on the Fuel Cell module.

FuelCell.cs


/************************************************************************
* CSX Industry - Life Support Part+Plugin Pack for Kerbal Space Program*
* *
* Initial Alpha Release Version 0.3a *
* *
* Created by Charlie S. *
* Built on July 18th, 2014 *
************************************************************************/

using UnityEngine;
using KSP.IO;
using System.Collections.Generic;

namespace CSXIndustry
{
public class FuelCell : PartModule
{
private PartResource hydrogen; // Fuel cell main fuel
private PartResource oxygen; // Fuel cell main fuel
private PartResource water; // Cooling and Future usage for: GreenhouseMoudle
private PartResource heat; // Future usage for: GreenhouseModule

[KSPField(guiActive = true, guiName = "Cell Temperature ", guiUnits = "C")]
private float cellTemp = 1000f; // Fuel Cell temperature

public override void OnStart(PartModule.StartState state)
{
if(state != StartState.Editor) // When flight starts
{
// Set PartResource to variables
this.hydrogen = this.part.Resources["Hydrogen"];
this.oxygen = this.part.Resources["Oxygen"];
this.water = this.part.Resources["Water"];
this.heat = this.part.Resources["Heat"];

// Set initial fuel cell temperature
this.part.temperature = cellTemp;
}
}

public override void OnUpdate()
{
if (UpdateCell()) // Update Fuel Cell and check if it's running or not
{
// If running, get number of parts that requires electrical charges
List<Part> required = GetChargeRequired();

if(required.Count > 0) // If there is at least one part that required charging
foreach (Part part in required) // For each part that requires charging
part.Resources[Resources.electriccharge].amount += (0.24 / (double)required.Count) * Time.deltaTime; // Distribute these powers to them

// If water tank is not full, fill them up too
if (water.amount < water.maxAmount)
water.amount += 1.0 * Time.deltaTime;

// If heat storage is not full, fill them up too
if (heat.amount < heat.maxAmount)
heat.amount += 10.0 * Time.deltaTime;
else cellTemp += 10f * Time.deltaTime; // If heat storage is full, then fuel cell's temperature increases
}
else // If Fuel Cell is not running
if (cellTemp > 1000f) // If Fuel Cell is overheated
cellTemp -= 1f * Time.deltaTime; // Cool it down over time

if (UpdateCooling()) // If cooling is activated
if (cellTemp > 1000f) // And cell is overheated
cellTemp -= 15f * Time.deltaTime; // Cool it down

if (oxygen.amount <= 0 || hydrogen.amount <= 0) // If either oxygen or hydrogen tank is empty
DeactivateCell(); // Stop the Fuel Cell

if (water.amount <= 0 || cellTemp <= 1000f) // If there is no water in water tank or the Fuel Cell is at it's normal temperature
DeactivateCooling(); // Stop the cooling

this.part.temperature = cellTemp; // Set Fuel Cell's temperature
}

private bool UpdateCell()
{
if (Events["DeactivateCell"].active) // If Fuel Cell is activated
if (oxygen.amount > 0 && hydrogen.amount > 0) // If both tank have enough fuel to generate power
{
// Use fuel and return true
oxygen.amount -= 1 * Time.deltaTime;
hydrogen.amount -= 2 * Time.deltaTime;
return true;
}
else return false; // Else return false; Fuel Cell has not enough fuel to generate power

return false; // If not just return false; Fuel Cell is not activated
}

private bool UpdateCooling()
{
if (Events["DeactivateCooling"].active) // If cooling is activated
if (water.amount > 0) // If there is enough water to run cooling
{
// Use water and return true
water.amount -= 2 * Time.deltaTime;
return true;
}
else return false; // Else return false; there is not enough water to run cooling

return false; // If cooling is not running return false; Cooling is not activated
}

private List<Part> GetChargeRequired()
{
List<Part> list = new List<Part>();

foreach(Part part in this.vessel.parts) // For each part in entire vessel
foreach(PartResource charge in part.Resources) // And for each resource in the part
if(charge.resourceName == Resources.electriccharge && (charge.amount < charge.maxAmount)) // If it is electric charge and is not full
list.Add(part); // Add it to the list

return list; // Return the list
}

// Below for right-click options and action groups //

[KSPEvent(guiActive = true, guiName = "Activate Fuel Cell")]
public void ActivateCell()
{
ScreenMessages.PostScreenMessage("Fuel Cell : On", 5.0f, ScreenMessageStyle.UPPER_CENTER);

Events["ActivateCell"].active = false;
Events["DeactivateCell"].active = true;
}

[KSPEvent(guiActive = true, guiName = "Deactivate Fuel Cell", active = false)]
public void DeactivateCell()
{
ScreenMessages.PostScreenMessage("Fuel Cell : Off", 5.0f, ScreenMessageStyle.UPPER_CENTER);

Events["ActivateCell"].active = true;
Events["DeactivateCell"].active = false;
}

[KSPEvent(guiActive = true, guiName = "Activate Cooling")]
public void ActivateCooling()
{
Events["ActivateCooling"].active = false;
Events["DeactivateCooling"].active = true;
}

[KSPEvent(guiActive = true, guiName = "Deactivate Cooling", active = false)]
public void DeactivateCooling()
{
Events["ActivateCooling"].active = true;
Events["DeactivateCooling"].active = false;
}

[KSPAction("Toggle Fuel Cell")]
public void FuelCellAction(KSPActionParam param)
{
if (param.type == KSPActionType.Activate)
ActivateCell();
else if (param.type == KSPActionType.Deactivate)
DeactivateCell();
}

[KSPAction("Toggle Cooling")]
public void CoolingAction(KSPActionParam param)
{
if (param.type == KSPActionType.Activate)
ActivateCooling();
else if (param.type == KSPActionType.Deactivate)
DeactivateCooling();
}
}
}

Resources.cs


/************************************************************************
* CSX Industry - Life Support Part+Plugin Pack for Kerbal Space Program*
* *
* Initial Alpha Release Version 0.3a *
* *
* Created by Charlie S. *
* Built on July 18th, 2014 *
************************************************************************/

namespace CSXIndustry
{
public class Resources
{
// KSP Default Resources
public const string liquidfuel = "LiquidFuel";
public const string oxidizer = "Oxidizer";
public const string electriccharge = "ElectricCharge";
public const string monopropellant = "MonoPropellant";

// CSX Custom Resources
public const string hydrogen = "Hydrogen";
public const string oxygen = "Oxygen";
public const string water = "Water";
public const string Heat = "Heat";
}
}

Now to stretch goals -

Want to add a bit of futuristic module, named Greenhouse Module. It would have a capability of producing food products by spending heat, water, electricity and (further) carbon dioxide.

Greenhouse Module would also generate oxygen probably..

Link to comment
Share on other sites

Day Six on plugin making, added crew manifest to work with food. Hasn't added carbon dioxide yet though.

Don't know why but couldn't initialize crew list with Awake() function, so had to add boolean to control initialize.

CrewManagement.cs


/************************************************************************
* CSX Industry - Life Support Part+Plugin Pack for Kerbal Space Program*
* *
* Initial Alpha Release Version 0.3a *
* *
* Created by Charlie S. *
* Built on July 18th, 2014 *
* Initial Built on July 12th, 2014 *
************************************************************************/

using UnityEngine;
using KSP.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CSXIndustry.CrewManage;

namespace CSXIndustry
{
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class CrewManagement : MonoBehaviour
{
// List of crews and all the modules with food
private List<ModCrew> crews;
private List<Part> foods;

private bool hasInit;

public void Awake() // At start
{
hasInit = false;
}

public void Update()
{
if (!hasInit)
Initialize();

// On every update frame
foreach(ModCrew crew in crews)
{
// Update crew details
crew.Update();
if(!crew.HasEaten()) // If this Kerman hasn't eaten
foreach(Part part in foods)
foreach(PartResource food in part.Resources) // For each food resource module
if(food.resourceName == Resources.food)
{
// Calculate food consume and convert it to next eating timer factor
double actual = part.RequestResource(Resources.food, 1.5 * TimeWarp.fixedDeltaTime);
double factor = actual / 1.5;

crew.SetEaten(true); // Set this Kerman to eaten status
crew.SetNextEatAt(300.0 * factor); // Set next eat timer

if (crew.GetEatTimer() < 1) // If this crew didn't eat
{
crew.SetKillTimer(crew.GetKillTimer() - (300.0 - (300.0 * factor))); // Deduct it's kill factor
crew.SetNextEatAt(300.0); // But show some mercy
}
}

if (crew.GetKillTimer() < 0) // If this Kerman's time has come
{
// Kill this Kerman and remove him from the list
crew.KillKerman();
crews.Remove(crew);
}
}
}

private void Initialize()
{
Debug.Log("Crew Management Plugin, Part of CSX_Industry.");
// Initiate new crew list
Debug.Log("Initializing crews");
crews = new List<ModCrew>();
foreach (ProtoCrewMember crew in FlightGlobals.ActiveVessel.GetVesselCrew()) // Search for crews inside of this vessel
{
crews.Add(new ModCrew(crew)); // Add them to the crew list
Debug.Log(crew.name + " has been added to crew list");
}

// Initiate new food list
Debug.Log("Initializing food list");
foods = new List<Part>();

// Search for any part with food resources in it.
foreach (Part part in FlightGlobals.ActiveVessel.Parts)
foreach (PartResource food in part.Resources)
if (food.resourceName == Resources.food)
{
foods.Add(part); // Add them to the food list
Debug.Log(part.name + " has been added to food module");
}

hasInit = true;
}
}
}

ModCrew.cs - To work with ProtoCrewMember bit easier


/************************************************************************
* CSX Industry - Life Support Part+Plugin Pack for Kerbal Space Program*
* *
* Initial Alpha Release Version 0.3a *
* *
* Created by Charlie S. *
* Built on July 18th, 2014 *
* Initial Built on July 12th, 2014 *
************************************************************************/

using UnityEngine;
using KSP.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSXIndustry.CrewManage
{
public class ModCrew
{
private double eatTimer = 300; // Timer that controls eating
private bool hasEaten = true; // Boolean that checks if this crew has eaten

private double killTimer = 3000; // Timer that checks if this Kerman needs to die

private ProtoCrewMember crew; // Detail of this crew

public ModCrew(ProtoCrewMember crew)
{
// Set this crew
this.crew = crew;
}

public void Update()
{
// Deduct eat timer
eatTimer -= 1.0 * TimeWarp.fixedDeltaTime;

// If it's time to eat
if(eatTimer < 1)
{
hasEaten = false; // Set eaten to false
}
}

public void KillKerman()
{
// Report message to player that this Kerman has died due to HUNGER
ScreenMessages.PostScreenMessage(crew.name + " has died due to hunger", 5.0f, ScreenMessageStyle.UPPER_CENTER);

crew.Die(); // Kill this Kerman
}

// All Get & Set functions
public bool HasEaten() { return this.hasEaten; }
public void SetEaten(bool hasEaten) { this.hasEaten = hasEaten; }

public double GetEatTimer() { return this.eatTimer; }
public void SetNextEatAt(double eatTimer) { this.eatTimer = (int) eatTimer; }

public double GetKillTimer() { return this.killTimer; }
public void SetKillTimer(double killTimer) { this.killTimer = (int) killTimer; }

public ProtoCrewMember GetCrew() { return this.crew; }
public void SetCrew(ProtoCrewMember crew) { this.crew = crew; }
}
}

Link to comment
Share on other sites

Day Eight on plugin making, been focusing on crew management.

Sources so far

CrewManagement.cs


/************************************************************************
* CSX Industry - Life Support Part+Plugin Pack for Kerbal Space Program*
* *
* Initial Alpha Release Version 0.3a *
* *
* Created by Charlie S. *
* Built on July 18th, 2014 *
* Initial Built on July 12th, 2014 *
************************************************************************/

using UnityEngine;
using KSP.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CSXIndustry.CrewManage;

namespace CSXIndustry
{
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class CrewManagement : MonoBehaviour
{
// List of crews and all the modules with crew neccessities
private List<ModCrew> crews;
private List<Part> foods, oxygen, water;

private bool hasInit;

public void Awake() // At start
{
hasInit = false;
}

public void Update()
{
if (!hasInit)
Initialize();

// On every update frame
foreach(ModCrew crew in crews)
{
// Update crew details
crew.Update();
if(!crew.HasEaten()) // If this Kerman hasn't eaten
foreach(Part part in foods)
foreach(PartResource food in part.Resources) // For each food resource module
if(food.resourceName == Resources.food)
{
// Calculate food consume and convert it to next eating timer factor
double actual = part.RequestResource(Resources.food, 1.5 * TimeWarp.fixedDeltaTime);
double factor = actual / 1.5;

crew.SetEaten(true); // Set this Kerman to eaten status
crew.SetNextEatAt(300.0 * factor); // Set next eat timer

if (crew.GetEatTimer() < 1) // If this crew didn't eat
{
crew.SetKillTimer(crew.GetKillTimer() - (300.0 - (300.0 * factor))); // Deduct it's kill factor
crew.SetNextEatAt(300.0); // But show some mercy
}
}

if (crew.GetKillTimer() < 0) // If this Kerman's time has come
{
// Kill this Kerman and remove him from the list
crew.KillKerman();
crews.Remove(crew);
crews.TrimExcess();
}
}

// If there are crews still
if(crews.Count > 0)
{
// They need to breath
}
}

private void Initialize()
{
Debug.Log("Crew Management Plugin, Part of CSX_Industry 0.3a");
// Initiate new crew list
Debug.Log("Initializing crews");
crews = new List<ModCrew>();
foreach (ProtoCrewMember crew in FlightGlobals.ActiveVessel.GetVesselCrew()) // Search for crews inside of this vessel
{
crews.Add(new ModCrew(crew)); // Add them to the crew list
Debug.Log(crew.name + " has been added to crew list");
}

// Initiate new food list
Debug.Log("Initializing food list");
foods = new List<Part>();
oxygen = new List<Part>();
water = new List<Part>();

// Search for any part with food resources in it.
foreach (Part part in FlightGlobals.ActiveVessel.Parts)
foreach (PartResource resource in part.Resources)
{
if (resource.resourceName == Resources.food)
{
foods.Add(part); // Add them to the food list
Debug.Log(part.name + " has been added to food module");
}
else if(resource.resourceName == Resources.oxygen)
{
oxygen.Add(part);
Debug.Log(part.name + " has been added to oxygen module");
}
else if(resource.resourceName == Resources.water)
{
water.Add(part);
Debug.Log(part.name + " has been added to water module");
}

}

hasInit = true;
}
}
}

ModCrew.cs


/************************************************************************
* CSX Industry - Life Support Part+Plugin Pack for Kerbal Space Program*
* *
* Initial Alpha Release Version 0.3a *
* *
* Created by Charlie S. *
* Built on July 18th, 2014 *
* Initial Built on July 12th, 2014 *
************************************************************************/

using UnityEngine;
using KSP.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSXIndustry.CrewManage
{
public class ModCrew
{
private double eatTimer = 300; // Timer that controls eating
private bool hasEaten = true; // Boolean that checks if this crew has eaten

private double killTimer = 3000; // Timer that checks if this Kerman needs to die

private ProtoCrewMember crew; // Detail of this crew

public ModCrew(ProtoCrewMember crew)
{
// Set this crew
this.crew = crew;
}

public void Update()
{
// Deduct eat timer
eatTimer -= 1.0 * TimeWarp.fixedDeltaTime;

// If it's time to eat
if(eatTimer < 1)
{
hasEaten = false; // Set eaten to false
}
}

public void KillKerman()
{
// Report message to player that this Kerman has died due to HUNGER
ScreenMessages.PostScreenMessage(crew.name + " has died due to hunger", 5.0f, ScreenMessageStyle.UPPER_CENTER);

crew.Die(); // Kill this Kerman
}

// All Get & Set functions
public bool HasEaten() { return this.hasEaten; }
public void SetEaten(bool hasEaten) { this.hasEaten = hasEaten; }

public double GetEatTimer() { return this.eatTimer; }
public void SetNextEatAt(double eatTimer) { this.eatTimer = (int) eatTimer; }

public double GetKillTimer() { return this.killTimer; }
public void SetKillTimer(double killTimer) { this.killTimer = (int) killTimer; }

public ProtoCrewMember GetCrew() { return this.crew; }
public void SetCrew(ProtoCrewMember crew) { this.crew = crew; }
}
}

Link to comment
Share on other sites

Been reading some CO2 filteration, and thought maybe it's useful if I make KO2 filter instead, use the Sabatier Reaction,

KO2 + 4H2 -> KH4 + 2H2O + Energy

And send these Kethane to waste depository, since Kethane itself cannot be used by any of my LS module(s).

Link to comment
Share on other sites

So, been looking into some APIs, and wanted to do some cargo type switching using right-click menu.

I've looked into SetResource(ConfigNode node), but couldn't figure out how exactly it works.

I don't know if that node is referring to a node under my MODULE section in Part.cfg


PART
{
MODULE
{
//... Some Fields
FuelTank
{
//...Resource
}
}
}

Something like this?

Link to comment
Share on other sites

Wonder this is possible :


class SomeClass : PartModule
{
[KSPField] public string partType;
}

public class SomePart : SomeClass
{
/*...*/
}

Then in a .cfg file :


PART
{
/*...*/
MODULE
{
name = SomePart
partType = "PartName"
}
}

So long story short, can I still call that field from inherited class?

Link to comment
Share on other sites

I'm pretty sure you can.

While I have not done it myself, while going through the FireSpitter mod to add it's actions groups to my mod, I saw that the partModule for the wings is

 FSActualWing : FSWingTemplate

and there was a

FSWingTemplate : PartModule

present in the .dll that the wings did not actually use.

(Can't remember the actual names off the top of my head, but the structure is the same.)

Testing required of course, but I'm thinking it should work.

D.

Link to comment
Share on other sites

Just for the sake of recording -

CSXPartModule.cs


using UnityEngine;
using KSP.IO;

namespace CSX.ModuleManagement
{
public class CSXPartModule : PartModule
{
[KSPField]
public string partType;
}

public enum CSXPartType
{
NONE,
Power,
Filter,
Produce,
Collector
}
}

And fuel cell module includes -

CSXFuelCell.cs


using UnityEngine;
using KSP.IO;
using System.Collections.Generic;

namespace CSX.ModuleManagement
{
public class CSXFuelCell : CSXPartModule
{
/*...*/

Then added partType to my .cfg and it worked nicely.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...