From f9c948f909cedde84e8a43f14302c3fe3a9d7a39 Mon Sep 17 00:00:00 2001 From: Quentin Date: Fri, 13 Oct 2023 00:10:18 +0200 Subject: [PATCH] fix(teleport): code reuse / fix lot of unnecessary logic running for tping our own ped. (#2246) --- src/backend/looped/weapons/tpgun.cpp | 3 ++- src/util/teleport.hpp | 21 ++++++++++++--------- src/views/self/view_teleport.cpp | 19 ++++++++----------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/backend/looped/weapons/tpgun.cpp b/src/backend/looped/weapons/tpgun.cpp index 020b0f4c..a96c4567 100644 --- a/src/backend/looped/weapons/tpgun.cpp +++ b/src/backend/looped/weapons/tpgun.cpp @@ -2,6 +2,7 @@ #include "core/enums.hpp" #include "gta/enums.hpp" #include "util/entity.hpp" +#include "util/teleport.hpp" namespace big { @@ -15,7 +16,7 @@ namespace big { Vector3 c; entity::raycast(&c); - PED::SET_PED_COORDS_KEEP_VEHICLE(self::ped, c.x, c.y, c.z); + teleport::to_coords(c); } } } diff --git a/src/util/teleport.hpp b/src/util/teleport.hpp index 40ad1522..91212d39 100644 --- a/src/util/teleport.hpp +++ b/src/util/teleport.hpp @@ -8,6 +8,11 @@ namespace big::teleport { + inline void to_coords(const Vector3& location) + { + PED::SET_PED_COORDS_KEEP_VEHICLE(self::ped, location.x, location.y, location.z + 1.f); + } + inline bool teleport_player_to_coords(player_ptr player, Vector3 coords, Vector3 euler = {0, 0, 0}) { Entity ent; @@ -20,7 +25,10 @@ namespace big::teleport bool is_local_player = (ent == self::ped || ent == self::veh); if (is_local_player) - PED::SET_PED_COORDS_KEEP_VEHICLE(ent, coords.x, coords.y, coords.z); + { + to_coords(coords); + return true; + } if (ENTITY::IS_ENTITY_DEAD(ent, true)) { @@ -151,11 +159,6 @@ namespace big::teleport return true; } - inline void to_coords(Vector3 location) - { - PED::SET_PED_COORDS_KEEP_VEHICLE(self::ped, location.x, location.y, location.z + 1.f); - } - inline bool to_blip(int sprite, int color = -1) { Vector3 location; @@ -166,7 +169,7 @@ namespace big::teleport if (sprite == (int)BlipIcons::Waypoint) entity::load_ground_at_3dcoord(location); - PED::SET_PED_COORDS_KEEP_VEHICLE(self::ped, location.x, location.y, location.z); + to_coords(location); return true; } @@ -175,7 +178,7 @@ namespace big::teleport { Vector3 location = ENTITY::GET_ENTITY_COORDS(ent, true); - PED::SET_PED_COORDS_KEEP_VEHICLE(self::ped, location.x, location.y, location.z); + to_coords(location); return true; } @@ -206,7 +209,7 @@ namespace big::teleport return false; } - PED::SET_PED_COORDS_KEEP_VEHICLE(self::ped, location.x, location.y, location.z); + to_coords(location); return false; } diff --git a/src/views/self/view_teleport.cpp b/src/views/self/view_teleport.cpp index 958b4fc4..0b97a2ab 100644 --- a/src/views/self/view_teleport.cpp +++ b/src/views/self/view_teleport.cpp @@ -41,7 +41,7 @@ namespace big ImGui::InputFloat3("##Customlocation", new_location); ImGui::SameLine(); components::button("Teleport", [] { - teleport::teleport_player_to_coords(g_player_service->get_self(), {new_location[0], new_location[1], new_location[2]}); + teleport::to_coords({new_location[0], new_location[1], new_location[2]}); }); ImGui::Spacing(); @@ -53,10 +53,10 @@ namespace big ImGui::BeginGroup(); components::button("Forward", [] { - teleport::teleport_player_to_coords(g_player_service->get_self(), ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(self::ped, 0, increment, 0)); + teleport::to_coords(ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(self::ped, 0, increment, 0)); }); components::button("Backward", [] { - teleport::teleport_player_to_coords(g_player_service->get_self(), ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(self::ped, 0, -increment, 0)); + teleport::to_coords(ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(self::ped, 0, -increment, 0)); }); ImGui::EndGroup(); @@ -64,10 +64,10 @@ namespace big ImGui::BeginGroup(); components::button("Left", [] { - teleport::teleport_player_to_coords(g_player_service->get_self(), ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(self::ped, -increment, 0, 0)); + teleport::to_coords(ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(self::ped, -increment, 0, 0)); }); components::button("Right", [] { - teleport::teleport_player_to_coords(g_player_service->get_self(), ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(self::ped, increment, 0, 0)); + teleport::to_coords(ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(self::ped, increment, 0, 0)); }); ImGui::EndGroup(); @@ -75,10 +75,10 @@ namespace big ImGui::BeginGroup(); components::button("Up", [] { - teleport::teleport_player_to_coords(g_player_service->get_self(), ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(self::ped, 0, 0, increment)); + teleport::to_coords(ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(self::ped, 0, 0, increment)); }); components::button("Down", [] { - teleport::teleport_player_to_coords(g_player_service->get_self(), ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(self::ped, 0, 0, -increment)); + teleport::to_coords(ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(self::ped, 0, 0, -increment)); }); ImGui::EndGroup(); @@ -130,10 +130,7 @@ namespace big if (components::button("TP_TO_IPL"_T.data())) { - PED::SET_PED_COORDS_KEEP_VEHICLE(self::ped, - selected_ipl.location.x, - selected_ipl.location.y, - selected_ipl.location.z); + teleport::to_coords(selected_ipl.location); } ImGui::Spacing();