Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

playable now checks for player

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