Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/util/object_manager.cc @ 7607

Last change on this file since 7607 was 7370, checked in by bensch, 19 years ago

orxonox/trunk: drawLists/tickLists are used, and made some TiXmlElement to const TiXmlElement, also fixed some bugs in the loadWorldEntities-functions from Gameworld and SipleGameMenu

File size: 6.1 KB
Line 
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
27using namespace std;
28SHELL_COMMAND(debug, ObjectManager, debug)
29    ->defaultValues("", 0);
30
31/**
32 * @brief standard constructor
33 */
34ObjectManager::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 */
48ObjectManager::~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 */
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}
67
68
69/**
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{
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 */
99void ObjectManager::toList (WorldEntity* entity, const std::string& omList)
100{
101  this->toList(entity, ObjectManager::StringToOMList(omList));
102}
103
104
105
106/**
107 * @returns a new List with a list of WorldEntities of distance Radius from center
108 */
109void ObjectManager::distanceFromObject(EntityList& entities, const PNode& center, float radius, ClassID classID)
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)
118        entities.push_back(dynamic_cast<WorldEntity*>(*node));
119  }
120}
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 */
128void 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    {
135      ObjectManager::EntityList::const_iterator entity;
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 */
152void ObjectManager::debug(const std::string& listName, unsigned int level)
153{
154  PRINT(0)("=================================\n");
155  PRINT(0)("=ObjectManager-DEBUG=============\n");
156  PRINT(0)("=================================\n");
157  if (listName.empty())
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 */
172const 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 */
188OM_LIST ObjectManager::StringToOMList(const std::string& listName)
189{
190  if (unlikely(listName.empty())) return OM_DEFAULT_LIST;
191
192  for(unsigned int i = 0; i < OM_SIZE; ++i) {
193    if(listName == ObjectManager::objectManagerListNames[i]) {
194      return (OM_LIST)i;
195    }
196  }
197  return OM_DEFAULT_LIST;
198}
199
200
201
202const 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};
Note: See TracBrowser for help on using the repository browser.