Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/world_entities/world_entity.cc @ 6421

Last change on this file since 6421 was 6421, checked in by rennerc, 18 years ago

ParamPowerUp now syncs state correctly
fixed a bug in worldEntity

File size: 11.4 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
[6002]29#include "state.h"
30
[2036]31using namespace std;
32
[5208]33SHELL_COMMAND(model, WorldEntity, loadModel)
34    ->describe("sets the Model of the WorldEntity")
[5555]35    ->defaultValues(2, "models/ships/fighter.obj", 1.0);
[5208]36
[6415]37SHELL_COMMAND(debugEntity, WorldEntity, debugWE);
[5208]38
[2043]39/**
[4836]40 *  Loads the WordEntity-specific Part of any derived Class
[5498]41 *
42 * @param root: Normally NULL, as the Derived Entities define a loadParams Function themeselves,
43 *              that can calls WorldEntities loadParams for itself.
44 */
[4261]45WorldEntity::WorldEntity(const TiXmlElement* root)
[5996]46  : Synchronizeable()
[2190]47{
[4320]48  this->setClassID(CL_WORLD_ENTITY, "WorldEntity");
[4597]49
[4682]50  this->obbTree = NULL;
[4261]51
[6421]52  this->md2TextureFileName = NULL;
53
[5995]54  if (root != NULL)
[4261]55    this->loadParams(root);
56
[4885]57  this->setVisibiliy(true);
[6142]58
59  this->objectListNumber = OM_INIT;
60  this->objectListIterator = NULL;
61
62  this->toList(OM_NULL);
[2190]63}
[2043]64
65/**
[4836]66 *  standard destructor
[2043]67*/
[2190]68WorldEntity::~WorldEntity ()
[2036]69{
[5498]70  // Delete the obbTree
[5302]71  if( this->obbTree != NULL)
[4814]72    delete this->obbTree;
[5994]73
74  // Delete the model (unregister it with the ResourceManager)
[6005]75  for (unsigned int i = 0; i < this->models.size(); i++)
76    this->setModel(NULL, i);
[6142]77
78  State::getObjectManager()->toList(this, OM_INIT);
[3531]79}
80
[5498]81/**
82 * loads the WorldEntity Specific Parameters.
83 * @param root: the XML-Element to load the Data From
84 */
[4436]85void WorldEntity::loadParams(const TiXmlElement* root)
86{
[5498]87  // Do the PNode loading stuff
[4436]88  static_cast<PNode*>(this)->loadParams(root);
[5498]89
[6222]90  LoadParam(root, "md2texture", this, WorldEntity, loadMD2Texture)
91      .describe("the fileName of the texture, that should be loaded onto this world-entity. (must be relative to the data-dir)")
92      .defaultValues(1, NULL);
93
[4436]94  // Model Loading
[5671]95  LoadParam(root, "model", this, WorldEntity, loadModel)
[5652]96      .describe("the fileName of the model, that should be loaded onto this world-entity. (must be relative to the data-dir)")
[5995]97      .defaultValues(3, NULL, 1.0f, 0);
[4436]98}
99
[6222]100
[3531]101/**
[4885]102 * loads a Model onto a WorldEntity
[4836]103 * @param fileName the name of the model to load
[5057]104 * @param scaling the Scaling of the model
[5498]105 *
106 * @todo fix this, so it only has one loadModel-Function.
[4261]107*/
[5995]108void WorldEntity::loadModel(const char* fileName, float scaling, unsigned int modelNumber)
[4261]109{
[6419]110  this->scaling = scaling;
[6341]111  if ( fileName != NULL && strcmp(fileName, "") )
[6142]112  {
[6222]113   // search for the special character # in the LoadParam
[6005]114    if (strchr(fileName, '#') != NULL)
[6222]115    {
116      PRINTF(4)("Found # in %s... searching for LOD's\n", fileName);
117      char* lodFile = new char[strlen(fileName)+1];
118      strcpy(lodFile, fileName);
119      char* depth = strchr(lodFile, '#');
120      for (unsigned int i = 0; i < 5; i++)
[6005]121      {
[6222]122        *depth = 48+(int)i;
123        printf("-------%s\n", lodFile);
124        if (ResourceManager::isInDataDir(lodFile))
125          this->loadModel(lodFile, scaling, i);
[6005]126      }
[6222]127      return;
128    }
[6418]129    if (scaling == 0.0)
130    {
131      scaling = 1.0;
132      PRINTF(1)("YOU GAVE ME A CRAPY SCALE resetting to 1\n");
133    }
[6222]134    if(strstr(fileName, ".obj"))
135    {
136      PRINTF(4)("fetching OBJ file: %s\n", fileName);
[6418]137      if (scaling == 1.0)
[6222]138        this->setModel((Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN), modelNumber);
139      else
140        this->setModel((Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN, &scaling), modelNumber);
141
142      if( modelNumber == 0)
143        this->buildObbTree(4);
144    }
145    else if(strstr(fileName, ".md2"))
146    {
147      PRINTF(4)("fetching MD2 file: %s\n", fileName);
148      Model* m = new MD2Model(fileName, this->md2TextureFileName);
149        //this->setModel((Model*)ResourceManager::getInstance()->load(fileName, MD2, RP_CAMPAIGN), 0);
150      this->setModel(m, 0);
151    }
[4732]152  }
153  else
[6341]154  {
[5995]155    this->setModel(NULL);
[6341]156  }
[4261]157}
158
[5061]159/**
[5994]160 * sets a specific Model for the Object.
161 * @param model The Model to set
162 * @param modelNumber the n'th model in the List to get.
163 */
164void WorldEntity::setModel(Model* model, unsigned int modelNumber)
165{
[5995]166  if (this->models.size() <= modelNumber)
167    this->models.resize(modelNumber+1, NULL);
168
169  if (this->models[modelNumber] != NULL)
[6004]170  {
[5995]171    Resource* resource = ResourceManager::getInstance()->locateResourceByPointer(this->models[modelNumber]);
[6222]172//     if (resource != NULL)
173    ResourceManager::getInstance()->unload(resource, RP_LEVEL);
[5994]174  }
[6222]175  else
176    delete this->models[modelNumber];
177
[5995]178  this->models[modelNumber] = model;
[5994]179
[6222]180
[6004]181//   if (this->model != NULL)
[5994]182//     this->buildObbTree(4);
183}
184
185
186/**
[5061]187 * builds the obb-tree
188 * @param depth the depth to calculate
189 */
190bool WorldEntity::buildObbTree(unsigned int depth)
191{
[5428]192  if (this->obbTree)
193    delete this->obbTree;
194
[5995]195  if (this->models[0] != NULL)
[5428]196  {
197    PRINTF(4)("creating obb tree\n");
[5708]198
199
[5995]200    this->obbTree = new OBBTree(depth, (sVec3D*)this->models[0]->getVertexArray(), this->models[0]->getVertexCount());
[5428]201    return true;
202  }
203  else
204  {
205    PRINTF(2)("could not create obb-tree, because no model was loaded yet\n");
206    this->obbTree = NULL;
207    return false;
208  }
[5061]209}
[5057]210
[6142]211/**
212 * @brief moves this entity to the List OM_List
213 * @param list the list to set this Entity to.
214 *
215 * this is the same as a call to State::getObjectManager()->toList(entity , list);
216 * directly, but with an easier interface.
217 *
218 * @todo inline this (peut etre)
219 */
220void WorldEntity::toList(OM_LIST list)
221{
222  State::getObjectManager()->toList(this, list);
223}
[5061]224
[6142]225
226
[4261]227/**
[4885]228 * sets the character attributes of a worldentity
[4836]229 * @param character attributes
[4885]230 *
231 * these attributes don't have to be set, only use them, if you need them
[2043]232*/
[5498]233//void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr)
234//{}
[2036]235
[3583]236
[2043]237/**
[5029]238 *  this function is called, when two entities collide
239 * @param entity: the world entity with whom it collides
240 *
241 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
242 */
243void WorldEntity::collidesWith(WorldEntity* entity, const Vector& location)
244{
[5498]245  /**
246   * THIS IS A DEFAULT COLLISION-Effect.
247   * IF YOU WANT TO CREATE A SPECIFIC COLLISION ON EACH OBJECT
248   * USE::
249   * if (entity->isA(CL_WHAT_YOU_ARE_LOOKING_FOR)) { printf "dothings"; };
250   *
251   * You can always define a default Action.... don't be affraid just test it :)
252   */
[5257]253//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
[5029]254}
255
[2043]256
257/**
[5498]258 *  this is called immediately after the Entity has been constructed, initialized and then Spawned into the World
[4885]259 *
[5498]260 */
[3229]261void WorldEntity::postSpawn ()
[2190]262{
263}
[2043]264
[3583]265
[2043]266/**
[4836]267 *  this method is called by the world if the WorldEntity leaves valid gamespace
[4885]268 *
269 * For free entities this means it left the Track boundaries. With bound entities it means its Location adresses a
270 * place that is not in the world anymore. In both cases you might have to take extreme measures (a.k.a. call destroy).
[5498]271 *
272 * NOT YET IMPLEMENTED
273 */
[3583]274void WorldEntity::leftWorld ()
[2190]275{
276}
[2043]277
[3583]278
[2190]279/**
[4836]280 *  this method is called every frame
281 * @param time: the time in seconds that has passed since the last tick
[4885]282 *
283 * Handle all stuff that should update with time inside this method (movement, animation, etc.)
[2043]284*/
[4570]285void WorldEntity::tick(float time)
[2190]286{
287}
[3583]288
[5498]289
[3583]290/**
[4836]291 *  the entity is drawn onto the screen with this function
[4885]292 *
293 * This is a central function of an entity: call it to let the entity painted to the screen.
294 * Just override this function with whatever you want to be drawn.
[3365]295*/
[5500]296void WorldEntity::draw() const
[3803]297{
[6281]298    //PRINTF(0)("(%s::%s)\n", this->getClassName(), this->getName());
299  //  assert(!unlikely(this->models.empty()));
[6002]300  {
301    glMatrixMode(GL_MODELVIEW);
302    glPushMatrix();
[4570]303
[6002]304    /* translate */
305    glTranslatef (this->getAbsCoor ().x,
306                  this->getAbsCoor ().y,
307                  this->getAbsCoor ().z);
[6004]308    Vector tmpRot = this->getAbsDir().getSpacialAxis();
309    glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
[2043]310
[6004]311
312    // This Draws the LOD's
[6002]313    float cameraDistance = (State::getCamera()->getAbsCoor() - this->getAbsCoor()).len();
[6004]314    if (cameraDistance > 30 && this->models.size() >= 3 && this->models[2] != NULL)
[6002]315    {
[6222]316      this->models[2]->draw();
[6004]317    }
318    else if (cameraDistance > 10 && this->models.size() >= 2 && this->models[1] != NULL)
[6002]319    {
320      this->models[1]->draw();
[6004]321    }
322    else if (this->models.size() >= 1 && this->models[0] != NULL)
[6002]323    {
324      this->models[0]->draw();
325    }
326    glPopMatrix();
327  }
[3803]328}
[3583]329
[6281]330
[5498]331/**
332 * DEBUG-DRAW OF THE BV-Tree.
333 * @param depth What depth to draw
334 * @param drawMode the mode to draw this entity under
335 */
[5501]336void WorldEntity::drawBVTree(unsigned int depth, int drawMode) const
[4684]337{
338  glMatrixMode(GL_MODELVIEW);
339  glPushMatrix();
340  /* translate */
341  glTranslatef (this->getAbsCoor ().x,
342                this->getAbsCoor ().y,
343                this->getAbsCoor ().z);
344  /* rotate */
[4998]345  Vector tmpRot = this->getAbsDir().getSpacialAxis();
346  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
[4684]347
348  if (this->obbTree)
349    this->obbTree->drawBV(depth, drawMode);
350  glPopMatrix();
351}
[6341]352
[6415]353
[6341]354/**
[6415]355 * Debug the WorldEntity
356 */
[6417]357void WorldEntity::debugEntity() const
[6415]358{
359  PRINT(0)("WorldEntity %s::%s  (DEBUG)\n", this->getClassName(), this->getName());
[6417]360  this->debugNode();
361  PRINT(0)("List: %s ; ModelCount %d - ", ObjectManager::OMListToString(this->objectListNumber) , this->models.size());
[6415]362  for (unsigned int i = 0; i < this->models.size(); i++)
363  {
364    if (models[i] != NULL)
365      PRINT(0)(" : %d:%s", i, this->models[i]->getName());
366  }
367  PRINT(0)("\n");
368
369}
370
371
372/**
[6341]373 * Writes data from network containing information about the state
374 * @param data pointer to data
375 * @param length length of data
376 * @param sender hostID of sender
377 */
378int WorldEntity::writeState( const byte * data, int length, int sender )
379{
380  char* modelFileName;
381  SYNCHELP_READ_BEGIN();
382
383  SYNCHELP_READ_FKT( PNode::writeState );
384
385  SYNCHELP_READ_STRINGM( modelFileName );
386  SYNCHELP_READ_FLOAT( scaling );
387  //check if modelFileName is relative to datadir or absolute
[6421]388
389  if ( strcmp(modelFileName, "") )
[6341]390  {
[6421]391
392    if ( strstr(modelFileName, ResourceManager::getInstance()->getDataDir()) )
393   {
394     loadModel( modelFileName+strlen(ResourceManager::getInstance()->getDataDir()), scaling );
395   }
396    else
397   {
398     loadModel( modelFileName, scaling );
399   }
[6341]400  }
401  delete[] modelFileName;
402
[6421]403  SYNCHELP_READ_STRINGM( modelFileName );
404  if ( strcmp(modelFileName, "") )
405    this->md2TextureFileName = modelFileName;
406
[6341]407  return SYNCHELP_READ_N;
408}
409
410/**
411 * data copied in data will bee sent to another host
412 * @param data pointer to data
413 * @param maxLength max length of data
414 * @return the number of bytes writen
415 */
416int WorldEntity::readState( byte * data, int maxLength )
417{
418  SYNCHELP_WRITE_BEGIN();
419
420  SYNCHELP_WRITE_FKT( PNode::readState );
421
422  SYNCHELP_WRITE_STRING( getModel( 0 )->getName() );
423  SYNCHELP_WRITE_FLOAT( scaling );
[6421]424  if ( this->md2TextureFileName!=NULL && strcmp(this->md2TextureFileName, "") )
425  {
426    SYNCHELP_WRITE_STRING(this->md2TextureFileName);
427  }
428  else
429  {
430    SYNCHELP_WRITE_STRING("");
431  }
432
[6341]433  return SYNCHELP_WRITE_N;
434}
Note: See TracBrowser for help on using the repository browser.