forked from rwmt/Multiplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactionCreator.cs
More file actions
296 lines (238 loc) · 9.77 KB
/
FactionCreator.cs
File metadata and controls
296 lines (238 loc) · 9.77 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Multiplayer.API;
using Multiplayer.Client.Util;
using Multiplayer.Common;
using RimWorld;
using RimWorld.Planet;
using Verse;
namespace Multiplayer.Client.Factions;
public static class FactionCreator
{
private static Dictionary<int, List<Pawn>> pawnStore = new();
public static bool generatingMap;
public const string preventSpecialCalculationPathInGenTicksTicksAbs = "dummy";
public static void ClearData()
{
pawnStore.Clear();
}
[SyncMethod(exposeParameters = new[] { 1 })]
public static void SendPawn(int playerId, Pawn p)
{
pawnStore.GetOrAddNew(playerId).Add(p);
}
[SyncMethod]
public static void CreateFaction(int playerId, FactionCreationData creationData)
{
var self = TickPatch.currentExecutingCmdIssuedBySelf;
LongEventHandler.QueueLongEvent(() =>
{
var scenario = creationData.scenarioDef?.scenario ?? Current.Game.Scenario;
Map newMap = null;
PrepareGameInitData(playerId, scenario, self, creationData.startingPossessions);
var newFaction = NewFactionWithIdeo(
creationData.factionName,
scenario.playerFaction.factionDef,
creationData.chooseIdeoInfo
);
if (creationData.generateMap)
using (MpScope.PushFaction(newFaction))
{
foreach (var pawn in StartingPawnUtility.StartingAndOptionalPawns)
pawn.ideo.SetIdeo(newFaction.ideos.PrimaryIdeo);
newMap = GenerateNewMap(creationData.startingTile, scenario, creationData.setupNextMapFromTickZero);
}
foreach (Map map in Find.Maps)
foreach (var f in Find.FactionManager.AllFactions.Where(f => f.IsPlayer))
map.attackTargetsCache.Notify_FactionHostilityChanged(f, newFaction);
using (MpScope.PushFaction(newFaction))
InitNewGame();
if (self)
{
Current.Game.CurrentMap = newMap;
Multiplayer.game.ChangeRealPlayerFaction(newFaction);
CameraJumper.TryJump(MapGenerator.playerStartSpotInt, newMap);
PostGameStart(scenario);
// todo setting faction of self
Multiplayer.Client.Send(
Packets.Client_SetFaction,
Multiplayer.session.playerId,
newFaction.loadID
);
}
}, "GeneratingMap", doAsynchronously: true, GameAndMapInitExceptionHandlers.ErrorWhileGeneratingMap);
}
private static void PostGameStart(Scenario scenario)
{
/**
ScenPart_StartingResearch.cs
ScenPart_AutoActivateMonolith.cs
ScenPart_CreateIncident.cs
ScenPart_GameStartDialog.cs
ScenPart_PlayerFaction.cs
ScenPart_Rule.cs
Would like to call PostGameStart on all implementations (scenario.PostGameStart) -
but dont know if it breaks with dlcs other than biotech - especially while only called
on self
**/
HashSet<Type> types = new HashSet<Type>
{
typeof(ScenPart_PlayerFaction),
typeof(ScenPart_GameStartDialog),
typeof(ScenPart_StartingResearch),
};
foreach (ScenPart part in scenario.AllParts)
{
if (types.Contains(part.GetType()))
{
part.PostGameStart();
}
}
}
private static Map GenerateNewMap(int tile, Scenario scenario, bool setupNextMapFromTickZero)
{
// This has to be null, otherwise, during map generation, Faction.OfPlayer returns it which breaks FactionContext
Find.GameInitData.playerFaction = null;
Find.GameInitData.PrepForMapGen();
// ScenPart_PlayerFaction --> PreMapGenerate
var settlement = (Settlement) WorldObjectMaker.MakeWorldObject(WorldObjectDefOf.Settlement);
settlement.Tile = tile;
settlement.SetFaction(Faction.OfPlayer);
Find.WorldObjects.Add(settlement);
// ^^^^ Duplicate Code here ^^^^
var prevScenario = Find.Scenario;
var prevStartingTile = Find.GameInfo.startingTile;
Current.Game.Scenario = scenario;
Find.GameInfo.startingTile = tile;
Find.GameInitData.startingTile = tile;
MapSetup.setupNextMapFromTickZero = setupNextMapFromTickZero;
generatingMap = true;
try
{
Map map = GetOrGenerateMapUtility.GetOrGenerateMap(
tile,
new IntVec3(250, 1, 250),
null
);
SetAllItemsOnMapForbidden(map);
return map;
}
finally
{
generatingMap = false;
Current.Game.Scenario = prevScenario;
Find.GameInfo.startingTile = prevStartingTile;
Find.GameInitData.startingTile = prevStartingTile;
}
}
// TODO: Find better Solution
// Workaround for the fact that the map is generated with all scattered items allowed
private static void SetAllItemsOnMapForbidden(Map map)
{
foreach (Thing thing in map.listerThings.AllThings)
{
thing.SetForbidden(true, false);
}
}
private static void InitNewGame()
{
PawnUtility.GiveAllStartingPlayerPawnsThought(ThoughtDefOf.NewColonyOptimism);
ResearchUtility.ApplyPlayerStartingResearch();
}
private static void PrepareGameInitData(int sessionId, Scenario scenario, bool self, List<ThingDefCount> startingPossessions)
{
if(!self)
{
Current.Game.InitData = new GameInitData()
{
startingPawnCount = GetStartingPawnsCountForScenario(scenario),
gameToLoad = preventSpecialCalculationPathInGenTicksTicksAbs
};
}
if (pawnStore.TryGetValue(sessionId, out var pawns))
{
Pawn firstPawn = pawns.First();
GameInitData gameInitData = Current.Game.InitData;
gameInitData.startingAndOptionalPawns = pawns;
gameInitData.startingPossessions = new Dictionary<Pawn, List<ThingDefCount>>();
foreach(var pawn in pawns)
{
gameInitData.startingPossessions[pawn] = new List<ThingDefCount>();
}
foreach (var possesion in startingPossessions)
{
gameInitData.startingPossessions[firstPawn].Add(possesion);
}
pawnStore.Remove(sessionId);
}
}
private static Faction NewFactionWithIdeo(string name, FactionDef def, ChooseIdeoInfo chooseIdeoInfo)
{
var faction = new Faction
{
loadID = Find.UniqueIDsManager.GetNextFactionID(),
def = def,
Name = name,
hidden = true
};
faction.ideos = new FactionIdeosTracker(faction);
if (!ModsConfig.IdeologyActive || Find.IdeoManager.classicMode || chooseIdeoInfo.SelectedIdeo == null)
{
faction.ideos.SetPrimary(Faction.OfPlayer.ideos.PrimaryIdeo);
}
else
{
var newIdeo = GenerateIdeo(chooseIdeoInfo);
faction.ideos.SetPrimary(newIdeo);
Find.IdeoManager.Add(newIdeo);
}
foreach (Faction other in Find.FactionManager.AllFactionsListForReading)
faction.TryMakeInitialRelationsWith(other);
Find.FactionManager.Add(faction);
var newWorldFactionData = FactionWorldData.New(faction.loadID);
Multiplayer.WorldComp.factionData[faction.loadID] = newWorldFactionData;
newWorldFactionData.ReassignIds();
// Add new faction to all maps (the new map is handled by map generation code)
foreach (Map map in Find.Maps)
MapSetup.InitNewFactionData(map, faction);
foreach (var f in Find.FactionManager.AllFactions.Where(f => f.IsPlayer))
if (f != faction)
faction.SetRelation(new FactionRelation(f, FactionRelationKind.Neutral));
return faction;
}
private static Ideo GenerateIdeo(ChooseIdeoInfo chooseIdeoInfo)
{
List<MemeDef> list = chooseIdeoInfo.SelectedIdeo.memes.ToList();
if (chooseIdeoInfo.SelectedStructure != null)
list.Add(chooseIdeoInfo.SelectedStructure);
else if (DefDatabase<MemeDef>.AllDefsListForReading.Where(m => m.category == MemeCategory.Structure && IdeoUtility.IsMemeAllowedFor(m, Find.Scenario.playerFaction.factionDef)).TryRandomElement(out var result))
list.Add(result);
Ideo ideo = IdeoGenerator.GenerateIdeo(new IdeoGenerationParms(Find.FactionManager.OfPlayer.def, forceNoExpansionIdeo: false, null, null, list, chooseIdeoInfo.SelectedIdeo.classicPlus, forceNoWeaponPreference: true));
new Page_ChooseIdeoPreset { selectedStyles = chooseIdeoInfo.SelectedStyles }.ApplySelectedStylesToIdeo(ideo);
return ideo;
}
public static int GetStartingPawnsCountForScenario(Scenario scenario)
{
foreach (ScenPart part in scenario.AllParts)
{
if (part is ScenPart_ConfigPage_ConfigureStartingPawnsBase startingPawnsConfig)
{
return startingPawnsConfig.TotalPawnCount;
}
}
MpLog.Error("No starting pawns config found to access startingPawnCount");
return 0;
}
}
public record FactionCreationData : ISyncSimple
{
public string factionName;
public int startingTile;
[CanBeNull] public ScenarioDef scenarioDef;
public ChooseIdeoInfo chooseIdeoInfo;
public bool generateMap;
public List<ThingDefCount> startingPossessions;
public bool setupNextMapFromTickZero;
}