Skip to content

Commit f701344

Browse files
committed
Optimize Straight and Bombard
- Let Straight check in order when calculating lead time - Fix the issue of excessive calculation of lead time in Bombard when using the combination "Trajectory.Bombard.FreeFallOnTarget=false,Trajectory.Bombard.LeadTimeCalculate=true,Trajectory.Bombard.NoLaunch=true" - Change abs to std::abs for Bombard - Add some comments to Bombard
1 parent bd177ab commit f701344

File tree

2 files changed

+69
-58
lines changed

2 files changed

+69
-58
lines changed

src/Ext/Bullet/Trajectories/BombardTrajectory.cpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ void BombardTrajectoryType::Read(CCINIClass* const pINI, const char* pSection)
6464
this->FallScatter_Min.Read(exINI, pSection, "Trajectory.Bombard.FallScatter.Min");
6565
this->FallScatter_Linear.Read(exINI, pSection, "Trajectory.Bombard.FallScatter.Linear");
6666
this->FallSpeed.Read(exINI, pSection, "Trajectory.Bombard.FallSpeed");
67-
68-
if (abs(this->FallSpeed.Get()) < 1e-10)
69-
this->FallSpeed = this->Trajectory_Speed;
70-
67+
this->FallSpeed = std::abs(this->FallSpeed.Get()) < 1e-10 ? this->Trajectory_Speed.Get() : this->FallSpeed.Get();
7168
this->DetonationDistance.Read(exINI, pSection, "Trajectory.Bombard.DetonationDistance");
7269
this->DetonationHeight.Read(exINI, pSection, "Trajectory.Bombard.DetonationHeight");
7370
this->EarlyDetonation.Read(exINI, pSection, "Trajectory.Bombard.EarlyDetonation");
@@ -123,10 +120,13 @@ void BombardTrajectory::OnUnlimbo(BulletClass* pBullet, CoordStruct* pCoord, Bul
123120
this->Height += pBullet->TargetCoords.Z;
124121
// use scaling since RandomRanged only support int
125122
this->FallPercent += ScenarioClass::Instance->Random.RandomRanged(0, static_cast<int>(200 * pType->FallPercentShift)) / 100.0;
123+
124+
// Record the initial target coordinates without offset
126125
this->InitialTargetCoord = pBullet->TargetCoords;
127126
this->LastTargetCoord = pBullet->TargetCoords;
128127
pBullet->Velocity = BulletVelocity::Empty;
129128

129+
// Record some information
130130
if (const auto pWeapon = pBullet->WeaponType)
131131
this->CountOfBurst = pWeapon->Burst;
132132

@@ -138,6 +138,7 @@ void BombardTrajectory::OnUnlimbo(BulletClass* pBullet, CoordStruct* pCoord, Bul
138138
this->OffsetCoord.Y = -(this->OffsetCoord.Y);
139139
}
140140

141+
// Wait, or launch immediately?
141142
if (!pType->NoLaunch || !pType->LeadTimeCalculate || !abstract_cast<FootClass*>(pBullet->Target))
142143
this->PrepareForOpenFire(pBullet);
143144
else
@@ -240,7 +241,7 @@ void BombardTrajectory::PrepareForOpenFire(BulletClass* pBullet)
240241
this->RefreshBulletLineTrail(pBullet);
241242

242243
pBullet->SetLocation(middleLocation);
243-
const auto pOwner = pBullet->Owner ? pBullet->Owner->Owner : BulletExt::ExtMap.Find(pBullet)->FirerHouse;
244+
const auto pOwner = pBullet->Owner ? pBullet->Owner->Owner : pExt->FirerHouse;
244245
AnimExt::CreateRandomAnim(pType->TurningPointAnims, middleLocation, pBullet->Owner, pOwner, true);
245246
}
246247
}
@@ -292,6 +293,7 @@ void BombardTrajectory::CalculateTargetCoords(BulletClass* pBullet)
292293

293294
pBullet->TargetCoords = theTargetCoords;
294295

