Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: each BaseObject saves its TiXmlElement as a TiXmlNode*.
This will be good for
1.) sync in network
2.) saving entitites

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