Skip to content

Commit 54a4b35

Browse files
committed
Simplify vector calculation for Straight
- Simplify vector calculation - Place commonly used values outside the loop for calculation - Fix doc typo
1 parent a7ca25f commit 54a4b35

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

docs/New-or-Enhanced-Logics.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,15 +757,15 @@ Trajectory.Speed=100.0 ; floating point value
757757
- `Trajectory.Straight.PassDetonate` enables extra detonations when the projectile is traveling. (You can use this when you want the projectile to detonate warheads every other distance/time during the flight.)
758758
- `Trajectory.Straight.PassDetonateWarhead` defines the warhead detonated by `Trajectory.Straight.PassDetonate`, and `Trajectory.Straight.PassDetonateDamage` defines the damage caused by `Trajectory.Straight.PassDetonateWarhead`.
759759
- `Trajectory.Straight.PassDetonateDelay` controls the delay for detonating the warhead defined by `Trajectory.Straight.Warhead`.
760-
- `Trajectory.Straight.PassDetonateInitialDelay` controls the initial delay for detonating the warhead defined by `Trajectory.Straight.Warhead`.
760+
- `Trajectory.Straight.PassDetonateInitialDelay` controls the initial delay for detonating the warhead defined by `Trajectory.Straight.PassDetonateWarhead`.
761761
- `Trajectory.Straight.PassDetonateLocal` controls whether `Trajectory.Straight.PassDetonateWarhead` and weapon's `Warhead` are always detonate at ground level. It will also no longer restrict vertical velocity of the projectile when using `Trajectory.Straight.ConfineAtHeight`.
762762
- `Trajectory.Straight.LeadTimeCalculate` controls whether the projectile need to calculate the lead time of the target when firing. Note that this will not affect the facing of the turret.
763763
- `Trajectory.Straight.OffsetCoord` controls the offsets of the target. Projectile will aim at this position to attack. It also supports `Inaccurate=yes` and `Trajectory.Straight.LeadTimeCalculate=true` on this basis.
764764
- `Trajectory.Straight.RotateCoord` controls whether to rotate the projectile's firing direction within the angle bisector of `Trajectory.Straight.OffsetCoord` according to the weapon's `Burst`. Set to 0 to disable this function.
765765
- `Trajectory.Straight.MirrorCoord` controls whether `Trajectory.Straight.OffsetCoord` need to mirror the lateral value to adapt to the current burst index. At the same time, the rotation direction calculated by `Trajectory.Straight.RotateCoord` will also be reversed, and the rotation angle between each adjacent projectile on each side will not change as a result.
766766
- `Trajectory.Straight.UseDisperseBurst` controls whether the calculation of `Trajectory.Straight.RotateCoord` is based on its superior's `Trajectory.Disperse.WeaponBurst` of the dispersed trajectory, rather than `Burst` of the weapon. If this value is not appropriate, it will result in unsatisfactory visual displays.
767767
- `Trajectory.Straight.AxisOfRotation` controls the rotation axis when calculating `Trajectory.Straight.RotateCoord`. The axis will rotates with the unit orientation or the vector that from target position to the source position.
768-
- `Trajectory.Straight.ProximityImpact` controls the initial proximity fuse times. When there are enough remaining times and the projectile approaches another valid target, it will detonate a warhead defined by `Trajectory.Straight.Warhead` on it. If the times is about to run out, it will also detonate itself at its location. This function can be cancelled by setting to 0. A negative integer means unlimited times. By the way, you can use the weapon's `Warhead` with low versus only to aim at the target, and use the `Trajectory.Straight.ProximityWarhead` to causing actual harm. (You can use this to cause non repeated damage to all units encountered during the flight of the projectile.)
768+
- `Trajectory.Straight.ProximityImpact` controls the initial proximity fuse times. When there are enough remaining times and the projectile approaches another valid target, it will detonate a warhead defined by `Trajectory.Straight.ProximityWarhead` on it. If the times is about to run out, it will also detonate itself at its location. This function can be cancelled by setting to 0. A negative integer means unlimited times. By the way, you can use the weapon's `Warhead` with low versus only to aim at the target, and use the `Trajectory.Straight.ProximityWarhead` to causing actual harm. (You can use this to cause non repeated damage to all units encountered during the flight of the projectile.)
769769
- `Trajectory.Straight.ProximityWarhead` defines the warhead detonated by `Trajectory.Straight.ProximityImpact`, and `Trajectory.Straight.ProximityDamage` defines the damage caused by `Trajectory.Straight.ProximityWarhead`.
770770
- `Trajectory.Straight.ProximityRadius` controls the range of proximity fuse. It can NOT be set as a negative integer.
771771
- `Trajectory.Straight.ProximityDirect` controls whether let the target receive damage instead of detonating the warhead.

