Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/space_ships/space_ship.cc @ 6637

Last change on this file since 6637 was 6637, checked in by bensch, 18 years ago

orxonox/trunk: merged the SpaceShipControl branche back to the trunk

merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/spaceshipcontrol . -r6482:HEAD

minor conflicts, and added file hover.cc/h to the Makefile.

File size: 17.4 KB
Line 
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
13   co-programmer: Silvan Nellen
14
15*/
16
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
18
19#include "executor/executor.h"
20#include "space_ship.h"
21
22#include "objModel.h"
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"
32#include "event_handler.h"
33
34#include "network_game_manager.h"
35
36#include "power_ups/weapon_power_up.h"
37#include "power_ups/param_power_up.h"
38
39#include "graphics_engine.h"
40
41#include "plane.h"
42
43#include "state.h"
44#include "player.h"
45
46
47// #include "lib/gui/gl_gui/glgui_bar.h"
48// #include "lib/gui/gl_gui/glgui_pushbutton.h"
49
50
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);
99
100  //weapons:
101  Weapon* wpRight = new TestGun(0);
102  wpRight->setName("testGun Right");
103  Weapon* wpLeft = new TestGun(1);
104  wpLeft->setName("testGun Left");
105  Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON));
106
107  cannon->setName("BFG");
108
109  this->addWeapon(wpLeft, 1, 0);
110  this->addWeapon(wpRight,1 ,1);
111  this->addWeapon(cannon, 0, 6);
112
113  //this->addWeapon(turret, 3, 0);
114
115  this->getWeaponManager()->changeWeaponConfig(1);
116}
117
118
119/**
120 * initializes a Spaceship
121 */
122void SpaceShip::init()
123{
124//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
125  this->setClassID(CL_SPACE_SHIP, "SpaceShip");
126
127  PRINTF(4)("SPACESHIP INIT\n");
128
129  EventHandler::getInstance()->grabEvents(true);
130
131  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
132  bFire = false;
133  xMouse = yMouse = 0;
134  yInvert = 1;
135  mouseSensitivity = 0.001;
136  airViscosity = 0.01;
137  controlVelocityX = 25;
138  controlVelocityY = 150;
139  shipInertia = 0.5             ;
140//  cycle = 0.0;
141
142  this->setMaxEnergy(100);
143  this->setEnergy(80);
144
145  travelSpeed = 40.0;
146  acceleration = 3;
147  this->velocity = this->getAbsDirX()*travelSpeed;
148  this->mouseDir = this->getAbsDir();
149  this->pitchDir = this->getAbsDir();
150
151//   GLGuiButton* button = new GLGuiPushButton();
152//    button->show();
153//    button->setLabel("orxonox");
154//    button->setBindNode(this);
155//     GLGuiBar* bar = new GLGuiBar();
156//     bar->show();
157//     bar->setValue(7.0);
158//     bar->setMaximum(10);
159//     bar->setSize2D( 20, 100);
160//     bar->setAbsCoor2D( 10, 200);
161
162  //add events to the eventlist
163  registerEvent(KeyMapper::PEV_UP);
164  registerEvent(KeyMapper::PEV_DOWN);
165  registerEvent(KeyMapper::PEV_LEFT);
166  registerEvent(KeyMapper::PEV_RIGHT);
167  //registerEvent(SDLK_q);
168  //registerEvent(SDLK_e);
169  registerEvent(KeyMapper::PEV_FIRE1);
170  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
171  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
172  //registerEvent(SDLK_PAGEUP);
173  //registerEvent(SDLK_PAGEDOWN);
174  registerEvent(EV_MOUSE_MOTION);
175
176  this->getWeaponManager()->setSlotCount(7);
177
178  this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0));
179  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
180
181  this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0));
182  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
183
184  this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5));
185  this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
186
187  this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5));
188  this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
189
190  this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5));
191  this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
192
193  this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5));
194  this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
195//
196   this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
197   this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
198   //
199//   this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
200//   this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
201//
202//   this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
203//   this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
204
205  this->getWeaponManager()->getFixedTarget()->setParent(this);
206  this->getWeaponManager()->getFixedTarget()->setRelCoor(100000,0,0);
207
208  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
209}
210
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{
217  WorldEntity::loadParams(root);
218}
219
220
221void SpaceShip::enter()
222{
223  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
224  this->attachCamera();
225}
226
227void SpaceShip::leave()
228{
229  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
230  this->detachCamera();
231}
232
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{
257  Playable::collidesWith(entity, location);
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{
271  WorldEntity::draw();
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{
283  this->getWeaponManager()->tick(time);
284  // weapon system manipulation
285  this->weaponAction();
286
287  if( xMouse != 0 || yMouse != 0)
288   {
289    if (xMouse > controlVelocityX) xMouse = controlVelocityX;
290    else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX;
291    if (yMouse > controlVelocityY) yMouse = controlVelocityY;
292    else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY;
293   
294    pitchDir = (Quaternion(xMouse*mouseSensitivity*0.5, Vector(1,0,0)));
295   
296    mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity*yInvert, Vector(0,0,1))*pitchDir);
297    xMouse = yMouse = 0;
298   }
299
300
301//   if( this != State::getPlayer()->getControllable())
302//     return;
303
304  // spaceship controlled movement
305  this->calculateVelocity(time);
306
307  Vector move = velocity*time;
308 
309  //orient the velocity in the direction of the spaceship.
310  travelSpeed = velocity.len();
311  velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity;
312  velocity = (velocity.getNormalized())*travelSpeed;
313
314  //orient the spaceship in direction of the mouse
315   rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, fabsf(time)*shipInertia);
316   if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
317    this->setAbsDir( rotQuat);
318   //this->setAbsDirSoft(mouseDir,5);
319
320  // this is the air friction (necessary for a smooth control)
321  if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001;
322  else if (travelSpeed <= 80) velocity -= velocity.getNormalized()*travelSpeed*0.001;
323 
324  //other physics (gravity)
325  if(travelSpeed < 120)
326  move += Vector(0,-1,0)*60*time + Vector(0,1,0)*travelSpeed/2*time;
327   
328  //hoover effect
329  //cycle += time;
330  //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
331
332  //readjust
333  //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
334  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
335
336  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
337
338  this->shiftCoor(move);
339
340
341}
342
343/**
344 *  calculate the velocity
345 * @param time the timeslice since the last frame
346*/
347void SpaceShip::calculateVelocity (float time)
348{
349  Vector accel(0.0, 0.0, 0.0);
350  /*
351  Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter
352  */
353  //float rotVal = 0.0;
354  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
355  /* calculate the direction in which the craft is heading  */
356
357  //Plane plane(Vector(0,1,0), Vector(0,0,0));
358
359  if( this->bUp )
360   {
361     //this->shiftCoor(this->getAbsDirX());
362      //accel += (this->getAbsDirX())*2;
363      accel += (this->getAbsDirX())*acceleration;
364
365   }
366
367  if( this->bDown )
368   {
369     //this->shiftCoor((this->getAbsDirX())*-1);
370     //accel -= (this->getAbsDirX())*2;
371     accel -= (this->getAbsDirX())*0.5*acceleration;
372   }
373
374  if( this->bLeft/* > -this->getRelCoor().z*2*/)
375  {
376    this->shiftDir(Quaternion(time, Vector(0,1,0)));
377//    accel -= rightDirection;
378    //velocityDir.normalize();
379    //rot +=Vector(1,0,0);
380    //rotVal -= .4;
381  }
382  if( this->bRight /* > this->getRelCoor().z*2*/)
383  {
384    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
385
386    //    accel += rightDirection;
387    //velocityDir.normalize();
388    //rot += Vector(1,0,0);
389    //rotVal += .4;
390  }
391
392
393  if( this->bRollL /* > -this->getRelCoor().z*2*/)
394  {
395    mouseDir *= Quaternion(-time*2, Vector(1,0,0));
396//    accel -= rightDirection;
397    //velocityDir.normalize();
398    //rot +=Vector(1,0,0);
399    //rotVal -= .4;
400  }
401  if( this->bRollR /* > this->getRelCoor().z*2*/)
402  {
403    mouseDir *= Quaternion(time*2, Vector(1,0,0));
404
405    //    accel += rightDirection;
406    //velocityDir.normalize();
407    //rot += Vector(1,0,0);
408    //rotVal += .4;
409  }
410  if (this->bAscend )
411  {
412    this->shiftDir(Quaternion(time, Vector(0,0,1)));
413
414//    accel += upDirection;
415    //velocityDir.normalize();
416    //rot += Vector(0,0,1);
417    //rotVal += .4;
418  }
419  if (this->bDescend )
420  {
421    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
422
423    //    accel -= upDirection;
424    //velocityDir.normalize();
425    //rot += Vector(0,0,1);
426    //rotVal -= .4;
427  }
428
429  velocity += accel;
430  //rot.normalize();
431  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
432}
433
434/**
435 * weapon manipulation by the player
436*/
437void SpaceShip::weaponAction()
438{
439  if( this->bFire)
440    {
441      this->getWeaponManager()->fire();
442    }
443}
444
445/**
446 * @todo switch statement ??
447 */
448void SpaceShip::process(const Event &event)
449{
450  if( event.type == KeyMapper::PEV_LEFT)
451      this->bRollL = event.bPressed;
452  else if( event.type == KeyMapper::PEV_RIGHT)
453      this->bRollR = event.bPressed;
454  else if( event.type == KeyMapper::PEV_FIRE1)
455      this->bFire = event.bPressed;
456  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
457  {
458    this->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
459  }
460  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
461    this->previousWeaponConfig();
462  else if( event.type == KeyMapper::PEV_UP)
463    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
464  else if( event.type == KeyMapper::PEV_DOWN)
465    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
466  else if( event.type == EV_MOUSE_MOTION)
467  {
468    this->xMouse += event.xRel;
469    this->yMouse += event.yRel;
470  }
471}
472
473#include "weapons/aiming_turret.h"
474// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
475void SpaceShip::ADDWEAPON()
476{
477  Weapon* turret = NULL;
478
479  if ((float)rand()/RAND_MAX < .9)
480  {
481    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
482    {
483      turret = new Turret();
484      this->addWeapon(turret, 2);
485      this->getWeaponManager()->changeWeaponConfig(2);
486    }
487  }
488  else
489  {
490    //if (this->getWeaponManager()->hasFreeSlot(3))
491    {
492      turret = dynamic_cast<Weapon*>(Factory::fabricate(CL_TARGETING_TURRET));
493      if (turret != NULL)
494      this->addWeapon(turret, 3);
495
496      this->getWeaponManager()->changeWeaponConfig(3);
497    }
498  }
499
500  if(turret != NULL)
501  {
502    turret->setName("Turret");
503    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
504  }
505}
506
507#define MASK_bUp         1
508#define MASK_bDown       2
509#define MASK_bLeft       4
510#define MASK_bRight      8
511#define MASK_bAscend    16
512#define MASK_bDescend   32
513#define MASK_bFire      64
514#define MASK_bRollL    128
515#define MASK_bRollR    256
516
517#define DATA_state       1
518#define DATA_flags       2
519#define DATA_mouse       3
520
521int SpaceShip::writeBytes( const byte * data, int length, int sender )
522{
523  SYNCHELP_READ_BEGIN();
524
525  byte b;
526  SYNCHELP_READ_BYTE( b );
527
528  if ( b == DATA_state /*&& (this->getHostID()!=this->getOwner() || sender==0)*/ )
529  {
530    PRINTF(0)("GOT STATE %d\n", this->getUniqueID());
531    setRequestedSync( false );
532    setIsOutOfSync( false );
533    SYNCHELP_READ_FKT( WorldEntity::writeState );
534    //SYNCHELP_READ_FLOAT( cycle );
535
536    return SYNCHELP_READ_N;
537  }
538
539
540  if ( b == DATA_flags /*&& this->getHostID()!=this->getOwner()*/ )
541  {
542    int flags = 0;
543    SYNCHELP_READ_INT( flags );
544
545    bUp = (flags & MASK_bUp) != 0;
546    bDown = (flags & MASK_bDown) != 0;
547    bLeft = (flags & MASK_bLeft) != 0;
548    bRight = (flags & MASK_bRight) != 0;
549    bAscend = (flags & MASK_bAscend) != 0;
550    bDescend = (flags & MASK_bDescend) != 0;
551    bFire = (flags & MASK_bFire) != 0;
552    bRollL = (flags & MASK_bRollL) != 0;
553    bRollR = (flags & MASK_bRollR) != 0;
554
555  }
556
557  /*if ( b == DATA_mouse && this->getHostID()!=this->getOwner() )
558  {
559    SYNCHELP_READ_FLOAT( xMouse );
560    SYNCHELP_READ_FLOAT( yMouse );
561    SYNCHELP_READ_FLOAT( mouseSensitivity );
562    SYNCHELP_READ_FLOAT( cycle );
563}*/
564
565  if ( this->getOwner() != this->getHostID() )
566    SYNCHELP_READ_FKT( PNode::writeSync );
567
568  return SYNCHELP_READ_N;
569}
570
571
572
573int SpaceShip::readBytes( byte * data, int maxLength, int * reciever )
574{
575  SYNCHELP_WRITE_BEGIN();
576
577  if ( isOutOfSync() && !requestedSync() /*&& this->getHostID()!=this->getOwner()*/ )
578  {
579    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
580    setRequestedSync( true );
581    PRINTF(0)("REQUESTED STATE %d\n", this->getUniqueID());
582  }
583
584  int rec = this->getRequestSync();
585  if ( rec > 0 )
586  {
587    *reciever = rec;
588
589    PRINTF(0)("SEND STATE %d %d\n", this->getUniqueID(), rec);
590
591    SYNCHELP_WRITE_BYTE( (byte)DATA_state );
592
593    SYNCHELP_WRITE_FKT( WorldEntity::readState );
594    //SYNCHELP_WRITE_FLOAT( cycle );
595
596    return SYNCHELP_WRITE_N;
597  }
598
599  *reciever = 0;
600
601  if ( this->getHostID()==this->getOwner() )
602  {
603    int mask = 0;
604
605    if ( bUp )
606      mask |= MASK_bUp;
607    if ( bDown )
608      mask |= MASK_bDown;
609    if ( bLeft )
610      mask |= MASK_bLeft;
611    if ( bRight )
612      mask |= MASK_bRight;
613    if ( bAscend )
614      mask |= MASK_bAscend;
615    if ( bFire )
616      mask |= MASK_bFire;
617    if ( bRollL )
618      mask |= MASK_bRollL;
619    if ( bRollR )
620      mask |= MASK_bRollR;
621
622
623    //static float oldxMouse = xMouse + 1.0;
624    //static float oldyMouse = yMouse + 1.0;
625
626    if ( mask != oldMask )
627    {
628      oldMask = mask;
629      SYNCHELP_WRITE_BYTE( DATA_flags );
630      SYNCHELP_WRITE_INT( mask );
631    }
632    else
633    {
634      SYNCHELP_WRITE_BYTE( 0 );
635    }
636
637    /*if ( oldxMouse != xMouse || oldyMouse != yMouse )
638    {
639      oldxMouse = xMouse;
640      oldyMouse = yMouse;
641      SYNCHELP_WRITE_BYTE( DATA_mouse );
642      SYNCHELP_WRITE_FLOAT( xMouse );
643      SYNCHELP_WRITE_FLOAT( yMouse );
644      SYNCHELP_WRITE_FLOAT( mouseSensitivity );
645      SYNCHELP_WRITE_FLOAT( cycle );
646    }*/
647  }
648  else
649  {
650    SYNCHELP_WRITE_BYTE( 0 );
651  }
652
653  if ( this->getOwner() == this->getHostID() )
654    SYNCHELP_WRITE_FKT( PNode::readSync );
655
656  return SYNCHELP_WRITE_N;
657}
Note: See TracBrowser for help on using the repository browser.