Index: /trunk/src/game/Object.h
===================================================================
--- /trunk/src/game/Object.h (revision 230)
+++ /trunk/src/game/Object.h (revision 268)
@@ -456,5 +456,5 @@
         GameObject* SummonGameObject(uint32 entry, float x, float y, float z, float ang, float rotation0, float rotation1, float rotation2, float rotation3, uint32 respawnTime);
         bool isActive() const { return m_isActive; }
-        virtual void setActive(bool isActive);
+        void setActive(bool isActive);
     protected:
         explicit WorldObject();
Index: /trunk/src/game/Map.h
===================================================================
--- /trunk/src/game/Map.h (revision 260)
+++ /trunk/src/game/Map.h (revision 268)
@@ -243,4 +243,8 @@
         uint32 GetPlayersCountExceptGMs() const;
         bool PlayersNearGrid(uint32 x,uint32 y) const;
+        bool ActiveObjectsNearGrid(uint32 x, uint32 y) const;
+
+        void AddActiveObject(WorldObject* obj) { i_activeObjects.insert(obj); }
+        void RemoveActiveObject(WorldObject* obj) { i_activeObjects.erase(obj); }
 
         void SendToPlayers(WorldPacket const* data) const;
@@ -288,4 +292,5 @@
         inline void setNGrid(NGridType* grid, uint32 x, uint32 y);
 
+        void UpdateActiveCells(const float &x, const float &y, const uint32 &t_diff);
     protected:
         typedef Trinity::ObjectLevelLockable<Map, ZThread::Mutex>::Lock Guard;
@@ -308,4 +313,5 @@
         time_t i_gridExpiry;
 
+        std::set<WorldObject *> i_activeObjects;
         std::set<WorldObject *> i_objectsToRemove;
 
Index: /trunk/src/game/ObjectAccessor.h
===================================================================
--- /trunk/src/game/ObjectAccessor.h (revision 257)
+++ /trunk/src/game/ObjectAccessor.h (revision 268)
@@ -195,6 +195,4 @@
         Corpse* ConvertCorpseForPlayer(uint64 player_guid);
 
-        bool ActiveObjectsNearGrid(uint32 x,uint32 y,uint32 m_id,uint32 i_id) const;
-
         static void UpdateObject(Object* obj, Player* exceptPlayer);
         static void _buildUpdateObject(Object* obj, UpdateDataMapType &);
@@ -202,7 +200,4 @@
         static void UpdateObjectVisibility(WorldObject* obj);
         static void UpdateVisibilityForPlayer(Player* player);
-
-        void AddActiveObject(WorldObject*);
-        void RemoveActiveObject(WorldObject*);
     private:
         struct WorldObjectChangeAccumulator
@@ -229,5 +224,4 @@
         void _update(void);
         std::set<Object *> i_objects;
-        std::set<WorldObject *> i_activeobjects;
         LockType i_playerGuard;
         LockType i_updateGuard;
Index: /trunk/src/game/Map.cpp
===================================================================
--- /trunk/src/game/Map.cpp (revision 266)
+++ /trunk/src/game/Map.cpp (revision 268)
@@ -595,10 +595,13 @@
 }
 
