Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
AniruddhKSP authored Aug 21, 2020
1 parent c9c6297 commit 67eef30
Show file tree
Hide file tree
Showing 11 changed files with 978 additions and 195 deletions.
85 changes: 83 additions & 2 deletions AblativeEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,89 @@ namespace BeamedPowerStandalone
{
public class AblativeEngine : ModuleEnginesFX
{
[KSPField(guiName = "Thrust", guiActive = true, guiActiveEditor = false, guiUnits = "N")]
[KSPField(guiName = "Thrust", guiActive = true, guiActiveEditor = false, guiUnits = "N", isPersistant = false)]
public float thrust_ui;
// not yet complete, and thus not compiled into released .dll

[KSPField(isPersistant = false)]
public float SurfaceArea;

Vector3d source; Vector3d dest; double prevThrust;
List<Vessel> VesselsList; List<string> targetList;
List<double> excessList; List<double> constantList;
List<string> wavelengthList; int frames; double received_power;
VesselFinder vesselFinder = new VesselFinder();
RelativeOrientation rotation = new RelativeOrientation();
//BPOcclusion occlusion = new BPOcclusion();

public void Start()
{
throttleInstant = false;
useEngineResponseTime = true;
engineID = "BPAE";
minThrust = 0;
engineType = EngineType.Generic;
frames = 145; prevThrust = 0;
VesselsList = new List<Vessel>();
excessList = new List<double>();
constantList = new List<double>();
wavelengthList = new List<string>();
}

public void FixedUpdate()
{
frames += 1;
if (frames == 150)
{
vesselFinder.SourceData(out VesselsList, out excessList, out constantList, out targetList, out wavelengthList);
frames = 0;
}

if (VesselsList.Count > 0)
{
dest = this.vessel.GetWorldPos3D();
received_power = 0;

// adds up all the received power values from all vessels in CorrectVesselList
for (int n = 0; n < VesselsList.Count; n++)
{
if (targetList[n] == this.vessel.GetDisplayName())
{
double excess2 = excessList[n]; double constant2 = constantList[n];
source = VesselsList[n].GetWorldPos3D();
double distance = Vector3d.Distance(source, dest);
double spot_area = Math.Pow((constant2 * distance) / 2, 2) * 3.14;
double flux = rotation.FractionalFlux(source, dest, this.vessel, this.part);
//occlusion.CheckIfOccluded(VesselsList[n], this.vessel, out _, out bool occluded);

// adding EC that has been received
if (SurfaceArea < spot_area)
{
//if (occluded == false)
//{
received_power += flux * Math.Round(SurfaceArea / spot_area * excess2);
//}
}
else
{
//if (occluded == false)
//{
received_power += flux * Math.Round(excess2, 1);
//}
}
}
}
}
else
{
received_power = 0;
}

atmCurveIsp.FindMinMaxValue(out _, out float VacIsp);
maxThrust = (float)(2 * received_power * 1000 / (VacIsp * 9.8) * 0.4);
double fuelrate = maxThrust / (9.81 * 3 * VacIsp);
double TempChangePerSec = received_power * 1000 / (fuelrate * 1200) * 0.7;
throttleResponseRate = (float)(Math.Abs(GetCurrentThrust() - prevThrust)); // yet to complete
prevThrust = GetCurrentThrust();
}
}
}
170 changes: 113 additions & 57 deletions BeamedPowerReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace BeamedPowerStandalone
{
// Part module for spherical or multi-directional receivers
// code relating to planetary occlusion has been commented out for now
public class WirelessReceiver : PartModule
{
// UI-right click menu in flight
Expand All @@ -15,8 +14,20 @@ public class WirelessReceiver : PartModule
[KSPField(guiName = "Received Power Limiter", isPersistant = true, guiActive = true, guiActiveEditor = false), UI_FloatRange(minValue = 0, maxValue = 100, stepIncrement = 1, requireFullControl = true, scene = UI_Scene.Flight)]
public float percentagePower;

[KSPField(guiName = "Received Power", isPersistant = false, guiActive = true, guiActiveEditor = false, guiUnits = "EC/s")]
public float received_power_ui;
[KSPField(guiName = "Received Power", isPersistant = true, guiActive = true, guiActiveEditor = false, guiUnits = "EC/s")]
public float receivedPower;

[KSPField(guiName = "Status", isPersistant = false, guiActive = true, guiActiveEditor = false)]
public string state;

[KSPField(guiName = "Core Temperature", groupName = "HeatInfo", groupDisplayName = "Heat Info", groupStartCollapsed = false, guiActive = true, guiActiveEditor = false, isPersistant = false, guiUnits = "K/900K")]
public float coreTemp;

[KSPField(guiName = "Skin Temperature", groupName = "HeatInfo", guiActive = true, guiActiveEditor = false, isPersistant = false, guiUnits = "K/1200K")]
public float skinTemp;

[KSPField(guiName = "Waste Heat", groupName = "HeatInfo", guiActive = true, guiActiveEditor = false, isPersistant = false, guiUnits = "kW")]
public float wasteHeat;

// 'recv_diameter' and 'recv_efficiency' values are set in part cfg file
[KSPField(isPersistant = false)]
Expand All @@ -26,10 +37,10 @@ public class WirelessReceiver : PartModule
public float recvEfficiency;

// declaring frequently used variables
Vector3d source; Vector3d dest; double received_power; int frames;
Vector3d source; Vector3d dest; double received_power; int frames; int initFrames;
readonly int EChash = PartResourceLibrary.Instance.GetDefinition("ElectricCharge").id;
TransmitterVessel transmitter = new TransmitterVessel();
// BPOcclusion occlusion = new BPOcclusion();
VesselFinder vesselFinder = new VesselFinder(); ModuleCoreHeat coreHeat;
AnimationSync animation; //BPOcclusion occlusion = new BPOcclusion();

List<Vessel> CorrectVesselList;
List<double> excessList;
Expand All @@ -38,11 +49,24 @@ public class WirelessReceiver : PartModule

public void Start()
{
frames = 145;
frames = 145; initFrames = 0;
CorrectVesselList = new List<Vessel>();
excessList = new List<double>();
constantList = new List<double>();
targetList = new List<string>();
wavelength_ui = "Long";
animation = new AnimationSync();
SetHeatParams();
}

private void SetHeatParams()
{
this.part.AddModule("ModuleCoreHeat");
coreHeat = this.part.Modules.GetModule<ModuleCoreHeat>();
coreHeat.CoreTempGoal = 1300d; // placeholder value, there is no optimum temperature
coreHeat.CoolantTransferMultiplier *= 3d;
coreHeat.radiatorCoolingFactor *= 2d;
coreHeat.HeatRadiantMultiplier *= 2d;
}

// setting action group capability
Expand All @@ -67,17 +91,44 @@ public void DeactivateBPReceiver(KSPActionParam param)
// adding part info to part description tab in editor
public string GetModuleTitle()
{
return "Wireless Source";
return "WirelessReceiver";
}
public override string GetModuleDisplayName()
{
return "Beamed Power Receiver";
}
public override string GetInfo()
{
return ("Dish Diameter: " + Convert.ToString(recvDiameter) + "\n"
+ "Efficiency: " + Convert.ToString(recvEfficiency) + "\n"
+ "Receiver Type: Sphere");
return ("Receiver Type: Sphere" + "\n"
+ "Dish Diameter: " + Convert.ToString(recvDiameter) + "m" + "\n"
+ "Efficiency: " + Convert.ToString(recvEfficiency * 100) + "%" + "\n" + "" + "\n"
+ "Max Core Temp: 900K" + "\n"
+ "Max Skin Temp: 1200K" + "\n" + "" + "\n"
+ "This receiver will shutdown past these temperatures.");
}

private void AddHeatToCore()
{
coreTemp = (float)(Math.Round(coreHeat.CoreTemperature, 1));
skinTemp = (float)(Math.Round(this.part.skinTemperature, 1));
if (coreTemp > 900f | skinTemp > 1200f)
{
state = "Exceeded Temperature Limit";
Listening = (Listening) ? false : false;
}
if (state == "Exceeded Temperature Limit" & (coreTemp > 700f | skinTemp > 1000f))
{
Listening = (Listening) ? false : false;
}
if (coreTemp < 700f & skinTemp < 1000f)
{
state = "Operational";
}
double heatModifier = (double)HighLogic.CurrentGame.Parameters.CustomParams<BPSettings>().PercentHeat / 100;
double heatExcess = (1 - recvEfficiency) * (received_power / recvEfficiency) * heatModifier;
wasteHeat = (float)Math.Round(heatExcess, 1);
coreHeat.AddEnergyToCore(heatExcess * 0.3 * 3 * Time.fixedDeltaTime); // first converted to kJ
this.part.AddSkinThermalFlux(heatExcess * 0.7); // some heat added to skin
}

// main block of code - runs every physics frame
Expand All @@ -86,10 +137,19 @@ public void FixedUpdate()
frames += 1;
if (frames == 150)
{
transmitter.LoadVessels(out CorrectVesselList, out excessList, out constantList, out targetList, out _);
vesselFinder.SourceData(out CorrectVesselList, out excessList, out constantList, out targetList, out _);
frames = 0;
}

if (initFrames < 60)
{
initFrames += 1;
}
else
{
AddHeatToCore();
}
animation.SyncAnimationState(this.part);

if (CorrectVesselList.Count > 0)
{
if (Listening == true)
Expand All @@ -106,86 +166,82 @@ public void FixedUpdate()
source = CorrectVesselList[n].GetWorldPos3D();
double distance = Vector3d.Distance(source, dest);
double spotsize = constant2 * distance;
//occlusion.CheckIfOccluded(CorrectVesselList[n], this.vessel, out _, out bool occluded);

// adding EC that has been received
if (recvDiameter < spotsize)
{
//if (DirectionalClass.CheckifOccluded(CorrectVesselList[n], this.vessel)==false)
//if (occluded == false)
//{
received_power += Math.Round(((recvDiameter / spotsize) * recvEfficiency * excess2 * (percentagePower / 100)), 1);
//}
}
else
{
//if (DirectionalClass.CheckifOccluded(CorrectVesselList[n], this.vessel) == false)
//if (occluded == false)
//{
received_power += Math.Round(((recvEfficiency * excess2) * (percentagePower / 100)), 1);
//}
}
}
}
BPSettings settings = new BPSettings();
if (settings.BackgroundProcessing == false)

if (HighLogic.CurrentGame.Parameters.CustomParams<BPSettings>().BackgroundProcessing == false)
{
this.part.RequestResource(EChash, -received_power * Time.fixedDeltaTime);
}
received_power_ui = Convert.ToSingle(received_power);
receivedPower = Convert.ToSingle(received_power);
}
else
{
received_power_ui = 0;
receivedPower = 0;
received_power = 0;
}
}
else
{
received_power_ui = 0;
receivedPower = 0;
received_power = 0;
}
}
}

