Skip to content

Commit

Permalink
Merge pull request #614 from zymex22/issue612opt2
Browse files Browse the repository at this point in the history
Added Cold Storage Unit
  • Loading branch information
Sn1p3rr3c0n authored Sep 18, 2022
2 parents c8380c3 + 6ef4757 commit 1eadbe8
Show file tree
Hide file tree
Showing 24 changed files with 1,055 additions and 113 deletions.
115 changes: 115 additions & 0 deletions Defs/ThingDefs_Buildings/Buildings_Storage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,121 @@
</li>
</modExtensions>
</ThingDef>

<!--==================== Cold Storage ====================-->
<ThingDef ParentName="PRF_BuildingBase">
<thingClass>ProjectRimFactory.Storage.Building_ColdStoragePowered</thingClass>
<defName>PRF_ColdStorageUnit_I</defName>
<label>Cold Storage Unit</label>
<description>A matter-energy conversion based storage unit which can hold up to 10k stacks, consuming 10 W of power per stack of items. In the event of a power outage, items already inside will be safe, but the Cold Storage Unit will not be able to store more. Don't break it.\n\n<i>Items stored in this Device are not spawned and therefore won't be automatically accessible by your pawns. Use I/O Ports to interact with it.</i></description>
<designatorDropdown>PRF_IoGroup</designatorDropdown>
<uiIconScale>0.9</uiIconScale>
<tradeTags>
<li>Furniture</li>
</tradeTags>
<altitudeLayer>Building</altitudeLayer>
<passability>Impassable</passability>
<blockWind>true</blockWind>
<fillPercent>1.0</fillPercent>
<castEdgeShadows>true</castEdgeShadows>
<rotatable>false</rotatable>
<canOverlapZones>false</canOverlapZones>
<drawGUIOverlay>true</drawGUIOverlay>
<minifiedDef>MinifiedThing</minifiedDef>
<terrainAffordanceNeeded>Heavy</terrainAffordanceNeeded>
<staticSunShadowHeight>0.5</staticSunShadowHeight>
<surfaceType>Item</surfaceType>
<inspectorTabs>
<li>ITab_Storage</li>
<li>ProjectRimFactory.Storage.UI.ITab_Items</li>
</inspectorTabs>
<graphicData>
<graphicClass>Graphic_Single</graphicClass>
<texPath>Storage/ColdStorageUnit</texPath>
<drawSize>(3,3)</drawSize>
<damageData>
<scratches>
</scratches>
<!-- <rect>(0.1,0,3,2.5)</rect> -->
<rect>(0.1,0,2.8,2.8)</rect>

<cornerTL>Damage/Corner</cornerTL>
<cornerBR>Damage/Corner</cornerBR>
<cornerBL>Damage/Corner</cornerBL>
<!-- <edgeBot>Damage/Edge</edgeBot> -->
</damageData>

</graphicData>
<statBases>
<Mass>50</Mass>
<MaxHitPoints>450</MaxHitPoints>
<WorkToBuild>5000</WorkToBuild>
<MarketValue>10880</MarketValue>
<Flammability>0.05</Flammability>
<Beauty>0.5</Beauty>
</statBases>
<building>
<preventDeteriorationOnTop>true</preventDeteriorationOnTop>
<ignoreStoredThingsBeauty>true</ignoreStoredThingsBeauty>
<defaultStorageSettings>
<priority>Normal</priority>
<filter>
<categories>
<li>Manufactured</li>
<li>ResourcesRaw</li>
<li>Items</li>
<li>BuildingsArt</li>
<li>Weapons</li>
<li>Apparel</li>
<li>BodyParts</li>
</categories>
<specialFiltersToDisallow>
<li>AllowRotten</li> <!-- Disallow rotten by default -->
</specialFiltersToDisallow>
</filter>
</defaultStorageSettings>
</building>
<size>(3,3)</size>
<pathCost>150</pathCost>
<costList>
<Steel>300</Steel>
<Gold>50</Gold>
<Uranium>20</Uranium>
<Plasteel>100</Plasteel>
<ComponentIndustrial>90</ComponentIndustrial>
<ComponentSpacer>20</ComponentSpacer>
<PRF_MachineFrame_III>1</PRF_MachineFrame_III>
<PRF_ElectronicChip_II>1</PRF_ElectronicChip_II>
</costList>
<tickerType>Normal</tickerType>
<researchPrerequisites>
<li>PRF_StorageIO_II</li>
</researchPrerequisites>
<comps>
<li Class="CompProperties_Glower">
<glowRadius>6</glowRadius>
<glowColor>(115,198,206,0)</glowColor>
</li>
<li Class="CompProperties_Power">
<compClass>CompPowerTrader</compClass>
<shortCircuitInRain>true</shortCircuitInRain>
<basePowerConsumption>0</basePowerConsumption>
</li>
<li Class="ProjectRimFactory.Common.CompProperties_CompOutputAdjustable"/>
<li Class="CompProperties_Flickable"/>
</comps>
<constructionSkillPrerequisite>10</constructionSkillPrerequisite>
<modExtensions>
<li Class="ProjectRimFactory.Storage.Editables.DefModExtension_CanUseStorageIOPorts" />
<li Class="ProjectRimFactory.Storage.Editables.DefModExtension_Crate">
<limit>10000</limit>
<destroyContainsItems>true</destroyContainsItems>
<hideItems>true</hideItems>
<forbidPawnAccess>false</forbidPawnAccess>
<hideRightClickMenus>true</hideRightClickMenus>
</li>
</modExtensions>
</ThingDef>

