Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: new Default Values… they now too are in MultiType instead of (count, …) or (count, va_arg) style

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