Skip to content

Commit

Permalink
fix #6076
Browse files Browse the repository at this point in the history
  • Loading branch information
rt committed Nov 11, 2018
1 parent fabec3d commit 15d6cc6
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 24 deletions.
13 changes: 8 additions & 5 deletions rts/Game/GameHelper.cpp
Expand Up @@ -647,22 +647,25 @@ void CGameHelper::GenerateWeaponTargets(const CWeapon* weapon, const CUnit* avoi
const float3& ownerPos = weaponOwner->pos;
const float3 testPos;

const float baseRange = weapon->range;
const float scanRadius = baseRange + weapon->autoTargetRangeBoost;
const float aimHeight = weapon->aimFromPos.y;
const float aimPosHeight = weapon->aimFromPos.y;
const float minMapHeight = std::max(0.0f, readMap->GetInitMinHeight());

// how much damage the weapon deals over 1 second
const float secDamage = weaponDmg->GetDefault() * weapon->salvoSize / weapon->reloadTime * GAME_SPEED;
const float heightMod = weaponDef->heightmod;

const float baseRange = weapon->range;
const float rangeBoost = weapon->autoTargetRangeBoost;
const float scanRadius = weapon->GetRange2D(rangeBoost, (aimPosHeight - minMapHeight) * heightMod);

// [0] := default, [1,2,3,4,5,6] := target is {avoidee, in bad category, crashing, last attacker, paralyzed, outside unboosted range}
constexpr float tgtPriorityMults[] = {1.0f, 10.0f, 100.0f, 1000.0f, 0.5f, 4.0f, 100000.0f};

const bool paralyzer = (weaponDmg->paralyzeDamageTime != 0);

// copy on purpose since the below calls lua
QuadFieldQuery qfQuery;
quadField.GetQuads(qfQuery, ownerPos, scanRadius + (aimHeight - std::max(0.0f, readMap->GetInitMinHeight())) * heightMod);
quadField.GetQuads(qfQuery, ownerPos, scanRadius);

const int tempNum = gs->GetTempNum();

Expand Down Expand Up @@ -696,7 +699,7 @@ void CGameHelper::GenerateWeaponTargets(const CWeapon* weapon, const CUnit* avoi
continue;
}

const float modRange = scanRadius + (aimHeight - targetPos.y) * heightMod;
const float modRange = weapon->GetRange2D(rangeBoost, (aimPosHeight - targetPos.y) * heightMod);
const float sqDist2D = ownerPos.SqDistance2D(targetPos);

if (sqDist2D > Square(modRange))
Expand Down
4 changes: 2 additions & 2 deletions rts/Sim/Weapons/Cannon.h
Expand Up @@ -33,8 +33,8 @@ class CCannon: public CWeapon
void UpdateWantedDir() override final { wantedDir = GetWantedDir(currentTargetPos - aimFromPos); }
void SlowUpdate() override final;

float GetRange2D(float yDiff, float rbFac, float hbFac) const { return (GetStaticRange2D({range, yDiff}, {projectileSpeed, gravity}, {rbFac, hbFac})); }
float GetRange2D(const float yDiff) const override final { return (GetRange2D(yDiff, rangeBoostFactor, heightBoostFactor)); }
float GetRange2D(float ydiff, float rbFac, float hbFac) const { return (GetStaticRange2D({range, ydiff}, {projectileSpeed, gravity}, {rbFac, hbFac})); }
float GetRange2D(float boost, float ydiff) const override final { return (GetRange2D(ydiff, rangeBoostFactor, heightBoostFactor)); }

// baseConsts{.x := weaponDefRange, .y := modHeightDif}
// projConsts{.x := speed, .y := gravity}
Expand Down
11 changes: 3 additions & 8 deletions rts/Sim/Weapons/StarburstLauncher.cpp
Expand Up @@ -44,15 +44,10 @@ void CStarburstLauncher::FireImpl(const bool scriptCall)

bool CStarburstLauncher::HaveFreeLineOfFire(const float3 srcPos, const float3 tgtPos, const SWeaponTarget& trg) const
{
const float3& wdir = weaponDef->fixedLauncher? weaponDir: UpVector;

if (TraceRay::TestCone(srcPos, wdir, 100.0f, 0.0f, owner->allyteam, avoidFlags, owner))
return false;

return true;
return (!TraceRay::TestCone(srcPos, weaponDef->fixedLauncher? weaponDir: UpVector, 100.0f, 0.0f, owner->allyteam, avoidFlags, owner));
}

float CStarburstLauncher::GetRange2D(const float yDiff) const
float CStarburstLauncher::GetRange2D(float boost, float ydiff) const
{
return range + (yDiff * weaponDef->heightmod);
return range + (ydiff * weaponDef->heightmod);
}
2 changes: 1 addition & 1 deletion rts/Sim/Weapons/StarburstLauncher.h
Expand Up @@ -11,7 +11,7 @@ class CStarburstLauncher: public CWeapon
public:
CStarburstLauncher(CUnit* owner = nullptr, const WeaponDef* def = nullptr);

float GetRange2D(const float yDiff) const override final;
float GetRange2D(float boost, float ydiff) const override final;

private:
const float3& GetAimFromPos(bool useMuzzle = false) const override { return weaponMuzzlePos; }
Expand Down
13 changes: 7 additions & 6 deletions rts/Sim/Weapons/Weapon.cpp
Expand Up @@ -655,6 +655,7 @@ bool CWeapon::AutoTarget()
// save the "best" bad target in case we have no other
// good targets (of higher priority) left in <targets>
const bool isBadTarget = (unit->category & badTargetCategory);

if (isBadTarget && (badTargetUnit != nullptr))
continue;

Expand Down Expand Up @@ -854,6 +855,7 @@ bool CWeapon::TryTarget(const float3 tgtPos, const SWeaponTarget& trg, bool preF

if (!TestTarget(tgtPos, trg))
return false;

// auto-targeted units are allowed to be out of range
// (UpdateFire will still block firing at such units)
if (!trg.isAutoTarget && !TestRange(tgtPos, trg))
Expand Down Expand Up @@ -890,7 +892,6 @@ bool CWeapon::TestTarget(const float3 tgtPos, const SWeaponTarget& trg) const
return false;
if (!trg.isUserTarget && trg.unit->IsNeutral() && owner->fireState < FIRESTATE_FIREATNEUTRAL)
return false;

// don't fire at allied targets
if (!trg.isUserTarget && teamHandler.Ally(owner->allyteam, trg.unit->allyteam))
return false;
Expand Down Expand Up @@ -944,11 +945,11 @@ bool CWeapon::TestRange(const float3 tgtPos, const SWeaponTarget& trg) const

if (trg.type == Target_Pos || weaponDef->cylinderTargeting < 0.01f) {
// check range in a sphere (with extra radius <heightDiff * heightMod>)
weaponRange = GetRange2D(heightDiff * weaponDef->heightmod);
weaponRange = GetRange2D(0.0f, heightDiff * weaponDef->heightmod);
} else {
// check range in a cylinder (with height <cylinderTargeting * range>)
if ((weaponDef->cylinderTargeting * range) > (math::fabsf(heightDiff) * weaponDef->heightmod)) {
weaponRange = GetRange2D(0.0f);
weaponRange = GetRange2D(0.0f, 0.0f);
}
}

Expand Down Expand Up @@ -1180,10 +1181,10 @@ float CWeapon::GetStaticRange2D(const CWeapon* w, const WeaponDef* wd, float mod
}


float CWeapon::GetRange2D(const float yDiff) const
float CWeapon::GetRange2D(float boost, float ydiff) const
{
const float rangeSq = range * range;
const float ydiffSq = yDiff * yDiff;
const float rangeSq = Square(range + boost);
const float ydiffSq = Square(ydiff);
const float root = rangeSq - ydiffSq;
return (math::sqrt(std::max(root, 0.0f)));
}
Expand Down
4 changes: 2 additions & 2 deletions rts/Sim/Weapons/Weapon.h
Expand Up @@ -73,9 +73,9 @@ class CWeapon : public CObject
float TargetWeight(const CUnit* unit) const;

static float GetStaticRange2D(const CWeapon* w, const WeaponDef* wd, float modHeightDiff, float modProjGravity);
static float GetLiveRange2D(const CWeapon* w, const WeaponDef* wd, float modHeightDiff, float modProjGravity) { return (w->GetRange2D(modHeightDiff)); }
static float GetLiveRange2D(const CWeapon* w, const WeaponDef* wd, float modHeightDiff, float modProjGravity) { return (w->GetRange2D(0.0f, modHeightDiff)); }

virtual float GetRange2D(const float yDiff) const;
virtual float GetRange2D(float boost, float ydiff) const;
virtual void UpdateProjectileSpeed(const float val) { projectileSpeed = val; }
virtual void UpdateRange(const float val) { range = val; }

Expand Down

0 comments on commit 15d6cc6

Please sign in to comment.