| [4744] | 1 | /* | 
|---|
| [3655] | 2 |    orxonox - the future of 3D-vertical-scrollers | 
|---|
 | 3 |  | 
|---|
 | 4 |    Copyright (C) 2004 orx | 
|---|
 | 5 |  | 
|---|
 | 6 |    This program is free software; you can redistribute it and/or modify | 
|---|
 | 7 |    it under the terms of the GNU General Public License as published by | 
|---|
 | 8 |    the Free Software Foundation; either version 2, or (at your option) | 
|---|
 | 9 |    any later version. | 
|---|
 | 10 |  | 
|---|
 | 11 |    ### File Specific: | 
|---|
| [6142] | 12 |    main-programmer: Benjamin Grauer | 
|---|
| [3655] | 13 |    co-programmer: ... | 
|---|
 | 14 | */ | 
|---|
 | 15 |  | 
|---|
 | 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY | 
|---|
 | 17 |  | 
|---|
| [5629] | 18 | #include "object_manager.h" | 
|---|
| [3655] | 19 |  | 
|---|
| [5795] | 20 | #include "world_entity.h" | 
|---|
 | 21 |  | 
|---|
| [6142] | 22 | #include "shell_command.h" | 
|---|
 | 23 |  | 
|---|
| [8362] | 24 | #include <cassert> | 
|---|
 | 25 | #include "debug.h" | 
|---|
| [6142] | 26 |  | 
|---|
| [9406] | 27 |  | 
|---|
| [6142] | 28 | SHELL_COMMAND(debug, ObjectManager, debug) | 
|---|
| [7198] | 29 |     ->defaultValues("", 0); | 
|---|
| [3655] | 30 |  | 
|---|
| [9869] | 31 | ObjectListDefinition(ObjectManager); | 
|---|
| [3655] | 32 | /** | 
|---|
| [6142] | 33 |  * @brief standard constructor | 
|---|
| [4838] | 34 |  */ | 
|---|
| [5629] | 35 | ObjectManager::ObjectManager () | 
|---|
| [3655] | 36 | { | 
|---|
| [9869] | 37 |   this->registerObject(this, ObjectManager::_objectList); | 
|---|
| [5629] | 38 |    this->setName("ObjectManager"); | 
|---|
| [5795] | 39 |  | 
|---|
 | 40 |    pNodeList = NULL; | 
|---|
| [3655] | 41 | } | 
|---|
 | 42 |  | 
|---|
| [6142] | 43 |  | 
|---|
| [3655] | 44 | /** | 
|---|
| [6142] | 45 |  * @brief standard deconstructor | 
|---|
 | 46 |  * | 
|---|
 | 47 |  * this also removes ALL entitites in existence. | 
|---|
| [4838] | 48 |  */ | 
|---|
| [6142] | 49 | ObjectManager::~ObjectManager () | 
|---|
 | 50 | { | 
|---|
 | 51 |   this->flush(); | 
|---|
 | 52 | } | 
|---|
| [3655] | 53 |  | 
|---|
 | 54 | /** | 
|---|
| [6142] | 55 |  * @brief flushes all entities | 
|---|
 | 56 |  * | 
|---|
 | 57 |  * this function deletes all entities that exist within the ObjectManager. | 
|---|
 | 58 |  * It does this by poping each list from the front, and delete the given object. | 
|---|
 | 59 |  * | 
|---|
 | 60 |  * automatically called by a destructor. | 
|---|
| [4838] | 61 |  */ | 
|---|
| [6142] | 62 | void ObjectManager::flush() | 
|---|
| [3655] | 63 | { | 
|---|
| [6142] | 64 |   for (unsigned int i = 0; i < OM_SIZE; ++i) | 
|---|
| [9869] | 65 |     while(!this->entityLists[i].empty()) | 
|---|
 | 66 |       delete this->entityLists[i].front(); | 
|---|
| [7785] | 67 |  | 
|---|
| [8037] | 68 |   // delete reflection list | 
|---|
 | 69 |   this->reflectionList.clear(); | 
|---|
| [3655] | 70 | } | 
|---|
| [5795] | 71 |  | 
|---|
| [6142] | 72 |  | 
|---|
| [5795] | 73 | /** | 
|---|
| [6142] | 74 |  * @brief moves an Entity from an old list to a new One | 
|---|
 | 75 |  * @param entity the entity to move. | 
|---|
 | 76 |  * @param omList the new List to move the entity to. | 
|---|
 | 77 |  * | 
|---|
 | 78 |  * this will erase the entity from the old list | 
|---|
 | 79 |  */ | 
