Skip to content

Commit

Permalink
fix #6306
Browse files Browse the repository at this point in the history
  • Loading branch information
rtri committed Sep 22, 2019
1 parent a73df2d commit ca1557c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
14 changes: 9 additions & 5 deletions rts/Game/GameHelper.cpp
Expand Up @@ -791,20 +791,24 @@ CUnit* CGameHelper::GetClosestEnemyUnitNoLosTest(
const float3& pos,
float searchRadius,
int searchAllyteam,
bool sphere,
bool testSphere,
bool canBeBlind
) {
if (sphere) {
CUnit* closestUnit = nullptr;

if (testSphere) {
// includes target radius
Query::ClosestUnit_InLos q(pos, searchRadius, canBeBlind);
QueryUnits(Filter::Enemy(excludeUnit, searchAllyteam), q);
return q.GetClosestUnit();
closestUnit = q.GetClosestUnit();
} else {
// cylinder (doesn't include target radius)
// excludes target radius
Query::ClosestUnit_InLos_Cylinder q(pos, searchRadius, canBeBlind);
QueryUnits(Filter::Enemy(excludeUnit, searchAllyteam), q);
return q.GetClosestUnit();
closestUnit = q.GetClosestUnit();
}

return closestUnit;
}

CUnit* CGameHelper::GetClosestFriendlyUnit(const CUnit* excludeUnit, const float3& pos, float searchRadius, int searchAllyteam)
Expand Down
2 changes: 1 addition & 1 deletion rts/Game/GameHelper.h
Expand Up @@ -68,7 +68,7 @@ class CGameHelper
const float3& pos,
float searchRadius,
int searchAllyteam,
bool sphere,
bool testSphere,
bool canBeBlind
);
static CUnit* GetClosestFriendlyUnit(const CUnit* excludeUnit, const float3& pos, float searchRadius, int searchAllyteam);
Expand Down
13 changes: 4 additions & 9 deletions rts/Lua/LuaSyncedRead.cpp
Expand Up @@ -2516,21 +2516,16 @@ int LuaSyncedRead::GetUnitNearestEnemy(lua_State* L)
if (unit == nullptr)
return 0;

const float range = luaL_optnumber(L, 2, 1.0e9f);
const bool useLos =
!CLuaHandle::GetHandleFullRead(L) || !lua_isboolean(L, 3) || lua_toboolean(L, 3);
const CUnit* target = nullptr;
const bool wantLOS = !lua_isboolean(L, 3) || lua_toboolean(L, 3);
const bool testLOS = !CLuaHandle::GetHandleFullRead(L) || wantLOS;

if (useLos) {
target = CGameHelper::GetClosestEnemyUnit(unit, unit->pos, range, unit->allyteam);
} else {
target = CGameHelper::GetClosestEnemyUnitNoLosTest(unit, unit->pos, range, unit->allyteam, false, true);
}
const CUnit* target = CGameHelper::GetClosestEnemyUnitNoLosTest(unit, unit->pos, luaL_optnumber(L, 2, 1.0e9f), unit->allyteam, false, !testLOS);

if (target != nullptr) {
lua_pushnumber(L, target->id);
return 1;
}

return 0;
}

Expand Down
4 changes: 3 additions & 1 deletion rts/Sim/Units/Unit.cpp
Expand Up @@ -2390,6 +2390,8 @@ bool CUnit::GetNewCloakState(bool stunCheck) {
assert(wantCloak);

// grab nearest enemy wrt our default decloak-distance
// (pass NoLosTest=true s.t. gadgets can decide how to
// react to cloaked enemy units within decloakDistance)
// Lua code can do more elaborate scans if it wants to
// and has access to cloakCost{Moving}/decloakDistance
//
Expand All @@ -2398,7 +2400,7 @@ bool CUnit::GetNewCloakState(bool stunCheck) {
const CUnit* closestEnemy = this;

if (!stunCheck)
closestEnemy = CGameHelper::GetClosestEnemyUnitNoLosTest(nullptr, midPos, decloakDistance, allyteam, unitDef->decloakSpherical, false);
closestEnemy = CGameHelper::GetClosestEnemyUnitNoLosTest(this, midPos, decloakDistance, allyteam, unitDef->decloakSpherical, true);

return (eventHandler.AllowUnitCloak(this, closestEnemy));
}
Expand Down

1 comment on commit ca1557c

@FLOZi
Copy link
Contributor

@FLOZi FLOZi commented on ca1557c Sep 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks!

Please sign in to comment.