Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: merged the network branche into the trunk

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