From 44d8dee8df1c46a551c03c7efcee7fd28ccaafdc Mon Sep 17 00:00:00 2001 From: Federico Cecchetto Date: Mon, 23 Aug 2021 17:10:37 +0200 Subject: [PATCH] Add vector methods --- src/dllmain.cpp | 7 +- src/game/scripting/function_tables.cpp | 12 +- src/game/scripting/lua/context.cpp | 182 ++++++++++++++++++++ src/game/scripting/lua/value_conversion.cpp | 2 +- 4 files changed, 192 insertions(+), 11 deletions(-) diff --git a/src/dllmain.cpp b/src/dllmain.cpp index c9aef3bd..154ab1bc 100644 --- a/src/dllmain.cpp +++ b/src/dllmain.cpp @@ -1,11 +1,10 @@ #include +#include "loader/component_loader.hpp" #include "game/game.hpp" #include "component/game_console.hpp" -#include "loader/component_loader.hpp" - #pragma warning(disable:4996) DWORD WINAPI console(LPVOID) @@ -37,9 +36,9 @@ void init() component_loader::post_unpack(); } -BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) +BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID lpReserved) { - if (ul_reason_for_call == DLL_PROCESS_ATTACH) + if (reason == DLL_PROCESS_ATTACH) { init(); } diff --git a/src/game/scripting/function_tables.cpp b/src/game/scripting/function_tables.cpp index 04471e0d..b25dfd53 100644 --- a/src/game/scripting/function_tables.cpp +++ b/src/game/scripting/function_tables.cpp @@ -280,7 +280,7 @@ namespace scripting {"vectornormalize", 281}, {"vectortoangles", 282}, {"vectortoyaw", 283}, - {"setclientmatchdata", 284}, + {"vectorlerp", 284}, {"anglestoup", 285}, {"anglestoright", 286}, {"anglestoforward", 287}, @@ -423,7 +423,7 @@ namespace scripting {"sub_7ff7de469d40", 501}, {"sub_7ff7de46a090", 502}, {"spawnvehicle", 503}, - {"getarray", 504}, + {"vehicle_getarray", 504}, {"pow", 505}, {"sub_7ff7de286950", 506}, {"getsquadassaultelo", 507}, @@ -678,7 +678,7 @@ namespace scripting {"hideallparts", 32848}, {"showpart", 32849}, {"showallparts", 32850}, - {"lintko", 32851}, + {"linkto", 32851}, {"linktoblendtotag", 32852}, {"unlink", 32853}, {"setnormalhealth", 32854}, @@ -806,7 +806,7 @@ namespace scripting {"setclock", 32988}, {"setclockup", 32989}, {"setvalue", 32990}, - {"sub_7ff7de2507c0", 32991}, + {"setwaypoint", 32991}, {"sub_7ff7de250930", 32992}, {"sub_7ff7de27b300", 32993}, {"sub_7ff7de27ba30", 32994}, @@ -1105,8 +1105,8 @@ namespace scripting {"sub_7ff7de276720", 33308}, {"sub_7ff7de276aa0", 33309}, {"sub_7ff7de277320", 33310}, - {"sub_7ff7de2784a0", 33311}, - {"sub_7ff7de278df0", 33312}, + {"getlocalplayerprofiledata", 33311}, + {"setlocalplayerprofiledata", 33312}, {"sub_7ff7de27aae0", 33313}, {"sub_7ff7de27abd0", 33314}, {"sub_7ff7de27ad70", 33315}, diff --git a/src/game/scripting/lua/context.cpp b/src/game/scripting/lua/context.cpp index fe4a4a82..cd9b1005 100644 --- a/src/game/scripting/lua/context.cpp +++ b/src/game/scripting/lua/context.cpp @@ -15,6 +15,57 @@ namespace scripting::lua { namespace { + vector normalize_vector(const vector& vec) + { + const auto length = sqrt((vec.get_x() * vec.get_x()) + (vec.get_y() * vec.get_y()) + (vec.get_z() * vec.get_z())); + + return vector( + vec.get_x() / length, + vec.get_y() / length, + vec.get_z() / length + ); + } + + vector vector_to_angles(const vector& vec) + { + const auto x = vec.get_x(); + const auto y = vec.get_y(); + const auto z = vec.get_z(); + + float yaw = 0.f; + float pitch = 0.f; + + if (x != 0.f && y != 0.f) + { + const auto v9 = atan2f(y, x) * 57.295776; + const auto v10 = v9 < 0.f + ? 360.f + : 0.0; + + yaw = v9 + v10; + const auto v11 = sqrtf((x * x) + (y * y)); + const auto v12 = atan2f(z, v11) * -57.295776; + + const auto v3 = v12 < 0 + ? 360.f + : 0.f; + + pitch = v12 + v3; + } + else + { + pitch = z < 0.f + ? 270.f + : 90.f; + } + + return vector( + pitch, + yaw, + 0 + ); + } + void setup_entity_type(sol::state& state, event_handler& handler, scheduler& scheduler) { state["level"] = entity{*game::levelEntityId}; @@ -28,6 +79,137 @@ namespace scripting::lua vector_type["g"] = sol::property(&vector::get_y, &vector::set_y); vector_type["b"] = sol::property(&vector::get_z, &vector::set_z); + vector_type[sol::meta_function::addition] = sol::overload( + [](const vector& a, const vector& b) + { + return vector( + a.get_x() + b.get_x(), + a.get_y() + b.get_y(), + a.get_z() + b.get_z() + ); + }, + [](const vector& a, const int value) + { + return vector( + a.get_x() + value, + a.get_y() + value, + a.get_z() + value + ); + } + ); + + vector_type[sol::meta_function::subtraction] = sol::overload( + [](const vector& a, const vector& b) + { + return vector( + a.get_x() - b.get_x(), + a.get_y() - b.get_y(), + a.get_z() - b.get_z() + ); + }, + [](const vector& a, const int value) + { + return vector( + a.get_x() - value, + a.get_y() - value, + a.get_z() - value + ); + } + ); + + vector_type[sol::meta_function::multiplication] = sol::overload( + [](const vector& a, const vector& b) + { + return vector( + a.get_x() * b.get_x(), + a.get_y() * b.get_y(), + a.get_z() * b.get_z() + ); + }, + [](const vector& a, const int value) + { + return vector( + a.get_x() * value, + a.get_y() * value, + a.get_z() * value + ); + } + ); + + vector_type[sol::meta_function::division] = sol::overload( + [](const vector& a, const vector& b) + { + return vector( + a.get_x() / b.get_x(), + a.get_y() / b.get_y(), + a.get_z() / b.get_z() + ); + }, + [](const vector& a, const int value) + { + return vector( + a.get_x() / value, + a.get_y() / value, + a.get_z() / value + ); + } + ); + + vector_type[sol::meta_function::equal_to] = [](const vector& a, const vector& b) + { + const auto normal_a = normalize_vector(a); + const auto normal_b = normalize_vector(b); + + return normal_a.get_x() == normal_b.get_x() && + normal_a.get_y() == normal_b.get_y() && + normal_a.get_z() == normal_b.get_z(); + }; + + vector_type[sol::meta_function::length] = [](const vector& a) + { + return sqrt((a.get_x() * a.get_x()) + (a.get_y() * a.get_y()) + (a.get_z() * a.get_z())); + }; + + vector_type[sol::meta_function::to_string] = [](const vector& a) + { + return utils::string::va("{x: %f, y: %f, z: %f}", a.get_x(), a.get_y(), a.get_z()); + }; + + vector_type["normalize"] = [](const vector& a) + { + return normalize_vector(a); + }; + + vector_type["toangles"] = [](const vector& a) + { + return call("vectortoangles", {a}).as(); + }; + + vector_type["toyaw"] = [](const vector& a) + { + return call("vectortoyaw", {a}).as(); + }; + + vector_type["tolerp"] = [](const vector& a) + { + return call("vectortolerp", {a}).as(); + }; + + vector_type["toup"] = [](const vector& a) + { + return call("anglestoup", {a}).as(); + }; + + vector_type["toright"] = [](const vector& a) + { + return call("anglestoright", {a}).as(); + }; + + vector_type["toforward"] = [](const vector& a) + { + return call("anglestoforward", {a}).as(); + }; + auto entity_type = state.new_usertype("entity"); for (const auto& func : method_map) diff --git a/src/game/scripting/lua/value_conversion.cpp b/src/game/scripting/lua/value_conversion.cpp index 49694a1a..c9cec6df 100644 --- a/src/game/scripting/lua/value_conversion.cpp +++ b/src/game/scripting/lua/value_conversion.cpp @@ -221,6 +221,6 @@ namespace scripting::lua return {state, value.as()}; } - return {}; + return {state, sol::lua_nil}; } }