This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
YimMenu/src/util/entity.hpp

255 lines
6.5 KiB
C++
Raw Normal View History

#pragma once
#include "gta/joaat.hpp"
#include "gta/replay.hpp"
#include "gta_util.hpp"
#include "math.hpp"
#include "natives.hpp"
#include "script.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)
{
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;
}
2023-02-09 05:46:08 +08:00
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);
2023-02-09 05:46:08 +08:00
}
2022-11-21 15:42:12 +00:00
2023-02-09 05:46:08 +08:00
inline bool take_control_of(Entity ent, int timeout = 300)
{
2023-03-10 22:41:46 +00:00
if (auto hnd = g_pointers->m_handle_to_ptr(ent))
{
2023-03-10 22:41:46 +00:00
if (!*g_pointers->m_is_session_started || !hnd->m_net_object || network_has_control_of_entity(hnd->m_net_object))
2023-02-09 05:46:08 +08:00
{
2023-03-10 22:41:46 +00:00
return true;
2023-02-09 05:46:08 +08:00
}
2023-03-10 22:41:46 +00:00
}
else
{
return false;
}
for (int i = 0; i < timeout; i++)
{
auto hnd = g_pointers->m_handle_to_ptr(ent);
if (!hnd || !hnd->m_net_object)
2023-02-09 05:46:08 +08:00
return false;
2023-03-10 22:41:46 +00:00
g_pointers->m_request_control(hnd->m_net_object);
if (timeout != 0)
script::get_current()->yield();
}
2023-03-10 22:41:46 +00:00
auto hnd = g_pointers->m_handle_to_ptr(ent);
if (!hnd || !hnd->m_net_object || !network_has_control_of_entity(hnd->m_net_object))
return false;
int netHandle = NETWORK::NETWORK_GET_NETWORK_ID_FROM_ENTITY(ent);
NETWORK::SET_NETWORK_ID_CAN_MIGRATE(netHandle, true);
return true;
}
inline std::vector<Entity> get_entities(bool vehicles, bool peds)
{
std::vector<Entity> target_entities;
target_entities.clear();
const auto replay_interface = *g_pointers->m_replay_interface;
if (!replay_interface)
return target_entities;
if (vehicles)
{
const auto vehicle_interface = replay_interface->m_vehicle_interface;
for (int i = 0; i < vehicle_interface->m_max_vehicles; i++)
{
const auto vehicle_ptr = vehicle_interface->get_vehicle(i);
if (!vehicle_ptr)
continue;
if (vehicle_ptr == gta_util::get_local_vehicle())
continue;
const auto veh = g_pointers->m_ptr_to_handle(vehicle_ptr);
if (!veh)
break;
target_entities.push_back(veh);
}
}
if (peds)
{
const auto ped_interface = replay_interface->m_ped_interface;
for (int i = 0; i < ped_interface->m_max_peds; i++)
{
const auto ped_ptr = ped_interface->get_ped(i);
if (!ped_ptr)
continue;
//make sure to don't include ourselves
if (ped_ptr == gta_util::get_local_ped())
continue;
const auto ped = g_pointers->m_ptr_to_handle(ped_ptr);
if (!ped)
break;
target_entities.push_back(ped);
}
}
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()
{
Entity closest_entity{};
float distance = 1;
//if (!vehicleInterface || !pedInterface)
// return 0;
auto replayInterface = *g_pointers->m_replay_interface;
auto vehicleInterface = replayInterface->m_vehicle_interface;
auto pedInterface = replayInterface->m_ped_interface;
for (auto veh : vehicleInterface->m_vehicle_list->m_vehicles)
{
if (veh.m_entity_ptr)
{
Vehicle handle = g_pointers->m_ptr_to_handle(veh.m_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(PLAYER::PLAYER_PED_ID(), handle, 17))
{
closest_entity = handle;
distance = distance_to_middle_of_screen(screenpos);
}
}
}
for (auto ped : pedInterface->m_ped_list->m_peds)
{
if (ped.m_entity_ptr)
{
Vehicle handle = g_pointers->m_ptr_to_handle(ped.m_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(PLAYER::PLAYER_PED_ID(), handle, 17) && handle != PLAYER::PLAYER_PED_ID())
{
closest_entity = handle;
distance = distance_to_middle_of_screen(screenpos);
}
}
}
return closest_entity;
}
}