|---|
 | 80 | void ObjectManager::toList (WorldEntity* entity, OM_LIST omList) | 
|---|
 | 81 | { | 
|---|
 | 82 |   assert (omList != OM_SIZE); | 
|---|
 | 83 |  | 
|---|
 | 84 |   if (likely(entity->getOMListNumber() != OM_INIT)) | 
|---|
| [9869] | 85 |     this->entityLists[entity->getOMListNumber()].erase(entity->getEntityIterator()); | 
|---|
| [6142] | 86 |  | 
|---|
 | 87 |   if (likely(omList != OM_INIT)) | 
|---|
 | 88 |   { | 
|---|
| [9869] | 89 |     this->entityLists[omList].push_back(entity); | 
|---|
 | 90 |     entity->getEntityIterator() = --this->entityLists[omList].end(); | 
|---|
| [6142] | 91 |     entity->getOMListNumber() = omList; | 
|---|
 | 92 |   } | 
|---|
 | 93 | } | 
|---|
 | 94 |  | 
|---|
 | 95 |  | 
|---|
 | 96 | /** | 
|---|
 | 97 |  * @see ObjectManager::toList(WorldEntity* OM_LIST) | 
|---|
 | 98 |  * @param entity the entity to move. | 
|---|
 | 99 |  * @param omList the new List to move the entity to. | 
|---|
 | 100 |  * | 
|---|
 | 101 |  * this function also does a transformation from omList as char* to OM_LIST. | 
|---|
 | 102 |  */ | 
|---|
| [7221] | 103 | void ObjectManager::toList (WorldEntity* entity, const std::string& omList) | 
|---|
| [6142] | 104 | { | 
|---|
 | 105 |   this->toList(entity, ObjectManager::StringToOMList(omList)); | 
|---|
 | 106 | } | 
|---|
 | 107 |  | 
|---|
 | 108 |  | 
|---|
 | 109 |  | 
|---|
 | 110 | /** | 
|---|
| [7368] | 111 |  * @returns a new List with a list of WorldEntities of distance Radius from center | 
|---|
| [5795] | 112 |  */ | 
|---|
| [9869] | 113 |   /* | 
|---|
 | 114 | void ObjectManager::distanceFromObject(EntityList& entities, const PNode& center, float radius, const ClassID& classID) | 
|---|
| [5795] | 115 | { | 
|---|
| [9869] | 116 |   TODO FIXME | 
|---|
 | 117 |  | 
|---|
 | 118 |   const std::list<BaseObject*>* entityList = ClassList::getList(classID); | 
|---|
 | 119 |   if (entityList != NULL) | 
|---|
| [5795] | 120 |   { | 
|---|
 | 121 |  | 
|---|
| [9406] | 122 |     std::list<BaseObject*>::const_iterator node; | 
|---|
| [9869] | 123 |     for (node = entityList->begin(); node != entityList->end(); node++) | 
|---|
| [5795] | 124 |       if ((dynamic_cast<PNode*>(*node)->getAbsCoor() - center.getAbsCoor()).len() < radius) | 
|---|
| [7368] | 125 |         entities.push_back(dynamic_cast<WorldEntity*>(*node)); | 
|---|
| [5795] | 126 |   } | 
|---|
 | 127 | } | 
|---|
| [9869] | 128 |   */ | 
|---|
| [6142] | 129 |  | 
|---|
 | 130 |  | 
