Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/world_entity.cc @ 6720

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

trunk: bug in loadModel

File size: 15.2 KB
RevLine 
[2036]1
2
[4570]3/*
[2036]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
[2190]15   co-programmer: Christian Meyer
[2036]16*/
[5300]17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
[2036]18
19#include "world_entity.h"
[5208]20#include "shell_command.h"
[5143]21
[5511]22#include "model.h"
[6222]23#include "md2Model.h"
[5143]24#include "resource_manager.h"
25#include "load_param.h"
[3803]26#include "vector.h"
[4682]27#include "obb_tree.h"
[3608]28
[6430]29#include "glgui_bar.h"
30
[6002]31#include "state.h"
32
[2036]33using namespace std;
34
[5208]35SHELL_COMMAND(model, WorldEntity, loadModel)
[6430]36->describe("sets the Model of the WorldEntity")
37->defaultValues(2, "models/ships/fighter.obj", 1.0);
[5208]38
[6424]39SHELL_COMMAND(debugEntity, WorldEntity, debugWE);
[5208]40
[2043]41/**
[4836]42 *  Loads the WordEntity-specific Part of any derived Class
[5498]43 *
44 * @param root: Normally NULL, as the Derived Entities define a loadParams Function themeselves,
45 *              that can calls WorldEntities loadParams for itself.
46 */
[6430]47WorldEntity::WorldEntity()
48    : Synchronizeable()
[2190]49{
[4320]50  this->setClassID(CL_WORLD_ENTITY, "WorldEntity");
[4597]51
[4682]52  this->obbTree = NULL;
[6700]53  this->healthWidget = NULL;
54  this->healthMax = 1.0f;
55  this->health = 1.0f;
[6695]56  this->scaling = 1.0f;
[4261]57
[6695]58  /* OSOLETE */
59  this->bVisible = true;
60  this->bCollide = true;
61
[6424]62  this->md2TextureFileName = NULL;
63
[6142]64  this->objectListNumber = OM_INIT;
65  this->objectListIterator = NULL;
66
67  this->toList(OM_NULL);
[2190]68}
[2043]69
70/**
[4836]71 *  standard destructor
[2043]72*/
[2190]73WorldEntity::~WorldEntity ()
[2036]74{
[5498]75  // Delete the obbTree
[5302]76  if( this->obbTree != NULL)
[4814]77    delete this->obbTree;
[5994]78
[6700]79  if (this->healthWidget != NULL)
80    delete this->healthWidget;
[6430]81
[5994]82  // Delete the model (unregister it with the ResourceManager)
[6005]83  for (unsigned int i = 0; i < this->models.size(); i++)
84    this->setModel(NULL, i);
[6142]85
86  State::getObjectManager()->toList(this, OM_INIT);
[3531]87}
88
[5498]89/**
90 * loads the WorldEntity Specific Parameters.
91 * @param root: the XML-Element to load the Data From
92 */
[4436]93void WorldEntity::loadParams(const TiXmlElement* root)
94{
[5498]95  // Do the PNode loading stuff
[6512]96  PNode::loadParams(root);
[5498]97
[6222]98  LoadParam(root, "md2texture", this, WorldEntity, loadMD2Texture)
[6430]99  .describe("the fileName of the texture, that should be loaded onto this world-entity. (must be relative to the data-dir)")
100  .defaultValues(1, NULL);
[6222]101
[4436]102  // Model Loading
[5671]103  LoadParam(root, "model", this, WorldEntity, loadModel)
[6430]104  .describe("the fileName of the model, that should be loaded onto this world-entity. (must be relative to the data-dir)")
105  .defaultValues(3, NULL, 1.0f, 0);
106
[6700]107  LoadParam(root, "maxHealth", this, WorldEntity, setHealthMax)
108  .describe("The Maximum health that can be loaded onto this entity")
[6430]109  .defaultValues(1, 1.0f);
110
[6700]111  LoadParam(root, "health", this, WorldEntity, setHealth)
112  .describe("The Health the WorldEntity has at this moment")
[6430]113  .defaultValues(1, 1.0f);
[4436]114}
115
[6222]116
[3531]117/**
[4885]118 * loads a Model onto a WorldEntity
[4836]119 * @param fileName the name of the model to load
[5057]120 * @param scaling the Scaling of the model
[5498]121 *
122 * @todo fix this, so it only has one loadModel-Function.
[4261]123*/
[5995]124void WorldEntity::loadModel(const char* fileName, float scaling, unsigned int modelNumber)
[4261]125{
[6695]126  this->modelLODName = fileName;
[6424]127  this->scaling = scaling;
[6341]128  if ( fileName != NULL && strcmp(fileName, "") )
[6142]129  {
[6430]130    // search for the special character # in the LoadParam
[6005]131    if (strchr(fileName, '#') != NULL)
[6222]132    {
133      PRINTF(4)("Found # in %s... searching for LOD's\n", fileName);
134      char* lodFile = new char[strlen(fileName)+1];
135      strcpy(lodFile, fileName);
136      char* depth = strchr(lodFile, '#');
[6720]137      for (unsigned int i = 0; i < 3; i++)
[6005]138      {
[6222]139        *depth = 48+(int)i;
140        if (ResourceManager::isInDataDir(lodFile))
141          this->loadModel(lodFile, scaling, i);
[6005]142      }
[6222]143      return;
144    }
[6720]145    if (this->scaling <= 0.0)
[6424]146    {
[6720]147      this->scaling = 1.0;
[6424]148      PRINTF(1)("YOU GAVE ME A CRAPY SCALE resetting to 1\n");
149    }
[6222]150    if(strstr(fileName, ".obj"))
151    {
152      PRINTF(4)("fetching OBJ file: %s\n", fileName);
[6720]153      if (this->scaling == 1.0)
[6222]154        this->setModel((Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN), modelNumber);
155      else
[6720]156        this->setModel((Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN, this->scaling), modelNumber);
[6222]157
158      if( modelNumber == 0)
159        this->buildObbTree(4);
160    }
161    else if(strstr(fileName, ".md2"))
162    {
163      PRINTF(4)("fetching MD2 file: %s\n", fileName);
164      Model* m = new MD2Model(fileName, this->md2TextureFileName);
[6430]165      //this->setModel((Model*)ResourceManager::getInstance()->load(fileName, MD2, RP_CAMPAIGN), 0);
[6222]166      this->setModel(m, 0);
167    }
[4732]168  }
169  else
[6341]170  {
[5995]171    this->setModel(NULL);
[6341]172  }
[4261]173}
174
[5061]175/**
[5994]176 * sets a specific Model for the Object.
177 * @param model The Model to set
178 * @param modelNumber the n'th model in the List to get.
179 */
180void WorldEntity::setModel(Model* model, unsigned int modelNumber)
181{
[5995]182  if (this->models.size() <= modelNumber)
183    this->models.resize(modelNumber+1, NULL);
184
185  if (this->models[modelNumber] != NULL)
[6004]186  {
[5995]187    Resource* resource = ResourceManager::getInstance()->locateResourceByPointer(this->models[modelNumber]);
[6430]188    //     if (resource != NULL)
[6222]189    ResourceManager::getInstance()->unload(resource, RP_LEVEL);
[5994]190  }
[6222]191  else
192    delete this->models[modelNumber];
193
[5995]194  this->models[modelNumber] = model;
[5994]195
[6222]196
[6430]197  //   if (this->model != NULL)
198  //     this->buildObbTree(4);
[5994]199}
200
201
202/**
[5061]203 * builds the obb-tree
204 * @param depth the depth to calculate
205 */
206bool WorldEntity::buildObbTree(unsigned int depth)
207{
[5428]208  if (this->obbTree)
209    delete this->obbTree;
210
[5995]211  if (this->models[0] != NULL)
[5428]212  {
213    PRINTF(4)("creating obb tree\n");
[5708]214
215
[5995]216    this->obbTree = new OBBTree(depth, (sVec3D*)this->models[0]->getVertexArray(), this->models[0]->getVertexCount());
[5428]217    return true;
218  }
219  else
220  {
221    PRINTF(2)("could not create obb-tree, because no model was loaded yet\n");
222    this->obbTree = NULL;
223    return false;
224  }
[5061]225}
[5057]226
[6142]227/**
228 * @brief moves this entity to the List OM_List
229 * @param list the list to set this Entity to.
230 *
231 * this is the same as a call to State::getObjectManager()->toList(entity , list);
232 * directly, but with an easier interface.
233 *
234 * @todo inline this (peut etre)
235 */
236void WorldEntity::toList(OM_LIST list)
237{
238  State::getObjectManager()->toList(this, list);
239}
[5061]240
[6142]241
242
[4261]243/**
[4885]244 * sets the character attributes of a worldentity
[4836]245 * @param character attributes
[4885]246 *
247 * these attributes don't have to be set, only use them, if you need them
[2043]248*/
[5498]249//void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr)
250//{}
[2036]251
[3583]252
[2043]253/**
[5029]254 *  this function is called, when two entities collide
255 * @param entity: the world entity with whom it collides
256 *
257 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
258 */
259void WorldEntity::collidesWith(WorldEntity* entity, const Vector& location)
260{
[5498]261  /**
262   * THIS IS A DEFAULT COLLISION-Effect.
263   * IF YOU WANT TO CREATE A SPECIFIC COLLISION ON EACH OBJECT
264   * USE::
265   * if (entity->isA(CL_WHAT_YOU_ARE_LOOKING_FOR)) { printf "dothings"; };
266   *
267   * You can always define a default Action.... don't be affraid just test it :)
268   */
[6430]269  //  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
[5029]270}
271
[2043]272
273/**
[5498]274 *  this is called immediately after the Entity has been constructed, initialized and then Spawned into the World
[4885]275 *
[5498]276 */
[3229]277void WorldEntity::postSpawn ()
[6430]278{}
[2043]279
[3583]280
[2043]281/**
[4836]282 *  this method is called by the world if the WorldEntity leaves valid gamespace
[4885]283 *
284 * For free entities this means it left the Track boundaries. With bound entities it means its Location adresses a
285 * place that is not in the world anymore. In both cases you might have to take extreme measures (a.k.a. call destroy).
[5498]286 *
287 * NOT YET IMPLEMENTED
288 */
[3583]289void WorldEntity::leftWorld ()
[6430]290{}
[2043]291
[3583]292
[2190]293/**
[4836]294 *  this method is called every frame
295 * @param time: the time in seconds that has passed since the last tick
[4885]296 *
297 * Handle all stuff that should update with time inside this method (movement, animation, etc.)
[2043]298*/
[4570]299void WorldEntity::tick(float time)
[6430]300{}
[3583]301
[5498]302
[3583]303/**
[4836]304 *  the entity is drawn onto the screen with this function
[4885]305 *
306 * This is a central function of an entity: call it to let the entity painted to the screen.
307 * Just override this function with whatever you want to be drawn.
[3365]308*/
[5500]309void WorldEntity::draw() const
[3803]310{
[6430]311  //PRINTF(0)("(%s::%s)\n", this->getClassName(), this->getName());
[6281]312  //  assert(!unlikely(this->models.empty()));
[6002]313  {
314    glMatrixMode(GL_MODELVIEW);
315    glPushMatrix();
[4570]316
[6002]317    /* translate */
318    glTranslatef (this->getAbsCoor ().x,
319                  this->getAbsCoor ().y,
320                  this->getAbsCoor ().z);
[6004]321    Vector tmpRot = this->getAbsDir().getSpacialAxis();
322    glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
[2043]323
[6004]324
325    // This Draws the LOD's
[6002]326    float cameraDistance = (State::getCamera()->getAbsCoor() - this->getAbsCoor()).len();
[6004]327    if (cameraDistance > 30 && this->models.size() >= 3 && this->models[2] != NULL)
[6002]328    {
[6222]329      this->models[2]->draw();
[6004]330    }
331    else if (cameraDistance > 10 && this->models.size() >= 2 && this->models[1] != NULL)
[6002]332    {
333      this->models[1]->draw();
[6004]334    }
335    else if (this->models.size() >= 1 && this->models[0] != NULL)
[6002]336    {
337      this->models[0]->draw();
338    }
339    glPopMatrix();
340  }
[3803]341}
[3583]342
[6430]343/**
[6700]344 * @param health the Health to add.
345 * @returns the health left (this->healthMax - health+this->health)
[6430]346 */
[6700]347float WorldEntity::increaseHealth(float health)
[6430]348{
[6700]349  this->health += health;
350  if (this->health > this->healthMax)
[6430]351  {
[6700]352    float retHealth = this->healthMax - this->health;
353    this->health = this->healthMax;
354    this->updateHealthWidget();
355    return retHealth;
[6430]356  }
[6700]357  this->updateHealthWidget();
[6430]358  return 0.0;
359}
[6281]360
[5498]361/**
[6700]362 * @param health the Health to be removed
[6430]363 * @returns 0.0 or the rest, that was not substracted (bellow 0.0)
364 */
[6700]365float WorldEntity::decreaseHealth(float health)
[6430]366{
[6700]367  this->health -= health;
[6430]368
[6700]369  if (this->health < 0)
[6430]370  {
[6700]371    float retHealth = -this->health;
372    this->health = 0.0f;
373    this->updateHealthWidget();
374    return retHealth;
[6430]375  }
[6700]376  this->updateHealthWidget();
[6430]377  return 0.0;
378
379}
380
381/**
[6700]382 * @param maxHealth the maximal health that can be loaded onto the entity.
[6430]383 */
[6700]384void WorldEntity::setHealthMax(float healthMax)
[6430]385{
[6700]386  this->healthMax = healthMax;
387  if (this->health > this->healthMax)
[6430]388  {
[6700]389    PRINTF(3)("new maxHealth is bigger as the old health. Did you really intend to do this for (%s::%s)\n", this->getClassName(), this->getName());
390    this->health = this->healthMax;
[6430]391  }
[6700]392  this->updateHealthWidget();
[6430]393}
394
[6431]395/**
[6700]396 * @brief creates the HealthWidget
[6431]397 *
[6700]398 * since not all entities need an HealthWidget, it is only created on request.
[6431]399 */
[6700]400void WorldEntity::createHealthWidget()
[6430]401{
[6700]402  if (this->healthWidget == NULL)
[6430]403  {
[6700]404    this->healthWidget = new GLGuiBar();
405    this->healthWidget->setSize2D(30,400);
406    this->healthWidget->setAbsCoor2D(10,100);
[6430]407
[6700]408    this->updateHealthWidget();
[6430]409  }
410  else
[6700]411    PRINTF(3)("Allready created the HealthWidget for %s::%s\n", this->getClassName(), this->getName());
[6430]412}
413
[6700]414void WorldEntity::increaseHealthMax(float increaseHealth)
[6440]415{
[6700]416  this->healthMax += increaseHealth;
417  this->updateHealthWidget();
[6440]418}
419
[6700]420
421GLGuiWidget* WorldEntity::getHealthWidget()
422{
423  this->createHealthWidget();
424  return this->healthWidget;
425}
426
[6431]427/**
[6700]428 * @param visibility shows or hides the health-bar
[6431]429 * (creates the widget if needed)
430 */
[6700]431void WorldEntity::setHealthWidgetVisibilit(bool visibility)
[6430]432{
433    if (visibility)
434    {
[6700]435      if (this->healthWidget != NULL)
436        this->healthWidget->show();
[6430]437      else
438      {
[6700]439        this->createHealthWidget();
440        this->updateHealthWidget();
441        this->healthWidget->show();
[6430]442      }
443    }
[6700]444    else if (this->healthWidget != NULL)
445      this->healthWidget->hide();
[6430]446}
447
[6431]448/**
[6700]449 * @brief updates the HealthWidget
[6431]450 */
[6700]451void WorldEntity::updateHealthWidget()
[6430]452{
[6700]453  if (this->healthWidget != NULL)
[6430]454  {
[6700]455    this->healthWidget->setMaximum(this->healthMax);
456    this->healthWidget->setValue(this->health);
[6430]457  }
458}
459
460
461/**
[5498]462 * DEBUG-DRAW OF THE BV-Tree.
463 * @param depth What depth to draw
464 * @param drawMode the mode to draw this entity under
465 */
[5501]466void WorldEntity::drawBVTree(unsigned int depth, int drawMode) const
[4684]467{
468  glMatrixMode(GL_MODELVIEW);
469  glPushMatrix();
470  /* translate */
471  glTranslatef (this->getAbsCoor ().x,
472                this->getAbsCoor ().y,
473                this->getAbsCoor ().z);
474  /* rotate */
[4998]475  Vector tmpRot = this->getAbsDir().getSpacialAxis();
476  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
[4684]477
478  if (this->obbTree)
479    this->obbTree->drawBV(depth, drawMode);
480  glPopMatrix();
481}
[6341]482
[6424]483
[6341]484/**
[6424]485 * Debug the WorldEntity
486 */
487void WorldEntity::debugEntity() const
488{
489  PRINT(0)("WorldEntity %s::%s  (DEBUG)\n", this->getClassName(), this->getName());
490  this->debugNode();
491  PRINT(0)("List: %s ; ModelCount %d - ", ObjectManager::OMListToString(this->objectListNumber) , this->models.size());
492  for (unsigned int i = 0; i < this->models.size(); i++)
493  {
494    if (models[i] != NULL)
495      PRINT(0)(" : %d:%s", i, this->models[i]->getName());
496  }
497  PRINT(0)("\n");
498
499}
500
501
[6498]502
503
504/********************************************************************************************
505 NETWORK STUFF
506 ********************************************************************************************/
507
508
[6424]509/**
[6341]510 * Writes data from network containing information about the state
511 * @param data pointer to data
512 * @param length length of data
513 * @param sender hostID of sender
514 */
515int WorldEntity::writeState( const byte * data, int length, int sender )
516{
517  char* modelFileName;
518  SYNCHELP_READ_BEGIN();
519
520  SYNCHELP_READ_FKT( PNode::writeState );
521
522  SYNCHELP_READ_STRINGM( modelFileName );
523  SYNCHELP_READ_FLOAT( scaling );
524  //check if modelFileName is relative to datadir or absolute
[6424]525
[6695]526
527  PRINTF(0)("================ LOADING MODEL %s, %f\n", modelFileName, scaling);
528
[6424]529  if ( strcmp(modelFileName, "") )
[6341]530  {
[6695]531    loadModel( modelFileName, scaling);
532    PRINTF(0)("modelfilename: %s\n", getModel( 0 )->getName());
[6341]533  }
534  delete[] modelFileName;
535
[6634]536  /*SYNCHELP_READ_STRINGM( modelFileName );
[6498]537
[6424]538  if ( strcmp(modelFileName, "") )
[6498]539    if ( strstr(modelFileName, ResourceManager::getInstance()->getDataDir()) )
540    {
541      this->md2TextureFileName = new char[strlen(modelFileName)-strlen(ResourceManager::getInstance()->getDataDir())+1];
542      strcpy((char*)this->md2TextureFileName, modelFileName+strlen(ResourceManager::getInstance()->getDataDir()));
543    }
544    else
545    {
546      this->md2TextureFileName = modelFileName;
547    }
[6634]548  */
[6424]549
[6341]550  return SYNCHELP_READ_N;
551}
552
[6498]553
[6341]554/**
555 * data copied in data will bee sent to another host
556 * @param data pointer to data
557 * @param maxLength max length of data
558 * @return the number of bytes writen
559 */
560int WorldEntity::readState( byte * data, int maxLength )
561{
562  SYNCHELP_WRITE_BEGIN();
563
564  SYNCHELP_WRITE_FKT( PNode::readState );
565
[6634]566  if ( getModel(0) && getModel(0)->getName() )
567  {
568    char* name = (char*)(getModel( 0 )->getName());
569
570    if ( strstr(name, ResourceManager::getInstance()->getDataDir()) )
571    {
572      name += strlen(ResourceManager::getInstance()->getDataDir());
573    }
574
575    SYNCHELP_WRITE_STRING( name );
576  }
577  else
578  {
579    SYNCHELP_WRITE_STRING("");
580  }
581
[6341]582  SYNCHELP_WRITE_FLOAT( scaling );
[6634]583  /*if ( this->md2TextureFileName!=NULL && strcmp(this->md2TextureFileName, "") )
[6424]584  {
585    SYNCHELP_WRITE_STRING(this->md2TextureFileName);
586  }
587  else
588  {
589    SYNCHELP_WRITE_STRING("");
[6634]590}*/
[6424]591
[6341]592  return SYNCHELP_WRITE_N;
593}
Note: See TracBrowser for help on using the repository browser.