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
20 #include "GameObject.h"
21 #include "UpdateMask.h"
23 #include "WorldPacket.h"
24 #include "WorldSession.h"
26 #include "ObjectAccessor.h"
27 #include "Database/DatabaseEnv.h"
28 #include "SpellAuras.h"
29 #include "MapManager.h"
30 #include "GridNotifiers.h"
32 #include "GridNotifiersImpl.h"
34 DynamicObject::DynamicObject() : WorldObject()
36 m_objectType
|= TYPEMASK_DYNAMICOBJECT
;
37 m_objectTypeId
= TYPEID_DYNAMICOBJECT
;
39 m_updateFlag
= (UPDATEFLAG_LOWGUID
| UPDATEFLAG_HIGHGUID
| UPDATEFLAG_HAS_POSITION
);
41 m_valuesCount
= DYNAMICOBJECT_END
;
44 void DynamicObject::AddToWorld()
46 ///- Register the dynamicObject for guid lookup
47 if(!IsInWorld()) ObjectAccessor::Instance().AddObject(this);
51 void DynamicObject::RemoveFromWorld()
53 ///- Remove the dynamicObject from the accessor
54 if(IsInWorld()) ObjectAccessor::Instance().RemoveObject(this);
55 Object::RemoveFromWorld();
58 bool DynamicObject::Create( uint32 guidlow
, Unit
*caster
, uint32 spellId
, uint32 effIndex
, float x
, float y
, float z
, int32 duration
, float radius
)
60 SetInstanceId(caster
->GetInstanceId());
62 WorldObject::_Create(guidlow
, HIGHGUID_DYNAMICOBJECT
, caster
->GetMapId(), caster
->GetPhaseMask());
65 if(!IsPositionValid())
67 sLog
.outError("ERROR: DynamicObject (spell %u eff %u) not created. Suggested coordinates isn't valid (X: %f Y: %f)",spellId
,effIndex
,GetPositionX(),GetPositionY());
72 SetFloatValue( OBJECT_FIELD_SCALE_X
, 1 );
73 SetUInt64Value( DYNAMICOBJECT_CASTER
, caster
->GetGUID() );
74 SetUInt32Value( DYNAMICOBJECT_BYTES
, 0x00000001 );
75 SetUInt32Value( DYNAMICOBJECT_SPELLID
, spellId
);
76 SetFloatValue( DYNAMICOBJECT_RADIUS
, radius
);
77 SetFloatValue( DYNAMICOBJECT_POS_X
, x
);
78 SetFloatValue( DYNAMICOBJECT_POS_Y
, y
);
79 SetFloatValue( DYNAMICOBJECT_POS_Z
, z
);
80 SetUInt32Value( DYNAMICOBJECT_CASTTIME
, getMSTime() ); // new 2.4.0
82 m_aliveDuration
= duration
;
84 m_effIndex
= effIndex
;
86 m_casterGuid
= caster
->GetGUID();
90 Unit
* DynamicObject::GetCaster() const
92 // can be not found in some cases
93 return ObjectAccessor::GetUnit(*this,m_casterGuid
);
96 void DynamicObject::Update(uint32 p_time
)
98 // caster can be not in world at time dynamic object update, but dynamic object not yet deleted in Unit destructor
99 Unit
* caster
= GetCaster();
106 bool deleteThis
= false;
108 if(m_aliveDuration
> int32(p_time
))
109 m_aliveDuration
-= p_time
;
113 // TODO: make a timer and update this in larger intervals
114 CellPair
p(MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY()));
116 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
119 MaNGOS::DynamicObjectUpdater
notifier(*this,caster
);
121 TypeContainerVisitor
<MaNGOS::DynamicObjectUpdater
, WorldTypeMapContainer
> world_object_notifier(notifier
);
122 TypeContainerVisitor
<MaNGOS::DynamicObjectUpdater
, GridTypeMapContainer
> grid_object_notifier(notifier
);
124 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
125 cell_lock
->Visit(cell_lock
, world_object_notifier
, *GetMap());
126 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *GetMap());
130 caster
->RemoveDynObjectWithGUID(GetGUID());
135 void DynamicObject::Delete()
137 SendObjectDeSpawnAnim(GetGUID());
138 AddObjectToRemoveList();
141 void DynamicObject::Delay(int32 delaytime
)
143 m_aliveDuration
-= delaytime
;
144 for(AffectedSet::iterator iunit
= m_affected
.begin();iunit
!= m_affected
.end();++iunit
)
146 (*iunit
)->DelayAura(m_spellId
, m_effIndex
, delaytime
);
149 bool DynamicObject::isVisibleForInState(Player
const* u
, bool inVisibleList
) const
151 return IsInWorld() && u
->IsInWorld() && IsWithinDistInMap(u
,World::GetMaxVisibleDistanceForObject()+(inVisibleList
? World::GetVisibleObjectGreyDistance() : 0.0f
), false);