[10041] Use for spell 49145 and ranks for decrease SPELL_DIRECT_DAMAGE damage.
[getmangos.git] / src / game / DynamicObject.cpp
blob6a36f6ddab3042a59a77a69d50dbce5e7d8ea693
1 /*
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
19 #include "Common.h"
20 #include "UpdateMask.h"
21 #include "Opcodes.h"
22 #include "World.h"
23 #include "ObjectAccessor.h"
24 #include "Database/DatabaseEnv.h"
25 #include "GridNotifiers.h"
26 #include "CellImpl.h"
27 #include "GridNotifiersImpl.h"
28 #include "SpellMgr.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 | UPDATEFLAG_POSITION);
37 m_valuesCount = DYNAMICOBJECT_END;
40 void DynamicObject::AddToWorld()
42 ///- Register the dynamicObject for guid lookup
43 if(!IsInWorld())
44 GetMap()->GetObjectsStore().insert<DynamicObject>(GetGUID(), (DynamicObject*)this);
46 Object::AddToWorld();
49 void DynamicObject::RemoveFromWorld()
51 ///- Remove the dynamicObject from the accessor
52 if(IsInWorld())
53 GetMap()->GetObjectsStore().erase<DynamicObject>(GetGUID(), (DynamicObject*)NULL);
55 Object::RemoveFromWorld();
58 bool DynamicObject::Create( uint32 guidlow, Unit *caster, uint32 spellId, SpellEffectIndex effIndex, float x, float y, float z, int32 duration, float radius )
60 WorldObject::_Create(guidlow, HIGHGUID_DYNAMICOBJECT, caster->GetPhaseMask());
61 SetMap(caster->GetMap());
62 Relocate(x, y, z, 0);
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());
67 return false;
70 SetEntry(spellId);
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 SetUInt32Value( DYNAMICOBJECT_CASTTIME, getMSTime() ); // new 2.4.0
78 m_aliveDuration = duration;
79 m_radius = radius;
80 m_effIndex = effIndex;
81 m_spellId = spellId;
83 // set to active for far sight case
84 if(SpellEntry const* spellEntry = sSpellStore.LookupEntry(spellId))
85 m_isActiveObject = IsSpellHaveEffect(spellEntry,SPELL_EFFECT_ADD_FARSIGHT);
87 return true;
90 Unit* DynamicObject::GetCaster() const
92 // can be not found in some cases
93 return ObjectAccessor::GetUnit(*this, GetCasterGUID());
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();
100 if(!caster)
102 Delete();
103 return;
106 bool deleteThis = false;
108 if(m_aliveDuration > int32(p_time))
109 m_aliveDuration -= p_time;
110 else
111 deleteThis = true;
113 // have radius and work as persistent effect
114 if(m_radius)
116 // TODO: make a timer and update this in larger intervals
117 MaNGOS::DynamicObjectUpdater notifier(*this, caster);
118 Cell::VisitAllObjects(this, notifier, m_radius);
121 if(deleteThis)
123 caster->RemoveDynObjectWithGUID(GetGUID());
124 Delete();
128 void DynamicObject::Delete()
130 SendObjectDeSpawnAnim(GetGUID());
131 AddObjectToRemoveList();
134 void DynamicObject::Delay(int32 delaytime)
136 m_aliveDuration -= delaytime;
137 for(AffectedSet::iterator iunit= m_affected.begin(); iunit != m_affected.end(); ++iunit)
138 if (*iunit)
139 (*iunit)->DelayAura(m_spellId, m_effIndex, delaytime);
142 bool DynamicObject::isVisibleForInState(Player const* u, WorldObject const* viewPoint, bool inVisibleList) const
144 if(!IsInWorld() || !u->IsInWorld())
145 return false;
147 // always seen by owner
148 if(GetCasterGUID()==u->GetGUID())
149 return true;
151 // normal case
152 return IsWithinDistInMap(viewPoint, World::GetMaxVisibleDistanceForObject() + (inVisibleList ? World::GetVisibleObjectGreyDistance() : 0.0f), false);
155 bool DynamicObject::IsHostileTo( Unit const* unit ) const
157 if (Unit* owner = GetCaster())
158 return owner->IsHostileTo(unit);
159 else
160 return false;
163 bool DynamicObject::IsFriendlyTo( Unit const* unit ) const
165 if (Unit* owner = GetCaster())
166 return owner->IsFriendlyTo(unit);
167 else
168 return true;