Skip to content

Commit

Permalink
Fix set license plate
Browse files Browse the repository at this point in the history
  • Loading branch information
Deewarz committed Jan 12, 2025
1 parent 32a85fb commit ee74892
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 33 deletions.
10 changes: 5 additions & 5 deletions code/client/src/core/modules/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ namespace MafiaMP::Core::Modules {

void Vehicle::Create(flecs::entity e, std::string modelName) {
auto info = Core::gApplication->GetEntityFactory()->RequestVehicle(std::move(modelName));
auto &trackingData = e.ensure<Core::Modules::Vehicle::Tracking>();
trackingData.info = info;
trackingData.car = nullptr;
auto &trackingData = e.ensure<Core::Modules::Vehicle::Tracking>();
trackingData.info = info;
trackingData.car = nullptr;

auto &interp = e.ensure<Interpolated>();
interp.interpolator.GetPosition()->SetCompensationFactor(1.5f);
Expand Down Expand Up @@ -211,7 +211,7 @@ namespace MafiaMP::Core::Modules {
vehicle->SetGear(updateData->gear);
vehicle->SetHandbrake(updateData->handbrake, false);
vehicle->SetHorn(updateData->hornOn);
if (::strcmp(vehicle->GetSPZText(), updateData->licensePlate) > 0) {
if (std::strcmp(vehicle->GetSPZText(), updateData->licensePlate) != 0) {
vehicle->SetSPZText(updateData->licensePlate, true);
}
vehicle->SetPower(updateData->power);
Expand Down Expand Up @@ -341,7 +341,7 @@ namespace MafiaMP::Core::Modules {

if (licensePlate.HasValue()) {
const auto plate = licensePlate().C_String();
::memcpy(updateData->licensePlate, plate, strlen(plate) + 1);
std::memcpy(updateData->licensePlate, plate, strlen(plate) + 1);
}

if (lockState.HasValue()) {
Expand Down
5 changes: 4 additions & 1 deletion code/server/src/core/builtins/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ namespace MafiaMP::Scripting {

void Vehicle::SetLicensePlate(std::string plate) {
auto vehData = _ent.get_mut<Shared::Modules::VehicleSync::UpdateData>();
::memcpy(vehData->licensePlate, plate.c_str(), std::min(sizeof(vehData->licensePlate), plate.length()));

size_t copyLength = std::min<size_t>(Shared::Modules::VehicleSync::LICENSE_PLATE_MAX_LENGTH - 1, plate.length());
std::memcpy(vehData->licensePlate, plate.c_str(), copyLength);

MafiaMP::Shared::RPC::VehicleSetProps msg {};
msg.licensePlate = plate.c_str();
FW_SEND_SERVER_COMPONENT_GAME_RPC(Shared::RPC::VehicleSetProps, _ent, msg);
Expand Down
12 changes: 0 additions & 12 deletions code/server/src/core/modules/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,6 @@ namespace MafiaMP::Core::Modules {

auto updateData = e.ensure<Shared::Modules::VehicleSync::UpdateData>();

// generate a random license plate
{
constexpr char letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
constexpr char numbers[] = "0123456789";
for (int i = 0; i < 2; i++) {
updateData.licensePlate[i] = letters[::rand() % (sizeof(letters) - 1)];
}
for (int i = 3; i < 6; i++) {
updateData.licensePlate[i] = numbers[::rand() % (sizeof(numbers) - 1)];
}
}

e.add<CarData>();
e.add<Framework::World::Modules::Base::RemovedOnGameModeReload>();

Expand Down
2 changes: 1 addition & 1 deletion code/shared/game_rpc/vehicle/vehicle_setprops.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace MafiaMP::Shared::RPC {
}

bool Valid() const override {
if (licensePlate.HasValue() && licensePlate().GetLength() > 7) {
if (licensePlate.HasValue() && licensePlate().GetLength() > Modules::VehicleSync::LICENSE_PLATE_MAX_LENGTH) {
return false;
}

Expand Down
52 changes: 38 additions & 14 deletions code/shared/modules/vehicle_sync.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace MafiaMP::Shared::Modules {
struct VehicleSync {
static constexpr uint32_t LICENSE_PLATE_MAX_LENGTH = 7;

enum class LockState {
UNLOCKED,
LOCKED,
Expand All @@ -16,27 +18,49 @@ namespace MafiaMP::Shared::Modules {

struct UpdateData {
glm::vec3 angularVelocity {};

bool beaconLightsOn = false;
float brake = 0.0f;

float brake = 0.0f;

glm::vec4 colorPrimary {1.0f, 1.0f, 1.0f, 1.0f};

glm::vec4 colorSecondary {1.0f, 1.0f, 1.0f, 1.0f};
float dirt = 0.0f;
bool engineOn = false;
float fuel = 100.0f; // We use arbitrary value, the max depends of the vehicle. See C_Motor::GetFuelSettings.
int gear = 0;
float handbrake = 0.0f;
bool hornOn = false;
char licensePlate[7] = "AZ-000";
LockState lockState = LockState::UNLOCKED;
float power = 0.0f;
bool radioOn = false;
int radioStationId = 0;

float dirt = 0.0f;

bool engineOn = false;

float fuel = 100.0f; // We use arbitrary value, the max depends of the vehicle. See C_Motor::GetFuelSettings.

int gear = 0;

float handbrake = 0.0f;

bool hornOn = false;

char licensePlate[LICENSE_PLATE_MAX_LENGTH] = "AZ-000";

LockState lockState = LockState::UNLOCKED;

float power = 0.0f;

bool radioOn = false;

int radioStationId = 0;

glm::vec4 rimColor {1.0f, 1.0f, 1.0f, 1.0f};
float rust = 0.0f;

float rust = 0.0f;

bool sirenOn = false;
float steer = 0.0f;

float steer = 0.0f;

glm::vec4 tireColor {1.0f, 1.0f, 1.0f, 1.0f};

glm::vec3 velocity {};

glm::vec4 windowTint {0.0f, 0.0f, 0.0f, 20.0f / 255.0f};
};

Expand Down

0 comments on commit ee74892

Please sign in to comment.