Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network: now every entity gets a network uniqueID

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