1
0
forked from metin2/server
server/game/src/entity_view.cpp

238 lines
4.2 KiB
C++
Raw Normal View History

2022-03-05 12:44:06 +02:00
#include "stdafx.h"
#include "utils.h"
#include "char.h"
#include "sectree_manager.h"
#include "config.h"
void CEntity::ViewCleanup()
{
ENTITY_MAP::iterator it = m_map_view.begin();
while (it != m_map_view.end())
{
LPENTITY entity = it->first;
++it;
entity->ViewRemove(this, false);
}
m_map_view.clear();
}
void CEntity::ViewReencode()
{
if (m_bIsObserver)
return;
EncodeRemovePacket(this);
EncodeInsertPacket(this);
ENTITY_MAP::iterator it = m_map_view.begin();
while (it != m_map_view.end())
{
LPENTITY entity = (it++)->first;
EncodeRemovePacket(entity);
if (!m_bIsObserver)
EncodeInsertPacket(entity);
if (!entity->m_bIsObserver)
entity->EncodeInsertPacket(this);
}
}
void CEntity::ViewInsert(LPENTITY entity, bool recursive)
{
if (this == entity)
return;
ENTITY_MAP::iterator it = m_map_view.find(entity);
if (m_map_view.end() != it)
{
it->second = m_iViewAge;
return;
}
m_map_view.insert(ENTITY_MAP::value_type(entity, m_iViewAge));
if (!entity->m_bIsObserver)
entity->EncodeInsertPacket(this);
if (recursive)
entity->ViewInsert(this, false);
}
void CEntity::ViewRemove(LPENTITY entity, bool recursive)
{
ENTITY_MAP::iterator it = m_map_view.find(entity);
if (it == m_map_view.end())
return;
m_map_view.erase(it);
if (!entity->m_bIsObserver)
entity->EncodeRemovePacket(this);
if (recursive)
entity->ViewRemove(this, false);
}
class CFuncViewInsert
{
private:
int dwViewRange;
public:
LPENTITY m_me;
CFuncViewInsert(LPENTITY ent) :
dwViewRange(VIEW_RANGE + VIEW_BONUS_RANGE),
m_me(ent)
{
}
void operator () (LPENTITY ent)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʈ<EFBFBD><C6AE> <20>ƴ<EFBFBD> <20><><EFBFBD><EFBFBD> <20>Ÿ<EFBFBD><C5B8><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>Ͽ<EFBFBD> <20>Ÿ<EFBFBD><C5B8><EFBFBD> <20>ָ<EFBFBD> <20>߰<EFBFBD><DFB0><EFBFBD><EFBFBD><EFBFBD> <20>ʴ´<CAB4>.
if (!ent->IsType(ENTITY_OBJECT))
if (DISTANCE_APPROX(ent->GetX() - m_me->GetX(), ent->GetY() - m_me->GetY()) > dwViewRange)
return;
// <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>߰<EFBFBD>
m_me->ViewInsert(ent);
// <20>Ѵ<EFBFBD> ij<><C4B3><EFBFBD>͸<EFBFBD>
if (ent->IsType(ENTITY_CHARACTER) && m_me->IsType(ENTITY_CHARACTER))
{
LPCHARACTER chMe = (LPCHARACTER) m_me;
LPCHARACTER chEnt = (LPCHARACTER) ent;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> NPC<50><43> StateMachine<6E><65> Ų<><C5B2>.
if (chMe->IsPC() && !chEnt->IsPC() && !chEnt->IsWarp() && !chEnt->IsGoto())
chEnt->StartStateMachine();
}
}
};
void CEntity::UpdateSectree()
{
if (!GetSectree())
{
if (IsType(ENTITY_CHARACTER))
{
LPCHARACTER tch = (LPCHARACTER) this;
sys_err("null sectree name: %s %d %d", tch->GetName(), GetX(), GetY());
}
return;
}
++m_iViewAge;
CFuncViewInsert f(this); // <20><><EFBFBD><EFBFBD> <20><>Ʈ<EFBFBD><C6AE><EFBFBD><EFBFBD> <20>ִ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9BFA1> <20>߰<EFBFBD>
GetSectree()->ForEachAround(f);
ENTITY_MAP::iterator it, this_it;
//
// m_map_view<65><77><EFBFBD><EFBFBD> <20>ʿ<EFBFBD> <20><><EFBFBD><EFBFBD> <20><EFBFBD><E0BCAE> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//
if (m_bObserverModeChange)
{
if (m_bIsObserver)
{
it = m_map_view.begin();
while (it != m_map_view.end())
{
this_it = it++;
if (this_it->second < m_iViewAge)
{
LPENTITY ent = this_it->first;
// <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
ent->EncodeRemovePacket(this);
m_map_view.erase(this_it);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
ent->ViewRemove(this, false);
}
else
{
LPENTITY ent = this_it->first;
// <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
//ent->EncodeRemovePacket(this);
//m_map_view.erase(this_it);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
//ent->ViewRemove(this, false);
EncodeRemovePacket(ent);
}
}
}
else
{
it = m_map_view.begin();
while (it != m_map_view.end())
{
this_it = it++;
if (this_it->second < m_iViewAge)
{
LPENTITY ent = this_it->first;
// <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
ent->EncodeRemovePacket(this);
m_map_view.erase(this_it);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
ent->ViewRemove(this, false);
}
else
{
LPENTITY ent = this_it->first;
ent->EncodeInsertPacket(this);
EncodeInsertPacket(ent);
ent->ViewInsert(this, true);
}
}
}
m_bObserverModeChange = false;
}
else
{
if (!m_bIsObserver)
{
it = m_map_view.begin();
while (it != m_map_view.end())
{
this_it = it++;
if (this_it->second < m_iViewAge)
{
LPENTITY ent = this_it->first;
// <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
ent->EncodeRemovePacket(this);
m_map_view.erase(this_it);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
ent->ViewRemove(this, false);
}
}
}
}
}