forked from Phobos-developers/Phobos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFireSuperWeapon.cpp
More file actions
467 lines (386 loc) · 13.3 KB
/
FireSuperWeapon.cpp
File metadata and controls
467 lines (386 loc) · 13.3 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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include "Body.h"
#include <SuperClass.h>
#include <BuildingClass.h>
#include <HouseClass.h>
#include <ScenarioClass.h>
#include <MessageListClass.h>
#include <Utilities/EnumFunctions.h>
#include <Utilities/GeneralUtils.h>
#include "Ext/House/Body.h"
#include "Ext/WarheadType/Body.h"
#include "Ext/WeaponType/Body.h"
#include <Ext/Scenario/Body.h>
// ============= New SuperWeapon Effects================
void SWTypeExt::FireSuperWeaponExt(SuperClass* pSW, const CellStruct& cell)
{
const auto pHouse = pSW->Owner;
const auto pType = pSW->Type;
auto const pTypeExt = SWTypeExt::ExtMap.Find(pType);
if (pTypeExt->LimboDelivery_Types.size() > 0)
pTypeExt->ApplyLimboDelivery(pHouse);
if (pTypeExt->LimboKill_IDs.size() > 0)
pTypeExt->ApplyLimboKill(pHouse);
if (pTypeExt->Detonate_Warhead || pTypeExt->Detonate_Weapon)
pTypeExt->ApplyDetonation(pHouse, cell);
if (pTypeExt->SW_Next.size() > 0)
pTypeExt->ApplySWNext(pSW, cell);
if (pTypeExt->Convert_Pairs.size() > 0)
pTypeExt->ApplyTypeConversion(pSW);
if (pTypeExt->SW_Link.size() > 0)
pTypeExt->ApplyLinkedSW(pSW);
if (static_cast<int>(pType->Type) == 28 && !pTypeExt->EMPulse_TargetSelf) // Ares' Type=EMPulse SW
pTypeExt->HandleEMPulseLaunch(pSW, cell);
auto& sw_ext = HouseExt::ExtMap.Find(pHouse)->SuperExts[pType->ArrayIndex];
sw_ext.ShotCount++;
const auto pTags = &pHouse->RelatedTags;
if (pTags->Count > 0)
{
auto RaiseEvent = [pTags](int nEvent, TechnoClass* pSource)
{
int index = 0;
int tagCount = pTags->Count;
while (tagCount > 0 && index < tagCount)
{
const auto pTag = pTags->Items[index];
if (pTag->RaiseEvent(static_cast<TriggerEvent>(nEvent), nullptr, CellStruct::Empty, false, pSource))
{
if (tagCount != pTags->Count)
{
tagCount = pTags->Count;
continue;
}
}
++index;
}
};
std::pair<SuperClass*, CellStruct> pass{ pSW, cell };
RaiseEvent(77, reinterpret_cast<TechnoClass*>(&pass));
RaiseEvent(75, reinterpret_cast<TechnoClass*>(pSW));
}
}
// ====================================================
#pragma region LimboDelivery
static inline void LimboCreate(BuildingTypeClass* pType, HouseClass* pOwner, int ID)
{
// BuildLimit check goes before creation
if (pType->BuildLimit > 0)
{
int sum = pOwner->CountOwnedNow(pType);
// copy Ares' deployable units x build limit fix
if (auto const pUndeploy = pType->UndeploysInto)
sum += pOwner->CountOwnedNow(pUndeploy);
if (sum >= pType->BuildLimit)
return;
}
if (auto const pBuilding = static_cast<BuildingClass*>(pType->CreateObject(pOwner)))
{
// All of these are mandatory
pBuilding->InLimbo = false;
pBuilding->IsAlive = true;
pBuilding->IsOnMap = true;
// Jun 3, 2023 - Starkku: For reasons beyond my comprehension, the discovery logic is checked for certain logics like power drain/output in campaign only.
// Normally on unlimbo the buildings are revealed to current player if unshrouded or if game is a campaign and to non-player houses always.
// Because of the unique nature of LimboDelivered buildings, this has been adjusted to always reveal to the current player in singleplayer
// and to the owner of the building regardless, removing the shroud check from the equation since they don't physically exist
if (SessionClass::IsCampaign())
pBuilding->DiscoveredBy(HouseClass::CurrentPlayer);
pBuilding->DiscoveredBy(pOwner);
pOwner->RegisterGain(pBuilding, false);
pOwner->RecheckTechTree = true;
pOwner->RecheckPower = true;
pOwner->Buildings.AddItem(pBuilding);
// Different types of building logics
if (pType->ConstructionYard)
pOwner->ConYards.AddItem(pBuilding); // why would you do that????
if (pType->SecretLab)
pOwner->SecretLabs.AddItem(pBuilding);
auto const pBuildingExt = BuildingExt::ExtMap.Find(pBuilding);
auto const pOwnerExt = HouseExt::ExtMap.Find(pOwner);
if (pType->FactoryPlant)
{
if (pBuildingExt->TypeExtData->FactoryPlant_AllowTypes.size() > 0 || pBuildingExt->TypeExtData->FactoryPlant_DisallowTypes.size() > 0)
{
pOwnerExt->RestrictedFactoryPlants.push_back(pBuilding);
}
else
{
pOwner->FactoryPlants.AddItem(pBuilding);
pOwner->CalculateCostMultipliers();
}
}
// BuildingClass::Place is already called in DiscoveredBy
// it added OrePurifier and xxxGainSelfHeal to House counter already
// LimboKill ID
pBuildingExt->LimboID = ID;
// Add building to list of owned limbo buildings
pOwnerExt->OwnedLimboDeliveredBuildings.push_back(pBuilding);
auto const pBldType = pBuilding->Type;
if (!pBldType->Insignificant && !pBldType->DontScore)
pOwnerExt->AddToLimboTracking(pBldType);
auto const pTechnoExt = TechnoExt::ExtMap.Find(pBuilding);
auto const pTechnoTypeExt = pTechnoExt->TypeExtData;
if (pTechnoTypeExt->AutoDeath_Behavior.isset())
{
ScenarioExt::Global()->AutoDeathObjects.push_back(pTechnoExt);
if (pTechnoTypeExt->AutoDeath_AfterDelay > 0)
pTechnoExt->AutoDeathTimer.Start(pTechnoTypeExt->AutoDeath_AfterDelay);
}
}
}
void SWTypeExt::ExtData::ApplyLimboDelivery(HouseClass* pHouse)
{
// random mode
if (this->LimboDelivery_RandomWeightsData.size())
{
int id = -1;
const size_t idsSize = this->LimboDelivery_IDs.size();
const auto results = this->WeightedRollsHandler(&this->LimboDelivery_RollChances, &this->LimboDelivery_RandomWeightsData, this->LimboDelivery_Types.size());
for (size_t result : results)
{
if (result < idsSize)
id = this->LimboDelivery_IDs[result];
LimboCreate(this->LimboDelivery_Types[result], pHouse, id);
}
}
// no randomness mode
else
{
int id = -1;
const size_t idsSize = this->LimboDelivery_IDs.size();
for (size_t i = 0; i < this->LimboDelivery_Types.size(); i++)
{
if (i < idsSize)
id = this->LimboDelivery_IDs[i];
LimboCreate(this->LimboDelivery_Types[i], pHouse, id);
}
}
}
void SWTypeExt::ExtData::ApplyLimboKill(HouseClass* pHouse)
{
for (int limboKillID : this->LimboKill_IDs)
{
for (HouseClass* pTargetHouse : HouseClass::Array)
{
if (EnumFunctions::CanTargetHouse(this->LimboKill_AffectsHouse, pHouse, pTargetHouse))
{
auto const pHouseExt = HouseExt::ExtMap.Find(pTargetHouse);
auto& vec = pHouseExt->OwnedLimboDeliveredBuildings;
for (auto it = vec.begin(); it != vec.end(); )
{
BuildingClass* const pBuilding = *it;
auto const pBuildingExt = BuildingExt::ExtMap.Find(pBuilding);
if (pBuildingExt->LimboID == limboKillID)
{
it = vec.erase(it);
auto const pBldType = pBuilding->Type;
// Remove limbo buildings' tracking here because their are not truely InLimbo
if (!pBldType->Insignificant && !pBldType->DontScore)
HouseExt::ExtMap.Find(pBuilding->Owner)->RemoveFromLimboTracking(pBldType);
pBuilding->Stun();
pBuilding->Limbo();
pBuilding->RegisterDestruction(nullptr);
pBuilding->UnInit();
}
else
{
++it;
}
}
}
}
}
}
#pragma endregion
void SWTypeExt::ExtData::ApplyDetonation(HouseClass* pHouse, const CellStruct& cell)
{
auto coords = MapClass::Instance.GetCellAt(cell)->GetCoords();
BuildingClass* pFirer = nullptr;
for (auto const& pBld : pHouse->Buildings)
{
if (this->IsLaunchSiteEligible(cell, pBld, false))
{
pFirer = pBld;
break;
}
}
if (this->Detonate_AtFirer)
coords = pFirer ? pFirer->GetCenterCoords() : CoordStruct::Empty;
const auto pWeapon = this->Detonate_Weapon;
auto const mapCoords = CellClass::Coord2Cell(coords);
if (!MapClass::Instance.CoordinatesLegal(mapCoords))
{
auto const ID = pWeapon ? pWeapon->get_ID() : this->Detonate_Warhead->get_ID();
Debug::Log("ApplyDetonation: Superweapon [%s] failed to detonate [%s] - cell at %d, %d is invalid.\n", this->OwnerObject()->get_ID(), ID, mapCoords.X, mapCoords.Y);
return;
}
if (pWeapon)
WeaponTypeExt::DetonateAt(pWeapon, coords, pFirer, this->Detonate_Damage.Get(pWeapon->Damage), pHouse);
else
{
if (this->Detonate_Warhead_Full)
WarheadTypeExt::DetonateAt(this->Detonate_Warhead, coords, pFirer, this->Detonate_Damage.Get(0), pHouse);
else
MapClass::DamageArea(coords, this->Detonate_Damage.Get(0), pFirer, this->Detonate_Warhead, true, pHouse);
}
}
void SWTypeExt::ExtData::ApplySWNext(SuperClass* pSW, const CellStruct& cell)
{
// SW.Next proper launching mechanic
auto LaunchTheSW = [=](const int swIdxToLaunch)
{
const auto pHouse = pSW->Owner;
if (const auto pSuper = pHouse->Supers.GetItem(swIdxToLaunch))
{
const auto pNextTypeExt = SWTypeExt::ExtMap.Find(pSuper->Type);
if (!this->SW_Next_RealLaunch
|| (pSuper->IsPresent && pSuper->IsReady && !pSuper->IsSuspended && pHouse->CanTransactMoney(pNextTypeExt->Money_Amount)))
{
if ((this->SW_Next_IgnoreInhibitors || !pNextTypeExt->HasInhibitor(pHouse, cell))
&& (this->SW_Next_IgnoreDesignators || pNextTypeExt->HasDesignator(pHouse, cell)))
{
const int oldstart = pSuper->RechargeTimer.StartTime;
const int oldleft = pSuper->RechargeTimer.TimeLeft;
pSuper->SetReadiness(true);
pSuper->Launch(cell, pHouse->IsCurrentPlayer());
pSuper->Reset();
if (!this->SW_Next_RealLaunch)
{
pSuper->RechargeTimer.StartTime = oldstart;
pSuper->RechargeTimer.TimeLeft = oldleft;
}
}
}
}
};
// random mode
if (this->SW_Next_RandomWeightsData.size())
{
const auto results = this->WeightedRollsHandler(&this->SW_Next_RollChances, &this->SW_Next_RandomWeightsData, this->SW_Next.size());
for (const int result : results)
LaunchTheSW(this->SW_Next[result]);
}
// no randomness mode
else
{
for (const auto swType : this->SW_Next)
LaunchTheSW(swType);
}
}
void SWTypeExt::ExtData::ApplyTypeConversion(SuperClass* pSW)
{
for (const auto pTargetFoot : FootClass::Array)
TypeConvertGroup::Convert(pTargetFoot, this->Convert_Pairs, pSW->Owner);
}
void SWTypeExt::ExtData::HandleEMPulseLaunch(SuperClass* pSW, const CellStruct& cell) const
{
auto const& pBuildings = this->GetEMPulseCannons(pSW->Owner, cell);
auto const count = this->SW_MaxCount >= 0 ? static_cast<size_t>(this->SW_MaxCount) : std::numeric_limits<size_t>::max();
for (size_t i = 0; i < pBuildings.size(); i++)
{
auto const pBuilding = pBuildings[i];
auto const pExt = BuildingExt::ExtMap.Find(pBuilding);
pExt->CurrentEMPulseSW = pSW;
if (i + 1 == count)
break;
}
if (this->EMPulse_SuspendOthers)
{
auto const pHouse = pSW->Owner;
auto const pHouseExt = HouseExt::ExtMap.Find(pHouse);
for (auto const& pSuper : pHouse->Supers)
{
if (static_cast<int>(pSuper->Type->Type) != 28 || pSuper == pSW)
continue;
auto const pTypeExt = SWTypeExt::ExtMap.Find(pSuper->Type);
bool suspend = false;
if (this->EMPulse_Cannons.empty() && pTypeExt->EMPulse_Cannons.empty())
{
suspend = true;
}
else
{
// Suspend if the two cannon lists share common items.
suspend = std::find_first_of(this->EMPulse_Cannons.begin(), this->EMPulse_Cannons.end(),
pTypeExt->EMPulse_Cannons.begin(), pTypeExt->EMPulse_Cannons.end()) != this->EMPulse_Cannons.end();
}
if (suspend)
{
pSuper->IsSuspended = true;
const int arrayIndex = pSW->Type->ArrayIndex;
if (pHouseExt->SuspendedEMPulseSWs.count(arrayIndex))
pHouseExt->SuspendedEMPulseSWs[arrayIndex].push_back(arrayIndex);
else
pHouseExt->SuspendedEMPulseSWs.insert({ arrayIndex, std::vector<int>{pSuper->Type->ArrayIndex} });
}
}
}
}
void SWTypeExt::ExtData::ApplyLinkedSW(SuperClass* pSW)
{
const auto pHouse = pSW->Owner;
const bool notObserver = !pHouse->IsObserver() || !pHouse->IsCurrentPlayerObserver();
if (pHouse->Defeated || !notObserver)
return;
auto linkedSW = [=](const int swIdxToAdd)
{
if (const auto pSuper = pHouse->Supers.GetItem(swIdxToAdd))
{
const bool granted = this->SW_Link_Grant && !pSuper->IsPresent && pSuper->Grant(true, false, false);
bool isActive = granted;
if (pSuper->IsPresent)
{
// check SW.Link.Reset first
if (this->SW_Link_Reset)
{
pSuper->Reset();
isActive = true;
}
// check SW.Link.Ready, which will default to SW.InitialReady for granted superweapon
else if (this->SW_Link_Ready || (granted && SWTypeExt::ExtMap.Find(pSuper->Type)->SW_InitialReady))
{
pSuper->RechargeTimer.TimeLeft = 0;
pSuper->SetReadiness(true);
isActive = true;
}
// reset granted superweapon if it doesn't meet above conditions
else if (granted)
{
pSuper->Reset();
}
}
if (granted && notObserver && pHouse->IsCurrentPlayer())
{
if (MouseClass::Instance.AddCameo(AbstractType::Special, swIdxToAdd))
MouseClass::Instance.RepaintSidebar(1);
}
return isActive;
}
return false;
};
bool isActive = false;
// random mode
if (this->SW_Link_RandomWeightsData.size())
{
const auto results = this->WeightedRollsHandler(&this->SW_Link_RollChances, &this->SW_Link_RandomWeightsData, this->SW_Link.size());
for (const int result : results)
{
if (linkedSW(this->SW_Link[result]))
isActive = true;
}
}
// no randomness mode
else
{
for (const auto swType : this->SW_Link)
{
if (linkedSW(swType))
isActive = true;
}
}
if (isActive && notObserver && pHouse->IsCurrentPlayer())
{
if (this->EVA_LinkedSWAcquired.isset())
VoxClass::PlayIndex(this->EVA_LinkedSWAcquired.Get(), -1, -1);
MessageListClass::Instance.PrintMessage(this->Message_LinkedSWAcquired.Get(), RulesClass::Instance->MessageDelay, HouseClass::CurrentPlayer->ColorSchemeIndex, true);
}
}