|
1 | 1 | #include <Ext\WeaponType\Body.h> |
| 2 | +#include <Ext/Techno/Body.h> |
2 | 3 | #include <Helpers/Macro.h> |
3 | 4 | #include <Utilities/GeneralUtils.h> |
| 5 | +#include <unordered_map> |
4 | 6 |
|
5 | 7 | namespace LaserDrawTemp |
6 | 8 | { |
@@ -53,3 +55,215 @@ DEFINE_HOOK(0x6FD3FD, TechnoClass_LaserZap_ZAdjust, 0x5) |
53 | 55 |
|
54 | 56 | return 0; |
55 | 57 | } |
| 58 | + |
| 59 | +#pragma region LaserPositionUpdate |
| 60 | + |
| 61 | +namespace LaserRT |
| 62 | +{ |
| 63 | + static CoordStruct GetRelativeFLH(TechnoClass* pShooter, int weaponIndex) |
| 64 | + { |
| 65 | + bool flhFound = false; |
| 66 | + CoordStruct result = TechnoExt::GetBurstFLH(pShooter, weaponIndex, flhFound); |
| 67 | + |
| 68 | + if (!flhFound) |
| 69 | + { |
| 70 | + result = pShooter->GetWeapon(weaponIndex)->FLH; |
| 71 | + |
| 72 | + if (pShooter->CurrentBurstIndex % 2 != 0) |
| 73 | + result.Y = -result.Y; |
| 74 | + } |
| 75 | + |
| 76 | + return result; |
| 77 | + } |
| 78 | + |
| 79 | + struct TrackingData |
| 80 | + { |
| 81 | + TechnoClass* Shooter { nullptr }; |
| 82 | + ObjectClass* Target { nullptr }; |
| 83 | + int WeaponIndex { 0 }; |
| 84 | + PositionFollow FollowMode { PositionFollow::None }; |
| 85 | + bool IsDiskLaser = false; |
| 86 | + CoordStruct SavedRelativeFLH { CoordStruct::Empty }; |
| 87 | + |
| 88 | + void Initialize(TechnoClass* pShooter, AbstractClass* pTarget, int weaponIdx, PositionFollow mode) |
| 89 | + { |
| 90 | + if (pShooter && mode & PositionFollow::Firer) |
| 91 | + { |
| 92 | + this->Shooter = pShooter; |
| 93 | + this->SavedRelativeFLH = LaserRT::GetRelativeFLH(pShooter, weaponIdx); |
| 94 | + } |
| 95 | + |
| 96 | + if (mode & PositionFollow::Target) |
| 97 | + this->Target = abstract_cast<ObjectClass*>(pTarget); |
| 98 | + |
| 99 | + this->WeaponIndex = weaponIdx; |
| 100 | + this->FollowMode = mode; |
| 101 | + } |
| 102 | + |
| 103 | + void PointerExpired(void* ptr, bool removed) |
| 104 | + { |
| 105 | + if (!removed) |
| 106 | + return; |
| 107 | + |
| 108 | + AnnounceInvalidPointer(this->Shooter, ptr); |
| 109 | + AnnounceInvalidPointer(this->Target, ptr); |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + std::unordered_map<LaserDrawClass*, TrackingData> TrackingMap; |
| 114 | + |
| 115 | + void SetLaserTrackingData(LaserDrawClass* pLaser, TechnoClass* pShooter, AbstractClass* pTarget, int weaponIdx, PositionFollow mode, bool ignoreShooter) |
| 116 | + { |
| 117 | + TrackingData data; |
| 118 | + data.Initialize(ignoreShooter ? nullptr : pShooter, pTarget, weaponIdx, mode); |
| 119 | + TrackingMap[pLaser] = data; |
| 120 | + } |
| 121 | + |
| 122 | + TechnoClass* Shooter = nullptr; |
| 123 | + AbstractClass* Target = nullptr; |
| 124 | + int WeaponIndex = 0; |
| 125 | + bool IgnoreShooter = false; |
| 126 | +} |
| 127 | + |
| 128 | +// container hooks |
| 129 | + |
| 130 | +// IsLaser ctor/dtor/remove hooks |
| 131 | +DEFINE_HOOK(0x54FE60, LaserDrawClass_CTOR_Update, 0x5) |
| 132 | +{ |
| 133 | + GET(LaserDrawClass*, pLaser, ECX); |
| 134 | + LaserRT::TrackingMap[pLaser] = LaserRT::TrackingData {}; |
| 135 | + return 0; |
| 136 | +} |
| 137 | + |
| 138 | +DEFINE_HOOK_AGAIN(0x5501D7, LaserDrawClass_DTOR_Tracking, 0x5) |
| 139 | +DEFINE_HOOK_AGAIN(0x5500EF, LaserDrawClass_DTOR_Tracking, 0x5) |
| 140 | +DEFINE_HOOK_AGAIN(0x550016, LaserDrawClass_DTOR_Tracking, 0x6) |
| 141 | +DEFINE_HOOK(0x54FFB0, LaserDrawClass_DTOR_Tracking, 0x7) // LaserDrawClass::DTOR |
| 142 | +{ |
| 143 | + GET(LaserDrawClass*, pLaser, ECX); |
| 144 | + LaserRT::TrackingMap.erase(pLaser); |
| 145 | + return 0; |
| 146 | +} |
| 147 | + |
| 148 | +void WeaponTypeExt::LaserTrackingPointerExpired(void* ptr, bool removed) |
| 149 | +{ |
| 150 | + for (auto& [_, data] : LaserRT::TrackingMap) |
| 151 | + data.PointerExpired(ptr, removed); |
| 152 | +} |
| 153 | + |
| 154 | +// hooks |
| 155 | + |
| 156 | +DEFINE_HOOK(0x6FD210, TechnoClass_LaserZap_SetTrackingContext, 0x7) |
| 157 | +{ |
| 158 | + GET(TechnoClass*, pShooter, ECX); |
| 159 | + GET_STACK(ObjectClass*, pTarget, 0x4); |
| 160 | + GET_STACK(int, weaponIdx, 0x8); |
| 161 | + |
| 162 | + LaserRT::Shooter = LaserRT::IgnoreShooter ? nullptr : pShooter; |
| 163 | + LaserRT::Target = pTarget; |
| 164 | + LaserRT::WeaponIndex = weaponIdx; |
| 165 | + return 0; |
| 166 | +} |
| 167 | + |
| 168 | +DEFINE_HOOK(0x6FD446, TechnoClass_LaserZap_Tracking, 0x7) |
| 169 | +{ |
| 170 | + GET(WeaponTypeClass*, pWeapon, ECX); |
| 171 | + const auto pShooter = std::exchange(LaserRT::Shooter, nullptr); |
| 172 | + const auto pTarget = std::exchange(LaserRT::Target, nullptr); |
| 173 | + const int weaponIdx = std::exchange(LaserRT::WeaponIndex, 0); |
| 174 | + |
| 175 | + GET(LaserDrawClass*, pLaser, EAX); |
| 176 | + const auto it = LaserRT::TrackingMap.find(pLaser); |
| 177 | + |
| 178 | + if (it == LaserRT::TrackingMap.cend()) |
| 179 | + return 0; |
| 180 | + |
| 181 | + const auto mode = WeaponTypeExt::ExtMap.Find(pWeapon)->LaserPositionUpdate.Get(); |
| 182 | + |
| 183 | + if (mode == PositionFollow::None) |
| 184 | + return 0; |
| 185 | + |
| 186 | + it->second.Initialize(pShooter, pTarget, weaponIdx, mode); |
| 187 | + return 0; |
| 188 | +} |
| 189 | + |
| 190 | +static LaserDrawClass* __fastcall Shrapnel_CreateLaser_Wrapper(TechnoClass* pShooter, void*, ObjectClass* pTarget |
| 191 | + , int weaponIdx, WeaponTypeClass* pWeapon, const CoordStruct& sourceCoords) |
| 192 | +{ |
| 193 | + LaserRT::IgnoreShooter = true; |
| 194 | + const auto pLaser = pShooter->CreateLaser(pTarget, weaponIdx, pWeapon, sourceCoords); |
| 195 | + LaserRT::IgnoreShooter = false; |
| 196 | + return pLaser; |
| 197 | +} |
| 198 | +DEFINE_FUNCTION_JUMP(CALL, 0x46A8AC, Shrapnel_CreateLaser_Wrapper) |
| 199 | +DEFINE_FUNCTION_JUMP(CALL, 0x46AD81, Shrapnel_CreateLaser_Wrapper) |
| 200 | + |
| 201 | +// DiskLaser main beam activation |
| 202 | +DEFINE_HOOK(0x4A7696, DiskLaser_Update_ActivateMainBeam_Tracking, 0x6) |
| 203 | +{ |
| 204 | + GET(LaserDrawClass*, pLaser, EAX); |
| 205 | + |
| 206 | + if (!pLaser) |
| 207 | + return 0; |
| 208 | + |
| 209 | + const auto it = LaserRT::TrackingMap.find(pLaser); |
| 210 | + |
| 211 | + if (it == LaserRT::TrackingMap.cend()) |
| 212 | + return 0; |
| 213 | + |
| 214 | + GET(DiskLaserClass*, pDiskLaser, ESI); |
| 215 | + const auto pWeapon = pDiskLaser->Weapon; |
| 216 | + |
| 217 | + if (!pWeapon) |
| 218 | + return 0; |
| 219 | + |
| 220 | + const auto mode = WeaponTypeExt::ExtMap.Find(pWeapon)->LaserPositionUpdate.Get(); |
| 221 | + |
| 222 | + if (mode == PositionFollow::None) |
| 223 | + return 0; |
| 224 | + |
| 225 | + it->second.Initialize(pDiskLaser->Owner, pDiskLaser->Target, 0, mode); |
| 226 | + |
| 227 | + if (it->second.Shooter && (mode & PositionFollow::Firer)) |
| 228 | + { |
| 229 | + CoordStruct flh; |
| 230 | + it->second.Shooter->GetFLH(&flh, 0, CoordStruct { 0, 0, 0 }); |
| 231 | + it->second.SavedRelativeFLH = pLaser->Source - flh; |
| 232 | + it->second.IsDiskLaser = true; |
| 233 | + } |
| 234 | + |
| 235 | + return 0; |
| 236 | +} |
| 237 | + |
| 238 | +// Per‑frame coordinate update |
| 239 | +DEFINE_HOOK(0x550173, LaserDrawClass_Update_Tracking, 0x6) |
| 240 | +{ |
| 241 | + GET(LaserDrawClass*, pLaser, ESI); |
| 242 | + const auto it = LaserRT::TrackingMap.find(pLaser); |
| 243 | + |
| 244 | + if (it == LaserRT::TrackingMap.cend()) |
| 245 | + return 0; |
| 246 | + |
| 247 | + const auto& data = it->second; |
| 248 | + |
| 249 | + if (const auto pShooter = data.Shooter) |
| 250 | + { |
| 251 | + if (data.IsDiskLaser) |
| 252 | + { |
| 253 | + CoordStruct flh; |
| 254 | + pShooter->GetFLH(&flh, data.WeaponIndex, CoordStruct { 0, 0, 0 }); |
| 255 | + pLaser->Source = flh + data.SavedRelativeFLH; |
| 256 | + } |
| 257 | + else |
| 258 | + { |
| 259 | + pLaser->Source = TechnoExt::GetFLHAbsoluteCoords(pShooter, data.SavedRelativeFLH, true); |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + if (const auto pTarget = data.Target) |
| 264 | + pLaser->Target = pTarget->GetTargetCoords(); |
| 265 | + |
| 266 | + return 0; |
| 267 | +} |
| 268 | + |
| 269 | +#pragma endregion |
0 commit comments