Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network: booleans now included

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  this->getWeaponManager()->tick(time);
277  // weapon system manipulation
278  this->weaponAction();
279
280//   if( this != State::getPlayer()->getControllable())
281//     return;
282
283  // spaceship controlled movement
284  this->calculateVelocity(time);
285
286  Vector move = (velocity)*time;
287
288  //orient the velocity in the direction of the spaceship.
289  travelSpeed = velocity.len();
290  velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity;
291  velocity = (velocity.getNormalized())*travelSpeed;
292
293  //orient the spaceship in direction of the mouse
294   rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, fabsf(time)*3.0);
295   if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
296    this->setAbsDir( rotQuat);
297   //this->setAbsDirSoft(mouseDir,5);
298
299  // this is the air friction (necessary for a smooth control)
300  if(velocity.len() != 0) velocity -= velocity*0.01;
301
302  //hoover effect
303  //cycle += time;
304  //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
305
306  //readjust
307
308  /*
309    In the game "Yager" the spaceship gets readjusted when the player moves the mouse.
310    I (bknecht) go and check it out how they do it, we could probably use this also in Orxonox.
311  */
312  //if (xMouse != 0 && yMouse != 0)
313
314  //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
315  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
316
317  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
318
319  this->shiftCoor (move);
320
321
322}
323
324/**
325 *  calculate the velocity
326 * @param time the timeslice since the last frame
327*/
328void SpaceShip::calculateVelocity (float time)
329{
330  Vector accel(0.0, 0.0, 0.0);
331  /*
332  Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter
333  */
334  //float rotVal = 0.0;
335  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
336  /* calculate the direction in which the craft is heading  */
337
338  Plane plane(Vector(0,1,0), Vector(0,0,0));
339
340  if( this->bUp )
341   {
342     //this->shiftCoor(this->getAbsDirX());
343      accel += (this->getAbsDirX())*2;
344
345      /* Heli-Steuerung
346         accel += (this->getAbsDirX()*2;
347         if(
348      */
349   }
350
351  if( this->bDown )
352   {
353     //this->shiftCoor((this->getAbsDirX())*-1);
354     accel -= (this->getAbsDirX())*2;
355   }
356
357  if( this->bLeft/* > -this->getRelCoor().z*2*/)
358  {
359    this->shiftDir(Quaternion(time, Vector(0,1,0)));
360//    accel -= rightDirection;
361    //velocityDir.normalize();
362    //rot +=Vector(1,0,0);
363    //rotVal -= .4;
364  }
365  if( this->bRight /* > this->getRelCoor().z*2*/)
366  {
367    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
368
369    //    accel += rightDirection;
370    //velocityDir.normalize();
371    //rot += Vector(1,0,0);
372    //rotVal += .4;
373  }
374
375
376  if( this->bRollL /* > -this->getRelCoor().z*2*/)
377  {
378    mouseDir *= Quaternion(-time, Vector(1,0,0));
379//    accel -= rightDirection;
380    //velocityDir.normalize();
381    //rot +=Vector(1,0,0);
382    //rotVal -= .4;
383  }
384  if( this->bRollR /* > this->getRelCoor().z*2*/)
385  {
386    mouseDir *= Quaternion(time, Vector(1,0,0));
387
388    //    accel += rightDirection;
389    //velocityDir.normalize();
390    //rot += Vector(1,0,0);
391    //rotVal += .4;
392  }
393  if (this->bAscend )
394  {
395    this->shiftDir(Quaternion(time, Vector(0,0,1)));
396
397//    accel += upDirection;
398    //velocityDir.normalize();
399    //rot += Vector(0,0,1);
400    //rotVal += .4;
401  }
402  if (this->bDescend )
403  {
404    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
405
406    //    accel -= upDirection;
407    //velocityDir.normalize();
408    //rot += Vector(0,0,1);
409    //rotVal -= .4;
410  }
411
412  velocity += accel;
413  //rot.normalize();
414  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
415}
416
417/**
418 * weapon manipulation by the player
419*/
420void SpaceShip::weaponAction()
421{
422  if( this->bFire)
423    {
424      this->getWeaponManager()->fire();
425    }
426}
427
428/**
429 * @todo switch statement ??
430 */
431void SpaceShip::process(const Event &event)
432{
433
434
435  if( event.type == SDLK_a)
436      this->bRollL = event.bPressed;
437  else if( event.type == SDLK_d)
438      this->bRollR = event.bPressed;
439  else if( event.type == KeyMapper::PEV_FIRE1)
440      this->bFire = event.bPressed;
441  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
442  {
443    this->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
444  }
445  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
446    this->previousWeaponConfig();
447  else if( event.type == SDLK_w)
448    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
449  else if( event.type == SDLK_s)
450    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
451  else if( event.type == EV_MOUSE_MOTION)
452  {
453    this->xMouse = event.xRel;
454    this->yMouse = event.yRel;
455    mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
456   // if( xMouse*xMouse + yMouse*yMouse < 0.9)
457     //this->setAbsDir(mouseDir);
458  }
459}
460
461/**
462 *
463 */
464bool SpaceShip::pickup(PowerUp* powerUp)
465{
466  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
467    Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon();
468    WeaponManager* manager = this->getWeaponManager();
469    int slot = manager->getNextFreeSlot(0, weapon->getCapability());
470    if(slot >= 0) {
471      manager->addWeapon(weapon, 0, slot);
472      return true;
473    }
474  }
475  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
476    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
477    switch(ppu->getType()) {
478      case PARAM_SHIELD:
479        break;
480    }
481  }
482  return false;
483}
484
485#include "weapons/aiming_turret.h"
486// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
487void SpaceShip::ADDWEAPON()
488{
489  Weapon* turret = NULL;
490
491  if ((float)rand()/RAND_MAX < .9)
492  {
493    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
494    {
495      turret = new Turret();
496      this->addWeapon(turret, 2);
497      this->getWeaponManager()->changeWeaponConfig(2);
498    }
499  }
500  else
501  {
502    //if (this->getWeaponManager()->hasFreeSlot(3))
503    {
504      turret = dynamic_cast<Weapon*>(Factory::fabricate(CL_TARGETING_TURRET));
505      if (turret != NULL)
506      this->addWeapon(turret, 3);
507
508      this->getWeaponManager()->changeWeaponConfig(3);
509    }
510  }
511
512  if(turret != NULL)
513  {
514    turret->setName("Turret");
515    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
516  }
517}
518
519#define MASK_bUp         1
520#define MASK_bDown       2
521#define MASK_bLeft       4
522#define MASK_bRight      8
523#define MASK_bAscend    16
524#define MASK_bDescend   32
525#define MASK_bFire      64
526#define MASK_bRollL    128
527#define MASK_bRollR    256
528
529#define DATA_state       1
530#define DATA_flags       2
531#define DATA_mouse       3
532
533int SpaceShip::writeBytes( const byte * data, int length, int sender )
534{
535  SYNCHELP_READ_BEGIN();
536
537  byte b;
538  SYNCHELP_READ_BYTE( b );
539
540  if ( b == DATA_state /*&& (this->getHostID()!=this->getOwner() || sender==0)*/ )
541  {
542    PRINTF(0)("GOT STATE %d\n", this->getUniqueID());
543    setRequestedSync( false );
544    setIsOutOfSync( false );
545    SYNCHELP_READ_FKT( WorldEntity::writeState );
546    //SYNCHELP_READ_FLOAT( cycle );
547
548    return SYNCHELP_READ_N;
549  }
550
551
552  if ( b == DATA_flags /*&& this->getHostID()!=this->getOwner()*/ )
553  {
554    int flags = 0;
555    SYNCHELP_READ_INT( flags );
556
557    bUp = (flags & MASK_bUp) != 0;
558    bDown = (flags & MASK_bDown) != 0;
559    bLeft = (flags & MASK_bLeft) != 0;
560    bRight = (flags & MASK_bRight) != 0;
561    bAscend = (flags & MASK_bAscend) != 0;
562    bDescend = (flags & MASK_bDescend) != 0;
563    bFire = (flags & MASK_bFire) != 0;
564    bRollL = (flags & MASK_bRollL) != 0;
565    bRollR = (flags & MASK_bRollR) != 0;
566
567  }
568
569  /*if ( b == DATA_mouse && this->getHostID()!=this->getOwner() )
570  {
571    SYNCHELP_READ_FLOAT( xMouse );
572    SYNCHELP_READ_FLOAT( yMouse );
573    SYNCHELP_READ_FLOAT( mouseSensitivity );
574    SYNCHELP_READ_FLOAT( cycle );
575}*/
576
577  if ( this->getOwner() != this->getHostID() )
578    SYNCHELP_READ_FKT( PNode::writeSync );
579
580  return SYNCHELP_READ_N;
581}
582
583
584
585int SpaceShip::readBytes( byte * data, int maxLength, int * reciever )
586{
587  SYNCHELP_WRITE_BEGIN();
588
589  if ( isOutOfSync() && !requestedSync() /*&& this->getHostID()!=this->getOwner()*/ )
590  {
591    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
592    setRequestedSync( true );
593    PRINTF(0)("REQUESTED STATE %d\n", this->getUniqueID());
594  }
595
596  int rec = this->getRequestSync();
597  if ( rec > 0 )
598  {
599    *reciever = rec;
600
601    PRINTF(0)("SEND STATE %d %d\n", this->getUniqueID(), rec);
602
603    SYNCHELP_WRITE_BYTE( (byte)DATA_state );
604
605    SYNCHELP_WRITE_FKT( WorldEntity::readState );
606    //SYNCHELP_WRITE_FLOAT( cycle );
607
608    return SYNCHELP_WRITE_N;
609  }
610
611  *reciever = 0;
612
613  if ( this->getHostID()==this->getOwner() )
614  {
615    int mask = 0;
616
617    if ( bUp )
618      mask |= MASK_bUp;
619    if ( bDown )
620      mask |= MASK_bDown;
621    if ( bLeft )
622      mask |= MASK_bLeft;
623    if ( bRight )
624      mask |= MASK_bRight;
625    if ( bAscend )
626      mask |= MASK_bAscend;
627    if ( bFire )
628      mask |= MASK_bFire;
629    if ( bRollL )
630      mask |= MASK_bRollL;
631    if ( bRollR )
632      mask |= MASK_bRollR;
633
634
635    //static float oldxMouse = xMouse + 1.0;
636    //static float oldyMouse = yMouse + 1.0;
637
638    if ( mask != oldMask )
639    {
640      oldMask = mask;
641      SYNCHELP_WRITE_BYTE( DATA_flags );
642      SYNCHELP_WRITE_INT( mask );
643    }
644    else
645    {
646      SYNCHELP_WRITE_BYTE( 0 );
647    }
648
649    /*if ( oldxMouse != xMouse || oldyMouse != yMouse )
650    {
651      oldxMouse = xMouse;
652      oldyMouse = yMouse;
653      SYNCHELP_WRITE_BYTE( DATA_mouse );
654      SYNCHELP_WRITE_FLOAT( xMouse );
655      SYNCHELP_WRITE_FLOAT( yMouse );
656      SYNCHELP_WRITE_FLOAT( mouseSensitivity );
657      SYNCHELP_WRITE_FLOAT( cycle );
658    }*/
659  }
660  else
661  {
662    SYNCHELP_WRITE_BYTE( 0 );
663  }
664
665  if ( this->getOwner() == this->getHostID() )
666    SYNCHELP_WRITE_FKT( PNode::readSync );
667
668  return SYNCHELP_WRITE_N;
669}
Note: See TracBrowser for help on using the repository browser.