2 * Copyright (C) 2005-2010 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
21 #include "Transports.h"
22 #include "MapManager.h"
23 #include "ObjectMgr.h"
24 #include "ObjectGuid.h"
27 #include "WorldPacket.h"
28 #include "DBCStores.h"
29 #include "ProgressBar.h"
31 void MapManager::LoadTransports()
33 QueryResult
*result
= WorldDatabase
.Query("SELECT entry, name, period FROM transports");
43 sLog
.outString( ">> Loaded %u transports", count
);
47 barGoLink
bar( (int)result
->GetRowCount() );
53 Transport
*t
= new Transport
;
55 Field
*fields
= result
->Fetch();
57 uint32 entry
= fields
[0].GetUInt32();
58 std::string name
= fields
[1].GetCppString();
59 t
->m_period
= fields
[2].GetUInt32();
61 const GameObjectInfo
*goinfo
= ObjectMgr::GetGameObjectInfo(entry
);
65 sLog
.outErrorDb("Transport ID:%u, Name: %s, will not be loaded, gameobject_template missing", entry
, name
.c_str());
70 if(goinfo
->type
!= GAMEOBJECT_TYPE_MO_TRANSPORT
)
72 sLog
.outErrorDb("Transport ID:%u, Name: %s, will not be loaded, gameobject_template type wrong", entry
, name
.c_str());
77 // sLog.outString("Loading transport %d between %s, %s", entry, name.c_str(), goinfo->name);
79 std::set
<uint32
> mapsUsed
;
81 if(!t
->GenerateWaypoints(goinfo
->moTransport
.taxiPathId
, mapsUsed
))
82 // skip transports with empty waypoints list
84 sLog
.outErrorDb("Transport (path id %u) path size = 0. Transport ignored, check DBC files or transport GO data0 field.",goinfo
->moTransport
.taxiPathId
);
91 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;
93 // creates the Gameobject
94 if(!t
->Create(entry
, mapid
, x
, y
, z
, o
, 100, 0))
100 m_Transports
.insert(t
);
102 for (std::set
<uint32
>::const_iterator i
= mapsUsed
.begin(); i
!= mapsUsed
.end(); ++i
)
103 m_TransportsByMap
[*i
].insert(t
);
105 //If we someday decide to use the grid to track transports, here:
106 t
->SetMap(sMapMgr
.CreateMap(mapid
, t
));
108 //t->GetMap()->Add<GameObject>((GameObject *)t);
110 } while(result
->NextRow());
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());
135 Transport::Transport() : GameObject()
137 m_updateFlag
= (UPDATEFLAG_TRANSPORT
| UPDATEFLAG_HIGHGUID
| UPDATEFLAG_HAS_POSITION
| UPDATEFLAG_ROTATION
);
140 bool Transport::Create(uint32 guidlow
, uint32 mapid
, float x
, float y
, float z
, float ang
, uint32 animprogress
, uint32 dynflags
)
143 // instance id and phaseMask isn't set to values different from std.
145 if(!IsPositionValid())
147 sLog
.outError("Transport (GUID: %u) not created. Suggested coordinates isn't valid (X: %f Y: %f)",
152 Object::_Create(guidlow
, 0, HIGHGUID_MO_TRANSPORT
);
154 GameObjectInfo
const* goinfo
= ObjectMgr::GetGameObjectInfo(guidlow
);
158 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
);
164 SetFloatValue(OBJECT_FIELD_SCALE_X
, goinfo
->size
);
166 SetUInt32Value(GAMEOBJECT_FACTION
, goinfo
->faction
);
167 //SetUInt32Value(GAMEOBJECT_FLAGS, goinfo->flags);
168 SetUInt32Value(GAMEOBJECT_FLAGS
, MAKE_PAIR32(0x28, 0x64));
169 SetUInt32Value(GAMEOBJECT_LEVEL
, m_period
);
170 SetEntry(goinfo
->id
);
172 SetUInt32Value(GAMEOBJECT_DISPLAYID
, goinfo
->displayId
);
174 SetGoState(GO_STATE_READY
);
175 SetGoType(GameobjectTypes(goinfo
->type
));
177 SetGoAnimProgress(animprogress
);
179 SetUInt32Value(GAMEOBJECT_DYNAMIC
, MAKE_PAIR32(0, dynflags
));
181 SetName(goinfo
->name
);
188 keyFrame(float _x
, float _y
, float _z
, uint32 _mapid
, int _actionflag
, int _delay
)
190 x
= _x
; y
= _y
; z
= _z
; mapid
= _mapid
; actionflag
= _actionflag
; delay
= _delay
; distFromPrev
= -1; distSinceStop
= -1; distUntilStop
= -1;
206 bool Transport::GenerateWaypoints(uint32 pathid
, std::set
<uint32
> &mapids
)
209 sObjectMgr
.GetTransportPathNodes(pathid
, path
);
214 std::vector
<keyFrame
> keyFrames
;
217 for (size_t i
= 1; i
< path
.Size() - 1; ++i
)
221 if ((path
[i
].mapid
== path
[i
+1].mapid
))
223 keyFrame
k(path
[i
].x
, path
[i
].y
, path
[i
].z
, path
[i
].mapid
, path
[i
].actionFlag
, path
[i
].delay
);
224 keyFrames
.push_back(k
);
225 mapids
.insert(k
.mapid
);
241 // first cell is arrived at by teleportation :S
242 keyFrames
[0].distFromPrev
= 0;
243 if (keyFrames
[0].actionflag
== 2)
248 // find the rest of the distances between key points
249 for (size_t i
= 1; i
< keyFrames
.size(); ++i
)
251 if ((keyFrames
[i
].actionflag
== 1) || (keyFrames
[i
].mapid
!= keyFrames
[i
-1].mapid
))
253 keyFrames
[i
].distFromPrev
= 0;
257 keyFrames
[i
].distFromPrev
=
258 sqrt(pow(keyFrames
[i
].x
- keyFrames
[i
- 1].x
, 2) +
259 pow(keyFrames
[i
].y
- keyFrames
[i
- 1].y
, 2) +
260 pow(keyFrames
[i
].z
- keyFrames
[i
- 1].z
, 2));
262 if (keyFrames
[i
].actionflag
== 2)
264 // remember first stop frame
272 for (size_t i
= 0; i
< keyFrames
.size(); ++i
)
274 int j
= (i
+ lastStop
) % keyFrames
.size();
275 if (keyFrames
[j
].actionflag
== 2)
278 tmpDist
+= keyFrames
[j
].distFromPrev
;
279 keyFrames
[j
].distSinceStop
= tmpDist
;
282 for (int i
= int(keyFrames
.size()) - 1; i
>= 0; i
--)
284 int j
= (i
+ (firstStop
+1)) % keyFrames
.size();
285 tmpDist
+= keyFrames
[(j
+ 1) % keyFrames
.size()].distFromPrev
;
286 keyFrames
[j
].distUntilStop
= tmpDist
;
287 if (keyFrames
[j
].actionflag
== 2)
291 for (size_t i
= 0; i
< keyFrames
.size(); ++i
)
293 if (keyFrames
[i
].distSinceStop
< (30 * 30 * 0.5f
))
294 keyFrames
[i
].tFrom
= sqrt(2 * keyFrames
[i
].distSinceStop
);
296 keyFrames
[i
].tFrom
= ((keyFrames
[i
].distSinceStop
- (30 * 30 * 0.5f
)) / 30) + 30;
298 if (keyFrames
[i
].distUntilStop
< (30 * 30 * 0.5f
))
299 keyFrames
[i
].tTo
= sqrt(2 * keyFrames
[i
].distUntilStop
);
301 keyFrames
[i
].tTo
= ((keyFrames
[i
].distUntilStop
- (30 * 30 * 0.5f
)) / 30) + 30;
303 keyFrames
[i
].tFrom
*= 1000;
304 keyFrames
[i
].tTo
*= 1000;
307 // for (int i = 0; i < keyFrames.size(); ++i) {
308 // 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);
311 // Now we're completely set up; we can move along the length of each waypoint at 100 ms intervals
312 // speed = max(30, t) (remember x = 0.5s^2, and when accelerating, a = 1 unit/s^2
314 bool teleport
= false;
315 if (keyFrames
[keyFrames
.size() - 1].mapid
!= keyFrames
[0].mapid
)
318 WayPoint
pos(keyFrames
[0].mapid
, keyFrames
[0].x
, keyFrames
[0].y
, keyFrames
[0].z
, teleport
);
319 m_WayPoints
[0] = pos
;
320 t
+= keyFrames
[0].delay
* 1000;
322 uint32 cM
= keyFrames
[0].mapid
;
323 for (size_t i
= 0; i
< keyFrames
.size() - 1; ++i
)
326 float tFrom
= keyFrames
[i
].tFrom
;
327 float tTo
= keyFrames
[i
].tTo
;
329 // keep the generation of all these points; we use only a few now, but may need the others later
330 if (((d
< keyFrames
[i
+ 1].distFromPrev
) && (tTo
> 0)))
332 while ((d
< keyFrames
[i
+ 1].distFromPrev
) && (tTo
> 0))
339 float newX
, newY
, newZ
;
340 newX
= keyFrames
[i
].x
+ (keyFrames
[i
+ 1].x
- keyFrames
[i
].x
) * d
/ keyFrames
[i
+ 1].distFromPrev
;
341 newY
= keyFrames
[i
].y
+ (keyFrames
[i
+ 1].y
- keyFrames
[i
].y
) * d
/ keyFrames
[i
+ 1].distFromPrev
;
342 newZ
= keyFrames
[i
].z
+ (keyFrames
[i
+ 1].z
- keyFrames
[i
].z
) * d
/ keyFrames
[i
+ 1].distFromPrev
;
344 bool teleport
= false;
345 if (keyFrames
[i
].mapid
!= cM
)
348 cM
= keyFrames
[i
].mapid
;
351 // sLog.outString("T: %d, D: %f, x: %f, y: %f, z: %f", t, d, newX, newY, newZ);
352 WayPoint
pos(keyFrames
[i
].mapid
, newX
, newY
, newZ
, teleport
);
354 m_WayPoints
[t
] = pos
;
357 if (tFrom
< tTo
) // caught in tFrom dock's "gravitational pull"
361 d
= 0.5f
* (tFrom
/ 1000) * (tFrom
/ 1000);
365 d
= 0.5f
* 30 * 30 + 30 * ((tFrom
- 30000) / 1000);
367 d
= d
- keyFrames
[i
].distSinceStop
;
373 d
= 0.5f
* (tTo
/ 1000) * (tTo
/ 1000);
377 d
= 0.5f
* 30 * 30 + 30 * ((tTo
- 30000) / 1000);
379 d
= keyFrames
[i
].distUntilStop
- d
;
386 if (keyFrames
[i
+ 1].tFrom
> keyFrames
[i
+ 1].tTo
)
387 t
+= 100 - ((long)keyFrames
[i
+ 1].tTo
% 100);
389 t
+= (long)keyFrames
[i
+ 1].tTo
% 100;
391 bool teleport
= false;
392 if ((keyFrames
[i
+ 1].actionflag
== 1) || (keyFrames
[i
+ 1].mapid
!= keyFrames
[i
].mapid
))
395 cM
= keyFrames
[i
+ 1].mapid
;
398 WayPoint
pos(keyFrames
[i
+ 1].mapid
, keyFrames
[i
+ 1].x
, keyFrames
[i
+ 1].y
, keyFrames
[i
+ 1].z
, teleport
);
400 // sLog.outString("T: %d, x: %f, y: %f, z: %f, t:%d", t, pos.x, pos.y, pos.z, teleport);
403 m_WayPoints
[t
] = pos
;
405 t
+= keyFrames
[i
+ 1].delay
* 1000;
406 // sLog.outString("------");
411 // sLog.outDetail(" Generated %lu waypoints, total time %u.", (unsigned long)m_WayPoints.size(), timer);
413 m_curr
= m_WayPoints
.begin();
414 m_curr
= GetNextWayPoint();
415 m_next
= GetNextWayPoint();
418 m_nextNodeTime
= m_curr
->first
;
423 Transport::WayPointMap::const_iterator
Transport::GetNextWayPoint()
425 WayPointMap::const_iterator iter
= m_curr
;
427 if (iter
== m_WayPoints
.end())
428 iter
= m_WayPoints
.begin();
432 void Transport::TeleportTransport(uint32 newMapid
, float x
, float y
, float z
)
434 Map
const* oldMap
= GetMap();
437 for(PlayerSet::iterator itr
= m_passengers
.begin(); itr
!= m_passengers
.end();)
439 PlayerSet::iterator it2
= itr
;
445 m_passengers
.erase(it2
);
449 if (plr
->isDead() && !plr
->HasFlag(PLAYER_FLAGS
, PLAYER_FLAGS_GHOST
))
451 plr
->ResurrectPlayer(1.0);
453 plr
->TeleportTo(newMapid
, x
, y
, z
, GetOrientation(), TELE_TO_NOT_LEAVE_TRANSPORT
);
455 //WorldPacket data(SMSG_811, 4);
457 //plr->GetSession()->SendPacket(&data);
460 //we need to create and save new Map object with 'newMapid' because if not done -> lead to invalid Map object reference...
461 //player far teleport would try to create same instance, but we need it NOW for transport...
462 //correct me if I'm wrong O.o
463 Map
* newMap
= sMapMgr
.CreateMap(newMapid
, this);
468 UpdateForMap(oldMap
);
469 UpdateForMap(newMap
);
473 bool Transport::AddPassenger(Player
* passenger
)
475 if (m_passengers
.find(passenger
) == m_passengers
.end())
477 sLog
.outDetail("Player %s boarded transport %s.", passenger
->GetName(), GetName());
478 m_passengers
.insert(passenger
);
483 bool Transport::RemovePassenger(Player
* passenger
)
485 if (m_passengers
.erase(passenger
))
486 sLog
.outDetail("Player %s removed from transport %s.", passenger
->GetName(), GetName());
490 void Transport::Update(time_t /*p_time*/)
492 if (m_WayPoints
.size() <= 1)
495 m_timer
= getMSTime() % m_period
;
496 while (((m_timer
- m_curr
->first
) % m_pathTime
) > ((m_next
->first
- m_curr
->first
) % m_pathTime
))
498 m_curr
= GetNextWayPoint();
499 m_next
= GetNextWayPoint();
501 // first check help in case client-server transport coordinates de-synchronization
502 if (m_curr
->second
.mapid
!= GetMapId() || m_curr
->second
.teleport
)
504 TeleportTransport(m_curr
->second
.mapid
, m_curr
->second
.x
, m_curr
->second
.y
, m_curr
->second
.z
);
508 Relocate(m_curr
->second
.x
, m_curr
->second
.y
, m_curr
->second
.z
);
512 for(PlayerSet::const_iterator itr = m_passengers.begin(); itr != m_passengers.end();)
514 PlayerSet::const_iterator it2 = itr;
516 //(*it2)->SetPosition( m_curr->second.x + (*it2)->GetTransOffsetX(), m_curr->second.y + (*it2)->GetTransOffsetY(), m_curr->second.z + (*it2)->GetTransOffsetZ(), (*it2)->GetTransOffsetO() );
520 m_nextNodeTime
= m_curr
->first
;
522 if (m_curr
== m_WayPoints
.begin() && (sLog
.getLogFilter() & LOG_FILTER_TRANSPORT_MOVES
)==0)
523 sLog
.outDetail(" ************ BEGIN ************** %s", GetName());
525 if ((sLog
.getLogFilter() & LOG_FILTER_TRANSPORT_MOVES
)==0)
526 sLog
.outDetail("%s moved to %f %f %f %d", GetName(), m_curr
->second
.x
, m_curr
->second
.y
, m_curr
->second
.z
, m_curr
->second
.mapid
);
530 void Transport::UpdateForMap(Map
const* targetMap
)
532 Map::PlayerList
const& pl
= targetMap
->GetPlayers();
536 if(GetMapId()==targetMap
->GetId())
538 for(Map::PlayerList::const_iterator itr
= pl
.begin(); itr
!= pl
.end(); ++itr
)
540 if(this != itr
->getSource()->GetTransport())
542 UpdateData transData
;
543 BuildCreateUpdateBlockForPlayer(&transData
, itr
->getSource());
545 transData
.BuildPacket(&packet
);
546 itr
->getSource()->SendDirectMessage(&packet
);
552 UpdateData transData
;
553 BuildOutOfRangeUpdateBlock(&transData
);
554 WorldPacket out_packet
;
555 transData
.BuildPacket(&out_packet
);
557 for(Map::PlayerList::const_iterator itr
= pl
.begin(); itr
!= pl
.end(); ++itr
)
558 if(this != itr
->getSource()->GetTransport())
559 itr
->getSource()->SendDirectMessage(&out_packet
);