public class TransmitterVessel
{
// Loading all vessels that have WirelessSource module, and adding them to a list to use later
public void LoadVessels(out List<Vessel> list1, out List<double> list2, out List<double> list3, out List<string> list4, out List<string> list5)
// adds received power calculator to receivers right-click menu in editor

[KSPField(guiName = "Distance", groupName = "calculator2", groupDisplayName = "Received Power Calculator", groupStartCollapsed = true, guiUnits = "Mm", guiActive = false, guiActiveEditor = true), UI_FloatRange(minValue = 0, maxValue = 20000000, stepIncrement = 0.001f, scene = UI_Scene.Editor)]
public float dist_ui;

[KSPField(guiName = "Source Dish Diameter", groupName = "calculator2", guiUnits = "m", guiActive = false, guiActiveEditor = true), UI_FloatRange(minValue = 0, maxValue = 100, stepIncrement = 0.5f, scene = UI_Scene.Editor)]
public float dish_dia_ui;

[KSPField(guiName = "Source Efficiency", groupName = "calculator2", guiActive = false, guiActiveEditor = true, guiUnits = "%"), UI_FloatRange(minValue = 0, maxValue = 100, stepIncrement = 1, scene = UI_Scene.Editor)]
public float efficiency;

[KSPField(guiName = "Power Beamed", groupName = "calculator2", guiUnits = "EC/s", guiActive = false, guiActiveEditor = true), UI_FloatRange(minValue = 0, maxValue = 100000, stepIncrement = 1, scene = UI_Scene.Editor)]
public float beamedPower;

[KSPField(guiName = "Result", groupName = "calculator2", guiUnits = "EC/s", guiActive = false, guiActiveEditor = true)]
public float powerReceived;

[KSPField(guiName = "Beamed Wavelength", groupName = "calculator2", guiActiveEditor = true, guiActive = false)]
public string wavelength_ui;

[KSPEvent(guiName = "Toggle Wavelength", guiActive = false, guiActiveEditor = true, groupName = "calculator2")]
public void ToggleWavelength()
{
ConfigNode Node = ConfigNode.Load(KSPUtil.ApplicationRootPath + "saves/" + HighLogic.SaveFolder + "/persistent.sfs");
ConfigNode FlightNode = Node.GetNode("GAME").GetNode("FLIGHTSTATE");
list1 = new List<Vessel>(); list2 = new List<double>();
list3 = new List<double>(); list4 = new List<string>();
list5 = new List<string>();
wavelength_ui = (wavelength_ui == "Long") ? "Short" : "Long";
}

foreach (ConfigNode vesselnode in FlightNode.GetNodes("VESSEL"))
public void Update()
{
if (HighLogic.LoadedSceneIsEditor)
{
foreach (ConfigNode partnode in vesselnode.GetNodes("PART"))
{
if (partnode.HasNode("MODULE"))
{
foreach (ConfigNode module in partnode.GetNodes("MODULE"))
{
if (module.GetValue("name") == "WirelessSource")
{
list2.Add(Convert.ToDouble(module.GetValue("excess")));
list3.Add(Convert.ToDouble(module.GetValue("constant")));
list4.Add(module.GetValue("TransmittingTo"));
list5.Add(module.GetValue("Wavelength"));
foreach (Vessel vessel in FlightGlobals.Vessels)
{
if (vesselnode.GetValue("name") == vessel.GetDisplayName())
{
list1.Add(vessel);
break;
}
}
break;
}
}
}
}
float wavelength_num = (float)((wavelength_ui == "Long") ? Math.Pow(10, -3) : 5 * Math.Pow(10, -8));
float spot_size = (float)(1.44 * wavelength_num * dist_ui * 1000000 / dish_dia_ui);
powerReceived = (spot_size > recvDiameter) ?
recvDiameter / spot_size * beamedPower * (efficiency / 100) * recvEfficiency : beamedPower * (efficiency / 100) * recvEfficiency;
powerReceived = (float)Math.Round(powerReceived, 1);
}
}

}
}

Loading

0 comments on commit 67eef30

Please sign in to comment.