Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TraceRay Lua Functions #1624

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions rts/Lua/LuaSyncedRead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "Sim/Misc/QuadField.h"
#include "Sim/Misc/TeamHandler.h"
#include "Sim/Misc/Wind.h"
#include "Sim/Misc/CollisionHandler.h"
#include "Sim/MoveTypes/StrafeAirMoveType.h"
#include "Sim/MoveTypes/GroundMoveType.h"
#include "Sim/MoveTypes/HoverAirMoveType.h"
Expand Down Expand Up @@ -382,6 +383,10 @@ bool LuaSyncedRead::PushEntries(lua_State* L)
REGISTER_LUA_CFUNC(GetFeaturePiecePosDir);
REGISTER_LUA_CFUNC(GetFeaturePieceMatrix);

REGISTER_LUA_CFUNC(TraceRayFeatures);
REGISTER_LUA_CFUNC(TraceRayGround);
REGISTER_LUA_CFUNC(TraceRayUnits);

REGISTER_LUA_CFUNC(GetRadarErrorParams);

if (!LuaMetalMap::PushReadEntries(L))
Expand Down Expand Up @@ -8515,8 +8520,121 @@ int LuaSyncedRead::GetUnitScriptNames(lua_State* L)

return 1;
}
/***
*
* @function Spring.TraceRayUnits
* @number posX
* @number posY
* @number posZ
* @number dirX
* @number dirY
* @number dirZ
* @number traceLength
* @treturn {{len, Id},...}
*/

int LuaSyncedRead::TraceRayUnits(lua_State* L) //returns the list of units that an raytrace has hit
{
float3 pos(luaL_checkfloat(L, 1), luaL_checkfloat(L, 2), luaL_checkfloat(L, 3));
float3 dir(luaL_checkfloat(L, 4), luaL_checkfloat(L, 5), luaL_checkfloat(L, 6));
float traceLength = luaL_checkfloat(L, 7);
QuadFieldQuery qfQuery;
quadField.GetQuadsOnRay(qfQuery, pos, dir, traceLength);
CollisionQuery cq;
int num = 0;
lua_newtable(L);
for (const int quadIdx : *qfQuery.quads) {
const CQuadField::Quad& quad = quadField.GetQuad(quadIdx);

for (CUnit* u : quad.units) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be good if the returned list was sorted in order of distance, from the closest.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you not reverse it in lua?


if (!u->HasCollidableStateBit(CSolidObject::CSTATE_BIT_QUADMAPRAYS))
continue;

if (CCollisionHandler::DetectHit(u, u->GetTransformMatrix(true), pos, pos + dir * traceLength, &cq, true)) {
const float len = cq.GetHitPosDist(pos, dir);
lua_newtable(L);
lua_pushnumber(L, len);
lua_rawseti(L, -2, 1);
lua_pushnumber(L, u->id);
lua_rawseti(L, -2, 2);
lua_rawseti(L, -2, num + 1);
num++;
}
}
}
return 1;
}
/***
*
* @function Spring.TraceRayFeatures
* @number posX
* @number posY
* @number posZ
* @number dirX
* @number dirY
* @number dirZ
* @number traceLength
* @treturn {{len, Id},...}
*/
int LuaSyncedRead::TraceRayFeatures(lua_State* L) //returns the list of features that an raytrace has hit
{
float3 pos(luaL_checkfloat(L, 1), luaL_checkfloat(L, 2), luaL_checkfloat(L, 3));
float3 dir(luaL_checkfloat(L, 4), luaL_checkfloat(L, 5), luaL_checkfloat(L, 6));
float traceLength = luaL_checkfloat(L, 7);
QuadFieldQuery qfQuery;
quadField.GetQuadsOnRay(qfQuery, pos, dir, traceLength);
CollisionQuery cq;
int num = 0;
lua_newtable(L);
for (const int quadIdx : *qfQuery.quads) {
const CQuadField::Quad& quad = quadField.GetQuad(quadIdx);

for (CFeature* f : quad.features) {
if (!f->HasCollidableStateBit(CSolidObject::CSTATE_BIT_QUADMAPRAYS))
continue;

if (CCollisionHandler::DetectHit(f, f->GetTransformMatrix(true), pos, pos + dir * traceLength, &cq, true)) {
const float len = cq.GetHitPosDist(pos, dir);
lua_newtable(L);
lua_pushnumber(L, len);
lua_rawseti(L, -2, 1);
lua_pushnumber(L, f->id);
lua_rawseti(L, -2, 2);
lua_rawseti(L, -2, num + 1);
num++;
}
}
}
return 1;
}
/***
*
* @function Spring.TraceRayGround
* @number posX
* @number posY
* @number posZ
* @number dirX
* @number dirY
* @number dirZ
* @number traceLength
* @treturn nil|len
*/
int LuaSyncedRead::TraceRayGround(lua_State* L)
12345swordy marked this conversation as resolved.
Show resolved Hide resolved
{
float3 pos(luaL_checkfloat(L, 1), luaL_checkfloat(L, 2), luaL_checkfloat(L, 3));
float3 dir(luaL_checkfloat(L, 4), luaL_checkfloat(L, 5), luaL_checkfloat(L, 6));
float traceLength = luaL_checkfloat(L, 7);
const float groundLength = CGround::LineGroundCol(pos, pos + dir * traceLength);

if (traceLength > groundLength && groundLength > 0.0f) {
traceLength = groundLength;
}
if (-1.0f != groundLength)
12345swordy marked this conversation as resolved.
Show resolved Hide resolved
return 0;
lua_pushnumber(L, traceLength);
return 1;
}
/******************************************************************************
* Misc
*
Expand Down
7 changes: 3 additions & 4 deletions rts/Lua/LuaSyncedRead.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,9 @@ class LuaSyncedRead {

static int GetRadarErrorParams(lua_State* L);

static int TraceRay(lua_State* L); //TODO: not implemented
static int TraceRayUnits(lua_State* L); //TODO: not implemented
static int TraceRayFeatures(lua_State* L); //TODO: not implemented
static int TraceRayGround(lua_State* L); //TODO: not implemented
static int TraceRayUnits(lua_State* L);
static int TraceRayFeatures(lua_State* L);
static int TraceRayGround(lua_State* L);
};

#endif /* LUA_SYNCED_READ_H */