-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathMultiplayerWorldComp.cs
198 lines (161 loc) · 6.14 KB
/
MultiplayerWorldComp.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
using RimWorld;
using RimWorld.Planet;
using System.Collections.Generic;
using System.Linq;
using Verse;
using Multiplayer.Client.Persistent;
using Multiplayer.Client.Saving;
using Multiplayer.Common;
namespace Multiplayer.Client;
public class MultiplayerWorldComp : IHasSessionData
{
// SortedDictionary to ensure determinism
public SortedDictionary<int, FactionWorldData> factionData = new();
public World world;
public TileTemperaturesComp uiTemperatures;
public List<MpTradeSession> trading = new(); // Should only be modified from MpTradeSession in PostAdd/Remove and ExposeData
public SessionManager sessionManager = new(null);
public Faction spectatorFaction;
private int currentFactionId;
public MultiplayerWorldComp(World world)
{
this.world = world;
uiTemperatures = new TileTemperaturesComp(world);
}
// Called from AsyncWorldTimeComp.ExposeData (for backcompat)
public void ExposeData()
{
ExposeFactionData();
sessionManager.ExposeSessions();
// Ensure a pause lock session exists if there's any pause locks registered
if (!PauseLockSession.pauseLocks.NullOrEmpty())
sessionManager.AddSession(new PauseLockSession(null));
DoBackCompat();
}
private void DoBackCompat()
{
if (Scribe.mode != LoadSaveMode.PostLoadInit)
return;
if (spectatorFaction == null)
{
void AddSpectatorFaction()
{
spectatorFaction = HostUtil.AddNewFaction("Spectator", FactionDefOf.PlayerColony);
factionData[spectatorFaction.loadID] = FactionWorldData.New(spectatorFaction.loadID);
factionData[spectatorFaction.loadID].ReassignIds();
foreach (var map in Find.Maps)
MapSetup.InitNewFactionData(map, spectatorFaction);
}
void RemoveOpponentFaction()
{
// Test faction left in by mistake in version 0.7
var opponent =
Find.FactionManager.AllFactions.FirstOrDefault(f => f.Name == "Opponent" && f.IsPlayer);
if (opponent is not null)
{
opponent.RemoveAllRelations();
Find.FactionManager.allFactions.Remove(opponent);
Log.Warning("Multiplayer removed dummy Opponent faction");
}
}
AddSpectatorFaction();
RemoveOpponentFaction();
}
}
private void ExposeFactionData()
{
Scribe_References.Look(ref spectatorFaction, "spectatorFaction");
if (Scribe.mode == LoadSaveMode.Saving)
{
int currentFactionId = GetFactionId(Find.ResearchManager);
Scribe_Custom.LookValue(currentFactionId, "currentFactionId");
var savedFactionData = new SortedDictionary<int, FactionWorldData>(factionData);
savedFactionData.Remove(currentFactionId);
Scribe_Custom.LookValueDeep(ref savedFactionData, "factionData");
}
else
{
// The faction whose data is currently set
Scribe_Values.Look(ref currentFactionId, "currentFactionId");
Scribe_Custom.LookValueDeep(ref factionData, "factionData");
factionData ??= new SortedDictionary<int, FactionWorldData>();
}
if (Scribe.mode == LoadSaveMode.LoadingVars && Multiplayer.session != null && Multiplayer.game != null)
{
Multiplayer.game.myFactionLoading =
Find.FactionManager.GetById(Multiplayer.session.myFactionId) ?? spectatorFaction;
}
if (Scribe.mode == LoadSaveMode.LoadingVars)
{
// Game manager order?
factionData[currentFactionId] = FactionWorldData.FromCurrent(currentFactionId);
}
}
public void WriteSessionData(ByteWriter writer)
{
sessionManager.WriteSessionData(writer);
}
public void ReadSessionData(ByteReader data)
{
sessionManager.ReadSessionData(data);
}
public void TickWorldSessions()
{
sessionManager.TickSessions();
}
public void RemoveTradeSession(MpTradeSession session)
{
// Cleanup and removal from `trading` field is handled in PostRemoveSession
sessionManager.RemoveSession(session);
}
public void SetFaction(Faction faction)
{
if (!factionData.TryGetValue(faction.loadID, out FactionWorldData data))
return;
Game game = Current.Game;
game.researchManager = data.researchManager;
game.drugPolicyDatabase = data.drugPolicyDatabase;
game.outfitDatabase = data.outfitDatabase;
game.foodRestrictionDatabase = data.foodRestrictionDatabase;
game.playSettings = data.playSettings;
game.history = data.history;
game.storyteller = data.storyteller;
game.storyWatcher = data.storyWatcher;
}
public void DirtyColonyTradeForMap(Map map)
{
if (map == null) return;
foreach (MpTradeSession session in trading)
if (session.playerNegotiator.Map == map)
session.deal.recacheColony = true;
}
public void DirtyTraderTradeForTrader(ITrader trader)
{
if (trader == null) return;
foreach (MpTradeSession session in trading)
if (session.trader == trader)
session.deal.recacheTrader = true;
}
public void DirtyTradeForSpawnedThing(Thing t)
{
if (t is not { Spawned: true }) return;
foreach (MpTradeSession session in trading)
if (session.playerNegotiator.Map == t.Map)
session.deal.recacheThings.Add(t);
}
public bool AnyTradeSessionsOnMap(Map map)
{
foreach (MpTradeSession session in trading)
if (session.playerNegotiator.Map == map)
return true;
return false;
}
public int GetFactionId(ResearchManager researchManager)
{
return factionData.First(kv => kv.Value.researchManager == researchManager).Key;
}
public override string ToString()
{
return $"{nameof(MultiplayerWorldComp)}_{world}";
}
}