Files
WickedEngine/WickedEngine/wiPhysics_BindLua.cpp
T
2023-11-03 08:12:28 +01:00

510 lines
16 KiB
C++

#include "wiPhysics_BindLua.h"
#include "wiPhysics.h"
#include "wiScene_BindLua.h"
#include "wiMath_BindLua.h"
#include "wiPrimitive_BindLua.h"
namespace wi::lua
{
Luna<Physics_BindLua>::FunctionType Physics_BindLua::methods[] = {
lunamethod(Physics_BindLua, SetEnabled),
lunamethod(Physics_BindLua, IsEnabled),
lunamethod(Physics_BindLua, SetSimulationEnabled),
lunamethod(Physics_BindLua, IsSimulationEnabled),
lunamethod(Physics_BindLua, SetDebugDrawEnabled),
lunamethod(Physics_BindLua, IsDebugDrawEnabled),
lunamethod(Physics_BindLua, SetAccuracy),
lunamethod(Physics_BindLua, GetAccuracy),
lunamethod(Physics_BindLua, SetFrameRate),
lunamethod(Physics_BindLua, GetFrameRate),
lunamethod(Physics_BindLua, SetLinearVelocity),
lunamethod(Physics_BindLua, SetAngularVelocity),
lunamethod(Physics_BindLua, ApplyForceAt),
lunamethod(Physics_BindLua, ApplyForce),
lunamethod(Physics_BindLua, ApplyForceAt),
lunamethod(Physics_BindLua, ApplyImpulse),
lunamethod(Physics_BindLua, ApplyImpulseAt),
lunamethod(Physics_BindLua, ApplyTorque),
lunamethod(Physics_BindLua, ApplyTorqueImpulse),
lunamethod(Physics_BindLua, SetActivationState),
lunamethod(Physics_BindLua, Intersects),
lunamethod(Physics_BindLua, PickDrag),
{ NULL, NULL }
};
Luna<Physics_BindLua>::PropertyType Physics_BindLua::properties[] = {
{ NULL, NULL }
};
int Physics_BindLua::SetEnabled(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 0)
{
wi::physics::SetEnabled(wi::lua::SGetBool(L, 1));
}
else
wi::lua::SError(L, "SetEnabled(bool value) not enough arguments!");
return 0;
}
int Physics_BindLua::IsEnabled(lua_State* L)
{
wi::lua::SSetBool(L, wi::physics::IsEnabled());
return 1;
}
int Physics_BindLua::SetSimulationEnabled(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 0)
{
wi::physics::SetSimulationEnabled(wi::lua::SGetBool(L, 1));
}
else
wi::lua::SError(L, "SetSimulationEnabled(bool value) not enough arguments!");
return 0;
}
int Physics_BindLua::IsSimulationEnabled(lua_State* L)
{
wi::lua::SSetBool(L, wi::physics::IsSimulationEnabled());
return 1;
}
int Physics_BindLua::SetDebugDrawEnabled(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 0)
{
wi::physics::SetDebugDrawEnabled(wi::lua::SGetBool(L, 1));
}
else
wi::lua::SError(L, "SetDebugDrawEnabled(bool value) not enough arguments!");
return 0;
}
int Physics_BindLua::IsDebugDrawEnabled(lua_State* L)
{
wi::lua::SSetBool(L, wi::physics::IsDebugDrawEnabled());
return 1;
}
int Physics_BindLua::SetAccuracy(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 0)
{
wi::physics::SetAccuracy(wi::lua::SGetInt(L, 1));
}
else
wi::lua::SError(L, "SetAccuracy(int value) not enough arguments!");
return 0;
}
int Physics_BindLua::GetAccuracy(lua_State* L)
{
wi::lua::SSetInt(L, wi::physics::GetAccuracy());
return 1;
}
int Physics_BindLua::SetFrameRate(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 0)
{
wi::physics::SetFrameRate(wi::lua::SGetFloat(L, 1));
}
else
wi::lua::SError(L, "SetFrameRate(float value) not enough arguments!");
return 0;
}
int Physics_BindLua::GetFrameRate(lua_State* L)
{
wi::lua::SSetFloat(L, wi::physics::GetFrameRate());
return 1;
}
int Physics_BindLua::SetLinearVelocity(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 1)
{
scene::RigidBodyPhysicsComponent_BindLua* component = Luna<scene::RigidBodyPhysicsComponent_BindLua>::lightcheck(L, 1);
if (component == nullptr)
{
wi::lua::SError(L, "SetLinearVelocity(RigidBodyPhysicsComponent component, Vector velocity) first argument is not a RigidBodyPhysicsComponent!");
return 0;
}
Vector_BindLua* vec = Luna<Vector_BindLua>::lightcheck(L, 2);
if (vec == nullptr)
{
wi::lua::SError(L, "SetLinearVelocity(RigidBodyPhysicsComponent component, Vector velocity) second argument is not a Vector!");
return 0;
}
wi::physics::SetLinearVelocity(
*component->component,
*(XMFLOAT3*)vec
);
}
else
wi::lua::SError(L, "SetLinearVelocity(RigidBodyPhysicsComponent component, Vector velocity) not enough arguments!");
return 0;
}
int Physics_BindLua::SetAngularVelocity(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 1)
{
scene::RigidBodyPhysicsComponent_BindLua* component = Luna<scene::RigidBodyPhysicsComponent_BindLua>::lightcheck(L, 1);
if (component == nullptr)
{
wi::lua::SError(L, "SetAngularVelocity(RigidBodyPhysicsComponent component, Vector velocity) first argument is not a RigidBodyPhysicsComponent!");
return 0;
}
Vector_BindLua* vec = Luna<Vector_BindLua>::lightcheck(L, 2);
if (vec == nullptr)
{
wi::lua::SError(L, "SetAngularVelocity(RigidBodyPhysicsComponent component, Vector velocity) second argument is not a Vector!");
return 0;
}
wi::physics::SetAngularVelocity(
*component->component,
*(XMFLOAT3*)vec
);
}
else
wi::lua::SError(L, "SetAngularVelocity(RigidBodyPhysicsComponent component, Vector velocity) not enough arguments!");
return 0;
}
int Physics_BindLua::ApplyForce(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 1)
{
scene::RigidBodyPhysicsComponent_BindLua* component = Luna<scene::RigidBodyPhysicsComponent_BindLua>::lightcheck(L, 1);
if (component == nullptr)
{
wi::lua::SError(L, "ApplyForce(RigidBodyPhysicsComponent component, Vector force) first argument is not a RigidBodyPhysicsComponent!");
return 0;
}
Vector_BindLua* vec = Luna<Vector_BindLua>::lightcheck(L, 2);
if (vec == nullptr)
{
wi::lua::SError(L, "ApplyForce(RigidBodyPhysicsComponent component, Vector force) second argument is not a Vector!");
return 0;
}
wi::physics::ApplyForce(
*component->component,
*(XMFLOAT3*)vec
);
}
else
wi::lua::SError(L, "ApplyForce(RigidBodyPhysicsComponent component, Vector force) not enough arguments!");
return 0;
}
int Physics_BindLua::ApplyForceAt(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 2)
{
scene::RigidBodyPhysicsComponent_BindLua* component = Luna<scene::RigidBodyPhysicsComponent_BindLua>::lightcheck(L, 1);
if (component == nullptr)
{
wi::lua::SError(L, "ApplyForceAt(RigidBodyPhysicsComponent component, Vector force, Vector at) first argument is not a RigidBodyPhysicsComponent!");
return 0;
}
Vector_BindLua* vec = Luna<Vector_BindLua>::lightcheck(L, 2);
if (vec == nullptr)
{
wi::lua::SError(L, "ApplyForceAt(RigidBodyPhysicsComponent component, Vector force, Vector at) second argument is not a Vector!");
return 0;
}
Vector_BindLua* vec2 = Luna<Vector_BindLua>::lightcheck(L, 3);
if (vec == nullptr)
{
wi::lua::SError(L, "ApplyForceAt(RigidBodyPhysicsComponent component, Vector force, Vector at) third argument is not a Vector!");
return 0;
}
wi::physics::ApplyForceAt(
*component->component,
*(XMFLOAT3*)vec,
*(XMFLOAT3*)vec2
);
}
else
wi::lua::SError(L, "ApplyForceAt(RigidBodyPhysicsComponent component, Vector force, Vector at) not enough arguments!");
return 0;
}
int Physics_BindLua::ApplyImpulse(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 1)
{
scene::RigidBodyPhysicsComponent_BindLua* component = Luna<scene::RigidBodyPhysicsComponent_BindLua>::lightcheck(L, 1);
if (component == nullptr)
{
scene::HumanoidComponent_BindLua* humanoid = Luna<scene::HumanoidComponent_BindLua>::lightcheck(L, 1);
if (humanoid == nullptr)
{
wi::lua::SError(L, "ApplyImpulse(RigidBodyPhysicsComponent component, Vector impulse) first argument is not a RigidBodyPhysicsComponent!");
wi::lua::SError(L, "ApplyImpulse(HumanoidComponent component, HumanoidBone bone, Vector impulse) first argument is not a HumanoidComponent!");
return 0;
}
wi::scene::HumanoidComponent::HumanoidBone bone = (wi::scene::HumanoidComponent::HumanoidBone)wi::lua::SGetInt(L, 2);
Vector_BindLua* vec = Luna<Vector_BindLua>::lightcheck(L, 3);
if (vec == nullptr)
{
wi::lua::SError(L, "ApplyImpulse(HumanoidComponent component, HumanoidBone bone, Vector impulse) third argument is not a Vector!");
return 0;
}
wi::physics::ApplyImpulse(
*humanoid->component,
bone,
*(XMFLOAT3*)vec
);
return 0;
}
Vector_BindLua* vec = Luna<Vector_BindLua>::lightcheck(L, 2);
if (vec == nullptr)
{
wi::lua::SError(L, "ApplyImpulse(RigidBodyPhysicsComponent component, Vector impulse) second argument is not a Vector!");
return 0;
}
wi::physics::ApplyImpulse(
*component->component,
*(XMFLOAT3*)vec
);
}
else
wi::lua::SError(L, "ApplyImpulse(RigidBodyPhysicsComponent component, Vector impulse) not enough arguments!");
return 0;
}
int Physics_BindLua::ApplyImpulseAt(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 2)
{
scene::RigidBodyPhysicsComponent_BindLua* component = Luna<scene::RigidBodyPhysicsComponent_BindLua>::lightcheck(L, 1);
if (component == nullptr)
{
scene::HumanoidComponent_BindLua* humanoid = Luna<scene::HumanoidComponent_BindLua>::lightcheck(L, 1);
if (humanoid == nullptr)
{
wi::lua::SError(L, "ApplyImpulseAt(RigidBodyPhysicsComponent component, Vector impulse, Vector at) first argument is not a RigidBodyPhysicsComponent!");
wi::lua::SError(L, "ApplyImpulseAt(HumanoidComponent component, HumanoidBone bone, Vector impulse, Vector at) first argument is not a HumanoidComponent!");
return 0;
}
wi::scene::HumanoidComponent::HumanoidBone bone = (wi::scene::HumanoidComponent::HumanoidBone)wi::lua::SGetInt(L, 2);
Vector_BindLua* vec = Luna<Vector_BindLua>::lightcheck(L, 3);
if (vec == nullptr)
{
wi::lua::SError(L, "ApplyImpulseAt(HumanoidComponent component, HumanoidBone bone, Vector impulse, Vector at) third argument is not a Vector!");
return 0;
}
Vector_BindLua* vec2 = Luna<Vector_BindLua>::lightcheck(L, 4);
if (vec2 == nullptr)
{
wi::lua::SError(L, "ApplyImpulseAt(HumanoidComponent component, HumanoidBone bone, Vector impulse, Vector at) fourth argument is not a Vector!");
return 0;
}
wi::physics::ApplyImpulseAt(
*humanoid->component,
bone,
*(XMFLOAT3*)vec,
*(XMFLOAT3*)vec2
);
return 0;
}
Vector_BindLua* vec = Luna<Vector_BindLua>::lightcheck(L, 2);
if (vec == nullptr)
{
wi::lua::SError(L, "ApplyImpulseAt(RigidBodyPhysicsComponent component, Vector impulse, Vector at) second argument is not a Vector!");
return 0;
}
Vector_BindLua* vec2 = Luna<Vector_BindLua>::lightcheck(L, 3);
if (vec == nullptr)
{
wi::lua::SError(L, "ApplyImpulseAt(RigidBodyPhysicsComponent component, Vector impulse, Vector at) third argument is not a Vector!");
return 0;
}
wi::physics::ApplyImpulseAt(
*component->component,
*(XMFLOAT3*)vec,
*(XMFLOAT3*)vec2
);
}
else
wi::lua::SError(L, "ApplyImpulseAt(RigidBodyPhysicsComponent component, Vector impulse, Vector at) not enough arguments!");
return 0;
}
int Physics_BindLua::ApplyTorque(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 1)
{
scene::RigidBodyPhysicsComponent_BindLua* component = Luna<scene::RigidBodyPhysicsComponent_BindLua>::lightcheck(L, 1);
if (component == nullptr)
{
wi::lua::SError(L, "ApplyTorque(RigidBodyPhysicsComponent component, Vector torque) first argument is not a RigidBodyPhysicsComponent!");
return 0;
}
Vector_BindLua* vec = Luna<Vector_BindLua>::lightcheck(L, 2);
if (vec == nullptr)
{
wi::lua::SError(L, "ApplyTorque(RigidBodyPhysicsComponent component, Vector torque) second argument is not a Vector!");
return 0;
}
wi::physics::ApplyTorque(
*component->component,
*(XMFLOAT3*)vec
);
}
else
wi::lua::SError(L, "ApplyTorque(RigidBodyPhysicsComponent component, Vector torque) not enough arguments!");
return 0;
}
int Physics_BindLua::ApplyTorqueImpulse(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 1)
{
scene::RigidBodyPhysicsComponent_BindLua* component = Luna<scene::RigidBodyPhysicsComponent_BindLua>::lightcheck(L, 1);
if (component == nullptr)
{
wi::lua::SError(L, "ApplyTorqueImpulse(RigidBodyPhysicsComponent component, Vector torque) first argument is not a RigidBodyPhysicsComponent!");
return 0;
}
Vector_BindLua* vec = Luna<Vector_BindLua>::lightcheck(L, 2);
if (vec == nullptr)
{
wi::lua::SError(L, "ApplyTorqueImpulse(RigidBodyPhysicsComponent component, Vector torque) second argument is not a Vector!");
return 0;
}
wi::physics::ApplyTorqueImpulse(
*component->component,
*(XMFLOAT3*)vec
);
}
else
wi::lua::SError(L, "ApplyTorqueImpulse(RigidBodyPhysicsComponent component, Vector torque) not enough arguments!");
return 0;
}
int Physics_BindLua::SetActivationState(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 1)
{
scene::RigidBodyPhysicsComponent_BindLua* rigid = Luna<scene::RigidBodyPhysicsComponent_BindLua>::lightcheck(L, 1);
if (rigid == nullptr)
{
scene::SoftBodyPhysicsComponent_BindLua* soft = Luna<scene::SoftBodyPhysicsComponent_BindLua>::lightcheck(L, 1);
if (soft == nullptr)
{
wi::lua::SError(L, "SetActivationState(RigidBodyPhysicsComponent | SoftBodyPhysicsComponent component, int state) first argument is not a RigidBodyPhysicsComponent or SoftBodyPhysicsComponent!");
return 0;
}
wi::physics::SetActivationState(
*soft->component,
(wi::physics::ActivationState)wi::lua::SGetInt(L, 2)
);
return 0;
}
wi::physics::SetActivationState(
*rigid->component,
(wi::physics::ActivationState)wi::lua::SGetInt(L, 2)
);
}
else
wi::lua::SError(L, "SetActivationState(RigidBodyPhysicsComponent | SoftBodyPhysicsComponent component, int state) not enough arguments!");
return 0;
}
int Physics_BindLua::Intersects(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 1)
{
scene::Scene_BindLua* scene = Luna<scene::Scene_BindLua>::lightcheck(L, 1);
if (scene == nullptr)
{
wi::lua::SError(L, "Intersects(Scene, Ray) first argument is not a Scene!");
return 0;
}
primitive::Ray_BindLua* ray = Luna<primitive::Ray_BindLua>::lightcheck(L, 2);
if (ray == nullptr)
{
wi::lua::SError(L, "Intersects(Scene, Ray) second argument is not a Ray!");
return 0;
}
wi::physics::RayIntersectionResult result = wi::physics::Intersects(*scene->scene, ray->ray);
wi::lua::SSetLongLong(L, result.entity);
Luna<Vector_BindLua>::push(L, result.position);
Luna<Vector_BindLua>::push(L, result.normal);
wi::lua::SSetLongLong(L, result.humanoid_ragdoll_entity);
wi::lua::SSetInt(L, (int)result.humanoid_bone);
Luna<Vector_BindLua>::push(L, result.position_local);
return 6;
}
wi::lua::SError(L, "Intersects(Scene, Ray) not enough arguments!");
return 0;
}
int Physics_BindLua::PickDrag(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 2)
{
scene::Scene_BindLua* scene = Luna<scene::Scene_BindLua>::lightcheck(L, 1);
if (scene == nullptr)
{
wi::lua::SError(L, "Intersects(Scene, Ray, PickDragOperation) first argument is not a Scene!");
return 0;
}
primitive::Ray_BindLua* ray = Luna<primitive::Ray_BindLua>::lightcheck(L, 2);
if (ray == nullptr)
{
wi::lua::SError(L, "Intersects(Scene, Ray, PickDragOperation) second argument is not a Ray!");
return 0;
}
PickDragOperation_BindLua* op = Luna<PickDragOperation_BindLua>::lightcheck(L, 3);
if (op == nullptr)
{
wi::lua::SError(L, "Intersects(Scene, Ray, PickDragOperation) third argument is not a PickDragOperation!");
return 0;
}
wi::physics::PickDrag(*scene->scene, ray->ray, op->op);
return 0;
}
wi::lua::SError(L, "Intersects(Scene, Ray, PickDragOperation) not enough arguments!");
return 0;
}
void Physics_BindLua::Bind()
{
static bool initialized = false;
if (!initialized)
{
initialized = true;
Luna<PickDragOperation_BindLua>::Register(wi::lua::GetLuaState());
Luna<Physics_BindLua>::Register(wi::lua::GetLuaState());
Luna<Physics_BindLua>::push_global(wi::lua::GetLuaState(), "physics");
wi::lua::RunText(R"(
ACTIVATION_STATE_ACTIVE = 0
ACTIVATION_STATE_INACTIVE = 1
)");
}
}
Luna<PickDragOperation_BindLua>::FunctionType PickDragOperation_BindLua::methods[] = {
lunamethod(PickDragOperation_BindLua, Finish),
{ NULL, NULL }
};
Luna<PickDragOperation_BindLua>::PropertyType PickDragOperation_BindLua::properties[] = {
{ NULL, NULL }
};
int PickDragOperation_BindLua::Finish(lua_State* L)
{
op = {};
return 0;
}
}