-
Notifications
You must be signed in to change notification settings - Fork 35
/
Transport.cpp
902 lines (745 loc) · 30.4 KB
/
Transport.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
/*
* Copyright (C) 2008-2013 Trinity <http://www.trinitycore.org/>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Updated by: Toba and Baeumchen (maddin)
*/
#include "Common.h"
#include "Transport.h"
#include "MapManager.h"
#include "ObjectMgr.h"
#include "Path.h"
#include "ScriptMgr.h"
#include "WorldPacket.h"
#include "DBCStores.h"
#include "World.h"
#include "GameObjectAI.h"
#include "Player.h"
#include "Vehicle.h"
Transport* MapManager::LoadTransportInMap(Map* instance, uint32 goEntry, uint32 period)
{
const GameObjectTemplate* goInfo = sObjectMgr->GetGameObjectTemplate(goEntry);
if (!goInfo)
{
return NULL;
}
if (goInfo->type != GAMEOBJECT_TYPE_MO_TRANSPORT)
{
return NULL;
}
Transport* t = new Transport(period, goInfo->ScriptId);
std::set<uint32> mapsUsed;
if (!t->GenerateWaypoints(goInfo->moTransport.taxiPathId, mapsUsed))
{
delete t;
return NULL;
}
uint32 transportLowGuid = sObjectMgr->GenerateLowGuid(HIGHGUID_MO_TRANSPORT);
if (!t->Create(transportLowGuid, goEntry, t->m_WayPoints[0].mapid, t->m_WayPoints[0].x, t->m_WayPoints[0].y, t->m_WayPoints[0].z-10, 0.0f, 0, 0))
{
delete t;
return NULL;
}
m_Transports.insert(t);
m_TransportsByInstanceIdMap[instance->GetInstanceId()].insert(t);
t->SetMap(instance);
t->AddToWorld();
return t;
}
void MapManager::UnLoadTransportFromMap(Transport* t)
{
Map* map = t->GetMap();
for (Transport::CreatureSet::iterator itr = t->m_NPCPassengerSet.begin(); itr != t->m_NPCPassengerSet.end();)
{
if (Creature* npc = *itr)
{
npc->SetTransport(NULL);
npc->setActive(false);
npc->RemoveFromWorld();
}
++itr;
}
UpdateData transData;
t->BuildOutOfRangeUpdateBlock(&transData);
WorldPacket out_packet;
transData.BuildPacket(&out_packet);
for (Map::PlayerList::const_iterator itr = map->GetPlayers().begin(); itr != map->GetPlayers().end(); ++itr)
if (t != itr->getSource()->GetTransport())
itr->getSource()->SendDirectMessage(&out_packet);
t->m_NPCPassengerSet.clear();
m_TransportsByInstanceIdMap[t->GetInstanceId()].erase(t);
m_Transports.erase(t);
t->m_WayPoints.clear();
t->RemoveFromWorld();
}
void MapManager::LoadTransportForPlayers(Player* player)
{
MapManager::TransportMap& tmap = sMapMgr->m_TransportsByInstanceIdMap;
UpdateData transData;
MapManager::TransportSet& tset = tmap[player->GetInstanceId()];
for (MapManager::TransportSet::const_iterator i = tset.begin(); i != tset.end(); ++i)
{
(*i)->BuildCreateUpdateBlockForPlayer(&transData, player);
}
WorldPacket packet;
transData.BuildPacket(&packet);
player->SendDirectMessage(&packet);
}
void MapManager::UnLoadTransportForPlayers(Player* player)
{
MapManager::TransportMap& tmap = sMapMgr->m_TransportsByInstanceIdMap;
UpdateData transData;
MapManager::TransportSet& tset = tmap[player->GetInstanceId()];
for (MapManager::TransportSet::const_iterator i = tset.begin(); i != tset.end(); ++i)
{
for (Transport::CreatureSet::iterator itr = (*i)->m_NPCPassengerSet.begin(); itr != (*i)->m_NPCPassengerSet.end();)
{
if (Creature* npc = *itr)
{
npc->SetTransport(NULL);
npc->setActive(false);
npc->RemoveFromWorld();
}
++itr;
}
(*i)->BuildOutOfRangeUpdateBlock(&transData);
}
WorldPacket packet;
transData.BuildPacket(&packet);
player->SendDirectMessage(&packet);
}
void MapManager::LoadTransports()
{
uint32 oldMSTime = getMSTime();
QueryResult result = WorldDatabase.Query("SELECT guid, entry, name, period, ScriptName FROM transports");
if (!result)
{
sLog->outInfo(LOG_FILTER_SERVER_LOADING, ">> Loaded 0 transports. DB table `transports` is empty!");
return;
}
uint32 count = 0;
do
{
Field* fields = result->Fetch();
uint32 lowguid = fields[0].GetUInt32();
uint32 entry = fields[1].GetUInt32();
std::string name = fields[2].GetString();
uint32 period = fields[3].GetUInt32();
uint32 scriptId = sObjectMgr->GetScriptId(fields[4].GetCString());
GameObjectTemplate const* goinfo = sObjectMgr->GetGameObjectTemplate(entry);
if (!goinfo)
{
sLog->outError(LOG_FILTER_SQL, "Transport ID:%u, Name: %s, will not be loaded, gameobject_template missing", entry, name.c_str());
continue;
}
if (goinfo->type != GAMEOBJECT_TYPE_MO_TRANSPORT)
{
sLog->outError(LOG_FILTER_SQL, "Transport ID:%u, Name: %s, will not be loaded, gameobject_template type wrong", entry, name.c_str());
continue;
}
// sLog->outInfo(LOG_FILTER_SERVER_LOADING, "Loading transport %d between %s, %s", entry, name.c_str(), goinfo->name);
std::set<uint32> mapsUsed;
Transport* t = new Transport(period, scriptId);
if (!t->GenerateWaypoints(goinfo->moTransport.taxiPathId, mapsUsed))
// skip transports with empty waypoints list
{
sLog->outError(LOG_FILTER_SQL, "Transport (path id %u) path size = 0. Transport ignored, check DBC files or transport GO data0 field.", goinfo->moTransport.taxiPathId);
delete t;
continue;
}
float x = t->m_WayPoints[0].x;
float y = t->m_WayPoints[0].y;
float z = t->m_WayPoints[0].z;
uint32 mapid = t->m_WayPoints[0].mapid;
float o = 1.0f;
// creates the Gameobject -- Gunship
if (!t->Create(lowguid, entry, mapid, x, y, z, o, 100, 0))
{
delete t;
continue;
}
m_Transports.insert(t);
for (std::set<uint32>::const_iterator i = mapsUsed.begin(); i != mapsUsed.end(); ++i)
m_TransportsByMap[*i].insert(t);
//If we someday decide to use the grid to track transports, here:
t->SetMap(sMapMgr->CreateBaseMap(mapid));
t->AddToWorld();
++count;
}
while (result->NextRow());
// check transport data DB integrity
result = WorldDatabase.Query("SELECT gameobject.guid, gameobject.id, transports.name FROM gameobject, transports WHERE gameobject.id = transports.entry");
if (result) // wrong data found
{
do
{
Field* fields = result->Fetch();
uint32 guid = fields[0].GetUInt32();
uint32 entry = fields[1].GetUInt32();
std::string name = fields[2].GetString();
sLog->outError(LOG_FILTER_SQL, "Transport %u '%s' have record (GUID: %u) in `gameobject`. Transports must not have any records in `gameobject` or its behavior will be unpredictable/bugged.", entry, name.c_str(), guid);
}
while (result->NextRow());
}
sLog->outInfo(LOG_FILTER_SERVER_LOADING, ">> Loaded %u transports in %u ms", count, GetMSTimeDiffToNow(oldMSTime));
}
void MapManager::LoadTransportNPCs()
{
uint32 oldMSTime = getMSTime();
// 0 1 2 3 4 5 6 7
QueryResult result = WorldDatabase.Query("SELECT guid, npc_entry, transport_entry, TransOffsetX, TransOffsetY, TransOffsetZ, TransOffsetO, emote FROM creature_transport");
if (!result)
{
sLog->outInfo(LOG_FILTER_SERVER_LOADING, ">> Loaded 0 transport NPCs. DB table `creature_transport` is empty!");
return;
}
uint32 count = 0;
do
{
Field* fields = result->Fetch();
uint32 guid = fields[0].GetInt32();
uint32 entry = fields[1].GetInt32();
uint32 transportEntry = fields[2].GetInt32();
float tX = fields[3].GetFloat();
float tY = fields[4].GetFloat();
float tZ = fields[5].GetFloat();
float tO = fields[6].GetFloat();
uint32 anim = fields[7].GetInt32();
for (MapManager::TransportSet::iterator itr = m_Transports.begin(); itr != m_Transports.end(); ++itr)
{
if ((*itr)->GetEntry() == transportEntry)
{
(*itr)->AddNPCPassenger(guid, entry, tX, tY, tZ, tO, anim);
break;
}
}
++count;
}
while (result->NextRow());
sLog->outInfo(LOG_FILTER_SERVER_LOADING, ">> Loaded %u transport npcs in %u ms", count, GetMSTimeDiffToNow(oldMSTime));
}
Transport::Transport(uint32 period, uint32 script) : GameObject(), m_pathTime(0), m_timer(0),
currenttguid(0), m_period(period), ScriptId(script), m_nextNodeTime(0)
{
m_updateFlag = (UPDATEFLAG_TRANSPORT | UPDATEFLAG_LOWGUID | UPDATEFLAG_STATIONARY_POSITION | UPDATEFLAG_ROTATION);
}
Transport::~Transport()
{
for (CreatureSet::iterator itr = m_NPCPassengerSet.begin(); itr != m_NPCPassengerSet.end(); ++itr)
{
(*itr)->SetTransport(NULL);
GetMap()->AddObjectToRemoveList(*itr);
}
}
bool Transport::Create(uint32 guidlow, uint32 entry, uint32 mapid, float x, float y, float z, float ang, uint32 animprogress, uint32 dynflags)
{
Relocate(x, y, z, ang);
// instance id and phaseMask isn't set to values different from std.
if (!IsPositionValid())
{
sLog->outError(LOG_FILTER_TRANSPORTS, "Transport (GUID: %u) not created. Suggested coordinates isn't valid (X: %f Y: %f)",
guidlow, x, y);
return false;
}
Object::_Create(guidlow, 0, HIGHGUID_MO_TRANSPORT);
GameObjectTemplate const* goinfo = sObjectMgr->GetGameObjectTemplate(entry);
if (!goinfo)
{
sLog->outError(LOG_FILTER_SQL, "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);
return false;
}
m_goInfo = goinfo;
SetObjectScale(goinfo->size);
SetUInt32Value(GAMEOBJECT_FACTION, goinfo->faction);
//SetUInt32Value(GAMEOBJECT_FLAGS, goinfo->flags); -- gunship
SetUInt32Value(GAMEOBJECT_FLAGS, MAKE_PAIR32(0x28, 0x64));
SetUInt32Value(GAMEOBJECT_LEVEL, m_period);
SetEntry(goinfo->entry);
SetDisplayId(goinfo->displayId);
SetGoState(GO_STATE_READY);
SetGoType(GameobjectTypes(goinfo->type));
SetGoAnimProgress(animprogress);
if (dynflags)
SetUInt32Value(GAMEOBJECT_DYNAMIC, MAKE_PAIR32(0, dynflags));
SetName(goinfo->name);
SetZoneScript();
return true;
}
struct keyFrame
{
explicit keyFrame(TaxiPathNodeEntry const& _node) : node(&_node),
distSinceStop(-1.0f), distUntilStop(-1.0f), distFromPrev(-1.0f), tFrom(0.0f), tTo(0.0f)
{
}
TaxiPathNodeEntry const* node;
float distSinceStop;
float distUntilStop;
float distFromPrev;
float tFrom, tTo;
};
bool Transport::GenerateWaypoints(uint32 pathid, std::set<uint32> &mapids)
{
if (pathid >= sTaxiPathNodesByPath.size())
return false;
TaxiPathNodeList const& path = sTaxiPathNodesByPath[pathid];
std::vector<keyFrame> keyFrames;
int mapChange = 0;
mapids.clear();
for (size_t i = 1; i < path.size() - 1; ++i)
{
if (mapChange == 0)
{
TaxiPathNodeEntry const& node_i = path[i];
if (node_i.mapid == path[i+1].mapid)
{
keyFrame k(node_i);
keyFrames.push_back(k);
mapids.insert(k.node->mapid);
}
else
{
mapChange = 1;
}
}
else
{
--mapChange;
}
}
int lastStop = -1;
int firstStop = -1;
// first cell is arrived at by teleportation :S
keyFrames[0].distFromPrev = 0;
if (keyFrames[0].node->actionFlag == 2)
{
lastStop = 0;
}
// find the rest of the distances between key points
for (size_t i = 1; i < keyFrames.size(); ++i)
{
if ((keyFrames[i].node->actionFlag == 1) || (keyFrames[i].node->mapid != keyFrames[i-1].node->mapid))
{
keyFrames[i].distFromPrev = 0;
}
else
{
keyFrames[i].distFromPrev =
sqrt(pow(keyFrames[i].node->x - keyFrames[i - 1].node->x, 2) +
pow(keyFrames[i].node->y - keyFrames[i - 1].node->y, 2) +
pow(keyFrames[i].node->z - keyFrames[i - 1].node->z, 2));
}
if (keyFrames[i].node->actionFlag == 2)
{
// remember first stop frame
if (firstStop == -1)
firstStop = i;
lastStop = i;
}
}
float tmpDist = 0;
for (size_t i = 0; i < keyFrames.size(); ++i)
{
int j = (i + lastStop) % keyFrames.size();
if (keyFrames[j].node->actionFlag == 2)
tmpDist = 0;
else
tmpDist += keyFrames[j].distFromPrev;
keyFrames[j].distSinceStop = tmpDist;
}
for (int i = int(keyFrames.size()) - 1; i >= 0; i--)
{
int j = (i + (firstStop+1)) % keyFrames.size();
tmpDist += keyFrames[(j + 1) % keyFrames.size()].distFromPrev;
keyFrames[j].distUntilStop = tmpDist;
if (keyFrames[j].node->actionFlag == 2)
tmpDist = 0;
}
for (size_t i = 0; i < keyFrames.size(); ++i)
{
if (keyFrames[i].distSinceStop < (30 * 30 * 0.5f))
keyFrames[i].tFrom = sqrt(2 * keyFrames[i].distSinceStop);
else
keyFrames[i].tFrom = ((keyFrames[i].distSinceStop - (30 * 30 * 0.5f)) / 30) + 30;
if (keyFrames[i].distUntilStop < (30 * 30 * 0.5f))
keyFrames[i].tTo = sqrt(2 * keyFrames[i].distUntilStop);
else
keyFrames[i].tTo = ((keyFrames[i].distUntilStop - (30 * 30 * 0.5f)) / 30) + 30;
keyFrames[i].tFrom *= 1000;
keyFrames[i].tTo *= 1000;
}
// for (int i = 0; i < keyFrames.size(); ++i) {
// sLog->outInfo(LOG_FILTER_TRANSPORTS, "%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);
// }
// Now we're completely set up; we can move along the length of each waypoint at 100 ms intervals
// speed = max(30, t) (remember x = 0.5s^2, and when accelerating, a = 1 unit/s^2
int t = 0;
bool teleport = false;
if (keyFrames[keyFrames.size() - 1].node->mapid != keyFrames[0].node->mapid)
teleport = true;
m_WayPoints[0] = WayPoint(keyFrames[0].node->mapid, keyFrames[0].node->x, keyFrames[0].node->y, keyFrames[0].node->z, teleport, 0,
keyFrames[0].node->arrivalEventID, keyFrames[0].node->departureEventID);
t += keyFrames[0].node->delay * 1000;
uint32 cM = keyFrames[0].node->mapid;
for (size_t i = 0; i < keyFrames.size() - 1; ++i)
{
float d = 0;
float tFrom = keyFrames[i].tFrom;
float tTo = keyFrames[i].tTo;
// keep the generation of all these points; we use only a few now, but may need the others later
if (((d < keyFrames[i + 1].distFromPrev) && (tTo > 0)))
{
while ((d < keyFrames[i + 1].distFromPrev) && (tTo > 0))
{
tFrom += 100;
tTo -= 100;
if (d > 0)
{
float newX = keyFrames[i].node->x + (keyFrames[i + 1].node->x - keyFrames[i].node->x) * d / keyFrames[i + 1].distFromPrev;
float newY = keyFrames[i].node->y + (keyFrames[i + 1].node->y - keyFrames[i].node->y) * d / keyFrames[i + 1].distFromPrev;
float newZ = keyFrames[i].node->z + (keyFrames[i + 1].node->z - keyFrames[i].node->z) * d / keyFrames[i + 1].distFromPrev;
teleport = false;
if (keyFrames[i].node->mapid != cM)
{
teleport = true;
cM = keyFrames[i].node->mapid;
}
// sLog->outInfo(LOG_FILTER_TRANSPORTS, "T: %d, D: %f, x: %f, y: %f, z: %f", t, d, newX, newY, newZ);
if (teleport)
m_WayPoints[t] = WayPoint(keyFrames[i].node->mapid, newX, newY, newZ, teleport, 0);
}
if (tFrom < tTo) // caught in tFrom dock's "gravitational pull"
{
if (tFrom <= 30000)
{
d = 0.5f * (tFrom / 1000) * (tFrom / 1000);
}
else
{
d = 0.5f * 30 * 30 + 30 * ((tFrom - 30000) / 1000);
}
d = d - keyFrames[i].distSinceStop;
}
else
{
if (tTo <= 30000)
{
d = 0.5f * (tTo / 1000) * (tTo / 1000);
}
else
{
d = 0.5f * 30 * 30 + 30 * ((tTo - 30000) / 1000);
}
d = keyFrames[i].distUntilStop - d;
}
t += 100;
}
t -= 100;
}
if (keyFrames[i + 1].tFrom > keyFrames[i + 1].tTo)
t += 100 - ((long)keyFrames[i + 1].tTo % 100);
else
t += (long)keyFrames[i + 1].tTo % 100;
teleport = false;
if ((keyFrames[i + 1].node->actionFlag == 1) || (keyFrames[i + 1].node->mapid != keyFrames[i].node->mapid))
{
teleport = true;
cM = keyFrames[i + 1].node->mapid;
}
m_WayPoints[t] = WayPoint(keyFrames[i + 1].node->mapid, keyFrames[i + 1].node->x, keyFrames[i + 1].node->y, keyFrames[i + 1].node->z, teleport,
0, keyFrames[i + 1].node->arrivalEventID, keyFrames[i + 1].node->departureEventID);
// sLog->outInfo(LOG_FILTER_TRANSPORTS, "T: %d, x: %f, y: %f, z: %f, t:%d", t, pos.x, pos.y, pos.z, teleport);
t += keyFrames[i + 1].node->delay * 1000;
}
uint32 timer = t;
// sLog->outInfo(LOG_FILTER_TRANSPORTS, " Generated %lu waypoints, total time %u.", (unsigned long)m_WayPoints.size(), timer);
m_curr = m_WayPoints.begin();
m_next = GetNextWayPoint();
m_pathTime = timer;
m_nextNodeTime = m_curr->first;
return true;
}
Transport::WayPointMap::const_iterator Transport::GetNextWayPoint()
{
WayPointMap::const_iterator iter = m_curr;
++iter;
if (iter == m_WayPoints.end())
iter = m_WayPoints.begin();
return iter;
}
void Transport::TeleportTransport(uint32 newMapid, float x, float y, float z)
{
Map const* oldMap = GetMap();
Relocate(x, y, z);
for (PlayerSet::const_iterator itr = m_passengers.begin(); itr != m_passengers.end();)
{
Player* player = *itr;
++itr;
if (player->isDead() && !player->HasFlag(PLAYER_FLAGS, PLAYER_FLAGS_GHOST))
player->ResurrectPlayer(1.0f);
player->TeleportTo(newMapid, x, y, z, GetOrientation(), TELE_TO_NOT_LEAVE_TRANSPORT);
}
//we need to create and save new Map object with 'newMapid' because if not done -> lead to invalid Map object reference...
//player far teleport would try to create same instance, but we need it NOW for transport...
RemoveFromWorld();
ResetMap();
Map* newMap = sMapMgr->CreateBaseMap(newMapid);
SetMap(newMap);
ASSERT(GetMap());
AddToWorld();
if (oldMap != newMap)
{
UpdateForMap(oldMap);
UpdateForMap(newMap);
}
for (CreatureSet::iterator itr = m_NPCPassengerSet.begin(); itr != m_NPCPassengerSet.end(); ++itr)
(*itr)->FarTeleportTo(newMap, x, y, z, (*itr)->GetOrientation());
}
bool Transport::AddPassenger(Player* passenger)
{
if (m_passengers.insert(passenger).second)
sLog->outInfo(LOG_FILTER_TRANSPORTS, "Player %s boarded transport %s.", passenger->GetName().c_str(), GetName().c_str());
sScriptMgr->OnAddPassenger(this, passenger);
return true;
}
bool Transport::RemovePassenger(Player* passenger)
{
if (m_passengers.erase(passenger))
sLog->outInfo(LOG_FILTER_TRANSPORTS, "Player %s removed from transport %s.", passenger->GetName().c_str(), GetName().c_str());
sScriptMgr->OnRemovePassenger(this, passenger);
return true;
}
void Transport::Update(uint32 p_diff)
{
if (!AI())
{
if (!AIM_Initialize())
sLog->outError(LOG_FILTER_TRANSPORTS, "Could not initialize GameObjectAI for Transport");
} else
AI()->UpdateAI(p_diff);
if (m_WayPoints.size() <= 1)
return;
m_timer = getMSTime() % m_period;
while (((m_timer - m_curr->first) % m_pathTime) > ((m_next->first - m_curr->first) % m_pathTime))
{
DoEventIfAny(*m_curr, true);
m_curr = GetNextWayPoint();
m_next = GetNextWayPoint();
DoEventIfAny(*m_curr, false);
// first check help in case client-server transport coordinates de-synchronization
if (m_curr->second.mapid != GetMapId() || m_curr->second.teleport)
{
TeleportTransport(m_curr->second.mapid, m_curr->second.x, m_curr->second.y, m_curr->second.z);
}
else
{
Relocate(m_curr->second.x, m_curr->second.y, m_curr->second.z, GetAngle(m_next->second.x, m_next->second.y) + float(M_PI));
UpdatePassengerPositions(); // COME BACK MARKER
// This forces the server to update positions in transportation for players -- gunship
UpdatePlayerPositions();
}
sScriptMgr->OnRelocate(this, m_curr->first, m_curr->second.mapid, m_curr->second.x, m_curr->second.y, m_curr->second.z);
m_nextNodeTime = m_curr->first;
if (m_curr == m_WayPoints.begin())
sLog->outDebug(LOG_FILTER_TRANSPORTS, " ************ BEGIN ************** %s", m_name.c_str());
sLog->outDebug(LOG_FILTER_TRANSPORTS, "%s moved to %d %f %f %f %d", m_name.c_str(), m_curr->second.id, m_curr->second.x, m_curr->second.y, m_curr->second.z, m_curr->second.mapid);
}
sScriptMgr->OnTransportUpdate(this, p_diff);
}
void Transport::UpdateForMap(Map const* targetMap)
{
Map::PlayerList const& player = targetMap->GetPlayers();
if (player.isEmpty())
return;
if (GetMapId() == targetMap->GetId())
{
for (Map::PlayerList::const_iterator itr = player.begin(); itr != player.end(); ++itr)
{
if (this != itr->getSource()->GetTransport())
{
UpdateData transData;
BuildCreateUpdateBlockForPlayer(&transData, itr->getSource());
WorldPacket packet;
transData.BuildPacket(&packet);
itr->getSource()->SendDirectMessage(&packet);
}
}
}
else
{
UpdateData transData;
BuildOutOfRangeUpdateBlock(&transData);
WorldPacket out_packet;
transData.BuildPacket(&out_packet);
for (Map::PlayerList::const_iterator itr = player.begin(); itr != player.end(); ++itr)
if (this != itr->getSource()->GetTransport())
itr->getSource()->SendDirectMessage(&out_packet);
}
}
void Transport::DoEventIfAny(WayPointMap::value_type const& node, bool departure)
{
if (uint32 eventid = departure ? node.second.departureEventID : node.second.arrivalEventID)
{
sLog->outDebug(LOG_FILTER_MAPSCRIPTS, "Taxi %s event %u of node %u of %s path", departure ? "departure" : "arrival", eventid, node.first, GetName().c_str());
GetMap()->ScriptsStart(sEventScripts, eventid, this, this);
EventInform(eventid);
}
}
void Transport::BuildStartMovePacket(Map const* targetMap)
{
SetFlag(GAMEOBJECT_FLAGS, GO_FLAG_IN_USE);
SetGoState(GO_STATE_ACTIVE);
UpdateForMap(targetMap);
}
void Transport::BuildStopMovePacket(Map const* targetMap)
{
RemoveFlag(GAMEOBJECT_FLAGS, GO_FLAG_IN_USE);
SetGoState(GO_STATE_READY);
UpdateForMap(targetMap);
}
uint32 Transport::AddNPCPassenger(uint32 tguid, uint32 entry, float x, float y, float z, float o, uint32 anim)
{
Map* map = GetMap();
//make it world object so it will not be unloaded with grid
Creature* creature = new Creature(true);
if (!creature->Create(sObjectMgr->GenerateLowGuid(HIGHGUID_UNIT), map, GetPhaseMask(), entry, 0, GetGOInfo()->faction, 0, 0, 0, 0))
{
delete creature;
return 0;
}
creature->SetTransport(this);
creature->AddUnitMovementFlag(MOVEMENTFLAG_ONTRANSPORT);
creature->m_movementInfo.guid = GetGUID();
creature->m_movementInfo.t_pos.Relocate(x, y, z, o);
if (anim)
creature->SetUInt32Value(UNIT_NPC_EMOTESTATE, anim);
creature->Relocate(
GetPositionX() + (x * std::cos(GetOrientation()) + y * std::sin(GetOrientation() + float(M_PI))),
GetPositionY() + (y * std::cos(GetOrientation()) + x * std::sin(GetOrientation())),
z + GetPositionZ(),
o + GetOrientation());
creature->SetHomePosition(creature->GetPositionX(), creature->GetPositionY(), creature->GetPositionZ(), creature->GetOrientation());
creature->SetTransportHomePosition(creature->m_movementInfo.t_pos);
if (!creature->IsPositionValid())
{
sLog->outError(LOG_FILTER_TRANSPORTS, "Creature (guidlow %d, entry %d) not created. Suggested coordinates isn't valid (X: %f Y: %f)", creature->GetGUIDLow(), creature->GetEntry(), creature->GetPositionX(), creature->GetPositionY());
delete creature;
return 0;
}
map->AddToMap(creature);
m_NPCPassengerSet.insert(creature);
if (tguid == 0)
{
++currenttguid;
tguid = currenttguid;
}
else
currenttguid = std::max(tguid, currenttguid);
creature->setActive(true);
creature->SetGUIDTransport(tguid);
sScriptMgr->OnAddCreaturePassenger(this, creature);
return tguid;
}
// gunship data
Creature* Transport::AddNPCPassengerInInstance(uint32 entry, float x, float y, float z, float o, uint32 anim)
{
Map* map = GetMap();
Creature* creature = new Creature;
if (!creature->Create(sObjectMgr->GenerateLowGuid(HIGHGUID_UNIT), map, GetPhaseMask(), entry, 0, GetGOInfo()->faction, 0, 0, 0, 0))
{
delete creature;
return 0;
}
creature->SetTransport(this);
creature->AddUnitMovementFlag(MOVEMENTFLAG_ONTRANSPORT);
creature->m_movementInfo.guid = GetGUID();
creature->m_movementInfo.t_pos.Relocate(x, y, z, o);
creature->Relocate(
GetPositionX() + (x * cos(GetOrientation()) + y * sin(GetOrientation() + float(M_PI))),
GetPositionY() + (y * cos(GetOrientation()) + x * sin(GetOrientation())),
z + GetPositionZ(),
o + GetOrientation());
creature->SetHomePosition(creature->GetPositionX(), creature->GetPositionY(), creature->GetPositionZ(), creature->GetOrientation());
if (!creature->IsPositionValid())
{
delete creature;
return 0;
}
map->AddToMap(creature);
m_NPCPassengerSet.insert(creature);
creature->setActive(true);
sScriptMgr->OnAddCreaturePassenger(this, creature);
return creature;
}
void Transport::UpdatePosition(MovementInfo* mi)
{
float transport_o = mi->pos.GetOrientation() - mi->t_pos.GetOrientation();
float transport_x = mi->pos.m_positionX - (mi->t_pos.m_positionX * std::cos(transport_o) - mi->t_pos.m_positionY * std::sin(transport_o));
float transport_y = mi->pos.m_positionY - (mi->t_pos.m_positionY * std::cos(transport_o) + mi->t_pos.m_positionX * std::sin(transport_o));
float transport_z = mi->pos.m_positionZ - mi->t_pos.m_positionZ;
Relocate(transport_x, transport_y, transport_z, transport_o);
UpdatePassengerPositions();
UpdatePlayerPositions();
}
void Transport::UpdatePassengerPositions()
{
for (CreatureSet::iterator itr = m_NPCPassengerSet.begin(); itr != m_NPCPassengerSet.end(); ++itr)
{
Creature* npc = *itr;
float x, y, z, o;
npc->m_movementInfo.t_pos.GetPosition(x, y, z, o);
CalculatePassengerPosition(x, y, z, o);
GetMap()->CreatureRelocation(npc, x, y, z, o, false);
npc->GetTransportHomePosition(x, y, z, o);
CalculatePassengerPosition(x, y, z, o);
npc->SetHomePosition(x, y, z, o);
}
}
// gunship Data
void Transport::UpdatePlayerPositions()
{
for (PlayerSet::iterator itr = m_passengers.begin(); itr != m_passengers.end(); ++itr)
{
Player* plr = *itr;
float x, y, z, o;
o = GetOrientation() + plr->m_movementInfo.t_pos.m_orientation;
x = GetPositionX() + (plr->m_movementInfo.t_pos.m_positionX * cos(GetOrientation()) + plr->m_movementInfo.t_pos.m_positionY * sin(GetOrientation() + M_PI));
y = GetPositionY() + (plr->m_movementInfo.t_pos.m_positionY * cos(GetOrientation()) + plr->m_movementInfo.t_pos.m_positionX * sin(GetOrientation()));
z = GetPositionZ() + plr->m_movementInfo.t_pos.m_positionZ;
plr->Relocate(x, y, z, o);
UpdateData transData;
WorldPacket packet;
transData.BuildPacket(&packet);
plr->SendDirectMessage(&packet);
}
}
void Transport::CalculatePassengerPosition(float& x, float& y, float& z, float& o) const
{
float inx = x, iny = y, inz = z, ino = o;
o = GetOrientation() + ino;
x = GetPositionX() + inx * std::cos(GetOrientation()) - iny * std::sin(GetOrientation());
y = GetPositionY() + iny * std::cos(GetOrientation()) + inx * std::sin(GetOrientation());
z = GetPositionZ() + inz;
}
void Transport::CalculatePassengerOffset(float& x, float& y, float& z, float& o) const
{
o -= GetOrientation();
z -= GetPositionZ();
y -= GetPositionY(); // y = searchedY * std::cos(o) + searchedX * std::sin(o)
x -= GetPositionX(); // x = searchedX * std::cos(o) + searchedY * std::sin(o + pi)
float inx = x, iny = y;
y = (iny - inx * tan(GetOrientation())) / (cos(GetOrientation()) + std::sin(GetOrientation()) * tan(GetOrientation()));
x = (inx + iny * tan(GetOrientation())) / (cos(GetOrientation()) + std::sin(GetOrientation()) * tan(GetOrientation()));
}