Skip to content

Commit

Permalink
#612 initial PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
Sn1p3rr3c0n committed Aug 19, 2022
1 parent c8380c3 commit f950f9c
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 304 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,14 @@ 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);
//Why is this here ??? seems to work just fine without it...
// 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);
// this.Map.GetComponent<PRFMapComponent>().DeRegisterIHideItemPos(this.Position,this);
base.DeSpawn(mode);

targets.ForEach(x => x.Unlink(this));
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.Position == thingPos;
}


Expand Down
132 changes: 1 addition & 131 deletions Source/ProjectRimFactory/Common/HarmonyPatches/PatchStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,138 +12,8 @@

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)
{
__result = true;
if (t != null)
{
Map thingmap = t.Map;
if (thingmap != null && t.def.category == ThingCategory.Item)
{
if (PatchStorageUtil.GetPRFMapComponent(thingmap)?.ShouldForbidPawnOutputAtPos(t.Position) ?? false)
{
return false;
}
}
}
return true;
}
}

[HarmonyPatch(typeof(Building_Storage), "Accepts")]
class Patch_Building_Storage_Accepts
{
static bool Prefix(Building_Storage __instance, Thing t, out bool __result)
{
__result = false;
if ((__instance as IForbidPawnInputItem)?.ForbidPawnInput ?? false)
{
if (!__instance.slotGroup.HeldThings.Contains(t))
{
return false;
}
}
return true;
}
}

[HarmonyPatch(typeof(FloatMenuMakerMap), "ChoicesAtFor")]
class Patch_FloatMenuMakerMap_ChoicesAtFor
{
static bool Prefix(Vector3 clickPos, Pawn pawn, out List<FloatMenuOption> __result)
{
if (pawn.Map.GetComponent<PRFMapComponent>().iHideRightMenus.Contains(clickPos.ToIntVec3()))
{
__result = new List<FloatMenuOption>();
return false;
}
__result = null;
return true;
}
}

[HarmonyPatch(typeof(Thing), "DrawGUIOverlay")]
class Patch_Thing_DrawGUIOverlay
{
static bool Prefix(Thing __instance)
{
if (__instance.def.category == ThingCategory.Item)
{
if (PatchStorageUtil.GetPRFMapComponent(__instance.Map)?.ShouldHideItemsAtPos(__instance.Position) ?? false)
{
return false;
}
}
return true;
}
}

[HarmonyPatch(typeof(ThingWithComps), "DrawGUIOverlay")]
class Patch_ThingWithComps_DrawGUIOverlay
{
static bool Prefix(Thing __instance)
{
if (__instance.def.category == ThingCategory.Item)
{
if (PatchStorageUtil.GetPRFMapComponent(__instance.Map)?.ShouldHideItemsAtPos(__instance.Position) ?? false)
{
return false;
}
}
return true;
}
}

[HarmonyPatch(typeof(ThingWithComps), "Draw")]
class Patch_ThingWithComps_Draw
{
static bool Prefix(Thing __instance)
{
if (__instance.def.category == ThingCategory.Item)
{
if (PatchStorageUtil.GetPRFMapComponent(__instance.Map)?.ShouldHideItemsAtPos(__instance.Position) ?? false)
{
return false;
}
}
return true;
}
}

[HarmonyPatch(typeof(Thing), "Print")]
class Patch_Thing_Print
{
static bool Prefix(Thing __instance, SectionLayer layer)
{
if (__instance.def.category == ThingCategory.Item)
{
if (PatchStorageUtil.GetPRFMapComponent(__instance.Map)?.ShouldHideItemsAtPos(__instance.Position) ?? false)
{
return false;
}
}
return true;
}
}

[HarmonyPatch(typeof(MinifiedThing), "Print")]
class Patch_MinifiedThing_Print
{
static bool Prefix(Thing __instance, SectionLayer layer)
{
if (__instance.def.category == ThingCategory.Item)
{
if (PatchStorageUtil.GetPRFMapComponent(__instance.Map)?.ShouldHideItemsAtPos(__instance.Position) ?? false)
{
return false;
}
}
return true;
}
}
//The DSU Pawn Access coud / Must be added somwhere else

static class PatchStorageUtil
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HarmonyLib;
using ProjectRimFactory.Storage;
using RimWorld;
using Verse;
using Verse.AI;
using System.Reflection.Emit;

