Changeset 257 for trunk/src/game/Map.cpp

Show
Ignore:
Timestamp:
11/19/08 13:51:33 (17 years ago)
Author:
yumileroy
Message:

*Merge from Mangos. Add MapReference?. Author: hunuza.
*Also re-commit the patches reverted in 255.

Original author: megamage
Date: 2008-11-18 19:40:06-06:00

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/game/Map.cpp

    r233 r257  
    3636#include "ScriptCalls.h" 
    3737#include "Group.h" 
     38#include "MapRefManager.h" 
    3839 
    3940#include "MapInstanced.h" 
     
    450451bool Map::Add(Player *player) 
    451452{ 
     453    player->GetMapRef().link(this, player); 
     454 
    452455    player->SetInstanceId(GetInstanceId()); 
    453456 
     
    594597void Map::Update(const uint32 &t_diff) 
    595598{ 
     599    resetMarkedCells(); 
     600 
     601    Trinity::ObjectUpdater updater(t_diff); 
     602    // for creature 
     603    TypeContainerVisitor<Trinity::ObjectUpdater, GridTypeMapContainer  > grid_object_update(updater); 
     604    // for pets 
     605    TypeContainerVisitor<Trinity::ObjectUpdater, WorldTypeMapContainer > world_object_update(updater); 
     606 
     607    for(MapRefManager::iterator iter = m_mapRefManager.begin(); iter != m_mapRefManager.end(); ++iter) 
     608    { 
     609        Player* plr = iter->getSource(); 
     610        if(!plr->IsInWorld()) 
     611            continue; 
     612 
     613        CellPair standing_cell(Trinity::ComputeCellPair(plr->GetPositionX(), plr->GetPositionY())); 
     614 
     615        // Check for correctness of standing_cell, it also avoids problems with update_cell 
     616        if (standing_cell.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || standing_cell.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP) 
     617            continue; 
     618 
     619        // the overloaded operators handle range checking 
     620        // so ther's no need for range checking inside the loop 
     621        CellPair begin_cell(standing_cell), end_cell(standing_cell); 
     622        begin_cell << 1; begin_cell -= 1;               // upper left 
     623        end_cell >> 1; end_cell += 1;                   // lower right 
     624 
     625        for(uint32 x = begin_cell.x_coord; x <= end_cell.x_coord; ++x) 
     626        { 
     627            for(uint32 y = begin_cell.y_coord; y <= end_cell.y_coord; ++y) 
     628            { 
     629                // marked cells are those that have been visited 
     630                // don't visit the same cell twice 
     631                uint32 cell_id = (y * TOTAL_NUMBER_OF_CELLS_PER_MAP) + x; 
     632                if(!isCellMarked(cell_id)) 
     633                { 
     634                    markCell(cell_id); 
     635                    CellPair pair(x,y); 
     636                    Cell cell(pair); 
     637                    cell.data.Part.reserved = CENTER_DISTRICT; 
     638                    cell.SetNoCreate(); 
     639                    CellLock<NullGuard> cell_lock(cell, pair); 
     640                    cell_lock->Visit(cell_lock, grid_object_update,  *this); 
     641                    cell_lock->Visit(cell_lock, world_object_update, *this); 
     642                } 
     643            } 
     644        } 
     645    } 
     646 
     647 
    596648    // Don't unload grids if it's battleground, since we may have manually added GOs,creatures, those doesn't load from DB at grid re-load ! 
    597649    // This isn't really bother us, since as soon as we have instanced BG-s, the whole map unloads as the BG gets ended 
     
    611663void Map::Remove(Player *player, bool remove) 
    612664{ 
     665    player->GetMapRef().unlink(); 
    613666    CellPair p = Trinity::ComputeCellPair(player->GetPositionX(), player->GetPositionY()); 
    614667    if(p.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP) 
     
    910963 
    911964    { 
    912         if(!pForce && ObjectAccessor::Instance().ActiveObjectsNearGrid(x, y, i_id, i_InstanceId) ) 
     965        if(!pForce && PlayersNearGrid(x, y) ) 
    913966            return false; 
    914967 
     
    14191472} 
    14201473 
     1474uint32 Map::GetPlayersCountExceptGMs() const 
     1475{ 
     1476    uint32 count = 0; 
     1477    for(MapRefManager::const_iterator itr = m_mapRefManager.begin(); itr != m_mapRefManager.end(); ++itr) 
     1478        if(!itr->getSource()->isGameMaster()) 
     1479            ++count; 
     1480    return count; 
     1481} 
     1482 
     1483void Map::SendToPlayers(WorldPacket const* data) const 
     1484{ 
     1485    for(MapRefManager::const_iterator itr = m_mapRefManager.begin(); itr != m_mapRefManager.end(); ++itr) 
     1486        itr->getSource()->GetSession()->SendPacket(data); 
     1487} 
     1488 
     1489bool Map::PlayersNearGrid(uint32 x, uint32 y) const 
     1490{ 
     1491    CellPair cell_min(x*MAX_NUMBER_OF_CELLS, y*MAX_NUMBER_OF_CELLS); 
     1492    CellPair cell_max(cell_min.x_coord + MAX_NUMBER_OF_CELLS, cell_min.y_coord+MAX_NUMBER_OF_CELLS); 
     1493    cell_min << 2; 
     1494    cell_min -= 2; 
     1495    cell_max >> 2; 
     1496    cell_max += 2; 
     1497 
     1498    for(MapRefManager::const_iterator iter = m_mapRefManager.begin(); iter != m_mapRefManager.end(); ++iter) 
     1499    { 
     1500        Player* plr = iter->getSource(); 
     1501 
     1502        CellPair p = Trinity::ComputeCellPair(plr->GetPositionX(), plr->GetPositionY()); 
     1503        if( (cell_min.x_coord <= p.x_coord && p.x_coord <= cell_max.x_coord) && 
     1504            (cell_min.y_coord <= p.y_coord && p.y_coord <= cell_max.y_coord) ) 
     1505            return true; 
     1506    } 
     1507 
     1508    return false; 
     1509} 
     1510 
    14211511template void Map::Add(Corpse *); 
    14221512template void Map::Add(Creature *); 
     
    14541544bool InstanceMap::CanEnter(Player *player) 
    14551545{ 
    1456     if(std::find(i_Players.begin(),i_Players.end(),player)!=i_Players.end()) 
     1546    if(player->GetMapRef().getTarget() == this) 
    14571547    { 
    14581548        sLog.outError("InstanceMap::CanEnter - player %s(%u) already in map %d,%d,%d!", player->GetName(), player->GetGUIDLow(), GetId(), GetInstanceId(), GetSpawnMode()); 
     
    15711661        SetResetSchedule(false); 
    15721662 
    1573         i_Players.push_back(player); 
    15741663        player->SendInitWorldStates(); 
    15751664        sLog.outDetail("MAP: Player '%s' entered the instance '%u' of map '%s'", player->GetName(), GetInstanceId(), GetMapName()); 
     
    15961685{ 
    15971686    sLog.outDetail("MAP: Removing player '%s' from instance '%u' of map '%s' before relocating to other map", player->GetName(), GetInstanceId(), GetMapName()); 
    1598     i_Players.remove(player); 
    15991687    SetResetSchedule(true); 
    1600     if(!m_unloadTimer && i_Players.empty()) 
     1688    //if last player set unload timer 
     1689    if(!m_unloadTimer && m_mapRefManager.getSize() == 1) 
    16011690        m_unloadTimer = m_unloadWhenEmpty ? MIN_UNLOAD_DELAY : std::max(sWorld.getConfig(CONFIG_INSTANCE_UNLOAD_DELAY), (uint32)MIN_UNLOAD_DELAY); 
    16021691    Map::Remove(player, remove); 
     
    16631752    // the instance must be deleted from the DB by InstanceSaveManager 
    16641753 
    1665     if(!i_Players.empty()) 
     1754    if(HavePlayers()) 
    16661755    { 
    16671756        if(method == INSTANCE_RESET_ALL) 
    16681757        { 
    16691758            // notify the players to leave the instance so it can be reset 
    1670             for(PlayerList::iterator itr = i_Players.begin(); itr != i_Players.end(); ++itr) 
    1671                 (*itr)->SendResetFailedNotify(GetId()); 
     1759            for(MapRefManager::iterator itr = m_mapRefManager.begin(); itr != m_mapRefManager.end(); ++itr) 
     1760                itr->getSource()->SendResetFailedNotify(GetId()); 
    16721761        } 
    16731762        else 
     
    16761765            { 
    16771766                // set the homebind timer for players inside (1 minute) 
    1678                 for(PlayerList::iterator itr = i_Players.begin(); itr != i_Players.end(); ++itr) 
    1679                     (*itr)->m_InstanceValid = false; 
     1767                for(MapRefManager::iterator itr = m_mapRefManager.begin(); itr != m_mapRefManager.end(); ++itr) 
     1768                    itr->getSource()->m_InstanceValid = false; 
    16801769            } 
    16811770 
     
    16931782    } 
    16941783 
    1695     return i_Players.empty(); 
    1696 } 
    1697  
    1698 uint32 InstanceMap::GetPlayersCountExceptGMs() const 
    1699 { 
    1700     uint32 count = 0; 
    1701     for(PlayerList::const_iterator itr = i_Players.begin(); itr != i_Players.end(); ++itr) 
    1702         if(!(*itr)->isGameMaster()) 
    1703             ++count; 
    1704     return count; 
     1784    return m_mapRefManager.isEmpty(); 
    17051785} 
    17061786 
     
    17161796    Group *group = player->GetGroup(); 
    17171797    // group members outside the instance group don't get bound 
    1718     for(PlayerList::iterator itr = i_Players.begin(); itr != i_Players.end(); ++itr) 
    1719     { 
    1720         if(*itr) 
    1721         { 
    1722             // players inside an instance cannot be bound to other instances 
    1723             // some players may already be permanently bound, in this case nothing happens 
    1724             InstancePlayerBind *bind = (*itr)->GetBoundInstance(save->GetMapId(), save->GetDifficulty()); 
    1725             if(!bind || !bind->perm) 
    1726             { 
    1727                 (*itr)->BindToInstance(save, true); 
    1728                 WorldPacket data(SMSG_INSTANCE_SAVE_CREATED, 4); 
    1729                 data << uint32(0); 
    1730                 (*itr)->GetSession()->SendPacket(&data); 
    1731             } 
    1732  
    1733             // if the leader is not in the instance the group will not get a perm bind 
    1734             if(group && group->GetLeaderGUID() == (*itr)->GetGUID()) 
    1735                 group->BindToInstance(save, true); 
    1736         } 
     1798    for(MapRefManager::iterator itr = m_mapRefManager.begin(); itr != m_mapRefManager.end(); ++itr) 
     1799    { 
     1800        Player* plr = itr->getSource(); 
     1801        // players inside an instance cannot be bound to other instances 
     1802        // some players may already be permanently bound, in this case nothing happens 
     1803        InstancePlayerBind *bind = plr->GetBoundInstance(save->GetMapId(), save->GetDifficulty()); 
     1804        if(!bind || !bind->perm) 
     1805        { 
     1806            plr->BindToInstance(save, true); 
     1807            WorldPacket data(SMSG_INSTANCE_SAVE_CREATED, 4); 
     1808            data << uint32(0); 
     1809            plr->GetSession()->SendPacket(&data); 
     1810        } 
     1811 
     1812        // if the leader is not in the instance the group will not get a perm bind 
     1813        if(group && group->GetLeaderGUID() == plr->GetGUID()) 
     1814            group->BindToInstance(save, true); 
    17371815    } 
    17381816} 
     
    17461824void InstanceMap::UnloadAll(bool pForce) 
    17471825{ 
    1748     if(!i_Players.empty()) 
     1826    if(HavePlayers()) 
    17491827    { 
    17501828        sLog.outError("InstanceMap::UnloadAll: there are still players in the instance at unload, should not happen!"); 
    1751         for(PlayerList::iterator itr = i_Players.begin(); itr != i_Players.end(); ++itr) 
    1752             if(*itr) (*itr)->TeleportTo((*itr)->m_homebindMapId, (*itr)->m_homebindX, (*itr)->m_homebindY, (*itr)->m_homebindZ, (*itr)->GetOrientation()); 
     1829        for(MapRefManager::iterator itr = m_mapRefManager.begin(); itr != m_mapRefManager.end(); ++itr) 
     1830        { 
     1831            Player* plr = itr->getSource(); 
     1832            plr->TeleportTo(plr->m_homebindMapId, plr->m_homebindX, plr->m_homebindY, plr->m_homebindZ, plr->GetOrientation()); 
     1833        } 
    17531834    } 
    17541835 
     
    17591840} 
    17601841 
    1761 void InstanceMap::SendResetWarnings(uint32 timeLeft) 
    1762 { 
    1763     for(PlayerList::iterator itr = i_Players.begin(); itr != i_Players.end(); ++itr) 
    1764         (*itr)->SendInstanceResetWarning(GetId(), timeLeft); 
     1842void InstanceMap::SendResetWarnings(uint32 timeLeft) const 
     1843{ 
     1844    for(MapRefManager::const_iterator itr = m_mapRefManager.begin(); itr != m_mapRefManager.end(); ++itr) 
     1845        itr->getSource()->SendInstanceResetWarning(GetId(), timeLeft); 
    17651846} 
    17661847 
     
    17701851    // the reset time is only scheduled when there are no payers inside 
    17711852    // it is assumed that the reset time will rarely (if ever) change while the reset is scheduled 
    1772     if(i_Players.empty() && !IsRaid() && !IsHeroic()) 
     1853    if(!HavePlayers() && !IsRaid() && !IsHeroic()) 
    17731854    { 
    17741855        InstanceSave *save = sInstanceSaveManager.GetInstanceSave(GetInstanceId()); 
     
    17781859} 
    17791860 
    1780 void InstanceMap::SendToPlayers(WorldPacket const* data) const 
    1781 { 
    1782     for(PlayerList::const_iterator itr = i_Players.begin(); itr != i_Players.end(); ++itr) 
    1783         (*itr)->GetSession()->SendPacket(data); 
    1784 } 
    1785  
    17861861/* ******* Battleground Instance Maps ******* */ 
    17871862 
     
    17971872bool BattleGroundMap::CanEnter(Player * player) 
    17981873{ 
    1799     if(std::find(i_Players.begin(),i_Players.end(),player)!=i_Players.end()) 
     1874    if(player->GetMapRef().getTarget() == this) 
    18001875    { 
    18011876        sLog.outError("BGMap::CanEnter - player %u already in map!", player->GetGUIDLow()); 
     
    18181893        if(!CanEnter(player)) 
    18191894            return false; 
    1820         i_Players.push_back(player); 
    18211895        // reset instance validity, battleground maps do not homebind 
    18221896        player->m_InstanceValid = true; 
     
    18281902{ 
    18291903    sLog.outDetail("MAP: Removing player '%s' from bg '%u' of map '%s' before relocating to other map", player->GetName(), GetInstanceId(), GetMapName()); 
    1830     i_Players.remove(player); 
    18311904    Map::Remove(player, remove); 
    18321905} 
     
    18391912void BattleGroundMap::UnloadAll(bool pForce) 
    18401913{ 
    1841     while(!i_Players.empty()) 
    1842     { 
    1843         PlayerList::iterator itr = i_Players.begin(); 
    1844         Player * plr = *itr; 
    1845         if(plr) (plr)->TeleportTo((*itr)->m_homebindMapId, (*itr)->m_homebindX, (*itr)->m_homebindY, (*itr)->m_homebindZ, (*itr)->GetOrientation()); 
     1914    while(HavePlayers()) 
     1915    { 
     1916        Player * plr = m_mapRefManager.getFirst()->getSource(); 
     1917        if(plr) (plr)->TeleportTo(plr->m_homebindMapId, plr->m_homebindX, plr->m_homebindY, plr->m_homebindZ, plr->GetOrientation()); 
    18461918        // TeleportTo removes the player from this map (if the map exists) -> calls BattleGroundMap::Remove -> invalidates the iterator. 
    18471919        // just in case, remove the player from the list explicitly here as well to prevent a possible infinite loop 
    18481920        // note that this remove is not needed if the code works well in other places 
    1849         i_Players.remove(plr); 
     1921        plr->GetMapRef().unlink(); 
    18501922    } 
    18511923