2022-01-08 05:42:36 +01:00
|
|
|
#include "mobile_service.hpp"
|
|
|
|
#include "fiber_pool.hpp"
|
|
|
|
#include "natives.hpp"
|
|
|
|
#include "script.hpp"
|
|
|
|
#include "util/mobile.hpp"
|
|
|
|
|
|
|
|
namespace big
|
|
|
|
{
|
2022-05-10 19:02:30 +02:00
|
|
|
personal_vehicle::personal_vehicle(int idx, script_global vehicle_idx)
|
2022-07-10 06:33:14 +08:00
|
|
|
: m_id(idx), m_vehicle_idx(vehicle_idx)
|
2022-01-08 05:42:36 +01:00
|
|
|
{
|
2022-07-10 06:33:14 +08:00
|
|
|
m_plate = m_vehicle_idx.at(1).as<char*>();
|
|
|
|
m_hash = *m_vehicle_idx.at(66).as<Hash*>();
|
|
|
|
m_state_bitfield = m_vehicle_idx.at(103).as<int*>();
|
|
|
|
|
2022-10-24 14:08:37 +02:00
|
|
|
m_name = std::format(
|
2022-07-10 06:33:14 +08:00
|
|
|
"{} ({})",
|
2022-09-20 14:03:19 +01:00
|
|
|
HUD::GET_FILENAME_FOR_AUDIO_CONVERSATION(VEHICLE::GET_DISPLAY_NAME_FROM_VEHICLE_MODEL(m_hash)),
|
2022-07-10 06:33:14 +08:00
|
|
|
m_plate
|
|
|
|
);
|
2022-01-08 05:42:36 +01:00
|
|
|
}
|
|
|
|
|
2022-05-10 19:02:30 +02:00
|
|
|
std::string personal_vehicle::get_display_name() const
|
2022-01-08 05:42:36 +01:00
|
|
|
{
|
|
|
|
return m_name + "##" + std::to_string(m_id);
|
|
|
|
}
|
|
|
|
|
2022-05-10 19:02:30 +02:00
|
|
|
Hash personal_vehicle::get_hash() const
|
2022-01-08 05:42:36 +01:00
|
|
|
{
|
|
|
|
return m_hash;
|
|
|
|
}
|
|
|
|
|
2022-05-10 19:02:30 +02:00
|
|
|
int personal_vehicle::get_id() const
|
2022-01-08 05:42:36 +01:00
|
|
|
{
|
|
|
|
return m_id;
|
|
|
|
}
|
|
|
|
|
2022-07-10 06:33:14 +08:00
|
|
|
const char* personal_vehicle::get_plate() const
|
|
|
|
{
|
|
|
|
return m_plate;
|
|
|
|
}
|
|
|
|
|
|
|
|
script_global personal_vehicle::get_vehicle_idx() const
|
|
|
|
{
|
|
|
|
return m_vehicle_idx;
|
|
|
|
}
|
|
|
|
|
2022-05-10 19:02:30 +02:00
|
|
|
void personal_vehicle::summon() const
|
2022-01-08 05:42:36 +01:00
|
|
|
{
|
|
|
|
mobile::mechanic::summon_vehicle_by_index(m_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
mobile_service::mobile_service()
|
|
|
|
{
|
|
|
|
g_mobile_service = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
mobile_service::~mobile_service()
|
|
|
|
{
|
|
|
|
g_mobile_service = nullptr;
|
|
|
|
}
|
|
|
|
|
2022-05-10 19:02:30 +02:00
|
|
|
void mobile_service::refresh_personal_vehicles()
|
|
|
|
{
|
|
|
|
const auto now = std::chrono::high_resolution_clock::now();
|
2022-05-11 00:02:33 +02:00
|
|
|
if (std::chrono::duration_cast<std::chrono::seconds>(now - m_last_update) < 10s) return;
|
|
|
|
m_last_update = std::chrono::high_resolution_clock::now();
|
2022-05-10 19:02:30 +02:00
|
|
|
|
2022-07-10 06:33:14 +08:00
|
|
|
g_fiber_pool->queue_job([this] {
|
2022-05-10 19:02:30 +02:00
|
|
|
register_vehicles();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-08 05:42:36 +01:00
|
|
|
void mobile_service::register_vehicles()
|
|
|
|
{
|
2022-05-10 19:02:30 +02:00
|
|
|
const auto array_size = *mobile::vehicle_global.as<int*>();
|
|
|
|
for (int i = 0; i < array_size; i++)
|
2022-01-08 05:42:36 +01:00
|
|
|
{
|
2022-05-10 19:02:30 +02:00
|
|
|
if (i % 100 == 0)
|
|
|
|
script::get_current()->yield();
|
2022-01-08 19:54:10 +01:00
|
|
|
|
2022-01-08 05:42:36 +01:00
|
|
|
auto veh_idx_global = mobile::vehicle_global.at(i, 142);
|
|
|
|
|
2022-05-10 19:02:30 +02:00
|
|
|
const auto hash = *veh_idx_global.at(66).as<Hash*>();
|
|
|
|
const auto& it = m_pv_lookup.find(i);
|
|
|
|
const auto exists = it != m_pv_lookup.end();
|
2022-01-08 19:54:10 +01:00
|
|
|
|
2022-05-10 19:02:30 +02:00
|
|
|
// double check if model is a vehicle
|
2022-01-08 05:42:36 +01:00
|
|
|
if (STREAMING::IS_MODEL_A_VEHICLE(hash))
|
|
|
|
{
|
2022-05-10 19:02:30 +02:00
|
|
|
auto veh = std::make_unique<personal_vehicle>(i, veh_idx_global);
|
2022-07-10 06:33:14 +08:00
|
|
|
|
2022-05-10 19:02:30 +02:00
|
|
|
if (exists)
|
2022-01-08 17:48:46 +01:00
|
|
|
{
|
2022-05-10 19:02:30 +02:00
|
|
|
// vehicle name is no longer the same, update the vehicle at that index
|
|
|
|
if (veh->get_display_name() != it->second)
|
|
|
|
{
|
|
|
|
m_personal_vehicles.erase(it->second);
|
2022-01-08 17:48:46 +01:00
|
|
|
|
2022-05-10 19:02:30 +02:00
|
|
|
it->second = veh->get_display_name();
|
|
|
|
m_personal_vehicles.emplace(veh->get_display_name(), std::move(veh));
|
|
|
|
}
|
2022-01-08 17:48:46 +01:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-10 19:02:30 +02:00
|
|
|
m_pv_lookup.emplace(i, veh->get_display_name()); // update lookup table
|
|
|
|
m_personal_vehicles.emplace(veh->get_display_name(), std::move(veh)); // add new vehicle
|
2022-01-08 19:54:10 +01:00
|
|
|
|
|
|
|
continue;
|
2022-01-08 05:42:36 +01:00
|
|
|
}
|
|
|
|
|
2022-05-10 19:02:30 +02:00
|
|
|
// vehicle existed at some point but no longer does
|
|
|
|
if (exists)
|
2022-01-08 19:54:10 +01:00
|
|
|
{
|
|
|
|
m_personal_vehicles.erase(it->second);
|
|
|
|
m_pv_lookup.erase(i);
|
|
|
|
}
|
2022-01-08 05:42:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|