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 trace ray ground Lua functions #1706

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
78 changes: 78 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 @@ -379,6 +380,9 @@ bool LuaSyncedRead::PushEntries(lua_State* L)
REGISTER_LUA_CFUNC(GetFeaturePiecePosDir);
REGISTER_LUA_CFUNC(GetFeaturePieceMatrix);

REGISTER_LUA_CFUNC(TraceRayGroundInDirection);
REGISTER_LUA_CFUNC(TraceRayGroundBetweenPositions);

REGISTER_LUA_CFUNC(GetRadarErrorParams);

if (!LuaMetalMap::PushReadEntries(L))
Expand Down Expand Up @@ -8592,6 +8596,80 @@ int LuaSyncedRead::GetUnitScriptNames(lua_State* L)
return 1;
}

static int TraceRayGroundImpl(lua_State *const L, const float3 &pos, const float3 &dir, const float maxLen, const bool testWater)
{
const float rayLength = CGround::LineGroundWaterCol(pos, dir, maxLen, testWater, CLuaHandle::GetHandleSynced(L));
if (rayLength == -1.0f)
return 0;

const auto collisionSpot = pos + dir * rayLength; // FIXME: would be nice if the CGround:: functions returned this so we wouldn't have to recalculate

lua_pushnumber(L, rayLength);
lua_pushnumber(L, collisionSpot.x);
lua_pushnumber(L, collisionSpot.y);
lua_pushnumber(L, collisionSpot.z);
return 4;
}

/*** Checks for a ground collision in given direction
*
* @function Spring.TraceRayGroundInDirection
*
* Checks if there is surface (ground, optionally water) towards a vector
* and returns the distance to the closest hit and its position, if any.
*
* @number posX
* @number posY
* @number posZ
* @number dirX
* @number dirY
* @number dirZ
* @bool[opt=true] testWater
* @treturn number rayLength
* @treturn number posX
* @treturn number posY
* @treturn number posZ
*/
int LuaSyncedRead::TraceRayGroundInDirection(lua_State* L)
{
const float3 pos(luaL_checkfloat(L, 1), luaL_checkfloat(L, 2), luaL_checkfloat(L, 3));
const auto dir = float3(luaL_checkfloat(L, 4), luaL_checkfloat(L, 5), luaL_checkfloat(L, 6)).Normalize();
const float maxLen = luaL_optfloat(L, 7, 999999.f);
saurtron marked this conversation as resolved.
Show resolved Hide resolved
const bool testWater = luaL_optboolean(L, 8, true);

return TraceRayGroundImpl(L, pos, dir, maxLen, testWater);
}

/*** Checks for a ground collision between two positions
*
* @function Spring.TraceRayGroundBetweenPositions
*
* Checks if there is surface (ground, optionally water) between two positions
* and returns the distance to the closest hit and its position, if any.
*
* @number startX
* @number startY
* @number startZ
* @number endX
* @number endY
* @number endZ
* @bool[opt=true] testWater
* @treturn number rayLength
* @treturn number posX
* @treturn number posY
* @treturn number posZ
*/
int LuaSyncedRead::TraceRayGroundBetweenPositions(lua_State* L)
{
const float3 start (luaL_checkfloat(L, 1), luaL_checkfloat(L, 2), luaL_checkfloat(L, 3));
const float3 end (luaL_checkfloat(L, 4), luaL_checkfloat(L, 5), luaL_checkfloat(L, 6));
const bool testWater = luaL_optboolean(L, 7, true);

const auto [dir, length] = (end - start).GetNormalized();

return TraceRayGroundImpl(L, start, dir, length, testWater);
}


/******************************************************************************
* Misc
Expand Down
3 changes: 2 additions & 1 deletion rts/Lua/LuaSyncedRead.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ class LuaSyncedRead {
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 TraceRayGroundBetweenPositions(lua_State* L);
static int TraceRayGroundInDirection(lua_State* L);
};

#endif /* LUA_SYNCED_READ_H */