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
351 lines (290 loc) · 10.5 KB
/
FireSuperWeapon.cpp
File metadata and controls
351 lines (290 loc) · 10.5 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
#include "Body.h"
#include <SuperClass.h>
#include <BuildingClass.h>
#include <HouseClass.h>
#include <ScenarioClass.h>
#include <ThemeClass.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)
{
auto const pTypeExt = SWTypeExt::ExtMap.Find(pSW->Type);
if(pTypeExt->LaunchTheme >= 0)
ThemeClass::Instance.Play(pTypeExt->LaunchTheme);
if (pTypeExt->LimboDelivery_Types.size() > 0)
pTypeExt->ApplyLimboDelivery(pSW->Owner);
if (pTypeExt->LimboKill_IDs.size() > 0)
pTypeExt->ApplyLimboKill(pSW->Owner);
if (pTypeExt->Detonate_Warhead || pTypeExt->Detonate_Weapon)
pTypeExt->ApplyDetonation(pSW->Owner, cell);
if (pTypeExt->SW_Next.size() > 0)
pTypeExt->ApplySWNext(pSW, cell);
if (pTypeExt->Convert_Pairs.size() > 0)
pTypeExt->ApplyTypeConversion(pSW);
if (static_cast<int>(pSW->Type->Type) == 28 && !pTypeExt->EMPulse_TargetSelf) // Ares' Type=EMPulse SW
pTypeExt->HandleEMPulseLaunch(pSW, cell);
auto& sw_ext = HouseExt::ExtMap.Find(pSW->Owner)->SuperExts[pSW->Type->ArrayIndex];
sw_ext.ShotCount++;
}
// ====================================================
#pragma region LimboDelivery
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;
// 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 - Starkku
if (SessionClass::IsCampaign())
pBuilding->DiscoveredBy(HouseClass::CurrentPlayer);
pBuilding->DiscoveredBy(pOwner);
pOwner->RegisterGain(pBuilding, false);
pOwner->UpdatePower();
pOwner->RecheckTechTree = true;
pOwner->RecheckPower = true;
pOwner->RecheckRadar = 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);
if (!pBuilding->Type->Insignificant && !pBuilding->Type->DontScore)
pOwnerExt->AddToLimboTracking(pBuilding->Type);
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;
size_t idsSize = this->LimboDelivery_IDs.size();
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;
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_Affected, 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);
// Remove limbo buildings' tracking here because their are not truely InLimbo
if (!pBuilding->Type->Insignificant && !pBuilding->Type->DontScore)
HouseExt::ExtMap.Find(pBuilding->Owner)->RemoveFromLimboTracking(pBuilding->Type);
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)
{
HouseClass* 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)))
{
int oldstart = pSuper->RechargeTimer.StartTime;
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())
{
auto results = this->WeightedRollsHandler(&this->SW_Next_RollChances, &this->SW_Next_RandomWeightsData, this->SW_Next.size());
for (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)
{
if (this->Convert_Pairs.size() == 0)
return;
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->EMPulseSW = pSW;
if (i + 1 == count)
break;
}
if (this->EMPulse_SuspendOthers)
{
auto const pHouseExt = HouseExt::ExtMap.Find(pSW->Owner);
for (auto const& pSuper : pSW->Owner->Supers)
{
if (static_cast<int>(pSuper->Type->Type) != 28 || pSuper == pSW)
continue;
auto const pTypeExt = SWTypeExt::ExtMap.Find(pSW->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;
if (pHouseExt->SuspendedEMPulseSWs.count(pSW->Type->ArrayIndex))
pHouseExt->SuspendedEMPulseSWs[pSW->Type->ArrayIndex].push_back(pSuper->Type->ArrayIndex);
else
pHouseExt->SuspendedEMPulseSWs.insert({ pSW->Type->ArrayIndex, std::vector<int>{pSuper->Type->ArrayIndex} });
}
}
}
}