Skip to content

Commit

Permalink
Vehicles: Fix various issues
Browse files Browse the repository at this point in the history
- Fix passenger sometimes visually not mounting
- Fix wrong seat coordinate if the player is the vehicle
- Deduplicate some code
  • Loading branch information
insunaa committed Jan 27, 2024
1 parent cc1a26f commit a89c0bd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,7 @@ struct SystemsShutdown : public AuraScript
{
if (seat && seat->IsVehicle())
{
seat->GetVehicleInfo()->RepopulateSeat(1);
seat->GetVehicleInfo()->RespawnAccessories(1);
}
}
}
Expand Down
108 changes: 41 additions & 67 deletions src/game/Entities/Vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,26 +201,6 @@ VehicleInfo::~VehicleInfo()
RemoveAccessoriesFromMap(); // Remove accessories (for example required with player vehicles)
}

void VehicleInfo::RepopulateSeat(uint8 seatId)
{
// Loading passengers (rough version only!)
SQLMultiStorage::SQLMSIteratorBounds<VehicleAccessory> bounds = sVehicleAccessoryStorage.getBounds<VehicleAccessory>(m_overwriteNpcEntry);
for (SQLMultiStorage::SQLMultiSIterator<VehicleAccessory> itr = bounds.first; itr != bounds.second; ++itr)
{
auto* seatEntry = GetSeatEntry(itr->seatId);
if (seatId == itr->seatId && seatEntry && seatEntry->m_ID && !GetPassenger(seatId))
{
if (Creature* summoned = m_owner->SummonCreature(itr->passengerEntry, m_owner->GetPositionX(), m_owner->GetPositionY(), m_owner->GetPositionZ(), 2 * m_owner->GetOrientation(), TEMPSPAWN_DEAD_DESPAWN, 0))
{
DEBUG_LOG("VehicleInfo(of %s)::Initialize: Load vehicle accessory %s onto seat %u", m_owner->GetGuidStr().c_str(), summoned->GetGuidStr().c_str(), itr->seatId);
m_accessoryGuids.insert(summoned->GetObjectGuid());
int32 basepoint0 = itr->seatId + 1;
summoned->CastCustomSpell((Unit*)m_owner, SPELL_RIDE_VEHICLE_HARDCODED, &basepoint0, nullptr, nullptr, TRIGGERED_OLD_TRIGGERED);
}
}
}
}

void VehicleInfo::Initialize()
{
if (!m_overwriteNpcEntry)
Expand All @@ -232,17 +212,9 @@ void VehicleInfo::Initialize()
SQLMultiStorage::SQLMSIteratorBounds<VehicleAccessory> bounds = sVehicleAccessoryStorage.getBounds<VehicleAccessory>(m_overwriteNpcEntry);
for (SQLMultiStorage::SQLMultiSIterator<VehicleAccessory> itr = bounds.first; itr != bounds.second; ++itr)
{
auto* seatEntry = GetSeatEntry(itr->seatId);
if (seatEntry && seatEntry->m_ID)
{
if (Creature* summoned = m_owner->SummonCreature(itr->passengerEntry, m_owner->GetPositionX(), m_owner->GetPositionY(), m_owner->GetPositionZ(), 2 * m_owner->GetOrientation(), TEMPSPAWN_DEAD_DESPAWN, 0))
{
DEBUG_LOG("VehicleInfo(of %s)::Initialize: Load vehicle accessory %s onto seat %u", m_owner->GetGuidStr().c_str(), summoned->GetGuidStr().c_str(), itr->seatId);
m_accessoryGuids.insert(summoned->GetObjectGuid());
int32 basepoint0 = itr->seatId + 1;
summoned->CastCustomSpell((Unit*)m_owner, SPELL_RIDE_VEHICLE_HARDCODED, &basepoint0, nullptr, nullptr, TRIGGERED_OLD_TRIGGERED);
}
}
Position pos = m_owner->GetPosition();
pos.o *= 2;
SummonPassenger(itr->passengerEntry, pos, itr->seatId);
}
}

Expand Down Expand Up @@ -328,21 +300,7 @@ void VehicleInfo::Board(Unit* passenger, uint8 seat)

