#pragma once #include "gta/joaat.hpp" #include "gta/replay.hpp" #include "gta_util.hpp" #include "math.hpp" #include "natives.hpp" #include "pools.hpp" #include "script.hpp" #include "services/players/player_service.hpp" namespace big::entity { inline void cage_ped(Ped ped) { Hash hash = RAGE_JOAAT("prop_gold_cont_01"); Vector3 location = ENTITY::GET_ENTITY_COORDS(ped, true); OBJECT::CREATE_OBJECT(hash, location.x, location.y, location.z - 1.f, true, false, false); } inline void clean_ped(Ped ped) { Ped player_ped = self::ped; PED::CLEAR_PED_BLOOD_DAMAGE(player_ped); PED::CLEAR_PED_WETNESS(player_ped); PED::CLEAR_PED_ENV_DIRT(player_ped); PED::RESET_PED_VISIBLE_DAMAGE(player_ped); } inline void delete_entity(Entity ent) { if (!ENTITY::DOES_ENTITY_EXIST(ent)) return; ENTITY::DETACH_ENTITY(ent, 1, 1); ENTITY::SET_ENTITY_VISIBLE(ent, false, false); NETWORK::NETWORK_SET_ENTITY_ONLY_EXISTS_FOR_PARTICIPANTS(ent, true); ENTITY::SET_ENTITY_COORDS_NO_OFFSET(ent, 0, 0, 0, 0, 0, 0); ENTITY::SET_ENTITY_AS_MISSION_ENTITY(ent, 1, 1); ENTITY::DELETE_ENTITY(&ent); } inline bool raycast(Entity* ent) { BOOL hit; Vector3 endCoords; Vector3 surfaceNormal; Vector3 camCoords = CAM::GET_GAMEPLAY_CAM_COORD(); Vector3 rot = CAM::GET_GAMEPLAY_CAM_ROT(2); Vector3 dir = math::rotation_to_direction(rot); Vector3 farCoords; farCoords.x = camCoords.x + dir.x * 1000; farCoords.y = camCoords.y + dir.y * 1000; farCoords.z = camCoords.z + dir.z * 1000; int ray = SHAPETEST::START_EXPENSIVE_SYNCHRONOUS_SHAPE_TEST_LOS_PROBE(camCoords.x, camCoords.y, camCoords.z, farCoords.x, farCoords.y, farCoords.z, -1, 0, 7); SHAPETEST::GET_SHAPE_TEST_RESULT(ray, &hit, &endCoords, &surfaceNormal, ent); return (bool)hit; } inline bool network_has_control_of_entity(rage::netObject* net_object) { return !net_object || !net_object->m_next_owner_id && (net_object->m_control_id == -1); } inline bool take_control_of(Entity ent, int timeout = 300) { if (!*g_pointers->m_gta.m_is_session_started) return true; auto hnd = g_pointers->m_gta.m_handle_to_ptr(ent); if (!hnd || !hnd->m_net_object || !*g_pointers->m_gta.m_is_session_started) return false; if (network_has_control_of_entity(hnd->m_net_object)) return true; for (int i = 0; i < timeout; i++) { g_pointers->m_gta.m_request_control(hnd->m_net_object); if (network_has_control_of_entity(hnd->m_net_object)) return true; if (timeout != 0) script::get_current()->yield(); } return false; } inline std::vector get_entities(bool vehicles, bool peds, bool props = false, bool include_self_veh = false) { std::vector target_entities; if (vehicles) { for (auto vehicle : pools::get_all_vehicles()) { if (!include_self_veh && vehicle == gta_util::get_local_vehicle()) continue; target_entities.push_back(g_pointers->m_gta.m_ptr_to_handle(vehicle)); } } if (peds) { for (auto ped : pools::get_all_peds()) { target_entities.push_back(g_pointers->m_gta.m_ptr_to_handle(ped)); } } if (props) { for (auto prop : pools::get_all_props()) { target_entities.push_back(g_pointers->m_gta.m_ptr_to_handle(prop)); } } return target_entities; } inline bool load_ground_at_3dcoord(Vector3& location) { float groundZ; const 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 (uint16_t z = 0; i && z < 1000; z += 100) { STREAMING::REQUEST_COLLISION_AT_COORD(location.x, location.y, (float)z); script::get_current()->yield(); } if (MISC::GET_GROUND_Z_FOR_3D_COORD(location.x, location.y, 1000.f, &groundZ, false, false)) { location.z = groundZ + 1.f; return true; } script::get_current()->yield(); } location.z = 1000.f; return false; } inline double distance_to_middle_of_screen(const rage::fvector2& screen_pos) { double cumulative_distance{}; if (screen_pos.x > 0.5) cumulative_distance += screen_pos.x - 0.5; else cumulative_distance += 0.5 - screen_pos.x; if (screen_pos.y > 0.5) cumulative_distance += screen_pos.y - 0.5; else cumulative_distance += 0.5 - screen_pos.y; return cumulative_distance; } inline Entity get_entity_closest_to_middle_of_screen(rage::fwEntity** pointer = nullptr, std::vector ignore_entities = {}, bool include_veh = true, bool include_ped = true, bool include_prop = true, bool include_players = true) { Entity closest_entity{}; rage::fwEntity* closest_entity_ptr = nullptr; float distance = 1; auto ignored_entity = [=](Entity handle) -> bool { if (handle == self::ped) return true; for (auto ent : ignore_entities) { if (handle == ent) return true; } return false; }; auto update_closest_entity = [&](Entity handle, rage::fwEntity* entity_ptr) { Vector3 pos = ENTITY::GET_ENTITY_COORDS(handle, 1); rage::fvector2 screenpos; HUD::GET_HUD_SCREEN_POSITION_FROM_WORLD_POSITION(pos.x, pos.y, pos.z, &screenpos.x, &screenpos.y); if (distance_to_middle_of_screen(screenpos) < distance && ENTITY::HAS_ENTITY_CLEAR_LOS_TO_ENTITY(self::ped, handle, 17) && !ignored_entity(handle)) { closest_entity = handle; closest_entity_ptr = entity_ptr; distance = distance_to_middle_of_screen(screenpos); } }; auto include_pool = [&](auto& pool) { for (const auto ptr : pool()) if (ptr) update_closest_entity(g_pointers->m_gta.m_ptr_to_handle(ptr), ptr); }; if (include_veh) include_pool(pools::get_all_vehicles); if (include_ped) include_pool(pools::get_all_peds); if (include_prop) include_pool(pools::get_all_props); if (include_players) { for (auto player : g_player_service->players() | std::ranges::views::values) { if (player->get_ped()) { Ped handle = g_pointers->m_gta.m_ptr_to_handle(player->get_ped()); update_closest_entity(handle, player->get_ped()); } } } if (pointer) *pointer = closest_entity_ptr; return closest_entity; } }