|---|
 | 131 | /** | 
|---|
 | 132 |  * @brief print out nice debug information about Elements in the list OM_LIST | 
|---|
 | 133 |  * @param omList the List to debug. | 
|---|
 | 134 |  * @param level: level 0: only show list info; level 1: also show entities and their names. | 
|---|
 | 135 |  */ | 
|---|
 | 136 | void ObjectManager::debug(OM_LIST omList, unsigned int level) const | 
|---|
 | 137 | { | 
|---|
 | 138 |   if (omList != OM_INIT || omList == OM_SIZE) | 
|---|
 | 139 |   { | 
|---|
| [9869] | 140 |     PRINT(0)(" +ObjectManager-LIST: '%s'==size='%d'==---\n", ObjectManager::OMListToString((OM_LIST)omList).c_str(), this->entityLists[omList].size()); | 
|---|
| [6142] | 141 |   //  if (level >= 1) | 
|---|
 | 142 |     { | 
|---|
| [7370] | 143 |       ObjectManager::EntityList::const_iterator entity; | 
|---|
| [9869] | 144 |       for (entity = this->entityLists[omList].begin(); entity != this->entityLists[omList].end(); entity++) | 
|---|
| [6142] | 145 |       { | 
|---|
| [9406] | 146 |         PRINT(0)(" | %s::%s\n",(*entity)->getClassCName(), (*entity)->getCName()); | 
|---|
| [6142] | 147 |       } | 
|---|
 | 148 |     } | 
|---|
 | 149 |   } | 
|---|
 | 150 |   else | 
|---|
 | 151 |     PRINTF(2)("Invalid query. for OM_INIT-LIST or OM_SIZE\n"); | 
|---|
 | 152 | } | 
|---|
 | 153 |  | 
|---|
 | 154 |  | 
|---|
 | 155 | /** | 
|---|
 | 156 |  * @brief prints out very nice debug information | 
|---|
 | 157 |  * @param listName the Name of the list to get Debug information from | 
|---|
 | 158 |  * @param level: level 0: only show list info; level 1: also show entities and their names. | 
|---|
 | 159 |  */ | 
|---|
| [7221] | 160 | void ObjectManager::debug(const std::string& listName, unsigned int level) | 
|---|
| [6142] | 161 | { | 
|---|
 | 162 |   PRINT(0)("=================================\n"); | 
|---|
 | 163 |   PRINT(0)("=ObjectManager-DEBUG=============\n"); | 
|---|
 | 164 |   PRINT(0)("=================================\n"); | 
|---|
| [7221] | 165 |   if (listName.empty()) | 
|---|
| [6142] | 166 |     for (unsigned int i = 0; i < OM_SIZE; ++i) | 
|---|
 | 167 |       debug((OM_LIST) i, level); | 
|---|
 | 168 |   else | 
|---|
 | 169 |     debug(ObjectManager::StringToOMList(listName)); | 
|---|
 | 170 |   PRINT(0)("=========================== OM ==\n"); | 
|---|
 | 171 | } | 
|---|
 | 172 |  | 
|---|
 | 173 |  | 
|---|
 | 174 |  | 
|---|
 | 175 | /** | 
|---|
 | 176 |  * @brief transforms an omList into a String (usefull for debugging). | 
|---|
 | 177 |  * @param omList the OM_LIST to be transformed into a String. | 
|---|
 | 178 |  * @returns the String transformed from omList. | 
|---|
 | 179 |  */ | 
|---|
| [9656] | 180 | const std::string& ObjectManager::OMListToString(OM_LIST omList) | 
|---|
| [6142] | 181 | { | 
|---|
 | 182 |   if (omList == OM_INIT || omList == OM_SIZE) | 
|---|
| [9656] | 183 |     return ObjectManager::objectManagerListNames[OM_NULL]; | 
|---|
| [6142] | 184 |  | 
|---|
 | 185 |   printf("%d\n", omList); | 
|---|
 | 186 |   return ObjectManager::objectManagerListNames[omList]; | 
|---|
 | 187 | } | 