296+
// Calculate the orientation of the coordinate system
295297
if (!pType->LeadTimeCalculate && theTargetCoords == theSourceCoords && pBullet->Owner) //For disperse.
296298
{
297299
const auto theOwnerCoords = pBullet->Owner->GetCoords();
@@ -302,13 +304,15 @@ void BombardTrajectory::CalculateTargetCoords(BulletClass* pBullet)
302304
this->RotateAngle = Math::atan2(theTargetCoords.Y - theSourceCoords.Y , theTargetCoords.X - theSourceCoords.X);
303305
}
304306

307+
// Add the fixed offset value
305308
if (this->OffsetCoord != CoordStruct::Empty)
306309
{
307310
pBullet->TargetCoords.X += static_cast<int>(this->OffsetCoord.X * Math::cos(this->RotateAngle) + this->OffsetCoord.Y * Math::sin(this->RotateAngle));
308311
pBullet->TargetCoords.Y += static_cast<int>(this->OffsetCoord.X * Math::sin(this->RotateAngle) - this->OffsetCoord.Y * Math::cos(this->RotateAngle));
309312
pBullet->TargetCoords.Z += this->OffsetCoord.Z;
310313
}
311314

315+
// Add random offset value
312316
if (pBullet->Type->Inaccurate)
313317
{
314318
const auto pTypeExt = BulletTypeExt::ExtMap.Find(pBullet->Type);
@@ -332,6 +336,7 @@ CoordStruct BombardTrajectory::CalculateBulletLeadTime(BulletClass* pBullet)
332336
const auto theTargetCoords = pTarget->GetCoords();
333337
const auto theSourceCoords = pBullet->Location;
334338

339+
// Solving trigonometric functions
335340
if (theTargetCoords != this->LastTargetCoord)
336341
{
337342
int travelTime = 0;
@@ -344,6 +349,11 @@ CoordStruct BombardTrajectory::CalculateBulletLeadTime(BulletClass* pBullet)
344349
travelTime += static_cast<int>(sqrt(2 * (this->Height - theTargetCoords.Z) / BulletTypeExt::GetAdjustedGravity(pBullet->Type)));
345350
coords += extraOffsetCoord * (travelTime + 1);
346351
}
352+
else if (pType->NoLaunch)
353+
{
354+
travelTime += static_cast<int>((this->Height - theTargetCoords.Z) / pType->FallSpeed);
355+
coords += extraOffsetCoord * (travelTime + 1);
356+
}
347357
else
348358
{
349359
const auto theDistanceSquared = targetSourceCoord.MagnitudeSquared();
@@ -356,19 +366,18 @@ CoordStruct BombardTrajectory::CalculateBulletLeadTime(BulletClass* pBullet)
356366
const auto horizonDistanceSquared = theDistanceSquared - verticalDistanceSquared;
357367
const auto horizonDistance = sqrt(horizonDistanceSquared);
358368

359-
const auto straightSpeed = pType->FreeFallOnTarget ? pType->Trajectory_Speed : pType->FallSpeed;
360-
const auto straightSpeedSquared = straightSpeed * straightSpeed;
361-
369+
const auto straightSpeedSquared = pType->FallSpeed * pType->FallSpeed;
362370
const auto baseFactor = straightSpeedSquared - targetSpeedSquared;
363371
const auto squareFactor = baseFactor * verticalDistanceSquared + straightSpeedSquared * horizonDistanceSquared;
364372

373+
// Is there a solution?
365374
if (squareFactor > 1e-10)
366375
{
367376
const auto minusFactor = -(horizonDistance * targetSpeed);
368377

369-
if (abs(baseFactor) < 1e-10)
378+
if (std::abs(baseFactor) < 1e-10)
370379
{
371-
travelTime = abs(horizonDistance) > 1e-10 ? (static_cast<int>(theDistanceSquared / (2 * horizonDistance * targetSpeed)) + 1) : 0;
380+
travelTime = std::abs(horizonDistance) > 1e-10 ? (static_cast<int>(theDistanceSquared / (2 * horizonDistance * targetSpeed)) + 1) : 0;
372381
}
373382
else
374383
{
@@ -402,7 +411,7 @@ void BombardTrajectory::CalculateDisperseBurst(BulletClass* pBullet)
402411
{
403412
const auto pType = this->Type;
404413

405-
if (!this->UseDisperseBurst && abs(pType->RotateCoord) > 1e-10 && this->CountOfBurst > 1)
414+
if (!this->UseDisperseBurst && std::abs(pType->RotateCoord) > 1e-10 && this->CountOfBurst > 1)
406415
{
407416
const auto axis = pType->AxisOfRotation.Get();
408417

@@ -415,7 +424,7 @@ void BombardTrajectory::CalculateDisperseBurst(BulletClass* pBullet)
415424

416425
const auto rotationAxisLengthSquared = rotationAxis.MagnitudeSquared();
417426

418-
if (abs(rotationAxisLengthSquared) > 1e-10)
427+
if (std::abs(rotationAxisLengthSquared) > 1e-10)
419428
{
420429
double extraRotate = 0.0;
421430
rotationAxis *= 1 / sqrt(rotationAxisLengthSquared);
@@ -570,7 +579,7 @@ void BombardTrajectory::BulletVelocityChange(BulletClass* pBullet)
570579
if (pType->LeadTimeCalculate && pTarget)
571580
this->LastTargetCoord = pTarget->GetCoords();
572581

573-
pBullet->Velocity *= abs((this->Height - pBullet->Location.Z) / pBullet->Velocity.Z);
582+
pBullet->Velocity *= std::abs((this->Height - pBullet->Location.Z) / pBullet->Velocity.Z);
574583
}
575584
}
576585
}

src/Ext/Bullet/Trajectories/StraightTrajectory.cpp

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -264,66 +264,68 @@ void StraightTrajectory::PrepareForOpenFire(BulletClass* pBullet)
264264
{
265265
const auto pType = this->Type;
266266
double rotateAngle = 0.0;
267-
const auto pTarget = pBullet->Target;
268267
auto theTargetCoords = pBullet->TargetCoords;
269268
auto theSourceCoords = pBullet->SourceCoords;
270269

271270
// TODO If I could calculate this before firing, perhaps it can solve the problem of one frame delay and not so correct turret orientation.
272-
if (pType->LeadTimeCalculate && pTarget)
271+
if (pType->LeadTimeCalculate)
273272
{
274-
theTargetCoords = pTarget->GetCoords();
275-
theSourceCoords = pBullet->Location;
276-
277-
// Solving trigonometric functions
278-
if (theTargetCoords != this->LastTargetCoord)
273+
if (const auto pTarget = pBullet->Target)
279274
{
280-
const auto extraOffsetCoord = theTargetCoords - this->LastTargetCoord;
281-
const auto targetSourceCoord = theSourceCoords - theTargetCoords;
282-
const auto lastSourceCoord = theSourceCoords - this->LastTargetCoord;
275+
theTargetCoords = pTarget->GetCoords();
276+
theSourceCoords = pBullet->Location;
283277

284-
const auto theDistanceSquared = targetSourceCoord.MagnitudeSquared();
285-
const auto targetSpeedSquared = extraOffsetCoord.MagnitudeSquared();
286-
const auto targetSpeed = sqrt(targetSpeedSquared);
278+
// Solving trigonometric functions
279+
if (theTargetCoords != this->LastTargetCoord)
280+
{
281+
const auto extraOffsetCoord = theTargetCoords - this->LastTargetCoord;
282+
const auto targetSourceCoord = theSourceCoords - theTargetCoords;
283+
const auto lastSourceCoord = theSourceCoords - this->LastTargetCoord;
287284

288-
const auto crossFactor = lastSourceCoord.CrossProduct(targetSourceCoord).MagnitudeSquared();
289-
const auto verticalDistanceSquared = crossFactor / targetSpeedSquared;
285+
const auto theDistanceSquared = targetSourceCoord.MagnitudeSquared();
286+
const auto targetSpeedSquared = extraOffsetCoord.MagnitudeSquared();
287+
const auto targetSpeed = sqrt(targetSpeedSquared);
290288

291-
const auto horizonDistanceSquared = theDistanceSquared - verticalDistanceSquared;
292-
const auto horizonDistance = sqrt(horizonDistanceSquared);
289+
const auto crossFactor = lastSourceCoord.CrossProduct(targetSourceCoord).MagnitudeSquared();
290+
const auto verticalDistanceSquared = crossFactor / targetSpeedSquared;
293291

294-
const auto straightSpeedSquared = pType->Trajectory_Speed * pType->Trajectory_Speed;
295-
const auto baseFactor = straightSpeedSquared - targetSpeedSquared;
296-
const auto squareFactor = baseFactor * verticalDistanceSquared + straightSpeedSquared * horizonDistanceSquared;
292+
const auto horizonDistanceSquared = theDistanceSquared - verticalDistanceSquared;
293+
const auto horizonDistance = sqrt(horizonDistanceSquared);
297294

298-
// Is there a solution?
299-
if (squareFactor > 1e-10)
300-
{
301-
const auto minusFactor = -(horizonDistance * targetSpeed);
302-
int travelTime = 0;
295+
const auto straightSpeedSquared = pType->Trajectory_Speed * pType->Trajectory_Speed;
296+
const auto baseFactor = straightSpeedSquared - targetSpeedSquared;
297+
const auto squareFactor = baseFactor * verticalDistanceSquared + straightSpeedSquared * horizonDistanceSquared;
303298

304-
if (std::abs(baseFactor) < 1e-10)
299+
// Is there a solution?
300+
if (squareFactor > 1e-10)
305301
{
306-
travelTime = std::abs(horizonDistance) > 1e-10 ? (static_cast<int>(theDistanceSquared / (2 * horizonDistance * targetSpeed)) + 1) : 0;
307-
}
308-
else
309-
{
310-
const auto travelTimeM = static_cast<int>((minusFactor - sqrt(squareFactor)) / baseFactor);
311-
const auto travelTimeP = static_cast<int>((minusFactor + sqrt(squareFactor)) / baseFactor);
312-
313-
if (travelTimeM > 0 && travelTimeP > 0)
314-
travelTime = travelTimeM < travelTimeP ? travelTimeM : travelTimeP;
315-
else if (travelTimeM > 0)
316-
travelTime = travelTimeM;
317-
else if (travelTimeP > 0)
318-
travelTime = travelTimeP;
319-
320-
if (targetSourceCoord.MagnitudeSquared() < lastSourceCoord.MagnitudeSquared())
321-
travelTime += 1;
302+
const auto minusFactor = -(horizonDistance * targetSpeed);
303+
int travelTime = 0;
304+
305+
if (std::abs(baseFactor) < 1e-10)
306+
{
307+
travelTime = std::abs(horizonDistance) > 1e-10 ? (static_cast<int>(theDistanceSquared / (2 * horizonDistance * targetSpeed)) + 1) : 0;
308+
}
322309
else
323-
travelTime += 2;
310+
{
311+
const auto travelTimeM = static_cast<int>((minusFactor - sqrt(squareFactor)) / baseFactor);
312+
const auto travelTimeP = static_cast<int>((minusFactor + sqrt(squareFactor)) / baseFactor);
313+
314+
if (travelTimeM > 0 && travelTimeP > 0)
315+
travelTime = travelTimeM < travelTimeP ? travelTimeM : travelTimeP;
316+
else if (travelTimeM > 0)
317+
travelTime = travelTimeM;
318+
else if (travelTimeP > 0)
319+
travelTime = travelTimeP;
320+
321+
if (targetSourceCoord.MagnitudeSquared() < lastSourceCoord.MagnitudeSquared())
322+
travelTime += 1;
323+
else
324+
travelTime += 2;
325+
}
326+
327+
theTargetCoords += extraOffsetCoord * travelTime;
324328
}
325-
326-
theTargetCoords += extraOffsetCoord * travelTime;
327329
}
328330
}
329331
}

0 commit comments

Comments
 (0)