/*
* Copyright (C) 2008-2020 TrinityCore
*
* 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 .
*/
#include "AreaTrigger.h"
#include "Log.h"
#include "Map.h"
#include "ObjectAccessor.h"
#include "PhasingHandler.h"
#include "SpellInfo.h"
#include "Unit.h"
#include "UpdateData.h"
AreaTrigger::AreaTrigger() : WorldObject(false), _duration(0)
{
m_objectType |= TYPEMASK_AREATRIGGER;
m_objectTypeId = TYPEID_AREATRIGGER;
m_updateFlag = UPDATEFLAG_STATIONARY_POSITION;
m_valuesCount = AREATRIGGER_END;
}
AreaTrigger::~AreaTrigger()
{
}
void AreaTrigger::AddToWorld()
{
///- Register the AreaTrigger for guid lookup and for caster
if (!IsInWorld())
{
GetMap()->GetObjectsStore().Insert(GetGUID(), this);
WorldObject::AddToWorld();
}
}
void AreaTrigger::RemoveFromWorld()
{
///- Remove the AreaTrigger from the accessor and from all lists of objects in world
if (IsInWorld())
{
WorldObject::RemoveFromWorld();
GetMap()->GetObjectsStore().Remove(GetGUID());
}
}
bool AreaTrigger::CreateAreaTrigger(uint32 guidlow, uint32 triggerEntry, Unit* caster, SpellInfo const* spell, Position const& pos)
{
SetMap(caster->GetMap());
Relocate(pos);
if (!IsPositionValid())
{
TC_LOG_ERROR("misc", "AreaTrigger (spell %u) not created. Invalid coordinates (X: %f Y: %f)", spell->Id, GetPositionX(), GetPositionY());
return false;
}
WorldObject::_Create(guidlow, HighGuid::AreaTrigger, caster->GetPhaseMask());
SetEntry(triggerEntry);
SetDuration(spell->GetDuration());
SetObjectScale(1);
SetUInt32Value(AREATRIGGER_SPELLID, spell->Id);
SetUInt32Value(AREATRIGGER_SPELLVISUALID, spell->SpellVisual[0]);
SetUInt32Value(AREATRIGGER_DURATION, spell->GetDuration());
SetFloatValue(AREATRIGGER_FINAL_POS + 0, pos.GetPositionX());
SetFloatValue(AREATRIGGER_FINAL_POS + 1, pos.GetPositionY());
SetFloatValue(AREATRIGGER_FINAL_POS + 2, pos.GetPositionZ());
PhasingHandler::InheritPhaseShift(this, caster);
if (!GetMap()->AddToMap(this))
return false;
return true;
}
void AreaTrigger::Update(uint32 p_time)
{
if (GetDuration() > int32(p_time))
_duration -= p_time;
else
Remove(); // expired
WorldObject::Update(p_time);
}
void AreaTrigger::Remove()
{
if (IsInWorld())
{
RemoveFromWorld();
AddObjectToRemoveList();
}
}