From a16c4269a57e1c48ed03ed9f396737aeff3e0f9d Mon Sep 17 00:00:00 2001 From: 12345swordy Date: Tue, 30 Jul 2024 22:01:54 -0500 Subject: [PATCH] Add ground trace ray functions --- rts/Lua/LuaSyncedRead.cpp | 78 +++++++++++++++++++++++++++++++++++++++ rts/Lua/LuaSyncedRead.h | 3 +- 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/rts/Lua/LuaSyncedRead.cpp b/rts/Lua/LuaSyncedRead.cpp index a6bed0597d..e00da4188d 100644 --- a/rts/Lua/LuaSyncedRead.cpp +++ b/rts/Lua/LuaSyncedRead.cpp @@ -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" @@ -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)) @@ -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); + 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 diff --git a/rts/Lua/LuaSyncedRead.h b/rts/Lua/LuaSyncedRead.h index e6fae6952a..916fb97035 100644 --- a/rts/Lua/LuaSyncedRead.h +++ b/rts/Lua/LuaSyncedRead.h @@ -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 */