Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/world_entities/space_ships/space_ship.cc @ 6688

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

spaceship: hack: load default model

File size: 16.8 KB
RevLine 
[5907]1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Benjamin Knecht
[6034]13   co-programmer: Silvan Nellen
[5907]14
15*/
16
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
18
19#include "executor/executor.h"
20#include "space_ship.h"
21
[6034]22#include "objModel.h"
[5907]23#include "resource_manager.h"
24
25#include "weapons/weapon_manager.h"
26#include "weapons/test_gun.h"
27#include "weapons/turret.h"
28#include "weapons/cannon.h"
29
30#include "factory.h"
31#include "key_mapper.h"
[5978]32#include "event_handler.h"
[5907]33
[6424]34#include "network_game_manager.h"
35
[6243]36#include "power_ups/weapon_power_up.h"
37#include "power_ups/param_power_up.h"
38
[5978]39#include "graphics_engine.h"
40
[6617]41#include "plane.h"
[6287]42
[6634]43#include "state.h"
44#include "player.h"
[6287]45
[6634]46
[6306]47// #include "lib/gui/gl_gui/glgui_bar.h"
48// #include "lib/gui/gl_gui/glgui_pushbutton.h"
[6287]49
50
[5907]51using namespace std;
52
53CREATE_FACTORY(SpaceShip, CL_SPACE_SHIP);
54
55/**
56 *  creates the controlable Spaceship
57 */
58SpaceShip::SpaceShip()
59{
60  this->init();
61}
62
63/**
64 *  destructs the spaceship, deletes alocated memory
65 */
66SpaceShip::~SpaceShip ()
67{
68}
69
70/**
71 * loads a Spaceships information from a specified file.
72 * @param fileName the name of the File to load the spaceship from (absolute path)
73 */
74SpaceShip::SpaceShip(const char* fileName)
75{
76  this->init();
77  TiXmlDocument doc(fileName);
78
79  if(!doc.LoadFile())
80  {
81    PRINTF(2)("Loading file %s failed for spaceship.\n", fileName);
82    return;
83  }
84
85  this->loadParams(doc.RootElement());
86}
87
88/**
89 *  creates a new Spaceship from Xml Data
90 * @param root the xml element containing spaceship data
91
92   @todo add more parameters to load
93*/
94SpaceShip::SpaceShip(const TiXmlElement* root)
95{
96  this->init();
97  if (root != NULL)
98    this->loadParams(root);
[6688]99  else
100  {
101    this->loadModel("models/ships/reap_#.obj");
102  }
103
[6672]104}
[5907]105
[6672]106
107/**
108 * initializes a Spaceship
109 */
110void SpaceShip::init()
111{
112//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
113  this->setClassID(CL_SPACE_SHIP, "SpaceShip");
114
115  PRINTF(4)("SPACESHIP INIT\n");
116
[5907]117  //weapons:
118  Weapon* wpRight = new TestGun(0);
119  wpRight->setName("testGun Right");
120  Weapon* wpLeft = new TestGun(1);
121  wpLeft->setName("testGun Left");
[5982]122  Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON));
[5907]123
124  cannon->setName("BFG");
125
[6443]126  this->addWeapon(wpLeft, 1, 0);
127  this->addWeapon(wpRight,1 ,1);
128  this->addWeapon(cannon, 0, 6);
[5907]129
[6443]130  //this->addWeapon(turret, 3, 0);
[5907]131
132  this->getWeaponManager()->changeWeaponConfig(1);
133
[6547]134  EventHandler::getInstance()->grabEvents(true);
[5907]135
[5978]136  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
[5907]137  bFire = false;
[5978]138  xMouse = yMouse = 0;
139  mouseSensitivity = 0.001;
[6034]140  airViscosity = 1.0;
[5978]141  cycle = 0.0;
[5907]142
[6430]143  this->setMaxEnergy(100);
144  this->setEnergy(80);
[5982]145
[5907]146  travelSpeed = 15.0;
147  this->velocity = Vector(0.0,0.0,0.0);
[6162]148  this->mouseDir = this->getAbsDir();
[5907]149
[6306]150//   GLGuiButton* button = new GLGuiPushButton();
151//    button->show();
152//    button->setLabel("orxonox");
153//    button->setBindNode(this);
154//     GLGuiBar* bar = new GLGuiBar();
155//     bar->show();
156//     bar->setValue(7.0);
157//     bar->setMaximum(10);
158//     bar->setSize2D( 20, 100);
159//     bar->setAbsCoor2D( 10, 200);
[6300]160
[5907]161  //add events to the eventlist
[5978]162  registerEvent(SDLK_w);
163  registerEvent(SDLK_s);
164  registerEvent(SDLK_a);
165  registerEvent(SDLK_d);
166  registerEvent(SDLK_q);
167  registerEvent(SDLK_e);
[5907]168  registerEvent(KeyMapper::PEV_FIRE1);
169  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
170  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
171  registerEvent(SDLK_PAGEUP);
172  registerEvent(SDLK_PAGEDOWN);
[5978]173  registerEvent(EV_MOUSE_MOTION);
[5907]174
175  this->getWeaponManager()->setSlotCount(7);
176
177  this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0));
178  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
179
180  this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0));
181  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
182
183  this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5));
184  this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
185
186  this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5));
187  this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
188
189  this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5));
190  this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
191
192  this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5));
193  this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
194//
195   this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
196   this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
197   //
198//   this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
199//   this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
200//
201//   this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
202//   this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
203
[6162]204  this->getWeaponManager()->getFixedTarget()->setParent(this);
205  this->getWeaponManager()->getFixedTarget()->setRelCoor(100000,0,0);
206
[6222]207  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
[5907]208}
209
[6672]210
[5907]211/**
212 * loads the Settings of a SpaceShip from an XML-element.
213 * @param root the XML-element to load the Spaceship's properties from
214 */
215void SpaceShip::loadParams(const TiXmlElement* root)
216{
[6512]217  WorldEntity::loadParams(root);
[5907]218}
219
220
[6222]221void SpaceShip::enter()
222{
223  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
[6241]224  this->attachCamera();
[6222]225}
226
227void SpaceShip::leave()
228{
229  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
[6241]230  this->detachCamera();
[6222]231}
232
[5907]233
234/**
235 *  effect that occurs after the SpaceShip is spawned
236*/
237void SpaceShip::postSpawn ()
238{
239  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
240}
241
242/**
243 *  the action occuring if the spaceship left the game
244*/
245void SpaceShip::leftWorld ()
246{}
247
248WorldEntity* ref = NULL;
249/**
250 *  this function is called, when two entities collide
251 * @param entity: the world entity with whom it collides
252 *
253 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
254 */
255void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location)
256{
[6436]257  Playable::collidesWith(entity, location);
[5907]258  if (entity->isA(CL_TURRET_POWER_UP) && entity != ref)
259  {
260    this->ADDWEAPON();
261    ref = entity;
262    }
263//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
264}
265
266/**
267 *  draws the spaceship after transforming it.
268*/
269void SpaceShip::draw () const
270{
[6281]271  WorldEntity::draw();
[5907]272  this->getWeaponManager()->draw();
273
274  //this->debug(0);
275}
276
277/**
278 *  the function called for each passing timeSnap
279 * @param time The timespan passed since last update
280*/
281void SpaceShip::tick (float time)
282{
[6634]283  this->getWeaponManager()->tick(time);
284  // weapon system manipulation
285  this->weaponAction();
[6034]286
[6634]287//   if( this != State::getPlayer()->getControllable())
288//     return;
289
[5907]290  // spaceship controlled movement
291  this->calculateVelocity(time);
292
293  Vector move = (velocity)*time;
294
[6034]295  //orient the velocity in the direction of the spaceship.
296  travelSpeed = velocity.len();
297  velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity;
298  velocity = (velocity.getNormalized())*travelSpeed;
[6222]299
[6162]300  //orient the spaceship in direction of the mouse
[6241]301   rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, fabsf(time)*3.0);
302   if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
[6162]303    this->setAbsDir( rotQuat);
304   //this->setAbsDirSoft(mouseDir,5);
[6222]305
[5978]306  // this is the air friction (necessary for a smooth control)
307  if(velocity.len() != 0) velocity -= velocity*0.01;
[5982]308
[6034]309  //hoover effect
[6634]310  //cycle += time;
311  //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
[5982]312
[6034]313  //readjust
[6430]314
[6426]315  /*
316    In the game "Yager" the spaceship gets readjusted when the player moves the mouse.
317    I (bknecht) go and check it out how they do it, we could probably use this also in Orxonox.
318  */
[6430]319  //if (xMouse != 0 && yMouse != 0)
320
[6426]321  //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
[6034]322  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
323
[5978]324  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
[5907]325
326  this->shiftCoor (move);
327
[6634]328
[5907]329}
330
331/**
332 *  calculate the velocity
333 * @param time the timeslice since the last frame
334*/
335void SpaceShip::calculateVelocity (float time)
336{
337  Vector accel(0.0, 0.0, 0.0);
[6162]338  /*
339  Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter
340  */
[5907]341  //float rotVal = 0.0;
[5912]342  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
[5907]343  /* calculate the direction in which the craft is heading  */
344
[6222]345  Plane plane(Vector(0,1,0), Vector(0,0,0));
346
[5907]347  if( this->bUp )
348   {
[5978]349     //this->shiftCoor(this->getAbsDirX());
350      accel += (this->getAbsDirX())*2;
[6222]351
[6162]352      /* Heli-Steuerung
353         accel += (this->getAbsDirX()*2;
354         if(
355      */
[5907]356   }
357
358  if( this->bDown )
[5908]359   {
[5978]360     //this->shiftCoor((this->getAbsDirX())*-1);
361     accel -= (this->getAbsDirX())*2;
[5907]362   }
363
[6034]364  if( this->bLeft/* > -this->getRelCoor().z*2*/)
[5907]365  {
[5912]366    this->shiftDir(Quaternion(time, Vector(0,1,0)));
367//    accel -= rightDirection;
[5908]368    //velocityDir.normalize();
[5907]369    //rot +=Vector(1,0,0);
370    //rotVal -= .4;
371  }
372  if( this->bRight /* > this->getRelCoor().z*2*/)
373  {
[5912]374    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
375
376    //    accel += rightDirection;
[5908]377    //velocityDir.normalize();
[5907]378    //rot += Vector(1,0,0);
379    //rotVal += .4;
380  }
[5982]381
[6034]382
[5978]383  if( this->bRollL /* > -this->getRelCoor().z*2*/)
384  {
[6162]385    mouseDir *= Quaternion(-time, Vector(1,0,0));
[5978]386//    accel -= rightDirection;
387    //velocityDir.normalize();
388    //rot +=Vector(1,0,0);
389    //rotVal -= .4;
390  }
391  if( this->bRollR /* > this->getRelCoor().z*2*/)
392  {
[6162]393    mouseDir *= Quaternion(time, Vector(1,0,0));
[5978]394
395    //    accel += rightDirection;
396    //velocityDir.normalize();
397    //rot += Vector(1,0,0);
398    //rotVal += .4;
399  }
[5907]400  if (this->bAscend )
401  {
[5912]402    this->shiftDir(Quaternion(time, Vector(0,0,1)));
403
404//    accel += upDirection;
[5908]405    //velocityDir.normalize();
[5907]406    //rot += Vector(0,0,1);
407    //rotVal += .4;
408  }
409  if (this->bDescend )
410  {
[5912]411    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
412
413    //    accel -= upDirection;
[5908]414    //velocityDir.normalize();
[5907]415    //rot += Vector(0,0,1);
416    //rotVal -= .4;
417  }
418
[5978]419  velocity += accel;
[5907]420  //rot.normalize();
421  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
422}
423
424/**
425 * weapon manipulation by the player
426*/
427void SpaceShip::weaponAction()
428{
429  if( this->bFire)
430    {
431      this->getWeaponManager()->fire();
432    }
433}
434
435/**
436 * @todo switch statement ??
437 */
438void SpaceShip::process(const Event &event)
439{
[5982]440
441
[5978]442  if( event.type == SDLK_a)
443      this->bRollL = event.bPressed;
444  else if( event.type == SDLK_d)
445      this->bRollR = event.bPressed;
[5907]446  else if( event.type == KeyMapper::PEV_FIRE1)
447      this->bFire = event.bPressed;
448  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
[6443]449  {
[6444]450    this->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
[6443]451  }
[5907]452  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
[6444]453    this->previousWeaponConfig();
[5978]454  else if( event.type == SDLK_w)
455    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
456  else if( event.type == SDLK_s)
457    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
458  else if( event.type == EV_MOUSE_MOTION)
459  {
460    this->xMouse = event.xRel;
461    this->yMouse = event.yRel;
[6162]462    mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
[6241]463   // if( xMouse*xMouse + yMouse*yMouse < 0.9)
464     //this->setAbsDir(mouseDir);
[5978]465  }
[5907]466}
467
468#include "weapons/aiming_turret.h"
469// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
470void SpaceShip::ADDWEAPON()
471{
472  Weapon* turret = NULL;
473
[6306]474  if ((float)rand()/RAND_MAX < .9)
[5907]475  {
476    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
477    {
478      turret = new Turret();
[6443]479      this->addWeapon(turret, 2);
[5907]480      this->getWeaponManager()->changeWeaponConfig(2);
481    }
482  }
483  else
484  {
485    //if (this->getWeaponManager()->hasFreeSlot(3))
486    {
[6426]487      turret = dynamic_cast<Weapon*>(Factory::fabricate(CL_TARGETING_TURRET));
488      if (turret != NULL)
[6443]489      this->addWeapon(turret, 3);
[5907]490
491      this->getWeaponManager()->changeWeaponConfig(3);
492    }
493  }
494
495  if(turret != NULL)
496  {
497    turret->setName("Turret");
498    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
499  }
500}
[6424]501
[6498]502#define MASK_bUp         1
503#define MASK_bDown       2
504#define MASK_bLeft       4
505#define MASK_bRight      8
506#define MASK_bAscend    16
507#define MASK_bDescend   32
508#define MASK_bFire      64
509#define MASK_bRollL    128
510#define MASK_bRollR    256
[6424]511
[6498]512#define DATA_state       1
513#define DATA_flags       2
514#define DATA_mouse       3
515
[6424]516int SpaceShip::writeBytes( const byte * data, int length, int sender )
517{
518  SYNCHELP_READ_BEGIN();
519
[6498]520  byte b;
521  SYNCHELP_READ_BYTE( b );
[6424]522
[6634]523  if ( b == DATA_state /*&& (this->getHostID()!=this->getOwner() || sender==0)*/ )
[6498]524  {
[6634]525    PRINTF(0)("GOT STATE %d\n", this->getUniqueID());
[6498]526    setRequestedSync( false );
527    setIsOutOfSync( false );
528    SYNCHELP_READ_FKT( WorldEntity::writeState );
[6634]529    //SYNCHELP_READ_FLOAT( cycle );
530
531    return SYNCHELP_READ_N;
[6498]532  }
533
[6634]534
535  if ( b == DATA_flags /*&& this->getHostID()!=this->getOwner()*/ )
[6498]536  {
[6634]537    int flags = 0;
[6498]538    SYNCHELP_READ_INT( flags );
539
[6634]540    bUp = (flags & MASK_bUp) != 0;
541    bDown = (flags & MASK_bDown) != 0;
542    bLeft = (flags & MASK_bLeft) != 0;
543    bRight = (flags & MASK_bRight) != 0;
544    bAscend = (flags & MASK_bAscend) != 0;
545    bDescend = (flags & MASK_bDescend) != 0;
546    bFire = (flags & MASK_bFire) != 0;
547    bRollL = (flags & MASK_bRollL) != 0;
548    bRollR = (flags & MASK_bRollR) != 0;
549
[6498]550  }
551
[6634]552  /*if ( b == DATA_mouse && this->getHostID()!=this->getOwner() )
[6498]553  {
554    SYNCHELP_READ_FLOAT( xMouse );
555    SYNCHELP_READ_FLOAT( yMouse );
556    SYNCHELP_READ_FLOAT( mouseSensitivity );
557    SYNCHELP_READ_FLOAT( cycle );
[6634]558}*/
[6498]559
[6634]560  if ( this->getOwner() != this->getHostID() )
561    SYNCHELP_READ_FKT( PNode::writeSync );
562
[6424]563  return SYNCHELP_READ_N;
564}
565
[6498]566
567
[6424]568int SpaceShip::readBytes( byte * data, int maxLength, int * reciever )
569{
[6498]570  SYNCHELP_WRITE_BEGIN();
571
[6634]572  if ( isOutOfSync() && !requestedSync() /*&& this->getHostID()!=this->getOwner()*/ )
[6424]573  {
574    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
575    setRequestedSync( true );
[6634]576    PRINTF(0)("REQUESTED STATE %d\n", this->getUniqueID());
[6424]577  }
578
579  int rec = this->getRequestSync();
580  if ( rec > 0 )
581  {
582    *reciever = rec;
583
[6634]584    PRINTF(0)("SEND STATE %d %d\n", this->getUniqueID(), rec);
585
[6498]586    SYNCHELP_WRITE_BYTE( (byte)DATA_state );
[6424]587
588    SYNCHELP_WRITE_FKT( WorldEntity::readState );
[6634]589    //SYNCHELP_WRITE_FLOAT( cycle );
[6424]590
591    return SYNCHELP_WRITE_N;
592  }
593
594  *reciever = 0;
[6498]595
596  if ( this->getHostID()==this->getOwner() )
597  {
598    int mask = 0;
599
600    if ( bUp )
601      mask |= MASK_bUp;
602    if ( bDown )
603      mask |= MASK_bDown;
604    if ( bLeft )
605      mask |= MASK_bLeft;
606    if ( bRight )
607      mask |= MASK_bRight;
608    if ( bAscend )
609      mask |= MASK_bAscend;
610    if ( bFire )
611      mask |= MASK_bFire;
612    if ( bRollL )
613      mask |= MASK_bRollL;
614    if ( bRollR )
615      mask |= MASK_bRollR;
616
617
[6634]618    //static float oldxMouse = xMouse + 1.0;
619    //static float oldyMouse = yMouse + 1.0;
620
[6498]621    if ( mask != oldMask )
622    {
623      oldMask = mask;
624      SYNCHELP_WRITE_BYTE( DATA_flags );
625      SYNCHELP_WRITE_INT( mask );
626    }
[6634]627    else
628    {
629      SYNCHELP_WRITE_BYTE( 0 );
630    }
[6498]631
[6634]632    /*if ( oldxMouse != xMouse || oldyMouse != yMouse )
[6498]633    {
634      oldxMouse = xMouse;
635      oldyMouse = yMouse;
636      SYNCHELP_WRITE_BYTE( DATA_mouse );
637      SYNCHELP_WRITE_FLOAT( xMouse );
638      SYNCHELP_WRITE_FLOAT( yMouse );
639      SYNCHELP_WRITE_FLOAT( mouseSensitivity );
640      SYNCHELP_WRITE_FLOAT( cycle );
[6634]641    }*/
[6498]642  }
[6634]643  else
644  {
645    SYNCHELP_WRITE_BYTE( 0 );
646  }
[6498]647
[6634]648  if ( this->getOwner() == this->getHostID() )
649    SYNCHELP_WRITE_FKT( PNode::readSync );
650
[6498]651  return SYNCHELP_WRITE_N;
[6424]652}
Note: See TracBrowser for help on using the repository browser.