Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/spaceshipcontrol/src/world_entities/world_entity.cc @ 6461

Last change on this file since 6461 was 6424, checked in by bensch, 19 years ago

orxonox/trunk: merged the branche network back to the trunk
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/network . -r 6351:HEAD
no conflicts

File size: 11.4 KB
Line 
1
2
3/*
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
15   co-programmer: Christian Meyer
16*/
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
18
19#include "world_entity.h"
20#include "shell_command.h"
21
22#include "model.h"
23#include "md2Model.h"
24#include "resource_manager.h"
25#include "load_param.h"
26#include "vector.h"
27#include "obb_tree.h"
28
29#include "state.h"
30
31using namespace std;
32
33SHELL_COMMAND(model, WorldEntity, loadModel)
34    ->describe("sets the Model of the WorldEntity")
35    ->defaultValues(2, "models/ships/fighter.obj", 1.0);
36
37SHELL_COMMAND(debugEntity, WorldEntity, debugWE);
38
39/**
40 *  Loads the WordEntity-specific Part of any derived Class
41 *
42 * @param root: Normally NULL, as the Derived Entities define a loadParams Function themeselves,
43 *              that can calls WorldEntities loadParams for itself.
44 */
45WorldEntity::WorldEntity(const TiXmlElement* root)
46  : Synchronizeable()
47{
48  this->setClassID(CL_WORLD_ENTITY, "WorldEntity");
49
50  this->obbTree = NULL;
51
52  this->md2TextureFileName = NULL;
53
54  if (root != NULL)
55    this->loadParams(root);
56
57  this->setVisibiliy(true);
58
59  this->objectListNumber = OM_INIT;
60  this->objectListIterator = NULL;
61
62  this->toList(OM_NULL);
63}
64
65/**
66 *  standard destructor
67*/
68WorldEntity::~WorldEntity ()
69{
70  // Delete the obbTree
71  if( this->obbTree != NULL)
72    delete this->obbTree;
73
74  // Delete the model (unregister it with the ResourceManager)
75  for (unsigned int i = 0; i < this->models.size(); i++)
76    this->setModel(NULL, i);
77
78  State::getObjectManager()->toList(this, OM_INIT);
79}
80
81/**
82 * loads the WorldEntity Specific Parameters.
83 * @param root: the XML-Element to load the Data From
84 */
85void WorldEntity::loadParams(const TiXmlElement* root)
86{
87  // Do the PNode loading stuff
88  static_cast<PNode*>(this)->loadParams(root);
89
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
94  // Model Loading
95  LoadParam(root, "model", this, WorldEntity, loadModel)
96      .describe("the fileName of the model, that should be loaded onto this world-entity. (must be relative to the data-dir)")
97      .defaultValues(3, NULL, 1.0f, 0);
98}
99
100
101/**
102 * loads a Model onto a WorldEntity
103 * @param fileName the name of the model to load
104 * @param scaling the Scaling of the model
105 *
106 * @todo fix this, so it only has one loadModel-Function.
107*/
108void WorldEntity::loadModel(const char* fileName, float scaling, unsigned int modelNumber)
109{
110  this->scaling = scaling;
111  if ( fileName != NULL && strcmp(fileName, "") )
112  {
113   // search for the special character # in the LoadParam
114    if (strchr(fileName, '#') != NULL)
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++)
121      {
122        *depth = 48+(int)i;
123        printf("-------%s\n", lodFile);
124        if (ResourceManager::isInDataDir(lodFile))
125          this->loadModel(lodFile, scaling, i);
126      }
127      return;
128    }
129    if (scaling == 0.0)
130    {
131      scaling = 1.0;
132      PRINTF(1)("YOU GAVE ME A CRAPY SCALE resetting to 1\n");
133    }
134    if(strstr(fileName, ".obj"))
135    {
136      PRINTF(4)("fetching OBJ file: %s\n", fileName);
137      if (scaling == 1.0)
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    }
152  }
153  else
154  {
155    this->setModel(NULL);
156  }
157}
158
159/**
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{
166  if (this->models.size() <= modelNumber)
167    this->models.resize(modelNumber+1, NULL);
168
169  if (this->models[modelNumber] != NULL)
170  {
171    Resource* resource = ResourceManager::getInstance()->locateResourceByPointer(this->models[modelNumber]);
172//     if (resource != NULL)
173    ResourceManager::getInstance()->unload(resource, RP_LEVEL);
174  }
175  else
176    delete this->models[modelNumber];
177
178  this->models[modelNumber] = model;
179
180
181//   if (this->model != NULL)
182//     this->buildObbTree(4);
183}
184
185
186/**
187 * builds the obb-tree
188 * @param depth the depth to calculate
189 */
190bool WorldEntity::buildObbTree(unsigned int depth)
191{
192  if (this->obbTree)
193    delete this->obbTree;
194
195  if (this->models[0] != NULL)
196  {
197    PRINTF(4)("creating obb tree\n");
198
199
200    this->obbTree = new OBBTree(depth, (sVec3D*)this->models[0]->getVertexArray(), this->models[0]->getVertexCount());
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  }
209}
210
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}
224
225
226
227/**
228 * sets the character attributes of a worldentity
229 * @param character attributes
230 *
231 * these attributes don't have to be set, only use them, if you need them
232*/
233//void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr)
234//{}
235
236
237/**
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{
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   */
253//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
254}
255
256
257/**
258 *  this is called immediately after the Entity has been constructed, initialized and then Spawned into the World
259 *
260 */
261void WorldEntity::postSpawn ()
262{
263}
264
265
266/**
267 *  this method is called by the world if the WorldEntity leaves valid gamespace
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).
271 *
272 * NOT YET IMPLEMENTED
273 */
274void WorldEntity::leftWorld ()
275{
276}
277
278
279/**
280 *  this method is called every frame
281 * @param time: the time in seconds that has passed since the last tick
282 *
283 * Handle all stuff that should update with time inside this method (movement, animation, etc.)
284*/
285void WorldEntity::tick(float time)
286{
287}
288
289
290/**
291 *  the entity is drawn onto the screen with this function
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.
295*/
296void WorldEntity::draw() const
297{
298    //PRINTF(0)("(%s::%s)\n", this->getClassName(), this->getName());
299  //  assert(!unlikely(this->models.empty()));
300  {
301    glMatrixMode(GL_MODELVIEW);
302    glPushMatrix();
303
304    /* translate */
305    glTranslatef (this->getAbsCoor ().x,
306                  this->getAbsCoor ().y,
307                  this->getAbsCoor ().z);
308    Vector tmpRot = this->getAbsDir().getSpacialAxis();
309    glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
310
311
312    // This Draws the LOD's
313    float cameraDistance = (State::getCamera()->getAbsCoor() - this->getAbsCoor()).len();
314    if (cameraDistance > 30 && this->models.size() >= 3 && this->models[2] != NULL)
315    {
316      this->models[2]->draw();
317    }
318    else if (cameraDistance > 10 && this->models.size() >= 2 && this->models[1] != NULL)
319    {
320      this->models[1]->draw();
321    }
322    else if (this->models.size() >= 1 && this->models[0] != NULL)
323    {
324      this->models[0]->draw();
325    }
326    glPopMatrix();
327  }
328}
329
330
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 */
336void WorldEntity::drawBVTree(unsigned int depth, int drawMode) const
337{
338  glMatrixMode(GL_MODELVIEW);
339  glPushMatrix();
340  /* translate */
341  glTranslatef (this->getAbsCoor ().x,
342                this->getAbsCoor ().y,
343                this->getAbsCoor ().z);
344  /* rotate */
345  Vector tmpRot = this->getAbsDir().getSpacialAxis();
346  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
347
348  if (this->obbTree)
349    this->obbTree->drawBV(depth, drawMode);
350  glPopMatrix();
351}
352
353
354/**
355 * Debug the WorldEntity
356 */
357void WorldEntity::debugEntity() const
358{
359  PRINT(0)("WorldEntity %s::%s  (DEBUG)\n", this->getClassName(), this->getName());
360  this->debugNode();
361  PRINT(0)("List: %s ; ModelCount %d - ", ObjectManager::OMListToString(this->objectListNumber) , this->models.size());
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/**
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
388
389  if ( strcmp(modelFileName, "") )
390  {
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    }
400  }
401  delete[] modelFileName;
402
403  SYNCHELP_READ_STRINGM( modelFileName );
404  if ( strcmp(modelFileName, "") )
405    this->md2TextureFileName = modelFileName;
406
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 );
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
433  return SYNCHELP_WRITE_N;
434}
Note: See TracBrowser for help on using the repository browser.