From 1d631a86ce7bbd1efa7fe6702faacf5522c57581 Mon Sep 17 00:00:00 2001 From: Yimura Date: Sat, 6 Feb 2021 00:33:59 +0100 Subject: [PATCH] refactor(Teleport): Fixed and optimised code --- BigBaseV2/src/features/teleport.cpp | 94 ++++++++++------------------- BigBaseV2/src/features/teleport.hpp | 3 +- 2 files changed, 33 insertions(+), 64 deletions(-) diff --git a/BigBaseV2/src/features/teleport.cpp b/BigBaseV2/src/features/teleport.cpp index ec3ded72..556fa0cc 100644 --- a/BigBaseV2/src/features/teleport.cpp +++ b/BigBaseV2/src/features/teleport.cpp @@ -1,41 +1,20 @@ #include "teleport.hpp" +#include "functions.hpp" #include "natives.hpp" #include "notify.hpp" #include "script.hpp" namespace big { - bool teleport::load_ground_at_3dcoord(Vector3 location) + bool teleport::load_ground_at_3dcoord(Vector3& location) { float groundZ; uint8_t attempts = 10; + for (uint8_t i = 0; i < attempts; i++) { // Only request a collision after the first try failed because the location might already be loaded on first attempt. - for (uint8_t z = 0; i && z < 1000; z += 100) - { - STREAMING::REQUEST_COLLISION_AT_COORD(location.x, location.y, z); - - script::get_current()->yield(); - } - - if (MISC::GET_GROUND_Z_FOR_3D_COORD(location.x, location.y, 1000.f, &groundZ, false, false)) - return true; - - script::get_current()->yield(); - } - - return false; - } - - Vector3 teleport::get_ground_at_3dcoord(Vector3 location) - { - float groundZ; - uint8_t attempts = 10; - for (uint8_t i = 0; i < attempts; i++) - { - // Only request a collision after the first try failed because the location might already be loaded on first attempt. - for (uint8_t z = 0; i && z < 1000; z += 100) + for (uint16_t z = 0; i && z < 1000; z += 100) { STREAMING::REQUEST_COLLISION_AT_COORD(location.x, location.y, (float)z); @@ -46,42 +25,36 @@ namespace big { location.z = groundZ + 1.f; - break; + return true; } - if (i == attempts - 1) location.z = 1000.f; - script::get_current()->yield(); } - return location; + location.z = 1000.f; + + return false; } bool teleport::bring_blip(int blipSprite, int blipColor, int flag) { - Blip blipHandle = HUD::GET_FIRST_BLIP_INFO_ID(blipSprite); - while (HUD::DOES_BLIP_EXIST(blipHandle) && (blipColor != -1 && HUD::GET_BLIP_COLOUR(blipHandle) != blipColor)) - { - blipHandle = HUD::GET_NEXT_BLIP_INFO_ID(blipSprite); + Blip blip; + for (blip = HUD::GET_FIRST_BLIP_INFO_ID(blipSprite); + HUD::DOES_BLIP_EXIST(blip) && + blipColor != -1 && HUD::GET_BLIP_COLOUR(blip); + blip = HUD::GET_NEXT_BLIP_INFO_ID(blipSprite) + ) script::get_current()->yield(); + if (!HUD::DOES_BLIP_EXIST(blip) || (blipColor != -1 && HUD::GET_BLIP_COLOUR(blip) != blipColor)) return false; - script::get_current()->yield(); - } - if (!HUD::DOES_BLIP_EXIST(blipHandle) || (blipColor != -1 && HUD::GET_BLIP_COLOUR(blipHandle) != blipColor)) return false; - - Vector3 location = HUD::GET_BLIP_COORDS(blipHandle); + Vector3 location = HUD::GET_BLIP_COORDS(blip); Entity veh = VEHICLE::GET_CLOSEST_VEHICLE(location.x, location.y, location.z, 5.f, 0, flag); if (!ENTITY::DOES_ENTITY_EXIST(veh)) return false; - while (!NETWORK::NETWORK_HAS_CONTROL_OF_ENTITY(veh)) - { - NETWORK::NETWORK_REQUEST_CONTROL_OF_ENTITY(veh); + func::take_control_of_entity(veh); - script::get_current()->yield(); - } - - Ped player = PLAYER::GET_PLAYER_PED_SCRIPT_INDEX(g_player.id); + Ped player = PLAYER::PLAYER_PED_ID(); location = ENTITY::GET_ENTITY_COORDS(player, true); ENTITY::SET_ENTITY_COORDS(veh, location.x, location.y, location.z + 1.f, 0, 0, 0, true); @@ -93,7 +66,8 @@ namespace big } else { - PED::SET_PED_INTO_VEHICLE(player, veh, -2); + int seatIndex = VEHICLE::IS_VEHICLE_SEAT_FREE(veh, -1, false) ? -1 : -2; + PED::SET_PED_INTO_VEHICLE(player, veh, seatIndex); } return true; @@ -103,17 +77,16 @@ namespace big { Ped player = PLAYER::GET_PLAYER_PED_SCRIPT_INDEX(g_player.id); - Blip blipHandle = HUD::GET_FIRST_BLIP_INFO_ID(blipSprite); - while (HUD::DOES_BLIP_EXIST(blipHandle) && (blipColor != -1 && HUD::GET_BLIP_COLOUR(blipHandle) != blipColor)) - { - blipHandle = HUD::GET_NEXT_BLIP_INFO_ID(blipSprite); + Blip blip; + for (blip = HUD::GET_FIRST_BLIP_INFO_ID(blipSprite); + HUD::DOES_BLIP_EXIST(blip) && + blipColor != -1 && HUD::GET_BLIP_COLOUR(blip); + blip = HUD::GET_NEXT_BLIP_INFO_ID(blipSprite) + ) script::get_current()->yield(); + if (!HUD::DOES_BLIP_EXIST(blip) || (blipColor != -1 && HUD::GET_BLIP_COLOUR(blip) != blipColor)) return false; - script::get_current()->yield(); - } - if (!HUD::DOES_BLIP_EXIST(blipHandle) || (blipColor != -1 && HUD::GET_BLIP_COLOUR(blipHandle) != blipColor)) return false; - - Vector3 location = HUD::GET_BLIP_COORDS(blipHandle); - location = get_ground_at_3dcoord(location); + Vector3 location = HUD::GET_BLIP_COORDS(blip); + load_ground_at_3dcoord(location); PED::SET_PED_COORDS_KEEP_VEHICLE(player, location.x, location.y, location.z); @@ -133,8 +106,7 @@ namespace big Vector3 location = ENTITY::GET_ENTITY_COORDS(target, true); - for (uint8_t i = 0; !load_ground_at_3dcoord(location); i++) - if (i == 5) break; + load_ground_at_3dcoord(location); Ped current = PLAYER::GET_PLAYER_PED_SCRIPT_INDEX(g_player.id); @@ -158,8 +130,7 @@ namespace big } } - int seatIndex = -2; - if (VEHICLE::IS_VEHICLE_SEAT_FREE(veh, -1, 0)) seatIndex = -1; + int seatIndex = VEHICLE::IS_VEHICLE_SEAT_FREE(veh, -1, false) ? -1 : -2; PED::SET_PED_INTO_VEHICLE(current, veh, seatIndex); } @@ -183,8 +154,7 @@ namespace big if (!HUD::DOES_BLIP_EXIST(blipHandle)) return false; Vector3 location = HUD::GET_BLIP_COORDS(blipHandle); - - location = get_ground_at_3dcoord(location); + load_ground_at_3dcoord(location); PED::SET_PED_COORDS_KEEP_VEHICLE(player, location.x, location.y, location.z); diff --git a/BigBaseV2/src/features/teleport.hpp b/BigBaseV2/src/features/teleport.hpp index 487e8ae2..a8a4173f 100644 --- a/BigBaseV2/src/features/teleport.hpp +++ b/BigBaseV2/src/features/teleport.hpp @@ -6,9 +6,8 @@ namespace big class teleport { public: + static bool load_ground_at_3dcoord(Vector3& location); static bool bring_blip(int blipSprite, int blipColor, int flag = 70); - static bool load_ground_at_3dcoord(Vector3 location); - static Vector3 get_ground_at_3dcoord(Vector3 location); static bool teleport_to_blip(int blipSprite, int blipColor = -1); static void teleport_into_player_vehicle(Player player); static void teleport_to_player(Player player);