Skip to content
Open
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
57 changes: 16 additions & 41 deletions Source/Client/AsyncTime/AsyncTimeComp.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using HarmonyLib;
using Multiplayer.Common;
using RimWorld;
using RimWorld.Planet;
using System;
using System.Collections.Generic;
using Verse;
using HarmonyLib;
using Multiplayer.Client.Comp;
using Multiplayer.Client.Factions;
using Multiplayer.Client.Patches;
using Multiplayer.Client.Saving;
using Multiplayer.Client.Util;
using System.Linq;
using Multiplayer.Common;
using RimWorld;
using RimWorld.Planet;
using Verse;

namespace Multiplayer.Client
{
Expand Down Expand Up @@ -51,11 +50,10 @@ public float TickRateMultiplier(TimeSpeed speed)
}
}

public TimeSpeed DesiredTimeSpeed => timeSpeedInt;

public void SetDesiredTimeSpeed(TimeSpeed speed)
public TimeSpeed DesiredTimeSpeed
{
timeSpeedInt = speed;
get => timeSpeedInt;
set => timeSpeedInt = value;
}

public bool Paused => this.ActualRateMultiplier(DesiredTimeSpeed) == 0f;
Expand Down Expand Up @@ -86,8 +84,6 @@ public int GameStartAbsTick
public bool forcedNormalSpeed;
public int eventCount;

public Storyteller storyteller;
public StoryWatcher storyWatcher;
public TimeSlower slower = new();

public TickList tickListNormal = new(TickerType.Normal);
Expand Down Expand Up @@ -131,8 +127,8 @@ public void Tick()

TickMapSessions();

storyteller.StorytellerTick();
storyWatcher.StoryWatcherTick();
Find.Storyteller.StorytellerTick();
Find.StoryWatcher.StoryWatcherTick();

QuestManagerTickAsyncTime();

Expand Down Expand Up @@ -173,28 +169,17 @@ public void UpdateManagers()
}

private TimeSnapshot? prevTime;
private Storyteller prevStoryteller;
private StoryWatcher prevStoryWatcher;

public void PreContext()
{
if (Multiplayer.GameComp.multifaction)
{
map.PushFaction(
map.ParentFaction is { IsPlayer: true }
map.PushFaction(
!Multiplayer.GameComp.multifaction || map.ParentFaction is { IsPlayer: true }
? map.ParentFaction
: Multiplayer.WorldComp.spectatorFaction,
force: true);
}
force: true);

prevTime = TimeSnapshot.GetAndSetFromMap(map);

prevStoryteller = Current.Game.storyteller;
prevStoryWatcher = Current.Game.storyWatcher;

Current.Game.storyteller = storyteller;
Current.Game.storyWatcher = storyWatcher;

Rand.PushState();
Rand.StateCompressed = randState;

Expand All @@ -204,16 +189,12 @@ public void PreContext()

public void PostContext()
{
Current.Game.storyteller = prevStoryteller;
Current.Game.storyWatcher = prevStoryWatcher;

prevTime?.Set();

randState = Rand.StateCompressed;
Rand.PopState();

if (Multiplayer.GameComp.multifaction)
map.PopFaction();
map.PopFaction();
}

public void ExposeData()
Expand All @@ -223,12 +204,6 @@ public void ExposeData()

Scribe_Values.Look(ref gameStartAbsTickMap, "gameStartAbsTickMap");

Scribe_Deep.Look(ref storyteller, "storyteller");

Scribe_Deep.Look(ref storyWatcher, "storyWatcher");
if (Scribe.mode == LoadSaveMode.LoadingVars && storyWatcher == null)
storyWatcher = new StoryWatcher();

Scribe_Custom.LookULong(ref randState, "randState", 1);
}

Expand Down Expand Up @@ -264,7 +239,7 @@ public void ExecuteCmd(ScheduledCommand cmd)
TickPatch.currentExecutingCmdType = cmdType;

PreContext();
map.PushFaction(cmd.GetFaction());
map.PushFaction(cmd.GetFaction(), force: true);

context.map = map;

Expand Down Expand Up @@ -293,7 +268,7 @@ public void ExecuteCmd(ScheduledCommand cmd)
if (cmdType == CommandType.MapTimeSpeed && Multiplayer.GameComp.asyncTime)
{
TimeSpeed speed = (TimeSpeed)data.ReadByte();
SetDesiredTimeSpeed(speed);
DesiredTimeSpeed = speed;

MpLog.Debug("Set map time speed " + speed);
}
Expand Down
8 changes: 4 additions & 4 deletions Source/Client/AsyncTime/AsyncTimePatches.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using HarmonyLib;
using Multiplayer.Client.Factions;
using RimWorld;
using RimWorld.Planet;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Verse;