<!--============================== I/O Port ==============================-->
<ThingDef ParentName="PRF_BuildingBase">
Expand Down
2 changes: 2 additions & 0 deletions Languages/English/Keyed/AllKeyed_Settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<PRF_Settings_C_Patches_Header>Patches</PRF_Settings_C_Patches_Header>
<PRF_Settings_C_Patches_Reachability_CanReach_ToolTip>Toggels the Advanced IO CanReach Patch\nWhen checked the Patch is active and allowes Pawns to reach DSU contents using Advanced IO Ports even if the Path to the DSU is blocked\nPatch is not active by default as it may cause spikes and is only usefull in some cases.\n(ProjectRimFactory.Common.HarmonyPatches.Patch_Reachability_CanReach:Prefix)</PRF_Settings_C_Patches_Reachability_CanReach_ToolTip>
<PRF_Settings_C_Patches_Reachability_CanReach>Advanced IO CanReach</PRF_Settings_C_Patches_Reachability_CanReach>
<PRF_Settings_C_Patches_WealthWatcher_CalculateWealthItems_ToolTip>Toggels the Cold Storage WealthWatcher Patch\nWhen checked the Patch is active and items in Coldstorage contribute to Colony Wealth.\nFor most Players we reccomend this to be on.\nPeformence load on this is ~1ms per 1k Things stored. This runs every 5k Ticks\n(ProjectRimFactory.Common.HarmonyPatches.Patch_WealthWatcher_CalculateWealthItems:Postfix)</PRF_Settings_C_Patches_WealthWatcher_CalculateWealthItems_ToolTip>
<PRF_Settings_C_Patches_WealthWatcher_CalculateWealthItems>Cold Storage WealthWatcher</PRF_Settings_C_Patches_WealthWatcher_CalculateWealthItems>

<PRF.Common.ForbidOnPlacingDefault>Forbid items on Placment</PRF.Common.ForbidOnPlacingDefault>
<PRF.Common.ForbidOnPlacingDefaultDesc>Toggle this to forbid items on placement</PRF.Common.ForbidOnPlacingDefaultDesc>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,11 @@ public override void SpawnSetup(Map map, bool respawningAfterLoad) {
}
// already set, but just in case:
this.products = thingOwnerInt.InnerListForReading;
map.GetComponent<PRFMapComponent>().RegisterIHideItemPos(this.Position, this);
}

