From ec244dbe366e84a93c8fa1ef294af4a2e4e3b0b1 Mon Sep 17 00:00:00 2001 From: Xanadu Date: Sat, 17 Jul 2010 02:35:14 +0200 Subject: Correctly redo file moves/renames from rev. 8452. --HG-- branch : trunk rename : src/server/game/Grids/Cell.h => src/server/game/Grids/Cells/Cell.h rename : src/server/game/Grids/CellImpl.h => src/server/game/Grids/Cells/CellImpl.h rename : src/server/framework/GameSystem/Grid.h => src/server/game/Grids/Grid.h rename : src/server/framework/GameSystem/GridLoader.h => src/server/game/Grids/GridLoader.h rename : src/server/framework/GameSystem/GridRefManager.h => src/server/game/Grids/GridRefManager.h rename : src/server/framework/GameSystem/GridReference.h => src/server/game/Grids/GridReference.h rename : src/server/framework/GameSystem/NGrid.h => src/server/game/Grids/NGrid.h rename : src/server/game/Grids/GridNotifiers.cpp => src/server/game/Grids/Notifiers/GridNotifiers.cpp rename : src/server/game/Grids/GridNotifiers.h => src/server/game/Grids/Notifiers/GridNotifiers.h rename : src/server/game/Grids/GridNotifiersImpl.h => src/server/game/Grids/Notifiers/GridNotifiersImpl.h rename : src/server/framework/Platform/CompilerDefs.h => src/server/shared/CompilerDefs.h rename : src/server/framework/Platform/Define.h => src/server/shared/Define.h rename : src/server/framework/Utilities/CountedReference/Reference.h => src/server/shared/Dynamic/CountedReference/Reference.h rename : src/server/framework/Utilities/CountedReference/ReferenceHolder.h => src/server/shared/Dynamic/CountedReference/ReferenceHolder.h rename : src/server/framework/Utilities/CountedReference/ReferenceImpl.h => src/server/shared/Dynamic/CountedReference/ReferenceImpl.h rename : src/server/framework/Dynamic/FactoryHolder.h => src/server/shared/Dynamic/FactoryHolder.h rename : src/server/framework/Utilities/LinkedList.h => src/server/shared/Dynamic/LinkedList.h rename : src/server/framework/Utilities/LinkedReference/RefManager.h => src/server/shared/Dynamic/LinkedReference/RefManager.h rename : src/server/framework/Utilities/LinkedReference/Reference.h => src/server/shared/Dynamic/LinkedReference/Reference.h rename : src/server/framework/Dynamic/ObjectRegistry.h => src/server/shared/Dynamic/ObjectRegistry.h rename : src/server/framework/GameSystem/TypeContainer.h => src/server/shared/Dynamic/TypeContainer.h rename : src/server/framework/GameSystem/TypeContainerFunctions.h => src/server/shared/Dynamic/TypeContainerFunctions.h rename : src/server/framework/GameSystem/TypeContainerFunctionsPtr.h => src/server/shared/Dynamic/TypeContainerFunctionsPtr.h rename : src/server/framework/GameSystem/TypeContainerVisitor.h => src/server/shared/Dynamic/TypeContainerVisitor.h rename : src/server/framework/Utilities/TypeList.h => src/server/shared/Dynamic/TypeList.h rename : src/server/framework/Utilities/UnorderedMap.h => src/server/shared/Dynamic/UnorderedMap.h rename : src/server/framework/Policies/CreationPolicy.h => src/server/shared/Policies/CreationPolicy.h rename : src/server/framework/Policies/ObjectLifeTime.cpp => src/server/shared/Policies/ObjectLifeTime.cpp rename : src/server/framework/Policies/ObjectLifeTime.h => src/server/shared/Policies/ObjectLifeTime.h rename : src/server/framework/Policies/Singleton.h => src/server/shared/Policies/Singleton.h rename : src/server/framework/Policies/SingletonImp.h => src/server/shared/Policies/SingletonImp.h rename : src/server/framework/Policies/ThreadingModel.h => src/server/shared/Policies/ThreadingModel.h rename : src/server/framework/Utilities/Callback.h => src/server/shared/Threading/Callback.h rename : src/server/framework/Utilities/ByteConverter.h => src/server/shared/Utilities/ByteConverter.h rename : src/server/framework/Utilities/EventProcessor.cpp => src/server/shared/Utilities/EventProcessor.cpp rename : src/server/framework/Utilities/EventProcessor.h => src/server/shared/Utilities/EventProcessor.h rename : src/server/framework/Network/SocketDefines.h => src/server/worldserver/RemoteAccess/SocketDefines.h --- src/server/framework/Dynamic/FactoryHolder.h | 63 - src/server/framework/Dynamic/ObjectRegistry.h | 111 -- src/server/framework/GameSystem/Grid.h | 143 --- src/server/framework/GameSystem/GridLoader.h | 79 -- src/server/framework/GameSystem/GridRefManager.h | 44 - src/server/framework/GameSystem/GridReference.h | 55 - src/server/framework/GameSystem/NGrid.h | 189 --- src/server/framework/GameSystem/TypeContainer.h | 126 -- .../framework/GameSystem/TypeContainerFunctions.h | 116 -- .../GameSystem/TypeContainerFunctionsPtr.h | 170 --- .../framework/GameSystem/TypeContainerVisitor.h | 119 -- src/server/framework/Network/SocketDefines.h | 49 - src/server/framework/Platform/CompilerDefs.h | 64 - src/server/framework/Platform/Define.h | 85 -- src/server/framework/Policies/CreationPolicy.h | 110 -- src/server/framework/Policies/ObjectLifeTime.cpp | 36 - src/server/framework/Policies/ObjectLifeTime.h | 53 - src/server/framework/Policies/Singleton.h | 65 -- src/server/framework/Policies/SingletonImp.h | 93 -- src/server/framework/Policies/ThreadingModel.h | 130 --- src/server/framework/Utilities/ByteConverter.h | 66 -- src/server/framework/Utilities/Callback.h | 386 ------ .../Utilities/CountedReference/Reference.h | 100 -- .../Utilities/CountedReference/ReferenceHolder.h | 42 - .../Utilities/CountedReference/ReferenceImpl.h | 133 --- src/server/framework/Utilities/EventProcessor.cpp | 101 -- src/server/framework/Utilities/EventProcessor.h | 73 -- src/server/framework/Utilities/LinkedList.h | 247 ---- .../Utilities/LinkedReference/RefManager.h | 58 - .../Utilities/LinkedReference/Reference.h | 94 -- src/server/framework/Utilities/TypeList.h | 46 - src/server/framework/Utilities/UnorderedMap.h | 73 -- src/server/game/Grids/Cell.h | 177 --- src/server/game/Grids/CellImpl.h | 297 ----- src/server/game/Grids/Cells/Cell.h | 177 +++ src/server/game/Grids/Cells/CellImpl.h | 297 +++++ src/server/game/Grids/Grid.h | 143 +++ src/server/game/Grids/GridLoader.h | 79 ++ src/server/game/Grids/GridNotifiers.cpp | 353 ------ src/server/game/Grids/GridNotifiers.h | 1232 -------------------- src/server/game/Grids/GridNotifiersImpl.h | 453 ------- src/server/game/Grids/GridRefManager.h | 44 + src/server/game/Grids/GridReference.h | 55 + src/server/game/Grids/NGrid.h | 189 +++ src/server/game/Grids/Notifiers/GridNotifiers.cpp | 353 ++++++ src/server/game/Grids/Notifiers/GridNotifiers.h | 1232 ++++++++++++++++++++ .../game/Grids/Notifiers/GridNotifiersImpl.h | 453 +++++++ src/server/shared/CompilerDefs.h | 64 + src/server/shared/Define.h | 85 ++ .../shared/Dynamic/CountedReference/Reference.h | 100 ++ .../Dynamic/CountedReference/ReferenceHolder.h | 42 + .../Dynamic/CountedReference/ReferenceImpl.h | 133 +++ src/server/shared/Dynamic/FactoryHolder.h | 63 + src/server/shared/Dynamic/LinkedList.h | 247 ++++ .../shared/Dynamic/LinkedReference/RefManager.h | 58 + .../shared/Dynamic/LinkedReference/Reference.h | 94 ++ src/server/shared/Dynamic/ObjectRegistry.h | 111 ++ src/server/shared/Dynamic/TypeContainer.h | 126 ++ src/server/shared/Dynamic/TypeContainerFunctions.h | 116 ++ .../shared/Dynamic/TypeContainerFunctionsPtr.h | 170 +++ src/server/shared/Dynamic/TypeContainerVisitor.h | 119 ++ src/server/shared/Dynamic/TypeList.h | 46 + src/server/shared/Dynamic/UnorderedMap.h | 73 ++ src/server/shared/Policies/CreationPolicy.h | 110 ++ src/server/shared/Policies/ObjectLifeTime.cpp | 36 + src/server/shared/Policies/ObjectLifeTime.h | 53 + src/server/shared/Policies/Singleton.h | 65 ++ src/server/shared/Policies/SingletonImp.h | 93 ++ src/server/shared/Policies/ThreadingModel.h | 130 +++ src/server/shared/Threading/Callback.h | 386 ++++++ src/server/shared/Utilities/ByteConverter.h | 66 ++ src/server/shared/Utilities/EventProcessor.cpp | 101 ++ src/server/shared/Utilities/EventProcessor.h | 73 ++ .../worldserver/RemoteAccess/SocketDefines.h | 49 + 74 files changed, 5831 insertions(+), 5831 deletions(-) delete mode 100644 src/server/framework/Dynamic/FactoryHolder.h delete mode 100644 src/server/framework/Dynamic/ObjectRegistry.h delete mode 100644 src/server/framework/GameSystem/Grid.h delete mode 100644 src/server/framework/GameSystem/GridLoader.h delete mode 100644 src/server/framework/GameSystem/GridRefManager.h delete mode 100644 src/server/framework/GameSystem/GridReference.h delete mode 100644 src/server/framework/GameSystem/NGrid.h delete mode 100644 src/server/framework/GameSystem/TypeContainer.h delete mode 100644 src/server/framework/GameSystem/TypeContainerFunctions.h delete mode 100644 src/server/framework/GameSystem/TypeContainerFunctionsPtr.h delete mode 100644 src/server/framework/GameSystem/TypeContainerVisitor.h delete mode 100644 src/server/framework/Network/SocketDefines.h delete mode 100644 src/server/framework/Platform/CompilerDefs.h delete mode 100644 src/server/framework/Platform/Define.h delete mode 100644 src/server/framework/Policies/CreationPolicy.h delete mode 100644 src/server/framework/Policies/ObjectLifeTime.cpp delete mode 100644 src/server/framework/Policies/ObjectLifeTime.h delete mode 100644 src/server/framework/Policies/Singleton.h delete mode 100644 src/server/framework/Policies/SingletonImp.h delete mode 100644 src/server/framework/Policies/ThreadingModel.h delete mode 100644 src/server/framework/Utilities/ByteConverter.h delete mode 100644 src/server/framework/Utilities/Callback.h delete mode 100644 src/server/framework/Utilities/CountedReference/Reference.h delete mode 100644 src/server/framework/Utilities/CountedReference/ReferenceHolder.h delete mode 100644 src/server/framework/Utilities/CountedReference/ReferenceImpl.h delete mode 100644 src/server/framework/Utilities/EventProcessor.cpp delete mode 100644 src/server/framework/Utilities/EventProcessor.h delete mode 100644 src/server/framework/Utilities/LinkedList.h delete mode 100644 src/server/framework/Utilities/LinkedReference/RefManager.h delete mode 100644 src/server/framework/Utilities/LinkedReference/Reference.h delete mode 100644 src/server/framework/Utilities/TypeList.h delete mode 100644 src/server/framework/Utilities/UnorderedMap.h delete mode 100644 src/server/game/Grids/Cell.h delete mode 100644 src/server/game/Grids/CellImpl.h create mode 100644 src/server/game/Grids/Cells/Cell.h create mode 100644 src/server/game/Grids/Cells/CellImpl.h create mode 100644 src/server/game/Grids/Grid.h create mode 100644 src/server/game/Grids/GridLoader.h delete mode 100644 src/server/game/Grids/GridNotifiers.cpp delete mode 100644 src/server/game/Grids/GridNotifiers.h delete mode 100644 src/server/game/Grids/GridNotifiersImpl.h create mode 100644 src/server/game/Grids/GridRefManager.h create mode 100644 src/server/game/Grids/GridReference.h create mode 100644 src/server/game/Grids/NGrid.h create mode 100644 src/server/game/Grids/Notifiers/GridNotifiers.cpp create mode 100644 src/server/game/Grids/Notifiers/GridNotifiers.h create mode 100644 src/server/game/Grids/Notifiers/GridNotifiersImpl.h create mode 100644 src/server/shared/CompilerDefs.h create mode 100644 src/server/shared/Define.h create mode 100644 src/server/shared/Dynamic/CountedReference/Reference.h create mode 100644 src/server/shared/Dynamic/CountedReference/ReferenceHolder.h create mode 100644 src/server/shared/Dynamic/CountedReference/ReferenceImpl.h create mode 100644 src/server/shared/Dynamic/FactoryHolder.h create mode 100644 src/server/shared/Dynamic/LinkedList.h create mode 100644 src/server/shared/Dynamic/LinkedReference/RefManager.h create mode 100644 src/server/shared/Dynamic/LinkedReference/Reference.h create mode 100644 src/server/shared/Dynamic/ObjectRegistry.h create mode 100644 src/server/shared/Dynamic/TypeContainer.h create mode 100644 src/server/shared/Dynamic/TypeContainerFunctions.h create mode 100644 src/server/shared/Dynamic/TypeContainerFunctionsPtr.h create mode 100644 src/server/shared/Dynamic/TypeContainerVisitor.h create mode 100644 src/server/shared/Dynamic/TypeList.h create mode 100644 src/server/shared/Dynamic/UnorderedMap.h create mode 100644 src/server/shared/Policies/CreationPolicy.h create mode 100644 src/server/shared/Policies/ObjectLifeTime.cpp create mode 100644 src/server/shared/Policies/ObjectLifeTime.h create mode 100644 src/server/shared/Policies/Singleton.h create mode 100644 src/server/shared/Policies/SingletonImp.h create mode 100644 src/server/shared/Policies/ThreadingModel.h create mode 100644 src/server/shared/Threading/Callback.h create mode 100644 src/server/shared/Utilities/ByteConverter.h create mode 100644 src/server/shared/Utilities/EventProcessor.cpp create mode 100644 src/server/shared/Utilities/EventProcessor.h create mode 100644 src/server/worldserver/RemoteAccess/SocketDefines.h (limited to 'src') diff --git a/src/server/framework/Dynamic/FactoryHolder.h b/src/server/framework/Dynamic/FactoryHolder.h deleted file mode 100644 index 282968d6097..00000000000 --- a/src/server/framework/Dynamic/FactoryHolder.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_FACTORY_HOLDER -#define TRINITY_FACTORY_HOLDER - -#include "Platform/Define.h" -#include "Utilities/TypeList.h" -#include "ObjectRegistry.h" -#include "Policies/SingletonImp.h" - -/** FactoryHolder holds a factory object of a specific type - */ -template -class FactoryHolder -{ - public: - typedef ObjectRegistry, Key > FactoryHolderRegistry; - typedef Trinity::Singleton FactoryHolderRepository; - - FactoryHolder(Key k) : i_key(k) {} - virtual ~FactoryHolder() {} - inline Key key() const { return i_key; } - - void RegisterSelf(void) { FactoryHolderRepository::Instance().InsertItem(this, i_key); } - void DeregisterSelf(void) { FactoryHolderRepository::Instance().RemoveItem(this, false); } - - /// Abstract Factory create method - virtual T* Create(void *data = NULL) const = 0; - private: - Key i_key; -}; - -/** Permissible is a classic way of letting the object decide - * whether how good they handle things. This is not retricted - * to factory selectors. - */ -template -class Permissible -{ - public: - virtual ~Permissible() {} - virtual int Permit(const T *) const = 0; -}; -#endif - diff --git a/src/server/framework/Dynamic/ObjectRegistry.h b/src/server/framework/Dynamic/ObjectRegistry.h deleted file mode 100644 index e6619427885..00000000000 --- a/src/server/framework/Dynamic/ObjectRegistry.h +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_OBJECTREGISTRY_H -#define TRINITY_OBJECTREGISTRY_H - -#include "Platform/Define.h" -#include "Utilities/UnorderedMap.h" -#include "Policies/Singleton.h" - -#include -#include -#include - -/** ObjectRegistry holds all registry item of the same type - */ -template -class ObjectRegistry -{ - public: - typedef std::map RegistryMapType; - - /// Returns a registry item - const T* GetRegistryItem(Key key) const - { - typename RegistryMapType::const_iterator iter = i_registeredObjects.find(key); - return( iter == i_registeredObjects.end() ? NULL : iter->second ); - } - - /// Inserts a registry item - bool InsertItem(T *obj, Key key, bool override = false) - { - typename RegistryMapType::iterator iter = i_registeredObjects.find(key); - if( iter != i_registeredObjects.end() ) - { - if( !override ) - return false; - delete iter->second; - i_registeredObjects.erase(iter); - } - - i_registeredObjects[key] = obj; - return true; - } - - /// Removes a registry item - void RemoveItem(Key key, bool delete_object = true) - { - typename RegistryMapType::iterator iter = i_registeredObjects.find(key); - if( iter != i_registeredObjects.end() ) - { - if( delete_object ) - delete iter->second; - i_registeredObjects.erase(iter); - } - } - - /// Returns true if registry contains an item - bool HasItem(Key key) const - { - return (i_registeredObjects.find(key) != i_registeredObjects.end()); - } - - /// Inefficiently return a vector of registered items - unsigned int GetRegisteredItems(std::vector &l) const - { - unsigned int sz = l.size(); - l.resize(sz + i_registeredObjects.size()); - for (typename RegistryMapType::const_iterator iter = i_registeredObjects.begin(); iter != i_registeredObjects.end(); ++iter) - l[sz++] = iter->first; - return i_registeredObjects.size(); - } - - /// Return the map of registered items - RegistryMapType const &GetRegisteredItems() const - { - return i_registeredObjects; - } - - private: - RegistryMapType i_registeredObjects; - friend class Trinity::OperatorNew >; - - // protected for friend use since it should be a singleton - ObjectRegistry() {} - ~ObjectRegistry() - { - for (typename RegistryMapType::iterator iter=i_registeredObjects.begin(); iter != i_registeredObjects.end(); ++iter) - delete iter->second; - i_registeredObjects.clear(); - } -}; -#endif - diff --git a/src/server/framework/GameSystem/Grid.h b/src/server/framework/GameSystem/Grid.h deleted file mode 100644 index 65bf3c92f9d..00000000000 --- a/src/server/framework/GameSystem/Grid.h +++ /dev/null @@ -1,143 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_GRID_H -#define TRINITY_GRID_H - -/* - @class Grid - Grid is a logical segment of the game world represented inside TrinIty. - Grid is bind at compile time to a particular type of object which - we call it the object of interested. There are many types of loader, - specially, dynamic loader, static loader, or on-demand loader. There's - a subtle difference between dynamic loader and on-demand loader but - this is implementation specific to the loader class. From the - Grid's perspective, the loader meets its API requirement is suffice. -*/ - -#include "Platform/Define.h" -#include "Policies/ThreadingModel.h" -#include "TypeContainer.h" -#include "TypeContainerVisitor.h" - -// forward declaration -template class GridLoader; - -template -< -class ACTIVE_OBJECT, -class WORLD_OBJECT_TYPES, -class GRID_OBJECT_TYPES, -class ThreadModel = Trinity::SingleThreaded -> -class Grid -{ - // allows the GridLoader to access its internals - template friend class GridLoader; - public: - - /** destructor to clean up its resources. This includes unloading the - grid if it has not been unload. - */ - ~Grid() {} - - /** an object of interested enters the grid - */ - template void AddWorldObject(SPECIFIC_OBJECT *obj) - { - if(!i_objects.template insert(obj)) - assert(false); - } - - /** an object of interested exits the grid - */ - template void RemoveWorldObject(SPECIFIC_OBJECT *obj) - { - if(!i_objects.template remove(obj)) - assert(false); - } - - /** Refreshes/update the grid. This required for remote grids. - */ - void RefreshGrid(void) { /* TBI */} - - /** Locks a grid. Any object enters must wait until the grid is unlock. - */ - void LockGrid(void) { /* TBI */ } - - /** Unlocks the grid. - */ - void UnlockGrid(void) { /* TBI */ } - - /** Grid visitor for grid objects - */ - template void Visit(TypeContainerVisitor > &visitor) - { - visitor.Visit(i_container); - } - - /** Grid visitor for world objects - */ - template void Visit(TypeContainerVisitor > &visitor) - { - visitor.Visit(i_objects); - } - - /** Returns the number of object within the grid. - */ - unsigned int ActiveObjectsInGrid(void) const { return /*m_activeGridObjects.size()+*/i_objects.template Count(); } - - /** Inserts a container type object into the grid. - */ - template void AddGridObject(SPECIFIC_OBJECT *obj) - { - if(!i_container.template insert(obj)) - assert(false); - } - - /** Removes a containter type object from the grid - */ - template void RemoveGridObject(SPECIFIC_OBJECT *obj) - { - if(!i_container.template remove(obj)) - assert(false); - } - - /*bool NoWorldObjectInGrid() const - { - return i_objects.GetElements().isEmpty(); - } - - bool NoGridObjectInGrid() const - { - return i_container.GetElements().isEmpty(); - }*/ - private: - - typedef typename ThreadModel::Lock Guard; - typedef typename ThreadModel::VolatileType VolatileType; - - TypeMapContainer i_container; - TypeMapContainer i_objects; - //typedef std::set ActiveGridObjects; - //ActiveGridObjects m_activeGridObjects; -}; -#endif - diff --git a/src/server/framework/GameSystem/GridLoader.h b/src/server/framework/GameSystem/GridLoader.h deleted file mode 100644 index 03fa0f5b813..00000000000 --- a/src/server/framework/GameSystem/GridLoader.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_GRIDLOADER_H -#define TRINITY_GRIDLOADER_H - -/** - @class GridLoader - The GridLoader is working in conjuction with the Grid and responsible - for loading and unloading object-types (one or more) when objects - enters a grid. Unloading is scheduled and might be canceled if - an interested object re-enters. GridLoader does not do the actuall - loading and unloading but implements as a template pattern that - delicate its loading and unloading for the actualy loader and unloader. - GridLoader manages the grid (both local and remote). - */ - -#include "Platform/Define.h" -#include "Grid.h" -#include "TypeContainerVisitor.h" - -template -< -class ACTIVE_OBJECT, -class WORLD_OBJECT_TYPES, -class GRID_OBJECT_TYPES -> -class GridLoader -{ - public: - - /** Loads the grid - */ - template - void Load(Grid &grid, LOADER &loader) - { - grid.LockGrid(); - loader.Load(grid); - grid.UnlockGrid(); - } - - /** Stop the grid - */ - template - void Stop(Grid &grid, STOPER &stoper) - { - grid.LockGrid(); - stoper.Stop(grid); - grid.UnlockGrid(); - } - /** Unloads the grid - */ - template - void Unload(Grid &grid, UNLOADER &unloader) - { - grid.LockGrid(); - unloader.Unload(grid); - grid.UnlockGrid(); - } -}; -#endif - diff --git a/src/server/framework/GameSystem/GridRefManager.h b/src/server/framework/GameSystem/GridRefManager.h deleted file mode 100644 index 79799105fb7..00000000000 --- a/src/server/framework/GameSystem/GridRefManager.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef _GRIDREFMANAGER -#define _GRIDREFMANAGER - -#include "Utilities/LinkedReference/RefManager.h" - -template -class GridReference; - -template -class GridRefManager : public RefManager, OBJECT> -{ - public: - typedef LinkedListHead::Iterator< GridReference > iterator; - - GridReference* getFirst() { return (GridReference*)RefManager, OBJECT>::getFirst(); } - GridReference* getLast() { return (GridReference*)RefManager, OBJECT>::getLast(); } - - iterator begin() { return iterator(getFirst()); } - iterator end() { return iterator(NULL); } - iterator rbegin() { return iterator(getLast()); } - iterator rend() { return iterator(NULL); } -}; -#endif - diff --git a/src/server/framework/GameSystem/GridReference.h b/src/server/framework/GameSystem/GridReference.h deleted file mode 100644 index d2e3a455895..00000000000 --- a/src/server/framework/GameSystem/GridReference.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef _GRIDREFERENCE_H -#define _GRIDREFERENCE_H - -#include "Utilities/LinkedReference/Reference.h" - -template -class GridRefManager; - -template -class GridReference : public Reference, OBJECT> -{ - protected: - void targetObjectBuildLink() - { - // called from link() - this->getTarget()->insertFirst(this); - this->getTarget()->incSize(); - } - void targetObjectDestroyLink() - { - // called from unlink() - if(this->isValid()) this->getTarget()->decSize(); - } - void sourceObjectDestroyLink() - { - // called from invalidate() - this->getTarget()->decSize(); - } - public: - GridReference() : Reference, OBJECT>() {} - ~GridReference() { this->unlink(); } - GridReference *next() { return (GridReference*)Reference, OBJECT>::next(); } -}; -#endif - diff --git a/src/server/framework/GameSystem/NGrid.h b/src/server/framework/GameSystem/NGrid.h deleted file mode 100644 index 3810286e123..00000000000 --- a/src/server/framework/GameSystem/NGrid.h +++ /dev/null @@ -1,189 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_NGRID_H -#define TRINITY_NGRID_H - -/** NGrid is nothing more than a wrapper of the Grid with an NxN cells - */ - -#include "GameSystem/Grid.h" -#include "GameSystem/GridReference.h" -#include "Timer.h" -#include "Util.h" - -#define DEFAULT_VISIBILITY_NOTIFY_PERIOD 1000 - -class GridInfo -{ -public: - GridInfo() - : i_timer(0), i_unloadActiveLockCount(0), i_unloadExplicitLock(false), i_unloadReferenceLock(false), - vis_Update(0, irand(0,DEFAULT_VISIBILITY_NOTIFY_PERIOD)) {} - GridInfo(time_t expiry, bool unload = true ) - : i_timer(expiry), i_unloadActiveLockCount(0), i_unloadExplicitLock(!unload), i_unloadReferenceLock(false), - vis_Update(0, irand(0,DEFAULT_VISIBILITY_NOTIFY_PERIOD)) {} - const TimeTracker& getTimeTracker() const { return i_timer; } - bool getUnloadLock() const { return i_unloadActiveLockCount || i_unloadExplicitLock || i_unloadReferenceLock; } - void setUnloadExplicitLock( bool on ) { i_unloadExplicitLock = on; } - void setUnloadReferenceLock( bool on ) { i_unloadReferenceLock = on; } - void incUnloadActiveLock() { ++i_unloadActiveLockCount; } - void decUnloadActiveLock() { if(i_unloadActiveLockCount) --i_unloadActiveLockCount; } - - void setTimer(const TimeTracker& pTimer) { i_timer = pTimer; } - void ResetTimeTracker(time_t interval) { i_timer.Reset(interval); } - void UpdateTimeTracker(time_t diff) { i_timer.Update(diff); } - PeriodicTimer& getRelocationTimer() { return vis_Update; } -private: - TimeTracker i_timer; - PeriodicTimer vis_Update; - - uint16 i_unloadActiveLockCount : 16; // lock from active object spawn points (prevent clone loading) - bool i_unloadExplicitLock : 1; // explicit manual lock or config setting - bool i_unloadReferenceLock : 1; // lock from instance map copy -}; - -typedef enum -{ - GRID_STATE_INVALID = 0, - GRID_STATE_ACTIVE = 1, - GRID_STATE_IDLE = 2, - GRID_STATE_REMOVAL= 3, - MAX_GRID_STATE = 4 -} grid_state_t; - -template -< -unsigned int N, -class ACTIVE_OBJECT, -class WORLD_OBJECT_TYPES, -class GRID_OBJECT_TYPES, -class ThreadModel = Trinity::SingleThreaded -> -class NGrid -{ - public: - - typedef Grid GridType; - NGrid(uint32 id, int32 x, int32 y, time_t expiry, bool unload = true) - : i_gridId(id), i_x(x), i_y(y), i_cellstate(GRID_STATE_INVALID), i_GridObjectDataLoaded(false) - { - i_GridInfo = GridInfo(expiry, unload); - } - - const GridType& operator()(unsigned short x, unsigned short y) const - { - ASSERT(x < N); - ASSERT(y < N); - return i_cells[x][y]; - } - - GridType& operator()(unsigned short x, unsigned short y) - { - ASSERT(x < N); - ASSERT(y < N); - return i_cells[x][y]; - } - - const uint32& GetGridId(void) const { return i_gridId; } - void SetGridId(const uint32 id) const { i_gridId = id; } - grid_state_t GetGridState(void) const { return i_cellstate; } - void SetGridState(grid_state_t s) { i_cellstate = s; } - int32 getX() const { return i_x; } - int32 getY() const { return i_y; } - - void link(GridRefManager >* pTo) - { - i_Reference.link(pTo, this); - } - bool isGridObjectDataLoaded() const { return i_GridObjectDataLoaded; } - void setGridObjectDataLoaded(bool pLoaded) { i_GridObjectDataLoaded = pLoaded; } - - GridInfo* getGridInfoRef() { return &i_GridInfo; } - const TimeTracker& getTimeTracker() const { return i_GridInfo.getTimeTracker(); } - bool getUnloadLock() const { return i_GridInfo.getUnloadLock(); } - void setUnloadExplicitLock( bool on ) { i_GridInfo.setUnloadExplicitLock(on); } - void setUnloadReferenceLock( bool on ) { i_GridInfo.setUnloadReferenceLock(on); } - void incUnloadActiveLock() { i_GridInfo.incUnloadActiveLock(); } - void decUnloadActiveLock() { i_GridInfo.decUnloadActiveLock(); } - void ResetTimeTracker(time_t interval) { i_GridInfo.ResetTimeTracker(interval); } - void UpdateTimeTracker(time_t diff) { i_GridInfo.UpdateTimeTracker(diff); } - - template void AddWorldObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj) - { - getGridType(x, y).AddWorldObject(obj); - } - - template void RemoveWorldObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj) - { - getGridType(x, y).RemoveWorldObject(obj); - } - - template void Visit(TypeContainerVisitor > &visitor) - { - for (unsigned int x=0; x < N; ++x) - for (unsigned int y=0; y < N; ++y) - getGridType(x, y).Visit(visitor); - } - - template void Visit(const uint32 &x, const uint32 &y, TypeContainerVisitor > &visitor) - { - getGridType(x, y).Visit(visitor); - } - - unsigned int ActiveObjectsInGrid(void) const - { - unsigned int count=0; - for (unsigned int x=0; x < N; ++x) - for (unsigned int y=0; y < N; ++y) - count += i_cells[x][y].ActiveObjectsInGrid(); - return count; - } - - template bool AddGridObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj) - { - return getGridType(x, y).AddGridObject(obj); - } - - template bool RemoveGridObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj) - { - return getGridType(x, y).RemoveGridObject(obj); - } - - private: - - GridType& getGridType(const uint32& x, const uint32& y) - { - ASSERT(x < N); - ASSERT(y < N); - return i_cells[x][y]; - } - - uint32 i_gridId; - GridInfo i_GridInfo; - GridReference > i_Reference; - int32 i_x; - int32 i_y; - grid_state_t i_cellstate; - GridType i_cells[N][N]; - bool i_GridObjectDataLoaded; -}; -#endif - diff --git a/src/server/framework/GameSystem/TypeContainer.h b/src/server/framework/GameSystem/TypeContainer.h deleted file mode 100644 index c2c9b4fcdea..00000000000 --- a/src/server/framework/GameSystem/TypeContainer.h +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_TYPECONTAINER_H -#define TRINITY_TYPECONTAINER_H - -/* - * Here, you'll find a series of containers that allow you to hold multiple - * types of object at the same time. - */ - -#include -#include -#include "Platform/Define.h" -#include "Utilities/TypeList.h" -#include "GameSystem/GridRefManager.h" - -/* - * @class ContainerMapList is a mulit-type container for map elements - * By itself its meaningless but collaborate along with TypeContainers, - * it become the most powerfully container in the whole system. - */ -template struct ContainerMapList -{ - //std::map _element; - GridRefManager _element; -}; - -template<> struct ContainerMapList /* nothing is in type null */ -{ -}; -template struct ContainerMapList > -{ - ContainerMapList _elements; - ContainerMapList _TailElements; -}; - -/* - * @class ContaierArrayList is a multi-type container for - * array of elements. - */ -template struct ContainerArrayList -{ - std::vector _element; -}; - -// termination condition -template<> struct ContainerArrayList {}; -// recursion -template struct ContainerArrayList > -{ - ContainerArrayList _elements; - ContainerArrayList _TailElements; -}; - -/* - * @class ContainerList is a simple list of different types of elements - * - */ -template struct ContainerList -{ - OBJECT _element; -}; - -/* TypeNull is underfined */ -template<> struct ContainerList {}; -template struct ContainerList > -{ - ContainerList _elements; - ContainerMapList _TailElements; -}; - -#include "TypeContainerFunctions.h" - -/* - * @class TypeMapContainer contains a fixed number of types and is - * determined at compile time. This is probably the most complicated - * class and do its simplest thing, that is, holds objects - * of different types. - */ - -template -class TypeMapContainer -{ - public: - template size_t Count() const { return Trinity::Count(i_elements, (SPECIFIC_TYPE*)NULL); } - - /// inserts a specific object into the container - template bool insert(SPECIFIC_TYPE *obj) - { - SPECIFIC_TYPE* t = Trinity::Insert(i_elements, obj); - return (t != NULL); - } - - /// Removes the object from the container, and returns the removed object - template bool remove(SPECIFIC_TYPE* obj) - { - SPECIFIC_TYPE* t = Trinity::Remove(i_elements, obj); - return (t != NULL); - } - - ContainerMapList & GetElements(void) { return i_elements; } - const ContainerMapList & GetElements(void) const { return i_elements;} - - private: - ContainerMapList i_elements; -}; -#endif - diff --git a/src/server/framework/GameSystem/TypeContainerFunctions.h b/src/server/framework/GameSystem/TypeContainerFunctions.h deleted file mode 100644 index edfbb40e659..00000000000 --- a/src/server/framework/GameSystem/TypeContainerFunctions.h +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TYPECONTAINER_FUNCTIONS_H -#define TYPECONTAINER_FUNCTIONS_H - -/* - * Here you'll find a list of helper functions to make - * the TypeContainer usefull. Without it, its hard - * to access or mutate the container. - */ - -#include "Platform/Define.h" -#include "Utilities/TypeList.h" -#include - -namespace Trinity -{ - /* ContainerMapList Helpers */ - // count functions - template size_t Count(const ContainerMapList &elements, SPECIFIC_TYPE* /*fake*/) - { - return elements._element.getSize(); - }; - - template size_t Count(const ContainerMapList &/*elements*/, SPECIFIC_TYPE* /*fake*/) - { - return 0; - } - - template size_t Count(const ContainerMapList &/*elements*/, SPECIFIC_TYPE* /*fake*/) - { - return 0; - } - - template size_t Count(const ContainerMapList >&elements, SPECIFIC_TYPE* fake) - { - return Count(elements._elements,fake); - } - - template size_t Count(const ContainerMapList >&elements, SPECIFIC_TYPE* fake) - { - return Count(elements._TailElements, fake); - } - - // non-const insert functions - template SPECIFIC_TYPE* Insert(ContainerMapList &elements, SPECIFIC_TYPE *obj) - { - //elements._element[hdl] = obj; - obj->GetGridRef().link(&elements._element, obj); - return obj; - }; - - template SPECIFIC_TYPE* Insert(ContainerMapList &/*elements*/, SPECIFIC_TYPE * /*obj*/) - { - return NULL; - } - - // this is a missed - template SPECIFIC_TYPE* Insert(ContainerMapList &/*elements*/, SPECIFIC_TYPE * /*obj*/) - { - return NULL; // a missed - } - - // Recursion - template SPECIFIC_TYPE* Insert(ContainerMapList >&elements, SPECIFIC_TYPE *obj) - { - SPECIFIC_TYPE* t= Insert(elements._elements, obj); - return (t != NULL ? t : Insert(elements._TailElements, obj)); - } - - // non-const remove method - template SPECIFIC_TYPE* Remove(ContainerMapList & /*elements*/, SPECIFIC_TYPE *obj) - { - obj->GetGridRef().unlink(); - return obj; - } - - template SPECIFIC_TYPE* Remove(ContainerMapList &/*elements*/, SPECIFIC_TYPE * /*obj*/) - { - return NULL; - } - - // this is a missed - template SPECIFIC_TYPE* Remove(ContainerMapList &/*elements*/, SPECIFIC_TYPE * /*obj*/) - { - return NULL; // a missed - } - - template SPECIFIC_TYPE* Remove(ContainerMapList > &elements, SPECIFIC_TYPE *obj) - { - // The head element is bad - SPECIFIC_TYPE* t = Remove(elements._elements, obj); - return ( t != NULL ? t : Remove(elements._TailElements, obj) ); - } - -} -#endif - diff --git a/src/server/framework/GameSystem/TypeContainerFunctionsPtr.h b/src/server/framework/GameSystem/TypeContainerFunctionsPtr.h deleted file mode 100644 index 2affcc457cc..00000000000 --- a/src/server/framework/GameSystem/TypeContainerFunctionsPtr.h +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TYPECONTAINER_FUNCTIONS_PTR_H -#define TYPECONTAINER_FUNCTIONS_PTR_H - -/* - * Here you'll find a list of helper functions to make - * the TypeContainer usefull. Without it, its hard - * to access or mutate the container. - */ - -#include "Platform/Define.h" -#include "Utilities/TypeList.h" -#include - -namespace Trinity -{ - /* ContainerMapList Helpers */ - // count functions - // template size_t Count(const ContainerMapList &elements, CountedPtr* /*fake*/) - // { - // return elements._element.size(); - // }; - // - // template size_t Count(const ContainerMapList &elements, CountedPtr* /*fake*/) - // { - // return 0; - // } - // - // template size_t Count(const ContainerMapList &elements, CountedPtr* /*fake*/) - // { - // return 0; - // } - // - // template size_t Count(const ContainerMapList >&elements, SPECIFIC_TYPE* fake) - // { - // return Count(elements._elements,fake); - // } - // - // template size_t Count(const ContainerMapList >&elements, SPECIFIC_TYPE* fake) - // { - // return Count(elements._TailElements, fake); - // } - - // non-const find functions - template CountedPtr& Find(ContainerMapList &elements, OBJECT_HANDLE hdl, CountedPtr* /*fake*/) - { - typename std::map >::iterator iter = elements._element.find(hdl); - return (iter == elements._element.end() ? NullPtr((SPECIFIC_TYPE*)NULL) : iter->second); - }; - - template CountedPtr& Find(ContainerMapList &elements, OBJECT_HANDLE hdl, CountedPtr* /*fake*/) - { - return NullPtr((SPECIFIC_TYPE*)NULL);// terminate recursion - } - - template CountedPtr& Find(ContainerMapList &elements, OBJECT_HANDLE hdl, CountedPtr* /*fake*/) - { - return NullPtr((SPECIFIC_TYPE*)NULL);// this is a missed - } - - template CountedPtr& Find(ContainerMapList >&elements, OBJECT_HANDLE hdl, CountedPtr* fake) - { - CountedPtr &t = Find(elements._elements, hdl,fake); - return (!t ? Find(elements._TailElements, hdl,fake) : t); - } - - // const find functions - template const CountedPtr& Find(const ContainerMapList &elements, OBJECT_HANDLE hdl, CountedPtr* /*fake*/) - { - typename CountedPtr::iterator iter = elements._element.find(hdl); - return (iter == elements._element.end() ? NullPtr((SPECIFIC_TYPE*)NULL) : iter->second); - }; - - template const CountedPtr& Find(const ContainerMapList &elements, OBJECT_HANDLE hdl, CountedPtr* /*fake*/) - { - return NullPtr((SPECIFIC_TYPE*)NULL); - } - - template const CountedPtr& Find(const ContainerMapList &elements, OBJECT_HANDLE hdl, CountedPtr* /*fake*/) - { - return NullPtr((SPECIFIC_TYPE*)NULL); - } - - template CountedPtr& Find(const ContainerMapList >&elements, OBJECT_HANDLE hdl, CountedPtr* fake) - { - CountedPtr &t = Find(elements._elements, hdl,fake); - if(!t) - t = Find(elements._TailElement, hdl,fake); - - return t; - } - - // non-const insert functions - template CountedPtr& Insert(ContainerMapList &elements, CountedPtr &obj, OBJECT_HANDLE hdl) - { - elements._element[hdl] = obj; - return obj; - }; - - template CountedPtr& Insert(ContainerMapList &elements, CountedPtr &obj, OBJECT_HANDLE hdl) - { - return NullPtr((SPECIFIC_TYPE*)NULL); - } - - // this is a missed - template CountedPtr& Insert(ContainerMapList &elements, CountedPtr &obj, OBJECT_HANDLE hdl) - { - return NullPtr((SPECIFIC_TYPE*)NULL);// a missed - } - - // Recursion - template CountedPtr& Insert(ContainerMapList >&elements, CountedPtr &obj, OBJECT_HANDLE hdl) - { - CountedPtr &t= Insert(elements._elements, obj, hdl); - return (!t ? Insert(elements._TailElements, obj, hdl) : t); - } - - // non-const remove method - template bool Remove(ContainerMapList &elements, CountedPtr &obj, OBJECT_HANDLE hdl) - { - typename std::map >::iterator iter = elements._element.find(hdl); - if( iter != elements._element.end() ) - { - elements._element.erase(iter); - return true; - } - - return false; // found... terminate the search - } - - template bool Remove(ContainerMapList &elements, CountedPtr &obj, OBJECT_HANDLE hdl) - { - return false; - } - - // this is a missed - template bool Remove(ContainerMapList &elements, CountedPtr &obj, OBJECT_HANDLE hdl) - { - return false; - } - - template bool Remove(ContainerMapList > &elements, CountedPtr &obj, OBJECT_HANDLE hdl) - { - // The head element is bad - bool t = Remove(elements._elements, obj, hdl); - return ( !t ? Remove(elements._TailElements, obj, hdl) : t ); - } - -} -#endif - diff --git a/src/server/framework/GameSystem/TypeContainerVisitor.h b/src/server/framework/GameSystem/TypeContainerVisitor.h deleted file mode 100644 index f15075e5afd..00000000000 --- a/src/server/framework/GameSystem/TypeContainerVisitor.h +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_TYPECONTAINERVISITOR_H -#define TRINITY_TYPECONTAINERVISITOR_H - -/* - * @class TypeContainerVisitor is implemented as a visitor pattern. It is - * a visitor to the TypeContainerList or TypeContainerMapList. The visitor has - * to overload its types as a visit method is called. - */ - -#include "Platform/Define.h" -#include "TypeContainer.h" - -// forward declaration -template class TypeContainerVisitor; - -// visitor helper -template void VisitorHelper(VISITOR &v, TYPE_CONTAINER &c) -{ - v.Visit(c); -}; - -// terminate condition for container list -template void VisitorHelper(VISITOR &v, ContainerList &c) -{ -} - -template void VisitorHelper(VISITOR &v, ContainerList &c) -{ - v.Visit(c._element); -} - -// recursion for container list -template void VisitorHelper(VISITOR &v, ContainerList > &c) -{ - VisitorHelper(v, c._elements); - VisitorHelper(v, c._TailElements); -} - -// terminate condition container map list -template void VisitorHelper(VISITOR &/*v*/, ContainerMapList &/*c*/) -{ -} - -template void VisitorHelper(VISITOR &v, ContainerMapList &c) -{ - v.Visit(c._element); -} - -// recursion container map list -template void VisitorHelper(VISITOR &v, ContainerMapList > &c) -{ - VisitorHelper(v, c._elements); - VisitorHelper(v, c._TailElements); -} - -// array list -template void VisitorHelper(VISITOR &v, ContainerArrayList &c) -{ - v.Visit(c._element); -} - -template void VisitorHelper(VISITOR &/*v*/, ContainerArrayList &/*c*/) -{ -} - -// recursion -template void VisitorHelper(VISITOR &v, ContainerArrayList > &c) -{ - VisitorHelper(v, c._elements); - VisitorHelper(v, c._TailElements); -} - -// for TypeMapContainer -template void VisitorHelper(VISITOR &v, TypeMapContainer &c) -{ - VisitorHelper(v, c.GetElements()); -} - -template -class TypeContainerVisitor -{ - public: - TypeContainerVisitor(VISITOR &v) : i_visitor(v) {} - - void Visit(TYPE_CONTAINER &c) - { - VisitorHelper(i_visitor, c); - } - - void Visit(const TYPE_CONTAINER &c) const - { - VisitorHelper(i_visitor, c); - } - - private: - VISITOR &i_visitor; -}; -#endif - diff --git a/src/server/framework/Network/SocketDefines.h b/src/server/framework/Network/SocketDefines.h deleted file mode 100644 index 49366097ae6..00000000000 --- a/src/server/framework/Network/SocketDefines.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_SOCKETDEFINES_H -#define TRINITY_SOCKETDEFINES_H - -#ifdef WIN32 - -/* Windows socket definitions - */ -#define FD_SETSIZE 1024 -#include -#include - -typedef SOCKET SocketHandle; -typedef fd_set SelectSet; - -#else - -/* The unix socket definitions - */ -#include -#include -#ifdef __APPLE_CC__ -#include -#endif - -typedef int SocketHandle; -typedef fd_set SelectSet; -#endif -#endif - diff --git a/src/server/framework/Platform/CompilerDefs.h b/src/server/framework/Platform/CompilerDefs.h deleted file mode 100644 index fb7dbfe4caa..00000000000 --- a/src/server/framework/Platform/CompilerDefs.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_COMPILERDEFS_H -#define TRINITY_COMPILERDEFS_H - -#define PLATFORM_WINDOWS 0 -#define PLATFORM_UNIX 1 -#define PLATFORM_APPLE 2 -#define PLATFORM_INTEL 3 - -// must be first (win 64 also define WIN32) -#if defined( _WIN64 ) -# define PLATFORM PLATFORM_WINDOWS -#elif defined( __WIN32__ ) || defined( WIN32 ) || defined( _WIN32 ) -# define PLATFORM PLATFORM_WINDOWS -#elif defined( __APPLE_CC__ ) -# define PLATFORM PLATFORM_APPLE -#elif defined( __INTEL_COMPILER ) -# define PLATFORM PLATFORM_INTEL -#else -# define PLATFORM PLATFORM_UNIX -#endif - -#define COMPILER_MICROSOFT 0 -#define COMPILER_GNU 1 -#define COMPILER_BORLAND 2 -#define COMPILER_INTEL 3 - -#ifdef _MSC_VER -# define COMPILER COMPILER_MICROSOFT -#elif defined( __BORLANDC__ ) -# define COMPILER COMPILER_BORLAND -#elif defined( __INTEL_COMPILER ) -# define COMPILER COMPILER_INTEL -#elif defined( __GNUC__ ) -# define COMPILER COMPILER_GNU -#else -# pragma error "FATAL ERROR: Unknown compiler." -#endif - -#if COMPILER == COMPILER_MICROSOFT -# pragma warning( disable : 4267 ) // conversion from 'size_t' to 'int', possible loss of data -# pragma warning( disable : 4786 ) // identifier was truncated to '255' characters in the debug information -#endif -#endif - diff --git a/src/server/framework/Platform/Define.h b/src/server/framework/Platform/Define.h deleted file mode 100644 index 9285bf289f9..00000000000 --- a/src/server/framework/Platform/Define.h +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_DEFINE_H -#define TRINITY_DEFINE_H - -#include - -#include -#include - -#include "Platform/CompilerDefs.h" - -#define TRINITY_LITTLEENDIAN 0 -#define TRINITY_BIGENDIAN 1 - -#if !defined(TRINITY_ENDIAN) -# if defined (ACE_BIG_ENDIAN) -# define TRINITY_ENDIAN TRINITY_BIGENDIAN -# else //ACE_BYTE_ORDER != ACE_BIG_ENDIAN -# define TRINITY_ENDIAN TRINITY_LITTLEENDIAN -# endif //ACE_BYTE_ORDER -#endif //TRINITY_ENDIAN - -#if PLATFORM == PLATFORM_WINDOWS -# define TRINITY_PATH_MAX MAX_PATH -# ifndef DECLSPEC_NORETURN -# define DECLSPEC_NORETURN __declspec(noreturn) -# endif //DECLSPEC_NORETURN -#else //PLATFORM != PLATFORM_WINDOWS -# define TRINITY_PATH_MAX PATH_MAX -# define DECLSPEC_NORETURN -#endif //PLATFORM - -#if !defined(DEBUG) -# define TRINITY_INLINE inline -#else //DEBUG -# if !defined(TRINITY_DEBUG) -# define TRINITY_DEBUG -# endif //TRINITY_DEBUG -# define TRINITY_INLINE -#endif //!DEBUG - -#if COMPILER == COMPILER_GNU -# define ATTR_NORETURN __attribute__((noreturn)) -# define ATTR_PRINTF(F,V) __attribute__ ((format (printf, F, V))) -#else //COMPILER != COMPILER_GNU -# define ATTR_NORETURN -# define ATTR_PRINTF(F,V) -#endif //COMPILER == COMPILER_GNU - -typedef ACE_INT64 int64; -typedef ACE_INT32 int32; -typedef ACE_INT16 int16; -typedef ACE_INT8 int8; -typedef ACE_UINT64 uint64; -typedef ACE_UINT32 uint32; -typedef ACE_UINT16 uint16; -typedef ACE_UINT8 uint8; - -#if COMPILER != COMPILER_MICROSOFT -typedef uint16 WORD; -typedef uint32 DWORD; -#endif //COMPILER - -typedef uint64 OBJECT_HANDLE; - -#endif //TRINITY_DEFINE_H diff --git a/src/server/framework/Policies/CreationPolicy.h b/src/server/framework/Policies/CreationPolicy.h deleted file mode 100644 index 8552ce7da52..00000000000 --- a/src/server/framework/Policies/CreationPolicy.h +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_CREATIONPOLICY_H -#define TRINITY_CREATIONPOLICY_H - -#include -#include "Platform/Define.h" - -namespace Trinity -{ - /** - * OperatorNew policy creates an object on the heap using new. - */ - template - class OperatorNew - { - public: - static T* Create(void) { return (new T); } - static void Destroy(T *obj) { delete obj; } - }; - - /** - * LocalStaticCreation policy creates an object on the stack - * the first time call Create. - */ - template - class LocalStaticCreation - { - union MaxAlign - { - char t_[sizeof(T)]; - short int shortInt_; - int int_; - long int longInt_; - float float_; - double double_; - long double longDouble_; - struct Test; - int Test::* pMember_; - int (Test::*pMemberFn_)(int); - }; - public: - static T* Create(void) - { - static MaxAlign si_localStatic; - return new(&si_localStatic) T; - } - - static void Destroy(T *obj) { obj->~T(); } - }; - - /** - * CreateUsingMalloc by pass the memory manger. - */ - template - class CreateUsingMalloc - { - public: - static T* Create() - { - void* p = ::malloc(sizeof(T)); - if (!p) return 0; - return new(p) T; - } - - static void Destroy(T* p) - { - p->~T(); - ::free(p); - } - }; - - /** - * CreateOnCallBack creates the object base on the call back. - */ - template - class CreateOnCallBack - { - public: - static T* Create() - { - return CALL_BACK::createCallBack(); - } - - static void Destroy(T *p) - { - CALL_BACK::destroyCallBack(p); - } - }; -} -#endif - diff --git a/src/server/framework/Policies/ObjectLifeTime.cpp b/src/server/framework/Policies/ObjectLifeTime.cpp deleted file mode 100644 index fd16873ae92..00000000000 --- a/src/server/framework/Policies/ObjectLifeTime.cpp +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#include -#include "ObjectLifeTime.h" - -namespace Trinity -{ - extern "C" void external_wrapper(void *p) - { - std::atexit( (void (*)())p ); - } - - void at_exit( void (*func)() ) - { - external_wrapper((void*)func); - } -} - diff --git a/src/server/framework/Policies/ObjectLifeTime.h b/src/server/framework/Policies/ObjectLifeTime.h deleted file mode 100644 index 61b90b59f6e..00000000000 --- a/src/server/framework/Policies/ObjectLifeTime.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_OBJECTLIFETIME_H -#define TRINITY_OBJECTLIFETIME_H - -#include -#include "Platform/Define.h" - -typedef void (* Destroyer)(void); - -namespace Trinity -{ - void at_exit( void (*func)() ); - - template - class ObjectLifeTime - { - public: - static void ScheduleCall(void (*destroyer)() ) - { - at_exit( destroyer ); - } - - DECLSPEC_NORETURN static void OnDeadReference(void) ATTR_NORETURN; - - }; - - template - void ObjectLifeTime::OnDeadReference(void) // We don't handle Dead Reference for now - { - throw std::runtime_error("Dead Reference"); - } -} -#endif - diff --git a/src/server/framework/Policies/Singleton.h b/src/server/framework/Policies/Singleton.h deleted file mode 100644 index da898558ca5..00000000000 --- a/src/server/framework/Policies/Singleton.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_SINGLETON_H -#define TRINITY_SINGLETON_H - -/** - * @brief class Singleton - */ - -#include "CreationPolicy.h" -#include "ThreadingModel.h" -#include "ObjectLifeTime.h" - -namespace Trinity -{ - template - < - typename T, - class ThreadingModel = Trinity::SingleThreaded, - class CreatePolicy = Trinity::OperatorNew, - class LifeTimePolicy = Trinity::ObjectLifeTime - > - class Singleton - { - public: - static T& Instance(); - - protected: - Singleton() {}; - - private: - - // Prohibited actions...this does not prevent hijacking. - Singleton(const Singleton &); - Singleton& operator=(const Singleton &); - - // Singleton Helpers - static void DestroySingleton(); - - // data structure - typedef typename ThreadingModel::Lock Guard; - static T *si_instance; - static bool si_destroyed; - }; -} -#endif - diff --git a/src/server/framework/Policies/SingletonImp.h b/src/server/framework/Policies/SingletonImp.h deleted file mode 100644 index 3e985cd5c64..00000000000 --- a/src/server/framework/Policies/SingletonImp.h +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_SINGLETONIMPL_H -#define TRINITY_SINGLETONIMPL_H - -#include "Singleton.h" - -// avoid the using namespace here cuz -// its a .h file afterall - -template -< -typename T, -class ThreadingModel, -class CreatePolicy, -class LifeTimePolicy -> -T& -Trinity::Singleton::Instance() -{ - if( !si_instance ) - { - // double-checked Locking pattern - Guard(); - if( !si_instance ) - { - if( si_destroyed ) - { - si_destroyed = false; - LifeTimePolicy::OnDeadReference(); - } - si_instance = CreatePolicy::Create(); - LifeTimePolicy::ScheduleCall(&DestroySingleton); - } - } - - return *si_instance; -} - -template -< -typename T, -class ThreadingModel, -class CreatePolicy, -class LifeTimePolicy -> -void -Trinity::Singleton::DestroySingleton() -{ - CreatePolicy::Destroy(si_instance); - si_instance = NULL; - si_destroyed = true; -} - -#define INSTANTIATE_SINGLETON_1(TYPE) \ - template class Trinity::Singleton, Trinity::OperatorNew, Trinity::ObjectLifeTime >; \ - template<> TYPE* Trinity::Singleton, Trinity::OperatorNew, Trinity::ObjectLifeTime >::si_instance = 0; \ - template<> bool Trinity::Singleton, Trinity::OperatorNew, Trinity::ObjectLifeTime >::si_destroyed = false - -#define INSTANTIATE_SINGLETON_2(TYPE, THREADINGMODEL) \ - template class Trinity::Singleton, Trinity::ObjectLifeTime >; \ - template<> TYPE* Trinity::Singleton, Trinity::ObjectLifeTime >::si_instance = 0; \ - template<> bool Trinity::Singleton, Trinity::ObjectLifeTime >::si_destroyed = false - -#define INSTANTIATE_SINGLETON_3(TYPE, THREADINGMODEL, CREATIONPOLICY ) \ - template class Trinity::Singleton >; \ - template<> TYPE* Trinity::Singleton >::si_instance = 0; \ - template<> bool Trinity::Singleton >::si_destroyed = false - -#define INSTANTIATE_SINGLETON_4(TYPE, THREADINGMODEL, CREATIONPOLICY, OBJECTLIFETIME) \ - template class Trinity::Singleton; \ - template<> TYPE* Trinity::Singleton::si_instance = 0; \ - template<> bool Trinity::Singleton::si_destroyed = false -#endif - diff --git a/src/server/framework/Policies/ThreadingModel.h b/src/server/framework/Policies/ThreadingModel.h deleted file mode 100644 index d4c5e9a2333..00000000000 --- a/src/server/framework/Policies/ThreadingModel.h +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_THREADINGMODEL_H -#define TRINITY_THREADINGMODEL_H - -/** - * @class ThreadingModel - * - */ - -#include "Platform/Define.h" - -namespace Trinity -{ - inline void Guard(void *) {} - - template class GeneralLock - { - public: - GeneralLock(MUTEX &m) : i_mutex(m) - { - i_mutex.acquire(); - } - - ~GeneralLock() - { - i_mutex.release(); - } - private: - GeneralLock(const GeneralLock &); - GeneralLock& operator=(const GeneralLock &); - MUTEX &i_mutex; - }; - - template - class SingleThreaded - { - public: - - struct Lock // empty object - { - Lock() {} - Lock(const T &) {} - Lock(const SingleThreaded &) // for single threaded we ignore this - { - } - }; - - typedef T VolatileType; - }; - - // object level lockable - template - class ObjectLevelLockable - { - public: - ObjectLevelLockable() : i_mtx() {} - - friend class Lock; - - class Lock - { - public: - Lock(ObjectLevelLockable &host) : i_lock(host.i_mtx) - { - } - - private: - GeneralLock i_lock; - }; - - typedef volatile T VolatileType; - - private: - // prevent the compiler creating a copy construct - ObjectLevelLockable(const ObjectLevelLockable &); - ObjectLevelLockable& operator=(const ObjectLevelLockable &); - - MUTEX i_mtx; - }; - - template - class ClassLevelLockable - { - public: - class Lock; - friend class Lock; - typedef volatile T VolatileType; - - ClassLevelLockable() {} - - class Lock - { - public: - Lock(T& /*host*/) { ClassLevelLockable::si_mtx.acquire(); } - Lock(ClassLevelLockable &) { ClassLevelLockable::si_mtx.acquire(); } - Lock() { ClassLevelLockable::si_mtx.acquire(); } - ~Lock() { ClassLevelLockable::si_mtx.release(); } - }; - - private: - static MUTEX si_mtx; - }; - -} - -template MUTEX Trinity::ClassLevelLockable::si_mtx; - -#define INSTANTIATE_CLASS_MUTEX(CTYPE,MUTEX) \ - template class Trinity::ClassLevelLockable -#endif - diff --git a/src/server/framework/Utilities/ByteConverter.h b/src/server/framework/Utilities/ByteConverter.h deleted file mode 100644 index f8b6bd72498..00000000000 --- a/src/server/framework/Utilities/ByteConverter.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_BYTECONVERTER_H -#define TRINITY_BYTECONVERTER_H - -/** ByteConverter reverse your byte order. This is use - for cross platform where they have different endians. - */ - -#include -#include - -namespace ByteConverter -{ - template - inline void convert(char *val) - { - std::swap(*val, *(val + T - 1)); - convert(val + 1); - } - - template<> inline void convert<0>(char *) {} - template<> inline void convert<1>(char *) {} // ignore central byte - - template inline void apply(T *val) - { - convert((char *)(val)); - } -} - -#if TRINITY_ENDIAN == TRINITY_BIGENDIAN -template inline void EndianConvert(T& val) { ByteConverter::apply(&val); } -template inline void EndianConvertReverse(T&) { } -#else -template inline void EndianConvert(T&) { } -template inline void EndianConvertReverse(T& val) { ByteConverter::apply(&val); } -#endif - -template void EndianConvert(T*); // will generate link error -template void EndianConvertReverse(T*); // will generate link error - -inline void EndianConvert(uint8&) { } -inline void EndianConvert( int8&) { } -inline void EndianConvertReverse(uint8&) { } -inline void EndianConvertReverse( int8&) { } - -#endif - diff --git a/src/server/framework/Utilities/Callback.h b/src/server/framework/Utilities/Callback.h deleted file mode 100644 index d2e2c36851a..00000000000 --- a/src/server/framework/Utilities/Callback.h +++ /dev/null @@ -1,386 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_CALLBACK_H -#define TRINITY_CALLBACK_H - -/// ------------ BASE CLASSES ------------ - -namespace Trinity -{ - template < class Class, typename ParamType1 = void, typename ParamType2 = void, typename ParamType3 = void, typename ParamType4 = void > - class _Callback - { - protected: - typedef void (Class::*Method)(ParamType1, ParamType2, ParamType3, ParamType4); - Class *m_object; - Method m_method; - ParamType1 m_param1; - ParamType2 m_param2; - ParamType3 m_param3; - ParamType4 m_param4; - void _Execute() { (m_object->*m_method)(m_param1, m_param2, m_param3, m_param4); } - public: - _Callback(Class *object, Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3, ParamType4 param4) - : m_object(object), m_method(method), m_param1(param1), m_param2(param2), m_param3(param3), m_param4(param4) {} - _Callback(_Callback < Class, ParamType1, ParamType2, ParamType3, ParamType4> const& cb) - : m_object(cb.object), m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2), m_param3(cb.m_param3), m_param4(cb.m_param4) {} - }; - - template < class Class, typename ParamType1, typename ParamType2, typename ParamType3 > - class _Callback < Class, ParamType1, ParamType2, ParamType3 > - { - protected: - typedef void (Class::*Method)(ParamType1, ParamType2, ParamType3); - Class *m_object; - Method m_method; - ParamType1 m_param1; - ParamType2 m_param2; - ParamType3 m_param3; - void _Execute() { (m_object->*m_method)(m_param1, m_param2, m_param3); } - public: - _Callback(Class *object, Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3) - : m_object(object), m_method(method), m_param1(param1), m_param2(param2) {} - _Callback(_Callback < Class, ParamType1, ParamType2, ParamType3 > const& cb) - : m_object(cb.object), m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2), m_param3(cb.m_param3) {} - }; - - template < class Class, typename ParamType1, typename ParamType2 > - class _Callback < Class, ParamType1, ParamType2 > - { - protected: - typedef void (Class::*Method)(ParamType1, ParamType2); - Class *m_object; - Method m_method; - ParamType1 m_param1; - ParamType2 m_param2; - void _Execute() { (m_object->*m_method)(m_param1, m_param2); } - public: - _Callback(Class *object, Method method, ParamType1 param1, ParamType2 param2) - : m_object(object), m_method(method), m_param1(param1), m_param2(param2) {} - _Callback(_Callback < Class, ParamType1, ParamType2 > const& cb) - : m_object(cb.m_object), m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2) {} - }; - - template < class Class, typename ParamType1 > - class _Callback < Class, ParamType1 > - { - protected: - typedef void (Class::*Method)(ParamType1); - Class *m_object; - Method m_method; - ParamType1 m_param1; - void _Execute() { (m_object->*m_method)(m_param1); } - public: - _Callback(Class *object, Method method, ParamType1 param1) - : m_object(object), m_method(method), m_param1(param1) {} - _Callback(_Callback < Class, ParamType1 > const& cb) - : m_object(cb.m_object), m_method(cb.m_method), m_param1(cb.m_param1) {} - }; - - template < class Class > - class _Callback < Class > - { - protected: - typedef void (Class::*Method)(); - Class *m_object; - Method m_method; - void _Execute() { (m_object->*m_method)(); } - public: - _Callback(Class *object, Method method) - : m_object(object), m_method(method) {} - _Callback(_Callback < Class > const& cb) - : m_object(cb.m_object), m_method(cb.m_method) {} - }; - - /// ---- Statics ---- - - template < typename ParamType1 = void, typename ParamType2 = void, typename ParamType3 = void, typename ParamType4 = void > - class _SCallback - { - protected: - typedef void (*Method)(ParamType1, ParamType2, ParamType3, ParamType4); - Method m_method; - ParamType1 m_param1; - ParamType2 m_param2; - ParamType3 m_param3; - ParamType4 m_param4; - void _Execute() { (*m_method)(m_param1, m_param2, m_param3, m_param4); } - public: - _SCallback(Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3, ParamType4 param4) - : m_method(method), m_param1(param1), m_param2(param2), m_param3(param3), m_param4(param4) {} - _SCallback(_SCallback < ParamType1, ParamType2, ParamType3, ParamType4> const& cb) - : m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2), m_param3(cb.m_param3), m_param4(cb.m_param4) {} - }; - - template < typename ParamType1, typename ParamType2, typename ParamType3 > - class _SCallback < ParamType1, ParamType2, ParamType3 > - { - protected: - typedef void (*Method)(ParamType1, ParamType2, ParamType3); - Method m_method; - ParamType1 m_param1; - ParamType2 m_param2; - ParamType3 m_param3; - void _Execute() { (*m_method)(m_param1, m_param2, m_param3); } - public: - _SCallback(Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3) - : m_method(method), m_param1(param1), m_param2(param2), m_param3(param3) {} - _SCallback(_SCallback < ParamType1, ParamType2, ParamType3 > const& cb) - : m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2), m_param3(cb.m_param3) {} - }; - - template < typename ParamType1, typename ParamType2 > - class _SCallback < ParamType1, ParamType2 > - { - protected: - typedef void (*Method)(ParamType1, ParamType2); - Method m_method; - ParamType1 m_param1; - ParamType2 m_param2; - void _Execute() { (*m_method)(m_param1, m_param2); } - public: - _SCallback(Method method, ParamType1 param1, ParamType2 param2) - : m_method(method), m_param1(param1), m_param2(param2) {} - _SCallback(_SCallback < ParamType1, ParamType2 > const& cb) - : m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2) {} - }; - - template < typename ParamType1 > - class _SCallback < ParamType1 > - { - protected: - typedef void (*Method)(ParamType1); - Method m_method; - ParamType1 m_param1; - void _Execute() { (*m_method)(m_param1); } - public: - _SCallback(Method method, ParamType1 param1) - : m_method(method), m_param1(param1) {} - _SCallback(_SCallback < ParamType1 > const& cb) - : m_method(cb.m_method), m_param1(cb.m_param1) {} - }; - - template < > - class _SCallback < > - { - protected: - typedef void (*Method)(); - Method m_method; - void _Execute() { (*m_method)(); } - public: - _SCallback(Method method) - : m_method(method) {} - _SCallback(_SCallback <> const& cb) - : m_method(cb.m_method) {} - }; -} - -/// --------- GENERIC CALLBACKS ---------- - -namespace Trinity -{ - class ICallback - { - public: - virtual void Execute() = 0; - virtual ~ICallback() {} - }; - - template < class CB > - class _ICallback : public CB, public ICallback - { - public: - _ICallback(CB const& cb) : CB(cb) {} - void Execute() { CB::_Execute(); } - }; - - template < class Class, typename ParamType1 = void, typename ParamType2 = void, typename ParamType3 = void, typename ParamType4 = void > - class Callback : - public _ICallback< _Callback < Class, ParamType1, ParamType2, ParamType3, ParamType4 > > - { - private: - typedef _Callback < Class, ParamType1, ParamType2, ParamType3, ParamType4 > C4; - public: - Callback(Class *object, typename C4::Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3, ParamType4 param4) - : _ICallback< C4 >(C4(object, method, param1, param2, param3, param4)) {} - }; - - template < class Class, typename ParamType1, typename ParamType2, typename ParamType3 > - class Callback < Class, ParamType1, ParamType2, ParamType3 > : - public _ICallback< _Callback < Class, ParamType1, ParamType2, ParamType3 > > - { - private: - typedef _Callback < Class, ParamType1, ParamType2, ParamType3 > C3; - public: - Callback(Class *object, typename C3::Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3) - : _ICallback< C3 >(C3(object, method, param1, param2, param3)) {} - }; - - template < class Class, typename ParamType1, typename ParamType2 > - class Callback < Class, ParamType1, ParamType2 > : - public _ICallback< _Callback < Class, ParamType1, ParamType2 > > - { - private: - typedef _Callback < Class, ParamType1, ParamType2 > C2; - public: - Callback(Class *object, typename C2::Method method, ParamType1 param1, ParamType2 param2) - : _ICallback< C2 >(C2(object, method, param1, param2)) {} - }; - - template < class Class, typename ParamType1 > - class Callback < Class, ParamType1 > : - public _ICallback< _Callback < Class, ParamType1 > > - { - private: - typedef _Callback < Class, ParamType1 > C1; - public: - Callback(Class *object, typename C1::Method method, ParamType1 param1) - : _ICallback< C1 >(C1(object, method, param1)) {} - }; - - template < class Class > - class Callback < Class > : public _ICallback< _Callback < Class > > - { - private: - typedef _Callback < Class > C0; - public: - Callback(Class *object, typename C0::Method method) - : _ICallback< C0 >(C0(object, method)) {} - }; -} - -/// ---------- QUERY CALLBACKS ----------- - -#include "QueryResult.h" -class QueryResult; - -namespace Trinity -{ - class IQueryCallback - { - public: - virtual void Execute() = 0; - virtual ~IQueryCallback() {} - virtual void SetResult(QueryResult_AutoPtr result) = 0; - virtual QueryResult_AutoPtr GetResult() = 0; - }; - - template < class CB > - class _IQueryCallback : public CB, public IQueryCallback - { - public: - _IQueryCallback(CB const& cb) : CB(cb) {} - void Execute() { CB::_Execute(); } - void SetResult(QueryResult_AutoPtr result) { CB::m_param1 = result; } - QueryResult_AutoPtr GetResult() { return CB::m_param1; } - }; - - template < class Class, typename ParamType1 = void, typename ParamType2 = void, typename ParamType3 = void > - class QueryCallback : - public _IQueryCallback< _Callback < Class, QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3 > > - { - private: - typedef _Callback < Class, QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3 > QC3; - public: - QueryCallback(Class *object, typename QC3::Method method, QueryResult_AutoPtr result, ParamType1 param1, ParamType2 param2, ParamType3 param3) - : _IQueryCallback< QC3 >(QC3(object, method, result, param1, param2, param3)) {} - }; - - template < class Class, typename ParamType1, typename ParamType2 > - class QueryCallback < Class, ParamType1, ParamType2 > : - public _IQueryCallback< _Callback < Class, QueryResult_AutoPtr, ParamType1, ParamType2 > > - { - private: - typedef _Callback < Class, QueryResult_AutoPtr, ParamType1, ParamType2 > QC2; - public: - QueryCallback(Class *object, typename QC2::Method method, QueryResult_AutoPtr result, ParamType1 param1, ParamType2 param2) - : _IQueryCallback< QC2 >(QC2(object, method, result, param1, param2)) {} - }; - - template < class Class, typename ParamType1 > - class QueryCallback < Class, ParamType1 > : - public _IQueryCallback< _Callback < Class, QueryResult_AutoPtr, ParamType1 > > - { - private: - typedef _Callback < Class, QueryResult_AutoPtr, ParamType1 > QC1; - public: - QueryCallback(Class *object, typename QC1::Method method, QueryResult_AutoPtr result, ParamType1 param1) - : _IQueryCallback< QC1 >(QC1(object, method, result, param1)) {} - }; - - template < class Class > - class QueryCallback < Class > : public _IQueryCallback< _Callback < Class, QueryResult_AutoPtr > > - { - private: - typedef _Callback < Class, QueryResult_AutoPtr > QC0; - public: - QueryCallback(Class *object, typename QC0::Method method, QueryResult_AutoPtr result) - : _IQueryCallback< QC0 >(QC0(object, method, result)) {} - }; - - /// ---- Statics ---- - - template < typename ParamType1 = void, typename ParamType2 = void, typename ParamType3 = void > - class SQueryCallback : - public _IQueryCallback< _SCallback < QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3 > > - { - private: - typedef _SCallback < QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3 > QC3; - public: - SQueryCallback(typename QC3::Method method, QueryResult_AutoPtr result, ParamType1 param1, ParamType2 param2, ParamType3 param3) - : _IQueryCallback< QC3 >(QC3(method, result, param1, param2, param3)) {} - }; - - template < typename ParamType1, typename ParamType2 > - class SQueryCallback < ParamType1, ParamType2 > : - public _IQueryCallback< _SCallback < QueryResult_AutoPtr, ParamType1, ParamType2 > > - { - private: - typedef _SCallback < QueryResult_AutoPtr, ParamType1, ParamType2 > QC2; - public: - SQueryCallback(typename QC2::Method method, QueryResult_AutoPtr result, ParamType1 param1, ParamType2 param2) - : _IQueryCallback< QC2 >(QC2(method, result, param1, param2)) {} - }; - - template < typename ParamType1 > - class SQueryCallback < ParamType1 > : - public _IQueryCallback< _SCallback < QueryResult_AutoPtr, ParamType1 > > - { - private: - typedef _SCallback < QueryResult_AutoPtr, ParamType1 > QC1; - public: - SQueryCallback(typename QC1::Method method, QueryResult_AutoPtr result, ParamType1 param1) - : _IQueryCallback< QC1 >(QC1(method, result, param1)) {} - }; - - template < > - class SQueryCallback < > : public _IQueryCallback< _SCallback < QueryResult_AutoPtr > > - { - private: - typedef _SCallback < QueryResult_AutoPtr > QC0; - public: - SQueryCallback(QC0::Method method, QueryResult_AutoPtr result) - : _IQueryCallback< QC0 >(QC0(method, result)) {} - }; -} - -#endif - diff --git a/src/server/framework/Utilities/CountedReference/Reference.h b/src/server/framework/Utilities/CountedReference/Reference.h deleted file mode 100644 index d3cfe55ffc0..00000000000 --- a/src/server/framework/Utilities/CountedReference/Reference.h +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_REFERENCE_H -#define TRINITY_REFERENCE_H - -/** - * Referencer - * Referencer is an object that holds a reference holder that hold a reference - * counted object. When an object's reference count drop to zero, it removes - * the object. This is a non intrusive mechanism and any object at any point - * in time can be referenced. When and object is reference counted, do not - * pass the object directly to other methods but rather, pass its - * reference around. Objects can be reference counted in both single threaded - * model and multi-threaded model - */ - -#include -#include "Platform/Define.h" -#include "Policies/ThreadingModel.h" -#include "ReferenceHolder.h" - -template -< -typename T, -class THREADING_MODEL = Trinity::SingleThreaded -> -class Referencer -{ - typedef typename THREADING_MODEL::Lock Lock; - typedef ReferenceHolder ReferenceeHolder; - public: - - /// Constructs a referencer. - Referencer(T *ref = NULL); - - /// Copy constructor - Referencer(const Referencer &obj) : i_holder(NULL) { *this = obj; } - - /// Destructor - ~Referencer(); - - /// Referencee accessor - T* referencee(void) { return (i_holder == NULL ? NULL : i_holder->i_referencee); } - const T* referencee(void) const { return (i_holder == NULL ? NULL : i_holder->i_referencee); } - - //T& referencee(void){ return _referencee(); } - //const T& referencee(void) const { return const_cast(this)->_referencee(); } - operator T&(void) { return _referencee(); } - operator const T&(void) const { return *const_cast(this)->_referencee(); } - - /// cast operators - T* operator*() { return (i_holder == NULL ? NULL : i_holder->i_referencee); } - T const * operator*() const { return (i_holder == NULL ? NULL : i_holder->i_referencee); } - - /// overload operators - T* operator->() { return (i_holder == NULL ? NULL : i_holder->i_referencee); } - const T * operator->() const { return (i_holder == NULL ? NULL : i_holder->i_referencee); } - - /// operator = - Referencer& operator=(const Referencer &obj); - Referencer& operator=(T *); - - /// returns true if i_referencee is null - bool isNull(void) const { return i_holder == NULL; } - - private: - - T& _referencee(void) - { - if( i_holder == NULL ) - throw std::runtime_error("Invalid access to null pointer"); - return *i_holder->i_referencee; - } - - void deReference(ReferenceeHolder *); - void addReference(ReferenceeHolder *); - - // private data - ReferenceeHolder *i_holder; -}; -#endif - diff --git a/src/server/framework/Utilities/CountedReference/ReferenceHolder.h b/src/server/framework/Utilities/CountedReference/ReferenceHolder.h deleted file mode 100644 index 597e9854be0..00000000000 --- a/src/server/framework/Utilities/CountedReference/ReferenceHolder.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_REFERENCEHOLDER_H -#define TRINITY_REFERENCEHOLDER_H - -/** ReferenceHolder holds the actualy referenced obejct as well the refence - count. The ReferenecHolder implements as a policy base object and - will decided by the Reference class to be consnsitent. - */ - -template -< -typename T, -class THREADING_MODEL -> -struct ReferenceHolder : public THREADING_MODEL -{ - explicit ReferenceHolder(T *ref) : i_referencee(ref), i_referenceCount(0) {} - T *i_referencee; - unsigned int i_referenceCount; - typedef typename THREADING_MODEL::Lock Lock; -}; -#endif - diff --git a/src/server/framework/Utilities/CountedReference/ReferenceImpl.h b/src/server/framework/Utilities/CountedReference/ReferenceImpl.h deleted file mode 100644 index cde330179e3..00000000000 --- a/src/server/framework/Utilities/CountedReference/ReferenceImpl.h +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_REFERENCEIMPL_H -#define TRINITY_REFERENCEIMPL_H - -#include "Reference.h" - -template -< -typename T, -class THREADING_MODEL -> -Referencer::Referencer(T *ref) -: i_holder(NULL) -{ - if( ref != NULL ) - { - i_holder = new ReferenceeHolder(ref); - ++i_holder->i_referenceCount; - } -} - -template -< -typename T, -class THREADING_MODEL -> -Referencer::~Referencer() -{ - if( i_holder != NULL ) - deReference(i_holder); - i_holder = NULL; -} - -template -< -typename T, -class THREADING_MODEL -> -Referencer& -Referencer::operator=(const Referencer &obj) -{ - if( i_holder != NULL ) - deReference(i_holder); - if( obj.i_holder != NULL ) - addReference(obj.i_holder); - i_holder = obj.i_holder; - return *this; -} - -template -< -typename T, -class THREADING_MODEL -> -Referencer& -Referencer::operator=(T *ref) -{ - if( i_holder != NULL ) - deReference(i_holder); - i_holder = NULL; - if( ref != NULL ) - { - i_holder = new ReferenceeHolder(ref); - ++i_holder->i_referenceCount; - } - - return *this; -} - -template -< -typename T, -class THREADING_MODEL -> -void -Referencer::deReference(ReferenceHolder *holder) -{ - assert( holder != NULL && holder->i_referenceCount > 0); - bool delete_object = false; - - { - // The guard is within the scope due to the guard - // must release earlier than expected. - Lock guard(*holder); - Guard(&guard); - - --holder->i_referenceCount; - if( holder->i_referenceCount == 0 ) - delete_object = true; - } - - if( delete_object ) - { - delete holder->i_referencee; - delete holder; - } -} - -template -< -typename T, -class THREADING_MODEL -> -void -Referencer::addReference(ReferenceHolder *holder) -{ - assert( i_holder != NULL ); - Lock guard(*holder); - Guard(&guard); - - ++holder->i_referenceCount; -} -#endif - diff --git a/src/server/framework/Utilities/EventProcessor.cpp b/src/server/framework/Utilities/EventProcessor.cpp deleted file mode 100644 index c695b43313a..00000000000 --- a/src/server/framework/Utilities/EventProcessor.cpp +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#include "EventProcessor.h" - -EventProcessor::EventProcessor() -{ - m_time = 0; - m_aborting = false; -} - -EventProcessor::~EventProcessor() -{ - KillAllEvents(true); -} - -void EventProcessor::Update(uint32 p_time) -{ - // update time - m_time += p_time; - - // main event loop - EventList::iterator i; - while (((i = m_events.begin()) != m_events.end()) && i->first <= m_time) - { - // get and remove event from queue - BasicEvent* Event = i->second; - m_events.erase(i); - - if (!Event->to_Abort) - { - if (Event->Execute(m_time, p_time)) - { - // completely destroy event if it is not re-added - delete Event; - } - } - else - { - Event->Abort(m_time); - delete Event; - } - } -} - -void EventProcessor::KillAllEvents(bool force) -{ - // prevent event insertions - m_aborting = true; - - // first, abort all existing events - for (EventList::iterator i = m_events.begin(); i != m_events.end();) - { - EventList::iterator i_old = i; - ++i; - - i_old->second->to_Abort = true; - i_old->second->Abort(m_time); - if(force || i_old->second->IsDeletable()) - { - delete i_old->second; - - if(!force) // need per-element cleanup - m_events.erase (i_old); - } - } - - // fast clear event list (in force case) - if(force) - m_events.clear(); -} - -void EventProcessor::AddEvent(BasicEvent* Event, uint64 e_time, bool set_addtime) -{ - if (set_addtime) Event->m_addTime = m_time; - Event->m_execTime = e_time; - m_events.insert(std::pair(e_time, Event)); -} - -uint64 EventProcessor::CalculateTime(uint64 t_offset) -{ - return(m_time + t_offset); -} - diff --git a/src/server/framework/Utilities/EventProcessor.h b/src/server/framework/Utilities/EventProcessor.h deleted file mode 100644 index 2712967e1b7..00000000000 --- a/src/server/framework/Utilities/EventProcessor.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef __EVENTPROCESSOR_H -#define __EVENTPROCESSOR_H - -#include "Platform/Define.h" - -#include - -// Note. All times are in milliseconds here. - -class BasicEvent -{ - public: - BasicEvent() { to_Abort = false; } - virtual ~BasicEvent() // override destructor to perform some actions on event removal - { - }; - - // this method executes when the event is triggered - // return false if event does not want to be deleted - // e_time is execution time, p_time is update interval - virtual bool Execute(uint64 /*e_time*/, uint32 /*p_time*/) { return true; } - - virtual bool IsDeletable() const { return true; } // this event can be safely deleted - - virtual void Abort(uint64 /*e_time*/) {} // this method executes when the event is aborted - - bool to_Abort; // set by externals when the event is aborted, aborted events don't execute - // and get Abort call when deleted - - // these can be used for time offset control - uint64 m_addTime; // time when the event was added to queue, filled by event handler - uint64 m_execTime; // planned time of next execution, filled by event handler -}; - -typedef std::multimap EventList; - -class EventProcessor -{ - public: - EventProcessor(); - ~EventProcessor(); - - void Update(uint32 p_time); - void KillAllEvents(bool force); - void AddEvent(BasicEvent* Event, uint64 e_time, bool set_addtime = true); - uint64 CalculateTime(uint64 t_offset); - protected: - uint64 m_time; - EventList m_events; - bool m_aborting; -}; -#endif - diff --git a/src/server/framework/Utilities/LinkedList.h b/src/server/framework/Utilities/LinkedList.h deleted file mode 100644 index b26687767b3..00000000000 --- a/src/server/framework/Utilities/LinkedList.h +++ /dev/null @@ -1,247 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef _LINKEDLIST -#define _LINKEDLIST - -#include "Common.h" - -//============================================ -class LinkedListHead; - -class LinkedListElement -{ - private: - friend class LinkedListHead; - - LinkedListElement* iNext; - LinkedListElement* iPrev; - public: - LinkedListElement() { iNext = NULL; iPrev = NULL; } - ~LinkedListElement() { delink(); } - - bool hasNext() const { return(iNext && iNext->iNext != NULL); } - bool hasPrev() const { return(iPrev && iPrev->iPrev != NULL); } - bool isInList() const { return(iNext != NULL && iPrev != NULL); } - - LinkedListElement * next() { return hasNext() ? iNext : NULL; } - LinkedListElement const* next() const { return hasNext() ? iNext : NULL; } - LinkedListElement * prev() { return hasPrev() ? iPrev : NULL; } - LinkedListElement const* prev() const { return hasPrev() ? iPrev : NULL; } - - LinkedListElement * nocheck_next() { return iNext; } - LinkedListElement const* nocheck_next() const { return iNext; } - LinkedListElement * nocheck_prev() { return iPrev; } - LinkedListElement const* nocheck_prev() const { return iPrev; } - - void delink() - { - if(isInList()) - { - iNext->iPrev = iPrev; iPrev->iNext = iNext; iNext = NULL; iPrev = NULL; - } - } - - void insertBefore(LinkedListElement* pElem) - { - pElem->iNext = this; - pElem->iPrev = iPrev; - iPrev->iNext = pElem; - iPrev = pElem; - } - - void insertAfter(LinkedListElement* pElem) - { - pElem->iPrev = this; - pElem->iNext = iNext; - iNext->iPrev = pElem; - iNext = pElem; - } -}; - -//============================================ - -class LinkedListHead -{ - private: - LinkedListElement iFirst; - LinkedListElement iLast; - uint32 iSize; - public: - LinkedListHead() - { - // create empty list - - iFirst.iNext = &iLast; - iLast.iPrev = &iFirst; - iSize = 0; - } - - bool isEmpty() const { return(!iFirst.iNext->isInList()); } - - LinkedListElement * getFirst() { return(isEmpty() ? NULL : iFirst.iNext); } - LinkedListElement const* getFirst() const { return(isEmpty() ? NULL : iFirst.iNext); } - - LinkedListElement * getLast() { return(isEmpty() ? NULL : iLast.iPrev); } - LinkedListElement const* getLast() const { return(isEmpty() ? NULL : iLast.iPrev); } - - void insertFirst(LinkedListElement* pElem) - { - iFirst.insertAfter(pElem); - } - - void insertLast(LinkedListElement* pElem) - { - iLast.insertBefore(pElem); - } - - uint32 getSize() const - { - if(!iSize) - { - uint32 result = 0; - LinkedListElement const* e = getFirst(); - while(e) - { - ++result; - e = e->next(); - } - return result; - } - else - return iSize; - } - - void incSize() { ++iSize; } - void decSize() { --iSize; } - - template - class Iterator - { - public: - typedef std::bidirectional_iterator_tag iterator_category; - typedef _Ty value_type; - typedef ptrdiff_t difference_type; - typedef ptrdiff_t distance_type; - typedef _Ty* pointer; - typedef _Ty const* const_pointer; - typedef _Ty& reference; - typedef _Ty const & const_reference; - - Iterator() : _Ptr(0) - { // construct with null node pointer - } - - Iterator(pointer _Pnode) : _Ptr(_Pnode) - { // construct with node pointer _Pnode - } - - Iterator& operator=(Iterator const &_Right) - { - return (*this) = _Right._Ptr; - } - - Iterator& operator=(const_pointer const &_Right) - { - _Ptr = (pointer)_Right; - return (*this); - } - - reference operator*() - { // return designated value - return *_Ptr; - } - - pointer operator->() - { // return pointer to class object - return _Ptr; - } - - Iterator& operator++() - { // preincrement - _Ptr = _Ptr->next(); - return (*this); - } - - Iterator operator++(int) - { // postincrement - iterator _Tmp = *this; - ++*this; - return (_Tmp); - } - - Iterator& operator--() - { // predecrement - _Ptr = _Ptr->prev(); - return (*this); - } - - Iterator operator--(int) - { // postdecrement - iterator _Tmp = *this; - --*this; - return (_Tmp); - } - - bool operator==(Iterator const &_Right) const - { // test for iterator equality - return (_Ptr == _Right._Ptr); - } - - bool operator!=(Iterator const &_Right) const - { // test for iterator inequality - return (!(*this == _Right)); - } - - bool operator==(pointer const &_Right) const - { // test for pointer equality - return (_Ptr != _Right); - } - - bool operator!=(pointer const &_Right) const - { // test for pointer equality - return (!(*this == _Right)); - } - - bool operator==(const_reference _Right) const - { // test for reference equality - return (_Ptr == &_Right); - } - - bool operator!=(const_reference _Right) const - { // test for reference equality - return (_Ptr != &_Right); - } - - pointer _Mynode() - { // return node pointer - return (_Ptr); - } - - protected: - pointer _Ptr; // pointer to node - }; - - typedef Iterator iterator; -}; - -//============================================ -#endif - diff --git a/src/server/framework/Utilities/LinkedReference/RefManager.h b/src/server/framework/Utilities/LinkedReference/RefManager.h deleted file mode 100644 index 7e294b4f5f0..00000000000 --- a/src/server/framework/Utilities/LinkedReference/RefManager.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef _REFMANAGER_H -#define _REFMANAGER_H -//===================================================== - -#include "Utilities/LinkedList.h" -#include "Utilities/LinkedReference/Reference.h" - -template class RefManager : public LinkedListHead -{ - public: - typedef LinkedListHead::Iterator< Reference > iterator; - RefManager() { } - virtual ~RefManager() { clearReferences(); } - - Reference* getFirst() { return ((Reference*) LinkedListHead::getFirst()); } - Reference const* getFirst() const { return ((Reference const*) LinkedListHead::getFirst()); } - Reference* getLast() { return ((Reference*) LinkedListHead::getLast()); } - Reference const* getLast() const { return ((Reference const*) LinkedListHead::getLast()); } - - iterator begin() { return iterator(getFirst()); } - iterator end() { return iterator(NULL); } - iterator rbegin() { return iterator(getLast()); } - iterator rend() { return iterator(NULL); } - - void clearReferences() - { - LinkedListElement* ref; - while((ref = getFirst()) != NULL) - { - ((Reference*) ref)->invalidate(); - ref->delink(); // the delink might be already done by invalidate(), but doing it here again does not hurt and insures an empty list - } - } -}; - -//===================================================== -#endif - diff --git a/src/server/framework/Utilities/LinkedReference/Reference.h b/src/server/framework/Utilities/LinkedReference/Reference.h deleted file mode 100644 index 4a1545f8f12..00000000000 --- a/src/server/framework/Utilities/LinkedReference/Reference.h +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef _REFERENCE_H -#define _REFERENCE_H - -#include "Utilities/LinkedList.h" - -//===================================================== - -template class Reference : public LinkedListElement -{ - private: - TO* iRefTo; - FROM* iRefFrom; - protected: - // Tell our refTo (target) object that we have a link - virtual void targetObjectBuildLink() = 0; - - // Tell our refTo (taget) object, that the link is cut - virtual void targetObjectDestroyLink() = 0; - - // Tell our refFrom (source) object, that the link is cut (Target destroyed) - virtual void sourceObjectDestroyLink() = 0; - public: - Reference() { iRefTo = NULL; iRefFrom = NULL; } - virtual ~Reference() {} - - // Create new link - void link(TO* toObj, FROM* fromObj) - { - assert(fromObj); // fromObj MUST not be NULL - if(isValid()) - unlink(); - if(toObj != NULL) - { - iRefTo = toObj; - iRefFrom = fromObj; - targetObjectBuildLink(); - } - } - - // We don't need the reference anymore. Call comes from the refFrom object - // Tell our refTo object, that the link is cut - void unlink() { targetObjectDestroyLink(); delink(); iRefTo = NULL; iRefFrom = NULL; } - - // Link is invalid due to destruction of referenced target object. Call comes from the refTo object - // Tell our refFrom object, that the link is cut - void invalidate() // the iRefFrom MUST remain!! - { - sourceObjectDestroyLink(); delink(); iRefTo = NULL; - } - - bool isValid() const // Only check the iRefTo - { - return iRefTo != NULL; - } - - Reference * next() { return((Reference *) LinkedListElement::next()); } - Reference const * next() const { return((Reference const *) LinkedListElement::next()); } - Reference * prev() { return((Reference *) LinkedListElement::prev()); } - Reference const * prev() const { return((Reference const *) LinkedListElement::prev()); } - - Reference * nocheck_next() { return((Reference *) LinkedListElement::nocheck_next()); } - Reference const * nocheck_next() const { return((Reference const *) LinkedListElement::nocheck_next()); } - Reference * nocheck_prev() { return((Reference *) LinkedListElement::nocheck_prev()); } - Reference const * nocheck_prev() const { return((Reference const *) LinkedListElement::nocheck_prev()); } - - TO* operator ->() const { return iRefTo; } - TO* getTarget() const { return iRefTo; } - - FROM* getSource() const { return iRefFrom; } -}; - -//===================================================== -#endif - diff --git a/src/server/framework/Utilities/TypeList.h b/src/server/framework/Utilities/TypeList.h deleted file mode 100644 index 02bc08083c2..00000000000 --- a/src/server/framework/Utilities/TypeList.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_TYPELIST_H -#define TRINITY_TYPELIST_H - -/* - @struct TypeList - TypeList is the most simple but yet the most powerfull class of all. It holds - at compile time the different type of objects in a linked list. - */ - -class TypeNull; - -template -struct TypeList -{ - typedef HEAD Head; - typedef TAIL Tail; -}; - -// enough for now.. can be expand at any point in time as needed -#define TYPELIST_1(T1) TypeList -#define TYPELIST_2(T1, T2) TypeList -#define TYPELIST_3(T1, T2, T3) TypeList -#define TYPELIST_4(T1, T2, T3, T4) TypeList -#define TYPELIST_5(T1, T2, T3, T4, T5) TypeList -#endif - diff --git a/src/server/framework/Utilities/UnorderedMap.h b/src/server/framework/Utilities/UnorderedMap.h deleted file mode 100644 index fce5ec82bfc..00000000000 --- a/src/server/framework/Utilities/UnorderedMap.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_UNORDERED_MAP_H -#define TRINITY_UNORDERED_MAP_H - -#include "Platform/CompilerDefs.h" -#include "Platform/Define.h" - -#if COMPILER == COMPILER_INTEL -#include -#elif COMPILER == COMPILER_GNU && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3) -#include -#elif COMPILER == COMPILER_GNU && __GNUC__ >= 3 -#include -#elif COMPILER == COMPILER_MICROSOFT && _MSC_VER >= 1500 && _HAS_TR1 // VC9.0 and later -#include -#else -#include -#endif - -#ifdef _STLPORT_VERSION -#define UNORDERED_MAP std::hash_map -using std::hash_map; -#elif COMPILER == COMPILER_MICROSOFT && _MSC_VER >= 1500 && _HAS_TR1 -#define UNORDERED_MAP std::tr1::unordered_map -#elif COMPILER == COMPILER_MICROSOFT && _MSC_VER >= 1300 -#define UNORDERED_MAP stdext::hash_map -using stdext::hash_map; -#elif COMPILER == COMPILER_INTEL -#define UNORDERED_MAP std::hash_map -using std::hash_map; -#elif COMPILER == COMPILER_GNU && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3) -#define UNORDERED_MAP std::tr1::unordered_map -#elif COMPILER == COMPILER_GNU && __GNUC__ >= 3 -#define UNORDERED_MAP __gnu_cxx::hash_map - -namespace __gnu_cxx -{ - template<> struct hash - { - size_t operator()(const unsigned long long &__x) const { return (size_t)__x; } - }; - template struct hash - { - size_t operator()(T * const &__x) const { return (size_t)__x; } - }; - -}; - -#else -#define UNORDERED_MAP std::hash_map -using std::hash_map; -#endif -#endif - diff --git a/src/server/game/Grids/Cell.h b/src/server/game/Grids/Cell.h deleted file mode 100644 index 49e0329ace6..00000000000 --- a/src/server/game/Grids/Cell.h +++ /dev/null @@ -1,177 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_CELL_H -#define TRINITY_CELL_H - -#include - -#include "GameSystem/TypeContainer.h" -#include "GameSystem/TypeContainerVisitor.h" - -#include "GridDefines.h" - -class Map; -class WorldObject; - -enum District -{ - UPPER_DISTRICT = 1, - LOWER_DISTRICT = 1 << 1, - LEFT_DISTRICT = 1 << 2, - RIGHT_DISTRICT = 1 << 3, - CENTER_DISTRICT = 1 << 4, - UPPER_LEFT_DISTRICT = (UPPER_DISTRICT | LEFT_DISTRICT), - UPPER_RIGHT_DISTRICT = (UPPER_DISTRICT | RIGHT_DISTRICT), - LOWER_LEFT_DISTRICT = (LOWER_DISTRICT | LEFT_DISTRICT), - LOWER_RIGHT_DISTRICT = (LOWER_DISTRICT | RIGHT_DISTRICT), - ALL_DISTRICT = (UPPER_DISTRICT | LOWER_DISTRICT | LEFT_DISTRICT | RIGHT_DISTRICT | CENTER_DISTRICT) -}; - -struct CellArea -{ - CellArea() : right_offset(0), left_offset(0), upper_offset(0), lower_offset(0) {} - CellArea(int right, int left, int upper, int lower) : right_offset(right), left_offset(left), upper_offset(upper), lower_offset(lower) {} - bool operator!() const { return !right_offset && !left_offset && !upper_offset && !lower_offset; } - - void ResizeBorders(CellPair& begin_cell, CellPair& end_cell) const - { - begin_cell << left_offset; - begin_cell -= lower_offset; - end_cell >> right_offset; - end_cell += upper_offset; - } - - int right_offset; - int left_offset; - int upper_offset; - int lower_offset; -}; - -struct Cell -{ - Cell() { data.All = 0; } - Cell(const Cell &cell) { data.All = cell.data.All; } - explicit Cell(CellPair const& p); - - void operator|=(Cell &cell) - { - data.Part.reserved = 0; - cell.data.Part.reserved = 0; - uint32 x, y, old_x, old_y; - Compute(x, y); - cell.Compute(old_x, old_y); - - if (std::abs(int(x-old_x)) > 1 || std::abs(int(y-old_y)) > 1) - { - data.Part.reserved = ALL_DISTRICT; - cell.data.Part.reserved = ALL_DISTRICT; - return; - } - - if (x < old_x) - { - data.Part.reserved |= LEFT_DISTRICT; - cell.data.Part.reserved |= RIGHT_DISTRICT; - } - else if (old_x < x) - { - data.Part.reserved |= RIGHT_DISTRICT; - cell.data.Part.reserved |= LEFT_DISTRICT; - } - if (y < old_y) - { - data.Part.reserved |= UPPER_DISTRICT; - cell.data.Part.reserved |= LOWER_DISTRICT; - } - else if (old_y < y) - { - data.Part.reserved |= LOWER_DISTRICT; - cell.data.Part.reserved |= UPPER_DISTRICT; - } - } - - void Compute(uint32 &x, uint32 &y) const - { - x = data.Part.grid_x*MAX_NUMBER_OF_CELLS + data.Part.cell_x; - y = data.Part.grid_y*MAX_NUMBER_OF_CELLS + data.Part.cell_y; - } - - bool DiffCell(const Cell &cell) const - { - return(data.Part.cell_x != cell.data.Part.cell_x || - data.Part.cell_y != cell.data.Part.cell_y); - } - - bool DiffGrid(const Cell &cell) const - { - return(data.Part.grid_x != cell.data.Part.grid_x || - data.Part.grid_y != cell.data.Part.grid_y); - } - - uint32 CellX() const { return data.Part.cell_x; } - uint32 CellY() const { return data.Part.cell_y; } - uint32 GridX() const { return data.Part.grid_x; } - uint32 GridY() const { return data.Part.grid_y; } - bool NoCreate() const { return data.Part.nocreate; } - void SetNoCreate() { data.Part.nocreate = 1; } - - CellPair cellPair() const - { - return CellPair( - data.Part.grid_x*MAX_NUMBER_OF_CELLS+data.Part.cell_x, - data.Part.grid_y*MAX_NUMBER_OF_CELLS+data.Part.cell_y); - } - - Cell& operator=(const Cell &cell) - { - this->data.All = cell.data.All; - return *this; - } - - bool operator == (const Cell &cell) const { return (data.All == cell.data.All); } - bool operator != (const Cell &cell) const { return !operator == (cell); } - union - { - struct - { - unsigned grid_x : 6; - unsigned grid_y : 6; - unsigned cell_x : 6; - unsigned cell_y : 6; - unsigned nocreate : 1; - unsigned reserved : 7; - } Part; - uint32 All; - } data; - - template void Visit(const CellPair&, TypeContainerVisitor &visitor, Map &) const; - template void Visit(const CellPair&, TypeContainerVisitor &visitor, Map &, const WorldObject&, float) const; - template void Visit(const CellPair&, TypeContainerVisitor &visitor, Map &, float, float, float) const; - - static CellArea CalculateCellArea(const WorldObject &obj, float radius); - static CellArea CalculateCellArea(float x, float y, float radius); - -private: - template void VisitCircle(TypeContainerVisitor &, Map &, const CellPair&, const CellPair&) const; -}; - -#endif - diff --git a/src/server/game/Grids/CellImpl.h b/src/server/game/Grids/CellImpl.h deleted file mode 100644 index d906e81a5c9..00000000000 --- a/src/server/game/Grids/CellImpl.h +++ /dev/null @@ -1,297 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_CELLIMPL_H -#define TRINITY_CELLIMPL_H - -#include - -#include "Cell.h" -#include "Map.h" -#include "Object.h" - -inline Cell::Cell(CellPair const& p) -{ - data.Part.grid_x = p.x_coord / MAX_NUMBER_OF_CELLS; - data.Part.grid_y = p.y_coord / MAX_NUMBER_OF_CELLS; - data.Part.cell_x = p.x_coord % MAX_NUMBER_OF_CELLS; - data.Part.cell_y = p.y_coord % MAX_NUMBER_OF_CELLS; - data.Part.nocreate = 0; - data.Part.reserved = 0; -} - -template -inline void -Cell::Visit(const CellPair& standing_cell, TypeContainerVisitor &visitor, Map &m) const -{ - if (standing_cell.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || standing_cell.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP) - return; - - uint16 district = (District)this->data.Part.reserved; - if (district == CENTER_DISTRICT) - { - m.Visit(*this, visitor); - return; - } - - // set up the cell range based on the district - // the overloaded operators handle range checking - CellPair begin_cell = standing_cell; - CellPair end_cell = standing_cell; - - switch(district) - { - case ALL_DISTRICT: - { - begin_cell << 1; begin_cell -= 1; // upper left - end_cell >> 1; end_cell += 1; // lower right - break; - } - case UPPER_LEFT_DISTRICT: - { - begin_cell << 1; begin_cell -= 1; // upper left - break; - } - case UPPER_RIGHT_DISTRICT: - { - begin_cell -= 1; // up - end_cell >> 1; // right - break; - } - case LOWER_LEFT_DISTRICT: - { - begin_cell << 1; // left - end_cell += 1; // down - break; - } - case LOWER_RIGHT_DISTRICT: - { - end_cell >> 1; end_cell += 1; // lower right - break; - } - case LEFT_DISTRICT: - { - begin_cell -= 1; // up - end_cell >> 1; end_cell += 1; // lower right - break; - } - case RIGHT_DISTRICT: - { - begin_cell << 1; begin_cell -= 1; // upper left - end_cell += 1; // down - break; - } - case UPPER_DISTRICT: - { - begin_cell << 1; begin_cell -= 1; // upper left - end_cell >> 1; // right - break; - } - case LOWER_DISTRICT: - { - begin_cell << 1; // left - end_cell >> 1; end_cell += 1; // lower right - break; - } - default: - { - assert(false); - break; - } - } - - // loop the cell range - 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++) - { - CellPair cell_pair(x,y); - Cell r_zone(cell_pair); - r_zone.data.Part.nocreate = this->data.Part.nocreate; - m.Visit(r_zone, visitor); - } - } -} - -inline int CellHelper(const float radius) -{ - if (radius < 1.0f) - return 0; - - return (int)ceilf(radius/SIZE_OF_GRID_CELL); -} - -inline CellArea Cell::CalculateCellArea(const WorldObject &obj, float radius) -{ - return Cell::CalculateCellArea(obj.GetPositionX(), obj.GetPositionY(), radius); -} - -inline CellArea Cell::CalculateCellArea(float x, float y, float radius) -{ - if (radius <= 0.0f) - return CellArea(); - - //lets calculate object coord offsets from cell borders. - //TODO: add more correct/generic method for this task - const float x_offset = (x - CENTER_GRID_CELL_OFFSET)/SIZE_OF_GRID_CELL; - const float y_offset = (y - CENTER_GRID_CELL_OFFSET)/SIZE_OF_GRID_CELL; - - const float x_val = floor(x_offset + CENTER_GRID_CELL_ID + 0.5f); - const float y_val = floor(y_offset + CENTER_GRID_CELL_ID + 0.5f); - - const float x_off = (x_offset - x_val + CENTER_GRID_CELL_ID) * SIZE_OF_GRID_CELL; - const float y_off = (y_offset - y_val + CENTER_GRID_CELL_ID) * SIZE_OF_GRID_CELL; - - const float tmp_diff = radius - CENTER_GRID_CELL_OFFSET; - //lets calculate upper/lower/right/left corners for cell search - int right = CellHelper(tmp_diff + x_off); - int left = CellHelper(tmp_diff - x_off); - int upper = CellHelper(tmp_diff + y_off); - int lower = CellHelper(tmp_diff - y_off); - - return CellArea(right, left, upper, lower); -} - -template -inline void -Cell::Visit(const CellPair& standing_cell, TypeContainerVisitor &visitor, Map &m, float radius, float x_off, float y_off) const -{ - if (standing_cell.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || standing_cell.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP) - return; - - //no jokes here... Actually placing ASSERT() here was good idea, but - //we had some problems with DynamicObjects, which pass radius = 0.0f (DB issue?) - //maybe it is better to just return when radius <= 0.0f? - if (radius <= 0.0f) - { - m.Visit(*this, visitor); - return; - } - //lets limit the upper value for search radius - if (radius > 333.0f) - radius = 333.0f; - - //lets calculate object coord offsets from cell borders. - CellArea area = Cell::CalculateCellArea(x_off, y_off, radius); - //if radius fits inside standing cell - if (!area) - { - m.Visit(*this, visitor); - return; - } - - CellPair begin_cell = standing_cell; - CellPair end_cell = standing_cell; - - area.ResizeBorders(begin_cell, end_cell); - //visit all cells, found in CalculateCellArea() - //if radius is known to reach cell area more than 4x4 then we should call optimized VisitCircle - //currently this technique works with MAX_NUMBER_OF_CELLS 16 and higher, with lower values - //there are nothing to optimize because SIZE_OF_GRID_CELL is too big... - if (((end_cell.x_coord - begin_cell.x_coord) > 4) && ((end_cell.y_coord - begin_cell.y_coord) > 4)) - { - VisitCircle(visitor, m, begin_cell, end_cell); - return; - } - - //ALWAYS visit standing cell first!!! Since we deal with small radiuses - //it is very essential to call visitor for standing cell firstly... - m.Visit(*this, visitor); - - // loop the cell range - 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) - { - CellPair cell_pair(x,y); - //lets skip standing cell since we already visited it - if (cell_pair != standing_cell) - { - Cell r_zone(cell_pair); - r_zone.data.Part.nocreate = this->data.Part.nocreate; - m.Visit(r_zone, visitor); - } - } - } -} - -template -inline void -Cell::Visit(const CellPair& l, TypeContainerVisitor &visitor, Map &m, const WorldObject &obj, float radius) const -{ - //we should increase search radius by object's radius, otherwise - //we could have problems with huge creatures, which won't attack nearest players etc - Visit(l, visitor, m, radius + obj.GetObjectSize(), obj.GetPositionX(), obj.GetPositionY()); -} - -template -inline void -Cell::VisitCircle(TypeContainerVisitor &visitor, Map &m, const CellPair& begin_cell, const CellPair& end_cell) const -{ - //here is an algorithm for 'filling' circum-squared octagon - uint32 x_shift = (uint32)ceilf((end_cell.x_coord - begin_cell.x_coord) * 0.3f - 0.5f); - //lets calculate x_start/x_end coords for central strip... - const uint32 x_start = begin_cell.x_coord + x_shift; - const uint32 x_end = end_cell.x_coord - x_shift; - - //visit central strip with constant width... - for (uint32 x = x_start; x <= x_end; ++x) - { - for (uint32 y = begin_cell.y_coord; y <= end_cell.y_coord; ++y) - { - CellPair cell_pair(x,y); - Cell r_zone(cell_pair); - r_zone.data.Part.nocreate = this->data.Part.nocreate; - m.Visit(r_zone, visitor); - } - } - - //if x_shift == 0 then we have too small cell area, which were already - //visited at previous step, so just return from procedure... - if (x_shift == 0) - return; - - uint32 y_start = end_cell.y_coord; - uint32 y_end = begin_cell.y_coord; - //now we are visiting borders of an octagon... - for (uint32 step = 1; step <= (x_start - begin_cell.x_coord); ++step) - { - //each step reduces strip height by 2 cells... - y_end += 1; - y_start -= 1; - for (uint32 y = y_start; y >= y_end; --y) - { - //we visit cells symmetrically from both sides, heading from center to sides and from up to bottom - //e.g. filling 2 trapezoids after filling central cell strip... - CellPair cell_pair_left(x_start - step, y); - Cell r_zone_left(cell_pair_left); - r_zone_left.data.Part.nocreate = this->data.Part.nocreate; - m.Visit(r_zone_left, visitor); - - //right trapezoid cell visit - CellPair cell_pair_right(x_end + step, y); - Cell r_zone_right(cell_pair_right); - r_zone_right.data.Part.nocreate = this->data.Part.nocreate; - m.Visit(r_zone_right, visitor); - } - } -} -#endif - diff --git a/src/server/game/Grids/Cells/Cell.h b/src/server/game/Grids/Cells/Cell.h new file mode 100644 index 00000000000..49e0329ace6 --- /dev/null +++ b/src/server/game/Grids/Cells/Cell.h @@ -0,0 +1,177 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_CELL_H +#define TRINITY_CELL_H + +#include + +#include "GameSystem/TypeContainer.h" +#include "GameSystem/TypeContainerVisitor.h" + +#include "GridDefines.h" + +class Map; +class WorldObject; + +enum District +{ + UPPER_DISTRICT = 1, + LOWER_DISTRICT = 1 << 1, + LEFT_DISTRICT = 1 << 2, + RIGHT_DISTRICT = 1 << 3, + CENTER_DISTRICT = 1 << 4, + UPPER_LEFT_DISTRICT = (UPPER_DISTRICT | LEFT_DISTRICT), + UPPER_RIGHT_DISTRICT = (UPPER_DISTRICT | RIGHT_DISTRICT), + LOWER_LEFT_DISTRICT = (LOWER_DISTRICT | LEFT_DISTRICT), + LOWER_RIGHT_DISTRICT = (LOWER_DISTRICT | RIGHT_DISTRICT), + ALL_DISTRICT = (UPPER_DISTRICT | LOWER_DISTRICT | LEFT_DISTRICT | RIGHT_DISTRICT | CENTER_DISTRICT) +}; + +struct CellArea +{ + CellArea() : right_offset(0), left_offset(0), upper_offset(0), lower_offset(0) {} + CellArea(int right, int left, int upper, int lower) : right_offset(right), left_offset(left), upper_offset(upper), lower_offset(lower) {} + bool operator!() const { return !right_offset && !left_offset && !upper_offset && !lower_offset; } + + void ResizeBorders(CellPair& begin_cell, CellPair& end_cell) const + { + begin_cell << left_offset; + begin_cell -= lower_offset; + end_cell >> right_offset; + end_cell += upper_offset; + } + + int right_offset; + int left_offset; + int upper_offset; + int lower_offset; +}; + +struct Cell +{ + Cell() { data.All = 0; } + Cell(const Cell &cell) { data.All = cell.data.All; } + explicit Cell(CellPair const& p); + + void operator|=(Cell &cell) + { + data.Part.reserved = 0; + cell.data.Part.reserved = 0; + uint32 x, y, old_x, old_y; + Compute(x, y); + cell.Compute(old_x, old_y); + + if (std::abs(int(x-old_x)) > 1 || std::abs(int(y-old_y)) > 1) + { + data.Part.reserved = ALL_DISTRICT; + cell.data.Part.reserved = ALL_DISTRICT; + return; + } + + if (x < old_x) + { + data.Part.reserved |= LEFT_DISTRICT; + cell.data.Part.reserved |= RIGHT_DISTRICT; + } + else if (old_x < x) + { + data.Part.reserved |= RIGHT_DISTRICT; + cell.data.Part.reserved |= LEFT_DISTRICT; + } + if (y < old_y) + { + data.Part.reserved |= UPPER_DISTRICT; + cell.data.Part.reserved |= LOWER_DISTRICT; + } + else if (old_y < y) + { + data.Part.reserved |= LOWER_DISTRICT; + cell.data.Part.reserved |= UPPER_DISTRICT; + } + } + + void Compute(uint32 &x, uint32 &y) const + { + x = data.Part.grid_x*MAX_NUMBER_OF_CELLS + data.Part.cell_x; + y = data.Part.grid_y*MAX_NUMBER_OF_CELLS + data.Part.cell_y; + } + + bool DiffCell(const Cell &cell) const + { + return(data.Part.cell_x != cell.data.Part.cell_x || + data.Part.cell_y != cell.data.Part.cell_y); + } + + bool DiffGrid(const Cell &cell) const + { + return(data.Part.grid_x != cell.data.Part.grid_x || + data.Part.grid_y != cell.data.Part.grid_y); + } + + uint32 CellX() const { return data.Part.cell_x; } + uint32 CellY() const { return data.Part.cell_y; } + uint32 GridX() const { return data.Part.grid_x; } + uint32 GridY() const { return data.Part.grid_y; } + bool NoCreate() const { return data.Part.nocreate; } + void SetNoCreate() { data.Part.nocreate = 1; } + + CellPair cellPair() const + { + return CellPair( + data.Part.grid_x*MAX_NUMBER_OF_CELLS+data.Part.cell_x, + data.Part.grid_y*MAX_NUMBER_OF_CELLS+data.Part.cell_y); + } + + Cell& operator=(const Cell &cell) + { + this->data.All = cell.data.All; + return *this; + } + + bool operator == (const Cell &cell) const { return (data.All == cell.data.All); } + bool operator != (const Cell &cell) const { return !operator == (cell); } + union + { + struct + { + unsigned grid_x : 6; + unsigned grid_y : 6; + unsigned cell_x : 6; + unsigned cell_y : 6; + unsigned nocreate : 1; + unsigned reserved : 7; + } Part; + uint32 All; + } data; + + template void Visit(const CellPair&, TypeContainerVisitor &visitor, Map &) const; + template void Visit(const CellPair&, TypeContainerVisitor &visitor, Map &, const WorldObject&, float) const; + template void Visit(const CellPair&, TypeContainerVisitor &visitor, Map &, float, float, float) const; + + static CellArea CalculateCellArea(const WorldObject &obj, float radius); + static CellArea CalculateCellArea(float x, float y, float radius); + +private: + template void VisitCircle(TypeContainerVisitor &, Map &, const CellPair&, const CellPair&) const; +}; + +#endif + diff --git a/src/server/game/Grids/Cells/CellImpl.h b/src/server/game/Grids/Cells/CellImpl.h new file mode 100644 index 00000000000..d906e81a5c9 --- /dev/null +++ b/src/server/game/Grids/Cells/CellImpl.h @@ -0,0 +1,297 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_CELLIMPL_H +#define TRINITY_CELLIMPL_H + +#include + +#include "Cell.h" +#include "Map.h" +#include "Object.h" + +inline Cell::Cell(CellPair const& p) +{ + data.Part.grid_x = p.x_coord / MAX_NUMBER_OF_CELLS; + data.Part.grid_y = p.y_coord / MAX_NUMBER_OF_CELLS; + data.Part.cell_x = p.x_coord % MAX_NUMBER_OF_CELLS; + data.Part.cell_y = p.y_coord % MAX_NUMBER_OF_CELLS; + data.Part.nocreate = 0; + data.Part.reserved = 0; +} + +template +inline void +Cell::Visit(const CellPair& standing_cell, TypeContainerVisitor &visitor, Map &m) const +{ + if (standing_cell.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || standing_cell.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP) + return; + + uint16 district = (District)this->data.Part.reserved; + if (district == CENTER_DISTRICT) + { + m.Visit(*this, visitor); + return; + } + + // set up the cell range based on the district + // the overloaded operators handle range checking + CellPair begin_cell = standing_cell; + CellPair end_cell = standing_cell; + + switch(district) + { + case ALL_DISTRICT: + { + begin_cell << 1; begin_cell -= 1; // upper left + end_cell >> 1; end_cell += 1; // lower right + break; + } + case UPPER_LEFT_DISTRICT: + { + begin_cell << 1; begin_cell -= 1; // upper left + break; + } + case UPPER_RIGHT_DISTRICT: + { + begin_cell -= 1; // up + end_cell >> 1; // right + break; + } + case LOWER_LEFT_DISTRICT: + { + begin_cell << 1; // left + end_cell += 1; // down + break; + } + case LOWER_RIGHT_DISTRICT: + { + end_cell >> 1; end_cell += 1; // lower right + break; + } + case LEFT_DISTRICT: + { + begin_cell -= 1; // up + end_cell >> 1; end_cell += 1; // lower right + break; + } + case RIGHT_DISTRICT: + { + begin_cell << 1; begin_cell -= 1; // upper left + end_cell += 1; // down + break; + } + case UPPER_DISTRICT: + { + begin_cell << 1; begin_cell -= 1; // upper left + end_cell >> 1; // right + break; + } + case LOWER_DISTRICT: + { + begin_cell << 1; // left + end_cell >> 1; end_cell += 1; // lower right + break; + } + default: + { + assert(false); + break; + } + } + + // loop the cell range + 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++) + { + CellPair cell_pair(x,y); + Cell r_zone(cell_pair); + r_zone.data.Part.nocreate = this->data.Part.nocreate; + m.Visit(r_zone, visitor); + } + } +} + +inline int CellHelper(const float radius) +{ + if (radius < 1.0f) + return 0; + + return (int)ceilf(radius/SIZE_OF_GRID_CELL); +} + +inline CellArea Cell::CalculateCellArea(const WorldObject &obj, float radius) +{ + return Cell::CalculateCellArea(obj.GetPositionX(), obj.GetPositionY(), radius); +} + +inline CellArea Cell::CalculateCellArea(float x, float y, float radius) +{ + if (radius <= 0.0f) + return CellArea(); + + //lets calculate object coord offsets from cell borders. + //TODO: add more correct/generic method for this task + const float x_offset = (x - CENTER_GRID_CELL_OFFSET)/SIZE_OF_GRID_CELL; + const float y_offset = (y - CENTER_GRID_CELL_OFFSET)/SIZE_OF_GRID_CELL; + + const float x_val = floor(x_offset + CENTER_GRID_CELL_ID + 0.5f); + const float y_val = floor(y_offset + CENTER_GRID_CELL_ID + 0.5f); + + const float x_off = (x_offset - x_val + CENTER_GRID_CELL_ID) * SIZE_OF_GRID_CELL; + const float y_off = (y_offset - y_val + CENTER_GRID_CELL_ID) * SIZE_OF_GRID_CELL; + + const float tmp_diff = radius - CENTER_GRID_CELL_OFFSET; + //lets calculate upper/lower/right/left corners for cell search + int right = CellHelper(tmp_diff + x_off); + int left = CellHelper(tmp_diff - x_off); + int upper = CellHelper(tmp_diff + y_off); + int lower = CellHelper(tmp_diff - y_off); + + return CellArea(right, left, upper, lower); +} + +template +inline void +Cell::Visit(const CellPair& standing_cell, TypeContainerVisitor &visitor, Map &m, float radius, float x_off, float y_off) const +{ + if (standing_cell.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || standing_cell.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP) + return; + + //no jokes here... Actually placing ASSERT() here was good idea, but + //we had some problems with DynamicObjects, which pass radius = 0.0f (DB issue?) + //maybe it is better to just return when radius <= 0.0f? + if (radius <= 0.0f) + { + m.Visit(*this, visitor); + return; + } + //lets limit the upper value for search radius + if (radius > 333.0f) + radius = 333.0f; + + //lets calculate object coord offsets from cell borders. + CellArea area = Cell::CalculateCellArea(x_off, y_off, radius); + //if radius fits inside standing cell + if (!area) + { + m.Visit(*this, visitor); + return; + } + + CellPair begin_cell = standing_cell; + CellPair end_cell = standing_cell; + + area.ResizeBorders(begin_cell, end_cell); + //visit all cells, found in CalculateCellArea() + //if radius is known to reach cell area more than 4x4 then we should call optimized VisitCircle + //currently this technique works with MAX_NUMBER_OF_CELLS 16 and higher, with lower values + //there are nothing to optimize because SIZE_OF_GRID_CELL is too big... + if (((end_cell.x_coord - begin_cell.x_coord) > 4) && ((end_cell.y_coord - begin_cell.y_coord) > 4)) + { + VisitCircle(visitor, m, begin_cell, end_cell); + return; + } + + //ALWAYS visit standing cell first!!! Since we deal with small radiuses + //it is very essential to call visitor for standing cell firstly... + m.Visit(*this, visitor); + + // loop the cell range + 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) + { + CellPair cell_pair(x,y); + //lets skip standing cell since we already visited it + if (cell_pair != standing_cell) + { + Cell r_zone(cell_pair); + r_zone.data.Part.nocreate = this->data.Part.nocreate; + m.Visit(r_zone, visitor); + } + } + } +} + +template +inline void +Cell::Visit(const CellPair& l, TypeContainerVisitor &visitor, Map &m, const WorldObject &obj, float radius) const +{ + //we should increase search radius by object's radius, otherwise + //we could have problems with huge creatures, which won't attack nearest players etc + Visit(l, visitor, m, radius + obj.GetObjectSize(), obj.GetPositionX(), obj.GetPositionY()); +} + +template +inline void +Cell::VisitCircle(TypeContainerVisitor &visitor, Map &m, const CellPair& begin_cell, const CellPair& end_cell) const +{ + //here is an algorithm for 'filling' circum-squared octagon + uint32 x_shift = (uint32)ceilf((end_cell.x_coord - begin_cell.x_coord) * 0.3f - 0.5f); + //lets calculate x_start/x_end coords for central strip... + const uint32 x_start = begin_cell.x_coord + x_shift; + const uint32 x_end = end_cell.x_coord - x_shift; + + //visit central strip with constant width... + for (uint32 x = x_start; x <= x_end; ++x) + { + for (uint32 y = begin_cell.y_coord; y <= end_cell.y_coord; ++y) + { + CellPair cell_pair(x,y); + Cell r_zone(cell_pair); + r_zone.data.Part.nocreate = this->data.Part.nocreate; + m.Visit(r_zone, visitor); + } + } + + //if x_shift == 0 then we have too small cell area, which were already + //visited at previous step, so just return from procedure... + if (x_shift == 0) + return; + + uint32 y_start = end_cell.y_coord; + uint32 y_end = begin_cell.y_coord; + //now we are visiting borders of an octagon... + for (uint32 step = 1; step <= (x_start - begin_cell.x_coord); ++step) + { + //each step reduces strip height by 2 cells... + y_end += 1; + y_start -= 1; + for (uint32 y = y_start; y >= y_end; --y) + { + //we visit cells symmetrically from both sides, heading from center to sides and from up to bottom + //e.g. filling 2 trapezoids after filling central cell strip... + CellPair cell_pair_left(x_start - step, y); + Cell r_zone_left(cell_pair_left); + r_zone_left.data.Part.nocreate = this->data.Part.nocreate; + m.Visit(r_zone_left, visitor); + + //right trapezoid cell visit + CellPair cell_pair_right(x_end + step, y); + Cell r_zone_right(cell_pair_right); + r_zone_right.data.Part.nocreate = this->data.Part.nocreate; + m.Visit(r_zone_right, visitor); + } + } +} +#endif + diff --git a/src/server/game/Grids/Grid.h b/src/server/game/Grids/Grid.h new file mode 100644 index 00000000000..65bf3c92f9d --- /dev/null +++ b/src/server/game/Grids/Grid.h @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_GRID_H +#define TRINITY_GRID_H + +/* + @class Grid + Grid is a logical segment of the game world represented inside TrinIty. + Grid is bind at compile time to a particular type of object which + we call it the object of interested. There are many types of loader, + specially, dynamic loader, static loader, or on-demand loader. There's + a subtle difference between dynamic loader and on-demand loader but + this is implementation specific to the loader class. From the + Grid's perspective, the loader meets its API requirement is suffice. +*/ + +#include "Platform/Define.h" +#include "Policies/ThreadingModel.h" +#include "TypeContainer.h" +#include "TypeContainerVisitor.h" + +// forward declaration +template class GridLoader; + +template +< +class ACTIVE_OBJECT, +class WORLD_OBJECT_TYPES, +class GRID_OBJECT_TYPES, +class ThreadModel = Trinity::SingleThreaded +> +class Grid +{ + // allows the GridLoader to access its internals + template friend class GridLoader; + public: + + /** destructor to clean up its resources. This includes unloading the + grid if it has not been unload. + */ + ~Grid() {} + + /** an object of interested enters the grid + */ + template void AddWorldObject(SPECIFIC_OBJECT *obj) + { + if(!i_objects.template insert(obj)) + assert(false); + } + + /** an object of interested exits the grid + */ + template void RemoveWorldObject(SPECIFIC_OBJECT *obj) + { + if(!i_objects.template remove(obj)) + assert(false); + } + + /** Refreshes/update the grid. This required for remote grids. + */ + void RefreshGrid(void) { /* TBI */} + + /** Locks a grid. Any object enters must wait until the grid is unlock. + */ + void LockGrid(void) { /* TBI */ } + + /** Unlocks the grid. + */ + void UnlockGrid(void) { /* TBI */ } + + /** Grid visitor for grid objects + */ + template void Visit(TypeContainerVisitor > &visitor) + { + visitor.Visit(i_container); + } + + /** Grid visitor for world objects + */ + template void Visit(TypeContainerVisitor > &visitor) + { + visitor.Visit(i_objects); + } + + /** Returns the number of object within the grid. + */ + unsigned int ActiveObjectsInGrid(void) const { return /*m_activeGridObjects.size()+*/i_objects.template Count(); } + + /** Inserts a container type object into the grid. + */ + template void AddGridObject(SPECIFIC_OBJECT *obj) + { + if(!i_container.template insert(obj)) + assert(false); + } + + /** Removes a containter type object from the grid + */ + template void RemoveGridObject(SPECIFIC_OBJECT *obj) + { + if(!i_container.template remove(obj)) + assert(false); + } + + /*bool NoWorldObjectInGrid() const + { + return i_objects.GetElements().isEmpty(); + } + + bool NoGridObjectInGrid() const + { + return i_container.GetElements().isEmpty(); + }*/ + private: + + typedef typename ThreadModel::Lock Guard; + typedef typename ThreadModel::VolatileType VolatileType; + + TypeMapContainer i_container; + TypeMapContainer i_objects; + //typedef std::set ActiveGridObjects; + //ActiveGridObjects m_activeGridObjects; +}; +#endif + diff --git a/src/server/game/Grids/GridLoader.h b/src/server/game/Grids/GridLoader.h new file mode 100644 index 00000000000..03fa0f5b813 --- /dev/null +++ b/src/server/game/Grids/GridLoader.h @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_GRIDLOADER_H +#define TRINITY_GRIDLOADER_H + +/** + @class GridLoader + The GridLoader is working in conjuction with the Grid and responsible + for loading and unloading object-types (one or more) when objects + enters a grid. Unloading is scheduled and might be canceled if + an interested object re-enters. GridLoader does not do the actuall + loading and unloading but implements as a template pattern that + delicate its loading and unloading for the actualy loader and unloader. + GridLoader manages the grid (both local and remote). + */ + +#include "Platform/Define.h" +#include "Grid.h" +#include "TypeContainerVisitor.h" + +template +< +class ACTIVE_OBJECT, +class WORLD_OBJECT_TYPES, +class GRID_OBJECT_TYPES +> +class GridLoader +{ + public: + + /** Loads the grid + */ + template + void Load(Grid &grid, LOADER &loader) + { + grid.LockGrid(); + loader.Load(grid); + grid.UnlockGrid(); + } + + /** Stop the grid + */ + template + void Stop(Grid &grid, STOPER &stoper) + { + grid.LockGrid(); + stoper.Stop(grid); + grid.UnlockGrid(); + } + /** Unloads the grid + */ + template + void Unload(Grid &grid, UNLOADER &unloader) + { + grid.LockGrid(); + unloader.Unload(grid); + grid.UnlockGrid(); + } +}; +#endif + diff --git a/src/server/game/Grids/GridNotifiers.cpp b/src/server/game/Grids/GridNotifiers.cpp deleted file mode 100644 index b10dfa8791e..00000000000 --- a/src/server/game/Grids/GridNotifiers.cpp +++ /dev/null @@ -1,353 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#include "GridNotifiers.h" -#include "GridNotifiersImpl.h" -#include "WorldPacket.h" -#include "WorldSession.h" -#include "UpdateData.h" -#include "Item.h" -#include "Map.h" -#include "Transports.h" -#include "ObjectAccessor.h" -#include "CellImpl.h" - -using namespace Trinity; - -void -VisibleNotifier::SendToSelf() -{ - // at this moment i_clientGUIDs have guids that not iterate at grid level checks - // but exist one case when this possible and object not out of range: transports - if (Transport* transport = i_player.GetTransport()) - for (Transport::PlayerSet::const_iterator itr = transport->GetPassengers().begin();itr != transport->GetPassengers().end();++itr) - { - if (vis_guids.find((*itr)->GetGUID()) != vis_guids.end()) - { - vis_guids.erase((*itr)->GetGUID()); - - i_player.UpdateVisibilityOf((*itr), i_data, i_visibleNow); - - if (!(*itr)->isNeedNotify(NOTIFY_VISIBILITY_CHANGED)) - (*itr)->UpdateVisibilityOf(&i_player); - } - } - - for (Player::ClientGUIDs::const_iterator it = vis_guids.begin();it != vis_guids.end(); ++it) - { - i_player.m_clientGUIDs.erase(*it); - i_data.AddOutOfRangeGUID(*it); - - if (IS_PLAYER_GUID(*it)) - { - Player* plr = ObjectAccessor::FindPlayer(*it); - if (plr && plr->IsInWorld() && !plr->isNeedNotify(NOTIFY_VISIBILITY_CHANGED)) - plr->UpdateVisibilityOf(&i_player); - } - } - - if (!i_data.HasData()) - return; - - WorldPacket packet; - i_data.BuildPacket(&packet); - i_player.GetSession()->SendPacket(&packet); - - for (std::set::const_iterator it = i_visibleNow.begin(); it != i_visibleNow.end(); ++it) - i_player.SendInitialVisiblePackets(*it); -} - -void -VisibleChangesNotifier::Visit(PlayerMapType &m) -{ - for (PlayerMapType::iterator iter=m.begin(); iter != m.end(); ++iter) - { - if (iter->getSource() == &i_object) - continue; - - iter->getSource()->UpdateVisibilityOf(&i_object); - - if (!iter->getSource()->GetSharedVisionList().empty()) - for (SharedVisionList::const_iterator i = iter->getSource()->GetSharedVisionList().begin(); - i != iter->getSource()->GetSharedVisionList().end(); ++i) - if ((*i)->m_seer == iter->getSource()) - (*i)->UpdateVisibilityOf(&i_object); - } -} - -void -VisibleChangesNotifier::Visit(CreatureMapType &m) -{ - for (CreatureMapType::iterator iter = m.begin(); iter != m.end(); ++iter) - if (!iter->getSource()->GetSharedVisionList().empty()) - for (SharedVisionList::const_iterator i = iter->getSource()->GetSharedVisionList().begin(); - i != iter->getSource()->GetSharedVisionList().end(); ++i) - if ((*i)->m_seer == iter->getSource()) - (*i)->UpdateVisibilityOf(&i_object); -} - -void -VisibleChangesNotifier::Visit(DynamicObjectMapType &m) -{ - for (DynamicObjectMapType::iterator iter = m.begin(); iter != m.end(); ++iter) - if (IS_PLAYER_GUID(iter->getSource()->GetCasterGUID())) - if (Player* caster = (Player*)iter->getSource()->GetCaster()) - if (caster->m_seer == iter->getSource()) - caster->UpdateVisibilityOf(&i_object); -} - -inline void CreatureUnitRelocationWorker(Creature* c, Unit* u) -{ - if (!u->isAlive() || !c->isAlive() || c == u || u->isInFlight()) - return; - - if (c->HasReactState(REACT_AGGRESSIVE) && !c->hasUnitState(UNIT_STAT_SIGHTLESS)) - if (c->_IsWithinDist(u, c->m_SightDistance, true) && c->IsAIEnabled) - c->AI()->MoveInLineOfSight_Safe(u); -} - -void PlayerRelocationNotifier::Visit(PlayerMapType &m) -{ - for (PlayerMapType::iterator iter=m.begin(); iter != m.end(); ++iter) - { - Player* plr = iter->getSource(); - - vis_guids.erase(plr->GetGUID()); - - i_player.UpdateVisibilityOf(plr,i_data,i_visibleNow); - - if (plr->m_seer->isNeedNotify(NOTIFY_VISIBILITY_CHANGED)) - continue; - - plr->UpdateVisibilityOf(&i_player); - } -} - -void PlayerRelocationNotifier::Visit(CreatureMapType &m) -{ - bool relocated_for_ai = (&i_player == i_player.m_seer); - - for (CreatureMapType::iterator iter=m.begin(); iter != m.end(); ++iter) - { - Creature * c = iter->getSource(); - - vis_guids.erase(c->GetGUID()); - - i_player.UpdateVisibilityOf(c,i_data,i_visibleNow); - - if (relocated_for_ai && !c->isNeedNotify(NOTIFY_VISIBILITY_CHANGED)) - CreatureUnitRelocationWorker(c, &i_player); - } -} - -void CreatureRelocationNotifier::Visit(PlayerMapType &m) -{ - for (PlayerMapType::iterator iter=m.begin(); iter != m.end(); ++iter) - { - Player * pl = iter->getSource(); - - if (!pl->m_seer->isNeedNotify(NOTIFY_VISIBILITY_CHANGED)) - pl->UpdateVisibilityOf(&i_creature); - - CreatureUnitRelocationWorker(&i_creature, pl); - } -} - -void CreatureRelocationNotifier::Visit(CreatureMapType &m) -{ - if (!i_creature.isAlive()) - return; - - for (CreatureMapType::iterator iter=m.begin(); iter != m.end(); ++iter) - { - Creature* c = iter->getSource(); - CreatureUnitRelocationWorker(&i_creature, c); - - if (!c->isNeedNotify(NOTIFY_VISIBILITY_CHANGED)) - CreatureUnitRelocationWorker(c, &i_creature); - } -} - -void DelayedUnitRelocation::Visit(CreatureMapType &m) -{ - for (CreatureMapType::iterator iter = m.begin(); iter != m.end(); ++iter) - { - Creature * unit = iter->getSource(); - if (!unit->isNeedNotify(NOTIFY_VISIBILITY_CHANGED)) - continue; - - CreatureRelocationNotifier relocate(*unit); - - TypeContainerVisitor c2world_relocation(relocate); - TypeContainerVisitor c2grid_relocation(relocate); - - cell.Visit(p, c2world_relocation, i_map, *unit, i_radius); - cell.Visit(p, c2grid_relocation, i_map, *unit, i_radius); - } -} - -void DelayedUnitRelocation::Visit(PlayerMapType &m) -{ - for (PlayerMapType::iterator iter = m.begin(); iter != m.end(); ++iter) - { - Player * player = iter->getSource(); - WorldObject const *viewPoint = player->m_seer; - - if (!viewPoint->isNeedNotify(NOTIFY_VISIBILITY_CHANGED)) - continue; - - if (player != viewPoint && !viewPoint->IsPositionValid()) - continue; - - CellPair pair2(Trinity::ComputeCellPair(viewPoint->GetPositionX(), viewPoint->GetPositionY())); - Cell cell2(pair2); - //cell.SetNoCreate(); need load cells around viewPoint or player, that's why its commented - - PlayerRelocationNotifier relocate(*player); - TypeContainerVisitor c2world_relocation(relocate); - TypeContainerVisitor c2grid_relocation(relocate); - - cell2.Visit(pair2, c2world_relocation, i_map, *viewPoint, i_radius); - cell2.Visit(pair2, c2grid_relocation, i_map, *viewPoint, i_radius); - - relocate.SendToSelf(); - } -} - -void AIRelocationNotifier::Visit(CreatureMapType &m) -{ - for (CreatureMapType::iterator iter = m.begin(); iter != m.end(); ++iter) - { - Creature *c = iter->getSource(); - CreatureUnitRelocationWorker(c, &i_unit); - if (isCreature) - CreatureUnitRelocationWorker((Creature*)&i_unit, c); - } -} - -void -MessageDistDeliverer::Visit(PlayerMapType &m) -{ - for (PlayerMapType::iterator iter = m.begin(); iter != m.end(); ++iter) - { - Player *target = iter->getSource(); - if (!target->InSamePhase(i_phaseMask)) - continue; - - if (target->GetExactDistSq(i_source) > i_distSq) - continue; - - // Send packet to all who are sharing the player's vision - if (!target->GetSharedVisionList().empty()) - { - SharedVisionList::const_iterator i = target->GetSharedVisionList().begin(); - for (; i != target->GetSharedVisionList().end(); ++i) - if ((*i)->m_seer == target) - SendPacket(*i); - } - - if (target->m_seer == target || target->GetVehicle()) - SendPacket(target); - } -} - -void -MessageDistDeliverer::Visit(CreatureMapType &m) -{ - for (CreatureMapType::iterator iter = m.begin(); iter != m.end(); ++iter) - { - if (!iter->getSource()->InSamePhase(i_phaseMask)) - continue; - - if (iter->getSource()->GetExactDistSq(i_source) > i_distSq) - continue; - - // Send packet to all who are sharing the creature's vision - if (!iter->getSource()->GetSharedVisionList().empty()) - { - SharedVisionList::const_iterator i = iter->getSource()->GetSharedVisionList().begin(); - for (; i != iter->getSource()->GetSharedVisionList().end(); ++i) - if ((*i)->m_seer == iter->getSource()) - SendPacket(*i); - } - } -} - -void -MessageDistDeliverer::Visit(DynamicObjectMapType &m) -{ - for (DynamicObjectMapType::iterator iter = m.begin(); iter != m.end(); ++iter) - { - if (!iter->getSource()->InSamePhase(i_phaseMask)) - continue; - - if (iter->getSource()->GetExactDistSq(i_source) > i_distSq) - continue; - - if (IS_PLAYER_GUID(iter->getSource()->GetCasterGUID())) - { - // Send packet back to the caster if the caster has vision of dynamic object - Player* caster = (Player*)iter->getSource()->GetCaster(); - if (caster && caster->m_seer == iter->getSource()) - SendPacket(caster); - } - } -} - -/* -void -MessageDistDeliverer::VisitObject(Player* plr) -{ - if (!i_ownTeamOnly || (i_source.GetTypeId() == TYPEID_PLAYER && plr->GetTeam() == ((Player&)i_source).GetTeam())) - { - SendPacket(plr); - } -} -*/ - -template void -ObjectUpdater::Visit(GridRefManager &m) -{ - for (typename GridRefManager::iterator iter = m.begin(); iter != m.end(); ++iter) - { - if (iter->getSource()->IsInWorld()) - iter->getSource()->Update(i_timeDiff); - } -} - -bool CannibalizeObjectCheck::operator()(Corpse* u) -{ - // ignore bones - if (u->GetType() == CORPSE_BONES) - return false; - - Player* owner = ObjectAccessor::FindPlayer(u->GetOwnerGUID()); - - if (!owner || i_funit->IsFriendlyTo(owner)) - return false; - - if (i_funit->IsWithinDistInMap(u, i_range)) - return true; - - return false; -} - -template void ObjectUpdater::Visit(GameObjectMapType &); -template void ObjectUpdater::Visit(DynamicObjectMapType &); diff --git a/src/server/game/Grids/GridNotifiers.h b/src/server/game/Grids/GridNotifiers.h deleted file mode 100644 index b0abf0aae79..00000000000 --- a/src/server/game/Grids/GridNotifiers.h +++ /dev/null @@ -1,1232 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_GRIDNOTIFIERS_H -#define TRINITY_GRIDNOTIFIERS_H - -#include "ObjectGridLoader.h" -#include "UpdateData.h" -#include - -#include "Corpse.h" -#include "Object.h" -#include "DynamicObject.h" -#include "GameObject.h" -#include "Player.h" -#include "Unit.h" -#include "CreatureAI.h" - -class Player; -//class Map; - -namespace Trinity -{ - struct VisibleNotifier - { - Player &i_player; - UpdateData i_data; - std::set i_visibleNow; - Player::ClientGUIDs vis_guids; - - VisibleNotifier(Player &player) : i_player(player), vis_guids(player.m_clientGUIDs) {} - template void Visit(GridRefManager &m); - void SendToSelf(void); - }; - - struct VisibleChangesNotifier - { - WorldObject &i_object; - - explicit VisibleChangesNotifier(WorldObject &object) : i_object(object) {} - template void Visit(GridRefManager &) {} - void Visit(PlayerMapType &); - void Visit(CreatureMapType &); - void Visit(DynamicObjectMapType &); - }; - - struct PlayerRelocationNotifier : public VisibleNotifier - { - PlayerRelocationNotifier(Player &pl) : VisibleNotifier(pl) {} - - template void Visit(GridRefManager &m) { VisibleNotifier::Visit(m); } - void Visit(CreatureMapType &); - void Visit(PlayerMapType &); - }; - - struct CreatureRelocationNotifier - { - Creature &i_creature; - CreatureRelocationNotifier(Creature &c) : i_creature(c) {} - template void Visit(GridRefManager &) {} - void Visit(CreatureMapType &); - void Visit(PlayerMapType &); - }; - - struct DelayedUnitRelocation - { - Map &i_map; - Cell &cell; - CellPair &p; - const float i_radius; - DelayedUnitRelocation(Cell &c, CellPair &pair, Map &map, float radius) : - cell(c), p(pair), i_map(map), i_radius(radius) {} - template void Visit(GridRefManager &) {} - void Visit(CreatureMapType &); - void Visit(PlayerMapType &); - }; - - struct AIRelocationNotifier - { - Unit &i_unit; - bool isCreature; - explicit AIRelocationNotifier(Unit &unit) : i_unit(unit), isCreature(unit.GetTypeId() == TYPEID_UNIT) {} - template void Visit(GridRefManager &) {} - void Visit(CreatureMapType &); - }; - - struct GridUpdater - { - GridType &i_grid; - uint32 i_timeDiff; - GridUpdater(GridType &grid, uint32 diff) : i_grid(grid), i_timeDiff(diff) {} - - template void updateObjects(GridRefManager &m) - { - for (typename GridRefManager::iterator iter = m.begin(); iter != m.end(); ++iter) - iter->getSource()->Update(i_timeDiff); - } - - void Visit(PlayerMapType &m) { updateObjects(m); } - void Visit(CreatureMapType &m){ updateObjects(m); } - void Visit(GameObjectMapType &m) { updateObjects(m); } - void Visit(DynamicObjectMapType &m) { updateObjects(m); } - void Visit(CorpseMapType &m) { updateObjects(m); } - }; - - struct MessageDistDeliverer - { - WorldObject *i_source; - WorldPacket *i_message; - uint32 i_phaseMask; - float i_distSq; - uint32 team; - Player const* skipped_receiver; - MessageDistDeliverer(WorldObject *src, WorldPacket *msg, float dist, bool own_team_only = false, Player const* skipped = NULL) - : i_source(src), i_message(msg), i_distSq(dist * dist), i_phaseMask(src->GetPhaseMask()) - , team((own_team_only && src->GetTypeId() == TYPEID_PLAYER) ? ((Player*)src)->GetTeam() : 0) - , skipped_receiver(skipped) - { - } - void Visit(PlayerMapType &m); - void Visit(CreatureMapType &m); - void Visit(DynamicObjectMapType &m); - template void Visit(GridRefManager &) {} - - void SendPacket(Player* plr) - { - // never send packet to self - if (plr == i_source || (team && plr->GetTeam() != team) || skipped_receiver == plr) - return; - - plr->GetSession()->SendPacket(i_message); - } - }; - - struct ObjectUpdater - { - uint32 i_timeDiff; - explicit ObjectUpdater(const uint32 &diff) : i_timeDiff(diff) {} - template void Visit(GridRefManager &m); - void Visit(PlayerMapType &) {} - void Visit(CorpseMapType &) {} - void Visit(CreatureMapType &); - }; - - // SEARCHERS & LIST SEARCHERS & WORKERS - - // WorldObject searchers & workers - - template - struct WorldObjectSearcher - { - uint32 i_phaseMask; - WorldObject* &i_object; - Check &i_check; - - WorldObjectSearcher(WorldObject const* searcher, WorldObject* & result, Check& check) - : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} - - void Visit(GameObjectMapType &m); - void Visit(PlayerMapType &m); - void Visit(CreatureMapType &m); - void Visit(CorpseMapType &m); - void Visit(DynamicObjectMapType &m); - - template void Visit(GridRefManager &) {} - }; - - template - struct WorldObjectListSearcher - { - uint32 i_phaseMask; - std::list &i_objects; - Check& i_check; - - WorldObjectListSearcher(WorldObject const* searcher, std::list &objects, Check & check) - : i_phaseMask(searcher->GetPhaseMask()), i_objects(objects),i_check(check) {} - - void Visit(PlayerMapType &m); - void Visit(CreatureMapType &m); - void Visit(CorpseMapType &m); - void Visit(GameObjectMapType &m); - void Visit(DynamicObjectMapType &m); - - template void Visit(GridRefManager &) {} - }; - - template - struct WorldObjectWorker - { - uint32 i_phaseMask; - Do const& i_do; - - WorldObjectWorker(WorldObject const* searcher, Do const& _do) - : i_phaseMask(searcher->GetPhaseMask()), i_do(_do) {} - - void Visit(GameObjectMapType &m) - { - for (GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - i_do(itr->getSource()); - } - - void Visit(PlayerMapType &m) - { - for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - i_do(itr->getSource()); - } - void Visit(CreatureMapType &m) - { - for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - i_do(itr->getSource()); - } - - void Visit(CorpseMapType &m) - { - for (CorpseMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - i_do(itr->getSource()); - } - - void Visit(DynamicObjectMapType &m) - { - for (DynamicObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - i_do(itr->getSource()); - } - - template void Visit(GridRefManager &) {} - }; - - // Gameobject searchers - - template - struct GameObjectSearcher - { - uint32 i_phaseMask; - GameObject* &i_object; - Check &i_check; - - GameObjectSearcher(WorldObject const* searcher, GameObject* & result, Check& check) - : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} - - void Visit(GameObjectMapType &m); - - template void Visit(GridRefManager &) {} - }; - - // Last accepted by Check GO if any (Check can change requirements at each call) - template - struct GameObjectLastSearcher - { - uint32 i_phaseMask; - GameObject* &i_object; - Check& i_check; - - GameObjectLastSearcher(WorldObject const* searcher, GameObject* & result, Check& check) - : i_phaseMask(searcher->GetPhaseMask()), i_object(result), i_check(check) {} - - void Visit(GameObjectMapType &m); - - template void Visit(GridRefManager &) {} - }; - - template - struct GameObjectListSearcher - { - uint32 i_phaseMask; - std::list &i_objects; - Check& i_check; - - GameObjectListSearcher(WorldObject const* searcher, std::list &objects, Check & check) - : i_phaseMask(searcher->GetPhaseMask()), i_objects(objects), i_check(check) {} - - void Visit(GameObjectMapType &m); - - template void Visit(GridRefManager &) {} - }; - - // Unit searchers - - // First accepted by Check Unit if any - template - struct UnitSearcher - { - uint32 i_phaseMask; - Unit* &i_object; - Check & i_check; - - UnitSearcher(WorldObject const* searcher, Unit* & result, Check & check) - : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} - - void Visit(CreatureMapType &m); - void Visit(PlayerMapType &m); - - template void Visit(GridRefManager &) {} - }; - - // Last accepted by Check Unit if any (Check can change requirements at each call) - template - struct UnitLastSearcher - { - uint32 i_phaseMask; - Unit* &i_object; - Check & i_check; - - UnitLastSearcher(WorldObject const* searcher, Unit* & result, Check & check) - : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} - - void Visit(CreatureMapType &m); - void Visit(PlayerMapType &m); - - template void Visit(GridRefManager &) {} - }; - - // All accepted by Check units if any - template - struct UnitListSearcher - { - uint32 i_phaseMask; - std::list &i_objects; - Check& i_check; - - UnitListSearcher(WorldObject const* searcher, std::list &objects, Check & check) - : i_phaseMask(searcher->GetPhaseMask()), i_objects(objects),i_check(check) {} - - void Visit(PlayerMapType &m); - void Visit(CreatureMapType &m); - - template void Visit(GridRefManager &) {} - }; - - // Creature searchers - - template - struct CreatureSearcher - { - uint32 i_phaseMask; - Creature* &i_object; - Check & i_check; - - CreatureSearcher(WorldObject const* searcher, Creature* & result, Check & check) - : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} - - void Visit(CreatureMapType &m); - - template void Visit(GridRefManager &) {} - }; - - // Last accepted by Check Creature if any (Check can change requirements at each call) - template - struct CreatureLastSearcher - { - uint32 i_phaseMask; - Creature* &i_object; - Check & i_check; - - CreatureLastSearcher(WorldObject const* searcher, Creature* & result, Check & check) - : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} - - void Visit(CreatureMapType &m); - - template void Visit(GridRefManager &) {} - }; - - template - struct CreatureListSearcher - { - uint32 i_phaseMask; - std::list &i_objects; - Check& i_check; - - CreatureListSearcher(WorldObject const* searcher, std::list &objects, Check & check) - : i_phaseMask(searcher->GetPhaseMask()), i_objects(objects),i_check(check) {} - - void Visit(CreatureMapType &m); - - template void Visit(GridRefManager &) {} - }; - - template - struct CreatureWorker - { - uint32 i_phaseMask; - Do& i_do; - - CreatureWorker(WorldObject const* searcher, Do& _do) - : i_phaseMask(searcher->GetPhaseMask()), i_do(_do) {} - - void Visit(CreatureMapType &m) - { - for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - i_do(itr->getSource()); - } - - template void Visit(GridRefManager &) {} - }; - - // Player searchers - - template - struct PlayerSearcher - { - uint32 i_phaseMask; - Player* &i_object; - Check & i_check; - - PlayerSearcher(WorldObject const* searcher, Player* & result, Check & check) - : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} - - void Visit(PlayerMapType &m); - - template void Visit(GridRefManager &) {} - }; - - template - struct PlayerListSearcher - { - uint32 i_phaseMask; - std::list &i_objects; - Check& i_check; - - PlayerListSearcher(WorldObject const* searcher, std::list &objects, Check & check) - : i_phaseMask(searcher->GetPhaseMask()), i_objects(objects),i_check(check) {} - - void Visit(PlayerMapType &m); - - template void Visit(GridRefManager &) {} - }; - - template - struct PlayerWorker - { - uint32 i_phaseMask; - Do& i_do; - - PlayerWorker(WorldObject const* searcher, Do& _do) - : i_phaseMask(searcher->GetPhaseMask()), i_do(_do) {} - - void Visit(PlayerMapType &m) - { - for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - i_do(itr->getSource()); - } - - template void Visit(GridRefManager &) {} - }; - - template - struct PlayerDistWorker - { - WorldObject const* i_searcher; - float i_dist; - Do& i_do; - - PlayerDistWorker(WorldObject const* searcher, float _dist, Do& _do) - : i_searcher(searcher), i_dist(_dist), i_do(_do) {} - - void Visit(PlayerMapType &m) - { - for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_searcher) && itr->getSource()->IsWithinDist(i_searcher,i_dist)) - i_do(itr->getSource()); - } - - template void Visit(GridRefManager &) {} - }; - - // CHECKS && DO classes - - // WorldObject check classes - class RaiseDeadObjectCheck - { - public: - RaiseDeadObjectCheck(Unit* funit, float range) : i_funit(funit), i_range(range) {} - bool operator()(Creature* u) - { - if (i_funit->GetTypeId() != TYPEID_PLAYER || !((Player*)i_funit)->isHonorOrXPTarget(u) || - u->getDeathState() != CORPSE || u->isDeadByDefault() || u->isInFlight() || - (u->GetCreatureTypeMask() & (1 << (CREATURE_TYPE_HUMANOID-1))) == 0 || - (u->GetDisplayId() != u->GetNativeDisplayId())) - return false; - - return i_funit->IsWithinDistInMap(u, i_range); - } - template bool operator()(NOT_INTERESTED*) { return false; } - private: - Unit* const i_funit; - float i_range; - }; - - class ExplodeCorpseObjectCheck - { - public: - ExplodeCorpseObjectCheck(Unit* funit, float range) : i_funit(funit), i_range(range) {} - bool operator()(Player* u) - { - if (u->getDeathState() != CORPSE || u->isInFlight() || - u->HasAuraType(SPELL_AURA_GHOST) || (u->GetDisplayId() != u->GetNativeDisplayId())) - return false; - - return i_funit->IsWithinDistInMap(u, i_range); - } - bool operator()(Creature* u) - { - if (u->getDeathState() != CORPSE || u->isInFlight() || u->isDeadByDefault() || - (u->GetDisplayId() != u->GetNativeDisplayId()) || - (u->GetCreatureTypeMask() & CREATURE_TYPEMASK_MECHANICAL_OR_ELEMENTAL) != 0) - return false; - - return i_funit->IsWithinDistInMap(u, i_range); - } - template bool operator()(NOT_INTERESTED*) { return false; } - private: - Unit* const i_funit; - float i_range; - }; - - class CannibalizeObjectCheck - { - public: - CannibalizeObjectCheck(Unit* funit, float range) : i_funit(funit), i_range(range) {} - bool operator()(Player* u) - { - if (i_funit->IsFriendlyTo(u) || u->isAlive() || u->isInFlight()) - return false; - - return i_funit->IsWithinDistInMap(u, i_range); - } - bool operator()(Corpse* u); - bool operator()(Creature* u) - { - if (i_funit->IsFriendlyTo(u) || u->isAlive() || u->isInFlight() || - (u->GetCreatureTypeMask() & CREATURE_TYPEMASK_HUMANOID_OR_UNDEAD) == 0) - return false; - - return i_funit->IsWithinDistInMap(u, i_range); - } - template bool operator()(NOT_INTERESTED*) { return false; } - private: - Unit* const i_funit; - float i_range; - }; - - // WorldObject do classes - - class RespawnDo - { - public: - RespawnDo() {} - void operator()(Creature* u) const { u->Respawn(); } - void operator()(GameObject* u) const { u->Respawn(); } - void operator()(WorldObject*) const {} - void operator()(Corpse*) const {} - }; - - // GameObject checks - - class GameObjectFocusCheck - { - public: - GameObjectFocusCheck(Unit const* unit,uint32 focusId) : i_unit(unit), i_focusId(focusId) {} - bool operator()(GameObject* go) const - { - if (go->GetGOInfo()->type != GAMEOBJECT_TYPE_SPELL_FOCUS) - return false; - - if (go->GetGOInfo()->spellFocus.focusId != i_focusId) - return false; - - float dist = (go->GetGOInfo()->spellFocus.dist)/2; - - return go->IsWithinDistInMap(i_unit, dist); - } - private: - Unit const* i_unit; - uint32 i_focusId; - }; - - // Find the nearest Fishing hole and return true only if source object is in range of hole - class NearestGameObjectFishingHole - { - public: - NearestGameObjectFishingHole(WorldObject const& obj, float range) : i_obj(obj), i_range(range) {} - bool operator()(GameObject* go) - { - if (go->GetGOInfo()->type == GAMEOBJECT_TYPE_FISHINGHOLE && go->isSpawned() && i_obj.IsWithinDistInMap(go, i_range) && i_obj.IsWithinDistInMap(go, go->GetGOInfo()->fishinghole.radius)) - { - i_range = i_obj.GetDistance(go); - return true; - } - return false; - } - float GetLastRange() const { return i_range; } - private: - WorldObject const& i_obj; - float i_range; - - // prevent clone - NearestGameObjectFishingHole(NearestGameObjectFishingHole const&); - }; - - class NearestGameObjectCheck - { - public: - NearestGameObjectCheck(WorldObject const& obj) : i_obj(obj), i_range(999) {} - bool operator()(GameObject* go) - { - if (i_obj.IsWithinDistInMap(go, i_range)) - { - i_range = i_obj.GetDistance(go); // use found GO range as new range limit for next check - return true; - } - return false; - } - float GetLastRange() const { return i_range; } - private: - WorldObject const& i_obj; - float i_range; - - // prevent clone this object - NearestGameObjectCheck(NearestGameObjectCheck const&); - }; - - // Success at unit in range, range update for next check (this can be use with GameobjectLastSearcher to find nearest GO) - class NearestGameObjectEntryInObjectRangeCheck - { - public: - NearestGameObjectEntryInObjectRangeCheck(WorldObject const& obj,uint32 entry, float range) : i_obj(obj), i_entry(entry), i_range(range) {} - bool operator()(GameObject* go) - { - if (go->GetEntry() == i_entry && i_obj.IsWithinDistInMap(go, i_range)) - { - i_range = i_obj.GetDistance(go); // use found GO range as new range limit for next check - return true; - } - return false; - } - float GetLastRange() const { return i_range; } - private: - WorldObject const& i_obj; - uint32 i_entry; - float i_range; - - // prevent clone this object - NearestGameObjectEntryInObjectRangeCheck(NearestGameObjectEntryInObjectRangeCheck const&); - }; - - class GameObjectWithDbGUIDCheck - { - public: - GameObjectWithDbGUIDCheck(WorldObject const& obj,uint32 db_guid) : i_obj(obj), i_db_guid(db_guid) {} - bool operator()(GameObject const* go) const - { - return go->GetDBTableGUIDLow() == i_db_guid; - } - private: - WorldObject const& i_obj; - uint32 i_db_guid; - }; - - // Unit checks - - class MostHPMissingInRange - { - public: - MostHPMissingInRange(Unit const* obj, float range, uint32 hp) : i_obj(obj), i_range(range), i_hp(hp) {} - bool operator()(Unit* u) - { - if (u->isAlive() && u->isInCombat() && !i_obj->IsHostileTo(u) && i_obj->IsWithinDistInMap(u, i_range) && u->GetMaxHealth() - u->GetHealth() > i_hp) - { - i_hp = u->GetMaxHealth() - u->GetHealth(); - return true; - } - return false; - } - private: - Unit const* i_obj; - float i_range; - uint32 i_hp; - }; - - class FriendlyCCedInRange - { - public: - FriendlyCCedInRange(Unit const* obj, float range) : i_obj(obj), i_range(range) {} - bool operator()(Unit* u) - { - if (u->isAlive() && u->isInCombat() && !i_obj->IsHostileTo(u) && i_obj->IsWithinDistInMap(u, i_range) && - (u->isFeared() || u->isCharmed() || u->isFrozen() || u->hasUnitState(UNIT_STAT_STUNNED) || u->hasUnitState(UNIT_STAT_CONFUSED))) - { - return true; - } - return false; - } - private: - Unit const* i_obj; - float i_range; - }; - - class FriendlyMissingBuffInRange - { - public: - FriendlyMissingBuffInRange(Unit const* obj, float range, uint32 spellid) : i_obj(obj), i_range(range), i_spell(spellid) {} - bool operator()(Unit* u) - { - if (u->isAlive() && u->isInCombat() && !i_obj->IsHostileTo(u) && i_obj->IsWithinDistInMap(u, i_range) && - !(u->HasAura(i_spell))) - { - return true; - } - return false; - } - private: - Unit const* i_obj; - float i_range; - uint32 i_spell; - }; - - class AnyUnfriendlyUnitInObjectRangeCheck - { - public: - AnyUnfriendlyUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) : i_obj(obj), i_funit(funit), i_range(range) {} - bool operator()(Unit* u) - { - if (u->isAlive() && i_obj->IsWithinDistInMap(u, i_range) && !i_funit->IsFriendlyTo(u)) - return true; - else - return false; - } - private: - WorldObject const* i_obj; - Unit const* i_funit; - float i_range; - }; - - class AnyUnfriendlyNoTotemUnitInObjectRangeCheck - { - public: - AnyUnfriendlyNoTotemUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) : i_obj(obj), i_funit(funit), i_range(range) {} - bool operator()(Unit* u) - { - if (!u->isAlive()) - return false; - - if (u->GetTypeId() == TYPEID_UNIT && ((Creature*)u)->isTotem()) - return false; - - return i_obj->IsWithinDistInMap(u, i_range) && !i_funit->IsFriendlyTo(u); - } - private: - WorldObject const* i_obj; - Unit const* i_funit; - float i_range; - }; - - class AnyUnfriendlyVisibleUnitInObjectRangeCheck - { - public: - AnyUnfriendlyVisibleUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) - : i_obj(obj), i_funit(funit), i_range(range) {} - - bool operator()(Unit* u) - { - return u->isAlive() - && i_obj->IsWithinDistInMap(u, i_range) - && !i_funit->IsFriendlyTo(u) - && u->isVisibleForOrDetect(i_funit, false); - } - private: - WorldObject const* i_obj; - Unit const* i_funit; - float i_range; - }; - - class CreatureWithDbGUIDCheck - { - public: - CreatureWithDbGUIDCheck(WorldObject const* obj, uint32 lowguid) : i_obj(obj), i_lowguid(lowguid) {} - bool operator()(Creature* u) - { - return u->GetDBTableGUIDLow() == i_lowguid; - } - private: - WorldObject const* i_obj; - uint32 i_lowguid; - }; - - class AnyFriendlyUnitInObjectRangeCheck - { - public: - AnyFriendlyUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) : i_obj(obj), i_funit(funit), i_range(range) {} - bool operator()(Unit* u) - { - if (u->isAlive() && i_obj->IsWithinDistInMap(u, i_range) && i_funit->IsFriendlyTo(u)) - return true; - else - return false; - } - private: - WorldObject const* i_obj; - Unit const* i_funit; - float i_range; - }; - - class AnyUnitInObjectRangeCheck - { - public: - AnyUnitInObjectRangeCheck(WorldObject const* obj, float range) : i_obj(obj), i_range(range) {} - bool operator()(Unit* u) - { - if (u->isAlive() && i_obj->IsWithinDistInMap(u, i_range)) - return true; - - return false; - } - private: - WorldObject const* i_obj; - float i_range; - }; - - // Success at unit in range, range update for next check (this can be use with UnitLastSearcher to find nearest unit) - class NearestAttackableUnitInObjectRangeCheck - { - public: - NearestAttackableUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) : i_obj(obj), i_funit(funit), i_range(range) {} - bool operator()(Unit* u) - { - if (u->isTargetableForAttack() && i_obj->IsWithinDistInMap(u, i_range) && - !i_funit->IsFriendlyTo(u) && u->isVisibleForOrDetect(i_funit,false)) - { - i_range = i_obj->GetDistance(u); // use found unit range as new range limit for next check - return true; - } - - return false; - } - private: - WorldObject const* i_obj; - Unit const* i_funit; - float i_range; - - // prevent clone this object - NearestAttackableUnitInObjectRangeCheck(NearestAttackableUnitInObjectRangeCheck const&); - }; - - class AnyAoETargetUnitInObjectRangeCheck - { - public: - AnyAoETargetUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) - : i_obj(obj), i_funit(funit), i_range(range) - { - Unit const* check = i_funit; - Unit const* owner = i_funit->GetOwner(); - if (owner) - check = owner; - i_targetForPlayer = (check->GetTypeId() == TYPEID_PLAYER); - } - bool operator()(Unit* u) - { - // Check contains checks for: live, non-selectable, non-attackable flags, flight check and GM check, ignore totems - if (!u->isTargetableForAttack()) - return false; - if (u->GetTypeId() == TYPEID_UNIT && ((Creature*)u)->isTotem()) - return false; - - if ((i_targetForPlayer ? !i_funit->IsFriendlyTo(u) : i_funit->IsHostileTo(u))&& i_obj->IsWithinDistInMap(u, i_range)) - return true; - - return false; - } - private: - bool i_targetForPlayer; - WorldObject const* i_obj; - Unit const* i_funit; - float i_range; - }; - - // do attack at call of help to friendly crearture - class CallOfHelpCreatureInRangeDo - { - public: - CallOfHelpCreatureInRangeDo(Unit* funit, Unit* enemy, float range) - : i_funit(funit), i_enemy(enemy), i_range(range) - {} - void operator()(Creature* u) - { - if (u == i_funit) - return; - - if (!u->CanAssistTo(i_funit, i_enemy, false)) - return; - - // too far - if (!u->IsWithinDistInMap(i_enemy, i_range)) - return; - - // only if see assisted creature's enemy - if (!u->IsWithinLOSInMap(i_enemy)) - return; - - if (u->AI()) - u->AI()->AttackStart(i_enemy); - } - private: - Unit* const i_funit; - Unit* const i_enemy; - float i_range; - }; - - struct AnyDeadUnitCheck - { - bool operator()(Unit* u) { return !u->isAlive(); } - }; - - struct AnyStealthedCheck - { - bool operator()(Unit* u) { return u->GetVisibility() == VISIBILITY_GROUP_STEALTH; } - }; - - // Creature checks - - class NearestHostileUnitCheck - { - public: - explicit NearestHostileUnitCheck(Creature const* creature, float dist = 0) : me(creature) - { - m_range = (dist == 0 ? 9999 : dist); - } - bool operator()(Unit* u) - { - if (!me->IsWithinDistInMap(u, m_range)) - return false; - - if (!me->canAttack(u)) - return false; - - m_range = me->GetDistance(u); // use found unit range as new range limit for next check - return true; - } - - private: - Creature const *me; - float m_range; - NearestHostileUnitCheck(NearestHostileUnitCheck const&); - }; - - class NearestHostileUnitInAttackDistanceCheck - { - public: - explicit NearestHostileUnitInAttackDistanceCheck(Creature const* creature, float dist = 0) : me(creature) - { - m_range = (dist == 0 ? 9999 : dist); - m_force = (dist == 0 ? false : true); - } - bool operator()(Unit* u) - { - if (!me->IsWithinDistInMap(u, m_range)) - return false; - - if (m_force) - { - if (!me->canAttack(u)) - return false; - } - else - { - if (!me->canStartAttack(u, false)) - return false; - } - - m_range = me->GetDistance(u); // use found unit range as new range limit for next check - return true; - } - float GetLastRange() const { return m_range; } - private: - Creature const *me; - float m_range; - bool m_force; - NearestHostileUnitInAttackDistanceCheck(NearestHostileUnitInAttackDistanceCheck const&); - }; - - class AnyAssistCreatureInRangeCheck - { - public: - AnyAssistCreatureInRangeCheck(Unit* funit, Unit* enemy, float range) - : i_funit(funit), i_enemy(enemy), i_range(range) - { - } - bool operator()(Creature* u) - { - if (u == i_funit) - return false; - - if (!u->CanAssistTo(i_funit, i_enemy)) - return false; - - // too far - if (!i_funit->IsWithinDistInMap(u, i_range)) - return false; - - // only if see assisted creature - if (!i_funit->IsWithinLOSInMap(u)) - return false; - - return true; - } - private: - Unit* const i_funit; - Unit* const i_enemy; - float i_range; - }; - - class NearestAssistCreatureInCreatureRangeCheck - { - public: - NearestAssistCreatureInCreatureRangeCheck(Creature* obj, Unit* enemy, float range) - : i_obj(obj), i_enemy(enemy), i_range(range) {} - - bool operator()(Creature* u) - { - if (u == i_obj) - return false; - if (!u->CanAssistTo(i_obj,i_enemy)) - return false; - - if (!i_obj->IsWithinDistInMap(u, i_range)) - return false; - - if (!i_obj->IsWithinLOSInMap(u)) - return false; - - i_range = i_obj->GetDistance(u); // use found unit range as new range limit for next check - return true; - } - float GetLastRange() const { return i_range; } - private: - Creature* const i_obj; - Unit* const i_enemy; - float i_range; - - // prevent clone this object - NearestAssistCreatureInCreatureRangeCheck(NearestAssistCreatureInCreatureRangeCheck const&); - }; - - // Success at unit in range, range update for next check (this can be use with CreatureLastSearcher to find nearest creature) - class NearestCreatureEntryWithLiveStateInObjectRangeCheck - { - public: - NearestCreatureEntryWithLiveStateInObjectRangeCheck(WorldObject const& obj, uint32 entry, bool alive, float range) - : i_obj(obj), i_entry(entry), i_alive(alive), i_range(range) {} - - bool operator()(Creature* u) - { - if (u->GetEntry() == i_entry && u->isAlive() == i_alive && i_obj.IsWithinDistInMap(u, i_range)) - { - i_range = i_obj.GetDistance(u); // use found unit range as new range limit for next check - return true; - } - return false; - } - float GetLastRange() const { return i_range; } - private: - WorldObject const& i_obj; - uint32 i_entry; - bool i_alive; - float i_range; - - // prevent clone this object - NearestCreatureEntryWithLiveStateInObjectRangeCheck(NearestCreatureEntryWithLiveStateInObjectRangeCheck const&); - }; - - class AnyPlayerInObjectRangeCheck - { - public: - AnyPlayerInObjectRangeCheck(WorldObject const* obj, float range) : i_obj(obj), i_range(range) {} - bool operator()(Player* u) - { - if (u->isAlive() && i_obj->IsWithinDistInMap(u, i_range)) - return true; - - return false; - } - private: - WorldObject const* i_obj; - float i_range; - }; - - class AllFriendlyCreaturesInGrid - { - public: - AllFriendlyCreaturesInGrid(Unit const* obj) : pUnit(obj) {} - bool operator() (Unit* u) - { - if (u->isAlive() && u->GetVisibility() == VISIBILITY_ON && u->IsFriendlyTo(pUnit)) - return true; - - return false; - } - private: - Unit const* pUnit; - }; - - class AllGameObjectsWithEntryInRange - { - public: - AllGameObjectsWithEntryInRange(const WorldObject* pObject, uint32 uiEntry, float fMaxRange) : m_pObject(pObject), m_uiEntry(uiEntry), m_fRange(fMaxRange) {} - bool operator() (GameObject* pGo) - { - if (pGo->GetEntry() == m_uiEntry && m_pObject->IsWithinDist(pGo,m_fRange,false)) - return true; - - return false; - } - private: - const WorldObject* m_pObject; - uint32 m_uiEntry; - float m_fRange; - }; - - class AllCreaturesOfEntryInRange - { - public: - AllCreaturesOfEntryInRange(const WorldObject* pObject, uint32 uiEntry, float fMaxRange) : m_pObject(pObject), m_uiEntry(uiEntry), m_fRange(fMaxRange) {} - bool operator() (Unit* pUnit) - { - if (pUnit->GetEntry() == m_uiEntry && m_pObject->IsWithinDist(pUnit,m_fRange,false)) - return true; - - return false; - } - - private: - const WorldObject* m_pObject; - uint32 m_uiEntry; - float m_fRange; - }; - - class PlayerAtMinimumRangeAway - { - public: - PlayerAtMinimumRangeAway(Unit const* unit, float fMinRange) : pUnit(unit), fRange(fMinRange) {} - bool operator() (Player* pPlayer) - { - //No threat list check, must be done explicit if expected to be in combat with creature - if (!pPlayer->isGameMaster() && pPlayer->isAlive() && !pUnit->IsWithinDist(pPlayer,fRange,false)) - return true; - - return false; - } - - private: - Unit const* pUnit; - float fRange; - }; - - class GameObjectInRangeCheck - { - public: - GameObjectInRangeCheck(float _x, float _y, float _z, float _range) : x(_x), y(_y), z(_z), range(_range) {} - bool operator() (GameObject* go) - { - return go->IsInRange(x, y, z, range); - } - private: - float x, y, z, range; - }; - - // Player checks and do - - // Prepare using Builder localized packets with caching and send to player - template - class LocalizedPacketDo - { - public: - explicit LocalizedPacketDo(Builder& builder) : i_builder(builder) {} - - ~LocalizedPacketDo() - { - for (size_t i = 0; i < i_data_cache.size(); ++i) - delete i_data_cache[i]; - } - void operator()(Player* p); - - private: - Builder& i_builder; - std::vector i_data_cache; // 0 = default, i => i-1 locale index - }; - - // Prepare using Builder localized packets with caching and send to player - template - class LocalizedPacketListDo - { - public: - typedef std::vector WorldPacketList; - explicit LocalizedPacketListDo(Builder& builder) : i_builder(builder) {} - - ~LocalizedPacketListDo() - { - for (size_t i = 0; i < i_data_cache.size(); ++i) - for (size_t j = 0; j < i_data_cache[i].size(); ++j) - delete i_data_cache[i][j]; - } - void operator()(Player* p); - - private: - Builder& i_builder; - std::vector i_data_cache; - // 0 = default, i => i-1 locale index - }; -} -#endif diff --git a/src/server/game/Grids/GridNotifiersImpl.h b/src/server/game/Grids/GridNotifiersImpl.h deleted file mode 100644 index 26a9c0bd328..00000000000 --- a/src/server/game/Grids/GridNotifiersImpl.h +++ /dev/null @@ -1,453 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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 - * 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 - */ - -#ifndef TRINITY_GRIDNOTIFIERSIMPL_H -#define TRINITY_GRIDNOTIFIERSIMPL_H - -#include "GridNotifiers.h" -#include "WorldPacket.h" -#include "Corpse.h" -#include "Player.h" -#include "UpdateData.h" -#include "CreatureAI.h" -#include "SpellAuras.h" - - -template -inline void -Trinity::VisibleNotifier::Visit(GridRefManager &m) -{ - for (typename GridRefManager::iterator iter = m.begin(); iter != m.end(); ++iter) - { - vis_guids.erase(iter->getSource()->GetGUID()); - i_player.UpdateVisibilityOf(iter->getSource(),i_data,i_visibleNow); - } -} - -inline void -Trinity::ObjectUpdater::Visit(CreatureMapType &m) -{ - for (CreatureMapType::iterator iter=m.begin(); iter != m.end(); ++iter) - if (iter->getSource()->IsInWorld() && !iter->getSource()->isSpiritService()) - iter->getSource()->Update(i_timeDiff); -} - -// SEARCHERS & LIST SEARCHERS & WORKERS - -// WorldObject searchers & workers - -template -void Trinity::WorldObjectSearcher::Visit(GameObjectMapType &m) -{ - // already found - if (i_object) - return; - - for (GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - { - if (!itr->getSource()->InSamePhase(i_phaseMask)) - continue; - - if (i_check(itr->getSource())) - { - i_object = itr->getSource(); - return; - } - } -} - -template -void Trinity::WorldObjectSearcher::Visit(PlayerMapType &m) -{ - // already found - if (i_object) - return; - - for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - { - if (!itr->getSource()->InSamePhase(i_phaseMask)) - continue; - - if (i_check(itr->getSource())) - { - i_object = itr->getSource(); - return; - } - } -} - -template -void Trinity::WorldObjectSearcher::Visit(CreatureMapType &m) -{ - // already found - if (i_object) - return; - - for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - { - if (!itr->getSource()->InSamePhase(i_phaseMask)) - continue; - - if (i_check(itr->getSource())) - { - i_object = itr->getSource(); - return; - } - } -} - -template -void Trinity::WorldObjectSearcher::Visit(CorpseMapType &m) -{ - // already found - if (i_object) - return; - - for (CorpseMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - { - if (!itr->getSource()->InSamePhase(i_phaseMask)) - continue; - - if (i_check(itr->getSource())) - { - i_object = itr->getSource(); - return; - } - } -} - -template -void Trinity::WorldObjectSearcher::Visit(DynamicObjectMapType &m) -{ - // already found - if (i_object) - return; - - for (DynamicObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - { - if (!itr->getSource()->InSamePhase(i_phaseMask)) - continue; - - if (i_check(itr->getSource())) - { - i_object = itr->getSource(); - return; - } - } -} - -template -void Trinity::WorldObjectListSearcher::Visit(PlayerMapType &m) -{ - for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - if (i_check(itr->getSource())) - i_objects.push_back(itr->getSource()); -} - -template -void Trinity::WorldObjectListSearcher::Visit(CreatureMapType &m) -{ - for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - if (i_check(itr->getSource())) - i_objects.push_back(itr->getSource()); -} - -template -void Trinity::WorldObjectListSearcher::Visit(CorpseMapType &m) -{ - for (CorpseMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - if (i_check(itr->getSource())) - i_objects.push_back(itr->getSource()); -} - -template -void Trinity::WorldObjectListSearcher::Visit(GameObjectMapType &m) -{ - for (GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - if (i_check(itr->getSource())) - i_objects.push_back(itr->getSource()); -} - -template -void Trinity::WorldObjectListSearcher::Visit(DynamicObjectMapType &m) -{ - for (DynamicObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - if (i_check(itr->getSource())) - i_objects.push_back(itr->getSource()); -} - -// Gameobject searchers - -template -void Trinity::GameObjectSearcher::Visit(GameObjectMapType &m) -{ - // already found - if (i_object) - return; - - for (GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - { - if (!itr->getSource()->InSamePhase(i_phaseMask)) - continue; - - if (i_check(itr->getSource())) - { - i_object = itr->getSource(); - return; - } - } -} - -template -void Trinity::GameObjectLastSearcher::Visit(GameObjectMapType &m) -{ - for (GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - { - if (!itr->getSource()->InSamePhase(i_phaseMask)) - continue; - - if (i_check(itr->getSource())) - i_object = itr->getSource(); - } -} - -template -void Trinity::GameObjectListSearcher::Visit(GameObjectMapType &m) -{ - for (GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - if (i_check(itr->getSource())) - i_objects.push_back(itr->getSource()); -} - -// Unit searchers - -template -void Trinity::UnitSearcher::Visit(CreatureMapType &m) -{ - // already found - if (i_object) - return; - - for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - { - if (!itr->getSource()->InSamePhase(i_phaseMask)) - continue; - - if (i_check(itr->getSource())) - { - i_object = itr->getSource(); - return; - } - } -} - -template -void Trinity::UnitSearcher::Visit(PlayerMapType &m) -{ - // already found - if (i_object) - return; - - for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - { - if (!itr->getSource()->InSamePhase(i_phaseMask)) - continue; - - if (i_check(itr->getSource())) - { - i_object = itr->getSource(); - return; - } - } -} - -template -void Trinity::UnitLastSearcher::Visit(CreatureMapType &m) -{ - for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - { - if (!itr->getSource()->InSamePhase(i_phaseMask)) - continue; - - if (i_check(itr->getSource())) - i_object = itr->getSource(); - } -} - -template -void Trinity::UnitLastSearcher::Visit(PlayerMapType &m) -{ - for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - { - if (!itr->getSource()->InSamePhase(i_phaseMask)) - continue; - - if (i_check(itr->getSource())) - i_object = itr->getSource(); - } -} - -template -void Trinity::UnitListSearcher::Visit(PlayerMapType &m) -{ - for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - if (i_check(itr->getSource())) - i_objects.push_back(itr->getSource()); -} - -template -void Trinity::UnitListSearcher::Visit(CreatureMapType &m) -{ - for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - if (i_check(itr->getSource())) - i_objects.push_back(itr->getSource()); -} - -// Creature searchers - -template -void Trinity::CreatureSearcher::Visit(CreatureMapType &m) -{ - // already found - if (i_object) - return; - - for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - { - if (!itr->getSource()->InSamePhase(i_phaseMask)) - continue; - - if (i_check(itr->getSource())) - { - i_object = itr->getSource(); - return; - } - } -} - -template -void Trinity::CreatureLastSearcher::Visit(CreatureMapType &m) -{ - for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - { - if (!itr->getSource()->InSamePhase(i_phaseMask)) - continue; - - if (i_check(itr->getSource())) - i_object = itr->getSource(); - } -} - -template -void Trinity::CreatureListSearcher::Visit(CreatureMapType &m) -{ - for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - if (i_check(itr->getSource())) - i_objects.push_back(itr->getSource()); -} - -template -void Trinity::PlayerListSearcher::Visit(PlayerMapType &m) -{ - for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - if (i_check(itr->getSource())) - i_objects.push_back(itr->getSource()); -} - -template -void Trinity::PlayerSearcher::Visit(PlayerMapType &m) -{ - // already found - if (i_object) - return; - - for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - { - if (!itr->getSource()->InSamePhase(i_phaseMask)) - continue; - - if (i_check(itr->getSource())) - { - i_object = itr->getSource(); - return; - } - } -} - -template -void Trinity::LocalizedPacketDo::operator()(Player* p) -{ - int32 loc_idx = p->GetSession()->GetSessionDbLocaleIndex(); - uint32 cache_idx = loc_idx+1; - WorldPacket* data; - - // create if not cached yet - if (i_data_cache.size() < cache_idx+1 || !i_data_cache[cache_idx]) - { - if (i_data_cache.size() < cache_idx+1) - i_data_cache.resize(cache_idx+1); - - data = new WorldPacket(SMSG_MESSAGECHAT, 200); - - i_builder(*data,loc_idx); - - i_data_cache[cache_idx] = data; - } - else - data = i_data_cache[cache_idx]; - - p->SendDirectMessage(data); -} - -template -void Trinity::LocalizedPacketListDo::operator()(Player* p) -{ - int32 loc_idx = p->GetSession()->GetSessionDbLocaleIndex(); - uint32 cache_idx = loc_idx+1; - WorldPacketList* data_list; - - // create if not cached yet - if (i_data_cache.size() < cache_idx+1 || i_data_cache[cache_idx].empty()) - { - if (i_data_cache.size() < cache_idx+1) - i_data_cache.resize(cache_idx+1); - - data_list = &i_data_cache[cache_idx]; - - i_builder(*data_list,loc_idx); - } - else - data_list = &i_data_cache[cache_idx]; - - for (size_t i = 0; i < data_list->size(); ++i) - p->SendDirectMessage((*data_list)[i]); -} - -#endif // TRINITY_GRIDNOTIFIERSIMPL_H diff --git a/src/server/game/Grids/GridRefManager.h b/src/server/game/Grids/GridRefManager.h new file mode 100644 index 00000000000..79799105fb7 --- /dev/null +++ b/src/server/game/Grids/GridRefManager.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef _GRIDREFMANAGER +#define _GRIDREFMANAGER + +#include "Utilities/LinkedReference/RefManager.h" + +template +class GridReference; + +template +class GridRefManager : public RefManager, OBJECT> +{ + public: + typedef LinkedListHead::Iterator< GridReference > iterator; + + GridReference* getFirst() { return (GridReference*)RefManager, OBJECT>::getFirst(); } + GridReference* getLast() { return (GridReference*)RefManager, OBJECT>::getLast(); } + + iterator begin() { return iterator(getFirst()); } + iterator end() { return iterator(NULL); } + iterator rbegin() { return iterator(getLast()); } + iterator rend() { return iterator(NULL); } +}; +#endif + diff --git a/src/server/game/Grids/GridReference.h b/src/server/game/Grids/GridReference.h new file mode 100644 index 00000000000..d2e3a455895 --- /dev/null +++ b/src/server/game/Grids/GridReference.h @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef _GRIDREFERENCE_H +#define _GRIDREFERENCE_H + +#include "Utilities/LinkedReference/Reference.h" + +template +class GridRefManager; + +template +class GridReference : public Reference, OBJECT> +{ + protected: + void targetObjectBuildLink() + { + // called from link() + this->getTarget()->insertFirst(this); + this->getTarget()->incSize(); + } + void targetObjectDestroyLink() + { + // called from unlink() + if(this->isValid()) this->getTarget()->decSize(); + } + void sourceObjectDestroyLink() + { + // called from invalidate() + this->getTarget()->decSize(); + } + public: + GridReference() : Reference, OBJECT>() {} + ~GridReference() { this->unlink(); } + GridReference *next() { return (GridReference*)Reference, OBJECT>::next(); } +}; +#endif + diff --git a/src/server/game/Grids/NGrid.h b/src/server/game/Grids/NGrid.h new file mode 100644 index 00000000000..3810286e123 --- /dev/null +++ b/src/server/game/Grids/NGrid.h @@ -0,0 +1,189 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_NGRID_H +#define TRINITY_NGRID_H + +/** NGrid is nothing more than a wrapper of the Grid with an NxN cells + */ + +#include "GameSystem/Grid.h" +#include "GameSystem/GridReference.h" +#include "Timer.h" +#include "Util.h" + +#define DEFAULT_VISIBILITY_NOTIFY_PERIOD 1000 + +class GridInfo +{ +public: + GridInfo() + : i_timer(0), i_unloadActiveLockCount(0), i_unloadExplicitLock(false), i_unloadReferenceLock(false), + vis_Update(0, irand(0,DEFAULT_VISIBILITY_NOTIFY_PERIOD)) {} + GridInfo(time_t expiry, bool unload = true ) + : i_timer(expiry), i_unloadActiveLockCount(0), i_unloadExplicitLock(!unload), i_unloadReferenceLock(false), + vis_Update(0, irand(0,DEFAULT_VISIBILITY_NOTIFY_PERIOD)) {} + const TimeTracker& getTimeTracker() const { return i_timer; } + bool getUnloadLock() const { return i_unloadActiveLockCount || i_unloadExplicitLock || i_unloadReferenceLock; } + void setUnloadExplicitLock( bool on ) { i_unloadExplicitLock = on; } + void setUnloadReferenceLock( bool on ) { i_unloadReferenceLock = on; } + void incUnloadActiveLock() { ++i_unloadActiveLockCount; } + void decUnloadActiveLock() { if(i_unloadActiveLockCount) --i_unloadActiveLockCount; } + + void setTimer(const TimeTracker& pTimer) { i_timer = pTimer; } + void ResetTimeTracker(time_t interval) { i_timer.Reset(interval); } + void UpdateTimeTracker(time_t diff) { i_timer.Update(diff); } + PeriodicTimer& getRelocationTimer() { return vis_Update; } +private: + TimeTracker i_timer; + PeriodicTimer vis_Update; + + uint16 i_unloadActiveLockCount : 16; // lock from active object spawn points (prevent clone loading) + bool i_unloadExplicitLock : 1; // explicit manual lock or config setting + bool i_unloadReferenceLock : 1; // lock from instance map copy +}; + +typedef enum +{ + GRID_STATE_INVALID = 0, + GRID_STATE_ACTIVE = 1, + GRID_STATE_IDLE = 2, + GRID_STATE_REMOVAL= 3, + MAX_GRID_STATE = 4 +} grid_state_t; + +template +< +unsigned int N, +class ACTIVE_OBJECT, +class WORLD_OBJECT_TYPES, +class GRID_OBJECT_TYPES, +class ThreadModel = Trinity::SingleThreaded +> +class NGrid +{ + public: + + typedef Grid GridType; + NGrid(uint32 id, int32 x, int32 y, time_t expiry, bool unload = true) + : i_gridId(id), i_x(x), i_y(y), i_cellstate(GRID_STATE_INVALID), i_GridObjectDataLoaded(false) + { + i_GridInfo = GridInfo(expiry, unload); + } + + const GridType& operator()(unsigned short x, unsigned short y) const + { + ASSERT(x < N); + ASSERT(y < N); + return i_cells[x][y]; + } + + GridType& operator()(unsigned short x, unsigned short y) + { + ASSERT(x < N); + ASSERT(y < N); + return i_cells[x][y]; + } + + const uint32& GetGridId(void) const { return i_gridId; } + void SetGridId(const uint32 id) const { i_gridId = id; } + grid_state_t GetGridState(void) const { return i_cellstate; } + void SetGridState(grid_state_t s) { i_cellstate = s; } + int32 getX() const { return i_x; } + int32 getY() const { return i_y; } + + void link(GridRefManager >* pTo) + { + i_Reference.link(pTo, this); + } + bool isGridObjectDataLoaded() const { return i_GridObjectDataLoaded; } + void setGridObjectDataLoaded(bool pLoaded) { i_GridObjectDataLoaded = pLoaded; } + + GridInfo* getGridInfoRef() { return &i_GridInfo; } + const TimeTracker& getTimeTracker() const { return i_GridInfo.getTimeTracker(); } + bool getUnloadLock() const { return i_GridInfo.getUnloadLock(); } + void setUnloadExplicitLock( bool on ) { i_GridInfo.setUnloadExplicitLock(on); } + void setUnloadReferenceLock( bool on ) { i_GridInfo.setUnloadReferenceLock(on); } + void incUnloadActiveLock() { i_GridInfo.incUnloadActiveLock(); } + void decUnloadActiveLock() { i_GridInfo.decUnloadActiveLock(); } + void ResetTimeTracker(time_t interval) { i_GridInfo.ResetTimeTracker(interval); } + void UpdateTimeTracker(time_t diff) { i_GridInfo.UpdateTimeTracker(diff); } + + template void AddWorldObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj) + { + getGridType(x, y).AddWorldObject(obj); + } + + template void RemoveWorldObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj) + { + getGridType(x, y).RemoveWorldObject(obj); + } + + template void Visit(TypeContainerVisitor > &visitor) + { + for (unsigned int x=0; x < N; ++x) + for (unsigned int y=0; y < N; ++y) + getGridType(x, y).Visit(visitor); + } + + template void Visit(const uint32 &x, const uint32 &y, TypeContainerVisitor > &visitor) + { + getGridType(x, y).Visit(visitor); + } + + unsigned int ActiveObjectsInGrid(void) const + { + unsigned int count=0; + for (unsigned int x=0; x < N; ++x) + for (unsigned int y=0; y < N; ++y) + count += i_cells[x][y].ActiveObjectsInGrid(); + return count; + } + + template bool AddGridObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj) + { + return getGridType(x, y).AddGridObject(obj); + } + + template bool RemoveGridObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj) + { + return getGridType(x, y).RemoveGridObject(obj); + } + + private: + + GridType& getGridType(const uint32& x, const uint32& y) + { + ASSERT(x < N); + ASSERT(y < N); + return i_cells[x][y]; + } + + uint32 i_gridId; + GridInfo i_GridInfo; + GridReference > i_Reference; + int32 i_x; + int32 i_y; + grid_state_t i_cellstate; + GridType i_cells[N][N]; + bool i_GridObjectDataLoaded; +}; +#endif + diff --git a/src/server/game/Grids/Notifiers/GridNotifiers.cpp b/src/server/game/Grids/Notifiers/GridNotifiers.cpp new file mode 100644 index 00000000000..b10dfa8791e --- /dev/null +++ b/src/server/game/Grids/Notifiers/GridNotifiers.cpp @@ -0,0 +1,353 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#include "GridNotifiers.h" +#include "GridNotifiersImpl.h" +#include "WorldPacket.h" +#include "WorldSession.h" +#include "UpdateData.h" +#include "Item.h" +#include "Map.h" +#include "Transports.h" +#include "ObjectAccessor.h" +#include "CellImpl.h" + +using namespace Trinity; + +void +VisibleNotifier::SendToSelf() +{ + // at this moment i_clientGUIDs have guids that not iterate at grid level checks + // but exist one case when this possible and object not out of range: transports + if (Transport* transport = i_player.GetTransport()) + for (Transport::PlayerSet::const_iterator itr = transport->GetPassengers().begin();itr != transport->GetPassengers().end();++itr) + { + if (vis_guids.find((*itr)->GetGUID()) != vis_guids.end()) + { + vis_guids.erase((*itr)->GetGUID()); + + i_player.UpdateVisibilityOf((*itr), i_data, i_visibleNow); + + if (!(*itr)->isNeedNotify(NOTIFY_VISIBILITY_CHANGED)) + (*itr)->UpdateVisibilityOf(&i_player); + } + } + + for (Player::ClientGUIDs::const_iterator it = vis_guids.begin();it != vis_guids.end(); ++it) + { + i_player.m_clientGUIDs.erase(*it); + i_data.AddOutOfRangeGUID(*it); + + if (IS_PLAYER_GUID(*it)) + { + Player* plr = ObjectAccessor::FindPlayer(*it); + if (plr && plr->IsInWorld() && !plr->isNeedNotify(NOTIFY_VISIBILITY_CHANGED)) + plr->UpdateVisibilityOf(&i_player); + } + } + + if (!i_data.HasData()) + return; + + WorldPacket packet; + i_data.BuildPacket(&packet); + i_player.GetSession()->SendPacket(&packet); + + for (std::set::const_iterator it = i_visibleNow.begin(); it != i_visibleNow.end(); ++it) + i_player.SendInitialVisiblePackets(*it); +} + +void +VisibleChangesNotifier::Visit(PlayerMapType &m) +{ + for (PlayerMapType::iterator iter=m.begin(); iter != m.end(); ++iter) + { + if (iter->getSource() == &i_object) + continue; + + iter->getSource()->UpdateVisibilityOf(&i_object); + + if (!iter->getSource()->GetSharedVisionList().empty()) + for (SharedVisionList::const_iterator i = iter->getSource()->GetSharedVisionList().begin(); + i != iter->getSource()->GetSharedVisionList().end(); ++i) + if ((*i)->m_seer == iter->getSource()) + (*i)->UpdateVisibilityOf(&i_object); + } +} + +void +VisibleChangesNotifier::Visit(CreatureMapType &m) +{ + for (CreatureMapType::iterator iter = m.begin(); iter != m.end(); ++iter) + if (!iter->getSource()->GetSharedVisionList().empty()) + for (SharedVisionList::const_iterator i = iter->getSource()->GetSharedVisionList().begin(); + i != iter->getSource()->GetSharedVisionList().end(); ++i) + if ((*i)->m_seer == iter->getSource()) + (*i)->UpdateVisibilityOf(&i_object); +} + +void +VisibleChangesNotifier::Visit(DynamicObjectMapType &m) +{ + for (DynamicObjectMapType::iterator iter = m.begin(); iter != m.end(); ++iter) + if (IS_PLAYER_GUID(iter->getSource()->GetCasterGUID())) + if (Player* caster = (Player*)iter->getSource()->GetCaster()) + if (caster->m_seer == iter->getSource()) + caster->UpdateVisibilityOf(&i_object); +} + +inline void CreatureUnitRelocationWorker(Creature* c, Unit* u) +{ + if (!u->isAlive() || !c->isAlive() || c == u || u->isInFlight()) + return; + + if (c->HasReactState(REACT_AGGRESSIVE) && !c->hasUnitState(UNIT_STAT_SIGHTLESS)) + if (c->_IsWithinDist(u, c->m_SightDistance, true) && c->IsAIEnabled) + c->AI()->MoveInLineOfSight_Safe(u); +} + +void PlayerRelocationNotifier::Visit(PlayerMapType &m) +{ + for (PlayerMapType::iterator iter=m.begin(); iter != m.end(); ++iter) + { + Player* plr = iter->getSource(); + + vis_guids.erase(plr->GetGUID()); + + i_player.UpdateVisibilityOf(plr,i_data,i_visibleNow); + + if (plr->m_seer->isNeedNotify(NOTIFY_VISIBILITY_CHANGED)) + continue; + + plr->UpdateVisibilityOf(&i_player); + } +} + +void PlayerRelocationNotifier::Visit(CreatureMapType &m) +{ + bool relocated_for_ai = (&i_player == i_player.m_seer); + + for (CreatureMapType::iterator iter=m.begin(); iter != m.end(); ++iter) + { + Creature * c = iter->getSource(); + + vis_guids.erase(c->GetGUID()); + + i_player.UpdateVisibilityOf(c,i_data,i_visibleNow); + + if (relocated_for_ai && !c->isNeedNotify(NOTIFY_VISIBILITY_CHANGED)) + CreatureUnitRelocationWorker(c, &i_player); + } +} + +void CreatureRelocationNotifier::Visit(PlayerMapType &m) +{ + for (PlayerMapType::iterator iter=m.begin(); iter != m.end(); ++iter) + { + Player * pl = iter->getSource(); + + if (!pl->m_seer->isNeedNotify(NOTIFY_VISIBILITY_CHANGED)) + pl->UpdateVisibilityOf(&i_creature); + + CreatureUnitRelocationWorker(&i_creature, pl); + } +} + +void CreatureRelocationNotifier::Visit(CreatureMapType &m) +{ + if (!i_creature.isAlive()) + return; + + for (CreatureMapType::iterator iter=m.begin(); iter != m.end(); ++iter) + { + Creature* c = iter->getSource(); + CreatureUnitRelocationWorker(&i_creature, c); + + if (!c->isNeedNotify(NOTIFY_VISIBILITY_CHANGED)) + CreatureUnitRelocationWorker(c, &i_creature); + } +} + +void DelayedUnitRelocation::Visit(CreatureMapType &m) +{ + for (CreatureMapType::iterator iter = m.begin(); iter != m.end(); ++iter) + { + Creature * unit = iter->getSource(); + if (!unit->isNeedNotify(NOTIFY_VISIBILITY_CHANGED)) + continue; + + CreatureRelocationNotifier relocate(*unit); + + TypeContainerVisitor c2world_relocation(relocate); + TypeContainerVisitor c2grid_relocation(relocate); + + cell.Visit(p, c2world_relocation, i_map, *unit, i_radius); + cell.Visit(p, c2grid_relocation, i_map, *unit, i_radius); + } +} + +void DelayedUnitRelocation::Visit(PlayerMapType &m) +{ + for (PlayerMapType::iterator iter = m.begin(); iter != m.end(); ++iter) + { + Player * player = iter->getSource(); + WorldObject const *viewPoint = player->m_seer; + + if (!viewPoint->isNeedNotify(NOTIFY_VISIBILITY_CHANGED)) + continue; + + if (player != viewPoint && !viewPoint->IsPositionValid()) + continue; + + CellPair pair2(Trinity::ComputeCellPair(viewPoint->GetPositionX(), viewPoint->GetPositionY())); + Cell cell2(pair2); + //cell.SetNoCreate(); need load cells around viewPoint or player, that's why its commented + + PlayerRelocationNotifier relocate(*player); + TypeContainerVisitor c2world_relocation(relocate); + TypeContainerVisitor c2grid_relocation(relocate); + + cell2.Visit(pair2, c2world_relocation, i_map, *viewPoint, i_radius); + cell2.Visit(pair2, c2grid_relocation, i_map, *viewPoint, i_radius); + + relocate.SendToSelf(); + } +} + +void AIRelocationNotifier::Visit(CreatureMapType &m) +{ + for (CreatureMapType::iterator iter = m.begin(); iter != m.end(); ++iter) + { + Creature *c = iter->getSource(); + CreatureUnitRelocationWorker(c, &i_unit); + if (isCreature) + CreatureUnitRelocationWorker((Creature*)&i_unit, c); + } +} + +void +MessageDistDeliverer::Visit(PlayerMapType &m) +{ + for (PlayerMapType::iterator iter = m.begin(); iter != m.end(); ++iter) + { + Player *target = iter->getSource(); + if (!target->InSamePhase(i_phaseMask)) + continue; + + if (target->GetExactDistSq(i_source) > i_distSq) + continue; + + // Send packet to all who are sharing the player's vision + if (!target->GetSharedVisionList().empty()) + { + SharedVisionList::const_iterator i = target->GetSharedVisionList().begin(); + for (; i != target->GetSharedVisionList().end(); ++i) + if ((*i)->m_seer == target) + SendPacket(*i); + } + + if (target->m_seer == target || target->GetVehicle()) + SendPacket(target); + } +} + +void +MessageDistDeliverer::Visit(CreatureMapType &m) +{ + for (CreatureMapType::iterator iter = m.begin(); iter != m.end(); ++iter) + { + if (!iter->getSource()->InSamePhase(i_phaseMask)) + continue; + + if (iter->getSource()->GetExactDistSq(i_source) > i_distSq) + continue; + + // Send packet to all who are sharing the creature's vision + if (!iter->getSource()->GetSharedVisionList().empty()) + { + SharedVisionList::const_iterator i = iter->getSource()->GetSharedVisionList().begin(); + for (; i != iter->getSource()->GetSharedVisionList().end(); ++i) + if ((*i)->m_seer == iter->getSource()) + SendPacket(*i); + } + } +} + +void +MessageDistDeliverer::Visit(DynamicObjectMapType &m) +{ + for (DynamicObjectMapType::iterator iter = m.begin(); iter != m.end(); ++iter) + { + if (!iter->getSource()->InSamePhase(i_phaseMask)) + continue; + + if (iter->getSource()->GetExactDistSq(i_source) > i_distSq) + continue; + + if (IS_PLAYER_GUID(iter->getSource()->GetCasterGUID())) + { + // Send packet back to the caster if the caster has vision of dynamic object + Player* caster = (Player*)iter->getSource()->GetCaster(); + if (caster && caster->m_seer == iter->getSource()) + SendPacket(caster); + } + } +} + +/* +void +MessageDistDeliverer::VisitObject(Player* plr) +{ + if (!i_ownTeamOnly || (i_source.GetTypeId() == TYPEID_PLAYER && plr->GetTeam() == ((Player&)i_source).GetTeam())) + { + SendPacket(plr); + } +} +*/ + +template void +ObjectUpdater::Visit(GridRefManager &m) +{ + for (typename GridRefManager::iterator iter = m.begin(); iter != m.end(); ++iter) + { + if (iter->getSource()->IsInWorld()) + iter->getSource()->Update(i_timeDiff); + } +} + +bool CannibalizeObjectCheck::operator()(Corpse* u) +{ + // ignore bones + if (u->GetType() == CORPSE_BONES) + return false; + + Player* owner = ObjectAccessor::FindPlayer(u->GetOwnerGUID()); + + if (!owner || i_funit->IsFriendlyTo(owner)) + return false; + + if (i_funit->IsWithinDistInMap(u, i_range)) + return true; + + return false; +} + +template void ObjectUpdater::Visit(GameObjectMapType &); +template void ObjectUpdater::Visit(DynamicObjectMapType &); diff --git a/src/server/game/Grids/Notifiers/GridNotifiers.h b/src/server/game/Grids/Notifiers/GridNotifiers.h new file mode 100644 index 00000000000..b0abf0aae79 --- /dev/null +++ b/src/server/game/Grids/Notifiers/GridNotifiers.h @@ -0,0 +1,1232 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_GRIDNOTIFIERS_H +#define TRINITY_GRIDNOTIFIERS_H + +#include "ObjectGridLoader.h" +#include "UpdateData.h" +#include + +#include "Corpse.h" +#include "Object.h" +#include "DynamicObject.h" +#include "GameObject.h" +#include "Player.h" +#include "Unit.h" +#include "CreatureAI.h" + +class Player; +//class Map; + +namespace Trinity +{ + struct VisibleNotifier + { + Player &i_player; + UpdateData i_data; + std::set i_visibleNow; + Player::ClientGUIDs vis_guids; + + VisibleNotifier(Player &player) : i_player(player), vis_guids(player.m_clientGUIDs) {} + template void Visit(GridRefManager &m); + void SendToSelf(void); + }; + + struct VisibleChangesNotifier + { + WorldObject &i_object; + + explicit VisibleChangesNotifier(WorldObject &object) : i_object(object) {} + template void Visit(GridRefManager &) {} + void Visit(PlayerMapType &); + void Visit(CreatureMapType &); + void Visit(DynamicObjectMapType &); + }; + + struct PlayerRelocationNotifier : public VisibleNotifier + { + PlayerRelocationNotifier(Player &pl) : VisibleNotifier(pl) {} + + template void Visit(GridRefManager &m) { VisibleNotifier::Visit(m); } + void Visit(CreatureMapType &); + void Visit(PlayerMapType &); + }; + + struct CreatureRelocationNotifier + { + Creature &i_creature; + CreatureRelocationNotifier(Creature &c) : i_creature(c) {} + template void Visit(GridRefManager &) {} + void Visit(CreatureMapType &); + void Visit(PlayerMapType &); + }; + + struct DelayedUnitRelocation + { + Map &i_map; + Cell &cell; + CellPair &p; + const float i_radius; + DelayedUnitRelocation(Cell &c, CellPair &pair, Map &map, float radius) : + cell(c), p(pair), i_map(map), i_radius(radius) {} + template void Visit(GridRefManager &) {} + void Visit(CreatureMapType &); + void Visit(PlayerMapType &); + }; + + struct AIRelocationNotifier + { + Unit &i_unit; + bool isCreature; + explicit AIRelocationNotifier(Unit &unit) : i_unit(unit), isCreature(unit.GetTypeId() == TYPEID_UNIT) {} + template void Visit(GridRefManager &) {} + void Visit(CreatureMapType &); + }; + + struct GridUpdater + { + GridType &i_grid; + uint32 i_timeDiff; + GridUpdater(GridType &grid, uint32 diff) : i_grid(grid), i_timeDiff(diff) {} + + template void updateObjects(GridRefManager &m) + { + for (typename GridRefManager::iterator iter = m.begin(); iter != m.end(); ++iter) + iter->getSource()->Update(i_timeDiff); + } + + void Visit(PlayerMapType &m) { updateObjects(m); } + void Visit(CreatureMapType &m){ updateObjects(m); } + void Visit(GameObjectMapType &m) { updateObjects(m); } + void Visit(DynamicObjectMapType &m) { updateObjects(m); } + void Visit(CorpseMapType &m) { updateObjects(m); } + }; + + struct MessageDistDeliverer + { + WorldObject *i_source; + WorldPacket *i_message; + uint32 i_phaseMask; + float i_distSq; + uint32 team; + Player const* skipped_receiver; + MessageDistDeliverer(WorldObject *src, WorldPacket *msg, float dist, bool own_team_only = false, Player const* skipped = NULL) + : i_source(src), i_message(msg), i_distSq(dist * dist), i_phaseMask(src->GetPhaseMask()) + , team((own_team_only && src->GetTypeId() == TYPEID_PLAYER) ? ((Player*)src)->GetTeam() : 0) + , skipped_receiver(skipped) + { + } + void Visit(PlayerMapType &m); + void Visit(CreatureMapType &m); + void Visit(DynamicObjectMapType &m); + template void Visit(GridRefManager &) {} + + void SendPacket(Player* plr) + { + // never send packet to self + if (plr == i_source || (team && plr->GetTeam() != team) || skipped_receiver == plr) + return; + + plr->GetSession()->SendPacket(i_message); + } + }; + + struct ObjectUpdater + { + uint32 i_timeDiff; + explicit ObjectUpdater(const uint32 &diff) : i_timeDiff(diff) {} + template void Visit(GridRefManager &m); + void Visit(PlayerMapType &) {} + void Visit(CorpseMapType &) {} + void Visit(CreatureMapType &); + }; + + // SEARCHERS & LIST SEARCHERS & WORKERS + + // WorldObject searchers & workers + + template + struct WorldObjectSearcher + { + uint32 i_phaseMask; + WorldObject* &i_object; + Check &i_check; + + WorldObjectSearcher(WorldObject const* searcher, WorldObject* & result, Check& check) + : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} + + void Visit(GameObjectMapType &m); + void Visit(PlayerMapType &m); + void Visit(CreatureMapType &m); + void Visit(CorpseMapType &m); + void Visit(DynamicObjectMapType &m); + + template void Visit(GridRefManager &) {} + }; + + template + struct WorldObjectListSearcher + { + uint32 i_phaseMask; + std::list &i_objects; + Check& i_check; + + WorldObjectListSearcher(WorldObject const* searcher, std::list &objects, Check & check) + : i_phaseMask(searcher->GetPhaseMask()), i_objects(objects),i_check(check) {} + + void Visit(PlayerMapType &m); + void Visit(CreatureMapType &m); + void Visit(CorpseMapType &m); + void Visit(GameObjectMapType &m); + void Visit(DynamicObjectMapType &m); + + template void Visit(GridRefManager &) {} + }; + + template + struct WorldObjectWorker + { + uint32 i_phaseMask; + Do const& i_do; + + WorldObjectWorker(WorldObject const* searcher, Do const& _do) + : i_phaseMask(searcher->GetPhaseMask()), i_do(_do) {} + + void Visit(GameObjectMapType &m) + { + for (GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + i_do(itr->getSource()); + } + + void Visit(PlayerMapType &m) + { + for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + i_do(itr->getSource()); + } + void Visit(CreatureMapType &m) + { + for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + i_do(itr->getSource()); + } + + void Visit(CorpseMapType &m) + { + for (CorpseMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + i_do(itr->getSource()); + } + + void Visit(DynamicObjectMapType &m) + { + for (DynamicObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + i_do(itr->getSource()); + } + + template void Visit(GridRefManager &) {} + }; + + // Gameobject searchers + + template + struct GameObjectSearcher + { + uint32 i_phaseMask; + GameObject* &i_object; + Check &i_check; + + GameObjectSearcher(WorldObject const* searcher, GameObject* & result, Check& check) + : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} + + void Visit(GameObjectMapType &m); + + template void Visit(GridRefManager &) {} + }; + + // Last accepted by Check GO if any (Check can change requirements at each call) + template + struct GameObjectLastSearcher + { + uint32 i_phaseMask; + GameObject* &i_object; + Check& i_check; + + GameObjectLastSearcher(WorldObject const* searcher, GameObject* & result, Check& check) + : i_phaseMask(searcher->GetPhaseMask()), i_object(result), i_check(check) {} + + void Visit(GameObjectMapType &m); + + template void Visit(GridRefManager &) {} + }; + + template + struct GameObjectListSearcher + { + uint32 i_phaseMask; + std::list &i_objects; + Check& i_check; + + GameObjectListSearcher(WorldObject const* searcher, std::list &objects, Check & check) + : i_phaseMask(searcher->GetPhaseMask()), i_objects(objects), i_check(check) {} + + void Visit(GameObjectMapType &m); + + template void Visit(GridRefManager &) {} + }; + + // Unit searchers + + // First accepted by Check Unit if any + template + struct UnitSearcher + { + uint32 i_phaseMask; + Unit* &i_object; + Check & i_check; + + UnitSearcher(WorldObject const* searcher, Unit* & result, Check & check) + : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} + + void Visit(CreatureMapType &m); + void Visit(PlayerMapType &m); + + template void Visit(GridRefManager &) {} + }; + + // Last accepted by Check Unit if any (Check can change requirements at each call) + template + struct UnitLastSearcher + { + uint32 i_phaseMask; + Unit* &i_object; + Check & i_check; + + UnitLastSearcher(WorldObject const* searcher, Unit* & result, Check & check) + : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} + + void Visit(CreatureMapType &m); + void Visit(PlayerMapType &m); + + template void Visit(GridRefManager &) {} + }; + + // All accepted by Check units if any + template + struct UnitListSearcher + { + uint32 i_phaseMask; + std::list &i_objects; + Check& i_check; + + UnitListSearcher(WorldObject const* searcher, std::list &objects, Check & check) + : i_phaseMask(searcher->GetPhaseMask()), i_objects(objects),i_check(check) {} + + void Visit(PlayerMapType &m); + void Visit(CreatureMapType &m); + + template void Visit(GridRefManager &) {} + }; + + // Creature searchers + + template + struct CreatureSearcher + { + uint32 i_phaseMask; + Creature* &i_object; + Check & i_check; + + CreatureSearcher(WorldObject const* searcher, Creature* & result, Check & check) + : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} + + void Visit(CreatureMapType &m); + + template void Visit(GridRefManager &) {} + }; + + // Last accepted by Check Creature if any (Check can change requirements at each call) + template + struct CreatureLastSearcher + { + uint32 i_phaseMask; + Creature* &i_object; + Check & i_check; + + CreatureLastSearcher(WorldObject const* searcher, Creature* & result, Check & check) + : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} + + void Visit(CreatureMapType &m); + + template void Visit(GridRefManager &) {} + }; + + template + struct CreatureListSearcher + { + uint32 i_phaseMask; + std::list &i_objects; + Check& i_check; + + CreatureListSearcher(WorldObject const* searcher, std::list &objects, Check & check) + : i_phaseMask(searcher->GetPhaseMask()), i_objects(objects),i_check(check) {} + + void Visit(CreatureMapType &m); + + template void Visit(GridRefManager &) {} + }; + + template + struct CreatureWorker + { + uint32 i_phaseMask; + Do& i_do; + + CreatureWorker(WorldObject const* searcher, Do& _do) + : i_phaseMask(searcher->GetPhaseMask()), i_do(_do) {} + + void Visit(CreatureMapType &m) + { + for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + i_do(itr->getSource()); + } + + template void Visit(GridRefManager &) {} + }; + + // Player searchers + + template + struct PlayerSearcher + { + uint32 i_phaseMask; + Player* &i_object; + Check & i_check; + + PlayerSearcher(WorldObject const* searcher, Player* & result, Check & check) + : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} + + void Visit(PlayerMapType &m); + + template void Visit(GridRefManager &) {} + }; + + template + struct PlayerListSearcher + { + uint32 i_phaseMask; + std::list &i_objects; + Check& i_check; + + PlayerListSearcher(WorldObject const* searcher, std::list &objects, Check & check) + : i_phaseMask(searcher->GetPhaseMask()), i_objects(objects),i_check(check) {} + + void Visit(PlayerMapType &m); + + template void Visit(GridRefManager &) {} + }; + + template + struct PlayerWorker + { + uint32 i_phaseMask; + Do& i_do; + + PlayerWorker(WorldObject const* searcher, Do& _do) + : i_phaseMask(searcher->GetPhaseMask()), i_do(_do) {} + + void Visit(PlayerMapType &m) + { + for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + i_do(itr->getSource()); + } + + template void Visit(GridRefManager &) {} + }; + + template + struct PlayerDistWorker + { + WorldObject const* i_searcher; + float i_dist; + Do& i_do; + + PlayerDistWorker(WorldObject const* searcher, float _dist, Do& _do) + : i_searcher(searcher), i_dist(_dist), i_do(_do) {} + + void Visit(PlayerMapType &m) + { + for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_searcher) && itr->getSource()->IsWithinDist(i_searcher,i_dist)) + i_do(itr->getSource()); + } + + template void Visit(GridRefManager &) {} + }; + + // CHECKS && DO classes + + // WorldObject check classes + class RaiseDeadObjectCheck + { + public: + RaiseDeadObjectCheck(Unit* funit, float range) : i_funit(funit), i_range(range) {} + bool operator()(Creature* u) + { + if (i_funit->GetTypeId() != TYPEID_PLAYER || !((Player*)i_funit)->isHonorOrXPTarget(u) || + u->getDeathState() != CORPSE || u->isDeadByDefault() || u->isInFlight() || + (u->GetCreatureTypeMask() & (1 << (CREATURE_TYPE_HUMANOID-1))) == 0 || + (u->GetDisplayId() != u->GetNativeDisplayId())) + return false; + + return i_funit->IsWithinDistInMap(u, i_range); + } + template bool operator()(NOT_INTERESTED*) { return false; } + private: + Unit* const i_funit; + float i_range; + }; + + class ExplodeCorpseObjectCheck + { + public: + ExplodeCorpseObjectCheck(Unit* funit, float range) : i_funit(funit), i_range(range) {} + bool operator()(Player* u) + { + if (u->getDeathState() != CORPSE || u->isInFlight() || + u->HasAuraType(SPELL_AURA_GHOST) || (u->GetDisplayId() != u->GetNativeDisplayId())) + return false; + + return i_funit->IsWithinDistInMap(u, i_range); + } + bool operator()(Creature* u) + { + if (u->getDeathState() != CORPSE || u->isInFlight() || u->isDeadByDefault() || + (u->GetDisplayId() != u->GetNativeDisplayId()) || + (u->GetCreatureTypeMask() & CREATURE_TYPEMASK_MECHANICAL_OR_ELEMENTAL) != 0) + return false; + + return i_funit->IsWithinDistInMap(u, i_range); + } + template bool operator()(NOT_INTERESTED*) { return false; } + private: + Unit* const i_funit; + float i_range; + }; + + class CannibalizeObjectCheck + { + public: + CannibalizeObjectCheck(Unit* funit, float range) : i_funit(funit), i_range(range) {} + bool operator()(Player* u) + { + if (i_funit->IsFriendlyTo(u) || u->isAlive() || u->isInFlight()) + return false; + + return i_funit->IsWithinDistInMap(u, i_range); + } + bool operator()(Corpse* u); + bool operator()(Creature* u) + { + if (i_funit->IsFriendlyTo(u) || u->isAlive() || u->isInFlight() || + (u->GetCreatureTypeMask() & CREATURE_TYPEMASK_HUMANOID_OR_UNDEAD) == 0) + return false; + + return i_funit->IsWithinDistInMap(u, i_range); + } + template bool operator()(NOT_INTERESTED*) { return false; } + private: + Unit* const i_funit; + float i_range; + }; + + // WorldObject do classes + + class RespawnDo + { + public: + RespawnDo() {} + void operator()(Creature* u) const { u->Respawn(); } + void operator()(GameObject* u) const { u->Respawn(); } + void operator()(WorldObject*) const {} + void operator()(Corpse*) const {} + }; + + // GameObject checks + + class GameObjectFocusCheck + { + public: + GameObjectFocusCheck(Unit const* unit,uint32 focusId) : i_unit(unit), i_focusId(focusId) {} + bool operator()(GameObject* go) const + { + if (go->GetGOInfo()->type != GAMEOBJECT_TYPE_SPELL_FOCUS) + return false; + + if (go->GetGOInfo()->spellFocus.focusId != i_focusId) + return false; + + float dist = (go->GetGOInfo()->spellFocus.dist)/2; + + return go->IsWithinDistInMap(i_unit, dist); + } + private: + Unit const* i_unit; + uint32 i_focusId; + }; + + // Find the nearest Fishing hole and return true only if source object is in range of hole + class NearestGameObjectFishingHole + { + public: + NearestGameObjectFishingHole(WorldObject const& obj, float range) : i_obj(obj), i_range(range) {} + bool operator()(GameObject* go) + { + if (go->GetGOInfo()->type == GAMEOBJECT_TYPE_FISHINGHOLE && go->isSpawned() && i_obj.IsWithinDistInMap(go, i_range) && i_obj.IsWithinDistInMap(go, go->GetGOInfo()->fishinghole.radius)) + { + i_range = i_obj.GetDistance(go); + return true; + } + return false; + } + float GetLastRange() const { return i_range; } + private: + WorldObject const& i_obj; + float i_range; + + // prevent clone + NearestGameObjectFishingHole(NearestGameObjectFishingHole const&); + }; + + class NearestGameObjectCheck + { + public: + NearestGameObjectCheck(WorldObject const& obj) : i_obj(obj), i_range(999) {} + bool operator()(GameObject* go) + { + if (i_obj.IsWithinDistInMap(go, i_range)) + { + i_range = i_obj.GetDistance(go); // use found GO range as new range limit for next check + return true; + } + return false; + } + float GetLastRange() const { return i_range; } + private: + WorldObject const& i_obj; + float i_range; + + // prevent clone this object + NearestGameObjectCheck(NearestGameObjectCheck const&); + }; + + // Success at unit in range, range update for next check (this can be use with GameobjectLastSearcher to find nearest GO) + class NearestGameObjectEntryInObjectRangeCheck + { + public: + NearestGameObjectEntryInObjectRangeCheck(WorldObject const& obj,uint32 entry, float range) : i_obj(obj), i_entry(entry), i_range(range) {} + bool operator()(GameObject* go) + { + if (go->GetEntry() == i_entry && i_obj.IsWithinDistInMap(go, i_range)) + { + i_range = i_obj.GetDistance(go); // use found GO range as new range limit for next check + return true; + } + return false; + } + float GetLastRange() const { return i_range; } + private: + WorldObject const& i_obj; + uint32 i_entry; + float i_range; + + // prevent clone this object + NearestGameObjectEntryInObjectRangeCheck(NearestGameObjectEntryInObjectRangeCheck const&); + }; + + class GameObjectWithDbGUIDCheck + { + public: + GameObjectWithDbGUIDCheck(WorldObject const& obj,uint32 db_guid) : i_obj(obj), i_db_guid(db_guid) {} + bool operator()(GameObject const* go) const + { + return go->GetDBTableGUIDLow() == i_db_guid; + } + private: + WorldObject const& i_obj; + uint32 i_db_guid; + }; + + // Unit checks + + class MostHPMissingInRange + { + public: + MostHPMissingInRange(Unit const* obj, float range, uint32 hp) : i_obj(obj), i_range(range), i_hp(hp) {} + bool operator()(Unit* u) + { + if (u->isAlive() && u->isInCombat() && !i_obj->IsHostileTo(u) && i_obj->IsWithinDistInMap(u, i_range) && u->GetMaxHealth() - u->GetHealth() > i_hp) + { + i_hp = u->GetMaxHealth() - u->GetHealth(); + return true; + } + return false; + } + private: + Unit const* i_obj; + float i_range; + uint32 i_hp; + }; + + class FriendlyCCedInRange + { + public: + FriendlyCCedInRange(Unit const* obj, float range) : i_obj(obj), i_range(range) {} + bool operator()(Unit* u) + { + if (u->isAlive() && u->isInCombat() && !i_obj->IsHostileTo(u) && i_obj->IsWithinDistInMap(u, i_range) && + (u->isFeared() || u->isCharmed() || u->isFrozen() || u->hasUnitState(UNIT_STAT_STUNNED) || u->hasUnitState(UNIT_STAT_CONFUSED))) + { + return true; + } + return false; + } + private: + Unit const* i_obj; + float i_range; + }; + + class FriendlyMissingBuffInRange + { + public: + FriendlyMissingBuffInRange(Unit const* obj, float range, uint32 spellid) : i_obj(obj), i_range(range), i_spell(spellid) {} + bool operator()(Unit* u) + { + if (u->isAlive() && u->isInCombat() && !i_obj->IsHostileTo(u) && i_obj->IsWithinDistInMap(u, i_range) && + !(u->HasAura(i_spell))) + { + return true; + } + return false; + } + private: + Unit const* i_obj; + float i_range; + uint32 i_spell; + }; + + class AnyUnfriendlyUnitInObjectRangeCheck + { + public: + AnyUnfriendlyUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) : i_obj(obj), i_funit(funit), i_range(range) {} + bool operator()(Unit* u) + { + if (u->isAlive() && i_obj->IsWithinDistInMap(u, i_range) && !i_funit->IsFriendlyTo(u)) + return true; + else + return false; + } + private: + WorldObject const* i_obj; + Unit const* i_funit; + float i_range; + }; + + class AnyUnfriendlyNoTotemUnitInObjectRangeCheck + { + public: + AnyUnfriendlyNoTotemUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) : i_obj(obj), i_funit(funit), i_range(range) {} + bool operator()(Unit* u) + { + if (!u->isAlive()) + return false; + + if (u->GetTypeId() == TYPEID_UNIT && ((Creature*)u)->isTotem()) + return false; + + return i_obj->IsWithinDistInMap(u, i_range) && !i_funit->IsFriendlyTo(u); + } + private: + WorldObject const* i_obj; + Unit const* i_funit; + float i_range; + }; + + class AnyUnfriendlyVisibleUnitInObjectRangeCheck + { + public: + AnyUnfriendlyVisibleUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) + : i_obj(obj), i_funit(funit), i_range(range) {} + + bool operator()(Unit* u) + { + return u->isAlive() + && i_obj->IsWithinDistInMap(u, i_range) + && !i_funit->IsFriendlyTo(u) + && u->isVisibleForOrDetect(i_funit, false); + } + private: + WorldObject const* i_obj; + Unit const* i_funit; + float i_range; + }; + + class CreatureWithDbGUIDCheck + { + public: + CreatureWithDbGUIDCheck(WorldObject const* obj, uint32 lowguid) : i_obj(obj), i_lowguid(lowguid) {} + bool operator()(Creature* u) + { + return u->GetDBTableGUIDLow() == i_lowguid; + } + private: + WorldObject const* i_obj; + uint32 i_lowguid; + }; + + class AnyFriendlyUnitInObjectRangeCheck + { + public: + AnyFriendlyUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) : i_obj(obj), i_funit(funit), i_range(range) {} + bool operator()(Unit* u) + { + if (u->isAlive() && i_obj->IsWithinDistInMap(u, i_range) && i_funit->IsFriendlyTo(u)) + return true; + else + return false; + } + private: + WorldObject const* i_obj; + Unit const* i_funit; + float i_range; + }; + + class AnyUnitInObjectRangeCheck + { + public: + AnyUnitInObjectRangeCheck(WorldObject const* obj, float range) : i_obj(obj), i_range(range) {} + bool operator()(Unit* u) + { + if (u->isAlive() && i_obj->IsWithinDistInMap(u, i_range)) + return true; + + return false; + } + private: + WorldObject const* i_obj; + float i_range; + }; + + // Success at unit in range, range update for next check (this can be use with UnitLastSearcher to find nearest unit) + class NearestAttackableUnitInObjectRangeCheck + { + public: + NearestAttackableUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) : i_obj(obj), i_funit(funit), i_range(range) {} + bool operator()(Unit* u) + { + if (u->isTargetableForAttack() && i_obj->IsWithinDistInMap(u, i_range) && + !i_funit->IsFriendlyTo(u) && u->isVisibleForOrDetect(i_funit,false)) + { + i_range = i_obj->GetDistance(u); // use found unit range as new range limit for next check + return true; + } + + return false; + } + private: + WorldObject const* i_obj; + Unit const* i_funit; + float i_range; + + // prevent clone this object + NearestAttackableUnitInObjectRangeCheck(NearestAttackableUnitInObjectRangeCheck const&); + }; + + class AnyAoETargetUnitInObjectRangeCheck + { + public: + AnyAoETargetUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) + : i_obj(obj), i_funit(funit), i_range(range) + { + Unit const* check = i_funit; + Unit const* owner = i_funit->GetOwner(); + if (owner) + check = owner; + i_targetForPlayer = (check->GetTypeId() == TYPEID_PLAYER); + } + bool operator()(Unit* u) + { + // Check contains checks for: live, non-selectable, non-attackable flags, flight check and GM check, ignore totems + if (!u->isTargetableForAttack()) + return false; + if (u->GetTypeId() == TYPEID_UNIT && ((Creature*)u)->isTotem()) + return false; + + if ((i_targetForPlayer ? !i_funit->IsFriendlyTo(u) : i_funit->IsHostileTo(u))&& i_obj->IsWithinDistInMap(u, i_range)) + return true; + + return false; + } + private: + bool i_targetForPlayer; + WorldObject const* i_obj; + Unit const* i_funit; + float i_range; + }; + + // do attack at call of help to friendly crearture + class CallOfHelpCreatureInRangeDo + { + public: + CallOfHelpCreatureInRangeDo(Unit* funit, Unit* enemy, float range) + : i_funit(funit), i_enemy(enemy), i_range(range) + {} + void operator()(Creature* u) + { + if (u == i_funit) + return; + + if (!u->CanAssistTo(i_funit, i_enemy, false)) + return; + + // too far + if (!u->IsWithinDistInMap(i_enemy, i_range)) + return; + + // only if see assisted creature's enemy + if (!u->IsWithinLOSInMap(i_enemy)) + return; + + if (u->AI()) + u->AI()->AttackStart(i_enemy); + } + private: + Unit* const i_funit; + Unit* const i_enemy; + float i_range; + }; + + struct AnyDeadUnitCheck + { + bool operator()(Unit* u) { return !u->isAlive(); } + }; + + struct AnyStealthedCheck + { + bool operator()(Unit* u) { return u->GetVisibility() == VISIBILITY_GROUP_STEALTH; } + }; + + // Creature checks + + class NearestHostileUnitCheck + { + public: + explicit NearestHostileUnitCheck(Creature const* creature, float dist = 0) : me(creature) + { + m_range = (dist == 0 ? 9999 : dist); + } + bool operator()(Unit* u) + { + if (!me->IsWithinDistInMap(u, m_range)) + return false; + + if (!me->canAttack(u)) + return false; + + m_range = me->GetDistance(u); // use found unit range as new range limit for next check + return true; + } + + private: + Creature const *me; + float m_range; + NearestHostileUnitCheck(NearestHostileUnitCheck const&); + }; + + class NearestHostileUnitInAttackDistanceCheck + { + public: + explicit NearestHostileUnitInAttackDistanceCheck(Creature const* creature, float dist = 0) : me(creature) + { + m_range = (dist == 0 ? 9999 : dist); + m_force = (dist == 0 ? false : true); + } + bool operator()(Unit* u) + { + if (!me->IsWithinDistInMap(u, m_range)) + return false; + + if (m_force) + { + if (!me->canAttack(u)) + return false; + } + else + { + if (!me->canStartAttack(u, false)) + return false; + } + + m_range = me->GetDistance(u); // use found unit range as new range limit for next check + return true; + } + float GetLastRange() const { return m_range; } + private: + Creature const *me; + float m_range; + bool m_force; + NearestHostileUnitInAttackDistanceCheck(NearestHostileUnitInAttackDistanceCheck const&); + }; + + class AnyAssistCreatureInRangeCheck + { + public: + AnyAssistCreatureInRangeCheck(Unit* funit, Unit* enemy, float range) + : i_funit(funit), i_enemy(enemy), i_range(range) + { + } + bool operator()(Creature* u) + { + if (u == i_funit) + return false; + + if (!u->CanAssistTo(i_funit, i_enemy)) + return false; + + // too far + if (!i_funit->IsWithinDistInMap(u, i_range)) + return false; + + // only if see assisted creature + if (!i_funit->IsWithinLOSInMap(u)) + return false; + + return true; + } + private: + Unit* const i_funit; + Unit* const i_enemy; + float i_range; + }; + + class NearestAssistCreatureInCreatureRangeCheck + { + public: + NearestAssistCreatureInCreatureRangeCheck(Creature* obj, Unit* enemy, float range) + : i_obj(obj), i_enemy(enemy), i_range(range) {} + + bool operator()(Creature* u) + { + if (u == i_obj) + return false; + if (!u->CanAssistTo(i_obj,i_enemy)) + return false; + + if (!i_obj->IsWithinDistInMap(u, i_range)) + return false; + + if (!i_obj->IsWithinLOSInMap(u)) + return false; + + i_range = i_obj->GetDistance(u); // use found unit range as new range limit for next check + return true; + } + float GetLastRange() const { return i_range; } + private: + Creature* const i_obj; + Unit* const i_enemy; + float i_range; + + // prevent clone this object + NearestAssistCreatureInCreatureRangeCheck(NearestAssistCreatureInCreatureRangeCheck const&); + }; + + // Success at unit in range, range update for next check (this can be use with CreatureLastSearcher to find nearest creature) + class NearestCreatureEntryWithLiveStateInObjectRangeCheck + { + public: + NearestCreatureEntryWithLiveStateInObjectRangeCheck(WorldObject const& obj, uint32 entry, bool alive, float range) + : i_obj(obj), i_entry(entry), i_alive(alive), i_range(range) {} + + bool operator()(Creature* u) + { + if (u->GetEntry() == i_entry && u->isAlive() == i_alive && i_obj.IsWithinDistInMap(u, i_range)) + { + i_range = i_obj.GetDistance(u); // use found unit range as new range limit for next check + return true; + } + return false; + } + float GetLastRange() const { return i_range; } + private: + WorldObject const& i_obj; + uint32 i_entry; + bool i_alive; + float i_range; + + // prevent clone this object + NearestCreatureEntryWithLiveStateInObjectRangeCheck(NearestCreatureEntryWithLiveStateInObjectRangeCheck const&); + }; + + class AnyPlayerInObjectRangeCheck + { + public: + AnyPlayerInObjectRangeCheck(WorldObject const* obj, float range) : i_obj(obj), i_range(range) {} + bool operator()(Player* u) + { + if (u->isAlive() && i_obj->IsWithinDistInMap(u, i_range)) + return true; + + return false; + } + private: + WorldObject const* i_obj; + float i_range; + }; + + class AllFriendlyCreaturesInGrid + { + public: + AllFriendlyCreaturesInGrid(Unit const* obj) : pUnit(obj) {} + bool operator() (Unit* u) + { + if (u->isAlive() && u->GetVisibility() == VISIBILITY_ON && u->IsFriendlyTo(pUnit)) + return true; + + return false; + } + private: + Unit const* pUnit; + }; + + class AllGameObjectsWithEntryInRange + { + public: + AllGameObjectsWithEntryInRange(const WorldObject* pObject, uint32 uiEntry, float fMaxRange) : m_pObject(pObject), m_uiEntry(uiEntry), m_fRange(fMaxRange) {} + bool operator() (GameObject* pGo) + { + if (pGo->GetEntry() == m_uiEntry && m_pObject->IsWithinDist(pGo,m_fRange,false)) + return true; + + return false; + } + private: + const WorldObject* m_pObject; + uint32 m_uiEntry; + float m_fRange; + }; + + class AllCreaturesOfEntryInRange + { + public: + AllCreaturesOfEntryInRange(const WorldObject* pObject, uint32 uiEntry, float fMaxRange) : m_pObject(pObject), m_uiEntry(uiEntry), m_fRange(fMaxRange) {} + bool operator() (Unit* pUnit) + { + if (pUnit->GetEntry() == m_uiEntry && m_pObject->IsWithinDist(pUnit,m_fRange,false)) + return true; + + return false; + } + + private: + const WorldObject* m_pObject; + uint32 m_uiEntry; + float m_fRange; + }; + + class PlayerAtMinimumRangeAway + { + public: + PlayerAtMinimumRangeAway(Unit const* unit, float fMinRange) : pUnit(unit), fRange(fMinRange) {} + bool operator() (Player* pPlayer) + { + //No threat list check, must be done explicit if expected to be in combat with creature + if (!pPlayer->isGameMaster() && pPlayer->isAlive() && !pUnit->IsWithinDist(pPlayer,fRange,false)) + return true; + + return false; + } + + private: + Unit const* pUnit; + float fRange; + }; + + class GameObjectInRangeCheck + { + public: + GameObjectInRangeCheck(float _x, float _y, float _z, float _range) : x(_x), y(_y), z(_z), range(_range) {} + bool operator() (GameObject* go) + { + return go->IsInRange(x, y, z, range); + } + private: + float x, y, z, range; + }; + + // Player checks and do + + // Prepare using Builder localized packets with caching and send to player + template + class LocalizedPacketDo + { + public: + explicit LocalizedPacketDo(Builder& builder) : i_builder(builder) {} + + ~LocalizedPacketDo() + { + for (size_t i = 0; i < i_data_cache.size(); ++i) + delete i_data_cache[i]; + } + void operator()(Player* p); + + private: + Builder& i_builder; + std::vector i_data_cache; // 0 = default, i => i-1 locale index + }; + + // Prepare using Builder localized packets with caching and send to player + template + class LocalizedPacketListDo + { + public: + typedef std::vector WorldPacketList; + explicit LocalizedPacketListDo(Builder& builder) : i_builder(builder) {} + + ~LocalizedPacketListDo() + { + for (size_t i = 0; i < i_data_cache.size(); ++i) + for (size_t j = 0; j < i_data_cache[i].size(); ++j) + delete i_data_cache[i][j]; + } + void operator()(Player* p); + + private: + Builder& i_builder; + std::vector i_data_cache; + // 0 = default, i => i-1 locale index + }; +} +#endif diff --git a/src/server/game/Grids/Notifiers/GridNotifiersImpl.h b/src/server/game/Grids/Notifiers/GridNotifiersImpl.h new file mode 100644 index 00000000000..26a9c0bd328 --- /dev/null +++ b/src/server/game/Grids/Notifiers/GridNotifiersImpl.h @@ -0,0 +1,453 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_GRIDNOTIFIERSIMPL_H +#define TRINITY_GRIDNOTIFIERSIMPL_H + +#include "GridNotifiers.h" +#include "WorldPacket.h" +#include "Corpse.h" +#include "Player.h" +#include "UpdateData.h" +#include "CreatureAI.h" +#include "SpellAuras.h" + + +template +inline void +Trinity::VisibleNotifier::Visit(GridRefManager &m) +{ + for (typename GridRefManager::iterator iter = m.begin(); iter != m.end(); ++iter) + { + vis_guids.erase(iter->getSource()->GetGUID()); + i_player.UpdateVisibilityOf(iter->getSource(),i_data,i_visibleNow); + } +} + +inline void +Trinity::ObjectUpdater::Visit(CreatureMapType &m) +{ + for (CreatureMapType::iterator iter=m.begin(); iter != m.end(); ++iter) + if (iter->getSource()->IsInWorld() && !iter->getSource()->isSpiritService()) + iter->getSource()->Update(i_timeDiff); +} + +// SEARCHERS & LIST SEARCHERS & WORKERS + +// WorldObject searchers & workers + +template +void Trinity::WorldObjectSearcher::Visit(GameObjectMapType &m) +{ + // already found + if (i_object) + return; + + for (GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + { + if (!itr->getSource()->InSamePhase(i_phaseMask)) + continue; + + if (i_check(itr->getSource())) + { + i_object = itr->getSource(); + return; + } + } +} + +template +void Trinity::WorldObjectSearcher::Visit(PlayerMapType &m) +{ + // already found + if (i_object) + return; + + for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + { + if (!itr->getSource()->InSamePhase(i_phaseMask)) + continue; + + if (i_check(itr->getSource())) + { + i_object = itr->getSource(); + return; + } + } +} + +template +void Trinity::WorldObjectSearcher::Visit(CreatureMapType &m) +{ + // already found + if (i_object) + return; + + for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + { + if (!itr->getSource()->InSamePhase(i_phaseMask)) + continue; + + if (i_check(itr->getSource())) + { + i_object = itr->getSource(); + return; + } + } +} + +template +void Trinity::WorldObjectSearcher::Visit(CorpseMapType &m) +{ + // already found + if (i_object) + return; + + for (CorpseMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + { + if (!itr->getSource()->InSamePhase(i_phaseMask)) + continue; + + if (i_check(itr->getSource())) + { + i_object = itr->getSource(); + return; + } + } +} + +template +void Trinity::WorldObjectSearcher::Visit(DynamicObjectMapType &m) +{ + // already found + if (i_object) + return; + + for (DynamicObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + { + if (!itr->getSource()->InSamePhase(i_phaseMask)) + continue; + + if (i_check(itr->getSource())) + { + i_object = itr->getSource(); + return; + } + } +} + +template +void Trinity::WorldObjectListSearcher::Visit(PlayerMapType &m) +{ + for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + if (i_check(itr->getSource())) + i_objects.push_back(itr->getSource()); +} + +template +void Trinity::WorldObjectListSearcher::Visit(CreatureMapType &m) +{ + for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + if (i_check(itr->getSource())) + i_objects.push_back(itr->getSource()); +} + +template +void Trinity::WorldObjectListSearcher::Visit(CorpseMapType &m) +{ + for (CorpseMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + if (i_check(itr->getSource())) + i_objects.push_back(itr->getSource()); +} + +template +void Trinity::WorldObjectListSearcher::Visit(GameObjectMapType &m) +{ + for (GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + if (i_check(itr->getSource())) + i_objects.push_back(itr->getSource()); +} + +template +void Trinity::WorldObjectListSearcher::Visit(DynamicObjectMapType &m) +{ + for (DynamicObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + if (i_check(itr->getSource())) + i_objects.push_back(itr->getSource()); +} + +// Gameobject searchers + +template +void Trinity::GameObjectSearcher::Visit(GameObjectMapType &m) +{ + // already found + if (i_object) + return; + + for (GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + { + if (!itr->getSource()->InSamePhase(i_phaseMask)) + continue; + + if (i_check(itr->getSource())) + { + i_object = itr->getSource(); + return; + } + } +} + +template +void Trinity::GameObjectLastSearcher::Visit(GameObjectMapType &m) +{ + for (GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + { + if (!itr->getSource()->InSamePhase(i_phaseMask)) + continue; + + if (i_check(itr->getSource())) + i_object = itr->getSource(); + } +} + +template +void Trinity::GameObjectListSearcher::Visit(GameObjectMapType &m) +{ + for (GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + if (i_check(itr->getSource())) + i_objects.push_back(itr->getSource()); +} + +// Unit searchers + +template +void Trinity::UnitSearcher::Visit(CreatureMapType &m) +{ + // already found + if (i_object) + return; + + for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + { + if (!itr->getSource()->InSamePhase(i_phaseMask)) + continue; + + if (i_check(itr->getSource())) + { + i_object = itr->getSource(); + return; + } + } +} + +template +void Trinity::UnitSearcher::Visit(PlayerMapType &m) +{ + // already found + if (i_object) + return; + + for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + { + if (!itr->getSource()->InSamePhase(i_phaseMask)) + continue; + + if (i_check(itr->getSource())) + { + i_object = itr->getSource(); + return; + } + } +} + +template +void Trinity::UnitLastSearcher::Visit(CreatureMapType &m) +{ + for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + { + if (!itr->getSource()->InSamePhase(i_phaseMask)) + continue; + + if (i_check(itr->getSource())) + i_object = itr->getSource(); + } +} + +template +void Trinity::UnitLastSearcher::Visit(PlayerMapType &m) +{ + for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + { + if (!itr->getSource()->InSamePhase(i_phaseMask)) + continue; + + if (i_check(itr->getSource())) + i_object = itr->getSource(); + } +} + +template +void Trinity::UnitListSearcher::Visit(PlayerMapType &m) +{ + for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + if (i_check(itr->getSource())) + i_objects.push_back(itr->getSource()); +} + +template +void Trinity::UnitListSearcher::Visit(CreatureMapType &m) +{ + for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + if (i_check(itr->getSource())) + i_objects.push_back(itr->getSource()); +} + +// Creature searchers + +template +void Trinity::CreatureSearcher::Visit(CreatureMapType &m) +{ + // already found + if (i_object) + return; + + for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + { + if (!itr->getSource()->InSamePhase(i_phaseMask)) + continue; + + if (i_check(itr->getSource())) + { + i_object = itr->getSource(); + return; + } + } +} + +template +void Trinity::CreatureLastSearcher::Visit(CreatureMapType &m) +{ + for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + { + if (!itr->getSource()->InSamePhase(i_phaseMask)) + continue; + + if (i_check(itr->getSource())) + i_object = itr->getSource(); + } +} + +template +void Trinity::CreatureListSearcher::Visit(CreatureMapType &m) +{ + for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + if (i_check(itr->getSource())) + i_objects.push_back(itr->getSource()); +} + +template +void Trinity::PlayerListSearcher::Visit(PlayerMapType &m) +{ + for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + if (i_check(itr->getSource())) + i_objects.push_back(itr->getSource()); +} + +template +void Trinity::PlayerSearcher::Visit(PlayerMapType &m) +{ + // already found + if (i_object) + return; + + for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + { + if (!itr->getSource()->InSamePhase(i_phaseMask)) + continue; + + if (i_check(itr->getSource())) + { + i_object = itr->getSource(); + return; + } + } +} + +template +void Trinity::LocalizedPacketDo::operator()(Player* p) +{ + int32 loc_idx = p->GetSession()->GetSessionDbLocaleIndex(); + uint32 cache_idx = loc_idx+1; + WorldPacket* data; + + // create if not cached yet + if (i_data_cache.size() < cache_idx+1 || !i_data_cache[cache_idx]) + { + if (i_data_cache.size() < cache_idx+1) + i_data_cache.resize(cache_idx+1); + + data = new WorldPacket(SMSG_MESSAGECHAT, 200); + + i_builder(*data,loc_idx); + + i_data_cache[cache_idx] = data; + } + else + data = i_data_cache[cache_idx]; + + p->SendDirectMessage(data); +} + +template +void Trinity::LocalizedPacketListDo::operator()(Player* p) +{ + int32 loc_idx = p->GetSession()->GetSessionDbLocaleIndex(); + uint32 cache_idx = loc_idx+1; + WorldPacketList* data_list; + + // create if not cached yet + if (i_data_cache.size() < cache_idx+1 || i_data_cache[cache_idx].empty()) + { + if (i_data_cache.size() < cache_idx+1) + i_data_cache.resize(cache_idx+1); + + data_list = &i_data_cache[cache_idx]; + + i_builder(*data_list,loc_idx); + } + else + data_list = &i_data_cache[cache_idx]; + + for (size_t i = 0; i < data_list->size(); ++i) + p->SendDirectMessage((*data_list)[i]); +} + +#endif // TRINITY_GRIDNOTIFIERSIMPL_H diff --git a/src/server/shared/CompilerDefs.h b/src/server/shared/CompilerDefs.h new file mode 100644 index 00000000000..fb7dbfe4caa --- /dev/null +++ b/src/server/shared/CompilerDefs.h @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_COMPILERDEFS_H +#define TRINITY_COMPILERDEFS_H + +#define PLATFORM_WINDOWS 0 +#define PLATFORM_UNIX 1 +#define PLATFORM_APPLE 2 +#define PLATFORM_INTEL 3 + +// must be first (win 64 also define WIN32) +#if defined( _WIN64 ) +# define PLATFORM PLATFORM_WINDOWS +#elif defined( __WIN32__ ) || defined( WIN32 ) || defined( _WIN32 ) +# define PLATFORM PLATFORM_WINDOWS +#elif defined( __APPLE_CC__ ) +# define PLATFORM PLATFORM_APPLE +#elif defined( __INTEL_COMPILER ) +# define PLATFORM PLATFORM_INTEL +#else +# define PLATFORM PLATFORM_UNIX +#endif + +#define COMPILER_MICROSOFT 0 +#define COMPILER_GNU 1 +#define COMPILER_BORLAND 2 +#define COMPILER_INTEL 3 + +#ifdef _MSC_VER +# define COMPILER COMPILER_MICROSOFT +#elif defined( __BORLANDC__ ) +# define COMPILER COMPILER_BORLAND +#elif defined( __INTEL_COMPILER ) +# define COMPILER COMPILER_INTEL +#elif defined( __GNUC__ ) +# define COMPILER COMPILER_GNU +#else +# pragma error "FATAL ERROR: Unknown compiler." +#endif + +#if COMPILER == COMPILER_MICROSOFT +# pragma warning( disable : 4267 ) // conversion from 'size_t' to 'int', possible loss of data +# pragma warning( disable : 4786 ) // identifier was truncated to '255' characters in the debug information +#endif +#endif + diff --git a/src/server/shared/Define.h b/src/server/shared/Define.h new file mode 100644 index 00000000000..9285bf289f9 --- /dev/null +++ b/src/server/shared/Define.h @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_DEFINE_H +#define TRINITY_DEFINE_H + +#include + +#include +#include + +#include "Platform/CompilerDefs.h" + +#define TRINITY_LITTLEENDIAN 0 +#define TRINITY_BIGENDIAN 1 + +#if !defined(TRINITY_ENDIAN) +# if defined (ACE_BIG_ENDIAN) +# define TRINITY_ENDIAN TRINITY_BIGENDIAN +# else //ACE_BYTE_ORDER != ACE_BIG_ENDIAN +# define TRINITY_ENDIAN TRINITY_LITTLEENDIAN +# endif //ACE_BYTE_ORDER +#endif //TRINITY_ENDIAN + +#if PLATFORM == PLATFORM_WINDOWS +# define TRINITY_PATH_MAX MAX_PATH +# ifndef DECLSPEC_NORETURN +# define DECLSPEC_NORETURN __declspec(noreturn) +# endif //DECLSPEC_NORETURN +#else //PLATFORM != PLATFORM_WINDOWS +# define TRINITY_PATH_MAX PATH_MAX +# define DECLSPEC_NORETURN +#endif //PLATFORM + +#if !defined(DEBUG) +# define TRINITY_INLINE inline +#else //DEBUG +# if !defined(TRINITY_DEBUG) +# define TRINITY_DEBUG +# endif //TRINITY_DEBUG +# define TRINITY_INLINE +#endif //!DEBUG + +#if COMPILER == COMPILER_GNU +# define ATTR_NORETURN __attribute__((noreturn)) +# define ATTR_PRINTF(F,V) __attribute__ ((format (printf, F, V))) +#else //COMPILER != COMPILER_GNU +# define ATTR_NORETURN +# define ATTR_PRINTF(F,V) +#endif //COMPILER == COMPILER_GNU + +typedef ACE_INT64 int64; +typedef ACE_INT32 int32; +typedef ACE_INT16 int16; +typedef ACE_INT8 int8; +typedef ACE_UINT64 uint64; +typedef ACE_UINT32 uint32; +typedef ACE_UINT16 uint16; +typedef ACE_UINT8 uint8; + +#if COMPILER != COMPILER_MICROSOFT +typedef uint16 WORD; +typedef uint32 DWORD; +#endif //COMPILER + +typedef uint64 OBJECT_HANDLE; + +#endif //TRINITY_DEFINE_H diff --git a/src/server/shared/Dynamic/CountedReference/Reference.h b/src/server/shared/Dynamic/CountedReference/Reference.h new file mode 100644 index 00000000000..d3cfe55ffc0 --- /dev/null +++ b/src/server/shared/Dynamic/CountedReference/Reference.h @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_REFERENCE_H +#define TRINITY_REFERENCE_H + +/** + * Referencer + * Referencer is an object that holds a reference holder that hold a reference + * counted object. When an object's reference count drop to zero, it removes + * the object. This is a non intrusive mechanism and any object at any point + * in time can be referenced. When and object is reference counted, do not + * pass the object directly to other methods but rather, pass its + * reference around. Objects can be reference counted in both single threaded + * model and multi-threaded model + */ + +#include +#include "Platform/Define.h" +#include "Policies/ThreadingModel.h" +#include "ReferenceHolder.h" + +template +< +typename T, +class THREADING_MODEL = Trinity::SingleThreaded +> +class Referencer +{ + typedef typename THREADING_MODEL::Lock Lock; + typedef ReferenceHolder ReferenceeHolder; + public: + + /// Constructs a referencer. + Referencer(T *ref = NULL); + + /// Copy constructor + Referencer(const Referencer &obj) : i_holder(NULL) { *this = obj; } + + /// Destructor + ~Referencer(); + + /// Referencee accessor + T* referencee(void) { return (i_holder == NULL ? NULL : i_holder->i_referencee); } + const T* referencee(void) const { return (i_holder == NULL ? NULL : i_holder->i_referencee); } + + //T& referencee(void){ return _referencee(); } + //const T& referencee(void) const { return const_cast(this)->_referencee(); } + operator T&(void) { return _referencee(); } + operator const T&(void) const { return *const_cast(this)->_referencee(); } + + /// cast operators + T* operator*() { return (i_holder == NULL ? NULL : i_holder->i_referencee); } + T const * operator*() const { return (i_holder == NULL ? NULL : i_holder->i_referencee); } + + /// overload operators + T* operator->() { return (i_holder == NULL ? NULL : i_holder->i_referencee); } + const T * operator->() const { return (i_holder == NULL ? NULL : i_holder->i_referencee); } + + /// operator = + Referencer& operator=(const Referencer &obj); + Referencer& operator=(T *); + + /// returns true if i_referencee is null + bool isNull(void) const { return i_holder == NULL; } + + private: + + T& _referencee(void) + { + if( i_holder == NULL ) + throw std::runtime_error("Invalid access to null pointer"); + return *i_holder->i_referencee; + } + + void deReference(ReferenceeHolder *); + void addReference(ReferenceeHolder *); + + // private data + ReferenceeHolder *i_holder; +}; +#endif + diff --git a/src/server/shared/Dynamic/CountedReference/ReferenceHolder.h b/src/server/shared/Dynamic/CountedReference/ReferenceHolder.h new file mode 100644 index 00000000000..597e9854be0 --- /dev/null +++ b/src/server/shared/Dynamic/CountedReference/ReferenceHolder.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_REFERENCEHOLDER_H +#define TRINITY_REFERENCEHOLDER_H + +/** ReferenceHolder holds the actualy referenced obejct as well the refence + count. The ReferenecHolder implements as a policy base object and + will decided by the Reference class to be consnsitent. + */ + +template +< +typename T, +class THREADING_MODEL +> +struct ReferenceHolder : public THREADING_MODEL +{ + explicit ReferenceHolder(T *ref) : i_referencee(ref), i_referenceCount(0) {} + T *i_referencee; + unsigned int i_referenceCount; + typedef typename THREADING_MODEL::Lock Lock; +}; +#endif + diff --git a/src/server/shared/Dynamic/CountedReference/ReferenceImpl.h b/src/server/shared/Dynamic/CountedReference/ReferenceImpl.h new file mode 100644 index 00000000000..cde330179e3 --- /dev/null +++ b/src/server/shared/Dynamic/CountedReference/ReferenceImpl.h @@ -0,0 +1,133 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_REFERENCEIMPL_H +#define TRINITY_REFERENCEIMPL_H + +#include "Reference.h" + +template +< +typename T, +class THREADING_MODEL +> +Referencer::Referencer(T *ref) +: i_holder(NULL) +{ + if( ref != NULL ) + { + i_holder = new ReferenceeHolder(ref); + ++i_holder->i_referenceCount; + } +} + +template +< +typename T, +class THREADING_MODEL +> +Referencer::~Referencer() +{ + if( i_holder != NULL ) + deReference(i_holder); + i_holder = NULL; +} + +template +< +typename T, +class THREADING_MODEL +> +Referencer& +Referencer::operator=(const Referencer &obj) +{ + if( i_holder != NULL ) + deReference(i_holder); + if( obj.i_holder != NULL ) + addReference(obj.i_holder); + i_holder = obj.i_holder; + return *this; +} + +template +< +typename T, +class THREADING_MODEL +> +Referencer& +Referencer::operator=(T *ref) +{ + if( i_holder != NULL ) + deReference(i_holder); + i_holder = NULL; + if( ref != NULL ) + { + i_holder = new ReferenceeHolder(ref); + ++i_holder->i_referenceCount; + } + + return *this; +} + +template +< +typename T, +class THREADING_MODEL +> +void +Referencer::deReference(ReferenceHolder *holder) +{ + assert( holder != NULL && holder->i_referenceCount > 0); + bool delete_object = false; + + { + // The guard is within the scope due to the guard + // must release earlier than expected. + Lock guard(*holder); + Guard(&guard); + + --holder->i_referenceCount; + if( holder->i_referenceCount == 0 ) + delete_object = true; + } + + if( delete_object ) + { + delete holder->i_referencee; + delete holder; + } +} + +template +< +typename T, +class THREADING_MODEL +> +void +Referencer::addReference(ReferenceHolder *holder) +{ + assert( i_holder != NULL ); + Lock guard(*holder); + Guard(&guard); + + ++holder->i_referenceCount; +} +#endif + diff --git a/src/server/shared/Dynamic/FactoryHolder.h b/src/server/shared/Dynamic/FactoryHolder.h new file mode 100644 index 00000000000..282968d6097 --- /dev/null +++ b/src/server/shared/Dynamic/FactoryHolder.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_FACTORY_HOLDER +#define TRINITY_FACTORY_HOLDER + +#include "Platform/Define.h" +#include "Utilities/TypeList.h" +#include "ObjectRegistry.h" +#include "Policies/SingletonImp.h" + +/** FactoryHolder holds a factory object of a specific type + */ +template +class FactoryHolder +{ + public: + typedef ObjectRegistry, Key > FactoryHolderRegistry; + typedef Trinity::Singleton FactoryHolderRepository; + + FactoryHolder(Key k) : i_key(k) {} + virtual ~FactoryHolder() {} + inline Key key() const { return i_key; } + + void RegisterSelf(void) { FactoryHolderRepository::Instance().InsertItem(this, i_key); } + void DeregisterSelf(void) { FactoryHolderRepository::Instance().RemoveItem(this, false); } + + /// Abstract Factory create method + virtual T* Create(void *data = NULL) const = 0; + private: + Key i_key; +}; + +/** Permissible is a classic way of letting the object decide + * whether how good they handle things. This is not retricted + * to factory selectors. + */ +template +class Permissible +{ + public: + virtual ~Permissible() {} + virtual int Permit(const T *) const = 0; +}; +#endif + diff --git a/src/server/shared/Dynamic/LinkedList.h b/src/server/shared/Dynamic/LinkedList.h new file mode 100644 index 00000000000..b26687767b3 --- /dev/null +++ b/src/server/shared/Dynamic/LinkedList.h @@ -0,0 +1,247 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef _LINKEDLIST +#define _LINKEDLIST + +#include "Common.h" + +//============================================ +class LinkedListHead; + +class LinkedListElement +{ + private: + friend class LinkedListHead; + + LinkedListElement* iNext; + LinkedListElement* iPrev; + public: + LinkedListElement() { iNext = NULL; iPrev = NULL; } + ~LinkedListElement() { delink(); } + + bool hasNext() const { return(iNext && iNext->iNext != NULL); } + bool hasPrev() const { return(iPrev && iPrev->iPrev != NULL); } + bool isInList() const { return(iNext != NULL && iPrev != NULL); } + + LinkedListElement * next() { return hasNext() ? iNext : NULL; } + LinkedListElement const* next() const { return hasNext() ? iNext : NULL; } + LinkedListElement * prev() { return hasPrev() ? iPrev : NULL; } + LinkedListElement const* prev() const { return hasPrev() ? iPrev : NULL; } + + LinkedListElement * nocheck_next() { return iNext; } + LinkedListElement const* nocheck_next() const { return iNext; } + LinkedListElement * nocheck_prev() { return iPrev; } + LinkedListElement const* nocheck_prev() const { return iPrev; } + + void delink() + { + if(isInList()) + { + iNext->iPrev = iPrev; iPrev->iNext = iNext; iNext = NULL; iPrev = NULL; + } + } + + void insertBefore(LinkedListElement* pElem) + { + pElem->iNext = this; + pElem->iPrev = iPrev; + iPrev->iNext = pElem; + iPrev = pElem; + } + + void insertAfter(LinkedListElement* pElem) + { + pElem->iPrev = this; + pElem->iNext = iNext; + iNext->iPrev = pElem; + iNext = pElem; + } +}; + +//============================================ + +class LinkedListHead +{ + private: + LinkedListElement iFirst; + LinkedListElement iLast; + uint32 iSize; + public: + LinkedListHead() + { + // create empty list + + iFirst.iNext = &iLast; + iLast.iPrev = &iFirst; + iSize = 0; + } + + bool isEmpty() const { return(!iFirst.iNext->isInList()); } + + LinkedListElement * getFirst() { return(isEmpty() ? NULL : iFirst.iNext); } + LinkedListElement const* getFirst() const { return(isEmpty() ? NULL : iFirst.iNext); } + + LinkedListElement * getLast() { return(isEmpty() ? NULL : iLast.iPrev); } + LinkedListElement const* getLast() const { return(isEmpty() ? NULL : iLast.iPrev); } + + void insertFirst(LinkedListElement* pElem) + { + iFirst.insertAfter(pElem); + } + + void insertLast(LinkedListElement* pElem) + { + iLast.insertBefore(pElem); + } + + uint32 getSize() const + { + if(!iSize) + { + uint32 result = 0; + LinkedListElement const* e = getFirst(); + while(e) + { + ++result; + e = e->next(); + } + return result; + } + else + return iSize; + } + + void incSize() { ++iSize; } + void decSize() { --iSize; } + + template + class Iterator + { + public: + typedef std::bidirectional_iterator_tag iterator_category; + typedef _Ty value_type; + typedef ptrdiff_t difference_type; + typedef ptrdiff_t distance_type; + typedef _Ty* pointer; + typedef _Ty const* const_pointer; + typedef _Ty& reference; + typedef _Ty const & const_reference; + + Iterator() : _Ptr(0) + { // construct with null node pointer + } + + Iterator(pointer _Pnode) : _Ptr(_Pnode) + { // construct with node pointer _Pnode + } + + Iterator& operator=(Iterator const &_Right) + { + return (*this) = _Right._Ptr; + } + + Iterator& operator=(const_pointer const &_Right) + { + _Ptr = (pointer)_Right; + return (*this); + } + + reference operator*() + { // return designated value + return *_Ptr; + } + + pointer operator->() + { // return pointer to class object + return _Ptr; + } + + Iterator& operator++() + { // preincrement + _Ptr = _Ptr->next(); + return (*this); + } + + Iterator operator++(int) + { // postincrement + iterator _Tmp = *this; + ++*this; + return (_Tmp); + } + + Iterator& operator--() + { // predecrement + _Ptr = _Ptr->prev(); + return (*this); + } + + Iterator operator--(int) + { // postdecrement + iterator _Tmp = *this; + --*this; + return (_Tmp); + } + + bool operator==(Iterator const &_Right) const + { // test for iterator equality + return (_Ptr == _Right._Ptr); + } + + bool operator!=(Iterator const &_Right) const + { // test for iterator inequality + return (!(*this == _Right)); + } + + bool operator==(pointer const &_Right) const + { // test for pointer equality + return (_Ptr != _Right); + } + + bool operator!=(pointer const &_Right) const + { // test for pointer equality + return (!(*this == _Right)); + } + + bool operator==(const_reference _Right) const + { // test for reference equality + return (_Ptr == &_Right); + } + + bool operator!=(const_reference _Right) const + { // test for reference equality + return (_Ptr != &_Right); + } + + pointer _Mynode() + { // return node pointer + return (_Ptr); + } + + protected: + pointer _Ptr; // pointer to node + }; + + typedef Iterator iterator; +}; + +//============================================ +#endif + diff --git a/src/server/shared/Dynamic/LinkedReference/RefManager.h b/src/server/shared/Dynamic/LinkedReference/RefManager.h new file mode 100644 index 00000000000..7e294b4f5f0 --- /dev/null +++ b/src/server/shared/Dynamic/LinkedReference/RefManager.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef _REFMANAGER_H +#define _REFMANAGER_H +//===================================================== + +#include "Utilities/LinkedList.h" +#include "Utilities/LinkedReference/Reference.h" + +template class RefManager : public LinkedListHead +{ + public: + typedef LinkedListHead::Iterator< Reference > iterator; + RefManager() { } + virtual ~RefManager() { clearReferences(); } + + Reference* getFirst() { return ((Reference*) LinkedListHead::getFirst()); } + Reference const* getFirst() const { return ((Reference const*) LinkedListHead::getFirst()); } + Reference* getLast() { return ((Reference*) LinkedListHead::getLast()); } + Reference const* getLast() const { return ((Reference const*) LinkedListHead::getLast()); } + + iterator begin() { return iterator(getFirst()); } + iterator end() { return iterator(NULL); } + iterator rbegin() { return iterator(getLast()); } + iterator rend() { return iterator(NULL); } + + void clearReferences() + { + LinkedListElement* ref; + while((ref = getFirst()) != NULL) + { + ((Reference*) ref)->invalidate(); + ref->delink(); // the delink might be already done by invalidate(), but doing it here again does not hurt and insures an empty list + } + } +}; + +//===================================================== +#endif + diff --git a/src/server/shared/Dynamic/LinkedReference/Reference.h b/src/server/shared/Dynamic/LinkedReference/Reference.h new file mode 100644 index 00000000000..4a1545f8f12 --- /dev/null +++ b/src/server/shared/Dynamic/LinkedReference/Reference.h @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef _REFERENCE_H +#define _REFERENCE_H + +#include "Utilities/LinkedList.h" + +//===================================================== + +template class Reference : public LinkedListElement +{ + private: + TO* iRefTo; + FROM* iRefFrom; + protected: + // Tell our refTo (target) object that we have a link + virtual void targetObjectBuildLink() = 0; + + // Tell our refTo (taget) object, that the link is cut + virtual void targetObjectDestroyLink() = 0; + + // Tell our refFrom (source) object, that the link is cut (Target destroyed) + virtual void sourceObjectDestroyLink() = 0; + public: + Reference() { iRefTo = NULL; iRefFrom = NULL; } + virtual ~Reference() {} + + // Create new link + void link(TO* toObj, FROM* fromObj) + { + assert(fromObj); // fromObj MUST not be NULL + if(isValid()) + unlink(); + if(toObj != NULL) + { + iRefTo = toObj; + iRefFrom = fromObj; + targetObjectBuildLink(); + } + } + + // We don't need the reference anymore. Call comes from the refFrom object + // Tell our refTo object, that the link is cut + void unlink() { targetObjectDestroyLink(); delink(); iRefTo = NULL; iRefFrom = NULL; } + + // Link is invalid due to destruction of referenced target object. Call comes from the refTo object + // Tell our refFrom object, that the link is cut + void invalidate() // the iRefFrom MUST remain!! + { + sourceObjectDestroyLink(); delink(); iRefTo = NULL; + } + + bool isValid() const // Only check the iRefTo + { + return iRefTo != NULL; + } + + Reference * next() { return((Reference *) LinkedListElement::next()); } + Reference const * next() const { return((Reference const *) LinkedListElement::next()); } + Reference * prev() { return((Reference *) LinkedListElement::prev()); } + Reference const * prev() const { return((Reference const *) LinkedListElement::prev()); } + + Reference * nocheck_next() { return((Reference *) LinkedListElement::nocheck_next()); } + Reference const * nocheck_next() const { return((Reference const *) LinkedListElement::nocheck_next()); } + Reference * nocheck_prev() { return((Reference *) LinkedListElement::nocheck_prev()); } + Reference const * nocheck_prev() const { return((Reference const *) LinkedListElement::nocheck_prev()); } + + TO* operator ->() const { return iRefTo; } + TO* getTarget() const { return iRefTo; } + + FROM* getSource() const { return iRefFrom; } +}; + +//===================================================== +#endif + diff --git a/src/server/shared/Dynamic/ObjectRegistry.h b/src/server/shared/Dynamic/ObjectRegistry.h new file mode 100644 index 00000000000..e6619427885 --- /dev/null +++ b/src/server/shared/Dynamic/ObjectRegistry.h @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_OBJECTREGISTRY_H +#define TRINITY_OBJECTREGISTRY_H + +#include "Platform/Define.h" +#include "Utilities/UnorderedMap.h" +#include "Policies/Singleton.h" + +#include +#include +#include + +/** ObjectRegistry holds all registry item of the same type + */ +template +class ObjectRegistry +{ + public: + typedef std::map RegistryMapType; + + /// Returns a registry item + const T* GetRegistryItem(Key key) const + { + typename RegistryMapType::const_iterator iter = i_registeredObjects.find(key); + return( iter == i_registeredObjects.end() ? NULL : iter->second ); + } + + /// Inserts a registry item + bool InsertItem(T *obj, Key key, bool override = false) + { + typename RegistryMapType::iterator iter = i_registeredObjects.find(key); + if( iter != i_registeredObjects.end() ) + { + if( !override ) + return false; + delete iter->second; + i_registeredObjects.erase(iter); + } + + i_registeredObjects[key] = obj; + return true; + } + + /// Removes a registry item + void RemoveItem(Key key, bool delete_object = true) + { + typename RegistryMapType::iterator iter = i_registeredObjects.find(key); + if( iter != i_registeredObjects.end() ) + { + if( delete_object ) + delete iter->second; + i_registeredObjects.erase(iter); + } + } + + /// Returns true if registry contains an item + bool HasItem(Key key) const + { + return (i_registeredObjects.find(key) != i_registeredObjects.end()); + } + + /// Inefficiently return a vector of registered items + unsigned int GetRegisteredItems(std::vector &l) const + { + unsigned int sz = l.size(); + l.resize(sz + i_registeredObjects.size()); + for (typename RegistryMapType::const_iterator iter = i_registeredObjects.begin(); iter != i_registeredObjects.end(); ++iter) + l[sz++] = iter->first; + return i_registeredObjects.size(); + } + + /// Return the map of registered items + RegistryMapType const &GetRegisteredItems() const + { + return i_registeredObjects; + } + + private: + RegistryMapType i_registeredObjects; + friend class Trinity::OperatorNew >; + + // protected for friend use since it should be a singleton + ObjectRegistry() {} + ~ObjectRegistry() + { + for (typename RegistryMapType::iterator iter=i_registeredObjects.begin(); iter != i_registeredObjects.end(); ++iter) + delete iter->second; + i_registeredObjects.clear(); + } +}; +#endif + diff --git a/src/server/shared/Dynamic/TypeContainer.h b/src/server/shared/Dynamic/TypeContainer.h new file mode 100644 index 00000000000..c2c9b4fcdea --- /dev/null +++ b/src/server/shared/Dynamic/TypeContainer.h @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_TYPECONTAINER_H +#define TRINITY_TYPECONTAINER_H + +/* + * Here, you'll find a series of containers that allow you to hold multiple + * types of object at the same time. + */ + +#include +#include +#include "Platform/Define.h" +#include "Utilities/TypeList.h" +#include "GameSystem/GridRefManager.h" + +/* + * @class ContainerMapList is a mulit-type container for map elements + * By itself its meaningless but collaborate along with TypeContainers, + * it become the most powerfully container in the whole system. + */ +template struct ContainerMapList +{ + //std::map _element; + GridRefManager _element; +}; + +template<> struct ContainerMapList /* nothing is in type null */ +{ +}; +template struct ContainerMapList > +{ + ContainerMapList _elements; + ContainerMapList _TailElements; +}; + +/* + * @class ContaierArrayList is a multi-type container for + * array of elements. + */ +template struct ContainerArrayList +{ + std::vector _element; +}; + +// termination condition +template<> struct ContainerArrayList {}; +// recursion +template struct ContainerArrayList > +{ + ContainerArrayList _elements; + ContainerArrayList _TailElements; +}; + +/* + * @class ContainerList is a simple list of different types of elements + * + */ +template struct ContainerList +{ + OBJECT _element; +}; + +/* TypeNull is underfined */ +template<> struct ContainerList {}; +template struct ContainerList > +{ + ContainerList _elements; + ContainerMapList _TailElements; +}; + +#include "TypeContainerFunctions.h" + +/* + * @class TypeMapContainer contains a fixed number of types and is + * determined at compile time. This is probably the most complicated + * class and do its simplest thing, that is, holds objects + * of different types. + */ + +template +class TypeMapContainer +{ + public: + template size_t Count() const { return Trinity::Count(i_elements, (SPECIFIC_TYPE*)NULL); } + + /// inserts a specific object into the container + template bool insert(SPECIFIC_TYPE *obj) + { + SPECIFIC_TYPE* t = Trinity::Insert(i_elements, obj); + return (t != NULL); + } + + /// Removes the object from the container, and returns the removed object + template bool remove(SPECIFIC_TYPE* obj) + { + SPECIFIC_TYPE* t = Trinity::Remove(i_elements, obj); + return (t != NULL); + } + + ContainerMapList & GetElements(void) { return i_elements; } + const ContainerMapList & GetElements(void) const { return i_elements;} + + private: + ContainerMapList i_elements; +}; +#endif + diff --git a/src/server/shared/Dynamic/TypeContainerFunctions.h b/src/server/shared/Dynamic/TypeContainerFunctions.h new file mode 100644 index 00000000000..edfbb40e659 --- /dev/null +++ b/src/server/shared/Dynamic/TypeContainerFunctions.h @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TYPECONTAINER_FUNCTIONS_H +#define TYPECONTAINER_FUNCTIONS_H + +/* + * Here you'll find a list of helper functions to make + * the TypeContainer usefull. Without it, its hard + * to access or mutate the container. + */ + +#include "Platform/Define.h" +#include "Utilities/TypeList.h" +#include + +namespace Trinity +{ + /* ContainerMapList Helpers */ + // count functions + template size_t Count(const ContainerMapList &elements, SPECIFIC_TYPE* /*fake*/) + { + return elements._element.getSize(); + }; + + template size_t Count(const ContainerMapList &/*elements*/, SPECIFIC_TYPE* /*fake*/) + { + return 0; + } + + template size_t Count(const ContainerMapList &/*elements*/, SPECIFIC_TYPE* /*fake*/) + { + return 0; + } + + template size_t Count(const ContainerMapList >&elements, SPECIFIC_TYPE* fake) + { + return Count(elements._elements,fake); + } + + template size_t Count(const ContainerMapList >&elements, SPECIFIC_TYPE* fake) + { + return Count(elements._TailElements, fake); + } + + // non-const insert functions + template SPECIFIC_TYPE* Insert(ContainerMapList &elements, SPECIFIC_TYPE *obj) + { + //elements._element[hdl] = obj; + obj->GetGridRef().link(&elements._element, obj); + return obj; + }; + + template SPECIFIC_TYPE* Insert(ContainerMapList &/*elements*/, SPECIFIC_TYPE * /*obj*/) + { + return NULL; + } + + // this is a missed + template SPECIFIC_TYPE* Insert(ContainerMapList &/*elements*/, SPECIFIC_TYPE * /*obj*/) + { + return NULL; // a missed + } + + // Recursion + template SPECIFIC_TYPE* Insert(ContainerMapList >&elements, SPECIFIC_TYPE *obj) + { + SPECIFIC_TYPE* t= Insert(elements._elements, obj); + return (t != NULL ? t : Insert(elements._TailElements, obj)); + } + + // non-const remove method + template SPECIFIC_TYPE* Remove(ContainerMapList & /*elements*/, SPECIFIC_TYPE *obj) + { + obj->GetGridRef().unlink(); + return obj; + } + + template SPECIFIC_TYPE* Remove(ContainerMapList &/*elements*/, SPECIFIC_TYPE * /*obj*/) + { + return NULL; + } + + // this is a missed + template SPECIFIC_TYPE* Remove(ContainerMapList &/*elements*/, SPECIFIC_TYPE * /*obj*/) + { + return NULL; // a missed + } + + template SPECIFIC_TYPE* Remove(ContainerMapList > &elements, SPECIFIC_TYPE *obj) + { + // The head element is bad + SPECIFIC_TYPE* t = Remove(elements._elements, obj); + return ( t != NULL ? t : Remove(elements._TailElements, obj) ); + } + +} +#endif + diff --git a/src/server/shared/Dynamic/TypeContainerFunctionsPtr.h b/src/server/shared/Dynamic/TypeContainerFunctionsPtr.h new file mode 100644 index 00000000000..2affcc457cc --- /dev/null +++ b/src/server/shared/Dynamic/TypeContainerFunctionsPtr.h @@ -0,0 +1,170 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TYPECONTAINER_FUNCTIONS_PTR_H +#define TYPECONTAINER_FUNCTIONS_PTR_H + +/* + * Here you'll find a list of helper functions to make + * the TypeContainer usefull. Without it, its hard + * to access or mutate the container. + */ + +#include "Platform/Define.h" +#include "Utilities/TypeList.h" +#include + +namespace Trinity +{ + /* ContainerMapList Helpers */ + // count functions + // template size_t Count(const ContainerMapList &elements, CountedPtr* /*fake*/) + // { + // return elements._element.size(); + // }; + // + // template size_t Count(const ContainerMapList &elements, CountedPtr* /*fake*/) + // { + // return 0; + // } + // + // template size_t Count(const ContainerMapList &elements, CountedPtr* /*fake*/) + // { + // return 0; + // } + // + // template size_t Count(const ContainerMapList >&elements, SPECIFIC_TYPE* fake) + // { + // return Count(elements._elements,fake); + // } + // + // template size_t Count(const ContainerMapList >&elements, SPECIFIC_TYPE* fake) + // { + // return Count(elements._TailElements, fake); + // } + + // non-const find functions + template CountedPtr& Find(ContainerMapList &elements, OBJECT_HANDLE hdl, CountedPtr* /*fake*/) + { + typename std::map >::iterator iter = elements._element.find(hdl); + return (iter == elements._element.end() ? NullPtr((SPECIFIC_TYPE*)NULL) : iter->second); + }; + + template CountedPtr& Find(ContainerMapList &elements, OBJECT_HANDLE hdl, CountedPtr* /*fake*/) + { + return NullPtr((SPECIFIC_TYPE*)NULL);// terminate recursion + } + + template CountedPtr& Find(ContainerMapList &elements, OBJECT_HANDLE hdl, CountedPtr* /*fake*/) + { + return NullPtr((SPECIFIC_TYPE*)NULL);// this is a missed + } + + template CountedPtr& Find(ContainerMapList >&elements, OBJECT_HANDLE hdl, CountedPtr* fake) + { + CountedPtr &t = Find(elements._elements, hdl,fake); + return (!t ? Find(elements._TailElements, hdl,fake) : t); + } + + // const find functions + template const CountedPtr& Find(const ContainerMapList &elements, OBJECT_HANDLE hdl, CountedPtr* /*fake*/) + { + typename CountedPtr::iterator iter = elements._element.find(hdl); + return (iter == elements._element.end() ? NullPtr((SPECIFIC_TYPE*)NULL) : iter->second); + }; + + template const CountedPtr& Find(const ContainerMapList &elements, OBJECT_HANDLE hdl, CountedPtr* /*fake*/) + { + return NullPtr((SPECIFIC_TYPE*)NULL); + } + + template const CountedPtr& Find(const ContainerMapList &elements, OBJECT_HANDLE hdl, CountedPtr* /*fake*/) + { + return NullPtr((SPECIFIC_TYPE*)NULL); + } + + template CountedPtr& Find(const ContainerMapList >&elements, OBJECT_HANDLE hdl, CountedPtr* fake) + { + CountedPtr &t = Find(elements._elements, hdl,fake); + if(!t) + t = Find(elements._TailElement, hdl,fake); + + return t; + } + + // non-const insert functions + template CountedPtr& Insert(ContainerMapList &elements, CountedPtr &obj, OBJECT_HANDLE hdl) + { + elements._element[hdl] = obj; + return obj; + }; + + template CountedPtr& Insert(ContainerMapList &elements, CountedPtr &obj, OBJECT_HANDLE hdl) + { + return NullPtr((SPECIFIC_TYPE*)NULL); + } + + // this is a missed + template CountedPtr& Insert(ContainerMapList &elements, CountedPtr &obj, OBJECT_HANDLE hdl) + { + return NullPtr((SPECIFIC_TYPE*)NULL);// a missed + } + + // Recursion + template CountedPtr& Insert(ContainerMapList >&elements, CountedPtr &obj, OBJECT_HANDLE hdl) + { + CountedPtr &t= Insert(elements._elements, obj, hdl); + return (!t ? Insert(elements._TailElements, obj, hdl) : t); + } + + // non-const remove method + template bool Remove(ContainerMapList &elements, CountedPtr &obj, OBJECT_HANDLE hdl) + { + typename std::map >::iterator iter = elements._element.find(hdl); + if( iter != elements._element.end() ) + { + elements._element.erase(iter); + return true; + } + + return false; // found... terminate the search + } + + template bool Remove(ContainerMapList &elements, CountedPtr &obj, OBJECT_HANDLE hdl) + { + return false; + } + + // this is a missed + template bool Remove(ContainerMapList &elements, CountedPtr &obj, OBJECT_HANDLE hdl) + { + return false; + } + + template bool Remove(ContainerMapList > &elements, CountedPtr &obj, OBJECT_HANDLE hdl) + { + // The head element is bad + bool t = Remove(elements._elements, obj, hdl); + return ( !t ? Remove(elements._TailElements, obj, hdl) : t ); + } + +} +#endif + diff --git a/src/server/shared/Dynamic/TypeContainerVisitor.h b/src/server/shared/Dynamic/TypeContainerVisitor.h new file mode 100644 index 00000000000..f15075e5afd --- /dev/null +++ b/src/server/shared/Dynamic/TypeContainerVisitor.h @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_TYPECONTAINERVISITOR_H +#define TRINITY_TYPECONTAINERVISITOR_H + +/* + * @class TypeContainerVisitor is implemented as a visitor pattern. It is + * a visitor to the TypeContainerList or TypeContainerMapList. The visitor has + * to overload its types as a visit method is called. + */ + +#include "Platform/Define.h" +#include "TypeContainer.h" + +// forward declaration +template class TypeContainerVisitor; + +// visitor helper +template void VisitorHelper(VISITOR &v, TYPE_CONTAINER &c) +{ + v.Visit(c); +}; + +// terminate condition for container list +template void VisitorHelper(VISITOR &v, ContainerList &c) +{ +} + +template void VisitorHelper(VISITOR &v, ContainerList &c) +{ + v.Visit(c._element); +} + +// recursion for container list +template void VisitorHelper(VISITOR &v, ContainerList > &c) +{ + VisitorHelper(v, c._elements); + VisitorHelper(v, c._TailElements); +} + +// terminate condition container map list +template void VisitorHelper(VISITOR &/*v*/, ContainerMapList &/*c*/) +{ +} + +template void VisitorHelper(VISITOR &v, ContainerMapList &c) +{ + v.Visit(c._element); +} + +// recursion container map list +template void VisitorHelper(VISITOR &v, ContainerMapList > &c) +{ + VisitorHelper(v, c._elements); + VisitorHelper(v, c._TailElements); +} + +// array list +template void VisitorHelper(VISITOR &v, ContainerArrayList &c) +{ + v.Visit(c._element); +} + +template void VisitorHelper(VISITOR &/*v*/, ContainerArrayList &/*c*/) +{ +} + +// recursion +template void VisitorHelper(VISITOR &v, ContainerArrayList > &c) +{ + VisitorHelper(v, c._elements); + VisitorHelper(v, c._TailElements); +} + +// for TypeMapContainer +template void VisitorHelper(VISITOR &v, TypeMapContainer &c) +{ + VisitorHelper(v, c.GetElements()); +} + +template +class TypeContainerVisitor +{ + public: + TypeContainerVisitor(VISITOR &v) : i_visitor(v) {} + + void Visit(TYPE_CONTAINER &c) + { + VisitorHelper(i_visitor, c); + } + + void Visit(const TYPE_CONTAINER &c) const + { + VisitorHelper(i_visitor, c); + } + + private: + VISITOR &i_visitor; +}; +#endif + diff --git a/src/server/shared/Dynamic/TypeList.h b/src/server/shared/Dynamic/TypeList.h new file mode 100644 index 00000000000..02bc08083c2 --- /dev/null +++ b/src/server/shared/Dynamic/TypeList.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_TYPELIST_H +#define TRINITY_TYPELIST_H + +/* + @struct TypeList + TypeList is the most simple but yet the most powerfull class of all. It holds + at compile time the different type of objects in a linked list. + */ + +class TypeNull; + +template +struct TypeList +{ + typedef HEAD Head; + typedef TAIL Tail; +}; + +// enough for now.. can be expand at any point in time as needed +#define TYPELIST_1(T1) TypeList +#define TYPELIST_2(T1, T2) TypeList +#define TYPELIST_3(T1, T2, T3) TypeList +#define TYPELIST_4(T1, T2, T3, T4) TypeList +#define TYPELIST_5(T1, T2, T3, T4, T5) TypeList +#endif + diff --git a/src/server/shared/Dynamic/UnorderedMap.h b/src/server/shared/Dynamic/UnorderedMap.h new file mode 100644 index 00000000000..fce5ec82bfc --- /dev/null +++ b/src/server/shared/Dynamic/UnorderedMap.h @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_UNORDERED_MAP_H +#define TRINITY_UNORDERED_MAP_H + +#include "Platform/CompilerDefs.h" +#include "Platform/Define.h" + +#if COMPILER == COMPILER_INTEL +#include +#elif COMPILER == COMPILER_GNU && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3) +#include +#elif COMPILER == COMPILER_GNU && __GNUC__ >= 3 +#include +#elif COMPILER == COMPILER_MICROSOFT && _MSC_VER >= 1500 && _HAS_TR1 // VC9.0 and later +#include +#else +#include +#endif + +#ifdef _STLPORT_VERSION +#define UNORDERED_MAP std::hash_map +using std::hash_map; +#elif COMPILER == COMPILER_MICROSOFT && _MSC_VER >= 1500 && _HAS_TR1 +#define UNORDERED_MAP std::tr1::unordered_map +#elif COMPILER == COMPILER_MICROSOFT && _MSC_VER >= 1300 +#define UNORDERED_MAP stdext::hash_map +using stdext::hash_map; +#elif COMPILER == COMPILER_INTEL +#define UNORDERED_MAP std::hash_map +using std::hash_map; +#elif COMPILER == COMPILER_GNU && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3) +#define UNORDERED_MAP std::tr1::unordered_map +#elif COMPILER == COMPILER_GNU && __GNUC__ >= 3 +#define UNORDERED_MAP __gnu_cxx::hash_map + +namespace __gnu_cxx +{ + template<> struct hash + { + size_t operator()(const unsigned long long &__x) const { return (size_t)__x; } + }; + template struct hash + { + size_t operator()(T * const &__x) const { return (size_t)__x; } + }; + +}; + +#else +#define UNORDERED_MAP std::hash_map +using std::hash_map; +#endif +#endif + diff --git a/src/server/shared/Policies/CreationPolicy.h b/src/server/shared/Policies/CreationPolicy.h new file mode 100644 index 00000000000..8552ce7da52 --- /dev/null +++ b/src/server/shared/Policies/CreationPolicy.h @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_CREATIONPOLICY_H +#define TRINITY_CREATIONPOLICY_H + +#include +#include "Platform/Define.h" + +namespace Trinity +{ + /** + * OperatorNew policy creates an object on the heap using new. + */ + template + class OperatorNew + { + public: + static T* Create(void) { return (new T); } + static void Destroy(T *obj) { delete obj; } + }; + + /** + * LocalStaticCreation policy creates an object on the stack + * the first time call Create. + */ + template + class LocalStaticCreation + { + union MaxAlign + { + char t_[sizeof(T)]; + short int shortInt_; + int int_; + long int longInt_; + float float_; + double double_; + long double longDouble_; + struct Test; + int Test::* pMember_; + int (Test::*pMemberFn_)(int); + }; + public: + static T* Create(void) + { + static MaxAlign si_localStatic; + return new(&si_localStatic) T; + } + + static void Destroy(T *obj) { obj->~T(); } + }; + + /** + * CreateUsingMalloc by pass the memory manger. + */ + template + class CreateUsingMalloc + { + public: + static T* Create() + { + void* p = ::malloc(sizeof(T)); + if (!p) return 0; + return new(p) T; + } + + static void Destroy(T* p) + { + p->~T(); + ::free(p); + } + }; + + /** + * CreateOnCallBack creates the object base on the call back. + */ + template + class CreateOnCallBack + { + public: + static T* Create() + { + return CALL_BACK::createCallBack(); + } + + static void Destroy(T *p) + { + CALL_BACK::destroyCallBack(p); + } + }; +} +#endif + diff --git a/src/server/shared/Policies/ObjectLifeTime.cpp b/src/server/shared/Policies/ObjectLifeTime.cpp new file mode 100644 index 00000000000..fd16873ae92 --- /dev/null +++ b/src/server/shared/Policies/ObjectLifeTime.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#include +#include "ObjectLifeTime.h" + +namespace Trinity +{ + extern "C" void external_wrapper(void *p) + { + std::atexit( (void (*)())p ); + } + + void at_exit( void (*func)() ) + { + external_wrapper((void*)func); + } +} + diff --git a/src/server/shared/Policies/ObjectLifeTime.h b/src/server/shared/Policies/ObjectLifeTime.h new file mode 100644 index 00000000000..61b90b59f6e --- /dev/null +++ b/src/server/shared/Policies/ObjectLifeTime.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_OBJECTLIFETIME_H +#define TRINITY_OBJECTLIFETIME_H + +#include +#include "Platform/Define.h" + +typedef void (* Destroyer)(void); + +namespace Trinity +{ + void at_exit( void (*func)() ); + + template + class ObjectLifeTime + { + public: + static void ScheduleCall(void (*destroyer)() ) + { + at_exit( destroyer ); + } + + DECLSPEC_NORETURN static void OnDeadReference(void) ATTR_NORETURN; + + }; + + template + void ObjectLifeTime::OnDeadReference(void) // We don't handle Dead Reference for now + { + throw std::runtime_error("Dead Reference"); + } +} +#endif + diff --git a/src/server/shared/Policies/Singleton.h b/src/server/shared/Policies/Singleton.h new file mode 100644 index 00000000000..da898558ca5 --- /dev/null +++ b/src/server/shared/Policies/Singleton.h @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_SINGLETON_H +#define TRINITY_SINGLETON_H + +/** + * @brief class Singleton + */ + +#include "CreationPolicy.h" +#include "ThreadingModel.h" +#include "ObjectLifeTime.h" + +namespace Trinity +{ + template + < + typename T, + class ThreadingModel = Trinity::SingleThreaded, + class CreatePolicy = Trinity::OperatorNew, + class LifeTimePolicy = Trinity::ObjectLifeTime + > + class Singleton + { + public: + static T& Instance(); + + protected: + Singleton() {}; + + private: + + // Prohibited actions...this does not prevent hijacking. + Singleton(const Singleton &); + Singleton& operator=(const Singleton &); + + // Singleton Helpers + static void DestroySingleton(); + + // data structure + typedef typename ThreadingModel::Lock Guard; + static T *si_instance; + static bool si_destroyed; + }; +} +#endif + diff --git a/src/server/shared/Policies/SingletonImp.h b/src/server/shared/Policies/SingletonImp.h new file mode 100644 index 00000000000..3e985cd5c64 --- /dev/null +++ b/src/server/shared/Policies/SingletonImp.h @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_SINGLETONIMPL_H +#define TRINITY_SINGLETONIMPL_H + +#include "Singleton.h" + +// avoid the using namespace here cuz +// its a .h file afterall + +template +< +typename T, +class ThreadingModel, +class CreatePolicy, +class LifeTimePolicy +> +T& +Trinity::Singleton::Instance() +{ + if( !si_instance ) + { + // double-checked Locking pattern + Guard(); + if( !si_instance ) + { + if( si_destroyed ) + { + si_destroyed = false; + LifeTimePolicy::OnDeadReference(); + } + si_instance = CreatePolicy::Create(); + LifeTimePolicy::ScheduleCall(&DestroySingleton); + } + } + + return *si_instance; +} + +template +< +typename T, +class ThreadingModel, +class CreatePolicy, +class LifeTimePolicy +> +void +Trinity::Singleton::DestroySingleton() +{ + CreatePolicy::Destroy(si_instance); + si_instance = NULL; + si_destroyed = true; +} + +#define INSTANTIATE_SINGLETON_1(TYPE) \ + template class Trinity::Singleton, Trinity::OperatorNew, Trinity::ObjectLifeTime >; \ + template<> TYPE* Trinity::Singleton, Trinity::OperatorNew, Trinity::ObjectLifeTime >::si_instance = 0; \ + template<> bool Trinity::Singleton, Trinity::OperatorNew, Trinity::ObjectLifeTime >::si_destroyed = false + +#define INSTANTIATE_SINGLETON_2(TYPE, THREADINGMODEL) \ + template class Trinity::Singleton, Trinity::ObjectLifeTime >; \ + template<> TYPE* Trinity::Singleton, Trinity::ObjectLifeTime >::si_instance = 0; \ + template<> bool Trinity::Singleton, Trinity::ObjectLifeTime >::si_destroyed = false + +#define INSTANTIATE_SINGLETON_3(TYPE, THREADINGMODEL, CREATIONPOLICY ) \ + template class Trinity::Singleton >; \ + template<> TYPE* Trinity::Singleton >::si_instance = 0; \ + template<> bool Trinity::Singleton >::si_destroyed = false + +#define INSTANTIATE_SINGLETON_4(TYPE, THREADINGMODEL, CREATIONPOLICY, OBJECTLIFETIME) \ + template class Trinity::Singleton; \ + template<> TYPE* Trinity::Singleton::si_instance = 0; \ + template<> bool Trinity::Singleton::si_destroyed = false +#endif + diff --git a/src/server/shared/Policies/ThreadingModel.h b/src/server/shared/Policies/ThreadingModel.h new file mode 100644 index 00000000000..d4c5e9a2333 --- /dev/null +++ b/src/server/shared/Policies/ThreadingModel.h @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_THREADINGMODEL_H +#define TRINITY_THREADINGMODEL_H + +/** + * @class ThreadingModel + * + */ + +#include "Platform/Define.h" + +namespace Trinity +{ + inline void Guard(void *) {} + + template class GeneralLock + { + public: + GeneralLock(MUTEX &m) : i_mutex(m) + { + i_mutex.acquire(); + } + + ~GeneralLock() + { + i_mutex.release(); + } + private: + GeneralLock(const GeneralLock &); + GeneralLock& operator=(const GeneralLock &); + MUTEX &i_mutex; + }; + + template + class SingleThreaded + { + public: + + struct Lock // empty object + { + Lock() {} + Lock(const T &) {} + Lock(const SingleThreaded &) // for single threaded we ignore this + { + } + }; + + typedef T VolatileType; + }; + + // object level lockable + template + class ObjectLevelLockable + { + public: + ObjectLevelLockable() : i_mtx() {} + + friend class Lock; + + class Lock + { + public: + Lock(ObjectLevelLockable &host) : i_lock(host.i_mtx) + { + } + + private: + GeneralLock i_lock; + }; + + typedef volatile T VolatileType; + + private: + // prevent the compiler creating a copy construct + ObjectLevelLockable(const ObjectLevelLockable &); + ObjectLevelLockable& operator=(const ObjectLevelLockable &); + + MUTEX i_mtx; + }; + + template + class ClassLevelLockable + { + public: + class Lock; + friend class Lock; + typedef volatile T VolatileType; + + ClassLevelLockable() {} + + class Lock + { + public: + Lock(T& /*host*/) { ClassLevelLockable::si_mtx.acquire(); } + Lock(ClassLevelLockable &) { ClassLevelLockable::si_mtx.acquire(); } + Lock() { ClassLevelLockable::si_mtx.acquire(); } + ~Lock() { ClassLevelLockable::si_mtx.release(); } + }; + + private: + static MUTEX si_mtx; + }; + +} + +template MUTEX Trinity::ClassLevelLockable::si_mtx; + +#define INSTANTIATE_CLASS_MUTEX(CTYPE,MUTEX) \ + template class Trinity::ClassLevelLockable +#endif + diff --git a/src/server/shared/Threading/Callback.h b/src/server/shared/Threading/Callback.h new file mode 100644 index 00000000000..d2e2c36851a --- /dev/null +++ b/src/server/shared/Threading/Callback.h @@ -0,0 +1,386 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_CALLBACK_H +#define TRINITY_CALLBACK_H + +/// ------------ BASE CLASSES ------------ + +namespace Trinity +{ + template < class Class, typename ParamType1 = void, typename ParamType2 = void, typename ParamType3 = void, typename ParamType4 = void > + class _Callback + { + protected: + typedef void (Class::*Method)(ParamType1, ParamType2, ParamType3, ParamType4); + Class *m_object; + Method m_method; + ParamType1 m_param1; + ParamType2 m_param2; + ParamType3 m_param3; + ParamType4 m_param4; + void _Execute() { (m_object->*m_method)(m_param1, m_param2, m_param3, m_param4); } + public: + _Callback(Class *object, Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3, ParamType4 param4) + : m_object(object), m_method(method), m_param1(param1), m_param2(param2), m_param3(param3), m_param4(param4) {} + _Callback(_Callback < Class, ParamType1, ParamType2, ParamType3, ParamType4> const& cb) + : m_object(cb.object), m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2), m_param3(cb.m_param3), m_param4(cb.m_param4) {} + }; + + template < class Class, typename ParamType1, typename ParamType2, typename ParamType3 > + class _Callback < Class, ParamType1, ParamType2, ParamType3 > + { + protected: + typedef void (Class::*Method)(ParamType1, ParamType2, ParamType3); + Class *m_object; + Method m_method; + ParamType1 m_param1; + ParamType2 m_param2; + ParamType3 m_param3; + void _Execute() { (m_object->*m_method)(m_param1, m_param2, m_param3); } + public: + _Callback(Class *object, Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3) + : m_object(object), m_method(method), m_param1(param1), m_param2(param2) {} + _Callback(_Callback < Class, ParamType1, ParamType2, ParamType3 > const& cb) + : m_object(cb.object), m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2), m_param3(cb.m_param3) {} + }; + + template < class Class, typename ParamType1, typename ParamType2 > + class _Callback < Class, ParamType1, ParamType2 > + { + protected: + typedef void (Class::*Method)(ParamType1, ParamType2); + Class *m_object; + Method m_method; + ParamType1 m_param1; + ParamType2 m_param2; + void _Execute() { (m_object->*m_method)(m_param1, m_param2); } + public: + _Callback(Class *object, Method method, ParamType1 param1, ParamType2 param2) + : m_object(object), m_method(method), m_param1(param1), m_param2(param2) {} + _Callback(_Callback < Class, ParamType1, ParamType2 > const& cb) + : m_object(cb.m_object), m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2) {} + }; + + template < class Class, typename ParamType1 > + class _Callback < Class, ParamType1 > + { + protected: + typedef void (Class::*Method)(ParamType1); + Class *m_object; + Method m_method; + ParamType1 m_param1; + void _Execute() { (m_object->*m_method)(m_param1); } + public: + _Callback(Class *object, Method method, ParamType1 param1) + : m_object(object), m_method(method), m_param1(param1) {} + _Callback(_Callback < Class, ParamType1 > const& cb) + : m_object(cb.m_object), m_method(cb.m_method), m_param1(cb.m_param1) {} + }; + + template < class Class > + class _Callback < Class > + { + protected: + typedef void (Class::*Method)(); + Class *m_object; + Method m_method; + void _Execute() { (m_object->*m_method)(); } + public: + _Callback(Class *object, Method method) + : m_object(object), m_method(method) {} + _Callback(_Callback < Class > const& cb) + : m_object(cb.m_object), m_method(cb.m_method) {} + }; + + /// ---- Statics ---- + + template < typename ParamType1 = void, typename ParamType2 = void, typename ParamType3 = void, typename ParamType4 = void > + class _SCallback + { + protected: + typedef void (*Method)(ParamType1, ParamType2, ParamType3, ParamType4); + Method m_method; + ParamType1 m_param1; + ParamType2 m_param2; + ParamType3 m_param3; + ParamType4 m_param4; + void _Execute() { (*m_method)(m_param1, m_param2, m_param3, m_param4); } + public: + _SCallback(Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3, ParamType4 param4) + : m_method(method), m_param1(param1), m_param2(param2), m_param3(param3), m_param4(param4) {} + _SCallback(_SCallback < ParamType1, ParamType2, ParamType3, ParamType4> const& cb) + : m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2), m_param3(cb.m_param3), m_param4(cb.m_param4) {} + }; + + template < typename ParamType1, typename ParamType2, typename ParamType3 > + class _SCallback < ParamType1, ParamType2, ParamType3 > + { + protected: + typedef void (*Method)(ParamType1, ParamType2, ParamType3); + Method m_method; + ParamType1 m_param1; + ParamType2 m_param2; + ParamType3 m_param3; + void _Execute() { (*m_method)(m_param1, m_param2, m_param3); } + public: + _SCallback(Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3) + : m_method(method), m_param1(param1), m_param2(param2), m_param3(param3) {} + _SCallback(_SCallback < ParamType1, ParamType2, ParamType3 > const& cb) + : m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2), m_param3(cb.m_param3) {} + }; + + template < typename ParamType1, typename ParamType2 > + class _SCallback < ParamType1, ParamType2 > + { + protected: + typedef void (*Method)(ParamType1, ParamType2); + Method m_method; + ParamType1 m_param1; + ParamType2 m_param2; + void _Execute() { (*m_method)(m_param1, m_param2); } + public: + _SCallback(Method method, ParamType1 param1, ParamType2 param2) + : m_method(method), m_param1(param1), m_param2(param2) {} + _SCallback(_SCallback < ParamType1, ParamType2 > const& cb) + : m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2) {} + }; + + template < typename ParamType1 > + class _SCallback < ParamType1 > + { + protected: + typedef void (*Method)(ParamType1); + Method m_method; + ParamType1 m_param1; + void _Execute() { (*m_method)(m_param1); } + public: + _SCallback(Method method, ParamType1 param1) + : m_method(method), m_param1(param1) {} + _SCallback(_SCallback < ParamType1 > const& cb) + : m_method(cb.m_method), m_param1(cb.m_param1) {} + }; + + template < > + class _SCallback < > + { + protected: + typedef void (*Method)(); + Method m_method; + void _Execute() { (*m_method)(); } + public: + _SCallback(Method method) + : m_method(method) {} + _SCallback(_SCallback <> const& cb) + : m_method(cb.m_method) {} + }; +} + +/// --------- GENERIC CALLBACKS ---------- + +namespace Trinity +{ + class ICallback + { + public: + virtual void Execute() = 0; + virtual ~ICallback() {} + }; + + template < class CB > + class _ICallback : public CB, public ICallback + { + public: + _ICallback(CB const& cb) : CB(cb) {} + void Execute() { CB::_Execute(); } + }; + + template < class Class, typename ParamType1 = void, typename ParamType2 = void, typename ParamType3 = void, typename ParamType4 = void > + class Callback : + public _ICallback< _Callback < Class, ParamType1, ParamType2, ParamType3, ParamType4 > > + { + private: + typedef _Callback < Class, ParamType1, ParamType2, ParamType3, ParamType4 > C4; + public: + Callback(Class *object, typename C4::Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3, ParamType4 param4) + : _ICallback< C4 >(C4(object, method, param1, param2, param3, param4)) {} + }; + + template < class Class, typename ParamType1, typename ParamType2, typename ParamType3 > + class Callback < Class, ParamType1, ParamType2, ParamType3 > : + public _ICallback< _Callback < Class, ParamType1, ParamType2, ParamType3 > > + { + private: + typedef _Callback < Class, ParamType1, ParamType2, ParamType3 > C3; + public: + Callback(Class *object, typename C3::Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3) + : _ICallback< C3 >(C3(object, method, param1, param2, param3)) {} + }; + + template < class Class, typename ParamType1, typename ParamType2 > + class Callback < Class, ParamType1, ParamType2 > : + public _ICallback< _Callback < Class, ParamType1, ParamType2 > > + { + private: + typedef _Callback < Class, ParamType1, ParamType2 > C2; + public: + Callback(Class *object, typename C2::Method method, ParamType1 param1, ParamType2 param2) + : _ICallback< C2 >(C2(object, method, param1, param2)) {} + }; + + template < class Class, typename ParamType1 > + class Callback < Class, ParamType1 > : + public _ICallback< _Callback < Class, ParamType1 > > + { + private: + typedef _Callback < Class, ParamType1 > C1; + public: + Callback(Class *object, typename C1::Method method, ParamType1 param1) + : _ICallback< C1 >(C1(object, method, param1)) {} + }; + + template < class Class > + class Callback < Class > : public _ICallback< _Callback < Class > > + { + private: + typedef _Callback < Class > C0; + public: + Callback(Class *object, typename C0::Method method) + : _ICallback< C0 >(C0(object, method)) {} + }; +} + +/// ---------- QUERY CALLBACKS ----------- + +#include "QueryResult.h" +class QueryResult; + +namespace Trinity +{ + class IQueryCallback + { + public: + virtual void Execute() = 0; + virtual ~IQueryCallback() {} + virtual void SetResult(QueryResult_AutoPtr result) = 0; + virtual QueryResult_AutoPtr GetResult() = 0; + }; + + template < class CB > + class _IQueryCallback : public CB, public IQueryCallback + { + public: + _IQueryCallback(CB const& cb) : CB(cb) {} + void Execute() { CB::_Execute(); } + void SetResult(QueryResult_AutoPtr result) { CB::m_param1 = result; } + QueryResult_AutoPtr GetResult() { return CB::m_param1; } + }; + + template < class Class, typename ParamType1 = void, typename ParamType2 = void, typename ParamType3 = void > + class QueryCallback : + public _IQueryCallback< _Callback < Class, QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3 > > + { + private: + typedef _Callback < Class, QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3 > QC3; + public: + QueryCallback(Class *object, typename QC3::Method method, QueryResult_AutoPtr result, ParamType1 param1, ParamType2 param2, ParamType3 param3) + : _IQueryCallback< QC3 >(QC3(object, method, result, param1, param2, param3)) {} + }; + + template < class Class, typename ParamType1, typename ParamType2 > + class QueryCallback < Class, ParamType1, ParamType2 > : + public _IQueryCallback< _Callback < Class, QueryResult_AutoPtr, ParamType1, ParamType2 > > + { + private: + typedef _Callback < Class, QueryResult_AutoPtr, ParamType1, ParamType2 > QC2; + public: + QueryCallback(Class *object, typename QC2::Method method, QueryResult_AutoPtr result, ParamType1 param1, ParamType2 param2) + : _IQueryCallback< QC2 >(QC2(object, method, result, param1, param2)) {} + }; + + template < class Class, typename ParamType1 > + class QueryCallback < Class, ParamType1 > : + public _IQueryCallback< _Callback < Class, QueryResult_AutoPtr, ParamType1 > > + { + private: + typedef _Callback < Class, QueryResult_AutoPtr, ParamType1 > QC1; + public: + QueryCallback(Class *object, typename QC1::Method method, QueryResult_AutoPtr result, ParamType1 param1) + : _IQueryCallback< QC1 >(QC1(object, method, result, param1)) {} + }; + + template < class Class > + class QueryCallback < Class > : public _IQueryCallback< _Callback < Class, QueryResult_AutoPtr > > + { + private: + typedef _Callback < Class, QueryResult_AutoPtr > QC0; + public: + QueryCallback(Class *object, typename QC0::Method method, QueryResult_AutoPtr result) + : _IQueryCallback< QC0 >(QC0(object, method, result)) {} + }; + + /// ---- Statics ---- + + template < typename ParamType1 = void, typename ParamType2 = void, typename ParamType3 = void > + class SQueryCallback : + public _IQueryCallback< _SCallback < QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3 > > + { + private: + typedef _SCallback < QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3 > QC3; + public: + SQueryCallback(typename QC3::Method method, QueryResult_AutoPtr result, ParamType1 param1, ParamType2 param2, ParamType3 param3) + : _IQueryCallback< QC3 >(QC3(method, result, param1, param2, param3)) {} + }; + + template < typename ParamType1, typename ParamType2 > + class SQueryCallback < ParamType1, ParamType2 > : + public _IQueryCallback< _SCallback < QueryResult_AutoPtr, ParamType1, ParamType2 > > + { + private: + typedef _SCallback < QueryResult_AutoPtr, ParamType1, ParamType2 > QC2; + public: + SQueryCallback(typename QC2::Method method, QueryResult_AutoPtr result, ParamType1 param1, ParamType2 param2) + : _IQueryCallback< QC2 >(QC2(method, result, param1, param2)) {} + }; + + template < typename ParamType1 > + class SQueryCallback < ParamType1 > : + public _IQueryCallback< _SCallback < QueryResult_AutoPtr, ParamType1 > > + { + private: + typedef _SCallback < QueryResult_AutoPtr, ParamType1 > QC1; + public: + SQueryCallback(typename QC1::Method method, QueryResult_AutoPtr result, ParamType1 param1) + : _IQueryCallback< QC1 >(QC1(method, result, param1)) {} + }; + + template < > + class SQueryCallback < > : public _IQueryCallback< _SCallback < QueryResult_AutoPtr > > + { + private: + typedef _SCallback < QueryResult_AutoPtr > QC0; + public: + SQueryCallback(QC0::Method method, QueryResult_AutoPtr result) + : _IQueryCallback< QC0 >(QC0(method, result)) {} + }; +} + +#endif + diff --git a/src/server/shared/Utilities/ByteConverter.h b/src/server/shared/Utilities/ByteConverter.h new file mode 100644 index 00000000000..f8b6bd72498 --- /dev/null +++ b/src/server/shared/Utilities/ByteConverter.h @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_BYTECONVERTER_H +#define TRINITY_BYTECONVERTER_H + +/** ByteConverter reverse your byte order. This is use + for cross platform where they have different endians. + */ + +#include +#include + +namespace ByteConverter +{ + template + inline void convert(char *val) + { + std::swap(*val, *(val + T - 1)); + convert(val + 1); + } + + template<> inline void convert<0>(char *) {} + template<> inline void convert<1>(char *) {} // ignore central byte + + template inline void apply(T *val) + { + convert((char *)(val)); + } +} + +#if TRINITY_ENDIAN == TRINITY_BIGENDIAN +template inline void EndianConvert(T& val) { ByteConverter::apply(&val); } +template inline void EndianConvertReverse(T&) { } +#else +template inline void EndianConvert(T&) { } +template inline void EndianConvertReverse(T& val) { ByteConverter::apply(&val); } +#endif + +template void EndianConvert(T*); // will generate link error +template void EndianConvertReverse(T*); // will generate link error + +inline void EndianConvert(uint8&) { } +inline void EndianConvert( int8&) { } +inline void EndianConvertReverse(uint8&) { } +inline void EndianConvertReverse( int8&) { } + +#endif + diff --git a/src/server/shared/Utilities/EventProcessor.cpp b/src/server/shared/Utilities/EventProcessor.cpp new file mode 100644 index 00000000000..c695b43313a --- /dev/null +++ b/src/server/shared/Utilities/EventProcessor.cpp @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#include "EventProcessor.h" + +EventProcessor::EventProcessor() +{ + m_time = 0; + m_aborting = false; +} + +EventProcessor::~EventProcessor() +{ + KillAllEvents(true); +} + +void EventProcessor::Update(uint32 p_time) +{ + // update time + m_time += p_time; + + // main event loop + EventList::iterator i; + while (((i = m_events.begin()) != m_events.end()) && i->first <= m_time) + { + // get and remove event from queue + BasicEvent* Event = i->second; + m_events.erase(i); + + if (!Event->to_Abort) + { + if (Event->Execute(m_time, p_time)) + { + // completely destroy event if it is not re-added + delete Event; + } + } + else + { + Event->Abort(m_time); + delete Event; + } + } +} + +void EventProcessor::KillAllEvents(bool force) +{ + // prevent event insertions + m_aborting = true; + + // first, abort all existing events + for (EventList::iterator i = m_events.begin(); i != m_events.end();) + { + EventList::iterator i_old = i; + ++i; + + i_old->second->to_Abort = true; + i_old->second->Abort(m_time); + if(force || i_old->second->IsDeletable()) + { + delete i_old->second; + + if(!force) // need per-element cleanup + m_events.erase (i_old); + } + } + + // fast clear event list (in force case) + if(force) + m_events.clear(); +} + +void EventProcessor::AddEvent(BasicEvent* Event, uint64 e_time, bool set_addtime) +{ + if (set_addtime) Event->m_addTime = m_time; + Event->m_execTime = e_time; + m_events.insert(std::pair(e_time, Event)); +} + +uint64 EventProcessor::CalculateTime(uint64 t_offset) +{ + return(m_time + t_offset); +} + diff --git a/src/server/shared/Utilities/EventProcessor.h b/src/server/shared/Utilities/EventProcessor.h new file mode 100644 index 00000000000..2712967e1b7 --- /dev/null +++ b/src/server/shared/Utilities/EventProcessor.h @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef __EVENTPROCESSOR_H +#define __EVENTPROCESSOR_H + +#include "Platform/Define.h" + +#include + +// Note. All times are in milliseconds here. + +class BasicEvent +{ + public: + BasicEvent() { to_Abort = false; } + virtual ~BasicEvent() // override destructor to perform some actions on event removal + { + }; + + // this method executes when the event is triggered + // return false if event does not want to be deleted + // e_time is execution time, p_time is update interval + virtual bool Execute(uint64 /*e_time*/, uint32 /*p_time*/) { return true; } + + virtual bool IsDeletable() const { return true; } // this event can be safely deleted + + virtual void Abort(uint64 /*e_time*/) {} // this method executes when the event is aborted + + bool to_Abort; // set by externals when the event is aborted, aborted events don't execute + // and get Abort call when deleted + + // these can be used for time offset control + uint64 m_addTime; // time when the event was added to queue, filled by event handler + uint64 m_execTime; // planned time of next execution, filled by event handler +}; + +typedef std::multimap EventList; + +class EventProcessor +{ + public: + EventProcessor(); + ~EventProcessor(); + + void Update(uint32 p_time); + void KillAllEvents(bool force); + void AddEvent(BasicEvent* Event, uint64 e_time, bool set_addtime = true); + uint64 CalculateTime(uint64 t_offset); + protected: + uint64 m_time; + EventList m_events; + bool m_aborting; +}; +#endif + diff --git a/src/server/worldserver/RemoteAccess/SocketDefines.h b/src/server/worldserver/RemoteAccess/SocketDefines.h new file mode 100644 index 00000000000..49366097ae6 --- /dev/null +++ b/src/server/worldserver/RemoteAccess/SocketDefines.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + * 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 + */ + +#ifndef TRINITY_SOCKETDEFINES_H +#define TRINITY_SOCKETDEFINES_H + +#ifdef WIN32 + +/* Windows socket definitions + */ +#define FD_SETSIZE 1024 +#include +#include + +typedef SOCKET SocketHandle; +typedef fd_set SelectSet; + +#else + +/* The unix socket definitions + */ +#include +#include +#ifdef __APPLE_CC__ +#include +#endif + +typedef int SocketHandle; +typedef fd_set SelectSet; +#endif +#endif + -- cgit v1.2.3