Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6603 was 6547, checked in by bensch, 19 years ago

orxonox/trunk: merged the WeaponManager back to the trunk

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
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#include "weapons/aiming_turret.h"
455// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
456void SpaceShip::ADDWEAPON()
457{
458  Weapon* turret = NULL;
459
460  if ((float)rand()/RAND_MAX < .9)
461  {
462    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
463    {
464      turret = new Turret();
465      this->addWeapon(turret, 2);
466      this->getWeaponManager()->changeWeaponConfig(2);
467    }
468  }
469  else
470  {
471    //if (this->getWeaponManager()->hasFreeSlot(3))
472    {
473      turret = dynamic_cast<Weapon*>(Factory::fabricate(CL_TARGETING_TURRET));
474      if (turret != NULL)
475      this->addWeapon(turret, 3);
476
477      this->getWeaponManager()->changeWeaponConfig(3);
478    }
479  }
480
481  if(turret != NULL)
482  {
483    turret->setName("Turret");
484    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
485  }
486}
487
488#define MASK_bUp         1
489#define MASK_bDown       2
490#define MASK_bLeft       4
491#define MASK_bRight      8
492#define MASK_bAscend    16
493#define MASK_bDescend   32
494#define MASK_bFire      64
495#define MASK_bRollL    128
496#define MASK_bRollR    256
497
498#define DATA_state       1
499#define DATA_flags       2
500#define DATA_mouse       3
501
502int SpaceShip::writeBytes( const byte * data, int length, int sender )
503{
504  SYNCHELP_READ_BEGIN();
505
506  byte b;
507  SYNCHELP_READ_BYTE( b );
508
509  if ( b == DATA_state && this->getHostID()!=this->getOwner() )
510  {
511    setRequestedSync( false );
512    setIsOutOfSync( false );
513    SYNCHELP_READ_FKT( WorldEntity::writeState );
514    SYNCHELP_READ_FLOAT( cycle );
515  }
516
517  if ( b == DATA_flags && this->getHostID()!=this->getOwner() )
518  {
519    int flags;
520    SYNCHELP_READ_INT( flags );
521
522    bUp = flags & MASK_bUp != 0;
523    bDown = flags & MASK_bDown != 0;
524    bLeft = flags & MASK_bLeft != 0;
525    bRight = flags & MASK_bRight != 0;
526    bAscend = flags & MASK_bAscend != 0;
527    bDescend = flags & MASK_bDescend != 0;
528    bFire = flags & MASK_bFire != 0;
529    bRollL = flags & MASK_bRollL != 0;
530    bRollR = flags & MASK_bRollR != 0;
531  }
532
533  if ( b == DATA_mouse && this->getHostID()!=this->getOwner() )
534  {
535    SYNCHELP_READ_FLOAT( xMouse );
536    SYNCHELP_READ_FLOAT( yMouse );
537    SYNCHELP_READ_FLOAT( mouseSensitivity );
538    SYNCHELP_READ_FLOAT( cycle );
539  }
540
541  return SYNCHELP_READ_N;
542}
543
544
545
546int SpaceShip::readBytes( byte * data, int maxLength, int * reciever )
547{
548  SYNCHELP_WRITE_BEGIN();
549
550  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
551  {
552    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
553    setRequestedSync( true );
554  }
555
556  int rec = this->getRequestSync();
557  if ( rec > 0 )
558  {
559    *reciever = rec;
560
561    SYNCHELP_WRITE_BYTE( (byte)DATA_state );
562
563    SYNCHELP_WRITE_FKT( WorldEntity::readState );
564    SYNCHELP_WRITE_FLOAT( cycle );
565
566    return SYNCHELP_WRITE_N;
567  }
568
569
570  *reciever = 0;
571
572  if ( this->getHostID()==this->getOwner() )
573  {
574    int mask = 0;
575
576    if ( bUp )
577      mask |= MASK_bUp;
578    if ( bDown )
579      mask |= MASK_bDown;
580    if ( bLeft )
581      mask |= MASK_bLeft;
582    if ( bRight )
583      mask |= MASK_bRight;
584    if ( bAscend )
585      mask |= MASK_bAscend;
586    if ( bFire )
587      mask |= MASK_bFire;
588    if ( bRollL )
589      mask |= MASK_bRollL;
590    if ( bRollR )
591      mask |= MASK_bRollR;
592
593    static int oldMask = mask+1; //so it is different the first time!
594    static float oldxMouse = xMouse + 1.0;
595    static float oldyMouse = yMouse + 1.0;
596
597    if ( mask != oldMask )
598    {
599      oldMask = mask;
600      SYNCHELP_WRITE_BYTE( DATA_flags );
601      SYNCHELP_WRITE_INT( mask );
602    }
603
604    if ( oldxMouse != xMouse || oldyMouse != yMouse )
605    {
606      oldxMouse = xMouse;
607      oldyMouse = yMouse;
608      SYNCHELP_WRITE_BYTE( DATA_mouse );
609      SYNCHELP_WRITE_FLOAT( xMouse );
610      SYNCHELP_WRITE_FLOAT( yMouse );
611      SYNCHELP_WRITE_FLOAT( mouseSensitivity );
612      SYNCHELP_WRITE_FLOAT( cycle );
613    }
614  }
615
616  return SYNCHELP_WRITE_N;
617}
Note: See TracBrowser for help on using the repository browser.