// Calculate passengers local position
float lx = 0.f, ly = 0.f, lz = 0.f, lo = 0.f;
auto* creatureDisplayInfo = sCreatureDisplayInfoStore.LookupEntry(static_cast<Creature*>(m_owner)->GetNativeDisplayId());
float scale = creatureDisplayInfo->scale;
scale *= sCreatureModelDataStore.LookupEntry(creatureDisplayInfo->ModelId)->Scale;
auto attachmentItr = sModelAttachmentStore.find(creatureDisplayInfo->ModelId);
if (attachmentItr != sModelAttachmentStore.end())
for (auto& attachment : attachmentItr->second)
{
if (attachment.id == attachmentLookup(seatEntry->m_attachmentID))
{
lx = (attachment.position.x + seatEntry->m_attachmentOffsetX) * scale;
ly = (attachment.position.y + seatEntry->m_attachmentOffsetY) * scale;
lz = (attachment.position.z + seatEntry->m_attachmentOffsetZ) * scale;
break;
}
}
GetSeatCoordinates(seatEntry, lx, ly, lz);

BoardPassenger(passenger, lx, ly, lz, lo, seat); // Use TransportBase to store the passenger
if (auto* rootVehicle = static_cast<Unit*>(m_owner)->FindRootVehicle())
Expand All @@ -354,7 +312,7 @@ void VehicleInfo::Board(Unit* passenger, uint8 seat)

if (passenger->GetTypeId() == TYPEID_PLAYER)
{
Player* pPlayer = (Player*)passenger;
Player* pPlayer = static_cast<Player*>(passenger);
pPlayer->UnsummonPetTemporaryIfAny();

WorldPacket data(SMSG_ON_CANCEL_EXPECTED_RIDE_VEHICLE_AURA);
Expand All @@ -363,13 +321,11 @@ void VehicleInfo::Board(Unit* passenger, uint8 seat)
data.Initialize(SMSG_BREAK_TARGET, m_owner->GetPackGUID().size());
data << m_owner->GetPackGUID();
pPlayer->GetSession()->SendPacket(data);

pPlayer->SetImmobilizedState(true);
}
else if (passenger->GetTypeId() == TYPEID_UNIT)
{
if (!passenger->IsRooted())
((Creature*)passenger)->SetImmobilizedState(true);
(static_cast<Creature*>(passenger))->SetImmobilizedState(true);
}

Movement::MoveSplineInit init(*passenger);
Expand Down Expand Up @@ -440,21 +396,7 @@ void VehicleInfo::SwitchSeat(Unit* passenger, uint8 seat)
RemoveSeatMods(passenger, seatEntry->m_flags);

float lx = 0.f, ly = 0.f, lz = 0.f, lo = 0.f;
auto* creatureDisplayInfo = sCreatureDisplayInfoStore.LookupEntry(static_cast<Creature*>(m_owner)->GetNativeDisplayId());
float scale = creatureDisplayInfo->scale;
scale *= sCreatureModelDataStore.LookupEntry(creatureDisplayInfo->ModelId)->Scale;
auto attachmentItr = sModelAttachmentStore.find(creatureDisplayInfo->ModelId);
if (attachmentItr != sModelAttachmentStore.end())
for (auto& attachment : attachmentItr->second)
{
if (attachment.id == attachmentLookup(seatEntry->m_attachmentID))
{
lx = (attachment.position.x + seatEntry->m_attachmentOffsetX) * scale;
ly = (attachment.position.y + seatEntry->m_attachmentOffsetY) * scale;
lz = (attachment.position.z + seatEntry->m_attachmentOffsetZ) * scale;
break;
}
}
GetSeatCoordinates(seatEntry, lx, ly, lz);

// Set to new seat
itr->second->SetTransportSeat(seat);
Expand Down Expand Up @@ -659,6 +601,32 @@ void VehicleInfo::CalculateBoardingPositionOf(float gx, float gy, float gz, floa
lo = MapManager::NormalizeOrientation(go - m_owner->GetOrientation());
}

