[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / src / game / Transports.cpp
blob6f6178694d0e85614a48edf921788ced88d0e977
1 /*
2 * Copyright (C) 2005-2009 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"
21 #include "Transports.h"
22 #include "MapManager.h"
23 #include "ObjectMgr.h"
24 #include "Path.h"
26 #include "WorldPacket.h"
27 #include "Database/DBCStores.h"
28 #include "ProgressBar.h"
30 void MapManager::LoadTransports()
32 QueryResult *result = WorldDatabase.Query("SELECT entry, name, period FROM transports");
34 uint32 count = 0;
36 if( !result )
38 barGoLink bar( 1 );
39 bar.step();
41 sLog.outString();
42 sLog.outString( ">> Loaded %u transports", count );
43 return;
46 barGoLink bar( result->GetRowCount() );
50 bar.step();
52 Transport *t = new Transport;
54 Field *fields = result->Fetch();
56 uint32 entry = fields[0].GetUInt32();
57 std::string name = fields[1].GetCppString();
58 t->m_period = fields[2].GetUInt32();
60 const GameObjectInfo *goinfo = objmgr.GetGameObjectInfo(entry);
62 if(!goinfo)
64 sLog.outErrorDb("Transport ID:%u, Name: %s, will not be loaded, gameobject_template missing", entry, name.c_str());
65 delete t;
66 continue;
69 if(goinfo->type != GAMEOBJECT_TYPE_MO_TRANSPORT)
71 sLog.outErrorDb("Transport ID:%u, Name: %s, will not be loaded, gameobject_template type wrong", entry, name.c_str());
72 delete t;
73 continue;
76 // sLog.outString("Loading transport %d between %s, %s", entry, name.c_str(), goinfo->name);
78 std::set<uint32> mapsUsed;
80 if(!t->GenerateWaypoints(goinfo->moTransport.taxiPathId, mapsUsed))
81 // skip transports with empty waypoints list
83 sLog.outErrorDb("Transport (path id %u) path size = 0. Transport ignored, check DBC files or transport GO data0 field.",goinfo->moTransport.taxiPathId);
84 delete t;
85 continue;
88 t->m_name = goinfo->name;
90 float x, y, z, o;
91 uint32 mapid;
92 x = t->m_WayPoints[0].x; y = t->m_WayPoints[0].y; z = t->m_WayPoints[0].z; mapid = t->m_WayPoints[0].mapid; o = 1;
94 // creates the Gameobject
95 if(!t->Create(entry, mapid, x, y, z, o, 100, 0))
97 delete t;
98 continue;
101 m_Transports.insert(t);
103 for (std::set<uint32>::iterator i = mapsUsed.begin(); i != mapsUsed.end(); ++i)
104 m_TransportsByMap[*i].insert(t);
106 //If we someday decide to use the grid to track transports, here:
107 //MapManager::Instance().LoadGrid(mapid,x,y,true);
108 //t->GetMap()->Add<GameObject>((GameObject *)t);
109 ++count;
110 } while(result->NextRow());
111 delete result;
113 sLog.outString();
114 sLog.outString( ">> Loaded %u transports", count );
116 // check transport data DB integrity
117 result = WorldDatabase.Query("SELECT gameobject.guid,gameobject.id,transports.name FROM gameobject,transports WHERE gameobject.id = transports.entry");
118 if(result) // wrong data found
122 Field *fields = result->Fetch();
124 uint32 guid = fields[0].GetUInt32();
125 uint32 entry = fields[1].GetUInt32();
126 std::string name = fields[2].GetCppString();
127 sLog.outErrorDb("Transport %u '%s' have record (GUID: %u) in `gameobject`. Transports DON'T must have any records in `gameobject` or its behavior will be unpredictable/bugged.",entry,name.c_str(),guid);
129 while(result->NextRow());
131 delete result;
135 Transport::Transport() : GameObject()
137 // 2.3.2 - 0x5A
138 m_updateFlag = (UPDATEFLAG_TRANSPORT | UPDATEFLAG_LOWGUID | UPDATEFLAG_HIGHGUID | UPDATEFLAG_HAS_POSITION);
141 bool Transport::Create(uint32 guidlow, uint32 mapid, float x, float y, float z, float ang, uint32 animprogress, uint32 dynflags)
143 Relocate(x,y,z,ang);
145 SetMapId(mapid);
146 // instance id and phaseMask isn't set to values different from std.
148 if(!IsPositionValid())
150 sLog.outError("ERROR: Transport (GUID: %u) not created. Suggested coordinates isn't valid (X: %f Y: %f)",
151 guidlow,x,y);
152 return false;
155 Object::_Create(guidlow, 0, HIGHGUID_MO_TRANSPORT);
157 GameObjectInfo const* goinfo = objmgr.GetGameObjectInfo(guidlow);
159 if (!goinfo)
161 sLog.outErrorDb("Transport not created: entry in `gameobject_template` not found, guidlow: %u map: %u (X: %f Y: %f Z: %f) ang: %f",guidlow, mapid, x, y, z, ang);
162 return false;
165 m_goInfo = goinfo;
167 SetFloatValue(OBJECT_FIELD_SCALE_X, goinfo->size);
169 SetUInt32Value(GAMEOBJECT_FACTION, goinfo->faction);
170 //SetUInt32Value(GAMEOBJECT_FLAGS, goinfo->flags);
171 SetUInt32Value(GAMEOBJECT_FLAGS, MAKE_PAIR32(0x28, 0x64));
172 SetUInt32Value(GAMEOBJECT_LEVEL, m_period);
173 SetEntry(goinfo->id);
175 SetUInt32Value(GAMEOBJECT_DISPLAYID, goinfo->displayId);
177 SetGoState(1);
178 SetGoType(GameobjectTypes(goinfo->type));
180 SetGoAnimProgress(animprogress);
181 if(dynflags)
182 SetUInt32Value(GAMEOBJECT_DYNAMIC, MAKE_PAIR32(0, dynflags));
184 return true;
187 struct keyFrame
189 keyFrame(float _x, float _y, float _z, uint32 _mapid, int _actionflag, int _delay)
191 x = _x; y = _y; z = _z; mapid = _mapid; actionflag = _actionflag; delay = _delay; distFromPrev = -1; distSinceStop = -1; distUntilStop = -1;
192 tFrom = 0; tTo = 0;
195 float x;
196 float y;
197 float z;
198 uint32 mapid;
199 int actionflag;
200 int delay;
201 float distSinceStop;
202 float distUntilStop;
203 float distFromPrev;
204 float tFrom, tTo;
207 bool Transport::GenerateWaypoints(uint32 pathid, std::set<uint32> &mapids)
209 TransportPath path;
210 objmgr.GetTransportPathNodes(pathid, path);
212 if (path.Empty())
213 return false;
215 std::vector<keyFrame> keyFrames;
216 int mapChange = 0;
217 mapids.clear();
218 for (size_t i = 1; i < path.Size() - 1; i++)
220 if (mapChange == 0)
222 if ((path[i].mapid == path[i+1].mapid))
224 keyFrame k(path[i].x, path[i].y, path[i].z, path[i].mapid, path[i].actionFlag, path[i].delay);
225 keyFrames.push_back(k);
226 mapids.insert(k.mapid);
228 else
230 mapChange = 1;
233 else
235 --mapChange;
239 int lastStop = -1;
240 int firstStop = -1;
242 // first cell is arrived at by teleportation :S
243 keyFrames[0].distFromPrev = 0;
244 if (keyFrames[0].actionflag == 2)
246 lastStop = 0;
249 // find the rest of the distances between key points
250 for (size_t i = 1; i < keyFrames.size(); i++)
252 if ((keyFrames[i].actionflag == 1) || (keyFrames[i].mapid != keyFrames[i-1].mapid))
254 keyFrames[i].distFromPrev = 0;
256 else
258 keyFrames[i].distFromPrev =
259 sqrt(pow(keyFrames[i].x - keyFrames[i - 1].x, 2) +
260 pow(keyFrames[i].y - keyFrames[i - 1].y, 2) +
261 pow(keyFrames[i].z - keyFrames[i - 1].z, 2));
263 if (keyFrames[i].actionflag == 2)
265 // remember first stop frame
266 if(firstStop == -1)
267 firstStop = i;
268 lastStop = i;
272 float tmpDist = 0;
273 for (size_t i = 0; i < keyFrames.size(); i++)
275 int j = (i + lastStop) % keyFrames.size();
276 if (keyFrames[j].actionflag == 2)
277 tmpDist = 0;
278 else
279 tmpDist += keyFrames[j].distFromPrev;
280 keyFrames[j].distSinceStop = tmpDist;
283 for (int i = int(keyFrames.size()) - 1; i >= 0; i--)
285 int j = (i + (firstStop+1)) % keyFrames.size();
286 tmpDist += keyFrames[(j + 1) % keyFrames.size()].distFromPrev;
287 keyFrames[j].distUntilStop = tmpDist;
288 if (keyFrames[j].actionflag == 2)
289 tmpDist = 0;
292 for (size_t i = 0; i < keyFrames.size(); i++)
294 if (keyFrames[i].distSinceStop < (30 * 30 * 0.5f))
295 keyFrames[i].tFrom = sqrt(2 * keyFrames[i].distSinceStop);
296 else
297 keyFrames[i].tFrom = ((keyFrames[i].distSinceStop - (30 * 30 * 0.5f)) / 30) + 30;
299 if (keyFrames[i].distUntilStop < (30 * 30 * 0.5f))
300 keyFrames[i].tTo = sqrt(2 * keyFrames[i].distUntilStop);
301 else
302 keyFrames[i].tTo = ((keyFrames[i].distUntilStop - (30 * 30 * 0.5f)) / 30) + 30;
304 keyFrames[i].tFrom *= 1000;
305 keyFrames[i].tTo *= 1000;
308 // for (int i = 0; i < keyFrames.size(); i++) {
309 // sLog.outString("%f, %f, %f, %f, %f, %f, %f", keyFrames[i].x, keyFrames[i].y, keyFrames[i].distUntilStop, keyFrames[i].distSinceStop, keyFrames[i].distFromPrev, keyFrames[i].tFrom, keyFrames[i].tTo);
310 // }
312 // Now we're completely set up; we can move along the length of each waypoint at 100 ms intervals
313 // speed = max(30, t) (remember x = 0.5s^2, and when accelerating, a = 1 unit/s^2
314 int t = 0;
315 bool teleport = false;
316 if (keyFrames[keyFrames.size() - 1].mapid != keyFrames[0].mapid)
317 teleport = true;
319 WayPoint pos(keyFrames[0].mapid, keyFrames[0].x, keyFrames[0].y, keyFrames[0].z, teleport);
320 m_WayPoints[0] = pos;
321 t += keyFrames[0].delay * 1000;
323 uint32 cM = keyFrames[0].mapid;
324 for (size_t i = 0; i < keyFrames.size() - 1; i++)
326 float d = 0;
327 float tFrom = keyFrames[i].tFrom;
328 float tTo = keyFrames[i].tTo;
330 // keep the generation of all these points; we use only a few now, but may need the others later
331 if (((d < keyFrames[i + 1].distFromPrev) && (tTo > 0)))
333 while ((d < keyFrames[i + 1].distFromPrev) && (tTo > 0))
335 tFrom += 100;
336 tTo -= 100;
338 if (d > 0)
340 float newX, newY, newZ;
341 newX = keyFrames[i].x + (keyFrames[i + 1].x - keyFrames[i].x) * d / keyFrames[i + 1].distFromPrev;
342 newY = keyFrames[i].y + (keyFrames[i + 1].y - keyFrames[i].y) * d / keyFrames[i + 1].distFromPrev;
343 newZ = keyFrames[i].z + (keyFrames[i + 1].z - keyFrames[i].z) * d / keyFrames[i + 1].distFromPrev;
345 bool teleport = false;
346 if (keyFrames[i].mapid != cM)
348 teleport = true;
349 cM = keyFrames[i].mapid;
352 // sLog.outString("T: %d, D: %f, x: %f, y: %f, z: %f", t, d, newX, newY, newZ);
353 WayPoint pos(keyFrames[i].mapid, newX, newY, newZ, teleport);
354 if (teleport)
355 m_WayPoints[t] = pos;
358 if (tFrom < tTo) // caught in tFrom dock's "gravitational pull"
360 if (tFrom <= 30000)
362 d = 0.5f * (tFrom / 1000) * (tFrom / 1000);
364 else
366 d = 0.5f * 30 * 30 + 30 * ((tFrom - 30000) / 1000);
368 d = d - keyFrames[i].distSinceStop;
370 else
372 if (tTo <= 30000)
374 d = 0.5f * (tTo / 1000) * (tTo / 1000);
376 else
378 d = 0.5f * 30 * 30 + 30 * ((tTo - 30000) / 1000);
380 d = keyFrames[i].distUntilStop - d;
382 t += 100;
384 t -= 100;
387 if (keyFrames[i + 1].tFrom > keyFrames[i + 1].tTo)
388 t += 100 - ((long)keyFrames[i + 1].tTo % 100);
389 else
390 t += (long)keyFrames[i + 1].tTo % 100;
392 bool teleport = false;
393 if ((keyFrames[i + 1].actionflag == 1) || (keyFrames[i + 1].mapid != keyFrames[i].mapid))
395 teleport = true;
396 cM = keyFrames[i + 1].mapid;
399 WayPoint pos(keyFrames[i + 1].mapid, keyFrames[i + 1].x, keyFrames[i + 1].y, keyFrames[i + 1].z, teleport);
401 // sLog.outString("T: %d, x: %f, y: %f, z: %f, t:%d", t, pos.x, pos.y, pos.z, teleport);
403 //if (teleport)
404 m_WayPoints[t] = pos;
406 t += keyFrames[i + 1].delay * 1000;
407 // sLog.outString("------");
410 uint32 timer = t;
412 // sLog.outDetail(" Generated %d waypoints, total time %u.", m_WayPoints.size(), timer);
414 m_curr = m_WayPoints.begin();
415 m_curr = GetNextWayPoint();
416 m_next = GetNextWayPoint();
417 m_pathTime = timer;
419 m_nextNodeTime = m_curr->first;
421 return true;
424 Transport::WayPointMap::iterator Transport::GetNextWayPoint()
426 WayPointMap::iterator iter = m_curr;
427 ++iter;
428 if (iter == m_WayPoints.end())
429 iter = m_WayPoints.begin();
430 return iter;
433 void Transport::TeleportTransport(uint32 newMapid, float x, float y, float z)
435 //GetMap()->Remove((GameObject *)this, false);
436 SetMapId(newMapid);
437 //MapManager::Instance().LoadGrid(newMapid,x,y,true);
438 Relocate(x, y, z);
439 //GetMap()->Add<GameObject>((GameObject *)this);
441 for(PlayerSet::iterator itr = m_passengers.begin(); itr != m_passengers.end();)
443 PlayerSet::iterator it2 = itr;
444 ++itr;
446 Player *plr = *it2;
447 if(!plr)
449 m_passengers.erase(it2);
450 continue;
453 if (plr->isDead() && !plr->HasFlag(PLAYER_FLAGS, PLAYER_FLAGS_GHOST))
455 plr->ResurrectPlayer(1.0);
457 plr->TeleportTo(newMapid, x, y, z, GetOrientation(), TELE_TO_NOT_LEAVE_TRANSPORT);
459 //WorldPacket data(SMSG_811, 4);
460 //data << uint32(0);
461 //plr->GetSession()->SendPacket(&data);
465 bool Transport::AddPassenger(Player* passenger)
467 if (m_passengers.find(passenger) == m_passengers.end())
469 sLog.outDetail("Player %s boarded transport %s.", passenger->GetName(), m_name.c_str());
470 m_passengers.insert(passenger);
472 return true;
475 bool Transport::RemovePassenger(Player* passenger)
477 if (m_passengers.erase(passenger))
478 sLog.outDetail("Player %s removed from transport %s.", passenger->GetName(), m_name.c_str());
479 return true;
482 void Transport::Update(uint32 /*p_time*/)
484 if (m_WayPoints.size() <= 1)
485 return;
487 m_timer = getMSTime() % m_period;
488 while (((m_timer - m_curr->first) % m_pathTime) > ((m_next->first - m_curr->first) % m_pathTime))
490 m_curr = GetNextWayPoint();
491 m_next = GetNextWayPoint();
493 // first check help in case client-server transport coordinates de-synchronization
494 if (m_curr->second.mapid != GetMapId() || m_curr->second.teleport)
496 TeleportTransport(m_curr->second.mapid, m_curr->second.x, m_curr->second.y, m_curr->second.z);
498 else
500 //MapManager::Instance().GetMap(m_curr->second.mapid)->GameobjectRelocation((GameObject *)this, m_curr->second.x, m_curr->second.y, m_curr->second.z, m_orientation);
501 Relocate(m_curr->second.x, m_curr->second.y, m_curr->second.z);
505 for(PlayerSet::iterator itr = m_passengers.begin(); itr != m_passengers.end();)
507 PlayerSet::iterator it2 = itr;
508 ++itr;
509 //(*it2)->SetPosition( m_curr->second.x + (*it2)->GetTransOffsetX(), m_curr->second.y + (*it2)->GetTransOffsetY(), m_curr->second.z + (*it2)->GetTransOffsetZ(), (*it2)->GetTransOffsetO() );
513 m_nextNodeTime = m_curr->first;
515 if (m_curr == m_WayPoints.begin() && (sLog.getLogFilter() & LOG_FILTER_TRANSPORT_MOVES)==0)
516 sLog.outDetail(" ************ BEGIN ************** %s", m_name.c_str());
518 // MapManager::Instance().GetMap(m_curr->second.mapid)->Add(&this); // -> // ->Add(t);
519 //MapManager::Instance().GetMap(m_curr->second.mapid)->Remove((GameObject *)this, false); // -> // ->Add(t);
520 //MapManager::Instance().GetMap(m_curr->second.mapid)->Add((GameObject *)this); // -> // ->Add(t);
522 if ((sLog.getLogFilter() & LOG_FILTER_TRANSPORT_MOVES)==0)
523 sLog.outDetail("%s moved to %f %f %f %d", m_name.c_str(), m_curr->second.x, m_curr->second.y, m_curr->second.z, m_curr->second.mapid);