Updated Copyright year to 2013
[getmangos.git] / src / game / Camera.cpp
blobb9aa4585cc584d80cf40000fbc7a38263d491072
1 /*
2 * Copyright (C) 2005-2013 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 "Camera.h"
20 #include "GridNotifiersImpl.h"
21 #include "CellImpl.h"
22 #include "Log.h"
23 #include "Errors.h"
24 #include "Player.h"
26 Camera::Camera(Player* pl) : m_owner(*pl), m_source(pl)
28 m_source->GetViewPoint().Attach(this);
31 Camera::~Camera()
33 // view of camera should be already reseted to owner (RemoveFromWorld -> Event_RemovedFromWorld -> ResetView)
34 MANGOS_ASSERT(m_source == &m_owner);
36 // for symmetry with constructor and way to make viewpoint's list empty
37 m_source->GetViewPoint().Detach(this);
40 void Camera::ReceivePacket(WorldPacket* data)
42 m_owner.SendDirectMessage(data);
45 void Camera::UpdateForCurrentViewPoint()
47 m_gridRef.unlink();
49 if (GridType* grid = m_source->GetViewPoint().m_grid)
50 grid->AddWorldObject(this);
52 UpdateVisibilityForOwner();
55 void Camera::SetView(WorldObject* obj, bool update_far_sight_field /*= true*/)
57 MANGOS_ASSERT(obj);
59 if (m_source == obj)
60 return;
62 if (!m_owner.IsInMap(obj))
64 sLog.outError("Camera::SetView, viewpoint is not in map with camera's owner");
65 return;
68 if (!obj->isType(TypeMask(TYPEMASK_DYNAMICOBJECT | TYPEMASK_UNIT)))
70 sLog.outError("Camera::SetView, viewpoint type is not available for client");
71 return;
74 // detach and deregister from active objects if there are no more reasons to be active
75 m_source->GetViewPoint().Detach(this);
76 if (!m_source->isActiveObject())
77 m_source->GetMap()->RemoveFromActive(m_source);
79 m_source = obj;
81 if (!m_source->isActiveObject())
82 m_source->GetMap()->AddToActive(m_source);
84 m_source->GetViewPoint().Attach(this);
86 if (update_far_sight_field)
87 m_owner.SetGuidValue(PLAYER_FARSIGHT, (m_source == &m_owner ? ObjectGuid() : m_source->GetObjectGuid()));
89 UpdateForCurrentViewPoint();
92 void Camera::Event_ViewPointVisibilityChanged()
94 if (!m_owner.HaveAtClient(m_source))
95 ResetView();
98 void Camera::ResetView(bool update_far_sight_field /*= true*/)
100 SetView(&m_owner, update_far_sight_field);
103 void Camera::Event_AddedToWorld()
105 GridType* grid = m_source->GetViewPoint().m_grid;
106 MANGOS_ASSERT(grid);
107 grid->AddWorldObject(this);
109 UpdateVisibilityForOwner();
112 void Camera::Event_RemovedFromWorld()
114 if (m_source == &m_owner)
116 m_gridRef.unlink();
117 return;
120 ResetView();
123 void Camera::Event_Moved()
125 m_gridRef.unlink();
126 m_source->GetViewPoint().m_grid->AddWorldObject(this);
129 void Camera::UpdateVisibilityOf(WorldObject* target)
131 m_owner.UpdateVisibilityOf(m_source, target);
134 template<class T>
135 void Camera::UpdateVisibilityOf(T* target, UpdateData& data, std::set<WorldObject*>& vis)
137 m_owner.template UpdateVisibilityOf<T>(m_source, target, data, vis);
140 template void Camera::UpdateVisibilityOf(Player* , UpdateData& , std::set<WorldObject*>&);
141 template void Camera::UpdateVisibilityOf(Creature* , UpdateData& , std::set<WorldObject*>&);
142 template void Camera::UpdateVisibilityOf(Corpse* , UpdateData& , std::set<WorldObject*>&);
143 template void Camera::UpdateVisibilityOf(GameObject* , UpdateData& , std::set<WorldObject*>&);
144 template void Camera::UpdateVisibilityOf(DynamicObject* , UpdateData& , std::set<WorldObject*>&);
146 void Camera::UpdateVisibilityForOwner()
148 MaNGOS::VisibleNotifier notifier(*this);
149 Cell::VisitAllObjects(m_source, notifier, m_source->GetMap()->GetVisibilityDistance(), false);
150 notifier.Notify();
153 //////////////////
155 ViewPoint::~ViewPoint()
157 if (!m_cameras.empty())
159 sLog.outError("ViewPoint destructor called, but some cameras referenced to it");