namespace Multiplayer.Client.AsyncTime
Expand Down Expand Up @@ -205,7 +205,7 @@ private static void PauseOnLetter(TickManager manager)
if (Multiplayer.GameComp.asyncTime)
{
var tickable = (ITickable)Multiplayer.MapContext.AsyncTime() ?? Multiplayer.AsyncWorldTime;
tickable.SetDesiredTimeSpeed(TimeSpeed.Paused);
tickable.DesiredTimeSpeed = TimeSpeed.Paused;
Multiplayer.GameComp.ResetAllTimeVotes(tickable.TickableId);
}
else
Expand Down
23 changes: 11 additions & 12 deletions Source/Client/AsyncTime/AsyncWorldTimeComp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ public float TickRateMultiplier(TimeSpeed speed)
}

// Run at the speed of the fastest map or at chosen speed if there are no maps
public TimeSpeed DesiredTimeSpeed => !Find.Maps.Any() ?
timeSpeedInt :
Find.Maps.Select(m => m.AsyncTime())
.Where(a => a.ActualRateMultiplier(a.DesiredTimeSpeed) != 0f)
.Max(a => a?.DesiredTimeSpeed) ?? TimeSpeed.Paused;

public void SetDesiredTimeSpeed(TimeSpeed speed)
public TimeSpeed DesiredTimeSpeed
{
timeSpeedInt = speed;
get => !Find.Maps.Any()
? timeSpeedInt
: Find.Maps.Select(m => m.AsyncTime())
.Where(a => a.ActualRateMultiplier(a.DesiredTimeSpeed) != 0f)
.Max(a => a?.DesiredTimeSpeed) ?? TimeSpeed.Paused;
set => timeSpeedInt = value;
}

public Queue<ScheduledCommand> Cmds => cmds;
Expand Down Expand Up @@ -274,16 +273,16 @@ private static void CreateJoinPointAndSendIfHost()
public void SetTimeEverywhere(TimeSpeed speed)
{
foreach (var map in Find.Maps)
map.AsyncTime().SetDesiredTimeSpeed(speed);
SetDesiredTimeSpeed(speed);
map.AsyncTime().DesiredTimeSpeed = speed;
DesiredTimeSpeed = speed;
}

public static float lastSpeedChange;

private void HandleTimeSpeed(ScheduledCommand cmd, ByteReader data)
{
TimeSpeed speed = (TimeSpeed)data.ReadByte();
SetDesiredTimeSpeed(speed);
DesiredTimeSpeed = speed;

if (!Multiplayer.GameComp.asyncTime)
{
Expand Down Expand Up @@ -311,7 +310,7 @@ private void HandleTimeVote(ScheduledCommand cmd, ByteReader data)
if (!Multiplayer.GameComp.asyncTime || vote == TimeVote.ResetGlobal)
SetTimeEverywhere(Multiplayer.GameComp.GetLowestTimeVote(TickableId));
else if (TickPatch.TickableById(tickableId) is { } tickable)
tickable.SetDesiredTimeSpeed(Multiplayer.GameComp.GetLowestTimeVote(tickableId));
tickable.DesiredTimeSpeed = Multiplayer.GameComp.GetLowestTimeVote(tickableId);
}

public void FinalizeInit()
Expand Down
6 changes: 2 additions & 4 deletions Source/Client/AsyncTime/ITickable.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Multiplayer.Common;
using System.Collections.Generic;
using Multiplayer.Common;
using Verse;

namespace Multiplayer.Client
Expand All @@ -12,9 +12,7 @@ public interface ITickable

float TimeToTickThrough { get; set; }

TimeSpeed DesiredTimeSpeed { get; }

void SetDesiredTimeSpeed(TimeSpeed speed);
TimeSpeed DesiredTimeSpeed { get; set; }

float TickRateMultiplier(TimeSpeed speed);

Expand Down
7 changes: 2 additions & 5 deletions Source/Client/Factions/FactionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Linq;
using RimWorld;
using RimWorld.Planet;
using System.Linq;
using Verse;

namespace Multiplayer.Client.Factions;
Expand All @@ -25,10 +25,7 @@ public static void PushFaction(this Map map, int factionId)
map.PushFaction(faction);
}

public static Faction PopFaction()
{
return PopFaction(null);
}
public static Faction PopFaction() => PopFaction(null);

public static Faction PopFaction(this Map map)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Client/MultiplayerStatic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ void LoadNextReplay()

