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

Trailer improvements #681

Merged
merged 3 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
49 changes: 32 additions & 17 deletions Server/Components/Vehicles/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void Vehicle::streamInForPlayer(IPlayer& player)

// Attempt to attach trailer to cab if both vehicles are streamed in.
// We are streaming in the trailer. Check if cab is streamed.
if (!towing && cab && cab->isStreamedInForPlayer(player))
if (cab && cab->isStreamedInForPlayer(player))
{
NetCode::RPC::AttachTrailer trailerRPC;
trailerRPC.TrailerID = poolID;
Expand All @@ -100,7 +100,7 @@ void Vehicle::streamInForPlayer(IPlayer& player)
}

// We are streaming in the cab. Check if trailer is streamed.
if (towing && trailer && trailer->isStreamedInForPlayer(player))
if (trailer && trailer->isStreamedInForPlayer(player))
{
NetCode::RPC::AttachTrailer trailerRPC;
trailerRPC.TrailerID = trailer->poolID;
Expand Down Expand Up @@ -216,16 +216,36 @@ bool Vehicle::updateFromDriverSync(const VehicleDriverSyncPacket& vehicleSync, I
updateOccupied();
}

// Reset the detaching flag when trailer is detached on driver's client.
if (vehicleSync.TrailerID == 0)
if (vehicleSync.TrailerID)
{
if (trailer)
{
if (trailer->getID() != vehicleSync.TrailerID)
{
// The client instantly jumped from one trailer to another one. Probably a cheat, so don't
// allow it.
return false;
}
}
else
{
// Got a new one that we didn't know about.
trailer = static_cast<Vehicle*>(pool->get(vehicleSync.TrailerID));
if (trailer)
{
trailer->cab = this;
}
}
}
else
{
// Reset the detaching flag when trailer is detached on driver's client.
detaching = false;

// Client is reporting no trailer (probably lost it) but server thinks there's still one. Detaching it server side.
if (trailer && Time::now() - trailer->trailerUpdateTime > Seconds(0))
{
trailer->cab = nullptr;
towing = false;
trailer = nullptr;
}
}
Expand Down Expand Up @@ -264,7 +284,7 @@ bool Vehicle::updateFromUnoccupied(const VehicleUnoccupiedSyncPacket& unoccupied
return handler->onUnoccupiedVehicleUpdate(*this, player, data);
});

if (cab && !towing)
if (cab)
{
cab->detachTrailer();
cab = nullptr;
Expand Down Expand Up @@ -312,16 +332,13 @@ bool Vehicle::updateFromTrailerSync(const VehicleTrailerSyncPacket& trailerSync,
{
if (cab && cab->trailer == this)
{
cab->towing = false;
cab->trailer = nullptr;
}

// Don't call attach RPC here. Client will attach it because trailerId is sent in driver sync.
// https://github.com/openmultiplayer/server-beta/issues/181
vehicle->trailer = this;
vehicle->towing = true;
cab = vehicle;
towing = false;
trailerUpdateTime = Time::now();
}

Expand Down Expand Up @@ -681,7 +698,6 @@ void Vehicle::_respawn()
driver = nullptr;
trailer = nullptr;
cab = nullptr;
towing = false;
detaching = false;
params = VehicleParams {};
}
Expand Down Expand Up @@ -711,7 +727,6 @@ void Vehicle::attachTrailer(IVehicle& trailer)
return;
}
this->trailer = static_cast<Vehicle*>(&trailer);
towing = true;
this->trailer->setCab(this);
this->trailer->trailerUpdateTime = Time::now();
NetCode::RPC::AttachTrailer trailerRPC;
Expand All @@ -722,14 +737,13 @@ void Vehicle::attachTrailer(IVehicle& trailer)

void Vehicle::detachTrailer()
{
if (trailer && towing)
if (trailer)
{
NetCode::RPC::DetachTrailer trailerRPC;
trailerRPC.VehicleID = poolID;
PacketHelper::broadcastToSome(trailerRPC, streamedFor_.entries());
trailer->setCab(nullptr);
trailer = nullptr;
towing = false;
detaching = true;
}
}
Expand Down Expand Up @@ -764,13 +778,14 @@ void Vehicle::setAngularVelocity(Vector3 velocity)

Vehicle::~Vehicle()
{
if (cab)
if (trailer)
{
cab->detachTrailer();
detachTrailer();
}
else if (trailer && towing)
if (cab)
{
detachTrailer();
cab->detachTrailer();
cab = nullptr;
}
}

Expand Down
19 changes: 3 additions & 16 deletions Server/Components/Vehicles/vehicle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class Vehicle final : public IVehicle, public PoolIDProvider, public NoCopy
int32_t bodyColour2 = -1;
uint8_t landingGear = 1;
bool respawning = false;
bool towing = false;
bool detaching = false;
FlatHashSet<IPlayer*> passengers;
HybridString<16> numberPlate = StringView("XYZSR998");
Expand All @@ -60,11 +59,8 @@ class Vehicle final : public IVehicle, public PoolIDProvider, public NoCopy
Vector3 velocity = Vector3(0.0f, 0.0f, 0.0f);
Vector3 angularVelocity = Vector3(0.0f, 0.0f, 0.0f);
TimePoint trailerUpdateTime;
union
{
Vehicle* trailer = nullptr;
Vehicle* cab;
};
Vehicle* trailer = nullptr;
Vehicle* cab = nullptr;
StaticArray<IVehicle*, MAX_VEHICLE_CARRIAGES> carriages;
VehicleParams params;
uint8_t sirenState = 0;
Expand All @@ -82,7 +78,6 @@ class Vehicle final : public IVehicle, public PoolIDProvider, public NoCopy
void setCab(Vehicle* cab)
{
this->cab = cab;
towing = false;
}

/// Set vehicle to respawn without emitting onRespawn event
Expand Down Expand Up @@ -357,25 +352,17 @@ class Vehicle final : public IVehicle, public PoolIDProvider, public NoCopy
void detachTrailer() override;

/// Checks if the current vehicle is a trailer.
bool isTrailer() const override { return !towing && cab != nullptr; }
bool isTrailer() const override { return cab != nullptr; }

/// Get the current vehicle's attached trailer.
IVehicle* getTrailer() const override
{
if (!towing)
{
return nullptr;
}
return trailer;
}

/// Get the current vehicle's cab.
IVehicle* getCab() const override
{
if (towing)
{
return nullptr;
}
return cab;
}

Expand Down