Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix set license plate #114

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 0 additions & 14 deletions code/server/src/core/modules/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,6 @@ namespace MafiaMP::Core::Modules {
auto &frame = e.ensure<Framework::World::Modules::Base::Frame>();
frame.modelName = "berkley_810"; /* TODO */

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this should be in a more global namespace / file. It's not entirely related to the sync.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know where I can put it? I agree but I don't know where.


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