Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6469 was 6469, checked in by rennerc, 18 years ago

space_ship: tried to implement control synchronization
entities will not be spawned at (0,0,0) anymore

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