public override void DeSpawn(DestroyMode mode = DestroyMode.Vanish)
{
var targets = AllNearbyLinkables().ToList();
this.Map.GetComponent<PRFMapComponent>().DeRegisterIHideItemPos(this.Position,this);
base.DeSpawn(mode);

targets.ForEach(x => x.Unlink(this));
Expand Down
129 changes: 115 additions & 14 deletions Source/ProjectRimFactory/Common/ConditionalPatchHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,136 @@
using Verse;
using HarmonyLib;
using Verse.AI;
using ProjectRimFactory.Storage;
using System.Reflection;

namespace ProjectRimFactory.Common
{
public static class ConditionalPatchHelper
{
public class TogglePatch
{
private bool Patched = false;

private readonly MethodInfo base_m;
private readonly HarmonyMethod trans_hm = null;
private readonly HarmonyMethod pre_hm = null;
private readonly HarmonyMethod post_hm = null;
private readonly MethodInfo trans_m = null;
private readonly MethodInfo pre_m = null;
private readonly MethodInfo post_m = null;

public TogglePatch(MethodInfo base_method, MethodInfo prefix = null, MethodInfo postfix = null, MethodInfo Transpiler = null)
{
base_m = base_method;
if (Transpiler != null) trans_hm = new HarmonyMethod(Transpiler);
if (prefix != null) pre_hm = new HarmonyMethod(prefix);
if (postfix != null) post_hm = new HarmonyMethod(postfix);
trans_m = Transpiler;
pre_m = prefix;
post_m = postfix;
}

public void PatchHandler(bool patch)
{
if (patch && !Patched)
{
harmony_instance.Patch(base_m,pre_hm,post_hm,trans_hm);
Patched = true;
}
else if (Patched && !patch)
{
if (trans_m != null) harmony_instance.Unpatch(base_m, trans_m);
if (pre_m != null) harmony_instance.Unpatch(base_m, pre_m);
if (post_m != null) harmony_instance.Unpatch(base_m, post_m);
Patched = false;
}
}

}

//conditional
private static Harmony harmony_instance = null;

private static bool Patch_Reachability_CanReach = false;
public static TogglePatch Patch_Reachability_CanReach = new TogglePatch(
AccessTools.Method(typeof(Verse.Reachability), "CanReach", new Type[] { typeof(IntVec3), typeof(LocalTargetInfo), typeof(PathEndMode), typeof(TraverseParms) }),
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_Reachability_CanReach), "Prefix")
);

public static TogglePatch Patch_WealthWatcher_CalculateWealthItems = new TogglePatch(
AccessTools.Method(typeof(RimWorld.WealthWatcher), "CalculateWealthItems"),
null,
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_WealthWatcher_CalculateWealthItems), "Postfix")
);

//Storage Patches
public static TogglePatch Patch_MinifiedThing_Print = new TogglePatch(
AccessTools.Method(typeof(RimWorld.MinifiedThing), "Print", new Type[] { typeof(SectionLayer)}),
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_MinifiedThing_Print), "Prefix")
);
public static TogglePatch Patch_Thing_Print = new TogglePatch(
AccessTools.Method(typeof(Verse.Thing), "Print", new Type[] { typeof(SectionLayer) }),
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_Thing_Print), "Prefix")
);
public static TogglePatch Patch_ThingWithComps_Draw = new TogglePatch(
AccessTools.Method(typeof(Verse.ThingWithComps), "Draw"),
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_ThingWithComps_Draw), "Prefix")
);
public static TogglePatch Patch_ThingWithComps_DrawGUIOverlay = new TogglePatch(
AccessTools.Method(typeof(Verse.ThingWithComps), "DrawGUIOverlay"),
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_ThingWithComps_DrawGUIOverlay), "Prefix")
);
public static TogglePatch Patch_Thing_DrawGUIOverlay = new TogglePatch(
AccessTools.Method(typeof(Verse.Thing), "DrawGUIOverlay"),
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_Thing_DrawGUIOverlay), "Prefix")
);
public static TogglePatch Patch_FloatMenuMakerMap_ChoicesAtFor = new TogglePatch(
AccessTools.Method(typeof(RimWorld.FloatMenuMakerMap), "ChoicesAtFor", new Type[] { typeof(UnityEngine.Vector3), typeof(Pawn), typeof(bool) }),
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_FloatMenuMakerMap_ChoicesAtFor), "Prefix")
);
public static TogglePatch Patch_Building_Storage_Accepts = new TogglePatch(
AccessTools.Method(typeof(RimWorld.Building_Storage), "Accepts", new Type[] { typeof(Verse.Thing)}),
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_Building_Storage_Accepts), "Prefix")
);
public static TogglePatch Patch_ForbidUtility_IsForbidden = new TogglePatch(
AccessTools.Method(typeof(RimWorld.ForbidUtility), "IsForbidden", new Type[] { typeof(Thing), typeof(Pawn) }),
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_ForbidUtility_IsForbidden), "Prefix")
);





public static void InitHarmony(Harmony harmony)
{
harmony_instance = harmony;
}

public static void Update_Patch_Reachability_CanReach()
static List<Building_MassStorageUnit> building_MassStorages = new List<Building_MassStorageUnit>();

