-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHEF_Utils.cs
308 lines (267 loc) · 10.9 KB
/
HEF_Utils.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using RimWorld;
using RimWorld.Planet;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using Verse;
using UnityEngine;
namespace rep.heframework
{
public static class HEF_Utils
{
public static List<Faction> ReturnHEFFactions()
{
List<Faction> factions = Find.FactionManager.AllFactions.Where(f => (f.def.HasModExtension<PawnGroupMakerExtension>())).ToList();
return factions;
}
public static List<Faction> ReturnHostileHEFFactions()
{
List<Faction> hostileFactions = ReturnHEFFactions().Where(f => f.HostileTo(Find.FactionManager.OfPlayer)).ToList();
return hostileFactions;
}
public static List<Site> FindHEFSitesFor(Faction fact)
{
List<Site> hefSites = new List<Site>();
List<Site> worldSites = Find.WorldObjects.Sites;
foreach (Site site in worldSites)
{
if (site == null)
continue;
if (site.Faction != fact)
continue;
if (site.MainSitePartDef == null)
continue;
if (site.MainSitePartDef.HasModExtension<WorldObjectExtension>())
{
hefSites.Add(site);
continue;
}
}
return hefSites;
}
public static List<SitePartDef> FindExistingHEFSiteDefsFor(Faction fact)
{
List<Site> hefSites = FindHEFSitesFor(fact);
List<SitePartDef> hefSitePartDefs = FindDefsForSites(hefSites);
return hefSitePartDefs;
}
public static List<SitePartDef> FindEligibleHEFSiteDefsFor(Faction fact)
{
HashSet<SitePartDef> eligibleDefs = new HashSet<SitePartDef>();
foreach (SitePartDef def in DefDatabase<SitePartDef>.AllDefs)
{
WorldObjectExtension extension = def.GetModExtension<WorldObjectExtension>();
if (extension != null && extension.factionsToPawnGroups.Any(x => x.faction == fact.def))
{
eligibleDefs.Add(def);
}
}
List<SitePartDef> usedDefs = FindExistingHEFSiteDefsFor(fact);
if (!usedDefs.NullOrEmpty())
{
//TODO add check for tag marking site as non-unique, skip removing from eligible list if so
foreach (SitePartDef def in usedDefs)
{
eligibleDefs.Remove(def);
}
}
return eligibleDefs.ToList();
}
public static List<SitePartDef> FindDefsForSites(List<Site> sites)
{
List<SitePartDef> defs = new List<SitePartDef>();
if (!sites.NullOrEmpty())
{
foreach (Site site in sites)
{
defs.Add(site.MainSitePartDef);
}
}
return defs;
}
public static List<string> FindStringsForDefs(List<SitePartDef> defs)
{
List<string> tags = new List<string>();
if (!defs.NullOrEmpty())
{
foreach (SitePartDef def in defs)
{
//the list is initialized empty in the class, so no null check needed
foreach (string tag in def.tags)
{
tags.Add(tag);
}
}
}
return tags;
}
public static bool CheckIfAllTagsPresent(List<string> pgmTags, List<string> siteTags)
{
foreach (string str in pgmTags)
{
if (!siteTags.Contains(str))
{
return false;
}
}
return true;
}
public static bool TryGenerateExtendedRaidInfo(IncidentParms parms, out List<Pawn> pawns, bool debugTest = false)
{
List<Site> hefSites;
List<SitePartDef> hefSiteDefs;
hefSites = HEF_Utils.FindHEFSitesFor(parms.faction);
hefSiteDefs = HEF_Utils.FindDefsForSites(hefSites);
PawnGroupKindDef combat = PawnGroupKindDefOf.Combat;
//Due to the Expansion Site system, it will be easiest to select the PawnGroupMaker first, as there is otherwise a high probability that selected Strategy/ArrivalMode will have no legal PawnGroup
if (!TryResolveTaggedPawnGroup(parms, hefSiteDefs, out TaggedPawnGroupMaker groupMaker))
{
pawns = new List<Pawn>();
return false;
}
ResolveRaidStrategy(parms, groupMaker);
ResolveRaidArriveMode(parms, groupMaker);
//ResolveRaidAgeRestriction(parms); //TODO - need to copy vanilla
if (!debugTest && !parms.raidArrivalMode.Worker.TryResolveRaidSpawnCenter(parms))
{
pawns = null;
return false;
}
float points = parms.points;
parms.points = AdjustedRaidPoints(parms.points, parms.raidArrivalMode, parms.raidStrategy, parms.faction, combat, parms.raidAgeRestriction);
PawnGroupMakerParms defaultPawnGroupMakerParms = IncidentParmsUtility.GetDefaultPawnGroupMakerParms(combat, parms);
pawns = GeneratePawns(defaultPawnGroupMakerParms, groupMaker).ToList();
if (pawns.Count == 0)
{
if (debugTest)
{
Log.Error("Got no pawns spawning raid from parms " + parms);
}
return false;
}
if (!debugTest)
{
parms.raidArrivalMode.Worker.Arrive(pawns, parms);
}
parms.pawnCount = pawns.Count;
//PostProcessSpawnedPawns(parms, pawns); //TODO re-enable this for 1.5
if (debugTest)
{
parms.target.StoryState.lastRaidFaction = parms.faction;
}
else
{
//GenerateRaidLoot(parms, points, pawns); //TODO copy vanilla
}
return true;
}
public static bool TryResolveTaggedPawnGroup(IncidentParms parms, List<SitePartDef> siteDefs, out TaggedPawnGroupMaker groupMaker)
{
List<TaggedPawnGroupMaker> possibleGroupMakers = new List<TaggedPawnGroupMaker>();
int highestTier = 0;
List<string> hefTags = HEF_Utils.FindStringsForDefs(siteDefs);
if (Prefs.DevMode)
{
foreach (string str in hefTags)
{
Log.Message($"found site tag: {str}");
}
}
FactionDef factionDef = parms.faction.def;
PawnGroupMakerExtension extension = factionDef.GetModExtension<PawnGroupMakerExtension>();
foreach (TaggedPawnGroupMaker tpgm in extension.taggedPawnGroupMakers)
{
if (HEF_Utils.CheckIfAllTagsPresent(tpgm.requiredSiteTags, hefTags))
{
if (Prefs.DevMode)
{
Log.Message($"Adding {(tpgm.groupName ?? "unnamed")} as pgm option");
}
possibleGroupMakers.Add(tpgm);
if (tpgm.groupTier > highestTier)
{
highestTier = tpgm.groupTier;
}
}
}
if (extension.alwaysUseHighestTier)
{
for (int i = possibleGroupMakers.Count - 1; i >= 0; i--)
{
if (possibleGroupMakers[i].groupTier < highestTier)
{
if (Prefs.DevMode)
{
Log.Message($"Removing {(possibleGroupMakers[i].groupName ?? "unnamed")} as pgm option due to low tier");
}
possibleGroupMakers.RemoveAt(i);
}
}
}
possibleGroupMakers.TryRandomElementByWeight((TaggedPawnGroupMaker gm) => gm.commonality, out groupMaker);
if (Prefs.DevMode)
{
Log.Message($"Selected {(groupMaker.groupName ?? "unnamed pgm")} as pgm");
}
return groupMaker != null;
}
public static void ResolveRaidStrategy(IncidentParms parms, TaggedPawnGroupMaker groupMaker)
{
if (parms.raidStrategy != null)
return;
parms.raidStrategy = groupMaker.allowedRaidStrategies.RandomElement(); //TODO consider using point curve weight
}
public static void ResolveRaidArriveMode(IncidentParms parms, TaggedPawnGroupMaker groupMaker)
{
if (parms.raidArrivalMode != null)
return;
parms.raidArrivalMode = groupMaker.allowedArrivalModes.RandomElement();
}
public static float AdjustedRaidPoints(float points, PawnsArrivalModeDef raidArrivalMode, RaidStrategyDef raidStrategy, Faction faction, PawnGroupKindDef groupKind, RaidAgeRestrictionDef ageRestriction = null)
{
//TODO adjust raid points based on certain sites
if (raidArrivalMode.pointsFactorCurve != null)
{
points *= raidArrivalMode.pointsFactorCurve.Evaluate(points);
}
if (raidStrategy.pointsFactorCurve != null)
{
points *= raidStrategy.pointsFactorCurve.Evaluate(points);
}
if (ageRestriction != null)
{
points *= ageRestriction.threatPointsFactor;
}
points = Mathf.Max(points, raidStrategy.Worker.MinimumPoints(faction, groupKind) * 1.05f);
return points;
}
public static IEnumerable<Pawn> GeneratePawns(PawnGroupMakerParms parms, PawnGroupMaker groupMaker, bool warnOnZeroResults = true)
{
if (parms.groupKind == null)
{
Log.Error("Tried to generate pawns with null pawn group kind def. parms=" + parms);
yield break;
}
if (parms.faction == null)
{
Log.Error("Tried to generate pawn kinds with null faction. parms=" + parms);
yield break;
}
foreach (Pawn item in groupMaker.GeneratePawns(parms, warnOnZeroResults))
{
yield return item;
}
}
public static WorldObjectExtension GetWorldObjectExtension(FactionDef factionDef, GenStepParams parms)
{
if (parms.sitePart != null)
{
return (WorldObjectExtension)parms.sitePart?.def.GetModExtension<WorldObjectExtension>();
}
return (WorldObjectExtension)factionDef.GetModExtension<WorldObjectExtension>();
}
}
}