Skip to content

Commit

Permalink
Bump to RimWorld 1.5.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratysz committed Apr 20, 2024
1 parent 90e9273 commit cae65f3
Show file tree
Hide file tree
Showing 9 changed files with 448 additions and 2 deletions.
1 change: 1 addition & 0 deletions About/About.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<li>1.2</li>
<li>1.3</li>
<li>1.4</li>
<li>1.5</li>
</supportedVersions>
<description>A new researchable (RT Mods research tab) function for the power switch: when enabled, the switch will automatically turn on when an adjacent powernet is about to run out of power, and turn off once the batteries are all full.

Expand Down
5 changes: 3 additions & 2 deletions About/Manifest.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Manifest>
<identifier>RT_PowerSwitch</identifier>
<version>1.5.0.0</version>
<version>1.6.0.0</version>
<targetVersions>
<li>1.0.0</li>
<li>1.1.0</li>
<li>1.2.0</li>
<li>1.3.0</li>
<li>1.4.0</li>
<li>1.5.0</li>
</targetVersions>
<suggests>
<li>RT_Fuse</li>
</suggests>
<showCrossPromotions>true</showCrossPromotions>
<manifestUri>https://raw.githubusercontent.com/Ratysz/RT_PowerSwitch/master/About/Manifest.xml</manifestUri>
<downloadUri>https://github.com/Ratysz/RT_PowerSwitch/releases/1.4-1.5.0</downloadUri>
<downloadUri>https://github.com/Ratysz/RT_PowerSwitch/releases/1.5-1.6.0</downloadUri>
</Manifest>
Binary file modified Assemblies/RT_PowerSwitch.dll
Binary file not shown.
4 changes: 4 additions & 0 deletions LoadFolders.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@
<li>/</li>
<li>v1.4</li>
</v1.4>
<v1.5>
<li>/</li>
<li>v1.5</li>
</v1.5>
</loadFolders>
Binary file added v1.5/Assemblies/RT_PowerSwitch.dll
Binary file not shown.
12 changes: 12 additions & 0 deletions v1.5/Source/CompProperties_RTPowerSwitch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Verse;

namespace RT_PowerSwitch
{
public class CompProperties_RTPowerSwitch : CompProperties
{
public CompProperties_RTPowerSwitch()
{
compClass = typeof(CompRTPowerSwitch);
}
}
}
282 changes: 282 additions & 0 deletions v1.5/Source/CompRTPowerSwitch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
using RimWorld;
using System.Collections.Generic;
using System.Text;
using Verse;

