Redesigned Vehicle Spawn menus to have consistent features. (#2063)

Closes #2053
This commit is contained in:
gir489
2023-08-31 09:43:55 -04:00
committed by GitHub
parent 4c6226d022
commit cf07cfec04
19 changed files with 1115 additions and 950 deletions

View File

@ -6,6 +6,8 @@
#include "util/ped.hpp"
#include "util/vehicle.hpp"
#include "services/vehicle/persist_car_service.hpp"
namespace big
{
model_preview_service::model_preview_service()
@ -65,6 +67,7 @@ namespace big
if (m_veh_model_hash != hash || m_veh_spawn_max != spawn_max)
{
m_veh_model_hash = hash;
m_current_persisted_vehicle_name.clear();
if (m_veh_model_hash != 0)
{
@ -80,6 +83,7 @@ namespace big
{
m_ped_model_hash = 0;
m_ped_clone = 0;
m_current_persisted_vehicle_name.clear();
if (m_veh_spawn_max != spawn_max || m_veh_owned_mods.size() != owned_mods.size()
|| !std::equal(m_veh_owned_mods.begin(), m_veh_owned_mods.end(), owned_mods.begin()))
@ -101,6 +105,21 @@ namespace big
}
}
void model_preview_service::show_vehicle_persisted(std::string vehicle_name)
{
m_ped_model_hash = 0;
m_ped_clone = 0;
m_veh_model_hash = 0;
if (m_current_persisted_vehicle_name != vehicle_name)
{
m_current_persisted_vehicle_name = vehicle_name;
m_new_model = true;
preview_loop();
}
}
void model_preview_service::preview_loop()
{
if (m_running || m_loop_running)
@ -113,7 +132,7 @@ namespace big
g_fiber_pool->queue_job([this] {
m_loop_running = true;
while (g_running && m_running && g_gui->is_open() && (m_ped_model_hash || m_veh_model_hash))
while (g_running && m_running && g_gui->is_open() && (m_ped_model_hash || m_veh_model_hash || !m_current_persisted_vehicle_name.empty()))
{
Vector3 location;
@ -121,7 +140,7 @@ namespace big
{
location = ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(self::ped, 0.f, 5.f, -.5f);
}
else if (m_veh_model_hash)
else if (m_veh_model_hash || !m_current_persisted_vehicle_name.empty())
{
location = ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(self::ped, 0.f, 10.f, .5f);
}
@ -151,6 +170,10 @@ namespace big
m_current_ent = vehicle::clone_from_owned_mods(m_veh_owned_mods, location, 0.f, false);
}
}
else if (!m_current_persisted_vehicle_name.empty())
{
m_current_ent = persist_car_service::load_vehicle(m_current_persisted_vehicle_name, g.persist_car.persist_vehicle_sub_folder, Vector3());
}
if (m_current_ent)
{
@ -168,8 +191,7 @@ namespace big
}
else if (m_new_model)
{
ENTITY::DETACH_ENTITY(m_current_ent, 1, 1);
ENTITY::DELETE_ENTITY(&m_current_ent);
entity::delete_entity(m_current_ent, true);
}
else
{
@ -190,13 +212,13 @@ namespace big
script::get_current()->yield(15ms);
}
ENTITY::DETACH_ENTITY(m_current_ent, 1, 1);
ENTITY::DELETE_ENTITY(&m_current_ent);
entity::delete_entity(m_current_ent, true);
m_current_ent = 0;
m_ped_model_hash = 0;
m_veh_model_hash = 0;
m_veh_owned_mods.clear();
m_current_persisted_vehicle_name.clear();
m_running = false;
m_loop_running = false;
});

View File

@ -22,6 +22,8 @@ namespace big
bool m_loop_running = false;
bool m_running = false;
std::string m_current_persisted_vehicle_name;
public:
model_preview_service();
~model_preview_service();
@ -31,6 +33,8 @@ namespace big
void show_vehicle(Hash hash, bool spawn_max);
void show_vehicle(const std::map<int, int32_t>& owned_mods, bool spawn_max);
void show_vehicle_persisted(std::string vehicle_name);
void show_vehicle(Vehicle veh);
void preview_loop();
void stop_preview();

View File

@ -27,7 +27,7 @@ namespace big
file_stream.close();
}
Vehicle persist_car_service::load_vehicle(std::string_view file_name, std::string folder_name)
Vehicle persist_car_service::load_vehicle(std::string_view file_name, std::string folder_name, const std::optional<Vector3>& spawn_coords)
{
const auto file = check_vehicle_folder(folder_name).get_file(file_name);
@ -46,7 +46,7 @@ namespace big
return NULL;
}
return spawn_vehicle_full(vehicle_json, self::ped);
return spawn_vehicle_full(vehicle_json, self::ped, spawn_coords);
}
std::vector<std::string> persist_car_service::list_files(std::string folder_name)
@ -80,13 +80,13 @@ namespace big
return spawn_vehicle_full(get_full_vehicle_json(vehicle), ped);
}
Vehicle persist_car_service::spawn_vehicle_full(nlohmann::json vehicle_json, Ped ped)
Vehicle persist_car_service::spawn_vehicle_full(nlohmann::json vehicle_json, Ped ped, const std::optional<Vector3>& spawn_coords)
{
const auto vehicle = spawn_vehicle(vehicle_json, ped);
const auto vehicle = spawn_vehicle(vehicle_json, ped, spawn_coords);
if (!vehicle_json[tow_key].is_null())
{
const auto tow = spawn_vehicle(vehicle_json[tow_key], ped);
const auto tow = spawn_vehicle(vehicle_json[tow_key], ped, spawn_coords);
auto pos = ENTITY::GET_ENTITY_COORDS(tow, true);
pos.x -= 10;
@ -101,7 +101,7 @@ namespace big
}
else if (!vehicle_json[trailer_key].is_null())
{
const auto trailer = spawn_vehicle(vehicle_json[trailer_key], ped);
const auto trailer = spawn_vehicle(vehicle_json[trailer_key], ped, spawn_coords);
VEHICLE::ATTACH_VEHICLE_TO_TRAILER(vehicle, trailer, 1.0f);
const auto rotation = ENTITY::GET_ENTITY_ROTATION(trailer, 2);
@ -114,9 +114,9 @@ namespace big
return vehicle;
}
Vehicle persist_car_service::spawn_vehicle(nlohmann::json vehicle_json, Ped ped)
Vehicle persist_car_service::spawn_vehicle(nlohmann::json vehicle_json, Ped ped, const std::optional<Vector3>& spawn_coords)
{
const auto vehicle = spawn_vehicle_json(vehicle_json, ped);
const auto vehicle = spawn_vehicle_json(vehicle_json, ped, spawn_coords);
std::vector<nlohmann::json> model_attachments = vehicle_json[model_attachments_key];
for (const auto& j : model_attachments)
@ -180,15 +180,16 @@ namespace big
return vehicle;
}
Vehicle persist_car_service::spawn_vehicle_json(nlohmann::json vehicle_json, Ped ped)
Vehicle persist_car_service::spawn_vehicle_json(nlohmann::json vehicle_json, Ped ped, const std::optional<Vector3>& spawn_coords)
{
const Hash vehicle_hash = vehicle_json[vehicle_model_hash_key];
Vector3 spawn_location = spawn_coords.has_value() ? spawn_coords.value() : vehicle::get_spawn_location(g.persist_car.spawn_inside, vehicle_hash);
const float spawn_heading = ENTITY::GET_ENTITY_HEADING(self::ped);
const auto pos = ENTITY::GET_ENTITY_COORDS(self::ped, true);
const auto vehicle = big::vehicle::spawn(vehicle_hash, spawn_location, spawn_heading);
const auto vehicle = big::vehicle::spawn(vehicle_hash, pos, ENTITY::GET_ENTITY_HEADING(ped));
script::get_current()->yield(); //This is needed to wait for the engine to instantiate things like the radio station so it won't overwrite it on the next frame.
if (spawn_location.x + spawn_location.y + spawn_location.z != 0)
script::get_current()->yield(); //This is needed to wait for the engine to instantiate things like the radio station so it won't overwrite it on the next frame.
VEHICLE::SET_VEHICLE_DIRT_LEVEL(vehicle, 0.0f);
VEHICLE::SET_VEHICLE_MOD_KIT(vehicle, 0);

View File

@ -12,7 +12,7 @@ namespace big
static Vehicle clone_ped_car(Ped ped, Vehicle vehicle);
static void save_vehicle(Vehicle vehicle, std::string_view file_name, std::string folder_name);
static Vehicle load_vehicle(std::string_view file_name, std::string folder_name = "");
static Vehicle load_vehicle(std::string_view file_name, std::string folder_name = "", const std::optional<Vector3>& = std::nullopt);
private:
static constexpr auto model_attachment_key = "model_attachment";
@ -64,9 +64,9 @@ namespace big
static constexpr auto clan_logo_key = "clan_logo";
static Vehicle spawn_vehicle_full(nlohmann::json vehicle_json, Ped ped);
static Vehicle spawn_vehicle(nlohmann::json vehicle_json, Ped ped);
static Vehicle spawn_vehicle_json(nlohmann::json vehicle_json, Ped ped);
static Vehicle spawn_vehicle_full(nlohmann::json vehicle_json, Ped ped, const std::optional<Vector3>& spawn_coords = std::nullopt);
static Vehicle spawn_vehicle(nlohmann::json vehicle_json, Ped ped, const std::optional<Vector3>& spawn_coords);
static Vehicle spawn_vehicle_json(nlohmann::json vehicle_json, Ped ped, const std::optional<Vector3>& spawn_coords = std::nullopt);
static nlohmann::json get_full_vehicle_json(Vehicle vehicle);