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 "UpdateMask.h"
23 #include "ObjectAccessor.h"
24 #include "Database/DatabaseEnv.h"
25 #include "GridNotifiers.h"
27 #include "GridNotifiersImpl.h"
30 DynamicObject::DynamicObject() : WorldObject(), m_isActiveObject(false)
32 m_objectType
|= TYPEMASK_DYNAMICOBJECT
;
33 m_objectTypeId
= TYPEID_DYNAMICOBJECT
;
35 m_updateFlag
= (UPDATEFLAG_HIGHGUID
| UPDATEFLAG_HAS_POSITION
);
37 m_valuesCount
= DYNAMICOBJECT_END
;
40 void DynamicObject::AddToWorld()
42 ///- Register the dynamicObject for guid lookup
44 ObjectAccessor::Instance().AddObject(this);
49 void DynamicObject::RemoveFromWorld()
51 ///- Remove the dynamicObject from the accessor
53 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 WorldObject::_Create(guidlow
, HIGHGUID_DYNAMICOBJECT
, caster
->GetPhaseMask());
61 SetMap(caster
->GetMap());
64 if(!IsPositionValid())
66 sLog
.outError("DynamicObject (spell %u eff %u) not created. Suggested coordinates isn't valid (X: %f Y: %f)",spellId
,effIndex
,GetPositionX(),GetPositionY());
71 SetFloatValue( OBJECT_FIELD_SCALE_X
, 1 );
72 SetUInt64Value( DYNAMICOBJECT_CASTER
, caster
->GetGUID() );
73 SetUInt32Value( DYNAMICOBJECT_BYTES
, 0x00000001 );
74 SetUInt32Value( DYNAMICOBJECT_SPELLID
, spellId
);
75 SetFloatValue( DYNAMICOBJECT_RADIUS
, radius
);
76 SetFloatValue( DYNAMICOBJECT_POS_X
, x
);
77 SetFloatValue( DYNAMICOBJECT_POS_Y
, y
);
78 SetFloatValue( DYNAMICOBJECT_POS_Z
, z
);
79 SetUInt32Value( DYNAMICOBJECT_CASTTIME
, getMSTime() ); // new 2.4.0
81 m_aliveDuration
= duration
;
83 m_effIndex
= effIndex
;
86 // set to active for far sight case
87 if(SpellEntry
const* spellEntry
= sSpellStore
.LookupEntry(spellId
))
88 m_isActiveObject
= IsSpellHaveEffect(spellEntry
,SPELL_EFFECT_ADD_FARSIGHT
);
93 Unit
* DynamicObject::GetCaster() const
95 // can be not found in some cases
96 return ObjectAccessor::GetUnit(*this, GetCasterGUID());
99 void DynamicObject::Update(uint32 p_time
)
101 // caster can be not in world at time dynamic object update, but dynamic object not yet deleted in Unit destructor
102 Unit
* caster
= GetCaster();
109 bool deleteThis
= false;
111 if(m_aliveDuration
> int32(p_time
))
112 m_aliveDuration
-= p_time
;
116 // have radius and work as persistent effect
119 // TODO: make a timer and update this in larger intervals
120 CellPair
p(MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY()));
122 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
125 MaNGOS::DynamicObjectUpdater
notifier(*this, caster
);
127 TypeContainerVisitor
<MaNGOS::DynamicObjectUpdater
, WorldTypeMapContainer
> world_object_notifier(notifier
);
128 TypeContainerVisitor
<MaNGOS::DynamicObjectUpdater
, GridTypeMapContainer
> grid_object_notifier(notifier
);
130 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
131 cell_lock
->Visit(cell_lock
, world_object_notifier
, *GetMap());
132 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *GetMap());
137 caster
->RemoveDynObjectWithGUID(GetGUID());
142 void DynamicObject::Delete()
144 SendObjectDeSpawnAnim(GetGUID());
145 AddObjectToRemoveList();
148 void DynamicObject::Delay(int32 delaytime
)
150 m_aliveDuration
-= delaytime
;
151 for(AffectedSet::iterator iunit
= m_affected
.begin(); iunit
!= m_affected
.end(); ++iunit
)
153 (*iunit
)->DelayAura(m_spellId
, m_effIndex
, delaytime
);
156 bool DynamicObject::isVisibleForInState(Player
const* u
, WorldObject
const* viewPoint
, bool inVisibleList
) const
158 if(!IsInWorld() || !u
->IsInWorld())
161 // always seen by owner
162 if(GetCasterGUID()==u
->GetGUID())
166 return IsWithinDistInMap(viewPoint
, World::GetMaxVisibleDistanceForObject() + (inVisibleList
? World::GetVisibleObjectGreyDistance() : 0.0f
), false);