-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathMultiplayerMapComp.cs
278 lines (236 loc) · 10 KB
/
MultiplayerMapComp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using System;
using System.Collections.Generic;
using System.Linq;
using HarmonyLib;
using Multiplayer.Client.Factions;
using Multiplayer.Client.Persistent;
using Multiplayer.Client.Saving;
using Multiplayer.Common;
using RimWorld;
using RimWorld.Planet;
using Verse;
namespace Multiplayer.Client
{
public class MultiplayerMapComp : IExposable, IHasSessionData
{
public static bool tickingFactions;
public Map map;
// SortedDictionary to ensure determinism
public SortedDictionary<int, FactionMapData> factionData = new();
public SortedDictionary<int, CustomFactionMapData> customFactionData = new();
public SessionManager sessionManager;
public List<PersistentDialog> mapDialogs = new();
public int autosaveCounter;
// for SaveCompression
public List<Thing> tempLoadedThings;
public MultiplayerMapComp(Map map)
{
this.map = map;
sessionManager = new(map);
}
public CaravanFormingSession CreateCaravanFormingSession(bool reform, Action onClosed, bool mapAboutToBeRemoved, IntVec3? meetingSpot = null)
{
var caravanForming = sessionManager.GetFirstOfType<CaravanFormingSession>();
if (caravanForming == null)
{
caravanForming = new CaravanFormingSession(map, reform, onClosed, mapAboutToBeRemoved, meetingSpot);
if (!sessionManager.AddSession(caravanForming))
{
// Shouldn't happen if the session doesn't exist already, show an error just in case
Log.Error($"Failed trying to created a session of type {nameof(CaravanFormingSession)} - prior session did not exist and creating session failed.");
return null;
}
}
return caravanForming;
}
public TransporterLoading CreateTransporterLoadingSession(List<CompTransporter> transporters)
{
var transporterLoading = sessionManager.GetFirstOfType<TransporterLoading>();
if (transporterLoading == null)
{
transporterLoading = new TransporterLoading(map, transporters);
if (!sessionManager.AddSession(transporterLoading))
{
// Shouldn't happen if the session doesn't exist already, show an error just in case
Log.Error($"Failed trying to created a session of type {nameof(TransporterLoading)} - prior session did not exist and creating session failed.");
return null;
}
}
return transporterLoading;
}
public RitualSession CreateRitualSession(RitualData data)
{
var ritualSession = sessionManager.GetFirstOfType<RitualSession>();
if (ritualSession == null)
{
ritualSession = new RitualSession(map, data);
if (!sessionManager.AddSession(ritualSession))
{
// Shouldn't happen if the session doesn't exist already, show an error just in case
Log.Error($"Failed trying to created a session of type {nameof(RitualSession)} - prior session did not exist and creating session failed.");
return null;
}
}
return ritualSession;
}
public void DoTick()
{
autosaveCounter++;
tickingFactions = true;
try
{
foreach (var data in factionData)
{
map.PushFaction(data.Key);
data.Value.listerHaulables.ListerHaulablesTick();
data.Value.resourceCounter.ResourceCounterTick();
map.PopFaction();
}
}
finally
{
tickingFactions = false;
}
}
public void SetFaction(Faction faction)
{
if (!factionData.TryGetValue(faction.loadID, out FactionMapData data))
return;
map.designationManager = data.designationManager;
map.areaManager = data.areaManager;
map.zoneManager = data.zoneManager;
map.haulDestinationManager = data.haulDestinationManager;
map.listerHaulables = data.listerHaulables;
map.resourceCounter = data.resourceCounter;
map.listerFilthInHomeArea = data.listerFilthInHomeArea;
map.listerMergeables = data.listerMergeables;
}
public CustomFactionMapData GetCurrentCustomFactionData()
{
return customFactionData[Faction.OfPlayer.loadID];
}
public void Notify_ThingDespawned(Thing t)
{
foreach (var data in customFactionData.Values)
data.unforbidden.Remove(t);
}
public void ExposeData()
{
// Data marker
if (Scribe.mode == LoadSaveMode.Saving)
{
bool isPlayerHome = map.IsPlayerHome;
Scribe_Values.Look(ref isPlayerHome, "isPlayerHome", false, true);
}
sessionManager.ExposeSessions();
Scribe_Collections.Look(ref mapDialogs, "mapDialogs", LookMode.Deep, map);
if (Scribe.mode == LoadSaveMode.LoadingVars && mapDialogs == null)
mapDialogs = new List<PersistentDialog>();
ExposeFactionData();
ExposeCustomFactionData();
}
public int currentFactionId;
private void ExposeFactionData()
{
if (Scribe.mode == LoadSaveMode.Saving)
{
int currentFactionId = GetFactionId(map.zoneManager);
Scribe_Custom.LookValue(currentFactionId, "currentFactionId");
var savedFactionData = new SortedDictionary<int, FactionMapData>(factionData);
savedFactionData.Remove(currentFactionId);
Scribe_Custom.LookValueDeep(ref savedFactionData, "factionMapData", map);
}
else
{
// The faction whose data is currently set
Scribe_Values.Look(ref currentFactionId, "currentFactionId");
Scribe_Custom.LookValueDeep(ref factionData, "factionMapData", map);
factionData ??= new SortedDictionary<int, FactionMapData>();
}
if (Scribe.mode == LoadSaveMode.LoadingVars)
{
factionData[currentFactionId] = FactionMapData.NewFromMap(map, currentFactionId);
}
}
private void ExposeCustomFactionData()
{
Scribe_Custom.LookValueDeep(ref customFactionData, "customFactionMapData", map);
customFactionData ??= new SortedDictionary<int, CustomFactionMapData>();
}
public void WriteSessionData(ByteWriter writer)
{
writer.WriteInt32(autosaveCounter);
sessionManager.WriteSessionData(writer);
}
public void ReadSessionData(ByteReader reader)
{
autosaveCounter = reader.ReadInt32();
sessionManager.ReadSessionData(reader);
}
public int GetFactionId(ZoneManager zoneManager)
{
return factionData.First(kv => kv.Value.zoneManager == zoneManager).Key;
}
}
[HarmonyPatch(typeof(MapDrawer), nameof(MapDrawer.DrawMapMesh))]
static class ForceShowDialogs
{
static void Prefix(MapDrawer __instance)
{
if (Multiplayer.Client == null) return;
var comp = __instance.map.MpComp();
if (comp.mapDialogs.Any())
{
var newDialog = comp.mapDialogs.First().Dialog;
//If NO mapdialogs (Dialog_NodeTrees) are open, add the first one to the window stack
if (!Find.WindowStack.IsOpen(typeof(Dialog_NodeTree)) && !Find.WindowStack.IsOpen(newDialog.GetType())) {
Find.WindowStack.Add(newDialog);
}
}
}
}
[HarmonyPatch(typeof(MapInterface), nameof(MapInterface.Notify_SwitchedMap))]
static class HandleMissingDialogsGameStart
{
static void Prefix()
{
if (Multiplayer.Client == null) return;
// Trading window on resume save
if (!Multiplayer.WorldComp.trading.NullOrEmpty()) return;
// playerNegotiator == null can only happen during loading? Is this a resuming check?
Multiplayer.WorldComp.trading.FirstOrDefault(t => t.playerNegotiator == null)?.OpenWindow();
}
}
[HarmonyPatch(typeof(WorldInterface), nameof(WorldInterface.WorldInterfaceUpdate))]
static class HandleDialogsOnWorldRender
{
static void Prefix(WorldInterface __instance)
{
if (Multiplayer.Client == null) return;
if (Find.World.renderer.wantedMode == WorldRenderMode.Planet)
{
// Hide trading window for map trades
if (Multiplayer.WorldComp.trading.All(t => t.playerNegotiator?.Map != null))
{
if (Find.WindowStack.IsOpen(typeof(TradingWindow)))
Find.WindowStack.TryRemove(typeof(TradingWindow), doCloseSound: false);
}
// Hide transport loading window
if (Find.WindowStack.IsOpen(typeof(TransporterLoadingProxy)))
Find.WindowStack.TryRemove(typeof(TransporterLoadingProxy), doCloseSound: false);
}
}
}
[HarmonyPatch(typeof(ListerHaulables))]
[HarmonyPatch(nameof(ListerHaulables.ListerHaulablesTick))]
public static class HaulablesTickPatch
{
static bool Prefix() => Multiplayer.Client == null || MultiplayerMapComp.tickingFactions;
}
[HarmonyPatch(typeof(ResourceCounter))]
[HarmonyPatch(nameof(ResourceCounter.ResourceCounterTick))]
public static class ResourcesTickPatch
{
static bool Prefix() => Multiplayer.Client == null || MultiplayerMapComp.tickingFactions;
}
}