private static void updatePatchStorage()
{
var patch = AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_Reachability_CanReach), "Prefix");
var base_m = AccessTools.Method(typeof(Verse.Reachability), "CanReach", new Type[] { typeof(IntVec3), typeof(LocalTargetInfo), typeof(PathEndMode), typeof(TraverseParms) });
if (ProjectRimFactory_ModSettings.PRF_Patch_Reachability_CanReach)
{
harmony_instance.Patch(base_m, new HarmonyMethod(patch) );
Patch_Reachability_CanReach = true;
}
else if(Patch_Reachability_CanReach)
{
harmony_instance.Unpatch(base_m, patch);
Patch_Reachability_CanReach = false;
}
bool state = building_MassStorages.Count > 0;

Patch_MinifiedThing_Print.PatchHandler(state);
Patch_Thing_Print.PatchHandler(state);
Patch_ThingWithComps_Draw.PatchHandler(state);
Patch_ThingWithComps_DrawGUIOverlay.PatchHandler(state);
Patch_Thing_DrawGUIOverlay.PatchHandler(state);
Patch_FloatMenuMakerMap_ChoicesAtFor.PatchHandler(state);
Patch_Building_Storage_Accepts.PatchHandler(state);
Patch_ForbidUtility_IsForbidden.PatchHandler(state);
}

public static void Register(Building_MassStorageUnit building)
{
building_MassStorages.Add(building);
updatePatchStorage();
}
public static void Deregister(Building_MassStorageUnit building)
{
building_MassStorages.Remove(building);
updatePatchStorage();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static bool CanMoveItem(Building_AdvancedStorageUnitIOPort port, Thing th
/// <returns></returns>
public static bool CanMoveItem(Building_AdvancedStorageUnitIOPort port, IntVec3 thingPos)
{
return port.boundStorageUnit?.AllSlotCells()?.Contains(thingPos) ?? false;
return port.boundStorageUnit?.HoldsPos(thingPos) ?? false;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

namespace ProjectRimFactory.Common.HarmonyPatches
{
[HarmonyPatch(typeof(ForbidUtility), "IsForbidden", new Type[] { typeof(Thing), typeof(Pawn) })]
class Patch_ForbidUtility_IsForbidden
{
static bool Prefix(Thing t, Pawn pawn, out bool __result)
Expand All @@ -33,7 +32,6 @@ static bool Prefix(Thing t, Pawn pawn, out bool __result)
}
}

[HarmonyPatch(typeof(Building_Storage), "Accepts")]
class Patch_Building_Storage_Accepts
{
static bool Prefix(Building_Storage __instance, Thing t, out bool __result)
Expand All @@ -50,7 +48,6 @@ static bool Prefix(Building_Storage __instance, Thing t, out bool __result)
}
}

[HarmonyPatch(typeof(FloatMenuMakerMap), "ChoicesAtFor")]
class Patch_FloatMenuMakerMap_ChoicesAtFor
{
static bool Prefix(Vector3 clickPos, Pawn pawn, out List<FloatMenuOption> __result)
Expand All @@ -65,7 +62,6 @@ static bool Prefix(Vector3 clickPos, Pawn pawn, out List<FloatMenuOption> __resu
}
}

[HarmonyPatch(typeof(Thing), "DrawGUIOverlay")]
class Patch_Thing_DrawGUIOverlay
{
static bool Prefix(Thing __instance)
Expand All @@ -81,7 +77,6 @@ static bool Prefix(Thing __instance)
}
}

[HarmonyPatch(typeof(ThingWithComps), "DrawGUIOverlay")]
class Patch_ThingWithComps_DrawGUIOverlay
{
static bool Prefix(Thing __instance)
Expand All @@ -97,7 +92,6 @@ static bool Prefix(Thing __instance)
}
}

[HarmonyPatch(typeof(ThingWithComps), "Draw")]
class Patch_ThingWithComps_Draw
{
static bool Prefix(Thing __instance)
Expand All @@ -113,7 +107,6 @@ static bool Prefix(Thing __instance)
}
}

[HarmonyPatch(typeof(Thing), "Print")]
class Patch_Thing_Print
{
static bool Prefix(Thing __instance, SectionLayer layer)
Expand All @@ -129,7 +122,6 @@ static bool Prefix(Thing __instance, SectionLayer layer)
}
}

[HarmonyPatch(typeof(MinifiedThing), "Print")]
class Patch_MinifiedThing_Print
{
static bool Prefix(Thing __instance, SectionLayer layer)
Expand Down
Loading

0 comments on commit 1eadbe8

Please sign in to comment.