Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6693 was 6693, checked in by patrick, 18 years ago

branches: removed spaceshipcontrol branche

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