Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/spaceshipcontrol/src/world_entities/space_ships/space_ship.cc @ 6723

Last change on this file since 6723 was 6723, checked in by snellen, 18 years ago

aim.cc updated

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