Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7072 was 7068, checked in by patrick, 18 years ago

trunk: the modelinfo of md2 models is now written too

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