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

[svn] * Allow WorldObjects? to keep the grid active, and prevent it from being unloaded. This can be done through calling WorldObject::setActive(bool) from the scripting library. Note that entire instances are still unloaded if no player is present on that map to save resources. This behavior can be changed if the need arises.

Original author: w12x
Date: 2008-10-27 08:41:55-05:00

Files:
1 modified

Legend:

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

    r102 r120  
    506506 
    507507void 
     508ObjectAccessor::AddActiveObject( WorldObject * obj ) 
     509{ 
     510    i_activeobjects.insert(obj); 
     511} 
     512 
     513void 
     514ObjectAccessor::RemoveActiveObject( WorldObject * obj ) 
     515{ 
     516    i_activeobjects.erase(obj); 
     517} 
     518 
     519void 
    508520ObjectAccessor::Update(uint32 diff) 
    509521{ 
    510522    { 
    511         typedef std::multimap<uint32, Player *> CreatureLocationHolderType; 
    512         CreatureLocationHolderType creature_locations; 
    513         //TODO: Player guard 
     523        // player update might remove the player from grid, and that causes crashes. We HAVE to update players first, and then the active objects. 
    514524        HashMapHolder<Player>::MapType& playerMap = HashMapHolder<Player>::GetContainer(); 
    515525        for(HashMapHolder<Player>::MapType::iterator iter = playerMap.begin(); iter != playerMap.end(); ++iter) 
     
    518528            { 
    519529                iter->second->Update(diff); 
    520                 creature_locations.insert( CreatureLocationHolderType::value_type(iter->second->GetMapId(), iter->second) ); 
    521530            } 
     531        } 
     532 
     533        // clone the active object list, because update might remove from it 
     534        std::set<WorldObject *> activeobjects(i_activeobjects); 
     535 
     536        std::set<WorldObject *>::const_iterator itr; 
     537        for(itr = activeobjects.begin(); itr != activeobjects.end(); ++itr) 
     538        { 
     539            (*itr)->GetMap()->resetMarkedCells(); 
    522540        } 
    523541 
     
    530548        TypeContainerVisitor<Trinity::ObjectUpdater, WorldTypeMapContainer > world_object_update(updater); 
    531549 
    532         for(CreatureLocationHolderType::iterator iter=creature_locations.begin(); iter != creature_locations.end(); ++iter) 
    533         { 
    534             MapManager::Instance().GetMap((*iter).first, (*iter).second)->resetMarkedCells(); 
    535         } 
    536  
    537         for(CreatureLocationHolderType::iterator iter=creature_locations.begin(); iter != creature_locations.end(); ++iter) 
    538         { 
    539             Player *player = (*iter).second; 
    540             map = MapManager::Instance().GetMap((*iter).first, player); 
    541  
    542             CellPair standing_cell(Trinity::ComputeCellPair(player->GetPositionX(), player->GetPositionY())); 
     550        for(itr = activeobjects.begin(); itr != activeobjects.end(); ++itr) 
     551        { 
     552            WorldObject *obj = (*itr); 
     553            map = obj->GetMap(); 
     554 
     555            CellPair standing_cell(Trinity::ComputeCellPair(obj->GetPositionX(), obj->GetPositionY())); 
    543556 
    544557            // Check for correctness of standing_cell, it also avoids problems with update_cell 
     
    577590 
    578591bool 
    579 ObjectAccessor::PlayersNearGrid(uint32 x, uint32 y, uint32 m_id, uint32 i_id) const 
     592ObjectAccessor::ActiveObjectsNearGrid(uint32 x, uint32 y, uint32 m_id, uint32 i_id) const 
    580593{ 
    581594    CellPair cell_min(x*MAX_NUMBER_OF_CELLS, y*MAX_NUMBER_OF_CELLS); 
     
    586599    cell_max += 2; 
    587600 
    588     //TODO: Guard player 
    589     HashMapHolder<Player>::MapType& playerMap = HashMapHolder<Player>::GetContainer(); 
    590     for(HashMapHolder<Player>::MapType::const_iterator iter=playerMap.begin(); iter != playerMap.end(); ++iter) 
    591     { 
    592         if( m_id != iter->second->GetMapId() || i_id != iter->second->GetInstanceId() ) 
     601    for(std::set<WorldObject*>::const_iterator itr = i_activeobjects.begin(); itr != i_activeobjects.end(); ++itr) 
     602    { 
     603        if( m_id != (*itr)->GetMapId() || i_id != (*itr)->GetInstanceId() ) 
    593604            continue; 
    594605 
    595         CellPair p = Trinity::ComputeCellPair(iter->second->GetPositionX(), iter->second->GetPositionY()); 
     606        CellPair p = Trinity::ComputeCellPair((*itr)->GetPositionX(), (*itr)->GetPositionY()); 
    596607        if( (cell_min.x_coord <= p.x_coord && p.x_coord <= cell_max.x_coord) && 
    597608            (cell_min.y_coord <= p.y_coord && p.y_coord <= cell_max.y_coord) ) 
    598609            return true; 
     610 
    599611    } 
    600612