-
Notifications
You must be signed in to change notification settings - Fork 304
/
GameObject.cpp
3062 lines (2565 loc) · 110 KB
/
GameObject.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
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the CMaNGOS Project. See AUTHORS file for Copyright information
*
* 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
*/
#include "Entities/GameObject.h"
#include "Quests/QuestDef.h"
#include "Globals/ObjectMgr.h"
#include "Pools/PoolManager.h"
#include "Spells/SpellMgr.h"
#include "Spells/Spell.h"
#include "Server/Opcodes.h"
#include "Server/WorldPacket.h"
#include "World/World.h"
#include "Database/DatabaseEnv.h"
#include "Loot/LootMgr.h"
#include "Grids/GridNotifiers.h"
#include "Grids/GridNotifiersImpl.h"
#include "Grids/CellImpl.h"
#include "Maps/InstanceData.h"
#include "Maps/MapManager.h"
#include "Maps/MapPersistentStateMgr.h"
#include "BattleGround/BattleGround.h"
#include "OutdoorPvP/OutdoorPvP.h"
#include "Util/Util.h"
#include "AI/ScriptDevAI/ScriptDevAIMgr.h"
#include "Vmap/GameObjectModel.h"
#include "Server/SQLStorages.h"
#include "World/WorldState.h"
#include <G3D/Box.h>
#include <G3D/CoordinateFrame.h>
#include <G3D/Quat.h>
#include "Entities/Transports.h"
bool QuaternionData::isUnit() const
{
return fabs(x * x + y * y + z * z + w * w - 1.0f) < 1e-5f;
}
void QuaternionData::toEulerAnglesZYX(float& Z, float& Y, float& X) const
{
G3D::Matrix3(G3D::Quat(x, y, z, w)).toEulerAnglesZYX(Z, Y, X);
}
QuaternionData QuaternionData::fromEulerAnglesZYX(float Z, float Y, float X)
{
G3D::Quat quat(G3D::Matrix3::fromEulerAnglesZYX(Z, Y, X));
return QuaternionData(quat.x, quat.y, quat.z, quat.w);
}
#include <G3D/Quat.h>
GameObject::GameObject() : WorldObject(),
m_model(nullptr),
m_captureSlider(0),
m_captureState(),
m_goInfo(nullptr),
m_displayInfo(nullptr),
m_AI(nullptr)
{
m_objectType |= TYPEMASK_GAMEOBJECT;
m_objectTypeId = TYPEID_GAMEOBJECT;
m_updateFlag = (UPDATEFLAG_HIGHGUID | UPDATEFLAG_HAS_POSITION | UPDATEFLAG_POSITION | UPDATEFLAG_ROTATION);
m_valuesCount = GAMEOBJECT_END;
m_respawnTime = 0;
m_respawnDelay = 25;
m_respawnOverriden = false;
m_respawnOverrideOnce = false;
m_forcedDespawn = false;
m_lootState = GO_READY;
m_spawnedByDefault = true;
m_useTimes = 0;
m_spellId = 0;
m_cooldownTime = 0;
m_captureTimer = 0;
m_packedRotation = 0;
m_lootGroupRecipientId = 0;
m_isInUse = false;
m_reStockTimer = 0;
m_rearmTimer = 0;
m_despawnTimer = TimePoint::max();
m_delayedActionTimer = 0;
m_goGroup = nullptr;
}
GameObject::~GameObject()
{
delete m_model;
}
GameObject* GameObject::CreateGameObject(uint32 entry)
{
GameObjectInfo const* goinfo = ObjectMgr::GetGameObjectInfo(entry);
if (goinfo && goinfo->type == GAMEOBJECT_TYPE_TRANSPORT)
return new ElevatorTransport;
return new GameObject;
}
void GameObject::AddToWorld()
{
///- Register the gameobject for guid lookup
if (!IsInWorld())
{
GetMap()->GetObjectsStore().insert<GameObject>(GetObjectGuid(), (GameObject*)this);
if (GetDbGuid())
GetMap()->AddDbGuidObject(this);
}
if (m_model)
GetMap()->InsertGameObjectModel(*m_model);
WorldObject::AddToWorld();
// After Object::AddToWorld so that for initial state the GO is added to the world (and hence handled correctly)
UpdateCollisionState();
if (IsSpawned()) // need to prevent linked trap addition due to Pool system Map::Add abuse
{
if (GameObject* linkedGO = SummonLinkedTrapIfAny())
SetLinkedTrap(linkedGO);
}
// Make active if required
if (GetGOInfo()->ExtraFlags & GAMEOBJECT_EXTRA_FLAG_ACTIVE)
SetActiveObjectState(true);
}
void GameObject::RemoveFromWorld()
{
///- Remove the gameobject from the accessor
if (IsInWorld())
{
// Notify the outdoor pvp script
if (OutdoorPvP* outdoorPvP = sOutdoorPvPMgr.GetScript(GetZoneId()))
outdoorPvP->HandleGameObjectRemove(this);
// Remove GO from owner
if (ObjectGuid owner_guid = GetOwnerGuid())
{
if (Unit* owner = ObjectAccessor::GetUnit(*this, owner_guid))
owner->RemoveGameObject(this, false);
else
{
sLog.outError("Delete %s with SpellId %u LinkedGO %u that lost references to owner %s GO list. Crash possible later.",
GetGuidStr().c_str(), m_spellId, GetGOInfo()->GetLinkedGameObjectEntry(), owner_guid.GetString().c_str());
}
}
if (m_model && GetMap()->ContainsGameObjectModel(*m_model))
GetMap()->RemoveGameObjectModel(*m_model);
GetMap()->GetObjectsStore().erase<GameObject>(GetObjectGuid(), (GameObject*)nullptr);
if (GetDbGuid())
GetMap()->RemoveDbGuidObject(this);
ClearGameObjectGroup();
}
WorldObject::RemoveFromWorld();
}
bool GameObject::Create(uint32 dbGuid, uint32 guidlow, uint32 name_id, Map* map, uint32 phaseMask, float x, float y, float z, float ang, const QuaternionData & rotation, uint8 animprogress, GOState goState)
{
MANGOS_ASSERT(map);
Relocate(x, y, z, ang);
SetMap(map);
SetPhaseMask(phaseMask, false);
m_stationaryPosition = Position(x, y, z, ang);
if (!IsPositionValid())
{
sLog.outError("Gameobject (GUID: %u Entry: %u ) not created. Suggested coordinates are invalid (X: %f Y: %f)", dbGuid, name_id, x, y);
return false;
}
GameObjectInfo const* goinfo = ObjectMgr::GetGameObjectInfo(name_id);
if (!goinfo)
{
sLog.outErrorDb("Gameobject (GUID: %u) not created: Entry %u does not exist in `gameobject_template`. Map: %u (X: %f Y: %f Z: %f) ang: %f", dbGuid, name_id, map->GetId(), x, y, z, ang);
return false;
}
Object::_Create(dbGuid, guidlow, goinfo->id, HIGHGUID_GAMEOBJECT);
m_goInfo = goinfo;
if (goinfo->type >= MAX_GAMEOBJECT_TYPE)
{
sLog.outErrorDb("Gameobject (GUID: %u) not created: Entry %u has invalid type %u in `gameobject_template`. It may crash client if created.", dbGuid, name_id, goinfo->type);
return false;
}
SetObjectScale(goinfo->size);
SetLocalRotation(rotation.x, rotation.y, rotation.z, rotation.w);
SetTransportPathRotation(QuaternionData(0, 0, 0, 1));
SetUInt32Value(GAMEOBJECT_FACTION, goinfo->faction);
SetUInt32Value(GAMEOBJECT_FLAGS, goinfo->flags);
if (goinfo->type == GAMEOBJECT_TYPE_TRANSPORT)
{
SetFlag(GAMEOBJECT_FLAGS, (GO_FLAG_TRANSPORT | GO_FLAG_NODESPAWN));
m_updateFlag = (m_updateFlag | UPDATEFLAG_TRANSPORT) & ~UPDATEFLAG_POSITION;
}
SetEntry(goinfo->id);
SetDisplayId(goinfo->displayId);
SetGoState(goState);
SetGoType(GameobjectTypes(goinfo->type));
SetGoArtKit(0); // unknown what this is
SetGoAnimProgress(animprogress);
switch (GetGoType())
{
case GAMEOBJECT_TYPE_DOOR:
case GAMEOBJECT_TYPE_BUTTON:
// safe to use door cos both have startOpen on same spot
SetGoState(goinfo->door.startOpen ? GO_STATE_ACTIVE : GO_STATE_READY);
if (goinfo->door.startOpen)
m_lootState = GO_ACTIVATED;
break;
case GAMEOBJECT_TYPE_TRAP:
// values from rogue detect traps aura
if (goinfo->trap.stealthed)
{
GetVisibilityData().SetStealthMask(STEALTH_TRAP, true);
GetVisibilityData().AddStealthStrength(STEALTH_TRAP, 70);
}
if (goinfo->trap.invisible)
{
GetVisibilityData().SetInvisibilityMask(INVISIBILITY_TRAP, true);
GetVisibilityData().AddInvisibilityValue(INVISIBILITY_TRAP, 300);
}
// [[fallthrough]]
case GAMEOBJECT_TYPE_FISHINGNODE:
m_lootState = GO_NOT_READY; // Initialize Traps and Fishingnode delayed in ::Update
break;
case GAMEOBJECT_TYPE_TRANSPORT:
SetUInt32Value(GAMEOBJECT_LEVEL, goinfo->transport.pause);
SetGoState(goinfo->transport.startOpen ? GO_STATE_ACTIVE : GO_STATE_READY);
SetGoAnimProgress(animprogress);
break;
case GAMEOBJECT_TYPE_GENERIC:
case GAMEOBJECT_TYPE_SPELL_FOCUS:
case GAMEOBJECT_TYPE_GOOBER:
case GAMEOBJECT_TYPE_CHEST:
SetUInt32Value(GAMEOBJECT_DYNAMIC, GO_DYNFLAG_LO_ACTIVATE | GO_DYNFLAG_LO_SPARKLE);
break;
case GAMEOBJECT_TYPE_QUESTGIVER:
SetUInt32Value(GAMEOBJECT_DYNAMIC, GO_DYNFLAG_LO_ACTIVATE);
break;
case GAMEOBJECT_TYPE_DESTRUCTIBLE_BUILDING:
ForceGameObjectHealth(GetMaxHealth(), nullptr);
break;
case GAMEOBJECT_TYPE_AURA_GENERATOR:
SetGoState(goinfo->auraGenerator.startOpen ? GO_STATE_ACTIVE : GO_STATE_READY);
break;
default:
break;
}
if (goinfo->StringId)
SetStringId(goinfo->StringId, true);
// Notify the battleground or outdoor pvp script
if (map->IsBattleGroundOrArena())
((BattleGroundMap*)map)->GetBG()->HandleGameObjectCreate(this);
else if (OutdoorPvP* outdoorPvP = sOutdoorPvPMgr.GetScript(GetZoneId()))
outdoorPvP->HandleGameObjectCreate(this);
// Notify the map's instance data.
// Only works if you create the object in it, not if it is moves to that map.
// Normally non-players do not teleport to other maps.
if (InstanceData* iData = map->GetInstanceData())
iData->OnObjectCreate(this);
// Check if GameObject is Large, skip if map has same or better visibility (e.g. Battleground)
if (GetGOInfo()->IsLargeGameObject() && GetVisibilityData().GetVisibilityDistance() < VISIBILITY_DISTANCE_LARGE)
GetVisibilityData().SetVisibilityDistanceOverride(VisibilityDistanceType::Large);
return true;
}
void GameObject::Update(const uint32 diff)
{
if (GetObjectGuid().IsMOTransport())
{
//((Transport*)this)->Update(p_time);
return;
}
m_events.Update(diff);
switch (m_lootState)
{
case GO_NOT_READY:
{
switch (GetGoType())
{
case GAMEOBJECT_TYPE_TRAP: // Initialized delayed to be able to use GetOwner()
{
// Arming Time for GAMEOBJECT_TYPE_TRAP (6)
// Note: wotlk+ specific types of traps have a default charge time
if (GetGOInfo()->trap.charges == 2 && GetGOInfo()->trap.diameter == 0)
m_cooldownTime = time(nullptr) + 10;
else
{
Unit* owner = GetOwner();
if (owner && owner->IsInCombat())
m_cooldownTime = time(nullptr) + GetGOInfo()->trap.startDelay;
}
m_lootState = GO_READY;
break;
}
case GAMEOBJECT_TYPE_FISHINGNODE: // Keep not ready for some delay
{
// fishing code (bobber ready)
if (time(nullptr) > m_respawnTime - FISHING_BOBBER_READY_TIME)
{
// splash bobber (bobber ready now)
Unit* caster = GetOwner();
if (caster && caster->GetTypeId() == TYPEID_PLAYER)
{
SetGoState(GO_STATE_ACTIVE);
// SetUInt32Value(GAMEOBJECT_FLAGS, GO_FLAG_NODESPAWN);
SendForcedObjectUpdate();
SendGameObjectCustomAnim(GetObjectGuid());
}
m_lootState = GO_READY; // can be successfully open with some chance
}
break;
}
case GAMEOBJECT_TYPE_CHEST:
{
if (m_goInfo->chest.chestRestockTime)
{
if (m_reStockTimer != 0)
{
if (m_reStockTimer <= time(nullptr))
{
m_reStockTimer = 0;
m_lootState = GO_READY;
delete m_loot;
m_loot = nullptr;
ForceValuesUpdateAtIndex(GAMEOBJECT_DYNAMIC);
}
}
else
m_lootState = GO_READY;
return;
}
m_lootState = GO_READY;
}
default:
break;
}
break;
}
case GO_READY:
{
if (m_respawnTime > 0) // timer on
{
if (m_respawnTime <= time(nullptr)) // timer expired
{
m_respawnTime = 0;
ClearAllUsesData();
switch (GetGoType())
{
case GAMEOBJECT_TYPE_FISHINGNODE: // can't fish now
{
Unit* caster = GetOwner();
if (caster && caster->GetTypeId() == TYPEID_PLAYER)
{
caster->FinishSpell(CURRENT_CHANNELED_SPELL);
WorldPacket data(SMSG_FISH_NOT_HOOKED, 0);
((Player*)caster)->GetSession()->SendPacket(data);
}
// can be deleted
m_lootState = GO_JUST_DEACTIVATED;
return;
}
case GAMEOBJECT_TYPE_DOOR:
case GAMEOBJECT_TYPE_BUTTON:
// we need to open doors if they are closed (add there another condition if this code breaks some usage, but it need to be here for battlegrounds)
if (GetGoState() != GO_STATE_READY)
ResetDoorOrButton();
// flags in AB are type_button and we need to add them here so no break!
default:
if (!m_spawnedByDefault) // despawn timer
{
// can be despawned or destroyed
SetLootState(GO_JUST_DEACTIVATED);
// Remove Wild-Summoned GO on timer expire
if (!HasStaticDBSpawnData())
{
if (Unit* owner = GetOwner())
owner->RemoveGameObject(this, false);
Delete();
}
return;
}
// respawn timer
GetMap()->Add(this);
AIM_Initialize();
break;
}
}
}
if (IsSpawned())
{
// traps can have time and can not have
GameObjectInfo const* goInfo = GetGOInfo();
if (goInfo->type == GAMEOBJECT_TYPE_TRAP && GetGoState() == GO_STATE_READY) // traps
{
if (m_cooldownTime < time(nullptr))
{
// FIXME: this is activation radius (in different casting radius that must be selected from spell data)
// TODO: move activated state code (cast itself) to GO_ACTIVATED, in this place only check activating and set state
float radius = float(goInfo->trap.diameter) / 2.0f;
// behavior verified on classic
// TODO: needs more research
if (goInfo->GetLockId() == 12) // 21 objects currently (hunter traps), all with 5 or less for diameter -> use diameter as radius instead
radius = float(goInfo->trap.diameter);
bool valid = true;
if (!radius)
{
if (goInfo->trap.cooldown != 3) // cast in other case (at some triggering/linked go/etc explicit call)
valid = false;
else
{
if (m_respawnTime > 0)
valid = false;
else // battlegrounds gameobjects has data2 == 0 && data5 == 3
radius = float(goInfo->trap.cooldown);
}
}
if (valid)
{
// Should trap trigger?
Unit* target = nullptr; // pointer to appropriate target if found any
if (std::function<bool(Unit*)>* functor = sScriptDevAIMgr.OnTrapSearch(this))
{
MaNGOS::AnyUnitFulfillingConditionInRangeCheck u_check(this, *functor, radius);
MaNGOS::UnitSearcher<MaNGOS::AnyUnitFulfillingConditionInRangeCheck> checker(target, u_check);
Cell::VisitAllObjects(this, checker, radius);
}
else
{
switch (goInfo->trapCustom.triggerOn)
{
case 1: // friendly
{
MaNGOS::AnySpellAssistableUnitInObjectRangeCheck u_check(this, nullptr, radius);
MaNGOS::UnitSearcher<MaNGOS::AnySpellAssistableUnitInObjectRangeCheck> checker(target, u_check);
Cell::VisitAllObjects(this, checker, radius);
break;
}
case 2: // all
{
MaNGOS::AnyUnitInObjectRangeCheck u_check(this, radius);
MaNGOS::UnitSearcher<MaNGOS::AnyUnitInObjectRangeCheck> checker(target, u_check);
Cell::VisitAllObjects(this, checker, radius);
break;
}
default: // unfriendly
{
MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck u_check(this, radius);
MaNGOS::UnitSearcher<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck> checker(target, u_check);
Cell::VisitAllObjects(this, checker, radius);
break;
}
}
}
if (target && (!goInfo->trapCustom.triggerOn || !target->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_UNINTERACTIBLE))) // do not trigger on hostile traps if not selectable
Use(target);
}
else
{
// Note: wotlk+ traps which work as bombs cast the spell automatically
if (GetGOInfo()->trap.charges == 2 && GetGOInfo()->trap.diameter == 0)
{
if (Unit* owner = GetOwner())
Use(owner);
SetLootState(GO_JUST_DEACTIVATED);
break;
}
}
}
}
int32 max_charges = goInfo->GetCharges(); // Only check usable (positive) charges; 0 : no charge; -1 : infinite charges
if (max_charges > 0 && m_useTimes >= uint32(max_charges))
{
m_useTimes = 0;
SetLootState(GO_JUST_DEACTIVATED); // can be despawned or destroyed
}
}
break;
}
case GO_ACTIVATED:
{
switch (GetGoType())
{
case GAMEOBJECT_TYPE_DOOR:
case GAMEOBJECT_TYPE_BUTTON:
if (GetGOInfo()->GetAutoCloseTime() && (m_cooldownTime < time(nullptr)))
ResetDoorOrButton();
break;
case GAMEOBJECT_TYPE_CHEST:
if (m_loot)
{
if (m_despawnTimer <= GetMap()->GetCurrentClockTime())
m_lootState = GO_JUST_DEACTIVATED;
m_loot->Update();
}
break;
case GAMEOBJECT_TYPE_TRAP:
// Note: wotlk+ traps which work as bombs can be disarmed
if (GetGOInfo()->trap.charges == 2 && GetGOInfo()->trap.diameter == 0)
{
SetLootState(GO_JUST_DEACTIVATED);
break;
}
if (m_rearmTimer == 0)
{
m_rearmTimer = time(nullptr) + GetRespawnDelay();
SetGoState(GO_STATE_ACTIVE_ALTERNATIVE);
}
if (m_rearmTimer < time(nullptr))
{
SetGoState(GO_STATE_READY);
m_lootState = GO_READY;
m_rearmTimer = 0;
}
break;
case GAMEOBJECT_TYPE_GOOBER:
if (m_cooldownTime < time(nullptr))
{
RemoveFlag(GAMEOBJECT_FLAGS, GO_FLAG_IN_USE);
SetLootState(GO_JUST_DEACTIVATED);
m_cooldownTime = 0;
}
break;
case GAMEOBJECT_TYPE_CAPTURE_POINT:
m_captureTimer += diff;
if (m_captureTimer >= 5000)
{
TickCapturePoint();
m_captureTimer -= 5000;
}
break;
default:
break;
}
break;
}
case GO_JUST_DEACTIVATED:
{
sWorldState.HandleGameObjectRevertState(this);
// If nearby linked trap exists, despawn it
if (GameObject* linkedTrap = GetLinkedTrap())
{
linkedTrap->SetLootState(GO_JUST_DEACTIVATED);
linkedTrap->Delete();
}
bool preventDespawn = false;
switch (GetGoType())
{
case GAMEOBJECT_TYPE_TRAP:
if (m_events.GetEvents().size() > 0)
{
preventDespawn = true;
break;
}
break;
case GAMEOBJECT_TYPE_GOOBER:
// if gameobject should cast spell, then this, but some GOs (type = 10) should be destroyed
if (uint32 spellId = GetGOInfo()->goober.spellId)
{
for (auto m_UniqueUser : m_UniqueUsers)
{
if (Player* owner = GetMap()->GetPlayer(m_UniqueUser))
owner->CastSpell(owner, spellId, TRIGGERED_NONE, nullptr, nullptr, GetObjectGuid());
}
ClearAllUsesData();
}
SetGoState(GO_STATE_READY);
// research - 185861 needs to be able to despawn as well TODO: fixup
// any return here in case battleground traps
break;
case GAMEOBJECT_TYPE_CAPTURE_POINT:
// there are some capture points which are used as visual GOs; we allow these to be despawned
if (!GetGOInfo()->capturePoint.radius)
break;
// remove capturing players because slider wont be displayed if capture point is being locked
for (auto m_UniqueUser : m_UniqueUsers)
{
if (Player* owner = GetMap()->GetPlayer(m_UniqueUser))
owner->SendUpdateWorldState(GetGOInfo()->capturePoint.worldState1, WORLD_STATE_REMOVE);
}
m_UniqueUsers.clear();
SetLootState(GO_READY);
return; // SetLootState and return because go is treated as "burning flag" due to GetGoAnimProgress() being 100 and would be removed on the client
case GAMEOBJECT_TYPE_CHEST:
m_despawnTimer = TimePoint::max();
// consumable confirmed to override chest restock
if (!m_goInfo->chest.consumable && m_goInfo->chest.chestRestockTime)
{
m_reStockTimer = time(nullptr) + m_goInfo->chest.chestRestockTime;
SetLootState(GO_NOT_READY);
ForceValuesUpdateAtIndex(GAMEOBJECT_DYNAMIC);
return;
}
break;
default:
break;
}
if (preventDespawn) // mainly serves to prevent casting traps from despawning
break;
// Remove wild summoned after use
if (!HasStaticDBSpawnData() && (!GetSpellId() || GetGOInfo()->GetDespawnPossibility() || GetGOInfo()->IsDespawnAtAction() || m_forcedDespawn))
{
if (Unit* owner = GetOwner())
owner->RemoveGameObject(this, false);
Delete();
return;
}
// burning flags in some battlegrounds, if you find better condition, just add it
if (GetGOInfo()->IsDespawnAtAction() || GetGoAnimProgress() > 0)
{
SendObjectDeSpawnAnim(GetObjectGuid());
// reset flags
if (GetMap()->Instanceable())
{
// In Instances GO_FLAG_LOCKED, GO_FLAG_INTERACT_COND or GO_FLAG_NO_INTERACT are not changed
uint32 currentLockOrInteractFlags = GetUInt32Value(GAMEOBJECT_FLAGS) & (GO_FLAG_LOCKED | GO_FLAG_INTERACT_COND | GO_FLAG_NO_INTERACT);
SetUInt32Value(GAMEOBJECT_FLAGS, (GetGOInfo()->flags & ~(GO_FLAG_LOCKED | GO_FLAG_INTERACT_COND | GO_FLAG_NO_INTERACT)) | currentLockOrInteractFlags);
}
else
SetUInt32Value(GAMEOBJECT_FLAGS, GetGOInfo()->flags);
}
delete m_loot;
m_loot = nullptr;
SetLootRecipient(nullptr);
SetLootState(GO_READY);
// non-consumable chests and goobers should never despawn
if ((GetGoType() == GAMEOBJECT_TYPE_CHEST || GetGoType() == GAMEOBJECT_TYPE_GOOBER) && !GetGOInfo()->IsDespawnAtAction() && !m_forcedDespawn)
return;
if (!m_respawnDelay)
return;
m_forcedDespawn = false;
if (AI())
AI()->JustDespawned();
if (InstanceData* iData = GetMap()->GetInstanceData())
iData->OnObjectDespawn(this);
if (!m_respawnOverriden)
{
// since pool system can fail to roll unspawned object, this one can remain spawned, so must set respawn nevertheless
if (IsSpawnedByDefault())
if (GameObjectData const* data = sObjectMgr.GetGOData(GetObjectGuid().GetCounter()))
m_respawnDelay = data->GetRandomRespawnTime();
}
else if (m_respawnOverrideOnce)
m_respawnOverriden = false;
m_respawnTime = m_spawnedByDefault ? time(nullptr) + m_respawnDelay : 0;
// if option not set then object will be saved at grid unload
if (sWorld.getConfig(CONFIG_BOOL_SAVE_RESPAWN_TIME_IMMEDIATELY))
SaveRespawnTime();
if (IsUsingNewSpawningSystem()) // does not support pooling
{
m_respawnTime = std::numeric_limits<time_t>::max();
if (m_respawnDelay && !GetGameObjectGroup())
GetMap()->GetSpawnManager().AddGameObject(GetDbGuid());
if (m_respawnDelay || !m_spawnedByDefault || m_forcedDespawn)
AddObjectToRemoveList();
}
else
{
// if part of pool, let pool system schedule new spawn instead of just scheduling respawn
if (uint16 poolid = sPoolMgr.IsPartOfAPool<GameObject>(GetDbGuid()))
sPoolMgr.UpdatePool<GameObject>(*GetMap()->GetPersistentState(), poolid, GetDbGuid());
}
// can be not in world at pool despawn
if (IsInWorld())
UpdateObjectVisibility();
break;
}
}
if (m_delayedActionTimer)
{
if (m_delayedActionTimer <= diff)
{
m_delayedActionTimer = 0;
TriggerDelayedAction();
}
else
m_delayedActionTimer -= diff;
}
if (m_AI)
m_AI->UpdateAI(diff);
WorldObject::Update(diff);
}
void GameObject::Heartbeat()
{
if (AI())
AI()->OnHeartbeat();
}
void GameObject::SetChestDespawn()
{
m_despawnTimer = GetMap()->GetCurrentClockTime() + std::chrono::minutes(5);
}
void GameObject::Refresh()
{
// not refresh despawned not casted GO (despawned casted GO destroyed in all cases anyway)
if (m_respawnTime > 0 && m_spawnedByDefault)
return;
if (IsSpawned())
{
GetMap()->Add(this);
AIM_Initialize();
}
}
void GameObject::AddUniqueUse(Player* player)
{
AddUse();
if (!m_firstUser)
m_firstUser = player->GetObjectGuid();
m_UniqueUsers.insert(player->GetObjectGuid());
}
void GameObject::Delete()
{
SendObjectDeSpawnAnim(GetObjectGuid());
SetGoState(GO_STATE_READY);
SetUInt32Value(GAMEOBJECT_FLAGS, GetGOInfo()->flags);
if (AI())
AI()->JustDespawned();
if (InstanceData* iData = GetMap()->GetInstanceData())
iData->OnObjectDespawn(this);
if (uint16 poolid = sPoolMgr.IsPartOfAPool<GameObject>(GetDbGuid()))
sPoolMgr.UpdatePool<GameObject>(*GetMap()->GetPersistentState(), poolid, GetDbGuid());
else
AddObjectToRemoveList();
if (GameObject* linkedTrap = GetLinkedTrap())
{
linkedTrap->SetLootState(GO_JUST_DEACTIVATED);
linkedTrap->Delete();
}
}
void GameObject::SaveToDB() const
{
// this should only be used when the gameobject has already been loaded
// preferably after adding to map, because mapid may not be valid otherwise
GameObjectData const* data = sObjectMgr.GetGOData(GetDbGuid());
if (!data)
{
sLog.outError("GameObject::SaveToDB failed, cannot get gameobject data!");
return;
}
SaveToDB(GetMapId(), data->spawnMask, data->phaseMask);
}
void GameObject::SaveToDB(uint32 mapid, uint8 spawnMask, uint32 phaseMask) const
{
const GameObjectInfo* goI = GetGOInfo();
if (!goI)
return;
// update in loaded data (changing data only in this place)
GameObjectData& data = sObjectMgr.NewGOData(GetGUIDLow());
// data->guid = guid don't must be update at save
data.id = GetEntry();
data.mapid = mapid;
data.phaseMask = phaseMask;
data.posX = GetPositionX();
data.posY = GetPositionY();
data.posZ = GetPositionZ();
data.orientation = GetOrientation();
data.rotation.x = m_localRotation.x;
data.rotation.y = m_localRotation.y;
data.rotation.z = m_localRotation.z;
data.rotation.w = m_localRotation.w;
data.spawntimesecsmin = m_spawnedByDefault ? (int32)m_respawnDelay : -(int32)m_respawnDelay;
data.spawntimesecsmax = m_spawnedByDefault ? (int32)m_respawnDelay : -(int32)m_respawnDelay;
data.spawnMask = spawnMask;
// updated in DB
std::ostringstream ss;
ss << "INSERT INTO gameobject VALUES ( "
<< GetGUIDLow() << ", "
<< GetEntry() << ", "
<< mapid << ", "
<< uint32(spawnMask) << "," // cast to prevent save as symbol
<< uint16(GetPhaseMask()) << "," // prevent out of range error
<< GetPositionX() << ", "
<< GetPositionY() << ", "
<< GetPositionZ() << ", "
<< GetOrientation() << ", "
<< m_localRotation.x << ", "
<< m_localRotation.y << ", "
<< m_localRotation.z << ", "
<< m_localRotation.w << ", "
<< m_respawnDelay << ", "
<< m_respawnDelay << ")";
WorldDatabase.BeginTransaction();
WorldDatabase.PExecuteLog("DELETE FROM gameobject WHERE guid = '%u'", GetGUIDLow());
WorldDatabase.PExecuteLog("%s", ss.str().c_str());
WorldDatabase.CommitTransaction();
}
bool GameObject::LoadFromDB(uint32 dbGuid, Map* map, uint32 newGuid, uint32 forcedEntry, GenericTransport* transport)
{
GameObjectData const* data = sObjectMgr.GetGOData(dbGuid);
if (!data)
{
sLog.outErrorDb("Gameobject (GUID: %u) not found in table `gameobject`, can't load. ", dbGuid);
return false;
}
// Gameobject can be loaded already in map if grid has been unloaded while gameobject moves to another grid
if (map->GetGameObject(dbGuid))
return false;
uint32 entry = forcedEntry ? forcedEntry : data->id;
// uint32 map_id = data->mapid; // already used before call
uint32 phaseMask = data->phaseMask;
float x = data->posX;
float y = data->posY;
float z = data->posZ;
float ang = data->orientation;
if (transport)
transport->CalculatePassengerPosition(x, y, z, &ang);
uint32 animprogress = data->animprogress;
SpawnGroupEntry* groupEntry = map->GetMapDataContainer().GetSpawnGroupByGuid(dbGuid, TYPEID_GAMEOBJECT); // use dynguid by default \o/
GameObjectGroup* group = nullptr;
if (groupEntry)
{
group = static_cast<GameObjectGroup*>(map->GetSpawnManager().GetSpawnGroup(groupEntry->Id));
if (!entry)
entry = group->GetGuidEntry(dbGuid);
}
bool dynguid = false;
if (map->IsDynguidForced())
dynguid = true;
if (!dynguid)
{
GameObjectInfo const* goinfo = ObjectMgr::GetGameObjectInfo(entry);
if ((goinfo && (goinfo->ExtraFlags & GAMEOBJECT_EXTRA_FLAG_DYNGUID) != 0 || groupEntry) && dbGuid == newGuid)
dynguid = true;
}
if (dynguid || newGuid == 0)
newGuid = map->GenerateLocalLowGuid(HIGHGUID_GAMEOBJECT);
if (uint32 randomEntry = sObjectMgr.GetRandomGameObjectEntry(dbGuid))
entry = randomEntry;
if (!Create(dbGuid, newGuid, entry, map, phaseMask, x, y, z, ang, data->rotation, animprogress, GO_STATE_READY))
return false;
if (data->goState != -1)
SetGoState(GOState(data->goState));
SetTransportPathRotation(data->path_rotation);
if (group)
SetGameObjectGroup(group);
if (groupEntry && groupEntry->StringId)
SetStringId(groupEntry->StringId, true);
if (!GetGOInfo()->GetDespawnPossibility() && !GetGOInfo()->IsDespawnAtAction() && data->spawntimesecsmin >= 0)
{
SetFlag(GAMEOBJECT_FLAGS, GO_FLAG_NODESPAWN);
m_spawnedByDefault = true;
m_respawnDelay = 0;
m_respawnTime = 0;
}
else
{
if (data->spawntimesecsmin >= 0)
{
m_spawnedByDefault = true;
m_respawnDelay = data->GetRandomRespawnTime();
m_respawnTime = map->GetPersistentState()->GetGORespawnTime(GetDbGuid());
// ready to respawn
if (m_respawnTime && m_respawnTime <= time(nullptr))
{
m_respawnTime = 0;
map->GetPersistentState()->SaveGORespawnTime(GetDbGuid(), 0);
}
}
else
{
m_spawnedByDefault = false;
m_respawnDelay = -data->spawntimesecsmin;
m_respawnTime = 0;
}
}
map->Add(this);
AIM_Initialize();
if (transport)
{
m_movementInfo.SetTransportPos(Position(data->posX, data->posY, data->posZ, data->orientation));