Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

some more entities should sync now

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