| [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" |
|---|
| [5795] | 19 | #include "class_list.h" |
|---|
| [3655] | 20 | |
|---|
| [5795] | 21 | #include "world_entity.h" |
|---|
| 22 | |
|---|
| [6142] | 23 | #include "shell_command.h" |
|---|
| 24 | |
|---|
| 25 | #include <assert.h> |
|---|
| 26 | |
|---|
| [3655] | 27 | using namespace std; |
|---|
| [6142] | 28 | SHELL_COMMAND(debug, ObjectManager, debug) |
|---|
| [7198] | 29 | ->defaultValues("", 0); |
|---|
| [3655] | 30 | |
|---|
| 31 | /** |
|---|
| [6142] | 32 | * @brief standard constructor |
|---|
| [4838] | 33 | */ |
|---|
| [5629] | 34 | ObjectManager::ObjectManager () |
|---|
| [3655] | 35 | { |
|---|
| [5629] | 36 | this->setClassID(CL_OBJECT_MANAGER, "ObjectManager"); |
|---|
| 37 | this->setName("ObjectManager"); |
|---|
| [5795] | 38 | |
|---|
| 39 | pNodeList = NULL; |
|---|
| [3655] | 40 | } |
|---|
| 41 | |
|---|
| [6142] | 42 | |
|---|
| [3655] | 43 | /** |
|---|
| [6142] | 44 | * @brief standard deconstructor |
|---|
| 45 | * |
|---|
| 46 | * this also removes ALL entitites in existence. |
|---|
| [4838] | 47 | */ |
|---|
| [6142] | 48 | ObjectManager::~ObjectManager () |
|---|
| 49 | { |
|---|
| 50 | this->flush(); |
|---|
| 51 | } |
|---|
| [3655] | 52 | |
|---|
| 53 | /** |
|---|
| [6142] | 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. |
|---|
| [4838] | 60 | */ |
|---|
| [6142] | 61 | void ObjectManager::flush() |
|---|
| [3655] | 62 | { |
|---|
| [6142] | 63 | for (unsigned int i = 0; i < OM_SIZE; ++i) |
|---|
| 64 | while(!this->objectLists[i].empty()) |
|---|
| 65 | delete this->objectLists[i].front(); |
|---|
| [7785] | 66 | |
|---|
| 67 | // delete reflectin list |
|---|
| 68 | while( !this->reflectionList.empty()) |
|---|
| 69 | delete this->reflectionList.front(); |
|---|
| [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)) |
|---|
| 85 | this->objectLists[entity->getOMListNumber()].erase(entity->getEntityIterator()); |
|---|
| 86 | |
|---|
| 87 | if (likely(omList != OM_INIT)) |
|---|
| 88 | { |
|---|
| 89 | this->objectLists[omList].push_back(entity); |
|---|
| 90 | entity->getEntityIterator() = --this->objectLists[omList].end(); |
|---|
| 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 | */ |
|---|
| [7368] | 113 | void ObjectManager::distanceFromObject(EntityList& entities, const PNode& center, float radius, ClassID classID) |
|---|
| [5795] | 114 | { |
|---|
| 115 | const std::list<BaseObject*>* objectList = ClassList::getList(classID); |
|---|
| 116 | if (objectList != NULL) |
|---|
| 117 | { |
|---|
| 118 | |
|---|
| 119 | list<BaseObject*>::const_iterator node; |
|---|
| 120 | for (node = objectList->begin(); node != objectList->end(); node++) |
|---|
| 121 | if ((dynamic_cast<PNode*>(*node)->getAbsCoor() - center.getAbsCoor()).len() < radius) |
|---|
| [7368] | 122 | entities.push_back(dynamic_cast<WorldEntity*>(*node)); |
|---|
| [5795] | 123 | } |
|---|
| 124 | } |
|---|
| [6142] | 125 | |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | * @brief print out nice debug information about Elements in the list OM_LIST |
|---|
| 129 | * @param omList the List to debug. |
|---|
| 130 | * @param level: level 0: only show list info; level 1: also show entities and their names. |
|---|
| 131 | */ |
|---|
| 132 | void ObjectManager::debug(OM_LIST omList, unsigned int level) const |
|---|
| 133 | { |
|---|
| 134 | if (omList != OM_INIT || omList == OM_SIZE) |
|---|
| 135 | { |
|---|
| 136 | PRINT(0)(" +ObjectManager-LIST: '%s'==size='%d'==---\n", ObjectManager::OMListToString((OM_LIST)omList), this->objectLists[omList].size()); |
|---|
| 137 | // if (level >= 1) |
|---|
| 138 | { |
|---|
| [7370] | 139 | ObjectManager::EntityList::const_iterator entity; |
|---|
| [6142] | 140 | for (entity = this->objectLists[omList].begin(); entity != this->objectLists[omList].end(); entity++) |
|---|
| 141 | { |
|---|
| 142 | PRINT(0)(" | %s::%s\n",(*entity)->getClassName(), (*entity)->getName()); |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | } |
|---|
| 146 | else |
|---|
| 147 | PRINTF(2)("Invalid query. for OM_INIT-LIST or OM_SIZE\n"); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | /** |
|---|
| 152 | * @brief prints out very nice debug information |
|---|
| 153 | * @param listName the Name of the list to get Debug information from |
|---|
| 154 | * @param level: level 0: only show list info; level 1: also show entities and their names. |
|---|
| 155 | */ |
|---|
| [7221] | 156 | void ObjectManager::debug(const std::string& listName, unsigned int level) |
|---|
| [6142] | 157 | { |
|---|
| 158 | PRINT(0)("=================================\n"); |
|---|
| 159 | PRINT(0)("=ObjectManager-DEBUG=============\n"); |
|---|
| 160 | PRINT(0)("=================================\n"); |
|---|
| [7221] | 161 | if (listName.empty()) |
|---|
| [6142] | 162 | for (unsigned int i = 0; i < OM_SIZE; ++i) |
|---|
| 163 | debug((OM_LIST) i, level); |
|---|
| 164 | else |
|---|
| 165 | debug(ObjectManager::StringToOMList(listName)); |
|---|
| 166 | PRINT(0)("=========================== OM ==\n"); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | /** |
|---|
| 172 | * @brief transforms an omList into a String (usefull for debugging). |
|---|
| 173 | * @param omList the OM_LIST to be transformed into a String. |
|---|
| 174 | * @returns the String transformed from omList. |
|---|
| 175 | */ |
|---|
| 176 | const char* ObjectManager::OMListToString(OM_LIST omList) |
|---|
| 177 | { |
|---|
| 178 | if (omList == OM_INIT || omList == OM_SIZE) |
|---|
| 179 | return "===invalid==="; |
|---|
| 180 | |
|---|
| 181 | printf("%d\n", omList); |
|---|
| 182 | return ObjectManager::objectManagerListNames[omList]; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | /** |
|---|
| 188 | * @brief transforms a String into an OM_LIST (usefull for debugging/Loading). |
|---|
| 189 | * @param listName the OM_LIST-name to be transformed into an OM_LIST. |
|---|
| 190 | * @returns the OM_LIST transformed from listName. or the default, if not found or NULL. |
|---|
| 191 | */ |
|---|
| [7221] | 192 | OM_LIST ObjectManager::StringToOMList(const std::string& listName) |
|---|
| [6142] | 193 | { |
|---|
| [7221] | 194 | if (unlikely(listName.empty())) return OM_DEFAULT_LIST; |
|---|
| [6142] | 195 | |
|---|
| 196 | for(unsigned int i = 0; i < OM_SIZE; ++i) { |
|---|
| [7221] | 197 | if(listName == ObjectManager::objectManagerListNames[i]) { |
|---|
| [6142] | 198 | return (OM_LIST)i; |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | return OM_DEFAULT_LIST; |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | const char* ObjectManager::objectManagerListNames[] = { |
|---|
| 207 | "null", |
|---|
| 208 | "dead", |
|---|
| 209 | "dead-tick", |
|---|
| 210 | "environ-notick", |
|---|
| 211 | "environ", |
|---|
| 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 | }; |
|---|