TrinityCore
Loading...
Searching...
No Matches
VehicleHandler.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "WorldSession.h"
19#include "DB2Structure.h"
20#include "Log.h"
21#include "Map.h"
22#include "ObjectAccessor.h"
23#include "Player.h"
24#include "Vehicle.h"
25#include "VehiclePackets.h"
26
28{
29 ObjectGuid vehicleGUID = _player->GetCharmedGUID();
30 if (vehicleGUID.IsEmpty())
31 return;
32
33 if (moveDismissVehicle.Status.guid != vehicleGUID)
34 {
35 TC_LOG_ERROR("network", "Player {} tried to dismiss a controlled vehicle ({}) that he has no control over. Possible cheater or malformed packet.",
36 GetPlayer()->GetGUID().ToString().c_str(), vehicleGUID.ToString().c_str());
37 return;
38 }
39
41}
42
44{
45 Unit* vehicle_base = GetPlayer()->GetVehicleBase();
46 if (!vehicle_base)
47 return;
48
50 if (!seat->CanSwitchFromSeat())
51 {
52 TC_LOG_ERROR("network", "HandleRequestVehiclePrevSeat: {} tried to switch seats but current seatflags {} don't permit that.",
53 GetPlayer()->GetGUID().ToString(), seat->Flags);
54 return;
55 }
56
57 GetPlayer()->ChangeSeat(-1, false);
58}
59
61{
62 Unit* vehicle_base = GetPlayer()->GetVehicleBase();
63 if (!vehicle_base)
64 return;
65
67 if (!seat->CanSwitchFromSeat())
68 {
69 TC_LOG_ERROR("network", "HandleRequestVehicleNextSeat: {} tried to switch seats but current seatflags {} don't permit that.",
70 GetPlayer()->GetGUID().ToString(), seat->Flags);
71 return;
72 }
73
74 GetPlayer()->ChangeSeat(-1, true);
75}
76
78{
79 Unit* vehicle_base = GetPlayer()->GetVehicleBase();
80 if (!vehicle_base)
81 return;
82
84 if (!seat->CanSwitchFromSeat())
85 {
86 TC_LOG_ERROR("network", "HandleMoveChangeVehicleSeats: Player {} tried to switch seats but current seatflags {} don't permit that.",
87 GetPlayer()->GetGUID().ToString(), seat->Flags);
88 return;
89 }
90
91 if (!ValidateMovementInfo(&moveChangeVehicleSeats.Status))
92 return;
93
94 vehicle_base->m_movementInfo = moveChangeVehicleSeats.Status;
95
96 if (moveChangeVehicleSeats.DstVehicle.IsEmpty())
97 GetPlayer()->ChangeSeat(-1, moveChangeVehicleSeats.DstSeatIndex != 255);
98 else if (Unit* vehUnit = ObjectAccessor::GetUnit(*GetPlayer(), moveChangeVehicleSeats.DstVehicle))
99 if (Vehicle* vehicle = vehUnit->GetVehicleKit())
100 if (vehicle->HasEmptySeat(moveChangeVehicleSeats.DstSeatIndex))
101 vehUnit->HandleSpellClick(GetPlayer(), int8(moveChangeVehicleSeats.DstSeatIndex));
102}
103
105{
106 Unit* vehicle_base = GetPlayer()->GetVehicleBase();
107 if (!vehicle_base)
108 return;
109
111 if (!seat->CanSwitchFromSeat())
112 {
113 TC_LOG_ERROR("network", "HandleRequestVehicleSwitchSeat: {} tried to switch seats but current seatflags {} don't permit that.",
114 GetPlayer()->GetGUID().ToString(), seat->Flags);
115 return;
116 }
117
118 if (vehicle_base->GetGUID() == requestVehicleSwitchSeat.Vehicle)
119 GetPlayer()->ChangeSeat(int8(requestVehicleSwitchSeat.SeatIndex));
120 else if (Unit* vehUnit = ObjectAccessor::GetUnit(*GetPlayer(), requestVehicleSwitchSeat.Vehicle))
121 if (Vehicle* vehicle = vehUnit->GetVehicleKit())
122 if (vehicle->HasEmptySeat(int8(requestVehicleSwitchSeat.SeatIndex)))
123 vehUnit->HandleSpellClick(GetPlayer(), int8(requestVehicleSwitchSeat.SeatIndex));
124}
125
127{
128 if (Player* player = ObjectAccessor::GetPlayer(*_player, rideVehicleInteract.Vehicle))
129 {
130 if (!player->GetVehicleKit())
131 return;
132 if (!player->IsInRaidWith(_player))
133 return;
134 if (!player->IsWithinDistInMap(_player, INTERACTION_DISTANCE))
135 return;
136 // Dont' allow players to enter player vehicle on arena
138 return;
139
140 _player->EnterVehicle(player);
141 }
142}
143
145{
146 Vehicle* vehicle = _player->GetVehicleKit();
147 if (!vehicle)
148 {
149 TC_LOG_ERROR("network", "HandleEjectPassenger: {} is not in a vehicle!", GetPlayer()->GetGUID().ToString());
150 return;
151 }
152
153 if (ejectPassenger.Passenger.IsUnit())
154 {
155 Unit* unit = ObjectAccessor::GetUnit(*_player, ejectPassenger.Passenger);
156 if (!unit) // creatures can be ejected too from player mounts
157 {
158 TC_LOG_ERROR("network", "{} tried to eject {} from vehicle, but the latter was not found in world!", GetPlayer()->GetGUID().ToString(), ejectPassenger.Passenger.ToString());
159 return;
160 }
161
162 if (!unit->IsOnVehicle(vehicle->GetBase()))
163 {
164 TC_LOG_ERROR("network", "{} tried to eject {}, but they are not in the same vehicle", GetPlayer()->GetGUID().ToString(), ejectPassenger.Passenger.ToString());
165 return;
166 }
167
168 VehicleSeatEntry const* seat = vehicle->GetSeatForPassenger(unit);
169 ASSERT(seat);
170 if (seat->IsEjectable())
171 unit->ExitVehicle();
172 else
173 TC_LOG_ERROR("network", "Player {} attempted to eject {} from non-ejectable seat.", GetPlayer()->GetGUID().ToString(), ejectPassenger.Passenger.ToString());
174 }
175 else
176 TC_LOG_ERROR("network", "HandleEjectPassenger: {} tried to eject invalid {} ", GetPlayer()->GetGUID().ToString(), ejectPassenger.Passenger.ToString());
177}
178
180{
181 if (Vehicle* vehicle = GetPlayer()->GetVehicle())
182 {
183 auto itr = vehicle->GetSeatIteratorForPassenger(GetPlayer());
184 if (itr != vehicle->Seats.end())
185 {
186 if (itr->second.SeatInfo->CanEnterOrExit())
188 else
189 TC_LOG_ERROR("network", "Player {} tried to exit vehicle, but seatflags {} (ID: {}) don't permit that.",
190 GetPlayer()->GetGUID().ToString(), vehicle->GetVehicleInfo()->SeatID[itr->first], itr->second.SeatInfo->Flags);
191 }
192 }
193}
194
int8_t int8
Definition Define.h:152
#define ASSERT
Definition Errors.h:80
#define TC_LOG_ERROR(filterType__, message__,...)
Definition Log.h:190
#define INTERACTION_DISTANCE
ObjectGuid const & GetGUID() const
Definition BaseEntity.h:163
bool IsBattleArena() const
Definition Map.cpp:3364
bool IsEmpty() const
Definition ObjectGuid.h:362
bool IsUnit() const
Definition ObjectGuid.h:370
std::string ToString() const
Definition Unit.h:635
void EnterVehicle(Unit *base, int8 seatId=-1)
Definition Unit.cpp:12749
Vehicle * GetVehicle() const
Definition Unit.h:1784
Unit * GetVehicleBase() const
Definition Unit.cpp:12111
ObjectGuid GetCharmedGUID() const
Definition Unit.h:1211
bool IsOnVehicle(Unit const *vehicle) const
Definition Unit.cpp:12106
void ChangeSeat(int8 seatId, bool next=true)
Definition Unit.cpp:12803
Vehicle * GetVehicleKit() const
Definition Unit.h:1782
virtual void ExitVehicle(Position const *exitPosition=nullptr)
Definition Unit.cpp:12835
Unit * GetBase() const
Definition Vehicle.h:49
VehicleSeatEntry const * GetSeatForPassenger(Unit const *passenger) const
Returns information on the seat of specified passenger, represented by the format in VehicleSeat....
Definition Vehicle.cpp:670
Map * FindMap() const
Definition Object.h:412
MovementInfo m_movementInfo
Definition Object.h:548
WorldPackets::Movement::MovementAck Data
void HandleRequestVehicleNextSeat(WorldPackets::Vehicle::RequestVehicleNextSeat &requestVehicleNextSeat)
void HandleEjectPassenger(WorldPackets::Vehicle::EjectPassenger &ejectPassenger)
bool ValidateMovementInfo(MovementInfo *mi) const
Player * GetPlayer() const
void HandleRequestVehicleExit(WorldPackets::Vehicle::RequestVehicleExit &requestVehicleExit)
void HandleMoveChangeVehicleSeats(WorldPackets::Vehicle::MoveChangeVehicleSeats &moveChangeVehicleSeats)
void HandleMoveSetVehicleRecAck(WorldPackets::Vehicle::MoveSetVehicleRecIdAck &setVehicleRecIdAck)
void HandleRequestVehiclePrevSeat(WorldPackets::Vehicle::RequestVehiclePrevSeat &requestVehiclePrevSeat)
Player * _player
void HandleRideVehicleInteract(WorldPackets::Vehicle::RideVehicleInteract &rideVehicleInteract)
void HandleRequestVehicleSwitchSeat(WorldPackets::Vehicle::RequestVehicleSwitchSeat &requestVehicleSwitchSeat)
void HandleMoveDismissVehicle(WorldPackets::Vehicle::MoveDismissVehicle &moveDismissVehicle)
TC_GAME_API Unit * GetUnit(WorldObject const &, ObjectGuid const &guid)
TC_GAME_API Player * GetPlayer(Map const *, ObjectGuid const &guid)
ObjectGuid guid
bool IsEjectable() const
bool CanSwitchFromSeat() const