namespace RT_PowerSwitch
{
public class CompRTPowerSwitch : ThingComp
{
private CompProperties_RTPowerSwitch Properties
{
get
{
return (CompProperties_RTPowerSwitch)props;
}
}

private static bool emergencyPowerResearchCompleted_cache = false;
private static bool emergencyPowerResearchCompleted_dirty = true;

private static bool EmergencyPowerResearchCompleted
{
get
{
if (emergencyPowerResearchCompleted_dirty)
{
emergencyPowerResearchCompleted_cache =
DefDatabase<ResearchProjectDef>.GetNamed("ResearchProject_RTEmergencyPower").IsFinished;
}
return emergencyPowerResearchCompleted_cache;
}
}

private static readonly System.Reflection.FieldInfo wantsSwitchOnField = typeof(CompFlickable).GetField(
"wantSwitchOn",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

private CompFlickable compFlickable;

private int tickStagger;
private static int lastTickStagger;

public bool emergencyPowerEnabled = false;
public float kickInPercentage = 1.0f;
public float shutOffPercentage = 100.0f;
public Direction8Way directionToMonitor = Direction8Way.North;
public bool usePercentages = true;

private IntVec3 MonitoredCell
{
get
{
var cell = parent.Position;
switch (directionToMonitor)
{
case Direction8Way.North:
{
cell.z += 1;
break;
}
case Direction8Way.South:
{
cell.z -= 1;
break;
}
case Direction8Way.West:
{
cell.x -= 1;
break;
}
case Direction8Way.East:
{
cell.x += 1;
break;
}
}
return cell;
}
}

public override void PostSpawnSetup(bool respawningAfterLoad)
{
base.PostSpawnSetup(respawningAfterLoad);

lastTickStagger++;
tickStagger = lastTickStagger;

compFlickable = parent.TryGetComp<CompFlickable>();
if (compFlickable == null)
{
Log.Error("CompRTPowerSwitch could not get parent's CompFlickable!");
}

emergencyPowerResearchCompleted_dirty = true;
}

public override string CompInspectStringExtra()
{
StringBuilder stringBuilder = new StringBuilder();
if (emergencyPowerEnabled)
{
PowerNet powerNet = parent.Map.powerNetGrid.TransmittedPowerNetAt(MonitoredCell);
if (powerNet != null)
{
float maxEnergy = 0;
float storedEnergy = 0;
foreach (CompPowerBattery compPowerBattery in powerNet.batteryComps)
{
maxEnergy += compPowerBattery.Props.storedEnergyMax;
storedEnergy += compPowerBattery.StoredEnergy;
}
if (maxEnergy > 0.0f)
{
stringBuilder.Append("CompRTPowerSwitch_MonitoredNetwork".Translate(
storedEnergy.ToString("N0"),
maxEnergy.ToString(),
(storedEnergy / maxEnergy).ToStringPercent()));
}
else
{
stringBuilder.Append("CompRTPowerSwitch_MonitoredNetworkInvalid".Translate());
}
}
else
{
stringBuilder.Append("CompRTPowerSwitch_MonitoredNetworkInvalid".Translate());
}
}
return stringBuilder.ToString();
}

public override void CompTick()
{
PowerSwitchTick(5);
}

public override IEnumerable<Gizmo> CompGetGizmosExtra()
{
if (EmergencyPowerResearchCompleted)
{
Command_Toggle command = new Command_Toggle
{
isActive = () => emergencyPowerEnabled,
toggleAction = () =>
{
emergencyPowerEnabled = !emergencyPowerEnabled;
},
icon = Resources.emergencyPowerButtonTexture,
defaultLabel = "CompRTPowerSwitch_EmergencyPowerToggle".Translate()
};
if (emergencyPowerEnabled)
{
command.defaultDesc = "CompRTPowerSwitch_EmergencyPowerOn".Translate();
}
else
{
command.defaultDesc = "CompRTPowerSwitch_EmergencyPowerOff".Translate();
}
yield return command;
}
}

public override void PostExposeData()
{
Scribe_Values.Look(ref emergencyPowerEnabled, "emergencyPowerEnabled", false);
Scribe_Values.Look(ref kickInPercentage, "kickInPercentage", 1.0f);
Scribe_Values.Look(ref shutOffPercentage, "shutOffPercentage", 100.0f);
Scribe_Values.Look(ref directionToMonitor, "directionToMonitor", Direction8Way.North);
Scribe_Values.Look(ref usePercentages, "usePercentages", true);
}

private void PowerSwitchTick(int tickAmount)
{
if ((Find.TickManager.TicksGame + tickStagger) % tickAmount == 0)
{
if (emergencyPowerEnabled)
{
ProcessCell(MonitoredCell);
}
}
}

public void ProcessCell(IntVec3 cell)
{
PowerNet powerNet = parent.Map.powerNetGrid.TransmittedPowerNetAt(cell);
if (powerNet != null)
{
float maxEnergy = 0;
float storedEnergy = 0;
foreach (CompPowerBattery compPowerBattery in powerNet.batteryComps)
{
maxEnergy += compPowerBattery.Props.storedEnergyMax;
storedEnergy += compPowerBattery.StoredEnergy;
}
if (maxEnergy > 0.0f)
{
if (shutOffPercentage > kickInPercentage)
{
if (compFlickable.SwitchIsOn)
{
if (usePercentages)
{
if (storedEnergy >= maxEnergy * 0.01f * shutOffPercentage)
{
SetFlicked(false);
}
}
else
{
if (storedEnergy >= shutOffPercentage)
{
SetFlicked(false);
}
}
}
else
{
if (usePercentages)
{
if (storedEnergy <= maxEnergy * 0.01f * kickInPercentage)
{
SetFlicked(true);
}
}
else
{
if (storedEnergy <= kickInPercentage)
{
SetFlicked(true);
}
}
}
}
else
{
if (compFlickable.SwitchIsOn)
{
if (usePercentages)
{
if (storedEnergy <= maxEnergy * 0.01f * shutOffPercentage)
{
SetFlicked(false);
}
}
else
{
if (storedEnergy <= shutOffPercentage)
{
SetFlicked(false);
}
}
}
else
{
if (usePercentages)
{
if (storedEnergy >= maxEnergy * 0.01f * kickInPercentage)
{
SetFlicked(true);
}
}
else
{
if (storedEnergy >= kickInPercentage)
{
SetFlicked(true);
}
}
}
}
}
}
}

private void SetFlicked(bool flicked)
{
wantsSwitchOnField.SetValue(compFlickable, flicked);
compFlickable.SwitchIsOn = flicked;
FlickUtility.UpdateFlickDesignation(parent);
}
}
}
Loading

0 comments on commit cae65f3

Please sign in to comment.