Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/current_cd/src/world_entities/world_entity.cc @ 6909

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

current_cd: merged with the current version of the trunk

File size: 15.7 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{
[6909]208<<<<<<< .working
[5428]209  if (this->obbTree)
210    delete this->obbTree;
211
[5995]212  if (this->models[0] != NULL)
[5428]213  {
[6909]214=======
215  if( this->model != NULL) {
216>>>>>>> .merge-right.r6905
[5428]217    PRINTF(4)("creating obb tree\n");
[6909]218<<<<<<< .working
[5708]219
220
[5995]221    this->obbTree = new OBBTree(depth, (sVec3D*)this->models[0]->getVertexArray(), this->models[0]->getVertexCount());
[6909]222=======
223    //this->obbTree = new OBBTree(depth, (sVec3D*)this->model->getVertexArray(), this->model->getVertexCount());
224    this->obbTree = new OBBTree(depth, *model->getModelInfo());
225>>>>>>> .merge-right.r6905
[5428]226    return true;
227  }
228  else
229  {
[6909]230    PRINTF(1)("could not create obb-tree, because no model was loaded yet\n");
[5428]231    this->obbTree = NULL;
232    return false;
233  }
[5061]234}
[5057]235
[6142]236/**
237 * @brief moves this entity to the List OM_List
238 * @param list the list to set this Entity to.
239 *
240 * this is the same as a call to State::getObjectManager()->toList(entity , list);
241 * directly, but with an easier interface.
242 *
243 * @todo inline this (peut etre)
244 */
245void WorldEntity::toList(OM_LIST list)
246{
247  State::getObjectManager()->toList(this, list);
248}
[5061]249
[6142]250
251
[4261]252/**
[4885]253 * sets the character attributes of a worldentity
[4836]254 * @param character attributes
[4885]255 *
256 * these attributes don't have to be set, only use them, if you need them
[2043]257*/
[5498]258//void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr)
259//{}
[2036]260
[3583]261
[2043]262/**
[5029]263 *  this function is called, when two entities collide
264 * @param entity: the world entity with whom it collides
265 *
266 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
267 */
[6909]268void WorldEntity::collidesWith(const WorldEntity& entity, const Vector& location) const
[5029]269{
[5498]270  /**
271   * THIS IS A DEFAULT COLLISION-Effect.
272   * IF YOU WANT TO CREATE A SPECIFIC COLLISION ON EACH OBJECT
273   * USE::
274   * if (entity->isA(CL_WHAT_YOU_ARE_LOOKING_FOR)) { printf "dothings"; };
275   *
276   * You can always define a default Action.... don't be affraid just test it :)
277   */
[6430]278  //  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
[5029]279}
280
[2043]281
282/**
[5498]283 *  this is called immediately after the Entity has been constructed, initialized and then Spawned into the World
[4885]284 *
[5498]285 */
[3229]286void WorldEntity::postSpawn ()
[6430]287{}
[2043]288
[3583]289
[2043]290/**
[4836]291 *  this method is called by the world if the WorldEntity leaves valid gamespace
[4885]292 *
293 * For free entities this means it left the Track boundaries. With bound entities it means its Location adresses a
294 * place that is not in the world anymore. In both cases you might have to take extreme measures (a.k.a. call destroy).
[5498]295 *
296 * NOT YET IMPLEMENTED
297 */
[3583]298void WorldEntity::leftWorld ()
[6430]299{}
[2043]300
[3583]301
[2190]302/**
[4836]303 *  this method is called every frame
304 * @param time: the time in seconds that has passed since the last tick
[4885]305 *
306 * Handle all stuff that should update with time inside this method (movement, animation, etc.)
[2043]307*/
[4570]308void WorldEntity::tick(float time)
[6430]309{}
[3583]310
[5498]311
[3583]312/**
[4836]313 *  the entity is drawn onto the screen with this function
[4885]314 *
315 * This is a central function of an entity: call it to let the entity painted to the screen.
316 * Just override this function with whatever you want to be drawn.
[3365]317*/
[5500]318void WorldEntity::draw() const
[3803]319{
[6430]320  //PRINTF(0)("(%s::%s)\n", this->getClassName(), this->getName());
[6281]321  //  assert(!unlikely(this->models.empty()));
[6002]322  {
323    glMatrixMode(GL_MODELVIEW);
324    glPushMatrix();
[4570]325
[6002]326    /* translate */
327    glTranslatef (this->getAbsCoor ().x,
328                  this->getAbsCoor ().y,
329                  this->getAbsCoor ().z);
[6004]330    Vector tmpRot = this->getAbsDir().getSpacialAxis();
331    glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
[2043]332
[6004]333
334    // This Draws the LOD's
[6002]335    float cameraDistance = (State::getCamera()->getAbsCoor() - this->getAbsCoor()).len();
[6004]336    if (cameraDistance > 30 && this->models.size() >= 3 && this->models[2] != NULL)
[6002]337    {
[6222]338      this->models[2]->draw();
[6004]339    }
340    else if (cameraDistance > 10 && this->models.size() >= 2 && this->models[1] != NULL)
[6002]341    {
342      this->models[1]->draw();
[6004]343    }
344    else if (this->models.size() >= 1 && this->models[0] != NULL)
[6002]345    {
346      this->models[0]->draw();
347    }
348    glPopMatrix();
349  }
[3803]350}
[3583]351
[6430]352/**
[6700]353 * @param health the Health to add.
354 * @returns the health left (this->healthMax - health+this->health)
[6430]355 */
[6700]356float WorldEntity::increaseHealth(float health)
[6430]357{
[6700]358  this->health += health;
359  if (this->health > this->healthMax)
[6430]360  {
[6700]361    float retHealth = this->healthMax - this->health;
362    this->health = this->healthMax;
363    this->updateHealthWidget();
364    return retHealth;
[6430]365  }
[6700]366  this->updateHealthWidget();
[6430]367  return 0.0;
368}
[6281]369
[5498]370/**
[6700]371 * @param health the Health to be removed
[6430]372 * @returns 0.0 or the rest, that was not substracted (bellow 0.0)
373 */
[6700]374float WorldEntity::decreaseHealth(float health)
[6430]375{
[6700]376  this->health -= health;
[6430]377
[6700]378  if (this->health < 0)
[6430]379  {
[6700]380    float retHealth = -this->health;
381    this->health = 0.0f;
382    this->updateHealthWidget();
383    return retHealth;
[6430]384  }
[6700]385  this->updateHealthWidget();
[6430]386  return 0.0;
387
388}
389
390/**
[6700]391 * @param maxHealth the maximal health that can be loaded onto the entity.
[6430]392 */
[6700]393void WorldEntity::setHealthMax(float healthMax)
[6430]394{
[6700]395  this->healthMax = healthMax;
396  if (this->health > this->healthMax)
[6430]397  {
[6700]398    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());
399    this->health = this->healthMax;
[6430]400  }
[6700]401  this->updateHealthWidget();
[6430]402}
403
[6431]404/**
[6700]405 * @brief creates the HealthWidget
[6431]406 *
[6700]407 * since not all entities need an HealthWidget, it is only created on request.
[6431]408 */
[6700]409void WorldEntity::createHealthWidget()
[6430]410{
[6700]411  if (this->healthWidget == NULL)
[6430]412  {
[6700]413    this->healthWidget = new GLGuiBar();
414    this->healthWidget->setSize2D(30,400);
415    this->healthWidget->setAbsCoor2D(10,100);
[6430]416
[6700]417    this->updateHealthWidget();
[6430]418  }
419  else
[6700]420    PRINTF(3)("Allready created the HealthWidget for %s::%s\n", this->getClassName(), this->getName());
[6430]421}
422
[6700]423void WorldEntity::increaseHealthMax(float increaseHealth)
[6440]424{
[6700]425  this->healthMax += increaseHealth;
426  this->updateHealthWidget();
[6440]427}
428
[6700]429
430GLGuiWidget* WorldEntity::getHealthWidget()
431{
432  this->createHealthWidget();
433  return this->healthWidget;
434}
435
[6431]436/**
[6700]437 * @param visibility shows or hides the health-bar
[6431]438 * (creates the widget if needed)
439 */
[6700]440void WorldEntity::setHealthWidgetVisibilit(bool visibility)
[6430]441{
442    if (visibility)
443    {
[6700]444      if (this->healthWidget != NULL)
445        this->healthWidget->show();
[6430]446      else
447      {
[6700]448        this->createHealthWidget();
449        this->updateHealthWidget();
450        this->healthWidget->show();
[6430]451      }
452    }
[6700]453    else if (this->healthWidget != NULL)
454      this->healthWidget->hide();
[6430]455}
456
[6431]457/**
[6700]458 * @brief updates the HealthWidget
[6431]459 */
[6700]460void WorldEntity::updateHealthWidget()
[6430]461{
[6700]462  if (this->healthWidget != NULL)
[6430]463  {
[6700]464    this->healthWidget->setMaximum(this->healthMax);
465    this->healthWidget->setValue(this->health);
[6430]466  }
467}
468
469
470/**
[5498]471 * DEBUG-DRAW OF THE BV-Tree.
472 * @param depth What depth to draw
473 * @param drawMode the mode to draw this entity under
474 */
[5501]475void WorldEntity::drawBVTree(unsigned int depth, int drawMode) const
[4684]476{
477  glMatrixMode(GL_MODELVIEW);
478  glPushMatrix();
479  /* translate */
480  glTranslatef (this->getAbsCoor ().x,
481                this->getAbsCoor ().y,
482                this->getAbsCoor ().z);
483  /* rotate */
[4998]484  Vector tmpRot = this->getAbsDir().getSpacialAxis();
485  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
[4684]486
487  if (this->obbTree)
488    this->obbTree->drawBV(depth, drawMode);
489  glPopMatrix();
490}
[6341]491
[6424]492
[6341]493/**
[6424]494 * Debug the WorldEntity
495 */
496void WorldEntity::debugEntity() const
497{
498  PRINT(0)("WorldEntity %s::%s  (DEBUG)\n", this->getClassName(), this->getName());
499  this->debugNode();
500  PRINT(0)("List: %s ; ModelCount %d - ", ObjectManager::OMListToString(this->objectListNumber) , this->models.size());
501  for (unsigned int i = 0; i < this->models.size(); i++)
502  {
503    if (models[i] != NULL)
504      PRINT(0)(" : %d:%s", i, this->models[i]->getName());
505  }
506  PRINT(0)("\n");
507
508}
509
510
[6498]511
512
513/********************************************************************************************
514 NETWORK STUFF
515 ********************************************************************************************/
516
517
[6424]518/**
[6341]519 * Writes data from network containing information about the state
520 * @param data pointer to data
521 * @param length length of data
522 * @param sender hostID of sender
523 */
524int WorldEntity::writeState( const byte * data, int length, int sender )
525{
526  char* modelFileName;
527  SYNCHELP_READ_BEGIN();
528
[6815]529  SYNCHELP_READ_FKT( PNode::writeState, NWT_WE_PN_WRITESTATE );
[6341]530
[6815]531  SYNCHELP_READ_STRINGM( modelFileName, NWT_WE_PN_MODELFILENAME );
532  SYNCHELP_READ_FLOAT( scaling, NWT_WE_PN_SCALING );
[6341]533  //check if modelFileName is relative to datadir or absolute
[6424]534
[6695]535
536  PRINTF(0)("================ LOADING MODEL %s, %f\n", modelFileName, scaling);
537
[6424]538  if ( strcmp(modelFileName, "") )
[6341]539  {
[6695]540    loadModel( modelFileName, scaling);
541    PRINTF(0)("modelfilename: %s\n", getModel( 0 )->getName());
[6341]542  }
543  delete[] modelFileName;
544
[6634]545  /*SYNCHELP_READ_STRINGM( modelFileName );
[6498]546
[6424]547  if ( strcmp(modelFileName, "") )
[6498]548    if ( strstr(modelFileName, ResourceManager::getInstance()->getDataDir()) )
549    {
550      this->md2TextureFileName = new char[strlen(modelFileName)-strlen(ResourceManager::getInstance()->getDataDir())+1];
551      strcpy((char*)this->md2TextureFileName, modelFileName+strlen(ResourceManager::getInstance()->getDataDir()));
552    }
553    else
554    {
555      this->md2TextureFileName = modelFileName;
556    }
[6634]557  */
[6424]558
[6341]559  return SYNCHELP_READ_N;
560}
561
[6498]562
[6341]563/**
564 * data copied in data will bee sent to another host
565 * @param data pointer to data
566 * @param maxLength max length of data
567 * @return the number of bytes writen
568 */
569int WorldEntity::readState( byte * data, int maxLength )
570{
571  SYNCHELP_WRITE_BEGIN();
572
[6815]573  SYNCHELP_WRITE_FKT( PNode::readState, NWT_WE_PN_WRITESTATE );
[6341]574
[6634]575  if ( getModel(0) && getModel(0)->getName() )
576  {
577    char* name = (char*)(getModel( 0 )->getName());
578
579    if ( strstr(name, ResourceManager::getInstance()->getDataDir()) )
580    {
581      name += strlen(ResourceManager::getInstance()->getDataDir());
582    }
583
[6815]584    SYNCHELP_WRITE_STRING( name, NWT_WE_PN_MODELFILENAME );
[6634]585  }
586  else
587  {
[6815]588    SYNCHELP_WRITE_STRING("", NWT_WE_PN_MODELFILENAME);
[6634]589  }
590
[6815]591  SYNCHELP_WRITE_FLOAT( scaling, NWT_WE_PN_SCALING );
[6634]592  /*if ( this->md2TextureFileName!=NULL && strcmp(this->md2TextureFileName, "") )
[6424]593  {
594    SYNCHELP_WRITE_STRING(this->md2TextureFileName);
595  }
596  else
597  {
598    SYNCHELP_WRITE_STRING("");
[6634]599}*/
[6424]600
[6341]601  return SYNCHELP_WRITE_N;
602}
Note: See TracBrowser for help on using the repository browser.