Skip to content

Commit

Permalink
Apply radar error to lua spatial queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
saurtron committed Dec 12, 2024
1 parent 05c5724 commit ec3183e
Showing 1 changed file with 100 additions and 39 deletions.
139 changes: 100 additions & 39 deletions rts/Lua/LuaSyncedRead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2908,6 +2908,8 @@ int LuaSyncedRead::GetTeamUnitCount(lua_State* L)
// unit
// readTeam for MY_UNIT_TEST
// allegiance for SIMPLE_TEAM_TEST and VISIBLE_TEAM_TEST
// readAllyTeam for ALLY_UNIT_TEST and ENEMY_UNIT_TEST
// readAllyTeam, fullRead for UNIT_ERROR_POS

#define NULL_TEST ; // always passes

Expand All @@ -2925,12 +2927,32 @@ int LuaSyncedRead::GetTeamUnitCount(lua_State* L)
if (unit->team != readTeam) { continue; }

#define ALLY_UNIT_TEST \
if (unit->allyteam != CLuaHandle::GetHandleReadAllyTeam(L)) { continue; }
if (unit->allyteam != readAllyTeam) { continue; }

#define ENEMY_UNIT_TEST \
if (unit->allyteam == CLuaHandle::GetHandleReadAllyTeam(L)) { continue; } \
if (unit->allyteam == readAllyTeam) { continue; } \
if (!LuaUtils::IsUnitVisible(L, unit)) { continue; }

#define UNIT_POS \
const float3& p = unit->midPos;

#define UNIT_ERROR_POS \
float3 p = unit->midPos; \
if (!LuaUtils::IsAllyUnit(L, unit)) \
p += unit->GetLuaErrorVector(readAllyTeam, fullRead);


/* Apply team error to planar mins/maxs boxes */
void ApplyPlanarTeamError(lua_State* L, int allegiance, float3& mins, float3& maxs) {
if ((allegiance >= 0 && !LuaUtils::IsAlliedTeam(L, allegiance)) ||
!(allegiance == LuaUtils::MyUnits || allegiance == LuaUtils::AllyUnits)) {
const int readAllyTeam = CLuaHandle::GetHandleReadAllyTeam(L);
const float allyTeamError = losHandler->GetAllyTeamRadarErrorSize(readAllyTeam);
const float3 allyTeamError3(allyTeamError, 0.0f, allyTeamError);
mins -= allyTeamError3;
maxs += allyTeamError3;
}
}

