245 lines
6.5 KiB
C++
245 lines
6.5 KiB
C++
/*
|
|
* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
|
|
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
|
|
*
|
|
* 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, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "Common.h"
|
|
#include "UpdateMask.h"
|
|
#include "Opcodes.h"
|
|
#include "World.h"
|
|
#include "ObjectAccessor.h"
|
|
#include "DatabaseEnv.h"
|
|
#include "GridNotifiers.h"
|
|
#include "GridNotifiersImpl.h"
|
|
#include "ScriptMgr.h"
|
|
#include "Transport.h"
|
|
|
|
DynamicObject::DynamicObject(bool isWorldObject) : WorldObject(isWorldObject),
|
|
_aura(NULL), _removedAura(NULL), _caster(NULL), _duration(0), _spellXSpellVisualId(0), _isViewpoint(false)
|
|
{
|
|
m_objectType |= TYPEMASK_DYNAMICOBJECT;
|
|
m_objectTypeId = TYPEID_DYNAMICOBJECT;
|
|
|
|
m_updateFlag = UPDATEFLAG_STATIONARY_POSITION;
|
|
|
|
m_valuesCount = DYNAMICOBJECT_END;
|
|
_dynamicValuesCount = DYNAMICOBJECT_DYNAMIC_END;
|
|
}
|
|
|
|
DynamicObject::~DynamicObject()
|
|
{
|
|
// make sure all references were properly removed
|
|
ASSERT(!_aura);
|
|
ASSERT(!_caster);
|
|
ASSERT(!_isViewpoint);
|
|
delete _removedAura;
|
|
}
|
|
|
|
void DynamicObject::AddToWorld()
|
|
{
|
|
///- Register the dynamicObject for guid lookup and for caster
|
|
if (!IsInWorld())
|
|
{
|
|
GetMap()->GetObjectsStore().Insert<DynamicObject>(GetGUID(), this);
|
|
WorldObject::AddToWorld();
|
|
BindToCaster();
|
|
}
|
|
}
|
|
|
|
void DynamicObject::RemoveFromWorld()
|
|
{
|
|
///- Remove the dynamicObject from the accessor and from all lists of objects in world
|
|
if (IsInWorld())
|
|
{
|
|
if (_isViewpoint)
|
|
RemoveCasterViewpoint();
|
|
|
|
if (_aura)
|
|
RemoveAura();
|
|
|
|
// dynobj could get removed in Aura::RemoveAura
|
|
if (!IsInWorld())
|
|
return;
|
|
|
|
UnbindFromCaster();
|
|
WorldObject::RemoveFromWorld();
|
|
GetMap()->GetObjectsStore().Remove<DynamicObject>(GetGUID());
|
|
}
|
|
}
|
|
|
|
bool DynamicObject::CreateDynamicObject(ObjectGuid::LowType guidlow, Unit* caster, SpellInfo const* spell, Position const& pos, float radius, DynamicObjectType type, uint32 spellXSpellVisualId)
|
|
{
|
|
_spellXSpellVisualId = spellXSpellVisualId;
|
|
SetMap(caster->GetMap());
|
|
Relocate(pos);
|
|
if (!IsPositionValid())
|
|
{
|
|
TC_LOG_ERROR("misc", "DynamicObject (spell %u) not created. Suggested coordinates isn't valid (X: %f Y: %f)", spell->Id, GetPositionX(), GetPositionY());
|
|
return false;
|
|
}
|
|
|
|
WorldObject::_Create(ObjectGuid::Create<HighGuid::DynamicObject>(GetMapId(), spell->Id, guidlow));
|
|
SetPhaseMask(caster->GetPhaseMask(), false);
|
|
|
|
uint32 spellVisual = 0;
|
|
if (SpellXSpellVisualEntry const* visual = sSpellXSpellVisualStore.LookupEntry(spellXSpellVisualId))
|
|
spellVisual = visual->SpellVisualID[0];
|
|
|
|
SetEntry(spell->Id);
|
|
SetObjectScale(1.0f);
|
|
SetGuidValue(DYNAMICOBJECT_CASTER, caster->GetGUID());
|
|
SetUInt32Value(DYNAMICOBJECT_BYTES, spellVisual | (type << 28));
|
|
SetUInt32Value(DYNAMICOBJECT_SPELLID, spell->Id);
|
|
SetFloatValue(DYNAMICOBJECT_RADIUS, radius);
|
|
SetUInt32Value(DYNAMICOBJECT_CASTTIME, getMSTime());
|
|
|
|
if (IsWorldObject())
|
|
setActive(true); //must before add to map to be put in world container
|
|
|
|
Transport* transport = caster->GetTransport();
|
|
if (transport)
|
|
{
|
|
float x, y, z, o;
|
|
pos.GetPosition(x, y, z, o);
|
|
transport->CalculatePassengerOffset(x, y, z, &o);
|
|
m_movementInfo.transport.pos.Relocate(x, y, z, o);
|
|
|
|
// This object must be added to transport before adding to map for the client to properly display it
|
|
transport->AddPassenger(this);
|
|
}
|
|
|
|
if (!GetMap()->AddToMap(this))
|
|
{
|
|
// Returning false will cause the object to be deleted - remove from transport
|
|
if (transport)
|
|
transport->RemovePassenger(this);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void DynamicObject::Update(uint32 p_time)
|
|
{
|
|
// caster has to be always available and in the same map
|
|
ASSERT(_caster);
|
|
ASSERT(_caster->GetMap() == GetMap());
|
|
|
|
bool expired = false;
|
|
|
|
if (_aura)
|
|
{
|
|
if (!_aura->IsRemoved())
|
|
_aura->UpdateOwner(p_time, this);
|
|
|
|
// _aura may be set to null in Aura::UpdateOwner call
|
|
if (_aura && (_aura->IsRemoved() || _aura->IsExpired()))
|
|
expired = true;
|
|
}
|
|
else
|
|
{
|
|
if (GetDuration() > int32(p_time))
|
|
_duration -= p_time;
|
|
else
|
|
expired = true;
|
|
}
|
|
|
|
if (expired)
|
|
Remove();
|
|
else
|
|
sScriptMgr->OnDynamicObjectUpdate(this, p_time);
|
|
}
|
|
|
|
void DynamicObject::Remove()
|
|
{
|
|
if (IsInWorld())
|
|
{
|
|
SendObjectDeSpawnAnim(GetGUID());
|
|
RemoveFromWorld();
|
|
AddObjectToRemoveList();
|
|
}
|
|
}
|
|
|
|
int32 DynamicObject::GetDuration() const
|
|
{
|
|
if (!_aura)
|
|
return _duration;
|
|
else
|
|
return _aura->GetDuration();
|
|
}
|
|
|
|
void DynamicObject::SetDuration(int32 newDuration)
|
|
{
|
|
if (!_aura)
|
|
_duration = newDuration;
|
|
else
|
|
_aura->SetDuration(newDuration);
|
|
}
|
|
|
|
void DynamicObject::Delay(int32 delaytime)
|
|
{
|
|
SetDuration(GetDuration() - delaytime);
|
|
}
|
|
|
|
void DynamicObject::SetAura(Aura* aura)
|
|
{
|
|
ASSERT(!_aura && aura);
|
|
_aura = aura;
|
|
}
|
|
|
|
void DynamicObject::RemoveAura()
|
|
{
|
|
ASSERT(_aura && !_removedAura);
|
|
_removedAura = _aura;
|
|
_aura = NULL;
|
|
if (!_removedAura->IsRemoved())
|
|
_removedAura->_Remove(AURA_REMOVE_BY_DEFAULT);
|
|
}
|
|
|
|
void DynamicObject::SetCasterViewpoint()
|
|
{
|
|
if (Player* caster = _caster->ToPlayer())
|
|
{
|
|
caster->SetViewpoint(this, true);
|
|
_isViewpoint = true;
|
|
}
|
|
}
|
|
|
|
void DynamicObject::RemoveCasterViewpoint()
|
|
{
|
|
if (Player* caster = _caster->ToPlayer())
|
|
{
|
|
caster->SetViewpoint(this, false);
|
|
_isViewpoint = false;
|
|
}
|
|
}
|
|
|
|
void DynamicObject::BindToCaster()
|
|
{
|
|
ASSERT(!_caster);
|
|
_caster = ObjectAccessor::GetUnit(*this, GetCasterGUID());
|
|
ASSERT(_caster);
|
|
ASSERT(_caster->GetMap() == GetMap());
|
|
_caster->_RegisterDynObject(this);
|
|
}
|
|
|
|
void DynamicObject::UnbindFromCaster()
|
|
{
|
|
ASSERT(_caster);
|
|
_caster->_UnregisterDynObject(this);
|
|
_caster = NULL;
|
|
}
|