-void Map::Update(const uint32 &t_diff)
-{
-    // TODO: need have an active object list for every map
-
-    /*resetMarkedCells();
-
+void Map::UpdateActiveCells(const float &x, const float &y, const uint32 &t_diff)
+{
+    CellPair standing_cell(Trinity::ComputeCellPair(x, y));
+
+    // Check for correctness of standing_cell, it also avoids problems with update_cell
+    if (standing_cell.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || standing_cell.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP)
+        return;
+
+    // will this reduce the speed?
     Trinity::ObjectUpdater updater(t_diff);
     // for creature
@@ -607,43 +610,52 @@
     TypeContainerVisitor<Trinity::ObjectUpdater, WorldTypeMapContainer > world_object_update(updater);
 
+    // the overloaded operators handle range checking
+    // so ther's no need for range checking inside the loop
+    CellPair begin_cell(standing_cell), end_cell(standing_cell);
+    begin_cell << 1; begin_cell -= 1;               // upper left
+    end_cell >> 1; end_cell += 1;                   // lower right
+
+    for(uint32 x = begin_cell.x_coord; x <= end_cell.x_coord; ++x)
+    {
+        for(uint32 y = begin_cell.y_coord; y <= end_cell.y_coord; ++y)
+        {
+            // marked cells are those that have been visited
+            // don't visit the same cell twice
+            uint32 cell_id = (y * TOTAL_NUMBER_OF_CELLS_PER_MAP) + x;
+            if(!isCellMarked(cell_id))
+            {
+                markCell(cell_id);
+                CellPair pair(x,y);
+                Cell cell(pair);
+                cell.data.Part.reserved = CENTER_DISTRICT;
+                cell.SetNoCreate();
+                CellLock<NullGuard> cell_lock(cell, pair);
+                cell_lock->Visit(cell_lock, grid_object_update,  *this);
+                cell_lock->Visit(cell_lock, world_object_update, *this);
+            }
+        }
+    }
+}
+
+void Map::Update(const uint32 &t_diff)
+{
+    resetMarkedCells();
+
+    // update cells around players
     for(MapRefManager::iterator iter = m_mapRefManager.begin(); iter != m_mapRefManager.end(); ++iter)
     {
         Player* plr = iter->getSource();
-        if(!plr->IsInWorld())
-            continue;
-
-        CellPair standing_cell(Trinity::ComputeCellPair(plr->GetPositionX(), plr->GetPositionY()));
-
-        // Check for correctness of standing_cell, it also avoids problems with update_cell
-        if (standing_cell.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || standing_cell.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP)
-            continue;
-
-        // the overloaded operators handle range checking
-        // so ther's no need for range checking inside the loop
-        CellPair begin_cell(standing_cell), end_cell(standing_cell);
-        begin_cell << 1; begin_cell -= 1;               // upper left
-        end_cell >> 1; end_cell += 1;                   // lower right
-
-        for(uint32 x = begin_cell.x_coord; x <= end_cell.x_coord; ++x)
-        {
-            for(uint32 y = begin_cell.y_coord; y <= end_cell.y_coord; ++y)
-            {
-                // marked cells are those that have been visited
-                // don't visit the same cell twice
-                uint32 cell_id = (y * TOTAL_NUMBER_OF_CELLS_PER_MAP) + x;
-                if(!isCellMarked(cell_id))
-                {
-                    markCell(cell_id);
-                    CellPair pair(x,y);
-                    Cell cell(pair);
-                    cell.data.Part.reserved = CENTER_DISTRICT;
-                    cell.SetNoCreate();
-                    CellLock<NullGuard> cell_lock(cell, pair);
-                    cell_lock->Visit(cell_lock, grid_object_update,  *this);
-                    cell_lock->Visit(cell_lock, world_object_update, *this);
-                }
-            }
-        }
-    }*/
+        if(plr->IsInWorld())
+            UpdateActiveCells(plr->GetPositionX(), plr->GetPositionY(), t_diff);
+    }
+
+    // update cells around active objects
+    // clone the active object list, because update might remove from it
+    std::set<WorldObject *> activeObjects(i_activeObjects);
+    for(std::set<WorldObject *>::iterator iter = activeObjects.begin(); iter != activeObjects.end(); ++iter)
+    {
+        if((*iter)->IsInWorld())
+            UpdateActiveCells((*iter)->GetPositionX(), (*iter)->GetPositionY(), t_diff);
+    }
 
     // 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 !
@@ -814,6 +826,11 @@
         AddCreatureToMoveList(creature,x,y,z,ang);
         // in diffcell/diffgrid case notifiers called at finishing move creature in Map::MoveAllCreaturesInMoveList