namespace ProjectRimFactory.Common.HarmonyPatches
{
[HarmonyPatch(typeof(Verse.AI.HaulAIUtility), "HaulToStorageJob")]
class Patch_HaulAIUtility_HaulToStorageJob
{

static bool foundPos = false;
static int cnt = 0;


static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{

Label end = generator.DefineLabel();
Label preend = generator.DefineLabel();

foreach (var instruction in instructions)
{

if (instruction.opcode == OpCodes.Brfalse_S) cnt++;

if (instruction.opcode == OpCodes.Brfalse_S && (cnt == 2 || cnt == 3))
{
yield return new CodeInstruction(OpCodes.Brfalse_S, preend);
continue;
}



if (!foundPos && instruction.opcode == OpCodes.Ldstr)
{
foundPos = true;
CodeInstruction code = new CodeInstruction(OpCodes.Nop);
code.labels.Add(preend);

yield return code;

yield return new CodeInstruction(OpCodes.Ldloc_2);

yield return new CodeInstruction(OpCodes.Call, AccessTools.Method(
typeof(Patch_HaulAIUtility_HaulToStorageJob),
nameof(Patch_HaulAIUtility_HaulToStorageJob.IsDSU), new[] { typeof(IHaulDestination) }));

yield return new CodeInstruction(OpCodes.Brfalse_S, end);



yield return new CodeInstruction(OpCodes.Ldarg_0);
yield return new CodeInstruction(OpCodes.Ldarg_1);

yield return new CodeInstruction(OpCodes.Ldloc_2);

yield return new CodeInstruction(OpCodes.Call, AccessTools.Method(
typeof(Patch_HaulAIUtility_HaulToStorageJob),
nameof(Patch_HaulAIUtility_HaulToStorageJob.HaulToCellStorageJob), new[] { typeof(Pawn) , typeof(Thing) , typeof(IHaulDestination) }));
yield return new CodeInstruction(OpCodes.Ret);


instruction.labels.Add(end);
//yield return new CodeInstruction(end);


}



yield return instruction;
}

}

public static Job HaulToCellStorageJob(Pawn p, Thing t, IHaulDestination dest)
{
//Probaly not that good
//When do i register it?
Building_MassStorageUnit dsu = dest as Building_MassStorageUnit;

Job job = JobMaker.MakeJob(JobDefOf.HaulToContainer, t, dsu);



job.count = 99999;

job.haulOpportunisticDuplicates = true;
job.haulMode = HaulMode.ToCellStorage;



return job;
}

public static bool IsDSU(IHaulDestination destination)
{
return (destination as Building_MassStorageUnit) != null;
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public static void Prefix(Thing __instance, out Building_MassStorageUnit __state
__state = pos.GetFirst<Building_MassStorageUnit>(__instance.Map);
}
}
public static void Postfix(Thing __instance, Building_MassStorageUnit __state)
/* public static void Postfix(Thing __instance, Building_MassStorageUnit __state)
{
__state?.Notify_LostThing(__instance);
}
}*/
}
}
40 changes: 2 additions & 38 deletions Source/ProjectRimFactory/Common/PRFMapComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,7 @@ public void DeRegisteradvancedIOLocations(IntVec3 pos)

}

public void RegisterIHideItemPos(IntVec3 pos, HarmonyPatches.IHideItem hideItem)
{
if(hideItemLocations.ContainsKey(pos))
{
hideItemLocations[pos].Add(hideItem);
}
else
{
hideItemLocations.Add(pos,new List<HarmonyPatches.IHideItem>() { hideItem});
}
}
public void DeRegisterIHideItemPos(IntVec3 pos, HarmonyPatches.IHideItem hideItem)
{
if (hideItemLocations[pos].Count <= 1)
{
hideItemLocations.Remove(pos);
}
else
{
hideItemLocations[pos].Remove(hideItem);
}

}

public void RegisterIForbidPawnOutputItem(IntVec3 pos, HarmonyPatches.IForbidPawnOutputItem ForbidPawnOutput)
{
if (ForbidPawnOutputItemLocations.ContainsKey(pos))
Expand Down Expand Up @@ -128,21 +106,7 @@ public void RemoveTicker(ITicker ticker)
{
this.tickers.Remove(ticker);
}
public void AddIHideRightClickMenu(Building b)
{
foreach (var v in b.OccupiedRect()) {
iHideRightMenus.Add(v);
}
}
public void RemoveIHideRightClickMenu(Building b)
{
foreach (var v in b.OccupiedRect()) {
iHideRightMenus.Remove(v);
foreach (var t in map.thingGrid.ThingsListAt(v)) {
if (t != b && t is IHideRightClickMenu) iHideRightMenus.Add(v);
}
}
}

}

public interface ITicker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private void HandleDepositWithdrawRequest()
for (int i = 0; i < output.Count; i++)
{
GenPlace.TryPlaceThing(output[i], SelBuilding.Position, SelBuilding.Map, ThingPlaceMode.Direct);
SelBuilding.boundStorageUnit.Notify_ReceivedThing(output[i]);
SelBuilding.boundStorageUnit.RegisterNewItem(output[i]);
}
SelBuilding.WithdrawPaperclips(result);
Messages.Message("SuccessfullyWithdrawnPaperclips".Translate(result), MessageTypeDefOf.PositiveEvent);
Expand Down
2 changes: 1 addition & 1 deletion Source/ProjectRimFactory/Storage/Building_ItemSlide.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public bool AcceptsThing(Thing newThing, IPRF_Building giver = null)
{

if (base.Accepts(newThing)){
Notify_ReceivedThing(newThing);
//Notify_ReceivedThing(newThing);
return true;
}
else
Expand Down
Loading

0 comments on commit f950f9c

Please sign in to comment.