|---|
 | 188 |  | 
|---|
 | 189 |  | 
|---|
 | 190 |  | 
|---|
 | 191 | /** | 
|---|
 | 192 |  * @brief transforms a String into an OM_LIST (usefull for debugging/Loading). | 
|---|
 | 193 |  * @param listName the OM_LIST-name to be transformed into an OM_LIST. | 
|---|
 | 194 |  * @returns the OM_LIST transformed from listName. or the default, if not found or NULL. | 
|---|
 | 195 |  */ | 
|---|
| [7221] | 196 | OM_LIST ObjectManager::StringToOMList(const std::string& listName) | 
|---|
| [6142] | 197 | { | 
|---|
| [7221] | 198 |   if (unlikely(listName.empty())) return OM_DEFAULT_LIST; | 
|---|
| [6142] | 199 |  | 
|---|
 | 200 |   for(unsigned int i = 0; i < OM_SIZE; ++i) { | 
|---|
| [7221] | 201 |     if(listName == ObjectManager::objectManagerListNames[i]) { | 
|---|
| [6142] | 202 |       return (OM_LIST)i; | 
|---|
 | 203 |     } | 
|---|
 | 204 |   } | 
|---|
| [9656] | 205 |   return OM_NULL; | 
|---|
| [6142] | 206 | } | 
|---|
 | 207 |  | 
|---|
 | 208 |  | 
|---|
 | 209 |  | 
|---|
| [9656] | 210 | const std::string ObjectManager::objectManagerListNames[] = { | 
|---|
| [6142] | 211 |     "null", | 
|---|
 | 212 |     "dead", | 
|---|
 | 213 |     "dead-tick", | 
|---|
 | 214 |     "environ-notick", | 
|---|
 | 215 |     "environ", | 
|---|
| [7836] | 216 |     "background", | 
|---|
| [6142] | 217 |     "common", | 
|---|
 | 218 |  | 
|---|
| [9008] | 219 |     "players", | 
|---|
 | 220 |     "players-proj", | 
|---|
 | 221 |  | 
|---|
| [6142] | 222 |     "group00", | 
|---|
 | 223 |     "group00-proj", | 
|---|
 | 224 |     "group01", | 
|---|
 | 225 |     "group01-proj", | 
|---|
 | 226 |     "group02", | 
|---|
 | 227 |     "group02-proj", | 
|---|
 | 228 |     "group03", | 
|---|
 | 229 |     "group03-proj", | 
|---|
 | 230 |     "group04", | 
|---|
 | 231 |     "group04-proj", | 
|---|
 | 232 |     "group05", | 
|---|
 | 233 |     "group05-proj", | 
|---|
 | 234 |     "group06", | 
|---|
 | 235 |     "group06-proj", | 
|---|
 | 236 |     "group07", | 
|---|
 | 237 |     "group07-proj", | 
|---|
 | 238 |     "group08", | 
|---|
 | 239 |     "group08-proj", | 
|---|
 | 240 |     "group09", | 
|---|
 | 241 |     "group09-proj", | 
|---|
 | 242 |     "group10", | 
|---|
 | 243 |     "group10-proj", | 
|---|
 | 244 |     "group11", | 
|---|
 | 245 |     "group11-proj", | 
|---|
 | 246 |     "group12", | 
|---|
 | 247 |     "group12-proj", | 
|---|
 | 248 |     "group13", | 
|---|
 | 249 |     "group13-proj", | 
|---|
 | 250 |     "group14", | 
|---|
 | 251 |     "group14-proj", | 
|---|
 | 252 |     "group15", | 
|---|
 | 253 |     "group15-proj" | 
|---|
 | 254 | }; | 
|---|