Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/world_entities/world_entity.cc @ 6418

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

network: scaling: warn instead of just set to 1.0 if the value of scaling is 0.0f

File size: 11.0 KB
Line 
1
2
3/*
4   orxonox - the future of 3D-vertical-scrollers
5
6   Copyright (C) 2004 orx
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2, or (at your option)
11   any later version.
12
13   ### File Specific:
14   main-programmer: Patrick Boenzli
15   co-programmer: Christian Meyer
16*/
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
18
19#include "world_entity.h"
20#include "shell_command.h"
21
22#include "model.h"
23#include "md2Model.h"
24#include "resource_manager.h"
25#include "load_param.h"
26#include "vector.h"
27#include "obb_tree.h"
28
29#include "state.h"
30
31using namespace std;
32
33SHELL_COMMAND(model, WorldEntity, loadModel)
34    ->describe("sets the Model of the WorldEntity")
35    ->defaultValues(2, "models/ships/fighter.obj", 1.0);
36
37SHELL_COMMAND(debugEntity, WorldEntity, debugWE);
38
39/**
40 *  Loads the WordEntity-specific Part of any derived Class
41 *
42 * @param root: Normally NULL, as the Derived Entities define a loadParams Function themeselves,
43 *              that can calls WorldEntities loadParams for itself.
44 */
45WorldEntity::WorldEntity(const TiXmlElement* root)
46  : Synchronizeable()
47{
48  this->setClassID(CL_WORLD_ENTITY, "WorldEntity");
49
50  this->obbTree = NULL;
51
52  if (root != NULL)
53    this->loadParams(root);
54
55  this->setVisibiliy(true);
56
57  this->objectListNumber = OM_INIT;
58  this->objectListIterator = NULL;
59
60  this->toList(OM_NULL);
61}
62
63/**
64 *  standard destructor
65*/
66WorldEntity::~WorldEntity ()
67{
68  // Delete the obbTree
69  if( this->obbTree != NULL)
70    delete this->obbTree;
71
72  // Delete the model (unregister it with the ResourceManager)
73  for (unsigned int i = 0; i < this->models.size(); i++)
74    this->setModel(NULL, i);
75
76  State::getObjectManager()->toList(this, OM_INIT);
77}
78
79/**
80 * loads the WorldEntity Specific Parameters.
81 * @param root: the XML-Element to load the Data From
82 */
83void WorldEntity::loadParams(const TiXmlElement* root)
84{
85  // Do the PNode loading stuff
86  static_cast<PNode*>(this)->loadParams(root);
87
88  LoadParam(root, "md2texture", this, WorldEntity, loadMD2Texture)
89      .describe("the fileName of the texture, that should be loaded onto this world-entity. (must be relative to the data-dir)")
90      .defaultValues(1, NULL);
91
92  // Model Loading
93  LoadParam(root, "model", this, WorldEntity, loadModel)
94      .describe("the fileName of the model, that should be loaded onto this world-entity. (must be relative to the data-dir)")
95      .defaultValues(3, NULL, 1.0f, 0);
96}
97
98
99/**
100 * loads a Model onto a WorldEntity
101 * @param fileName the name of the model to load
102 * @param scaling the Scaling of the model
103 *
104 * @todo fix this, so it only has one loadModel-Function.
105*/
106void WorldEntity::loadModel(const char* fileName, float scaling, unsigned int modelNumber)
107{
108  if ( fileName != NULL && strcmp(fileName, "") )
109  {
110   // search for the special character # in the LoadParam
111    if (strchr(fileName, '#') != NULL)
112    {
113      PRINTF(4)("Found # in %s... searching for LOD's\n", fileName);
114      char* lodFile = new char[strlen(fileName)+1];
115      strcpy(lodFile, fileName);
116      char* depth = strchr(lodFile, '#');
117      for (unsigned int i = 0; i < 5; i++)
118      {
119        *depth = 48+(int)i;
120        printf("-------%s\n", lodFile);
121        if (ResourceManager::isInDataDir(lodFile))
122          this->loadModel(lodFile, scaling, i);
123      }
124      return;
125    }
126    if (scaling == 0.0)
127    {
128      scaling = 1.0;
129      PRINTF(1)("YOU GAVE ME A CRAPY SCALE resetting to 1\n");
130    }
131    if(strstr(fileName, ".obj"))
132    {
133      PRINTF(4)("fetching OBJ file: %s\n", fileName);
134      if (scaling == 1.0)
135        this->setModel((Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN), modelNumber);
136      else
137        this->setModel((Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN, &scaling), modelNumber);
138
139      if( modelNumber == 0)
140        this->buildObbTree(4);
141    }
142    else if(strstr(fileName, ".md2"))
143    {
144      PRINTF(4)("fetching MD2 file: %s\n", fileName);
145      Model* m = new MD2Model(fileName, this->md2TextureFileName);
146        //this->setModel((Model*)ResourceManager::getInstance()->load(fileName, MD2, RP_CAMPAIGN), 0);
147      this->setModel(m, 0);
148    }
149  }
150  else
151  {
152    this->setModel(NULL);
153  }
154}
155
156/**
157 * sets a specific Model for the Object.
158 * @param model The Model to set
159 * @param modelNumber the n'th model in the List to get.
160 */
161void WorldEntity::setModel(Model* model, unsigned int modelNumber)
162{
163  if (this->models.size() <= modelNumber)
164    this->models.resize(modelNumber+1, NULL);
165
166  if (this->models[modelNumber] != NULL)
167  {
168    Resource* resource = ResourceManager::getInstance()->locateResourceByPointer(this->models[modelNumber]);
169//     if (resource != NULL)
170    ResourceManager::getInstance()->unload(resource, RP_LEVEL);
171  }
172  else
173    delete this->models[modelNumber];
174
175  this->models[modelNumber] = model;
176
177
178//   if (this->model != NULL)
179//     this->buildObbTree(4);
180}
181
182
183/**
184 * builds the obb-tree
185 * @param depth the depth to calculate
186 */
187bool WorldEntity::buildObbTree(unsigned int depth)
188{
189  if (this->obbTree)
190    delete this->obbTree;
191
192  if (this->models[0] != NULL)
193  {
194    PRINTF(4)("creating obb tree\n");
195
196
197    this->obbTree = new OBBTree(depth, (sVec3D*)this->models[0]->getVertexArray(), this->models[0]->getVertexCount());
198    return true;
199  }
200  else
201  {
202    PRINTF(2)("could not create obb-tree, because no model was loaded yet\n");
203    this->obbTree = NULL;
204    return false;
205  }
206}
207
208/**
209 * @brief moves this entity to the List OM_List
210 * @param list the list to set this Entity to.
211 *
212 * this is the same as a call to State::getObjectManager()->toList(entity , list);
213 * directly, but with an easier interface.
214 *
215 * @todo inline this (peut etre)
216 */
217void WorldEntity::toList(OM_LIST list)
218{
219  State::getObjectManager()->toList(this, list);
220}
221
222
223
224/**
225 * sets the character attributes of a worldentity
226 * @param character attributes
227 *
228 * these attributes don't have to be set, only use them, if you need them
229*/
230//void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr)
231//{}
232
233
234/**
235 *  this function is called, when two entities collide
236 * @param entity: the world entity with whom it collides
237 *
238 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
239 */
240void WorldEntity::collidesWith(WorldEntity* entity, const Vector& location)
241{
242  /**
243   * THIS IS A DEFAULT COLLISION-Effect.
244   * IF YOU WANT TO CREATE A SPECIFIC COLLISION ON EACH OBJECT
245   * USE::
246   * if (entity->isA(CL_WHAT_YOU_ARE_LOOKING_FOR)) { printf "dothings"; };
247   *
248   * You can always define a default Action.... don't be affraid just test it :)
249   */
250//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
251}
252
253
254/**
255 *  this is called immediately after the Entity has been constructed, initialized and then Spawned into the World
256 *
257 */
258void WorldEntity::postSpawn ()
259{
260}
261
262
263/**
264 *  this method is called by the world if the WorldEntity leaves valid gamespace
265 *
266 * For free entities this means it left the Track boundaries. With bound entities it means its Location adresses a
267 * place that is not in the world anymore. In both cases you might have to take extreme measures (a.k.a. call destroy).
268 *
269 * NOT YET IMPLEMENTED
270 */
271void WorldEntity::leftWorld ()
272{
273}
274
275
276/**
277 *  this method is called every frame
278 * @param time: the time in seconds that has passed since the last tick
279 *
280 * Handle all stuff that should update with time inside this method (movement, animation, etc.)
281*/
282void WorldEntity::tick(float time)
283{
284}
285
286
287/**
288 *  the entity is drawn onto the screen with this function
289 *
290 * This is a central function of an entity: call it to let the entity painted to the screen.
291 * Just override this function with whatever you want to be drawn.
292*/
293void WorldEntity::draw() const
294{
295    //PRINTF(0)("(%s::%s)\n", this->getClassName(), this->getName());
296  //  assert(!unlikely(this->models.empty()));
297  {
298    glMatrixMode(GL_MODELVIEW);
299    glPushMatrix();
300
301    /* translate */
302    glTranslatef (this->getAbsCoor ().x,
303                  this->getAbsCoor ().y,
304                  this->getAbsCoor ().z);
305    Vector tmpRot = this->getAbsDir().getSpacialAxis();
306    glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
307
308
309    // This Draws the LOD's
310    float cameraDistance = (State::getCamera()->getAbsCoor() - this->getAbsCoor()).len();
311    if (cameraDistance > 30 && this->models.size() >= 3 && this->models[2] != NULL)
312    {
313      this->models[2]->draw();
314    }
315    else if (cameraDistance > 10 && this->models.size() >= 2 && this->models[1] != NULL)
316    {
317      this->models[1]->draw();
318    }
319    else if (this->models.size() >= 1 && this->models[0] != NULL)
320    {
321      this->models[0]->draw();
322    }
323    glPopMatrix();
324  }
325}
326
327
328/**
329 * DEBUG-DRAW OF THE BV-Tree.
330 * @param depth What depth to draw
331 * @param drawMode the mode to draw this entity under
332 */
333void WorldEntity::drawBVTree(unsigned int depth, int drawMode) const
334{
335  glMatrixMode(GL_MODELVIEW);
336  glPushMatrix();
337  /* translate */
338  glTranslatef (this->getAbsCoor ().x,
339                this->getAbsCoor ().y,
340                this->getAbsCoor ().z);
341  /* rotate */
342  Vector tmpRot = this->getAbsDir().getSpacialAxis();
343  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
344
345  if (this->obbTree)
346    this->obbTree->drawBV(depth, drawMode);
347  glPopMatrix();
348}
349
350
351/**
352 * Debug the WorldEntity
353 */
354void WorldEntity::debugEntity() const
355{
356  PRINT(0)("WorldEntity %s::%s  (DEBUG)\n", this->getClassName(), this->getName());
357  this->debugNode();
358  PRINT(0)("List: %s ; ModelCount %d - ", ObjectManager::OMListToString(this->objectListNumber) , this->models.size());
359  for (unsigned int i = 0; i < this->models.size(); i++)
360  {
361    if (models[i] != NULL)
362      PRINT(0)(" : %d:%s", i, this->models[i]->getName());
363  }
364  PRINT(0)("\n");
365
366}
367
368
369/**
370 * Writes data from network containing information about the state
371 * @param data pointer to data
372 * @param length length of data
373 * @param sender hostID of sender
374 */
375int WorldEntity::writeState( const byte * data, int length, int sender )
376{
377  char* modelFileName;
378  SYNCHELP_READ_BEGIN();
379
380  SYNCHELP_READ_FKT( PNode::writeState );
381
382  SYNCHELP_READ_STRINGM( modelFileName );
383  SYNCHELP_READ_FLOAT( scaling );
384  //check if modelFileName is relative to datadir or absolute
385  if ( strstr(modelFileName, ResourceManager::getInstance()->getDataDir()) )
386  {
387    loadModel( modelFileName+strlen(ResourceManager::getInstance()->getDataDir()), scaling );
388  }
389  else
390  {
391    loadModel( modelFileName, scaling );
392  }
393  delete[] modelFileName;
394
395  return SYNCHELP_READ_N;
396}
397
398/**
399 * data copied in data will bee sent to another host
400 * @param data pointer to data
401 * @param maxLength max length of data
402 * @return the number of bytes writen
403 */
404int WorldEntity::readState( byte * data, int maxLength )
405{
406  SYNCHELP_WRITE_BEGIN();
407
408  SYNCHELP_WRITE_FKT( PNode::readState );
409
410  SYNCHELP_WRITE_STRING( getModel( 0 )->getName() );
411  SYNCHELP_WRITE_FLOAT( scaling );
412  return SYNCHELP_WRITE_N;
413}
Note: See TracBrowser for help on using the repository browser.