Updated Copyright year to 2013
[getmangos.git] / src / game / TaxiHandler.cpp
blob2730b8f28149f41d9278a7363a7af8c260daa321
1 /*
2 * Copyright (C) 2005-2013 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Common.h"
20 #include "Database/DatabaseEnv.h"
21 #include "WorldPacket.h"
22 #include "WorldSession.h"
23 #include "Opcodes.h"
24 #include "Log.h"
25 #include "ObjectMgr.h"
26 #include "Player.h"
27 #include "UpdateMask.h"
28 #include "Path.h"
29 #include "WaypointMovementGenerator.h"
31 void WorldSession::HandleTaxiNodeStatusQueryOpcode(WorldPacket& recv_data)
33 DEBUG_LOG("WORLD: Received CMSG_TAXINODE_STATUS_QUERY");
35 ObjectGuid guid;
37 recv_data >> guid;
38 SendTaxiStatus(guid);
41 void WorldSession::SendTaxiStatus(ObjectGuid guid)
43 // cheating checks
44 Creature* unit = GetPlayer()->GetMap()->GetCreature(guid);
45 if (!unit)
47 DEBUG_LOG("WorldSession::SendTaxiStatus - %s not found or you can't interact with it.", guid.GetString().c_str());
48 return;
51 uint32 curloc = sObjectMgr.GetNearestTaxiNode(unit->GetPositionX(), unit->GetPositionY(), unit->GetPositionZ(), unit->GetMapId(), GetPlayer()->GetTeam());
53 // not found nearest
54 if (curloc == 0)
55 return;
57 DEBUG_LOG("WORLD: current location %u ", curloc);
59 WorldPacket data(SMSG_TAXINODE_STATUS, 9);
60 data << ObjectGuid(guid);
61 data << uint8(GetPlayer()->m_taxi.IsTaximaskNodeKnown(curloc) ? 1 : 0);
62 SendPacket(&data);
64 DEBUG_LOG("WORLD: Sent SMSG_TAXINODE_STATUS");
67 void WorldSession::HandleTaxiQueryAvailableNodes(WorldPacket& recv_data)
69 DEBUG_LOG("WORLD: Received CMSG_TAXIQUERYAVAILABLENODES");
71 ObjectGuid guid;
72 recv_data >> guid;
74 // cheating checks
75 Creature* unit = GetPlayer()->GetNPCIfCanInteractWith(guid, UNIT_NPC_FLAG_FLIGHTMASTER);
76 if (!unit)
78 DEBUG_LOG("WORLD: HandleTaxiQueryAvailableNodes - %s not found or you can't interact with him.", guid.GetString().c_str());
79 return;
82 // remove fake death
83 if (GetPlayer()->hasUnitState(UNIT_STAT_DIED))
84 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
86 // unknown taxi node case
87 if (SendLearnNewTaxiNode(unit))
88 return;
90 // known taxi node case
91 SendTaxiMenu(unit);
94 void WorldSession::SendTaxiMenu(Creature* unit)
96 // find current node
97 uint32 curloc = sObjectMgr.GetNearestTaxiNode(unit->GetPositionX(), unit->GetPositionY(), unit->GetPositionZ(), unit->GetMapId(), GetPlayer()->GetTeam());
99 if (curloc == 0)
100 return;
102 DEBUG_LOG("WORLD: CMSG_TAXINODE_STATUS_QUERY %u ", curloc);
104 WorldPacket data(SMSG_SHOWTAXINODES, (4 + 8 + 4 + 8 * 4));
105 data << uint32(1);
106 data << unit->GetObjectGuid();
107 data << uint32(curloc);
108 GetPlayer()->m_taxi.AppendTaximaskTo(data, GetPlayer()->isTaxiCheater());
109 SendPacket(&data);
111 DEBUG_LOG("WORLD: Sent SMSG_SHOWTAXINODES");
114 void WorldSession::SendDoFlight(uint32 mountDisplayId, uint32 path, uint32 pathNode)
116 // remove fake death
117 if (GetPlayer()->hasUnitState(UNIT_STAT_DIED))
118 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
120 while (GetPlayer()->GetMotionMaster()->GetCurrentMovementGeneratorType() == FLIGHT_MOTION_TYPE)
121 GetPlayer()->GetMotionMaster()->MovementExpired(false);
123 if (mountDisplayId)
124 GetPlayer()->Mount(mountDisplayId);
126 GetPlayer()->GetMotionMaster()->MoveTaxiFlight(path, pathNode);
129 bool WorldSession::SendLearnNewTaxiNode(Creature* unit)
131 // find current node
132 uint32 curloc = sObjectMgr.GetNearestTaxiNode(unit->GetPositionX(), unit->GetPositionY(), unit->GetPositionZ(), unit->GetMapId(), GetPlayer()->GetTeam());
134 if (curloc == 0)
135 return true; // `true` send to avoid WorldSession::SendTaxiMenu call with one more curlock seartch with same false result.
137 if (GetPlayer()->m_taxi.SetTaximaskNode(curloc))
139 WorldPacket msg(SMSG_NEW_TAXI_PATH, 0);
140 SendPacket(&msg);
142 WorldPacket update(SMSG_TAXINODE_STATUS, 9);
143 update << ObjectGuid(unit->GetObjectGuid());
144 update << uint8(1);
145 SendPacket(&update);
147 return true;
149 else
150 return false;
153 void WorldSession::SendActivateTaxiReply(ActivateTaxiReply reply)
155 WorldPacket data(SMSG_ACTIVATETAXIREPLY, 4);
156 data << uint32(reply);
157 SendPacket(&data);
159 DEBUG_LOG("WORLD: Sent SMSG_ACTIVATETAXIREPLY");
162 void WorldSession::HandleActivateTaxiExpressOpcode(WorldPacket& recv_data)
164 DEBUG_LOG("WORLD: Received CMSG_ACTIVATETAXIEXPRESS");
166 ObjectGuid guid;
167 uint32 node_count;
169 recv_data >> guid >> node_count;
171 Creature* npc = GetPlayer()->GetNPCIfCanInteractWith(guid, UNIT_NPC_FLAG_FLIGHTMASTER);
172 if (!npc)
174 DEBUG_LOG("WORLD: HandleActivateTaxiExpressOpcode - %s not found or you can't interact with it.", guid.GetString().c_str());
175 return;
177 std::vector<uint32> nodes;
179 for (uint32 i = 0; i < node_count; ++i)
181 uint32 node;
182 recv_data >> node;
183 nodes.push_back(node);
186 if (nodes.empty())
187 return;
189 DEBUG_LOG("WORLD: Received CMSG_ACTIVATETAXIEXPRESS from %d to %d" , nodes.front(), nodes.back());
191 GetPlayer()->ActivateTaxiPathTo(nodes, npc);
194 void WorldSession::HandleMoveSplineDoneOpcode(WorldPacket& recv_data)
196 DEBUG_LOG("WORLD: Received CMSG_MOVE_SPLINE_DONE");
198 MovementInfo movementInfo; // used only for proper packet read
199 recv_data >> movementInfo;
201 // in taxi flight packet received in 2 case:
202 // 1) end taxi path in far (multi-node) flight
203 // 2) switch from one map to other in case multi-map taxi path
204 // we need process only (1)
205 uint32 curDest = GetPlayer()->m_taxi.GetTaxiDestination();
206 if (!curDest)
207 return;
209 TaxiNodesEntry const* curDestNode = sTaxiNodesStore.LookupEntry(curDest);
211 // far teleport case
212 if (curDestNode && curDestNode->map_id != GetPlayer()->GetMapId())
214 if (GetPlayer()->GetMotionMaster()->GetCurrentMovementGeneratorType() == FLIGHT_MOTION_TYPE)
216 // short preparations to continue flight
217 FlightPathMovementGenerator* flight = (FlightPathMovementGenerator*)(GetPlayer()->GetMotionMaster()->top());
219 flight->Interrupt(*GetPlayer()); // will reset at map landing
221 flight->SetCurrentNodeAfterTeleport();
222 TaxiPathNodeEntry const& node = flight->GetPath()[flight->GetCurrentNode()];
223 flight->SkipCurrentNode();
225 GetPlayer()->TeleportTo(curDestNode->map_id, node.x, node.y, node.z, GetPlayer()->GetOrientation());
227 return;
230 uint32 destinationnode = GetPlayer()->m_taxi.NextTaxiDestination();
231 if (destinationnode > 0) // if more destinations to go
233 // current source node for next destination
234 uint32 sourcenode = GetPlayer()->m_taxi.GetTaxiSource();
236 // Add to taximask middle hubs in taxicheat mode (to prevent having player with disabled taxicheat and not having back flight path)
237 if (GetPlayer()->isTaxiCheater())
239 if (GetPlayer()->m_taxi.SetTaximaskNode(sourcenode))
241 WorldPacket data(SMSG_NEW_TAXI_PATH, 0);
242 _player->GetSession()->SendPacket(&data);
246 DEBUG_LOG("WORLD: Taxi has to go from %u to %u", sourcenode, destinationnode);
248 uint32 mountDisplayId = sObjectMgr.GetTaxiMountDisplayId(sourcenode, GetPlayer()->GetTeam());
250 uint32 path, cost;
251 sObjectMgr.GetTaxiPath(sourcenode, destinationnode, path, cost);
253 if (path && mountDisplayId)
254 SendDoFlight(mountDisplayId, path, 1); // skip start fly node
255 else
256 GetPlayer()->m_taxi.ClearTaxiDestinations(); // clear problematic path and next
258 else
259 GetPlayer()->m_taxi.ClearTaxiDestinations(); // not destinations, clear source node
262 void WorldSession::HandleActivateTaxiOpcode(WorldPacket& recv_data)
264 DEBUG_LOG("WORLD: Received CMSG_ACTIVATETAXI");
266 ObjectGuid guid;
267 std::vector<uint32> nodes;
268 nodes.resize(2);
270 recv_data >> guid >> nodes[0] >> nodes[1];
271 DEBUG_LOG("WORLD: Received CMSG_ACTIVATETAXI from %d to %d" , nodes[0], nodes[1]);
272 Creature* npc = GetPlayer()->GetNPCIfCanInteractWith(guid, UNIT_NPC_FLAG_FLIGHTMASTER);
273 if (!npc)
275 DEBUG_LOG("WORLD: HandleActivateTaxiOpcode - %s not found or you can't interact with it.", guid.GetString().c_str());
276 return;
279 GetPlayer()->ActivateTaxiPathTo(nodes, npc);