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