void VehicleInfo::GetSeatCoordinates(const VehicleSeatEntry* seatEntry, float& lx, float& ly, float& lz) const
{
uint32 displayId = 0;
if (m_owner->IsPlayer() && static_cast<Player*>(m_owner)->IsMounted())
displayId = static_cast<Player*>(m_owner)->GetMountID();
else
displayId = static_cast<Unit*>(m_owner)->GetNativeDisplayId();
auto* creatureDisplayInfo = sCreatureDisplayInfoStore.LookupEntry(displayId);
float scale = creatureDisplayInfo->scale;
scale *= sCreatureModelDataStore.LookupEntry(creatureDisplayInfo->ModelId)->Scale;
auto attachmentItr = sModelAttachmentStore.find(creatureDisplayInfo->ModelId);
if (attachmentItr != sModelAttachmentStore.end())
{
for (auto& attachment : attachmentItr->second)
{
if (attachment.id == attachmentLookup(seatEntry->m_attachmentID))
{
lx = (attachment.position.x + seatEntry->m_attachmentOffsetX) * scale;
ly = (attachment.position.y + seatEntry->m_attachmentOffsetY) * scale;
lz = (attachment.position.z + seatEntry->m_attachmentOffsetZ) * scale;
break;
}
}
}
}

void VehicleInfo::RemoveAccessoriesFromMap()
{
// Remove all accessories
Expand Down Expand Up @@ -848,7 +816,10 @@ bool VehicleInfo::IsUsableSeatForPlayer(uint32 seatFlags, uint32 seatFlagsB) con
/// Add control and such modifiers to a passenger if required
void VehicleInfo::ApplySeatMods(Unit* passenger, uint32 seatFlags)
{
Unit* pVehicle = (Unit*)m_owner; // Vehicles are alawys Unit
if (m_owner->IsPlayer())
return;

Unit* pVehicle = static_cast<Unit*>(m_owner); // Vehicles are alawys Unit (except with multi-person mounts)

if (seatFlags & SEAT_FLAG_NOT_SELECTABLE)
passenger->SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_UNINTERACTIBLE);
Expand Down Expand Up @@ -945,7 +916,10 @@ void VehicleInfo::ApplySeatMods(Unit* passenger, uint32 seatFlags)
/// Remove control and such modifiers to a passenger if they were added
void VehicleInfo::RemoveSeatMods(Unit* passenger, uint32 seatFlags)
{
Unit* pVehicle = (Unit*)m_owner;
if (m_owner->IsPlayer())
return;

Unit* pVehicle = static_cast<Unit*>(m_owner);

if (seatFlags & SEAT_FLAG_NOT_SELECTABLE)
passenger->RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_UNINTERACTIBLE);
Expand Down
3 changes: 2 additions & 1 deletion src/game/Entities/Vehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ class VehicleInfo : public TransportBase
{
public:
explicit VehicleInfo(Unit* owner, VehicleEntry const* vehicleEntry, uint32 overwriteNpcEntry);
void RepopulateSeat(uint8 seatId);
void Initialize(); ///< Initializes the accessories
bool IsInitialized() const { return m_isInitialized; }
void SummonPassenger(uint32 entry, Position const& pos, uint8 seatId);
Expand Down Expand Up @@ -111,6 +110,8 @@ class VehicleInfo : public TransportBase
private:
// Internal use to calculate the boarding position
void CalculateBoardingPositionOf(float gx, float gy, float gz, float go, float& lx, float& ly, float& lz, float& lo) const;
// Internal use to calculate the coordinates of the seat
void GetSeatCoordinates(const VehicleSeatEntry* seatEntry, float& lx, float& ly, float& lz) const;

// Seat information
bool GetUsableSeatFor(Unit* passenger, uint8& seat, bool reset, bool next) const;
Expand Down
7 changes: 3 additions & 4 deletions src/game/Entities/VehicleHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ void WorldSession::HandleRideVehicleInteract(WorldPacket& recvPacket)
ObjectGuid playerGuid;
recvPacket >> playerGuid;

//Player* vehicle = _player->GetMap()->GetPlayer(playerGuid);
Unit* vehicle = _player->GetMap()->GetUnit(playerGuid);

if (!vehicle || !vehicle->IsVehicle())
Expand Down Expand Up @@ -236,10 +235,10 @@ void WorldSession::HandleEjectPassenger(WorldPacket& recvPacket)
return;

// _player is not on a vehicle
if (!_player->IsBoarded())
if (!_player->IsBoarded() && !_player->IsVehicle())
return;

Unit* vehicle = dynamic_cast<Unit*>(_player->GetTransportInfo()->GetTransport());
Unit* vehicle = static_cast<Unit*>(passenger->GetTransportInfo()->GetTransport());

if (!vehicle)
return;
Expand Down

0 comments on commit a89c0bd

Please sign in to comment.