Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6686 was 6679, checked in by bensch, 19 years ago

orxonox/trunk: WeaponManager seems to work, better than before.

File size: 17.3 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.01;
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     accel -= (this->getAbsDirX())*0.5*acceleration;
370   }
371
372  if( this->bLeft/* > -this->getRelCoor().z*2*/)
373  {
374    this->shiftDir(Quaternion(time, Vector(0,1,0)));
375//    accel -= rightDirection;
376    //velocityDir.normalize();
377    //rot +=Vector(1,0,0);
378    //rotVal -= .4;
379  }
380  if( this->bRight /* > this->getRelCoor().z*2*/)
381  {
382    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
383
384    //    accel += rightDirection;
385    //velocityDir.normalize();
386    //rot += Vector(1,0,0);
387    //rotVal += .4;
388  }
389
390
391  if( this->bRollL /* > -this->getRelCoor().z*2*/)
392  {
393    mouseDir *= Quaternion(-time*2, Vector(1,0,0));
394//    accel -= rightDirection;
395    //velocityDir.normalize();
396    //rot +=Vector(1,0,0);
397    //rotVal -= .4;
398  }
399  if( this->bRollR /* > this->getRelCoor().z*2*/)
400  {
401    mouseDir *= Quaternion(time*2, Vector(1,0,0));
402
403    //    accel += rightDirection;
404    //velocityDir.normalize();
405    //rot += Vector(1,0,0);
406    //rotVal += .4;
407  }
408  if (this->bAscend )
409  {
410    this->shiftDir(Quaternion(time, Vector(0,0,1)));
411
412//    accel += upDirection;
413    //velocityDir.normalize();
414    //rot += Vector(0,0,1);
415    //rotVal += .4;
416  }
417  if (this->bDescend )
418  {
419    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
420
421    //    accel -= upDirection;
422    //velocityDir.normalize();
423    //rot += Vector(0,0,1);
424    //rotVal -= .4;
425  }
426
427  velocity += accel;
428  //rot.normalize();
429  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
430}
431
432/**
433 * weapon manipulation by the player
434*/
435void SpaceShip::weaponAction()
436{
437  if( this->bFire)
438    {
439      this->getWeaponManager()->fire();
440    }
441}
442
443/**
444 * @todo switch statement ??
445 */
446void SpaceShip::process(const Event &event)
447{
448  if( event.type == KeyMapper::PEV_LEFT)
449      this->bRollL = event.bPressed;
450  else if( event.type == KeyMapper::PEV_RIGHT)
451      this->bRollR = event.bPressed;
452  else if( event.type == KeyMapper::PEV_FIRE1)
453      this->bFire = event.bPressed;
454  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
455  {
456    this->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
457  }
458  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
459    this->previousWeaponConfig();
460  else if( event.type == KeyMapper::PEV_UP)
461    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
462  else if( event.type == KeyMapper::PEV_DOWN)
463    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
464  else if( event.type == EV_MOUSE_MOTION)
465  {
466    this->xMouse += event.xRel;
467    this->yMouse += event.yRel;
468  }
469}
470
471#include "weapons/aiming_turret.h"
472// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
473void SpaceShip::ADDWEAPON()
474{
475  Weapon* turret = NULL;
476
477  if ((float)rand()/RAND_MAX < .9)
478  {
479    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
480    {
481      turret = new Turret();
482      this->addWeapon(turret, 2);
483      this->getWeaponManager()->changeWeaponConfig(2);
484    }
485  }
486  else
487  {
488    //if (this->getWeaponManager()->hasFreeSlot(3))
489    {
490      turret = dynamic_cast<Weapon*>(Factory::fabricate(CL_TARGETING_TURRET));
491      if (turret != NULL)
492      this->addWeapon(turret, 3);
493
494      this->getWeaponManager()->changeWeaponConfig(3);
495    }
496  }
497
498  if(turret != NULL)
499  {
500    turret->setName("Turret");
501    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
502  }
503}
504
505#define MASK_bUp         1
506#define MASK_bDown       2
507#define MASK_bLeft       4
508#define MASK_bRight      8
509#define MASK_bAscend    16
510#define MASK_bDescend   32
511#define MASK_bFire      64
512#define MASK_bRollL    128
513#define MASK_bRollR    256
514
515#define DATA_state       1
516#define DATA_flags       2
517#define DATA_mouse       3
518
519int SpaceShip::writeBytes( const byte * data, int length, int sender )
520{
521  SYNCHELP_READ_BEGIN();
522
523  byte b;
524  SYNCHELP_READ_BYTE( b );
525
526  if ( b == DATA_state /*&& (this->getHostID()!=this->getOwner() || sender==0)*/ )
527  {
528    PRINTF(0)("GOT STATE %d\n", this->getUniqueID());
529    setRequestedSync( false );
530    setIsOutOfSync( false );
531    SYNCHELP_READ_FKT( WorldEntity::writeState );
532    //SYNCHELP_READ_FLOAT( cycle );
533
534    return SYNCHELP_READ_N;
535  }
536
537
538  if ( b == DATA_flags /*&& this->getHostID()!=this->getOwner()*/ )
539  {
540    int flags = 0;
541    SYNCHELP_READ_INT( flags );
542
543    bUp = (flags & MASK_bUp) != 0;
544    bDown = (flags & MASK_bDown) != 0;
545    bLeft = (flags & MASK_bLeft) != 0;
546    bRight = (flags & MASK_bRight) != 0;
547    bAscend = (flags & MASK_bAscend) != 0;
548    bDescend = (flags & MASK_bDescend) != 0;
549    bFire = (flags & MASK_bFire) != 0;
550    bRollL = (flags & MASK_bRollL) != 0;
551    bRollR = (flags & MASK_bRollR) != 0;
552
553  }
554
555  /*if ( b == DATA_mouse && this->getHostID()!=this->getOwner() )
556  {
557    SYNCHELP_READ_FLOAT( xMouse );
558    SYNCHELP_READ_FLOAT( yMouse );
559    SYNCHELP_READ_FLOAT( mouseSensitivity );
560    SYNCHELP_READ_FLOAT( cycle );
561}*/
562
563  if ( this->getOwner() != this->getHostID() )
564    SYNCHELP_READ_FKT( PNode::writeSync );
565
566  return SYNCHELP_READ_N;
567}
568
569
570
571int SpaceShip::readBytes( byte * data, int maxLength, int * reciever )
572{
573  SYNCHELP_WRITE_BEGIN();
574
575  if ( isOutOfSync() && !requestedSync() /*&& this->getHostID()!=this->getOwner()*/ )
576  {
577    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
578    setRequestedSync( true );
579    PRINTF(0)("REQUESTED STATE %d\n", this->getUniqueID());
580  }
581
582  int rec = this->getRequestSync();
583  if ( rec > 0 )
584  {
585    *reciever = rec;
586
587    PRINTF(0)("SEND STATE %d %d\n", this->getUniqueID(), rec);
588
589    SYNCHELP_WRITE_BYTE( (byte)DATA_state );
590
591    SYNCHELP_WRITE_FKT( WorldEntity::readState );
592    //SYNCHELP_WRITE_FLOAT( cycle );
593
594    return SYNCHELP_WRITE_N;
595  }
596
597  *reciever = 0;
598
599  if ( this->getHostID()==this->getOwner() )
600  {
601    int mask = 0;
602
603    if ( bUp )
604      mask |= MASK_bUp;
605    if ( bDown )
606      mask |= MASK_bDown;
607    if ( bLeft )
608      mask |= MASK_bLeft;
609    if ( bRight )
610      mask |= MASK_bRight;
611    if ( bAscend )
612      mask |= MASK_bAscend;
613    if ( bFire )
614      mask |= MASK_bFire;
615    if ( bRollL )
616      mask |= MASK_bRollL;
617    if ( bRollR )
618      mask |= MASK_bRollR;
619
620
621    //static float oldxMouse = xMouse + 1.0;
622    //static float oldyMouse = yMouse + 1.0;
623
624    if ( mask != oldMask )
625    {
626      oldMask = mask;
627      SYNCHELP_WRITE_BYTE( DATA_flags );
628      SYNCHELP_WRITE_INT( mask );
629    }
630    else
631    {
632      SYNCHELP_WRITE_BYTE( 0 );
633    }
634
635    /*if ( oldxMouse != xMouse || oldyMouse != yMouse )
636    {
637      oldxMouse = xMouse;
638      oldyMouse = yMouse;
639      SYNCHELP_WRITE_BYTE( DATA_mouse );
640      SYNCHELP_WRITE_FLOAT( xMouse );
641      SYNCHELP_WRITE_FLOAT( yMouse );
642      SYNCHELP_WRITE_FLOAT( mouseSensitivity );
643      SYNCHELP_WRITE_FLOAT( cycle );
644    }*/
645  }
646  else
647  {
648    SYNCHELP_WRITE_BYTE( 0 );
649  }
650
651  if ( this->getOwner() == this->getHostID() )
652    SYNCHELP_WRITE_FKT( PNode::readSync );
653
654  return SYNCHELP_WRITE_N;
655}
Note: See TracBrowser for help on using the repository browser.