Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/objectmanager/src/util/object_manager.cc @ 6121

Last change on this file since 6121 was 6121, checked in by bensch, 18 years ago

orxonox/trunk: packing the first entities into their lists

File size: 6.1 KB
RevLine 
[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:
[6091]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
[6091]23#include "shell_command.h"
[6121]24
25#include <assert.h>
26
[3655]27using namespace std;
[6091]28SHELL_COMMAND(debug, ObjectManager, debug)
29    ->defaultValues(2, NULL, 0);
[3655]30
31/**
[6120]32 * @brief standard constructor
[4838]33 */
[5629]34ObjectManager::ObjectManager ()
[3655]35{
[5629]36   this->setClassID(CL_OBJECT_MANAGER, "ObjectManager");
37   this->setName("ObjectManager");
[5795]38
39   pNodeList = NULL;
[3655]40}
41
42
43/**
[6120]44 * @brief standard deconstructor
45 *
46 * this also removes ALL entitites in existence.
[4838]47 */
[5629]48ObjectManager::~ObjectManager ()
[3655]49{
[6120]50  this->flush();
[3655]51}
[5795]52
[6120]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 */
61void 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}
[6089]67
[6120]68
[5795]69/**
[6089]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 */
76void ObjectManager::toList (WorldEntity* entity, OM_LIST omList)
77{
[6121]78  assert (omList != OM_SIZE);
79
[6089]80  if (likely(entity->getOMListNumber() != OM_INIT))
81    this->objectLists[entity->getOMListNumber()].erase(entity->getEntityIterator());
82
83  if (likely(omList != OM_INIT))
84  {
[6091]85    this->objectLists[omList].push_back(entity);
86    entity->getEntityIterator() = --this->objectLists[omList].end();
[6089]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 */
99void ObjectManager::toList (WorldEntity* entity, const char* omList)
100{
[6091]101  this->toList(entity, ObjectManager::StringToOMList(omList));
[6089]102}
103
104
105
106/**
[5795]107 * returns a new List with a list of WorldEntities of distance Radius from center
108 */
109std::list<WorldEntity*>* ObjectManager::distanceFromObject(const PNode& center, float radius, ClassID classID)
110{
111  const std::list<BaseObject*>* objectList = ClassList::getList(classID);
112  if (objectList != NULL)
113  {
114    std::list<WorldEntity*>* newList = new std::list<WorldEntity*>;
115
116    list<BaseObject*>::const_iterator node;
117    for (node = objectList->begin(); node != objectList->end(); node++)
118      if ((dynamic_cast<PNode*>(*node)->getAbsCoor() - center.getAbsCoor()).len() < radius)
119        newList->push_back(dynamic_cast<WorldEntity*>(*node));
120    return newList;
121  }
122  return NULL;
123}
[6089]124
125
[6091]126/**
127 * @brief print out nice debug information about Elements in the list OM_LIST
128 * @param omList the List to debug.
[6121]129 * @param level: level 0: only show list info; level 1: also show entities and their names.
[6091]130 */
[6121]131void ObjectManager::debug(OM_LIST omList, unsigned int level) const
[6091]132{
[6120]133  if (omList != OM_INIT || omList == OM_SIZE)
[6091]134  {
[6120]135    PRINT(0)(" +ObjectManager-LIST: '%s'-size='%d'-----\n", ObjectManager::OMListToString((OM_LIST) omList), this->objectLists[omList].size());
[6121]136    if (level > 1)
[6091]137    {
[6121]138      std::list<WorldEntity*>::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      }
[6091]143    }
144  }
145  else
146    PRINTF(2)("Invalid query. for OM_INIT-LIST\n");
147}
[6089]148
149
[6091]150/**
151 * @brief prints out very nice debug information
152 * @param listName the Name of the list to get Debug information from
[6121]153 * @param level: level 0: only show list info; level 1: also show entities and their names.
[6091]154 */
[6121]155void ObjectManager::debug(const char* listName, unsigned int level)
[6091]156{
[6121]157  PRINT(0)("=================================\n");
[6091]158  PRINT(0)("=ObjectManager-DEBUG=============\n");
[6121]159  PRINT(0)("=================================\n");
[6091]160  if (listName == NULL || listName[0] == '\0')
161    for (unsigned int i = 0; i < OM_SIZE; ++i)
[6121]162      debug((OM_LIST) i, level);
[6091]163  else
164    debug(ObjectManager::StringToOMList(listName));
165  PRINT(0)("=========================== OM ==\n");
166}
[6089]167
[6091]168
169
[6089]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 */
[6091]175const char* ObjectManager::OMListToString(OM_LIST omList)
[6089]176{
[6120]177  if (omList == OM_INIT || omList == OM_SIZE)
178    return "===invalid===";
179
[6119]180  return ObjectManager::objectManagerListNames[omList];
[6089]181}
182
183
184
[6119]185/**
186 * @brief transforms a String into an OM_LIST (usefull for debugging/Loading).
187 * @param listName the OM_LIST-name to be transformed into an OM_LIST.
188 * @returns the OM_LIST transformed from listName. or the default, if not found or NULL.
189 */
[6089]190OM_LIST ObjectManager::StringToOMList(const char* listName)
191{
192  if (unlikely(listName == NULL)) return OM_DEFAULT_LIST;
193
[6119]194  for(unsigned int i = 0; i < OM_SIZE; ++i) {
195    if(!strcmp(listName, ObjectManager::objectManagerListNames[i])) {
196      return (OM_LIST)i;
197    }
198  }
[6089]199  return OM_DEFAULT_LIST;
200}
201
[6119]202
203
204const char* ObjectManager::objectManagerListNames[] = {
205    "null",
206    "dead",
[6121]207    "dead-tick"
[6119]208    "environ-notick",
209    "environ",
210    "common",
211
212    "group00",
213    "group00-proj",
214    "group01",
215    "group01-proj",
216    "group02",
217    "group02-proj",
218    "group03",
219    "group03-proj",
220    "group04",
221    "group04-proj",
222    "group05",
223    "group05-proj",
224    "group06",
225    "group06-proj",
226    "group07",
227    "group07-proj",
228    "group08",
229    "group08-proj",
230    "group09",
231    "group09-proj",
232    "group10",
233    "group10-proj",
234    "group11",
235    "group11-proj",
236    "group12",
237    "group12-proj",
238    "group13",
239    "group13-proj",
240    "group14",
241    "group14-proj",
242    "group15",
243    "group15-proj"
244};
Note: See TracBrowser for help on using the repository browser.