TrinityCore
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 GetPlayer()->ValidateMovementInfo(&moveChangeVehicleSeats.Status);
92
93 if (vehicle_base->GetGUID() != moveChangeVehicleSeats.Status.guid)
94 return;
95
96 vehicle_base->m_movementInfo = moveChangeVehicleSeats.Status;
97
98 if (moveChangeVehicleSeats.DstVehicle.IsEmpty())
99 GetPlayer()->ChangeSeat(-1, moveChangeVehicleSeats.DstSeatIndex != 255);
100 else if (Unit* vehUnit = ObjectAccessor::GetUnit(*GetPlayer(), moveChangeVehicleSeats.DstVehicle))
101 if (Vehicle* vehicle = vehUnit->GetVehicleKit())
102 if (vehicle->HasEmptySeat(moveChangeVehicleSeats.DstSeatIndex))
103 vehUnit->HandleSpellClick(GetPlayer(), int8(moveChangeVehicleSeats.DstSeatIndex));
104}
105
107{
108 Unit* vehicle_base = GetPlayer()->GetVehicleBase();
109 if (!vehicle_base)
110 return;
111
113 if (!seat->CanSwitchFromSeat())
114 {
115 TC_LOG_ERROR("network", "HandleRequestVehicleSwitchSeat: {} tried to switch seats but current seatflags {} don't permit that.",
116 GetPlayer()->GetGUID().ToString(), seat->Flags);
117 return;
118 }
119
120 if (vehicle_base->GetGUID() == requestVehicleSwitchSeat.Vehicle)
121 GetPlayer()->ChangeSeat(int8(requestVehicleSwitchSeat.SeatIndex));
122 else if (Unit* vehUnit = ObjectAccessor::GetUnit(*GetPlayer(), requestVehicleSwitchSeat.Vehicle))
123 if (Vehicle* vehicle = vehUnit->GetVehicleKit())
124 if (vehicle->HasEmptySeat(int8(requestVehicleSwitchSeat.SeatIndex)))
125 vehUnit->HandleSpellClick(GetPlayer(), int8(requestVehicleSwitchSeat.SeatIndex));
126}
127
129{
130 if (Player* player = ObjectAccessor::GetPlayer(*_player, rideVehicleInteract.Vehicle))
131 {
132 if (!player->GetVehicleKit())
133 return;
134 if (!player->IsInRaidWith(_player))
135 return;
136 if (!player->IsWithinDistInMap(_player, INTERACTION_DISTANCE))
137 return;
138 // Dont' allow players to enter player vehicle on arena
140 return;
141
142 _player->EnterVehicle(player);
143 }
144}
145
147{
148 Vehicle* vehicle = _player->GetVehicleKit();
149 if (!vehicle)
150 {
151 TC_LOG_ERROR("network", "HandleEjectPassenger: {} is not in a vehicle!", GetPlayer()->GetGUID().ToString());
152 return;
153 }
154
155 if (ejectPassenger.Passenger.IsUnit())
156 {
157 Unit* unit = ObjectAccessor::GetUnit(*_player, ejectPassenger.Passenger);
158 if (!unit) // creatures can be ejected too from player mounts
159 {
160 TC_LOG_ERROR("network", "{} tried to eject {} from vehicle, but the latter was not found in world!", GetPlayer()->GetGUID().ToString(), ejectPassenger.Passenger.ToString());
161 return;
162 }
163
164 if (!unit->IsOnVehicle(vehicle->GetBase()))
165 {
166 TC_LOG_ERROR("network", "{} tried to eject {}, but they are not in the same vehicle", GetPlayer()->GetGUID().ToString(), ejectPassenger.Passenger.ToString());
167 return;
168 }
169
170 VehicleSeatEntry const* seat = vehicle->GetSeatForPassenger(unit);
171 ASSERT(seat);
172 if (seat->IsEjectable())
173 unit->ExitVehicle();
174 else
175 TC_LOG_ERROR("network", "Player {} attempted to eject {} from non-ejectable seat.", GetPlayer()->GetGUID().ToString(), ejectPassenger.Passenger.ToString());
176 }
177 else
178 TC_LOG_ERROR("network", "HandleEjectPassenger: {} tried to eject invalid {} ", GetPlayer()->GetGUID().ToString(), ejectPassenger.Passenger.ToString());
179}
180
182{
183 if (Vehicle* vehicle = GetPlayer()->GetVehicle())
184 {
185 auto itr = vehicle->GetSeatIteratorForPassenger(GetPlayer());
186 if (itr != vehicle->Seats.end())
187 {
188 if (itr->second.SeatInfo->CanEnterOrExit())
190 else
191 TC_LOG_ERROR("network", "Player {} tried to exit vehicle, but seatflags {} (ID: {}) don't permit that.",
192 GetPlayer()->GetGUID().ToString(), vehicle->GetVehicleInfo()->SeatID[itr->first], itr->second.SeatInfo->Flags);
193 }
194 }
195}
196
198{
199 GetPlayer()->ValidateMovementInfo(&setVehicleRecIdAck.Data.Status);
200}
int8_t int8
Definition: Define.h:140
#define ASSERT
Definition: Errors.h:68
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:165
#define INTERACTION_DISTANCE
Definition: ObjectDefines.h:24
bool IsBattleArena() const
Definition: Map.cpp:3335
bool IsEmpty() const
Definition: ObjectGuid.h:319
bool IsUnit() const
Definition: ObjectGuid.h:327
std::string ToString() const
Definition: ObjectGuid.cpp:554
static ObjectGuid GetGUID(Object const *o)
Definition: Object.h:159
void ValidateMovementInfo(MovementInfo *mi)
Definition: Player.cpp:29539
Definition: Unit.h:627
void EnterVehicle(Unit *base, int8 seatId=-1)
Definition: Unit.cpp:12118
Vehicle * GetVehicle() const
Definition: Unit.h:1713
Unit * GetVehicleBase() const
Definition: Unit.cpp:11480
ObjectGuid GetCharmedGUID() const
Definition: Unit.h:1190
bool IsOnVehicle(Unit const *vehicle) const
Definition: Unit.cpp:11475
void ChangeSeat(int8 seatId, bool next=true)
Definition: Unit.cpp:12172
Vehicle * GetVehicleKit() const
Definition: Unit.h:1711
virtual void ExitVehicle(Position const *exitPosition=nullptr)
Definition: Unit.cpp:12204
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:647
Map * FindMap() const
Definition: Object.h:625
MovementInfo m_movementInfo
Definition: Object.h:761
WorldPackets::Movement::MovementAck Data
void HandleRequestVehicleNextSeat(WorldPackets::Vehicle::RequestVehicleNextSeat &requestVehicleNextSeat)
void HandleEjectPassenger(WorldPackets::Vehicle::EjectPassenger &ejectPassenger)
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)
std::string ToString(Type &&val, Params &&... params)
ObjectGuid guid
Definition: MovementInfo.h:30
bool IsEjectable() const
bool CanSwitchFromSeat() const