-
Notifications
You must be signed in to change notification settings - Fork 3
/
MechBonding.cs
447 lines (403 loc) · 20.8 KB
/
MechBonding.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
using System;
using System.Reflection;
using BattleTech;
using Harmony;
using BattleTech.UI;
using Newtonsoft.Json;
using System.IO;
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using Localize;
using Error = BestHTTP.SocketIO.Error;
using Logger = Pilot_Quirks.Pre_Control.Helper.Logger;
using HBS.Collections;
using BattleTech.Data;
namespace Pilot_Quirks
{
class MechBonding
{
public static Dictionary<string, Dictionary<string, int>> PilotsAndMechs = new Dictionary<string, Dictionary<string, int>>();
public static int PQ_GUID = 0;
//Add bonded mechs to generated pilots.
[HarmonyPatch(typeof(StarSystem), "GeneratePilots")]
public static class StarSystem_Patch
{
public static void Postfix(StarSystem __instance)
{
if (!Pre_Control.settings.MechBonding)
return;
try
{
var Sim = UnityGameInstance.BattleTechGame.Simulation;
if (Sim.IsCampaign && !Sim.CompanyTags.Contains("map_travel_1"))
return;
foreach (PilotDef pilot in __instance.AvailablePilots)
{
// 8-8-8-8 pilot = 56,000xp
int drops = ((pilot.ExperienceSpent + pilot.ExperienceUnspent) * Pre_Control.settings.pilot_drops_for_8_pilot / 56000);
pilot.PilotTags.Add("PQ_Mech_Mastery");
string PilotTattoo = "PQ_Pilot_GUID_" + PQ_GUID;
pilot.PilotTags.Add(PilotTattoo);
PQ_GUID++;
// Add training in a Chameleon if Inner sphere
if (pilot.PilotTags.Contains("pilot_davion") ||
pilot.PilotTags.Contains("pilot_liao") ||
pilot.PilotTags.Contains("pilot_kurita") ||
pilot.PilotTags.Contains("pilot_steiner") ||
pilot.PilotTags.Contains("pilot_marik"))
{
int trainingDrops = UnityEngine.Random.Range(0, 4);
if (trainingDrops > drops)
{
trainingDrops = drops;
}
if (trainingDrops > 0)
{
if (!PilotsAndMechs.Keys.Contains(PilotTattoo))
{
Dictionary<string, int> tempD = new Dictionary<string, int>();
tempD.Add("Chameleon", trainingDrops);
PilotsAndMechs.Add(PilotTattoo, tempD);
drops -= trainingDrops;
}
}
}
if (drops > 0)
{
int thisDrops1 = UnityEngine.Random.Range(1, drops);
int thisDrops2 = 0;
int thisDrops3 = 0;
drops -= thisDrops1;
if (drops > 0)
{
thisDrops2 = UnityEngine.Random.Range(1, drops);
drops -= thisDrops2;
}
if (drops > 0)
{
thisDrops3 = drops;
}
List<string> includes = new List<string>();
List<string> excludes = new List<string>();
string team = "";
if (pilot.PilotTags.Contains("pilot_davion"))
{
team = "Davion";
}
else if (pilot.PilotTags.Contains("pilot_kurita"))
{
team = "Kurita";
}
else if (pilot.PilotTags.Contains("pilot_liao"))
{
team = "Liao";
}
else if (pilot.PilotTags.Contains("pilot_marik"))
{
team = "Marik";
}
else if (pilot.PilotTags.Contains("pilot_steiner"))
{
team = "Steiner";
}
else
{
team = "AuriganMercenaries";
}
includes.Add("unit_mech");
excludes.Add($"unit_very_rare_{team}");
excludes.Add($"unit_ext_rare_{team}");
excludes.Add($"unit_none_{team}");
excludes.Add($"unit_hero_{team}");
excludes.Add("unit_LosTech");
excludes.Add("unit_era_2900");
excludes.Add("unit_clan");
if (drops < 20)
{
excludes.Add("unit_assault");
}
if (drops < 10)
{
excludes.Add("unit_heavy");
}
TagSet unitTagSet = new TagSet(includes);
TagSet unitExcludedTagSet = new TagSet(excludes);
DataManager dm = __instance.Sim.DataManager;
List<UnitDef_MDD> list = MetadataDatabase.Instance.GetMatchingUnitDefs(unitTagSet, unitExcludedTagSet, true, __instance.Sim.CurrentDate, __instance.Sim.CompanyTags);
string mechName = "";
if (list.Count > 0)
{
list.Shuffle<UnitDef_MDD>();
mechName = dm.MechDefs.Get(list[0].UnitDefID).Chassis.Description.UIName;
if (!PilotsAndMechs.Keys.Contains(PilotTattoo))
{
Dictionary<string, int> tempD = new Dictionary<string, int>();
tempD.Add(mechName, thisDrops3);
PilotsAndMechs.Add(PilotTattoo, tempD);
}
else if (PilotsAndMechs[PilotTattoo].ContainsKey(mechName))
{
PilotsAndMechs[PilotTattoo][mechName] += thisDrops3;
}
else
{
PilotsAndMechs[PilotTattoo].Add(mechName, thisDrops3);
}
}
else
{
if (!PilotsAndMechs.Keys.Contains(PilotTattoo))
{
Dictionary<string, int> tempD = new Dictionary<string, int>();
tempD.Add("Griffin", thisDrops3);
PilotsAndMechs.Add(PilotTattoo, tempD);
}
else if (PilotsAndMechs[PilotTattoo].ContainsKey("Griffin"))
{
PilotsAndMechs[PilotTattoo]["Griffin"] += thisDrops3;
}
else
{
PilotsAndMechs[PilotTattoo].Add("Griffin", thisDrops3);
}
}
excludes.Add($"unit_rare_{team}");
list = MetadataDatabase.Instance.GetMatchingUnitDefs(unitTagSet, unitExcludedTagSet, true, __instance.Sim.CurrentDate, __instance.Sim.CompanyTags);
if (list.Count > 0)
{
list.Shuffle<UnitDef_MDD>();
mechName = dm.MechDefs.Get(list[0].UnitDefID).Chassis.Description.UIName;
if (PilotsAndMechs[PilotTattoo].ContainsKey(mechName))
{
PilotsAndMechs[PilotTattoo][mechName] += thisDrops2;
}
else
{
PilotsAndMechs[PilotTattoo].Add(mechName, thisDrops2);
}
}
else
{
if (PilotsAndMechs[PilotTattoo].ContainsKey("Griffin"))
{
PilotsAndMechs[PilotTattoo]["Griffin"] += thisDrops2;
}
else
{
PilotsAndMechs[PilotTattoo].Add("Griffin", thisDrops2);
}
}
excludes.Add($"unit_uncommon_{team}");
list = MetadataDatabase.Instance.GetMatchingUnitDefs(unitTagSet, unitExcludedTagSet, true, __instance.Sim.CurrentDate, __instance.Sim.CompanyTags);
if (list.Count > 0)
{
list.Shuffle<UnitDef_MDD>();
mechName = dm.MechDefs.Get(list[0].UnitDefID).Chassis.Description.UIName;
if (PilotsAndMechs[PilotTattoo].ContainsKey(mechName))
{
PilotsAndMechs[PilotTattoo][mechName] += thisDrops1;
}
else
{
PilotsAndMechs[PilotTattoo].Add(mechName, thisDrops1);
}
}
else
{
if (PilotsAndMechs[PilotTattoo].ContainsKey("Griffin"))
{
PilotsAndMechs[PilotTattoo]["Griffin"] += thisDrops1;
}
else
{
PilotsAndMechs[PilotTattoo].Add("Griffin", thisDrops1);
}
}
}
}
}
catch (Exception e)
{
Logger.LogError(e);
}
//Trim out pilots that are no longer needed from the master list.
var sim = UnityGameInstance.BattleTechGame.Simulation;
Dictionary<string, Dictionary<string, int>> PAM_Clone = new Dictionary<string, Dictionary<string, int>>(PilotsAndMechs);
foreach (string pilot in PAM_Clone.Keys)
{
bool TrimPilot = true;
foreach (PilotDef pilotdef in __instance.AvailablePilots)
{
string PilotTattoo = pilotdef.PilotTags.FirstOrDefault(x => x.StartsWith("PQ_Pilot_GUID"));
if (PilotTattoo == pilot)
TrimPilot = false;
}
foreach (Pilot hiredpilot in sim.PilotRoster)
{
string PilotTattoo = hiredpilot.pilotDef.PilotTags.FirstOrDefault(x => x.StartsWith("PQ_Pilot_GUID"));
if (PilotTattoo == pilot)
TrimPilot = false;
}
foreach (Pilot deadpilot in sim.Graveyard)
{
string PilotTattoo = deadpilot.pilotDef.PilotTags.FirstOrDefault(x => x.StartsWith("PQ_Pilot_GUID"));
if (PilotTattoo == pilot)
TrimPilot = false;
}
string CommanderTattoo = sim.Commander.pilotDef.PilotTags.FirstOrDefault(x => x.StartsWith("PQ_Pilot_GUID"));
if (CommanderTattoo == pilot)
TrimPilot = false;
if (TrimPilot)
PilotsAndMechs.Remove(pilot);
}
}
}
//Clear out them tags and add the Mech Mastery Tag. PQ_pilot_green is processed in Pilot Fatigue and cleared there.
[HarmonyPatch(typeof(AAR_UnitStatusWidget), "FillInPilotData")]
public static class AAR_UnitStatusWidget_FillInPilotData_Prefix
{
public static void Prefix(AAR_UnitStatusWidget __instance, SimGameState ___simState)
{
if (!Pre_Control.settings.MechBonding)
return;
UnitResult unitResult = Traverse.Create(__instance).Field("UnitData").GetValue<UnitResult>();
if (!unitResult.pilot.pilotDef.PilotTags.Contains("PQ_Mech_Mastery"))
unitResult.pilot.pilotDef.PilotTags.Add("PQ_Mech_Mastery");
bool HasTattoo = unitResult.pilot.pilotDef.PilotTags.Any(x => x.StartsWith("PQ_Pilot_GUID"));
if (!HasTattoo)
{
string PilotTattoo = "PQ_Pilot_GUID_" + PQ_GUID;
unitResult.pilot.pilotDef.PilotTags.Add(PilotTattoo);
PQ_GUID++;
}
if (unitResult.pilot.pilotDef.PilotTags.Contains("PQ_pilot_regular"))
unitResult.pilot.pilotDef.PilotTags.Remove("PQ_pilot_regular");
if (unitResult.pilot.pilotDef.PilotTags.Contains("PQ_pilot_veteran"))
unitResult.pilot.pilotDef.PilotTags.Remove("PQ_pilot_veteran");
if (unitResult.pilot.pilotDef.PilotTags.Contains("PQ_pilot_elite"))
unitResult.pilot.pilotDef.PilotTags.Remove("PQ_pilot_elite");
if (unitResult.pilot.pilotDef.PilotTags.Contains("PQ_Pilot_MissionTattoo"))
unitResult.pilot.pilotDef.PilotTags.Remove("PQ_Pilot_MissionTattoo");
}
}
//Adjust skills based upon mastery.
[HarmonyPatch(typeof(Pilot))]
[HarmonyPatch("Piloting", MethodType.Getter)]
public class Pilot_Piloting_Patch
{
public static void Postfix(Pilot __instance, ref int __result)
{
if (!Pre_Control.settings.MechBonding)
return;
if (__instance.pilotDef.PilotTags.Contains("PQ_pilot_regular"))
__result += 1;
}
}
[HarmonyPatch(typeof(Pilot))]
[HarmonyPatch("Gunnery", MethodType.Getter)]
public class Pilot_Gunnery_Patch
{
public static void Postfix(Pilot __instance, ref int __result)
{
if (!Pre_Control.settings.MechBonding)
return;
if (__instance.pilotDef.PilotTags.Contains("PQ_pilot_elite"))
__result += 1;
}
}
[HarmonyPatch(typeof(LineOfSight), "GetAllSensorRangeAbsolutes")]
public static class LineOfSight_GetAllSensorRangeAbsolutes_Patch
{
public static void Postfix(AbstractActor source, ref float __result)
{
if (!Pre_Control.settings.MechBonding)
return;
Pilot pilot = source.GetPilot();
if (pilot.pilotDef.PilotTags.Contains("PQ_pilot_veteran"))
__result += pilot.Tactics * 5;
}
}
[HarmonyPatch(typeof(LineOfSight), "GetAllSpotterAbsolutes")]
public static class LineOfSight_GetAllSpotterAbsolutes_Patch
{
public static void Postfix(AbstractActor source, ref float __result)
{
if (!Pre_Control.settings.MechBonding)
return;
Pilot pilot = source.GetPilot();
if (pilot.pilotDef.PilotTags.Contains("PQ_pilot_veteran"))
__result += pilot.Tactics * 5;
}
}
//Track how many times the pilots drop with each 'Mech.
[HarmonyPatch(typeof(CombatHUDMWStatus), "InitForPilot")]
public static class CombatHUDMWStatus_InitForPilot_Patches
{
public static void Prefix(AbstractActor actor, Pilot pilot)
{
if (!Pre_Control.settings.MechBonding)
return;
//I've added a silent catch to deal with NPC pilots.
try
{
if (actor is Mech ourMech)
{
bool HasTattoo = pilot.pilotDef.PilotTags.Any(x => x.StartsWith("PQ_Pilot_GUID"));
string TempTattoo = "PQ_Pilot_MissionTattoo";
if (pilot.pilotDef.PilotTags.Contains(TempTattoo))
return;
if (!HasTattoo)
{
pilot.pilotDef.PilotTags.Add("PQ_Pilot_GUID_" + MechBonding.PQ_GUID);
MechBonding.PQ_GUID++;
}
string PilotTattoo = pilot.pilotDef.PilotTags.First(x => x.StartsWith("PQ_Pilot_GUID"));
//Add to the counter for 'Mech piloting.
if (!PilotsAndMechs.Keys.Contains(PilotTattoo))
{
Dictionary<string, int> tempD = new Dictionary<string, int>();
tempD.Add(ourMech.MechDef.Chassis.Description.UIName, 1);
PilotsAndMechs.Add(PilotTattoo, tempD);
}
else if (!PilotsAndMechs[PilotTattoo].Keys.Contains(ourMech.MechDef.Chassis.Description.UIName))
PilotsAndMechs[PilotTattoo].Add(ourMech.MechDef.Chassis.Description.UIName, 1);
else
PilotsAndMechs[PilotTattoo][ourMech.MechDef.Chassis.Description.UIName] += 1;
//Add tags based upon 'Mech experience for the pilot.
List<string> TopThreeMechs = new List<string>();
if (PilotsAndMechs.Keys.Contains(PilotTattoo))
{
var MechExperience = PilotsAndMechs[PilotTattoo];
int i = 0;
foreach (var mech in MechExperience.OrderByDescending(x => x.Value))
{
TopThreeMechs.Add(mech.Key);
i++;
if (i == 3)
break;
}
if (TopThreeMechs.Contains(ourMech.MechDef.Chassis.Description.UIName))
{
if (MechExperience[ourMech.MechDef.Chassis.Description.UIName] >= Pre_Control.settings.Tier1)
pilot.pilotDef.PilotTags.Add("PQ_pilot_green");
if (MechExperience[ourMech.MechDef.Chassis.Description.UIName] >= Pre_Control.settings.Tier2)
pilot.pilotDef.PilotTags.Add("PQ_pilot_regular");
if (MechExperience[ourMech.MechDef.Chassis.Description.UIName] >= Pre_Control.settings.Tier3)
pilot.pilotDef.PilotTags.Add("PQ_pilot_veteran");
if (MechExperience[ourMech.MechDef.Chassis.Description.UIName] >= Pre_Control.settings.Tier4)
pilot.pilotDef.PilotTags.Add("PQ_pilot_elite");
}
}
pilot.pilotDef.PilotTags.Add(TempTattoo);
}
}
catch (Exception e)
{
Pre_Control.Helper.Logger.LogError(e);
}
}
}
}
}