2021-08-28 10:47:57 +02:00
|
|
|
#include "luabindings.hpp"
|
|
|
|
|
|
|
|
#include <components/lua/luastate.hpp>
|
2022-07-10 17:27:00 +02:00
|
|
|
#include <components/detournavigator/navigator.hpp>
|
|
|
|
#include <components/detournavigator/navigatorutils.hpp>
|
|
|
|
#include <components/settings/settings.hpp>
|
2021-08-28 10:47:57 +02:00
|
|
|
|
|
|
|
#include "../mwbase/environment.hpp"
|
|
|
|
#include "../mwbase/world.hpp"
|
|
|
|
#include "../mwphysics/raycasting.hpp"
|
|
|
|
|
2022-03-18 00:27:16 +01:00
|
|
|
#include "luamanagerimp.hpp"
|
2021-08-28 10:47:57 +02:00
|
|
|
#include "worldview.hpp"
|
|
|
|
|
|
|
|
namespace sol
|
|
|
|
{
|
|
|
|
template <>
|
|
|
|
struct is_automagical<MWPhysics::RayCastingResult> : std::false_type {};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace MWLua
|
|
|
|
{
|
|
|
|
sol::table initNearbyPackage(const Context& context)
|
|
|
|
{
|
|
|
|
sol::table api(context.mLua->sol(), sol::create);
|
|
|
|
WorldView* worldView = context.mWorldView;
|
|
|
|
|
|
|
|
sol::usertype<MWPhysics::RayCastingResult> rayResult =
|
|
|
|
context.mLua->sol().new_usertype<MWPhysics::RayCastingResult>("RayCastingResult");
|
|
|
|
rayResult["hit"] = sol::readonly_property([](const MWPhysics::RayCastingResult& r) { return r.mHit; });
|
|
|
|
rayResult["hitPos"] = sol::readonly_property([](const MWPhysics::RayCastingResult& r) -> sol::optional<osg::Vec3f>
|
|
|
|
{
|
|
|
|
if (r.mHit)
|
|
|
|
return r.mHitPos;
|
|
|
|
else
|
|
|
|
return sol::nullopt;
|
|
|
|
});
|
|
|
|
rayResult["hitNormal"] = sol::readonly_property([](const MWPhysics::RayCastingResult& r) -> sol::optional<osg::Vec3f>
|
|
|
|
{
|
|
|
|
if (r.mHit)
|
|
|
|
return r.mHitNormal;
|
|
|
|
else
|
|
|
|
return sol::nullopt;
|
|
|
|
});
|
|
|
|
rayResult["hitObject"] = sol::readonly_property([worldView](const MWPhysics::RayCastingResult& r) -> sol::optional<LObject>
|
|
|
|
{
|
|
|
|
if (r.mHitObject.isEmpty())
|
|
|
|
return sol::nullopt;
|
|
|
|
else
|
|
|
|
return LObject(getId(r.mHitObject), worldView->getObjectRegistry());
|
|
|
|
});
|
|
|
|
|
2022-05-16 00:16:26 +02:00
|
|
|
api["COLLISION_TYPE"] = LuaUtil::makeStrictReadOnly(context.mLua->tableFromPairs<std::string_view, MWPhysics::CollisionType>({
|
2021-11-11 22:35:41 +01:00
|
|
|
{"World", MWPhysics::CollisionType_World},
|
|
|
|
{"Door", MWPhysics::CollisionType_Door},
|
|
|
|
{"Actor", MWPhysics::CollisionType_Actor},
|
|
|
|
{"HeightMap", MWPhysics::CollisionType_HeightMap},
|
|
|
|
{"Projectile", MWPhysics::CollisionType_Projectile},
|
|
|
|
{"Water", MWPhysics::CollisionType_Water},
|
2021-04-11 10:28:56 +02:00
|
|
|
{"Default", MWPhysics::CollisionType_Default},
|
|
|
|
{"AnyPhysical", MWPhysics::CollisionType_AnyPhysical},
|
|
|
|
{"Camera", MWPhysics::CollisionType_CameraOnly},
|
|
|
|
{"VisualOnly", MWPhysics::CollisionType_VisualOnly},
|
2021-11-11 22:35:41 +01:00
|
|
|
}));
|
2021-08-28 10:47:57 +02:00
|
|
|
|
2021-09-28 12:25:37 +02:00
|
|
|
api["castRay"] = [](const osg::Vec3f& from, const osg::Vec3f& to, sol::optional<sol::table> options)
|
2021-08-28 10:47:57 +02:00
|
|
|
{
|
|
|
|
MWWorld::Ptr ignore;
|
2021-09-28 12:25:37 +02:00
|
|
|
int collisionType = MWPhysics::CollisionType_Default;
|
2021-08-28 10:47:57 +02:00
|
|
|
float radius = 0;
|
|
|
|
if (options)
|
|
|
|
{
|
|
|
|
sol::optional<LObject> ignoreObj = options->get<sol::optional<LObject>>("ignore");
|
|
|
|
if (ignoreObj) ignore = ignoreObj->ptr();
|
|
|
|
collisionType = options->get<sol::optional<int>>("collisionType").value_or(collisionType);
|
|
|
|
radius = options->get<sol::optional<float>>("radius").value_or(0);
|
|
|
|
}
|
|
|
|
const MWPhysics::RayCastingInterface* rayCasting = MWBase::Environment::get().getWorld()->getRayCasting();
|
|
|
|
if (radius <= 0)
|
|
|
|
return rayCasting->castRay(from, to, ignore, std::vector<MWWorld::Ptr>(), collisionType);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!ignore.isEmpty()) throw std::logic_error("Currently castRay doesn't support `ignore` when radius > 0");
|
|
|
|
return rayCasting->castSphere(from, to, radius, collisionType);
|
|
|
|
}
|
|
|
|
};
|
2021-08-27 09:26:38 +02:00
|
|
|
// TODO: async raycasting
|
2021-09-28 12:25:37 +02:00
|
|
|
/*api["asyncCastRay"] = [luaManager = context.mLuaManager](
|
2021-08-27 09:26:38 +02:00
|
|
|
const Callback& luaCallback, const osg::Vec3f& from, const osg::Vec3f& to, sol::optional<sol::table> options)
|
|
|
|
{
|
|
|
|
std::function<void(MWPhysics::RayCastingResult)> callback =
|
|
|
|
luaManager->wrapLuaCallback<MWPhysics::RayCastingResult>(luaCallback);
|
|
|
|
MWPhysics::RayCastingInterface* rayCasting = MWBase::Environment::get().getWorld()->getRayCasting();
|
|
|
|
|
|
|
|
// Handle options the same way as in `castRay`.
|
|
|
|
|
|
|
|
// NOTE: `callback` is not thread safe. If MWPhysics works in separate thread, it must put results to a queue
|
|
|
|
// and use this callback from the main thread at the beginning of the next frame processing.
|
|
|
|
rayCasting->asyncCastRay(callback, from, to, ignore, std::vector<MWWorld::Ptr>(), collisionType);
|
|
|
|
};*/
|
2022-03-18 00:27:16 +01:00
|
|
|
api["castRenderingRay"] = [manager=context.mLuaManager](const osg::Vec3f& from, const osg::Vec3f& to)
|
|
|
|
{
|
|
|
|
if (!manager->isProcessingInputEvents())
|
|
|
|
{
|
|
|
|
throw std::logic_error("castRenderingRay can be used only in player scripts during processing of input events; "
|
|
|
|
"use asyncCastRenderingRay instead.");
|
|
|
|
}
|
|
|
|
MWPhysics::RayCastingResult res;
|
|
|
|
MWBase::Environment::get().getWorld()->castRenderingRay(res, from, to, false, false);
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
api["asyncCastRenderingRay"] =
|
|
|
|
[manager=context.mLuaManager](const LuaUtil::Callback& callback, const osg::Vec3f& from, const osg::Vec3f& to)
|
|
|
|
{
|
|
|
|
manager->addAction([manager, callback, from, to]
|
|
|
|
{
|
|
|
|
MWPhysics::RayCastingResult res;
|
|
|
|
MWBase::Environment::get().getWorld()->castRenderingRay(res, from, to, false, false);
|
|
|
|
manager->queueCallback(callback, sol::make_object(callback.mFunc.lua_state(), res));
|
|
|
|
});
|
|
|
|
};
|
2021-08-28 10:47:57 +02:00
|
|
|
|
|
|
|
api["activators"] = LObjectList{worldView->getActivatorsInScene()};
|
|
|
|
api["actors"] = LObjectList{worldView->getActorsInScene()};
|
|
|
|
api["containers"] = LObjectList{worldView->getContainersInScene()};
|
|
|
|
api["doors"] = LObjectList{worldView->getDoorsInScene()};
|
|
|
|
api["items"] = LObjectList{worldView->getItemsInScene()};
|
2022-07-10 17:27:00 +02:00
|
|
|
|
|
|
|
api["NAVIGATOR_FLAGS"] = LuaUtil::makeStrictReadOnly(
|
|
|
|
context.mLua->tableFromPairs<std::string_view, DetourNavigator::Flag>({
|
|
|
|
{"Walk", DetourNavigator::Flag_walk},
|
|
|
|
{"Swim", DetourNavigator::Flag_swim},
|
|
|
|
{"OpenDoor", DetourNavigator::Flag_openDoor},
|
|
|
|
{"UsePathgrid", DetourNavigator::Flag_usePathgrid},
|
|
|
|
}));
|
|
|
|
|
|
|
|
api["COLLISION_SHAPE_TYPE"] = LuaUtil::makeStrictReadOnly(
|
|
|
|
context.mLua->tableFromPairs<std::string_view, DetourNavigator::CollisionShapeType>({
|
|
|
|
{"Aabb", DetourNavigator::CollisionShapeType::Aabb},
|
|
|
|
{"RotatingBox", DetourNavigator::CollisionShapeType::RotatingBox},
|
|
|
|
}));
|
|
|
|
|
|
|
|
api["FIND_PATH_STATUS"] = LuaUtil::makeStrictReadOnly(
|
|
|
|
context.mLua->tableFromPairs<std::string_view, DetourNavigator::Status>({
|
|
|
|
{"Success", DetourNavigator::Status::Success},
|
|
|
|
{"PartialPath", DetourNavigator::Status::PartialPath},
|
|
|
|
{"NavMeshNotFound", DetourNavigator::Status::NavMeshNotFound},
|
|
|
|
{"StartPolygonNotFound", DetourNavigator::Status::StartPolygonNotFound},
|
|
|
|
{"EndPolygonNotFound", DetourNavigator::Status::EndPolygonNotFound},
|
|
|
|
{"MoveAlongSurfaceFailed", DetourNavigator::Status::MoveAlongSurfaceFailed},
|
|
|
|
{"FindPathOverPolygonsFailed", DetourNavigator::Status::FindPathOverPolygonsFailed},
|
|
|
|
{"GetPolyHeightFailed", DetourNavigator::Status::GetPolyHeightFailed},
|
|
|
|
{"InitNavMeshQueryFailed", DetourNavigator::Status::InitNavMeshQueryFailed},
|
|
|
|
}));
|
|
|
|
|
|
|
|
static const DetourNavigator::AgentBounds defaultAgentBounds {
|
|
|
|
DetourNavigator::defaultCollisionShapeType,
|
|
|
|
Settings::Manager::getVector3("default actor pathfind half extents", "Game"),
|
|
|
|
};
|
|
|
|
static const float defaultStepSize = 2 * std::max(defaultAgentBounds.mHalfExtents.x(), defaultAgentBounds.mHalfExtents.y());
|
|
|
|
static constexpr DetourNavigator::Flags defaultIncludeFlags = DetourNavigator::Flag_walk
|
|
|
|
| DetourNavigator::Flag_swim
|
|
|
|
| DetourNavigator::Flag_openDoor
|
|
|
|
| DetourNavigator::Flag_usePathgrid;
|
|
|
|
|
|
|
|
api["findPath"] = [] (const osg::Vec3f& source, const osg::Vec3f& destination,
|
|
|
|
const sol::optional<sol::table>& options)
|
|
|
|
{
|
|
|
|
DetourNavigator::AgentBounds agentBounds = defaultAgentBounds;
|
|
|
|
float stepSize = defaultStepSize;
|
|
|
|
DetourNavigator::Flags includeFlags = defaultIncludeFlags;
|
|
|
|
DetourNavigator::AreaCosts areaCosts {};
|
|
|
|
float destinationTolerance = 1;
|
|
|
|
|
|
|
|
if (options.has_value())
|
|
|
|
{
|
|
|
|
if (const auto& t = options->get<sol::optional<sol::table>>("agentBounds"))
|
|
|
|
{
|
|
|
|
if (const auto& v = t->get<sol::optional<DetourNavigator::CollisionShapeType>>("shapeType"))
|
|
|
|
agentBounds.mShapeType = *v;
|
|
|
|
if (const auto& v = t->get<sol::optional<osg::Vec3f>>("halfExtents"))
|
|
|
|
{
|
|
|
|
agentBounds.mHalfExtents = *v;
|
|
|
|
stepSize = 2 * std::max(v->x(), v->y());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (const auto& v = options->get<sol::optional<float>>("stepSize"))
|
|
|
|
stepSize = *v;
|
|
|
|
if (const auto& v = options->get<sol::optional<DetourNavigator::Flags>>("includeFlags"))
|
|
|
|
includeFlags = *v;
|
|
|
|
if (const auto& t = options->get<sol::optional<sol::table>>("areaCosts"))
|
|
|
|
{
|
|
|
|
if (const auto& v = t->get<sol::optional<float>>("water"))
|
|
|
|
areaCosts.mWater = *v;
|
|
|
|
if (const auto& v = t->get<sol::optional<float>>("door"))
|
|
|
|
areaCosts.mDoor = *v;
|
|
|
|
if (const auto& v = t->get<sol::optional<float>>("pathgrid"))
|
|
|
|
areaCosts.mPathgrid = *v;
|
|
|
|
if (const auto& v = t->get<sol::optional<float>>("ground"))
|
|
|
|
areaCosts.mGround = *v;
|
|
|
|
}
|
|
|
|
if (const auto& v = options->get<sol::optional<float>>("destinationTolerance"))
|
|
|
|
destinationTolerance = *v;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<osg::Vec3f> result;
|
|
|
|
|
|
|
|
const DetourNavigator::Status status = DetourNavigator::findPath(
|
|
|
|
*MWBase::Environment::get().getWorld()->getNavigator(), agentBounds, stepSize, source,
|
|
|
|
destination, includeFlags, areaCosts, destinationTolerance, std::back_inserter(result));
|
|
|
|
|
|
|
|
return std::make_tuple(status, std::move(result));
|
|
|
|
};
|
|
|
|
|
|
|
|
api["findRandomPointAroundCircle"] = [] (const osg::Vec3f& position, float maxRadius,
|
|
|
|
const sol::optional<sol::table>& options)
|
|
|
|
{
|
|
|
|
DetourNavigator::AgentBounds agentBounds = defaultAgentBounds;
|
|
|
|
DetourNavigator::Flags includeFlags = defaultIncludeFlags;
|
|
|
|
|
|
|
|
if (options.has_value())
|
|
|
|
{
|
|
|
|
if (const auto& t = options->get<sol::optional<sol::table>>("agentBounds"))
|
|
|
|
{
|
|
|
|
if (const auto& v = t->get<sol::optional<DetourNavigator::CollisionShapeType>>("shapeType"))
|
|
|
|
agentBounds.mShapeType = *v;
|
|
|
|
if (const auto& v = t->get<sol::optional<osg::Vec3f>>("halfExtents"))
|
|
|
|
agentBounds.mHalfExtents = *v;
|
|
|
|
}
|
|
|
|
if (const auto& v = options->get<sol::optional<DetourNavigator::Flags>>("includeFlags"))
|
|
|
|
includeFlags = *v;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr auto getRandom = []
|
|
|
|
{
|
|
|
|
return Misc::Rng::rollProbability(MWBase::Environment::get().getWorld()->getPrng());
|
|
|
|
};
|
|
|
|
|
|
|
|
return DetourNavigator::findRandomPointAroundCircle(*MWBase::Environment::get().getWorld()->getNavigator(),
|
|
|
|
agentBounds, position, maxRadius, includeFlags, getRandom);
|
|
|
|
};
|
|
|
|
|
|
|
|
api["castNavigationRay"] = [] (const osg::Vec3f& from, const osg::Vec3f& to,
|
|
|
|
const sol::optional<sol::table>& options)
|
|
|
|
{
|
|
|
|
DetourNavigator::AgentBounds agentBounds = defaultAgentBounds;
|
|
|
|
DetourNavigator::Flags includeFlags = defaultIncludeFlags;
|
|
|
|
|
|
|
|
if (options.has_value())
|
|
|
|
{
|
|
|
|
if (const auto& t = options->get<sol::optional<sol::table>>("agentBounds"))
|
|
|
|
{
|
|
|
|
if (const auto& v = t->get<sol::optional<DetourNavigator::CollisionShapeType>>("shapeType"))
|
|
|
|
agentBounds.mShapeType = *v;
|
|
|
|
if (const auto& v = t->get<sol::optional<osg::Vec3f>>("halfExtents"))
|
|
|
|
agentBounds.mHalfExtents = *v;
|
|
|
|
}
|
|
|
|
if (const auto& v = options->get<sol::optional<DetourNavigator::Flags>>("includeFlags"))
|
|
|
|
includeFlags = *v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DetourNavigator::raycast(*MWBase::Environment::get().getWorld()->getNavigator(),
|
|
|
|
agentBounds, from, to, includeFlags);
|
|
|
|
};
|
|
|
|
|
2021-08-28 10:47:57 +02:00
|
|
|
return LuaUtil::makeReadOnly(api);
|
|
|
|
}
|
|
|
|
}
|