forked from Phobos-developers/Phobos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHooks.AnimCreateUnit.cpp
More file actions
220 lines (177 loc) · 6.84 KB
/
Hooks.AnimCreateUnit.cpp
File metadata and controls
220 lines (177 loc) · 6.84 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
// Anim-to--Unit
// Author: Otamaa, revisions by Starkku
#include "Body.h"
#include <AircraftTrackerClass.h>
#include <BulletClass.h>
#include <HouseClass.h>
#include <JumpjetLocomotionClass.h>
#include <ScenarioClass.h>
#include <Ext/Bullet/Body.h>
#include <Ext/TechnoType/Body.h>
#include <Ext/Techno/Body.h>
#include <Ext/AnimType/Body.h>
DEFINE_HOOK(0x737F6D, UnitClass_TakeDamage_Destroy, 0x7)
{
GET(UnitClass* const, pThis, ESI);
REF_STACK(args_ReceiveDamage const, Receivedamageargs, STACK_OFFSET(0x44, 0x4));
R->ECX(R->ESI());
TechnoExt::ExtMap.Find(pThis)->ReceiveDamage = true;
AnimTypeExt::ProcessDestroyAnims(pThis, Receivedamageargs.Attacker);
pThis->Destroy();
return 0x737F74;
}
DEFINE_HOOK(0x738807, UnitClass_Destroy_DestroyAnim, 0x8)
{
GET(UnitClass* const, pThis, ESI);
auto const Extension = TechnoExt::ExtMap.Find(pThis);
if (!Extension->ReceiveDamage)
AnimTypeExt::ProcessDestroyAnims(pThis);
return 0x73887E;
}
// Performance tweak, mark once instead of every frame.
// DEFINE_HOOK(0x423BC8, AnimClass_AI_CreateUnit_MarkOccupationBits, 0x6)
DEFINE_HOOK(0x4226F0, AnimClass_CTOR_CreateUnit_MarkOccupationBits, 0x6)
{
GET(AnimClass* const, pThis, ESI);
auto const pTypeExt = AnimTypeExt::ExtMap.Find(pThis->Type);
if (pTypeExt->CreateUnit.Get())
{
auto location = pThis->GetCoords();
if (auto pCell = pThis->GetCell())
location = pCell->GetCoordsWithBridge();
else
location.Z = MapClass::Instance->GetCellFloorHeight(location);
pThis->MarkAllOccupationBits(location);
}
return 0; //return (pThis->Type->MakeInfantry != -1) ? 0x423BD6 : 0x423C03;
}
DEFINE_HOOK(0x424932, AnimClass_AI_CreateUnit_ActualAffects, 0x6)
{
GET(AnimClass* const, pThis, ESI);
auto const pTypeExt = AnimTypeExt::ExtMap.Find(pThis->Type);
if (auto unit = pTypeExt->CreateUnit.Get())
{
HouseClass* decidedOwner = pThis->Owner && !pThis->Owner->Defeated
? pThis->Owner : HouseClass::FindCivilianSide();
auto pCell = pThis->GetCell();
CoordStruct location = pThis->GetCoords();
auto origLocation = location;
if (pCell)
origLocation = pCell->GetCoordsWithBridge();
else
origLocation.Z = MapClass::Instance->GetCellFloorHeight(location);
pThis->UnmarkAllOccupationBits(origLocation);
bool allowBridges = GroundType::Array[static_cast<int>(LandType::Clear)].Cost[static_cast<int>(unit->SpeedType)] > 0.0;
bool isBridge = allowBridges && pCell->ContainsBridge();
if (pTypeExt->CreateUnit_ConsiderPathfinding && (!pCell || !pCell->IsClearToMove(unit->SpeedType, false, false, -1, unit->MovementZone, -1, isBridge)) )
{
auto nCell = MapClass::Instance->NearByLocation(CellClass::Coord2Cell(location),
unit->SpeedType, -1, unit->MovementZone, isBridge, 1, 1, true,
false, false, isBridge, CellStruct::Empty, false, false);
pCell = MapClass::Instance->TryGetCellAt(nCell);
location = pCell->GetCoords();
}
if (pCell)
{
isBridge = allowBridges && pCell->ContainsBridge();
int bridgeZ = isBridge ? CellClass::BridgeHeight : 0;
int baseHeight = pThis->GetCoords().Z;
int zCoord = pTypeExt->CreateUnit_AlwaysSpawnOnGround ? INT32_MIN : baseHeight;
int cellFloorHeight = MapClass::Instance->GetCellFloorHeight(location) + bridgeZ;
if (!pTypeExt->CreateUnit_AlwaysSpawnOnGround && pTypeExt->CreateUnit_SpawnHeight.isset())
location.Z = cellFloorHeight + pTypeExt->CreateUnit_SpawnHeight;
else
location.Z = Math::max(cellFloorHeight, zCoord);
if (auto pTechno = static_cast<FootClass*>(unit->CreateObject(decidedOwner)))
{
bool success = false;
auto const pExt = AnimExt::ExtMap.Find(pThis);
TechnoClass* pInvoker = pExt->Invoker;
HouseClass* pInvokerHouse = pExt->InvokerHouse;
auto facing = pTypeExt->CreateUnit_RandomFacing
? static_cast<DirType>(ScenarioClass::Instance->Random.RandomRanged(0, 255)) : pTypeExt->CreateUnit_Facing;
auto resultingFacing = pTypeExt->CreateUnit_InheritDeathFacings && pExt->FromDeathUnit ? pExt->DeathUnitFacing : facing;
pTechno->OnBridge = isBridge;
bool inAir = pThis->IsOnMap && location.Z >= Unsorted::CellHeight * 2;
bool parachuted = false;
if (pTypeExt->CreateUnit_SpawnParachutedInAir && !pTypeExt->CreateUnit_AlwaysSpawnOnGround && inAir)
{
parachuted = true;
success = pTechno->SpawnParachuted(location);
}
else if (!pCell->GetBuilding() || !pTypeExt->CreateUnit_ConsiderPathfinding)
{
++Unsorted::IKnowWhatImDoing;
success = pTechno->Unlimbo(location, resultingFacing);
--Unsorted::IKnowWhatImDoing;
}
else
{
success = pTechno->Unlimbo(location, resultingFacing);
}
if (success)
{
if (!pTypeExt->CreateUnit_SpawnAnim.empty())
{
if (auto const pAnimType = pTypeExt->CreateUnit_SpawnAnim[ScenarioClass::Instance->Random.RandomRanged(0, pTypeExt->CreateUnit_SpawnAnim.size() - 1)])
{
if (auto const pAnim = GameCreate<AnimClass>(pAnimType, location))
{
AnimExt::SetAnimOwnerHouseKind(pAnim, pInvokerHouse, nullptr, false, true);
if (auto const pAnimExt = AnimExt::ExtMap.Find(pAnim))
pAnimExt->SetInvoker(pInvoker, pInvokerHouse);
}
}
}
if (pTechno->HasTurret() && pExt->FromDeathUnit && pExt->DeathUnitHasTurret && pTypeExt->CreateUnit_InheritTurretFacings)
{
pTechno->SecondaryFacing.SetCurrent(pExt->DeathUnitTurretFacing);
Debug::Log("CreateUnit: Using stored turret facing %d\n", pExt->DeathUnitTurretFacing.GetFacing<256>());
}
if (!pTechno->InLimbo)
{
if (!pTypeExt->CreateUnit_AlwaysSpawnOnGround)
{
if (auto const pJJLoco = locomotion_cast<JumpjetLocomotionClass*>(pTechno->Locomotor))
{
auto const pType = pTechno->GetTechnoType();
pJJLoco->LocomotionFacing.SetCurrent(DirStruct(static_cast<DirType>(resultingFacing)));
if (pType->BalloonHover)
{
// Makes the jumpjet think it is hovering without actually moving.
pJJLoco->State = JumpjetLocomotionClass::State::Hovering;
pJJLoco->IsMoving = true;
pJJLoco->DestinationCoords = location;
pJJLoco->CurrentHeight = pType->JumpjetHeight;
if (!inAir)
AircraftTrackerClass::Instance->Add(pTechno);
}
else if (inAir)
{
// Order non-BalloonHover jumpjets to land.
pJJLoco->Move_To(location);
}
}
else if (inAir && !parachuted)
{
pTechno->IsFallingDown = true;
}
}
auto mission = pTypeExt->CreateUnit_Mission;
if (!decidedOwner->IsControlledByHuman())
mission = pTypeExt->CreateUnit_AIMission.Get(mission);
pTechno->QueueMission(mission, false);
}
if (!decidedOwner->Type->MultiplayPassive)
decidedOwner->RecheckTechTree = true;
}
else
{
if (pTechno)
pTechno->UnInit();
}
}
}
}
return (pThis->Type->MakeInfantry != -1) ? 0x42493E : 0x424B31;
}