src/Ext/Bullet/Trajectories/StraightTrajectory.cpp

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,8 @@ void StraightTrajectory::PrepareForDetonateAt(BulletClass* pBullet, HouseClass*
787787
return;
788788

789789
// Step 1: Find valid targets on the ground within range.
790-
std::vector<CellClass*> recCellClass = PhobosTrajectoryType::GetCellsInProximityRadius(pBullet, pType->ProximityRadius.Get());
790+
const auto radius = pType->ProximityRadius.Get();
791+
std::vector<CellClass*> recCellClass = PhobosTrajectoryType::GetCellsInProximityRadius(pBullet, radius);
791792
const size_t cellSize = recCellClass.size() * 2;
792793
size_t vectSize = cellSize;
793794
size_t thisSize = 0;
@@ -798,10 +799,11 @@ void StraightTrajectory::PrepareForDetonateAt(BulletClass* pBullet, HouseClass*
798799
static_cast<int>(pBullet->Velocity.Y),
799800
static_cast<int>(pBullet->Velocity.Z)
800801
};
802+
const auto velocitySq = velocityCrd.MagnitudeSquared();
803+
const auto pTarget = pBullet->Target;
801804

802805
std::vector<TechnoClass*> validTechnos;
803806
validTechnos.reserve(vectSize);
804-
const auto pTarget = pBullet->Target;
805807

806808
for (const auto& pRecCell : recCellClass)
807809
{
@@ -824,19 +826,23 @@ void StraightTrajectory::PrepareForDetonateAt(BulletClass* pBullet, HouseClass*
824826
if (!pType->ProximityAllies && pOwner && pOwner->IsAlliedWith(pTechno->Owner) && pTechno != pTarget)
825827
continue;
826828

827-
const auto distanceCrd = pTechno->GetCoords() - pBullet->SourceCoords;
828-
const auto locationCrd = (velocityCrd + (pBullet->Location - pBullet->SourceCoords));
829-
const auto terminalCrd = distanceCrd - locationCrd;
830-
auto distance = locationCrd.MagnitudeSquared(); // Not true distance yet.
829+
// Check distance
830+
const auto targetCrd = pTechno->GetCoords();
831+
const auto pathCrd = targetCrd - pBullet->SourceCoords;
832+
833+
if (pathCrd * velocityCrd < 0) // In front of the techno
834+
continue;
835+
836+
const auto distanceCrd = targetCrd - pBullet->Location;
837+
const auto nextDistanceCrd = distanceCrd - velocityCrd;
831838

832-
// Between front and back
833-
if (distanceCrd * velocityCrd < 0 || terminalCrd * velocityCrd > 0)
839+
if (nextDistanceCrd * velocityCrd > 0) // Behind the bullet
834840
continue;
835841

836-
distance = (distance > 1e-10) ? sqrt(distanceCrd.CrossProduct(terminalCrd).MagnitudeSquared() / distance) : distanceCrd.Magnitude();
842+
const auto cross = distanceCrd.CrossProduct(nextDistanceCrd).MagnitudeSquared();
843+
const auto distance = (velocitySq > 1e-10) ? sqrt(cross / velocitySq) : distanceCrd.Magnitude();
837844

838-
// Between left and right (cylindrical)
839-
if (technoType != AbstractType::Building && distance > pType->ProximityRadius.Get())
845+
if (technoType != AbstractType::Building && distance > radius) // In the cylinder
840846
continue;
841847

842848
if (thisSize >= vectSize)
@@ -854,26 +860,35 @@ void StraightTrajectory::PrepareForDetonateAt(BulletClass* pBullet, HouseClass*
854860
if (pType->ProximityFlight)
855861
{
856862
const auto airTracker = &AircraftTrackerClass::Instance;
857-
airTracker->FillCurrentVector(MapClass::Instance->GetCellAt(pBullet->Location + velocityCrd * 0.5), static_cast<int>((pType->ProximityRadius.Get() + pType->Trajectory_Speed / 2) / Unsorted::LeptonsPerCell));
863+
airTracker->FillCurrentVector(MapClass::Instance->GetCellAt(pBullet->Location + velocityCrd * 0.5),
864+
Game::F2I(sqrt(radius * radius + (velocitySq / 4)) / Unsorted::LeptonsPerCell));
858865

859866
for (auto pTechno = airTracker->Get(); pTechno; pTechno = airTracker->Get())
860867
{
861868
if (!pTechno->IsAlive || !pTechno->IsOnMap || pTechno->Health <= 0 || pTechno->InLimbo || pTechno->IsSinking)
862869
continue;
863870

871+
// Not directly harming friendly forces
864872
if (!pType->ProximityAllies && pOwner && pOwner->IsAlliedWith(pTechno->Owner) && pTechno != pTarget)
865873
continue;
866874

867-
const auto distanceCrd = pTechno->GetCoords() - pBullet->Location;
868-
const auto terminalCrd = distanceCrd - velocityCrd;
869-
auto distance = velocityCrd.MagnitudeSquared(); // Not true distance yet.
875+
// Check distance
876+
const auto targetCrd = pTechno->GetCoords();
877+
const auto pathCrd = targetCrd - pBullet->SourceCoords;
878+
879+
if (pathCrd * velocityCrd < 0) // In front of the techno
880+
continue;
881+
882+
const auto distanceCrd = targetCrd - pBullet->Location;
883+
const auto nextDistanceCrd = distanceCrd - velocityCrd;
870884

871-
if (distanceCrd * velocityCrd < 0 || terminalCrd * velocityCrd > 0)
885+
if (nextDistanceCrd * velocityCrd > 0) // Behind the bullet
872886
continue;
873887

874-
distance = (distance > 1e-10) ? sqrt(distanceCrd.CrossProduct(terminalCrd).MagnitudeSquared() / distance) : distanceCrd.Magnitude();
888+
const auto cross = distanceCrd.CrossProduct(nextDistanceCrd).MagnitudeSquared();
889+
const auto distance = (velocitySq > 1e-10) ? sqrt(cross / velocitySq) : distanceCrd.Magnitude();
875890

876-
if (distance > pType->ProximityRadius.Get())
891+
if (distance > radius) // In the cylinder
877892
continue;
878893

879894
if (thisSize >= vectSize)

0 commit comments

Comments
 (0)