From 8bf5d116fe4c7b2addeba2dac9a8b1fc93486444 Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 21 Feb 2014 15:26:33 +0200 Subject: Split cMap::UpdateClient --- src/Map.cpp | 126 ++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 76 insertions(+), 50 deletions(-) (limited to 'src/Map.cpp') diff --git a/src/Map.cpp b/src/Map.cpp index cb5472a22..0028a1e94 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -278,28 +278,35 @@ void cMap::UpdateDecorators(void) -void cMap::UpdateClient(cPlayer * a_Player) +void cMap::AddPlayer(cPlayer * a_Player, cClientHandle * a_Handle, Int64 a_WorldAge) { - ASSERT(a_Player != NULL); - cClientHandle * Handle = a_Player->GetClientHandle(); + cMapClient MapClient; + + MapClient.m_LastUpdate = a_WorldAge; + MapClient.m_SendInfo = true; + MapClient.m_Handle = a_Handle; + + m_Clients.push_back(MapClient); + + cMapDecorator PlayerDecorator(this, a_Player); + + m_Decorators.push_back(PlayerDecorator); +} + + - if (Handle == NULL) - { - return; - } - Int64 WorldAge = a_Player->GetWorld()->GetWorldAge(); - // Remove invalid clients +void cMap::RemoveInactiveClients(Int64 a_WorldAge) +{ for (cMapClientList::iterator it = m_Clients.begin(); it != m_Clients.end();) { - // Check if client is active - if (it->m_LastUpdate < WorldAge - 5) + if (it->m_LastUpdate < a_WorldAge) { // Remove associated decorators for (cMapDecoratorList::iterator it2 = m_Decorators.begin(); it2 != m_Decorators.end();) { - if (it2->GetPlayer()->GetClientHandle() == Handle) + if (it2->GetPlayer()->GetClientHandle() == it->m_Handle) { // Erase decorator cMapDecoratorList::iterator temp = it2; @@ -322,63 +329,82 @@ void cMap::UpdateClient(cPlayer * a_Player) ++it; } } +} - // Linear search for client state - for (cMapClientList::iterator it = m_Clients.begin(); it != m_Clients.end(); ++it) + + + + +void cMap::StreamNext(cMapClient & a_Client) +{ + cClientHandle * Handle = a_Client.m_Handle; + + if (a_Client.m_SendInfo) { - if (it->m_Handle == Handle) - { - it->m_LastUpdate = WorldAge; + Handle->SendMapInfo(m_ID, m_Scale); - if (it->m_SendInfo) - { - Handle->SendMapInfo(m_ID, m_Scale); + a_Client.m_SendInfo = false; - it->m_SendInfo = false; + return; + } - return; - } + ++a_Client.m_NextDecoratorUpdate; - ++it->m_NextDecoratorUpdate; + if (a_Client.m_NextDecoratorUpdate >= 4) + { + // TODO 2014-02-19 xdot + // This is dangerous as the player object may have been destroyed before the decorator is erased from the list + UpdateDecorators(); - if (it->m_NextDecoratorUpdate >= 4) - { - // TODO 2014-02-19 xdot - // This is dangerous as the player object may have been destroyed before the decorator is erased from the list - UpdateDecorators(); + Handle->SendMapDecorators(m_ID, m_Decorators); - Handle->SendMapDecorators(m_ID, m_Decorators); + a_Client.m_NextDecoratorUpdate = 0; + } + else + { + ++a_Client.m_DataUpdate; - it->m_NextDecoratorUpdate = 0; - } - else - { - ++it->m_DataUpdate; + unsigned int Y = (a_Client.m_DataUpdate * 11) % m_Width; - unsigned int Y = (it->m_DataUpdate * 11) % m_Width; + const Byte * Colors = &m_Data[Y * m_Height]; - const Byte * Colors = &m_Data[Y * m_Height]; + Handle->SendMapColumn(m_ID, Y, 0, Colors, m_Height); + } +} - Handle->SendMapColumn(m_ID, Y, 0, Colors, m_Height); - } - return; - } + + + +void cMap::UpdateClient(cPlayer * a_Player) +{ + ASSERT(a_Player != NULL); + cClientHandle * Handle = a_Player->GetClientHandle(); + + if (Handle == NULL) + { + return; } - // New player, construct a new client state - cMapClient MapClient; + Int64 WorldAge = a_Player->GetWorld()->GetWorldAge(); - MapClient.m_LastUpdate = WorldAge; - MapClient.m_SendInfo = true; - MapClient.m_Handle = a_Player->GetClientHandle(); + RemoveInactiveClients(WorldAge - 5); - m_Clients.push_back(MapClient); + // Linear search for client state + for (cMapClientList::iterator it = m_Clients.begin(); it != m_Clients.end(); ++it) + { + if (it->m_Handle == Handle) + { + it->m_LastUpdate = WorldAge; - // Insert new decorator - cMapDecorator PlayerDecorator(this, a_Player); + StreamNext(*it); - m_Decorators.push_back(PlayerDecorator); + return; + } + } + + // New player, construct a new client state + AddPlayer(a_Player, Handle, WorldAge); } -- cgit v1.2.3