[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(); |
---|
[3655] | 66 | } |
---|
[5795] | 67 | |
---|
[6142] | 68 | |
---|
[5795] | 69 | /** |
---|
[6142] | 70 | * @brief moves an Entity from an old list to a new One |
---|
| 71 | * @param entity the entity to move. |
---|
| 72 | * @param omList the new List to move the entity to. |
---|
| 73 | * |
---|
| 74 | * this will erase the entity from the old list |
---|
| 75 | */ |
---|
| 76 | void ObjectManager::toList (WorldEntity* entity, OM_LIST omList) |
---|
| 77 | { |
---|
| 78 | assert (omList != OM_SIZE); |
---|
| 79 | |
---|
| 80 | if (likely(entity->getOMListNumber() != OM_INIT)) |
---|
| 81 | this->objectLists[entity->getOMListNumber()].erase(entity->getEntityIterator()); |
---|
| 82 | |
---|
| 83 | if (likely(omList != OM_INIT)) |
---|
| 84 | { |
---|
| 85 | this->objectLists[omList].push_back(entity); |
---|
| 86 | entity->getEntityIterator() = --this->objectLists[omList].end(); |
---|
| 87 | entity->getOMListNumber() = omList; |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | * @see ObjectManager::toList(WorldEntity* OM_LIST) |
---|
| 94 | * @param entity the entity to move. |
---|
| 95 | * @param omList the new List to move the entity to. |
---|
| 96 | * |
---|
| 97 | * this function also does a transformation from omList as char* to OM_LIST. |
---|
| 98 | */ |
---|
[7221] | 99 | void ObjectManager::toList (WorldEntity* entity, const std::string& omList) |
---|
[6142] | 100 | { |
---|
| 101 | this->toList(entity, ObjectManager::StringToOMList(omList)); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | /** |
---|
[7368] | 107 | * @returns a new List with a list of WorldEntities of distance Radius from center |
---|
[5795] | 108 | */ |
---|
[7368] | 109 | void ObjectManager::distanceFromObject(EntityList& entities, const PNode& center, float radius, ClassID classID) |
---|
[5795] | 110 | { |
---|
| 111 | const std::list<BaseObject*>* objectList = ClassList::getList(classID); |
---|
| 112 | if (objectList != NULL) |
---|
| 113 | { |
---|
| 114 | |
---|
| 115 | list<BaseObject*>::const_iterator node; |
---|
| 116 | for (node = objectList->begin(); node != objectList->end(); node++) |
---|
| 117 | if ((dynamic_cast<PNode*>(*node)->getAbsCoor() - center.getAbsCoor()).len() < radius) |
---|
[7368] | 118 | entities.push_back(dynamic_cast<WorldEntity*>(*node)); |
---|
[5795] | 119 | } |
---|
| 120 | } |
---|
[6142] | 121 | |
---|
| 122 | |
---|
| 123 | /** |
---|
| 124 | * @brief print out nice debug information about Elements in the list OM_LIST |
---|
| 125 | * @param omList the List to debug. |
---|
| 126 | * @param level: level 0: only show list info; level 1: also show entities and their names. |
---|
| 127 | */ |
---|
| 128 | void ObjectManager::debug(OM_LIST omList, unsigned int level) const |
---|
| 129 | { |
---|
| 130 | if (omList != OM_INIT || omList == OM_SIZE) |
---|
| 131 | { |
---|
| 132 | PRINT(0)(" +ObjectManager-LIST: '%s'==size='%d'==---\n", ObjectManager::OMListToString((OM_LIST)omList), this->objectLists[omList].size()); |
---|
| 133 | // if (level >= 1) |
---|
| 134 | { |
---|
[7370] | 135 | ObjectManager::EntityList::const_iterator entity; |
---|
[6142] | 136 | for (entity = this->objectLists[omList].begin(); entity != this->objectLists[omList].end(); entity++) |
---|
| 137 | { |
---|
| 138 | PRINT(0)(" | %s::%s\n",(*entity)->getClassName(), (*entity)->getName()); |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | else |
---|
| 143 | PRINTF(2)("Invalid query. for OM_INIT-LIST or OM_SIZE\n"); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | /** |
---|
| 148 | * @brief prints out very nice debug information |
---|
| 149 | * @param listName the Name of the list to get Debug information from |
---|
| 150 | * @param level: level 0: only show list info; level 1: also show entities and their names. |
---|
| 151 | */ |
---|
[7221] | 152 | void ObjectManager::debug(const std::string& listName, unsigned int level) |
---|
[6142] | 153 | { |
---|
| 154 | PRINT(0)("=================================\n"); |
---|
| 155 | PRINT(0)("=ObjectManager-DEBUG=============\n"); |
---|
| 156 | PRINT(0)("=================================\n"); |
---|
[7221] | 157 | if (listName.empty()) |
---|
[6142] | 158 | for (unsigned int i = 0; i < OM_SIZE; ++i) |
---|
| 159 | debug((OM_LIST) i, level); |
---|
| 160 | else |
---|
| 161 | debug(ObjectManager::StringToOMList(listName)); |
---|
| 162 | PRINT(0)("=========================== OM ==\n"); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | * @brief transforms an omList into a String (usefull for debugging). |
---|
| 169 | * @param omList the OM_LIST to be transformed into a String. |
---|
| 170 | * @returns the String transformed from omList. |
---|
| 171 | */ |
---|
| 172 | const char* ObjectManager::OMListToString(OM_LIST omList) |
---|
| 173 | { |
---|
| 174 | if (omList == OM_INIT || omList == OM_SIZE) |
---|
| 175 | return "===invalid==="; |
---|
| 176 | |
---|
| 177 | printf("%d\n", omList); |
---|
| 178 | return ObjectManager::objectManagerListNames[omList]; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | /** |
---|
| 184 | * @brief transforms a String into an OM_LIST (usefull for debugging/Loading). |
---|
| 185 | * @param listName the OM_LIST-name to be transformed into an OM_LIST. |
---|
| 186 | * @returns the OM_LIST transformed from listName. or the default, if not found or NULL. |
---|
| 187 | */ |
---|
[7221] | 188 | OM_LIST ObjectManager::StringToOMList(const std::string& listName) |
---|
[6142] | 189 | { |
---|
[7221] | 190 | if (unlikely(listName.empty())) return OM_DEFAULT_LIST; |
---|
[6142] | 191 | |
---|
| 192 | for(unsigned int i = 0; i < OM_SIZE; ++i) { |
---|
[7221] | 193 | if(listName == ObjectManager::objectManagerListNames[i]) { |
---|
[6142] | 194 | return (OM_LIST)i; |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | return OM_DEFAULT_LIST; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | const char* ObjectManager::objectManagerListNames[] = { |
---|
| 203 | "null", |
---|
| 204 | "dead", |
---|
| 205 | "dead-tick", |
---|
| 206 | "environ-notick", |
---|
| 207 | "environ", |
---|
| 208 | "common", |
---|
| 209 | |
---|
| 210 | "group00", |
---|
| 211 | "group00-proj", |
---|
| 212 | "group01", |
---|
| 213 | "group01-proj", |
---|
| 214 | "group02", |
---|
| 215 | "group02-proj", |
---|
| 216 | "group03", |
---|
| 217 | "group03-proj", |
---|
| 218 | "group04", |
---|
| 219 | "group04-proj", |
---|
| 220 | "group05", |
---|
| 221 | "group05-proj", |
---|
| 222 | "group06", |
---|
| 223 | "group06-proj", |
---|
| 224 | "group07", |
---|
| 225 | "group07-proj", |
---|
| 226 | "group08", |
---|
| 227 | "group08-proj", |
---|
| 228 | "group09", |
---|
| 229 | "group09-proj", |
---|
| 230 | "group10", |
---|
| 231 | "group10-proj", |
---|
| 232 | "group11", |
---|
| 233 | "group11-proj", |
---|
| 234 | "group12", |
---|
| 235 | "group12-proj", |
---|
| 236 | "group13", |
---|
| 237 | "group13-proj", |
---|
| 238 | "group14", |
---|
| 239 | "group14-proj", |
---|
| 240 | "group15", |
---|
| 241 | "group15-proj" |
---|
| 242 | }; |
---|