/////////////////////////////////////////////////////////////// // // CSpatialDatabaseImpl::AllQuery // // Return the list of all entities // /////////////////////////////////////////////////////////////// void CSpatialDatabaseImpl::AllQuery ( CElementResult& outResult ) { // Do any pending updates first FlushUpdateQueue (); // Copy results from map to output outResult.clear (); for ( std::map < CElement*, SEntityInfo >::iterator it = m_InfoMap.begin (); it != m_InfoMap.end (); ++it ) outResult.push_back ( it->first ); }
// Put this player in other players nearlist if this player can observe them in some way void CPlayer::UpdateOthersNearList ( void ) { m_UpdateNearListTimer.Reset (); // Get the two positions to check const CVector& vecPlayerPosition = GetPosition (); CVector vecCameraPosition; GetCamera ()->GetPosition ( vecCameraPosition ); m_vecUpdateNearLastPosition = vecPlayerPosition; // Fill resultNearBoth with rough list of nearby players CElementResult resultNearBoth; { // Calculate distance from player to his camera. (Note as spatial database is 2D, we can use the 2D distance here) const float fCameraDistance = DistanceBetweenPoints2D ( vecCameraPosition, vecPlayerPosition ); if ( fCameraDistance < 40.f ) { // // If player near his camera (which is the usual case), we can do optimized things // // Do one query with a slightly bigger sphere const CVector vecAvgPos = ( vecCameraPosition + vecPlayerPosition ) * 0.5f; GetSpatialDatabase()->SphereQuery ( resultNearBoth, CSphere ( vecAvgPos, DISTANCE_FOR_NEAR_VIEWER + fCameraDistance * 0.5f ) ); } else { // // Bit more complicated if camera is not near player // // Perform queries on spatial database CElementResult resultNearCamera; GetSpatialDatabase()->SphereQuery ( resultNearCamera, CSphere ( vecCameraPosition, DISTANCE_FOR_NEAR_VIEWER ) ); CElementResult resultNearPlayer; GetSpatialDatabase()->SphereQuery ( resultNearPlayer, CSphere ( vecPlayerPosition, DISTANCE_FOR_NEAR_VIEWER ) ); std::set < CPlayer* > mergedList; // Merge for ( CElementResult::const_iterator it = resultNearCamera.begin () ; it != resultNearCamera.end (); ++it ) if ( (*it)->GetType () == CElement::PLAYER ) mergedList.insert ( (CPlayer*)*it ); for ( CElementResult::const_iterator it = resultNearPlayer.begin () ; it != resultNearPlayer.end (); ++it ) if ( (*it)->GetType () == CElement::PLAYER ) mergedList.insert ( (CPlayer*)*it ); // Copy to resultNearBoth for ( std::set < CPlayer* > ::iterator it = mergedList.begin (); it != mergedList.end (); ++it ) resultNearBoth.push_back ( *it ); } } // Accurately check distance to other players, and put this player in their near list for ( CElementResult::const_iterator it = resultNearBoth.begin () ; it != resultNearBoth.end (); ++it ) { if ( (*it)->GetType () == CElement::PLAYER ) { CPlayer* pOtherPlayer = (CPlayer*)*it; if ( pOtherPlayer != this ) { const CVector& vecOtherPlayerPos = pOtherPlayer->GetPosition (); // Check distance is accurate if ( ( vecPlayerPosition - vecOtherPlayerPos ).LengthSquared () < DISTANCE_FOR_NEAR_VIEWER * DISTANCE_FOR_NEAR_VIEWER || ( vecCameraPosition - vecOtherPlayerPos ).LengthSquared () < DISTANCE_FOR_NEAR_VIEWER * DISTANCE_FOR_NEAR_VIEWER ) { // Check dimension matches if ( m_usDimension == pOtherPlayer->GetDimension () ) { pOtherPlayer->RefreshNearPlayer ( this ); // Lightsync needs it the other way round if ( g_pBandwidthSettings->bLightSyncEnabled ) this->RefreshNearPlayer ( pOtherPlayer ); } } } } } }