/***
*
Expand All @@ -2949,36 +2971,51 @@ int LuaSyncedRead::GetUnitsInRectangle(lua_State* L)
const float xmax = luaL_checkfloat(L, 3);
const float zmax = luaL_checkfloat(L, 4);

const float3 mins(xmin, 0.0f, zmin);
const float3 maxs(xmax, 0.0f, zmax);
float3 mins(xmin, 0.0f, zmin);
float3 maxs(xmax, 0.0f, zmax);

const int allegiance = LuaUtils::ParseAllegiance(L, __func__, 5);
const int readAllyTeam = CLuaHandle::GetHandleReadAllyTeam(L);
const bool fullRead = CLuaHandle::GetHandleFullRead(L);

#define RECTANGLE_TEST ; // no test, GetUnitsExact is sufficient
#define NULL_TEST ; // no test, GetUnitsExact is sufficient

#define RECTANGLE_TEST \
const float x = p.x; \
const float z = p.z; \
if ((x < xmin) || (x > xmax)) { \
continue; \
} \
if ((z < zmin) || (z > zmax)) { \
continue; \
}

if (!fullRead)
ApplyPlanarTeamError(L, allegiance, mins, maxs);

QuadFieldQuery qfQuery;
quadField.GetUnitsExact(qfQuery, mins, maxs);
const auto& units = (*qfQuery.units);

if (allegiance >= 0) {
if (LuaUtils::IsAlliedTeam(L, allegiance)) {
LOOP_UNIT_CONTAINER(SIMPLE_TEAM_TEST, RECTANGLE_TEST, true);
LOOP_UNIT_CONTAINER(SIMPLE_TEAM_TEST, NULL_TEST, true);
} else {
LOOP_UNIT_CONTAINER(VISIBLE_TEAM_TEST, RECTANGLE_TEST, true);
LOOP_UNIT_CONTAINER(VISIBLE_TEAM_TEST, UNIT_ERROR_POS RECTANGLE_TEST, true);
}
}
else if (allegiance == LuaUtils::MyUnits) {
const int readTeam = CLuaHandle::GetHandleReadTeam(L);
LOOP_UNIT_CONTAINER(MY_UNIT_TEST, RECTANGLE_TEST, true);
LOOP_UNIT_CONTAINER(MY_UNIT_TEST, NULL_TEST, true);
}
else if (allegiance == LuaUtils::AllyUnits) {
LOOP_UNIT_CONTAINER(ALLY_UNIT_TEST, RECTANGLE_TEST, true);
LOOP_UNIT_CONTAINER(ALLY_UNIT_TEST, NULL_TEST, true);
}
else if (allegiance == LuaUtils::EnemyUnits) {
LOOP_UNIT_CONTAINER(ENEMY_UNIT_TEST, RECTANGLE_TEST, true);
LOOP_UNIT_CONTAINER(ENEMY_UNIT_TEST, UNIT_ERROR_POS RECTANGLE_TEST, true);
}
else { // AllUnits
LOOP_UNIT_CONTAINER(VISIBLE_TEST, RECTANGLE_TEST, true);
LOOP_UNIT_CONTAINER(VISIBLE_TEST, UNIT_ERROR_POS RECTANGLE_TEST, true);
}

return 1;
Expand Down Expand Up @@ -3006,40 +3043,56 @@ int LuaSyncedRead::GetUnitsInBox(lua_State* L)
const float ymax = luaL_checkfloat(L, 5);
const float zmax = luaL_checkfloat(L, 6);

const float3 mins(xmin, 0.0f, zmin);
const float3 maxs(xmax, 0.0f, zmax);
float3 mins(xmin, 0.0f, zmin);
float3 maxs(xmax, 0.0f, zmax);

const int allegiance = LuaUtils::ParseAllegiance(L, __func__, 7);
const int readAllyTeam = CLuaHandle::GetHandleReadAllyTeam(L);
const bool fullRead = CLuaHandle::GetHandleFullRead(L);

#define BOX_TEST \
const float y = unit->midPos.y; \
const float y = p.y; \
if ((y < ymin) || (y > ymax)) { \
continue; \
continue; \
}

#define BOX_TEST_FULL \
BOX_TEST \
const float x = p.x; \
if ((x < xmin) || (x > xmax)) { \
continue; \
} \
const float z = p.z; \
if ((z < zmin) || (z > zmax)) { \
continue; \
}

if (!fullRead)
ApplyPlanarTeamError(L, allegiance, mins, maxs);

QuadFieldQuery qfQuery;
quadField.GetUnitsExact(qfQuery, mins, maxs);
const auto& units = (*qfQuery.units);

if (allegiance >= 0) {
if (LuaUtils::IsAlliedTeam(L, allegiance)) {
LOOP_UNIT_CONTAINER(SIMPLE_TEAM_TEST, BOX_TEST, true);
LOOP_UNIT_CONTAINER(SIMPLE_TEAM_TEST, UNIT_POS BOX_TEST, true);
} else {
LOOP_UNIT_CONTAINER(VISIBLE_TEAM_TEST, BOX_TEST, true);
LOOP_UNIT_CONTAINER(VISIBLE_TEAM_TEST, UNIT_ERROR_POS BOX_TEST_FULL, true);
}
}
else if (allegiance == LuaUtils::MyUnits) {
const int readTeam = CLuaHandle::GetHandleReadTeam(L);
LOOP_UNIT_CONTAINER(MY_UNIT_TEST, BOX_TEST, true);
LOOP_UNIT_CONTAINER(MY_UNIT_TEST, UNIT_POS BOX_TEST, true);
}
else if (allegiance == LuaUtils::AllyUnits) {
LOOP_UNIT_CONTAINER(ALLY_UNIT_TEST, BOX_TEST, true);
LOOP_UNIT_CONTAINER(ALLY_UNIT_TEST, UNIT_POS BOX_TEST, true);
}
else if (allegiance == LuaUtils::EnemyUnits) {
LOOP_UNIT_CONTAINER(ENEMY_UNIT_TEST, BOX_TEST, true);
LOOP_UNIT_CONTAINER(ENEMY_UNIT_TEST, UNIT_ERROR_POS BOX_TEST_FULL, true);
}
else { // AllUnits
LOOP_UNIT_CONTAINER(VISIBLE_TEST, BOX_TEST, true);
LOOP_UNIT_CONTAINER(VISIBLE_TEST, UNIT_ERROR_POS BOX_TEST_FULL, true);
}

return 1;
Expand All @@ -3061,43 +3114,47 @@ int LuaSyncedRead::GetUnitsInCylinder(lua_State* L)
const float radius = luaL_checkfloat(L, 3);
const float radSqr = (radius * radius);

const float3 mins(x - radius, 0.0f, z - radius);
const float3 maxs(x + radius, 0.0f, z + radius);
float3 mins(x - radius, 0.0f, z - radius);
float3 maxs(x + radius, 0.0f, z + radius);

const int allegiance = LuaUtils::ParseAllegiance(L, __func__, 4);
const int readAllyTeam = CLuaHandle::GetHandleReadAllyTeam(L);
const bool fullRead = CLuaHandle::GetHandleFullRead(L);

#define CYLINDER_TEST \
const float3& p = unit->midPos; \
const float dx = (p.x - x); \
const float dz = (p.z - z); \
const float dist = ((dx * dx) + (dz * dz)); \
if (dist > radSqr) { \
continue; \
} \

if (!fullRead)
ApplyPlanarTeamError(L, allegiance, mins, maxs);

QuadFieldQuery qfQuery;
quadField.GetUnitsExact(qfQuery, mins, maxs);
const auto& units = (*qfQuery.units);

if (allegiance >= 0) {
if (LuaUtils::IsAlliedTeam(L, allegiance)) {
LOOP_UNIT_CONTAINER(SIMPLE_TEAM_TEST, CYLINDER_TEST, true);
LOOP_UNIT_CONTAINER(SIMPLE_TEAM_TEST, UNIT_POS CYLINDER_TEST, true);
} else {
LOOP_UNIT_CONTAINER(VISIBLE_TEAM_TEST, CYLINDER_TEST, true);
LOOP_UNIT_CONTAINER(VISIBLE_TEAM_TEST, UNIT_ERROR_POS CYLINDER_TEST, true);
}
}
else if (allegiance == LuaUtils::MyUnits) {
const int readTeam = CLuaHandle::GetHandleReadTeam(L);
LOOP_UNIT_CONTAINER(MY_UNIT_TEST, CYLINDER_TEST, true);
LOOP_UNIT_CONTAINER(MY_UNIT_TEST, UNIT_POS CYLINDER_TEST, true);
}
else if (allegiance == LuaUtils::AllyUnits) {
LOOP_UNIT_CONTAINER(ALLY_UNIT_TEST, CYLINDER_TEST, true);
LOOP_UNIT_CONTAINER(ALLY_UNIT_TEST, UNIT_POS CYLINDER_TEST, true);
}
else if (allegiance == LuaUtils::EnemyUnits) {
LOOP_UNIT_CONTAINER(ENEMY_UNIT_TEST, CYLINDER_TEST, true);
LOOP_UNIT_CONTAINER(ENEMY_UNIT_TEST, UNIT_ERROR_POS CYLINDER_TEST, true);
}
else { // AllUnits
LOOP_UNIT_CONTAINER(VISIBLE_TEST, CYLINDER_TEST, true);
LOOP_UNIT_CONTAINER(VISIBLE_TEST, UNIT_ERROR_POS CYLINDER_TEST, true);
}

return 1;
Expand All @@ -3122,13 +3179,14 @@ int LuaSyncedRead::GetUnitsInSphere(lua_State* L)
const float radSqr = (radius * radius);

const float3 pos(x, y, z);
const float3 mins(x - radius, 0.0f, z - radius);
const float3 maxs(x + radius, 0.0f, z + radius);
float3 mins(x - radius, 0.0f, z - radius);
float3 maxs(x + radius, 0.0f, z + radius);

const int allegiance = LuaUtils::ParseAllegiance(L, __func__, 5);
const int readAllyTeam = CLuaHandle::GetHandleReadAllyTeam(L);
const bool fullRead = CLuaHandle::GetHandleFullRead(L);

#define SPHERE_TEST \
const float3& p = unit->midPos; \
const float dx = (p.x - x); \
const float dy = (p.y - y); \
const float dz = (p.z - z); \
Expand All @@ -3138,29 +3196,32 @@ int LuaSyncedRead::GetUnitsInSphere(lua_State* L)
continue; \
} \

if (!fullRead)
ApplyPlanarTeamError(L, allegiance, mins, maxs);

QuadFieldQuery qfQuery;
quadField.GetUnitsExact(qfQuery, mins, maxs);
const auto& units = (*qfQuery.units);

if (allegiance >= 0) {
if (LuaUtils::IsAlliedTeam(L, allegiance)) {
LOOP_UNIT_CONTAINER(SIMPLE_TEAM_TEST, SPHERE_TEST, true);
LOOP_UNIT_CONTAINER(SIMPLE_TEAM_TEST, UNIT_POS SPHERE_TEST, true);
} else {
LOOP_UNIT_CONTAINER(VISIBLE_TEAM_TEST, SPHERE_TEST, true);
LOOP_UNIT_CONTAINER(VISIBLE_TEAM_TEST, UNIT_ERROR_POS SPHERE_TEST, true);
}
}
else if (allegiance == LuaUtils::MyUnits) {
const int readTeam = CLuaHandle::GetHandleReadTeam(L);
LOOP_UNIT_CONTAINER(MY_UNIT_TEST, SPHERE_TEST, true);
LOOP_UNIT_CONTAINER(MY_UNIT_TEST, UNIT_POS SPHERE_TEST, true);
}
else if (allegiance == LuaUtils::AllyUnits) {
LOOP_UNIT_CONTAINER(ALLY_UNIT_TEST, SPHERE_TEST, true);
LOOP_UNIT_CONTAINER(ALLY_UNIT_TEST, UNIT_POS SPHERE_TEST, true);
}
else if (allegiance == LuaUtils::EnemyUnits) {
LOOP_UNIT_CONTAINER(ENEMY_UNIT_TEST, SPHERE_TEST, true);
LOOP_UNIT_CONTAINER(ENEMY_UNIT_TEST, UNIT_ERROR_POS SPHERE_TEST, true);
}
else { // AllUnits
LOOP_UNIT_CONTAINER(VISIBLE_TEST, SPHERE_TEST, true);
LOOP_UNIT_CONTAINER(VISIBLE_TEST, UNIT_ERROR_POS SPHERE_TEST, true);
}

return 1;
Expand Down

0 comments on commit ec3183e

Please sign in to comment.