Files
TCPlayerbotCore/src/server/game/Entities/DynamicObject/DynamicObject.cpp
T

221 lines
5.7 KiB
C++
Raw Normal View History

/*
2011-01-01 15:01:13 +01:00
* Copyright (C) 2008-2011 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/>.
2008-10-02 16:23:55 -05:00
*/
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#include "Common.h"
2010-06-08 15:50:23 -06:00
#include "UpdateMask.h"
#include "Opcodes.h"
2008-10-02 16:23:55 -05:00
#include "World.h"
#include "ObjectAccessor.h"
2010-06-08 15:50:23 -06:00
#include "DatabaseEnv.h"
#include "GridNotifiers.h"
#include "CellImpl.h"
#include "GridNotifiersImpl.h"
#include "ScriptMgr.h"
2009-10-17 15:51:44 -07:00
DynamicObject::DynamicObject() : WorldObject(),
_aura(NULL), _caster(NULL), _duration(0), _isViewpoint(false)
2008-10-02 16:23:55 -05:00
{
m_objectType |= TYPEMASK_DYNAMICOBJECT;
m_objectTypeId = TYPEID_DYNAMICOBJECT;
2009-10-17 15:51:44 -07:00
m_updateFlag = (UPDATEFLAG_HIGHGUID | UPDATEFLAG_HAS_POSITION | UPDATEFLAG_POSITION);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
m_valuesCount = DYNAMICOBJECT_END;
}
DynamicObject::~DynamicObject()
{
// make sure all references were properly removed
ASSERT(!_aura);
ASSERT(!_caster);
ASSERT(!_isViewpoint);
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void DynamicObject::AddToWorld()
{
///- Register the dynamicObject for guid lookup and for caster
2010-04-07 19:14:10 +02:00
if (!IsInWorld())
{
sObjectAccessor->AddObject(this);
WorldObject::AddToWorld();
BindToCaster();
}
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void DynamicObject::RemoveFromWorld()
{
///- Remove the dynamicObject from the accessor and from all lists of objects in world
2010-04-07 19:14:10 +02:00
if (IsInWorld())
{
if (_isViewpoint)
RemoveCasterViewpoint();
if (_aura)
RemoveAura();
// dynobj could get removed in Aura::RemoveAura
if (!IsInWorld())
return;
UnbindFromCaster();
WorldObject::RemoveFromWorld();
sObjectAccessor->RemoveObject(this);
}
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
bool DynamicObject::Create(uint32 guidlow, Unit* caster, uint32 spellId, Position const& pos, float radius, bool active, DynamicObjectType type)
2008-10-02 16:23:55 -05:00
{
SetMap(caster->GetMap());
2009-08-29 23:20:16 -05:00
Relocate(pos);
2010-04-07 19:14:10 +02:00
if (!IsPositionValid())
2008-10-02 16:23:55 -05:00
{
sLog->outError("DynamicObject (spell %u) not created. Suggested coordinates isn't valid (X: %f Y: %f)",spellId,GetPositionX(),GetPositionY());
2008-10-02 16:23:55 -05:00
return false;
}
2009-10-17 15:51:44 -07:00
2009-08-01 00:20:32 +02:00
WorldObject::_Create(guidlow, HIGHGUID_DYNAMICOBJECT, caster->GetPhaseMask());
2009-10-17 15:51:44 -07:00
SetEntry(spellId);
2010-04-07 22:59:46 +02:00
SetFloatValue(OBJECT_FIELD_SCALE_X, 1);
SetUInt64Value(DYNAMICOBJECT_CASTER, caster->GetGUID());
// The lower word of DYNAMICOBJECT_BYTES must be 0x0001. This value means that the visual radius will be overriden
// by client for most of the "ground patch" visual effect spells and a few "skyfall" ones like Hurricane.
// If any other value is used, the client will _always_ use the radius provided in DYNAMICOBJECT_RADIUS, but
2010-04-07 18:09:10 +02:00
// precompensation is necessary (eg radius *= 2) for many spells. Anyway, blizz sends 0x0001 for all the spells
// I saw sniffed...
SetByteValue(DYNAMICOBJECT_BYTES, 0, type);
2010-04-07 22:59:46 +02:00
SetUInt32Value(DYNAMICOBJECT_SPELLID, spellId);
SetFloatValue(DYNAMICOBJECT_RADIUS, radius);
SetUInt32Value(DYNAMICOBJECT_CASTTIME, getMSTime());
2009-10-17 15:51:44 -07:00
m_isWorldObject = active;
2008-10-02 16:23:55 -05:00
return true;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void DynamicObject::Update(uint32 p_time)
{
// caster has to be always avalible and in the same map
ASSERT(_caster);
ASSERT(_caster->GetMap() == GetMap());
2009-10-17 15:51:44 -07:00
2010-01-10 01:23:15 +01:00
bool expired = false;
2009-10-17 15:51:44 -07:00
if (_aura)
{
if (!_aura->IsRemoved())
_aura->UpdateOwner(p_time, this);
2009-10-17 16:20:24 -07:00
// m_aura may be set to null in Aura::UpdateOwner call
if (_aura && (_aura->IsRemoved() || _aura->IsExpired()))
2010-01-10 01:23:15 +01:00
expired = true;
}
2010-01-10 01:23:15 +01:00
else
{
2010-04-07 19:14:10 +02:00
if (GetDuration() > int32(p_time))
_duration -= p_time;
2010-01-10 01:23:15 +01:00
else
expired = true;
}
2009-10-17 15:51:44 -07:00
2010-01-10 01:23:15 +01:00
if (expired)
Remove();
else
sScriptMgr->OnDynamicObjectUpdate(this, p_time);
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
void DynamicObject::Remove()
2008-10-02 16:23:55 -05:00
{
if (IsInWorld())
2010-01-10 01:23:15 +01:00
{
SendObjectDeSpawnAnim(GetGUID());
RemoveFromWorld();
AddObjectToRemoveList();
2010-01-10 01:23:15 +01:00
}
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2010-01-10 01:23:15 +01:00
int32 DynamicObject::GetDuration() const
{
if (!_aura)
return _duration;
2010-01-10 01:23:15 +01:00
else
return _aura->GetDuration();
2010-01-10 01:23:15 +01:00
}
void DynamicObject::SetDuration(int32 newDuration)
{
if (!_aura)
_duration = newDuration;
2010-01-10 01:23:15 +01:00
else
_aura->SetDuration(newDuration);
2010-01-10 01:23:15 +01:00
}
2008-10-02 16:23:55 -05:00
void DynamicObject::Delay(int32 delaytime)
{
2010-01-10 01:23:15 +01:00
SetDuration(GetDuration() - delaytime);
2008-10-02 16:23:55 -05:00
}
void DynamicObject::SetAura(Aura* aura)
{
ASSERT(!_aura && aura);
_aura = aura;
}
void DynamicObject::RemoveAura()
{
ASSERT(_aura);
if (!_aura->IsRemoved())
_aura->_Remove(AURA_REMOVE_BY_DEFAULT);
delete _aura;
_aura = NULL;
}
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;
}