Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: md2 scaling patch

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