fix(teleport): code reuse / fix lot of unnecessary logic running for tping our own ped. (#2246)

This commit is contained in:
Quentin 2023-10-13 00:10:18 +02:00 committed by GitHub
parent bb4ec59086
commit f9c948f909
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 21 deletions

View File

@ -2,6 +2,7 @@
#include "core/enums.hpp" #include "core/enums.hpp"
#include "gta/enums.hpp" #include "gta/enums.hpp"
#include "util/entity.hpp" #include "util/entity.hpp"
#include "util/teleport.hpp"
namespace big namespace big
{ {
@ -15,7 +16,7 @@ namespace big
{ {
Vector3 c; Vector3 c;
entity::raycast(&c); entity::raycast(&c);
PED::SET_PED_COORDS_KEEP_VEHICLE(self::ped, c.x, c.y, c.z); teleport::to_coords(c);
} }
} }
} }

View File

@ -8,6 +8,11 @@
namespace big::teleport 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}) inline bool teleport_player_to_coords(player_ptr player, Vector3 coords, Vector3 euler = {0, 0, 0})
{ {
Entity ent; Entity ent;
@ -20,7 +25,10 @@ namespace big::teleport
bool is_local_player = (ent == self::ped || ent == self::veh); bool is_local_player = (ent == self::ped || ent == self::veh);
if (is_local_player) 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)) if (ENTITY::IS_ENTITY_DEAD(ent, true))
{ {
@ -151,11 +159,6 @@ namespace big::teleport
return true; 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) inline bool to_blip(int sprite, int color = -1)
{ {
Vector3 location; Vector3 location;
@ -166,7 +169,7 @@ namespace big::teleport
if (sprite == (int)BlipIcons::Waypoint) if (sprite == (int)BlipIcons::Waypoint)
entity::load_ground_at_3dcoord(location); 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; return true;
} }
@ -175,7 +178,7 @@ namespace big::teleport
{ {
Vector3 location = ENTITY::GET_ENTITY_COORDS(ent, true); 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; return true;
} }
@ -206,7 +209,7 @@ namespace big::teleport
return false; return false;
} }
PED::SET_PED_COORDS_KEEP_VEHICLE(self::ped, location.x, location.y, location.z); to_coords(location);
return false; return false;
} }

View File

@ -41,7 +41,7 @@ namespace big
ImGui::InputFloat3("##Customlocation", new_location); ImGui::InputFloat3("##Customlocation", new_location);
ImGui::SameLine(); ImGui::SameLine();
components::button("Teleport", [] { 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(); ImGui::Spacing();
@ -53,10 +53,10 @@ namespace big
ImGui::BeginGroup(); ImGui::BeginGroup();
components::button("Forward", [] { 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", [] { 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(); ImGui::EndGroup();
@ -64,10 +64,10 @@ namespace big
ImGui::BeginGroup(); ImGui::BeginGroup();
components::button("Left", [] { 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", [] { 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(); ImGui::EndGroup();
@ -75,10 +75,10 @@ namespace big
ImGui::BeginGroup(); ImGui::BeginGroup();
components::button("Up", [] { 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", [] { 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(); ImGui::EndGroup();
@ -130,10 +130,7 @@ namespace big
if (components::button("TP_TO_IPL"_T.data())) if (components::button("TP_TO_IPL"_T.data()))
{ {
PED::SET_PED_COORDS_KEEP_VEHICLE(self::ped, teleport::to_coords(selected_ipl.location);
selected_ipl.location.x,
selected_ipl.location.y,
selected_ipl.location.z);
} }
ImGui::Spacing(); ImGui::Spacing();