Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: split Rotation/Line/Quaternion/Plane(Rectangle) into seperate files

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