-        if(creature->isPossessedByPlayer())
-            EnsureGridLoadedForPlayer(new_cell, (Player*)creature->GetCharmer(), false);
+        if(creature->isActive())
+        {
+            if(creature->isPossessedByPlayer())
+                EnsureGridLoadedForPlayer(new_cell, (Player*)creature->GetCharmer(), false);
+            else
+                EnsureGridLoadedForPlayer(new_cell, NULL, false);
+        }
     }
     else
@@ -1502,4 +1519,34 @@
 
         CellPair p = Trinity::ComputeCellPair(plr->GetPositionX(), plr->GetPositionY());
+        if( (cell_min.x_coord <= p.x_coord && p.x_coord <= cell_max.x_coord) &&
+            (cell_min.y_coord <= p.y_coord && p.y_coord <= cell_max.y_coord) )
+            return true;
+    }
+
+    return false;
+}
+
+bool Map::ActiveObjectsNearGrid(uint32 x, uint32 y) const
+{
+    CellPair cell_min(x*MAX_NUMBER_OF_CELLS, y*MAX_NUMBER_OF_CELLS);
+    CellPair cell_max(cell_min.x_coord + MAX_NUMBER_OF_CELLS, cell_min.y_coord+MAX_NUMBER_OF_CELLS);
+    cell_min << 2;
+    cell_min -= 2;
+    cell_max >> 2;
+    cell_max += 2;
+
+    for(MapRefManager::const_iterator iter = m_mapRefManager.begin(); iter != m_mapRefManager.end(); ++iter)
+    {
+        Player* plr = iter->getSource();
+
+        CellPair p = Trinity::ComputeCellPair(plr->GetPositionX(), plr->GetPositionY());
+        if( (cell_min.x_coord <= p.x_coord && p.x_coord <= cell_max.x_coord) &&
+            (cell_min.y_coord <= p.y_coord && p.y_coord <= cell_max.y_coord) )
+            return true;
+    }
+
+    for(std::set<WorldObject*>::const_iterator itr = i_activeObjects.begin(); itr != i_activeObjects.end(); ++itr)
+    {
+        CellPair p = Trinity::ComputeCellPair((*itr)->GetPositionX(), (*itr)->GetPositionY());
         if( (cell_min.x_coord <= p.x_coord && p.x_coord <= cell_max.x_coord) &&
             (cell_min.y_coord <= p.y_coord && p.y_coord <= cell_max.y_coord) )
Index: /trunk/src/game/Object.cpp
===================================================================
--- /trunk/src/game/Object.cpp (revision 230)
+++ /trunk/src/game/Object.cpp (revision 268)
@@ -991,6 +991,6 @@
 WorldObject::~WorldObject()
 {
-    if(m_isActive && IsInWorld())
-        ObjectAccessor::Instance().RemoveActiveObject(this);
+    if(m_isActive && !isType(TYPEMASK_PLAYER) && IsInWorld())
+        GetMap()->RemoveActiveObject(this);
 }
 
@@ -998,13 +998,14 @@
 {
     // if already in the same activity state as we try to set, do nothing
-    if(isActive == m_isActive)
+    if(isActive == m_isActive || isType(TYPEMASK_PLAYER))
         return;
+
     m_isActive = isActive;
     if(IsInWorld())
     {
         if(isActive)
-            ObjectAccessor::Instance().AddActiveObject(this);
+            GetMap()->AddActiveObject(this);
         else
-            ObjectAccessor::Instance().RemoveActiveObject(this);
+            GetMap()->RemoveActiveObject(this);
     }
 }
@@ -1013,12 +1014,12 @@
 {
     Object::AddToWorld();
-    if(m_isActive)
-        ObjectAccessor::Instance().AddActiveObject(this);
+    if(m_isActive && !isType(TYPEMASK_PLAYER))
+        GetMap()->AddActiveObject(this);
 }
 
 void WorldObject::RemoveFromWorld()
 {
-    if(m_isActive)
-        ObjectAccessor::Instance().RemoveActiveObject(this);
+    if(m_isActive && !isType(TYPEMASK_PLAYER))
+        GetMap()->RemoveActiveObject(this);
     Object::RemoveFromWorld();
 }
Index: /trunk/src/game/GridStates.cpp
===================================================================
--- /trunk/src/game/GridStates.cpp (revision 120)
+++ /trunk/src/game/GridStates.cpp (revision 268)
@@ -11,10 +11,10 @@
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
@@ -37,5 +37,5 @@
     if( info.getTimeTracker().Passed() )
     {
-        if( grid.ActiveObjectsInGrid() == 0 && !ObjectAccessor::Instance().ActiveObjectsNearGrid(x, y, m.GetId(), m.GetInstanceId()) )
+        if( grid.ActiveObjectsInGrid() == 0 && !m.ActiveObjectsNearGrid(x, y) )
         {
             ObjectGridStoper stoper(grid);
@@ -51,5 +51,5 @@
 
 void
-IdleState::Update(Map &m, NGridType &grid, GridInfo &info, const uint32 &x, const uint32 &y, const uint32 &) const
+IdleState::Update(Map &m, NGridType &grid, GridInfo &, const uint32 &x, const uint32 &y, const uint32 &) const
 {
     m.ResetGridExpiry(grid);
Index: /trunk/src/game/Player.h
===================================================================
--- /trunk/src/game/Player.h (revision 257)
+++ /trunk/src/game/Player.h (revision 268)
@@ -898,6 +898,4 @@
         void AddToWorld();
         void RemoveFromWorld();
-        // always active
-        void setActive(bool) {}
 
         void SetViewport(uint64 guid, bool movable);
Index: /trunk/src/game/ObjectAccessor.cpp
===================================================================
--- /trunk/src/game/ObjectAccessor.cpp (revision 266)
+++ /trunk/src/game/ObjectAccessor.cpp (revision 268)
@@ -488,22 +488,8 @@
 
 void
-ObjectAccessor::AddActiveObject( WorldObject * obj )
-{
-    i_activeobjects.insert(obj);
-}
-
-void
-ObjectAccessor::RemoveActiveObject( WorldObject * obj )
-{
-    i_activeobjects.erase(obj);
-}
-
-void
 ObjectAccessor::Update(uint32 diff)
 {
-
-    {
+/*    {
         //Player update now in MapManager -> UpdatePlayers
-        /*
         // player update might remove the player from grid, and that causes crashes. We HAVE to update players first, and then the active objects.
         HashMapHolder<Player>::MapType& playerMap = HashMapHolder<Player>::GetContainer();
@@ -514,5 +500,5 @@
                 iter->second->Update(diff);
             }
-        }*/
+        }
 
         // TODO: move this to Map::Update
@@ -575,5 +561,5 @@
             }
         }
-    }
+    }*/
 
     UpdateDataMapType update_players;
@@ -609,28 +595,4 @@
 }
 
-bool
-ObjectAccessor::ActiveObjectsNearGrid(uint32 x, uint32 y, uint32 m_id, uint32 i_id) const
-{
-    CellPair cell_min(x*MAX_NUMBER_OF_CELLS, y*MAX_NUMBER_OF_CELLS);
-    CellPair cell_max(cell_min.x_coord + MAX_NUMBER_OF_CELLS, cell_min.y_coord+MAX_NUMBER_OF_CELLS);
-    cell_min << 2;
-    cell_min -= 2;
-    cell_max >> 2;
-    cell_max += 2;
-
-    for(std::set<WorldObject*>::const_iterator itr = i_activeobjects.begin(); itr != i_activeobjects.end(); ++itr)
-    {
-        if( m_id != (*itr)->GetMapId() || i_id != (*itr)->GetInstanceId() )
-            continue;
-
-        CellPair p = Trinity::ComputeCellPair((*itr)->GetPositionX(), (*itr)->GetPositionY());
-        if( (cell_min.x_coord <= p.x_coord && p.x_coord <= cell_max.x_coord) &&
-            (cell_min.y_coord <= p.y_coord && p.y_coord <= cell_max.y_coord) )
-            return true;
-    }
-
-    return false;
-}
-
 void
 ObjectAccessor::WorldObjectChangeAccumulator::Visit(PlayerMapType &m)
