Changeset 257 for trunk/src/game/Map.cpp
- Timestamp:
- 11/19/08 13:51:33 (17 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/game/Map.cpp
r233 r257 36 36 #include "ScriptCalls.h" 37 37 #include "Group.h" 38 #include "MapRefManager.h" 38 39 39 40 #include "MapInstanced.h" … … 450 451 bool Map::Add(Player *player) 451 452 { 453 player->GetMapRef().link(this, player); 454 452 455 player->SetInstanceId(GetInstanceId()); 453 456 … … 594 597 void Map::Update(const uint32 &t_diff) 595 598 { 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 596 648 // 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 ! 597 649 // This isn't really bother us, since as soon as we have instanced BG-s, the whole map unloads as the BG gets ended … … 611 663 void Map::Remove(Player *player, bool remove) 612 664 { 665 player->GetMapRef().unlink(); 613 666 CellPair p = Trinity::ComputeCellPair(player->GetPositionX(), player->GetPositionY()); 614 667 if(p.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP) … … 910 963 911 964 { 912 if(!pForce && ObjectAccessor::Instance().ActiveObjectsNearGrid(x, y, i_id, i_InstanceId) )965 if(!pForce && PlayersNearGrid(x, y) ) 913 966 return false; 914 967 … … 1419 1472 } 1420 1473 1474 uint32 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 1483 void 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 1489 bool 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 1421 1511 template void Map::Add(Corpse *); 1422 1512 template void Map::Add(Creature *); … … 1454 1544 bool InstanceMap::CanEnter(Player *player) 1455 1545 { 1456 if( std::find(i_Players.begin(),i_Players.end(),player)!=i_Players.end())1546 if(player->GetMapRef().getTarget() == this) 1457 1547 { 1458 1548 sLog.outError("InstanceMap::CanEnter - player %s(%u) already in map %d,%d,%d!", player->GetName(), player->GetGUIDLow(), GetId(), GetInstanceId(), GetSpawnMode()); … … 1571 1661 SetResetSchedule(false); 1572 1662 1573 i_Players.push_back(player);1574 1663 player->SendInitWorldStates(); 1575 1664 sLog.outDetail("MAP: Player '%s' entered the instance '%u' of map '%s'", player->GetName(), GetInstanceId(), GetMapName()); … … 1596 1685 { 1597 1686 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);1599 1687 SetResetSchedule(true); 1600 if(!m_unloadTimer && i_Players.empty()) 1688 //if last player set unload timer 1689 if(!m_unloadTimer && m_mapRefManager.getSize() == 1) 1601 1690 m_unloadTimer = m_unloadWhenEmpty ? MIN_UNLOAD_DELAY : std::max(sWorld.getConfig(CONFIG_INSTANCE_UNLOAD_DELAY), (uint32)MIN_UNLOAD_DELAY); 1602 1691 Map::Remove(player, remove); … … 1663 1752 // the instance must be deleted from the DB by InstanceSaveManager 1664 1753 1665 if( !i_Players.empty())1754 if(HavePlayers()) 1666 1755 { 1667 1756 if(method == INSTANCE_RESET_ALL) 1668 1757 { 1669 1758 // 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()); 1672 1761 } 1673 1762 else … … 1676 1765 { 1677 1766 // 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; 1680 1769 } 1681 1770 … … 1693 1782 } 1694 1783 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(); 1705 1785 } 1706 1786 … … 1716 1796 Group *group = player->GetGroup(); 1717 1797 // 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); 1737 1815 } 1738 1816 } … … 1746 1824 void InstanceMap::UnloadAll(bool pForce) 1747 1825 { 1748 if( !i_Players.empty())1826 if(HavePlayers()) 1749 1827 { 1750 1828 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 } 1753 1834 } 1754 1835 … … 1759 1840 } 1760 1841 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);1842 void 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); 1765 1846 } 1766 1847 … … 1770 1851 // the reset time is only scheduled when there are no payers inside 1771 1852 // 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()) 1773 1854 { 1774 1855 InstanceSave *save = sInstanceSaveManager.GetInstanceSave(GetInstanceId()); … … 1778 1859 } 1779 1860 1780 void InstanceMap::SendToPlayers(WorldPacket const* data) const1781 {1782 for(PlayerList::const_iterator itr = i_Players.begin(); itr != i_Players.end(); ++itr)1783 (*itr)->GetSession()->SendPacket(data);1784 }1785 1786 1861 /* ******* Battleground Instance Maps ******* */ 1787 1862 … … 1797 1872 bool BattleGroundMap::CanEnter(Player * player) 1798 1873 { 1799 if( std::find(i_Players.begin(),i_Players.end(),player)!=i_Players.end())1874 if(player->GetMapRef().getTarget() == this) 1800 1875 { 1801 1876 sLog.outError("BGMap::CanEnter - player %u already in map!", player->GetGUIDLow()); … … 1818 1893 if(!CanEnter(player)) 1819 1894 return false; 1820 i_Players.push_back(player);1821 1895 // reset instance validity, battleground maps do not homebind 1822 1896 player->m_InstanceValid = true; … … 1828 1902 { 1829 1903 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);1831 1904 Map::Remove(player, remove); 1832 1905 } … … 1839 1912 void BattleGroundMap::UnloadAll(bool pForce) 1840 1913 { 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()); 1846 1918 // TeleportTo removes the player from this map (if the map exists) -> calls BattleGroundMap::Remove -> invalidates the iterator. 1847 1919 // just in case, remove the player from the list explicitly here as well to prevent a possible infinite loop 1848 1920 // note that this remove is not needed if the code works well in other places 1849 i_Players.remove(plr);1921 plr->GetMapRef().unlink(); 1850 1922 } 1851 1923