Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Source/Client/Comp/Map/FactionMapData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Multiplayer.Client.Factions;
using Multiplayer.Client.Persistent;
using RimWorld;
using Verse;

Expand All @@ -23,6 +24,7 @@ public class FactionMapData : IExposable
public ListerFilthInHomeArea listerFilthInHomeArea;
public ListerMergeables listerMergeables;

public GravshipCache gravshipCache;
private FactionMapData() { }

// Loading ctor
Expand All @@ -35,6 +37,8 @@ public FactionMapData(Map map)
resourceCounter = new ResourceCounter(map);
listerFilthInHomeArea = new ListerFilthInHomeArea(map);
listerMergeables = new ListerMergeables(map);

gravshipCache = new GravshipCache(map);
}

private FactionMapData(int factionId, Map map) : this(map)
Expand Down Expand Up @@ -65,6 +69,8 @@ public void ExposeData()
planManager ??= new PlanManager(map);
}

gravshipCache = new GravshipCache(map);

ExposeActor.OnPostInit(() => map.PopFaction());
}

Expand All @@ -89,6 +95,8 @@ public static FactionMapData NewFromMap(Map map, int factionId)
resourceCounter = map.resourceCounter,
listerFilthInHomeArea = map.listerFilthInHomeArea,
listerMergeables = map.listerMergeables,

gravshipCache = new GravshipCache(map)
};
}
}
2 changes: 2 additions & 0 deletions Source/Client/Comp/Map/MultiplayerMapComp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ public void SetFaction(Faction faction)
map.resourceCounter = data.resourceCounter;
map.listerFilthInHomeArea = data.listerFilthInHomeArea;
map.listerMergeables = data.listerMergeables;

data?.gravshipCache.Apply();
}

public CustomFactionMapData GetCurrentCustomFactionData()
Expand Down
10 changes: 8 additions & 2 deletions Source/Client/Comp/World/FactionWorldData.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Multiplayer.Client.Persistent;
using RimWorld;
using Verse;

Expand All @@ -17,6 +18,7 @@ public class FactionWorldData : IExposable
public Storyteller storyteller;
public StoryWatcher storyWatcher;

public GravshipCache gravshipCache;
public FactionWorldData() { }

public void ExposeData()
Expand Down Expand Up @@ -74,7 +76,9 @@ public static FactionWorldData New(int factionId)

history = new History(),
storyteller = new Storyteller(Find.Storyteller.def, Find.Storyteller.difficultyDef, Find.Storyteller.difficulty),
storyWatcher = new StoryWatcher()
storyWatcher = new StoryWatcher(),

gravshipCache = new GravshipCache(),
};
}

Expand All @@ -92,7 +96,9 @@ public static FactionWorldData FromCurrent(int factionId)

history = Find.History,
storyteller = Find.Storyteller,
storyWatcher = Find.StoryWatcher
storyWatcher = Find.StoryWatcher,

gravshipCache = new GravshipCache()
};
}
}
3 changes: 3 additions & 0 deletions Source/Client/Comp/World/MultiplayerWorldComp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ private void ExposeFactionData()
// Game manager order?
factionData[currentFactionId] = FactionWorldData.FromCurrent(currentFactionId);
}

}

public void WriteSessionData(ByteWriter writer)
Expand Down Expand Up @@ -164,6 +165,8 @@ public void SetFaction(Faction faction)
game.history = data.history;
game.storyteller = data.storyteller;
game.storyWatcher = data.storyWatcher;

//data?.gravshipCache.Apply();
}

public void DirtyColonyTradeForMap(Map map)
Expand Down
60 changes: 60 additions & 0 deletions Source/Client/Patches/GravshipCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using HarmonyLib;
using Multiplayer.Client.Util;
using RimWorld;
using System.Collections.Generic;
using Verse;

namespace Multiplayer.Client.Persistent
{
[HarmonyPatch(typeof(GravshipUtility), nameof(GravshipUtility.GetPlayerGravEngine_NewTemp))]
public static class PatchGravshipUtilityGetPlayerGravEngine_NewTemp
{
static bool Prefix(Map map, Building_GravEngine __result, Building_GravEngine __state)
{
if (!ModsConfig.OdysseyActive || Faction.OfPlayer.loadID < 0)
{
return false;
}
GravshipCache cache = map.MpComp().factionData.GetValueOrDefault(Faction.OfPlayer.loadID).gravshipCache;
if (cache != null)
{
Building_GravEngine cachedGravEngine = __state = cache.cachedGravEngine;

// uptime cache
if (Find.TickManager.TicksGame == cache.lastCachedEngineTick)
{
__result = cachedGravEngine;
return false;
}
// no cache, default to search for one
if (cachedGravEngine == null)
{
//MpLog.Debug($"Allowed searching for gravengine of {Faction.OfPlayer} at {map.GetUniqueLoadID()} tick.{Find.TickManager.TicksGame}");
return true;
}
// cached but old, validate cache available
if (cachedGravEngine.Map == map && !cachedGravEngine.Destroyed)
{
return false;
}
}
return true;
}
static void Finalizer(Building_GravEngine __result, Building_GravEngine __state, Map map)
{
if (Faction.OfPlayer.loadID > 0)
{
GravshipCache cache = map.MpComp().factionData.GetValueOrDefault(Faction.OfPlayer.loadID).gravshipCache;
if(cache != null)
{
// if engine updated
if (__result != __state)
cache.cachedGravEngine = __result;

cache.lastCachedEngineTick = Find.TickManager.TicksGame;
}
}

}
}
}
32 changes: 32 additions & 0 deletions Source/Client/Persistent/GravshipCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using RimWorld;
using Verse;

namespace Multiplayer.Client.Persistent
{
public class GravshipCache
{
public Map parent;
public Building_GravEngine cachedGravEngine;
public int lastCachedEngineTick;
public GravshipCache(Map map = null)
{
parent = map;
}
public void Apply()
{
if (cachedGravEngine != null)
{
GravshipUtility.lastCachedEngineTick = lastCachedEngineTick;
GravshipUtility.cachedGravEngine = cachedGravEngine;
GravshipUtility.lastCachedEngineMapID = parent.uniqueID;
}
else
{
//comment out so default fall to world status
GravshipUtility.lastCachedEngineTick = -1;
GravshipUtility.cachedGravEngine = null;
GravshipUtility.lastCachedEngineMapID = -1;
}
}
}
}
Loading