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 "DBCStructure.h"
20#include "Log.h"
21#include "Map.h"
22#include "MovementPackets.h"
23#include "ObjectAccessor.h"
24#include "Player.h"
25#include "Vehicle.h"
26#include "WorldPacket.h"
27
29{
30 TC_LOG_DEBUG("network", "WORLD: Recvd CMSG_DISMISS_CONTROLLED_VEHICLE");
31
32 MovementInfo mi;
33
34 recvData >> mi.guid.ReadAsPacked();
35 recvData >> mi;
36
37 Unit* mover = ValidateAndGetUnitBeingMoved(mi.guid, false);
38 if (!ValidateMovementInfo(mover, &mi))
39 return;
40
42 mover->m_movementInfo = mi;
43
44 if (Unit* vehicleBase = _player->GetVehicleBase())
45 {
46 vehicleBase->SendPetDismissSound();
48 }
49}
50
52{
53 TC_LOG_DEBUG("network", "WORLD: Recvd CMSG_CHANGE_SEATS_ON_CONTROLLED_VEHICLE");
54
55 Unit* vehicle_base = GetPlayer()->GetVehicleBase();
56 if (!vehicle_base)
57 {
58 recvData.rfinish(); // prevent warnings spam
59 return;
60 }
61
63 if (!seat->CanSwitchFromSeat())
64 {
65 recvData.rfinish(); // prevent warnings spam
66 TC_LOG_ERROR("network", "HandleChangeSeatsOnControlledVehicle, Opcode: {}, Player {} tried to switch seats but current seatflags {} don't permit that.",
67 recvData.GetOpcode(), GetPlayer()->GetGUID().ToString(), seat->Flags);
68 return;
69 }
70
71 switch (recvData.GetOpcode())
72 {
74 GetPlayer()->ChangeSeat(-1, false);
75 break;
77 GetPlayer()->ChangeSeat(-1, true);
78 break;
80 {
81 ObjectGuid guid; // current vehicle guid
82 recvData >> guid.ReadAsPacked();
83
84 MovementInfo movementInfo;
85 movementInfo.guid = guid;
86 ReadMovementInfo(recvData, &movementInfo);
87 vehicle_base->m_movementInfo = movementInfo;
88
89 ObjectGuid accessory; // accessory guid
90 recvData >> accessory.ReadAsPacked();
91
92 int8 seatId;
93 recvData >> seatId;
94
95 if (vehicle_base->GetGUID() != guid)
96 return;
97
98 if (!accessory)
99 GetPlayer()->ChangeSeat(-1, seatId > 0); // prev/next
100 else if (Unit* vehUnit = ObjectAccessor::GetUnit(*GetPlayer(), accessory))
101 {
102 if (Vehicle* vehicle = vehUnit->GetVehicleKit())
103 if (vehicle->HasEmptySeat(seatId))
104 vehUnit->HandleSpellClick(GetPlayer(), seatId);
105 }
106 break;
107 }
109 {
110 ObjectGuid guid; // current vehicle guid
111 recvData >> guid.ReadAsPacked();
112
113 int8 seatId;
114 recvData >> seatId;
115
116 if (vehicle_base->GetGUID() == guid)
117 GetPlayer()->ChangeSeat(seatId);
118 else if (Unit* vehUnit = ObjectAccessor::GetUnit(*GetPlayer(), guid))
119 if (Vehicle* vehicle = vehUnit->GetVehicleKit())
120 if (vehicle->HasEmptySeat(seatId))
121 vehUnit->HandleSpellClick(GetPlayer(), seatId);
122 break;
123 }
124 default:
125 break;
126 }
127}
128
130{
131 // Read guid
132 ObjectGuid guid;
133 data >> guid;
134
135 if (Player* player = ObjectAccessor::GetPlayer(*_player, guid))
136 {
137 if (!player->GetVehicleKit())
138 return;
139 if (!player->IsInRaidWith(_player))
140 return;
141 if (!player->IsWithinDistInMap(_player, INTERACTION_DISTANCE))
142 return;
143 // Dont' allow players to enter player vehicle on arena
145 return;
146
147 _player->EnterVehicle(player);
148 }
149}
150
152{
153 Vehicle* vehicle = _player->GetVehicleKit();
154 if (!vehicle)
155 {
156 data.rfinish(); // prevent warnings spam
157 TC_LOG_ERROR("network", "HandleEjectPassenger: {} is not in a vehicle!", GetPlayer()->GetGUID().ToString());
158 return;
159 }
160
161 ObjectGuid guid;
162 data >> guid;
163
164 if (guid.IsUnit())
165 {
166 Unit* unit = ObjectAccessor::GetUnit(*_player, guid);
167 if (!unit) // creatures can be ejected too from player mounts
168 {
169 TC_LOG_ERROR("network", "{} tried to eject {} from vehicle, but the latter was not found in world!", GetPlayer()->GetGUID().ToString(), guid.ToString());
170 return;
171 }
172
173 if (!unit->IsOnVehicle(vehicle->GetBase()))
174 {
175 TC_LOG_ERROR("network", "{} tried to eject {}, but they are not in the same vehicle", GetPlayer()->GetGUID().ToString(), guid.ToString());
176 return;
177 }
178
179 VehicleSeatEntry const* seat = vehicle->GetSeatForPassenger(unit);
180 ASSERT(seat);
181 if (seat->IsEjectable())
182 unit->ExitVehicle();
183 else
184 TC_LOG_ERROR("network", "Player {} attempted to eject {} from non-ejectable seat.", GetPlayer()->GetGUID().ToString(), guid.ToString());
185 }
186 else
187 TC_LOG_ERROR("network", "HandleEjectPassenger: {} tried to eject invalid {} ", GetPlayer()->GetGUID().ToString(), guid.ToString());
188}
189
191{
192 TC_LOG_DEBUG("network", "WORLD: Recvd CMSG_REQUEST_VEHICLE_EXIT");
193
194 if (Vehicle* vehicle = GetPlayer()->GetVehicle())
195 {
196 if (VehicleSeatEntry const* seat = vehicle->GetSeatForPassenger(GetPlayer()))
197 {
198 if (seat->CanEnterOrExit())
200 else
201 TC_LOG_ERROR("network", "Player {} tried to exit vehicle, but seatflags {} (ID: {}) don't permit that.",
202 GetPlayer()->GetGUID().ToString(), seat->ID, seat->Flags);
203 }
204 }
205}
int8_t int8
Definition Define.h:131
#define ASSERT
Definition Errors.h:68
#define TC_LOG_DEBUG(filterType__,...)
Definition Log.h:156
#define TC_LOG_ERROR(filterType__,...)
Definition Log.h:165
#define INTERACTION_DISTANCE
void rfinish()
Definition ByteBuffer.h:316
bool IsBattleArena() const
Definition Map.cpp:4272
bool IsUnit() const
Definition ObjectGuid.h:180
std::string ToString() const
PackedGuidReader ReadAsPacked()
Definition ObjectGuid.h:146
static ObjectGuid GetGUID(Object const *o)
Definition Object.h:78
Definition Unit.h:769
void EnterVehicle(Unit *base, int8 seatId=-1)
Definition Unit.cpp:12569
Vehicle * GetVehicle() const
Definition Unit.h:1737
Unit * GetVehicleBase() const
Definition Unit.cpp:11826
bool IsOnVehicle(Unit const *vehicle) const
Definition Unit.cpp:11821
void ChangeSeat(int8 seatId, bool next=true)
Definition Unit.cpp:12629
Vehicle * GetVehicleKit() const
Definition Unit.h:1735
virtual void ExitVehicle(Position const *exitPosition=nullptr)
Definition Unit.cpp:12661
Unit * GetBase() const
May be called from scripts.
Definition Vehicle.h:45
VehicleSeatEntry const * GetSeatForPassenger(Unit const *passenger) const
Returns information on the seat of specified passenger, represented by the format in VehicleSeat....
Definition Vehicle.cpp:646
Map * FindMap() const
Definition Object.h:450
MovementInfo m_movementInfo
Definition Object.h:575
uint16 GetOpcode() const
Definition WorldPacket.h:80
void HandleEnterPlayerVehicle(WorldPacket &data)
void HandleDismissControlledVehicle(WorldPacket &recvData)
Unit * ValidateAndGetUnitBeingMoved(ObjectGuid guid, bool forStatusAck) const
void HandleChangeSeatsOnControlledVehicle(WorldPacket &recvData)
void HandleEjectPassenger(WorldPacket &data)
uint32 AdjustClientMovementTime(uint32 time) const
Player * GetPlayer() const
bool ValidateMovementInfo(Unit const *mover, MovementInfo *mi) const
void HandleRequestVehicleExit(WorldPacket &recvData)
void ReadMovementInfo(WorldPacket &data, MovementInfo *mi)
Player * _player
@ CMSG_REQUEST_VEHICLE_SWITCH_SEAT
Definition Opcodes.h:1174
@ CMSG_REQUEST_VEHICLE_NEXT_SEAT
Definition Opcodes.h:1173
@ CMSG_REQUEST_VEHICLE_PREV_SEAT
Definition Opcodes.h:1172
@ CMSG_CHANGE_SEATS_ON_CONTROLLED_VEHICLE
Definition Opcodes.h:1208
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