Replay.LoadReplay(Replay.SavedReplayFile(current[1]), true, () =>
{
TickPatch.AllTickables.Do(t => t.SetDesiredTimeSpeed(TimeSpeed.Normal));
TickPatch.AllTickables.Do(t => t.DesiredTimeSpeed = TimeSpeed.Normal);

void TickBatch()
{
Expand Down
10 changes: 5 additions & 5 deletions Source/Client/Networking/HostUtil.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using Multiplayer.Client.Networking;
using Multiplayer.Common;
using RimWorld;
using System;
using System.ComponentModel;
using System.Diagnostics;
Expand All @@ -9,7 +6,10 @@
using System.Threading.Tasks;
using Multiplayer.Client.AsyncTime;
using Multiplayer.Client.Comp;
using Multiplayer.Client.Networking;
using Multiplayer.Client.Util;
using Multiplayer.Common;
using RimWorld;
using UnityEngine;
using Verse;

Expand Down Expand Up @@ -94,9 +94,9 @@ private static void PrepareGame()

private static void SetGameState(ServerSettings settings)
{
Multiplayer.AsyncWorldTime.SetDesiredTimeSpeed(TimeSpeed.Paused);
Multiplayer.AsyncWorldTime.DesiredTimeSpeed = TimeSpeed.Paused;
foreach (var map in Find.Maps)
map.AsyncTime().SetDesiredTimeSpeed(TimeSpeed.Paused);
map.AsyncTime().DesiredTimeSpeed = TimeSpeed.Paused;

Find.TickManager.CurTimeSpeed = TimeSpeed.Paused;

Expand Down
25 changes: 9 additions & 16 deletions Source/Client/Patches/MapSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,19 @@ namespace Multiplayer.Client;
[HarmonyPatch(typeof(MapGenerator), nameof(MapGenerator.GenerateMap))]
public static class MapSetup
{
public static bool SetupNextMapFromTickZero = false;
public static bool SetupNextMapFromTickZero;

static void Prefix(ref Action<Map> extraInitBeforeContentGen)
{
if (Multiplayer.Client == null) return;
extraInitBeforeContentGen += SetupMap;
}

public static void SetupMap(Map map)
{
SetupMap(map, false);
extraInitBeforeContentGen += map => SetupMap(map);
}

public static void SetupMap(Map map, bool usingMapTimeFromSingleplayer = false)
{
Log.Message("MP: Setting up map " + map.uniqueID);

// Initialize and store Multiplayer
// Initialize and store Multiplayer

var mapComp = new MultiplayerMapComp(map);
Multiplayer.game.mapComps.Add(mapComp);
Expand All @@ -40,15 +35,14 @@ public static void SetupMap(Map map, bool usingMapTimeFromSingleplayer = false)
// Add all other (non Faction.OfPlayer) factions to the map
foreach (var faction in Find.FactionManager.AllFactions.Where(f => f.IsPlayer))
if (faction != Faction.OfPlayer)
InitNewFactionData(map, faction);
InitNewFactionData(map, faction);
}

private static AsyncTimeComp CreateAsyncTimeCompForMap(Map map, bool usingMapTimeFromSingleplayer)
{
int startingMapTicks;
int gameStartAbsTick;
TimeSpeed startingTimeSpeed;
AsyncTimeComp asyncTimeCompForMap;

bool startingMapTimeFromBeginning =
Multiplayer.GameComp.multifaction &&
Expand Down Expand Up @@ -77,12 +71,11 @@ private static AsyncTimeComp CreateAsyncTimeCompForMap(Map map, bool usingMapTim
if (!Multiplayer.GameComp.asyncTime)
startingTimeSpeed = Find.TickManager.CurTimeSpeed;

asyncTimeCompForMap = new AsyncTimeComp(map, gameStartAbsTick);
asyncTimeCompForMap.mapTicks = startingMapTicks;
asyncTimeCompForMap.SetDesiredTimeSpeed(startingTimeSpeed);

asyncTimeCompForMap.storyteller = new Storyteller(Find.Storyteller.def, Find.Storyteller.difficultyDef, Find.Storyteller.difficulty);
asyncTimeCompForMap.storyWatcher = new StoryWatcher();
var asyncTimeCompForMap = new AsyncTimeComp(map, gameStartAbsTick)
{
mapTicks = startingMapTicks,
DesiredTimeSpeed = startingTimeSpeed
};

SetupNextMapFromTickZero = false;

Expand Down
21 changes: 8 additions & 13 deletions Source/Client/Patches/TickPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ private static bool RunCmds()
ScheduledCommand cmd = tickable.Cmds.Dequeue();
// Minimal code impact fix for #733. Having all the commands be added to a single queue gets rid of
// the out-of-order execution problem. With a proper fix, this can be reverted to tickable.ExecuteCmd
TickableById(cmd.mapId).ExecuteCmd(cmd);
var target = TickableById(cmd.mapId);
if (target == null)
{
Log.Error($"!!! Tickable of {cmd.mapId} not found! {cmd}");
} else target.ExecuteCmd(cmd);

if (LongEventHandler.eventQueue.Count > 0) return true; // Yield to e.g. join-point creation
}
Expand Down Expand Up @@ -290,10 +294,7 @@ public static float ActualRateMultiplier(this ITickable tickable, TimeSpeed spee
return rate;
}

public static void ClearSimulating()
{
simulating = null;
}
public static void ClearSimulating() => simulating = null;

public static void Reset()
{
Expand All @@ -309,15 +310,9 @@ public static void Reset()
TimeControlPatch.prePauseTimeSpeed = null;
}

public static void SetTimer(int value)
{
Timer = value;
}
public static void SetTimer(int value) => Timer = value;

public static ITickable TickableById(int tickableId)
{
return AllTickables.FirstOrDefault(t => t.TickableId == tickableId);
}
public static ITickable TickableById(int tickableId) => AllTickables.FirstOrDefault(t => t.TickableId == tickableId);
}

public class SimulatingData
Expand Down
Loading
Loading