Files
TCPlayerbotCore/src/game/DynamicObject.cpp
T

188 lines
5.7 KiB
C++
Raw Normal View History

2008-10-02 16:23:55 -05:00
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
2008-10-14 11:57:03 -05:00
*
* Copyright (C) 2008-2009 Trinity <http://www.trinitycore.org/>
2008-10-02 16:23:55 -05:00
*
* 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
2008-10-02 16:23:55 -05:00
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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"
#include "UpdateMask.h"
#include "Opcodes.h"
#include "World.h"
#include "ObjectAccessor.h"
#include "Database/DatabaseEnv.h"
#include "GridNotifiers.h"
#include "CellImpl.h"
#include "GridNotifiersImpl.h"
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
DynamicObject::DynamicObject() : WorldObject()
{
m_objectType |= TYPEMASK_DYNAMICOBJECT;
m_objectTypeId = TYPEID_DYNAMICOBJECT;
2009-10-17 15:51:44 -07:00
2009-06-11 00:45:59 -05:00
m_updateFlag = (UPDATEFLAG_HIGHGUID | UPDATEFLAG_HAS_POSITION);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
m_valuesCount = DYNAMICOBJECT_END;
}
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
if(!IsInWorld())
{
ObjectAccessor::Instance().AddObject(this);
WorldObject::AddToWorld();
}
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
if(IsInWorld())
{
if(m_isWorldObject)
{
if(Unit *caster = GetCaster())
{
if(caster->GetTypeId() == TYPEID_PLAYER)
((Player*)caster)->SetViewpoint(this, false);
}
else
{
sLog.outCrash("DynamicObject::RemoveFromWorld cannot find viewpoint owner");
}
}
WorldObject::RemoveFromWorld();
ObjectAccessor::Instance().RemoveObject(this);
}
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2009-08-29 23:20:16 -05:00
bool DynamicObject::Create(uint32 guidlow, Unit *caster, uint32 spellId, uint32 effMask, const Position &pos, int32 duration, float radius, bool active)
2008-10-02 16:23:55 -05:00
{
SetMap(caster->GetMap());
2009-08-29 23:20:16 -05:00
Relocate(pos);
2008-10-02 16:23:55 -05:00
if(!IsPositionValid())
{
sLog.outError("DynamicObject (spell %u eff %u) not created. Suggested coordinates isn't valid (X: %f Y: %f)",spellId,effMask,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);
2008-10-02 16:23:55 -05:00
SetFloatValue( OBJECT_FIELD_SCALE_X, 1 );
SetUInt64Value( DYNAMICOBJECT_CASTER, caster->GetGUID() );
SetUInt32Value( DYNAMICOBJECT_BYTES, 0x00000001 );
SetUInt32Value( DYNAMICOBJECT_SPELLID, spellId );
SetFloatValue( DYNAMICOBJECT_RADIUS, radius);
2009-08-29 23:20:16 -05:00
SetFloatValue( DYNAMICOBJECT_POS_X, pos.m_positionX );
SetFloatValue( DYNAMICOBJECT_POS_Y, pos.m_positionY );
SetFloatValue( DYNAMICOBJECT_POS_Z, pos.m_positionZ );
2008-10-02 16:23:55 -05:00
SetUInt32Value( DYNAMICOBJECT_CASTTIME, getMSTime() ); // new 2.4.0
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
m_aliveDuration = duration;
m_radius = radius;
m_effMask = effMask;
2008-10-02 16:23:55 -05:00
m_spellId = spellId;
m_updateTimer = 0;
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
Unit* DynamicObject::GetCaster() const
{
// can be not found in some cases
return ObjectAccessor::GetUnit(*this, GetCasterGUID());
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::Update(uint32 p_time)
{
// caster can be not in world at time dynamic object update, but dynamic object not yet deleted in Unit destructor
Unit* caster = GetCaster();
if (!caster)
2008-10-02 16:23:55 -05:00
{
Delete();
return;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
bool deleteThis = false;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
if(m_aliveDuration > int32(p_time))
m_aliveDuration -= p_time;
else
deleteThis = true;
2009-10-17 15:51:44 -07:00
/*
// have radius and work as persistent effect
if(m_radius)
{
// TODO: make a timer and update this in larger intervals
CellPair p(MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY()));
Cell cell(p);
cell.data.Part.reserved = ALL_DISTRICT;
cell.SetNoCreate();
2009-10-17 16:20:24 -07:00
MaNGOS::DynamicObjectUpdater notifier(*this, caster);
2009-10-17 16:20:24 -07:00
TypeContainerVisitor<MaNGOS::DynamicObjectUpdater, WorldTypeMapContainer > world_object_notifier(notifier);
TypeContainerVisitor<MaNGOS::DynamicObjectUpdater, GridTypeMapContainer > grid_object_notifier(notifier);
2009-10-17 16:20:24 -07:00
CellLock<GridReadGuard> cell_lock(cell, p);
cell_lock->Visit(cell_lock, world_object_notifier, *GetMap(), *this, m_radius);
cell_lock->Visit(cell_lock, grid_object_notifier, *GetMap(), *this, m_radius);
}
*/
2009-10-17 15:51:44 -07:00
if (m_effMask)
{
if (m_updateTimer < p_time)
{
Trinity::DynamicObjectUpdater notifier(*this,caster);
VisitNearbyObject(GetRadius(), notifier);
m_updateTimer = 500; // is this official-like?
} else m_updateTimer -= p_time;
}
2009-10-17 15:51:44 -07:00
if (deleteThis)
2008-10-02 16:23:55 -05:00
{
caster->RemoveDynObjectWithGUID(GetGUID());
Delete();
}
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void DynamicObject::Delete()
{
SendObjectDeSpawnAnim(GetGUID());
RemoveFromWorld();
2008-10-02 16:23:55 -05:00
AddObjectToRemoveList();
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void DynamicObject::Delay(int32 delaytime)
{
m_aliveDuration -= delaytime;
for (AffectedSet::iterator iunit = m_affected.begin(); iunit != m_affected.end(); ++iunit)
2008-10-02 16:23:55 -05:00
if (*iunit)
(*iunit)->DelayAura(m_spellId, GetCaster()->GetGUID(), delaytime);
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
bool DynamicObject::isVisibleForInState(Player const* u, bool inVisibleList) const
{
return IsInWorld() && u->IsInWorld()
2009-03-10 15:26:17 -06:00
&& (IsWithinDistInMap(u->m_seer,World::GetMaxVisibleDistanceForObject()+(inVisibleList ? World::GetVisibleObjectGreyDistance() : 0.0f), false));
2008-10-02 16:23:55 -05:00
}
2009-02-17 20:07:49 -06:00