Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6636 was 6634, checked in by bensch, 19 years ago

orxonox/trunk: merged the network-branche back to the trunk

merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/network . -r6500:HEAD
minor conflicts in texture and one Makefile resolved to the trunk

also made a small patch to texture, so it Modulates with GL_REPEAT

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  //weapons:
101  Weapon* wpRight = new TestGun(0);
102  wpRight->setName("testGun Right");
103  Weapon* wpLeft = new TestGun(1);
104  wpLeft->setName("testGun Left");
105  Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON));
106
107  cannon->setName("BFG");
108
109  this->addWeapon(wpLeft, 1, 0);
110  this->addWeapon(wpRight,1 ,1);
111  this->addWeapon(cannon, 0, 6);
112
113  //this->addWeapon(turret, 3, 0);
114
115  this->getWeaponManager()->changeWeaponConfig(1);
116}
117
118
119/**
120 * initializes a Spaceship
121 */
122void SpaceShip::init()
123{
124//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
125  this->setClassID(CL_SPACE_SHIP, "SpaceShip");
126
127  PRINTF(4)("SPACESHIP INIT\n");
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 * loads the Settings of a SpaceShip from an XML-element.
207 * @param root the XML-element to load the Spaceship's properties from
208 */
209void SpaceShip::loadParams(const TiXmlElement* root)
210{
211  WorldEntity::loadParams(root);
212}
213
214
215void SpaceShip::enter()
216{
217  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
218  this->attachCamera();
219}
220
221void SpaceShip::leave()
222{
223  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
224  this->detachCamera();
225}
226
227
228/**
229 *  effect that occurs after the SpaceShip is spawned
230*/
231void SpaceShip::postSpawn ()
232{
233  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
234}
235
236/**
237 *  the action occuring if the spaceship left the game
238*/
239void SpaceShip::leftWorld ()
240{}
241
242WorldEntity* ref = NULL;
243/**
244 *  this function is called, when two entities collide
245 * @param entity: the world entity with whom it collides
246 *
247 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
248 */
249void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location)
250{
251  Playable::collidesWith(entity, location);
252  if (entity->isA(CL_TURRET_POWER_UP) && entity != ref)
253  {
254    this->ADDWEAPON();
255    ref = entity;
256    }
257//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
258}
259
260/**
261 *  draws the spaceship after transforming it.
262*/
263void SpaceShip::draw () const
264{
265  WorldEntity::draw();
266  this->getWeaponManager()->draw();
267
268  //this->debug(0);
269}
270
271/**
272 *  the function called for each passing timeSnap
273 * @param time The timespan passed since last update
274*/
275void SpaceShip::tick (float time)
276{
277  this->getWeaponManager()->tick(time);
278  // weapon system manipulation
279  this->weaponAction();
280
281//   if( this != State::getPlayer()->getControllable())
282//     return;
283
284  // spaceship controlled movement
285  this->calculateVelocity(time);
286
287  Vector move = (velocity)*time;
288
289  //orient the velocity in the direction of the spaceship.
290  travelSpeed = velocity.len();
291  velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity;
292  velocity = (velocity.getNormalized())*travelSpeed;
293
294  //orient the spaceship in direction of the mouse
295   rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, fabsf(time)*3.0);
296   if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
297    this->setAbsDir( rotQuat);
298   //this->setAbsDirSoft(mouseDir,5);
299
300  // this is the air friction (necessary for a smooth control)
301  if(velocity.len() != 0) velocity -= velocity*0.01;
302
303  //hoover effect
304  //cycle += time;
305  //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
306
307  //readjust
308
309  /*
310    In the game "Yager" the spaceship gets readjusted when the player moves the mouse.
311    I (bknecht) go and check it out how they do it, we could probably use this also in Orxonox.
312  */
313  //if (xMouse != 0 && yMouse != 0)
314
315  //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
316  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
317
318  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
319
320  this->shiftCoor (move);
321
322
323}
324
325/**
326 *  calculate the velocity
327 * @param time the timeslice since the last frame
328*/
329void SpaceShip::calculateVelocity (float time)
330{
331  Vector accel(0.0, 0.0, 0.0);
332  /*
333  Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter
334  */
335  //float rotVal = 0.0;
336  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
337  /* calculate the direction in which the craft is heading  */
338
339  Plane plane(Vector(0,1,0), Vector(0,0,0));
340
341  if( this->bUp )
342   {
343     //this->shiftCoor(this->getAbsDirX());
344      accel += (this->getAbsDirX())*2;
345
346      /* Heli-Steuerung
347         accel += (this->getAbsDirX()*2;
348         if(
349      */
350   }
351
352  if( this->bDown )
353   {
354     //this->shiftCoor((this->getAbsDirX())*-1);
355     accel -= (this->getAbsDirX())*2;
356   }
357
358  if( this->bLeft/* > -this->getRelCoor().z*2*/)
359  {
360    this->shiftDir(Quaternion(time, Vector(0,1,0)));
361//    accel -= rightDirection;
362    //velocityDir.normalize();
363    //rot +=Vector(1,0,0);
364    //rotVal -= .4;
365  }
366  if( this->bRight /* > this->getRelCoor().z*2*/)
367  {
368    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
369
370    //    accel += rightDirection;
371    //velocityDir.normalize();
372    //rot += Vector(1,0,0);
373    //rotVal += .4;
374  }
375
376
377  if( this->bRollL /* > -this->getRelCoor().z*2*/)
378  {
379    mouseDir *= Quaternion(-time, Vector(1,0,0));
380//    accel -= rightDirection;
381    //velocityDir.normalize();
382    //rot +=Vector(1,0,0);
383    //rotVal -= .4;
384  }
385  if( this->bRollR /* > this->getRelCoor().z*2*/)
386  {
387    mouseDir *= Quaternion(time, Vector(1,0,0));
388
389    //    accel += rightDirection;
390    //velocityDir.normalize();
391    //rot += Vector(1,0,0);
392    //rotVal += .4;
393  }
394  if (this->bAscend )
395  {
396    this->shiftDir(Quaternion(time, Vector(0,0,1)));
397
398//    accel += upDirection;
399    //velocityDir.normalize();
400    //rot += Vector(0,0,1);
401    //rotVal += .4;
402  }
403  if (this->bDescend )
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
413  velocity += accel;
414  //rot.normalize();
415  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
416}
417
418/**
419 * weapon manipulation by the player
420*/
421void SpaceShip::weaponAction()
422{
423  if( this->bFire)
424    {
425      this->getWeaponManager()->fire();
426    }
427}
428
429/**
430 * @todo switch statement ??
431 */
432void SpaceShip::process(const Event &event)
433{
434
435
436  if( event.type == SDLK_a)
437      this->bRollL = event.bPressed;
438  else if( event.type == SDLK_d)
439      this->bRollR = event.bPressed;
440  else if( event.type == KeyMapper::PEV_FIRE1)
441      this->bFire = event.bPressed;
442  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
443  {
444    this->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
445  }
446  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
447    this->previousWeaponConfig();
448  else if( event.type == SDLK_w)
449    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
450  else if( event.type == SDLK_s)
451    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
452  else if( event.type == EV_MOUSE_MOTION)
453  {
454    this->xMouse = event.xRel;
455    this->yMouse = event.yRel;
456    mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
457   // if( xMouse*xMouse + yMouse*yMouse < 0.9)
458     //this->setAbsDir(mouseDir);
459  }
460}
461
462#include "weapons/aiming_turret.h"
463// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
464void SpaceShip::ADDWEAPON()
465{
466  Weapon* turret = NULL;
467
468  if ((float)rand()/RAND_MAX < .9)
469  {
470    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
471    {
472      turret = new Turret();
473      this->addWeapon(turret, 2);
474      this->getWeaponManager()->changeWeaponConfig(2);
475    }
476  }
477  else
478  {
479    //if (this->getWeaponManager()->hasFreeSlot(3))
480    {
481      turret = dynamic_cast<Weapon*>(Factory::fabricate(CL_TARGETING_TURRET));
482      if (turret != NULL)
483      this->addWeapon(turret, 3);
484
485      this->getWeaponManager()->changeWeaponConfig(3);
486    }
487  }
488
489  if(turret != NULL)
490  {
491    turret->setName("Turret");
492    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
493  }
494}
495
496#define MASK_bUp         1
497#define MASK_bDown       2
498#define MASK_bLeft       4
499#define MASK_bRight      8
500#define MASK_bAscend    16
501#define MASK_bDescend   32
502#define MASK_bFire      64
503#define MASK_bRollL    128
504#define MASK_bRollR    256
505
506#define DATA_state       1
507#define DATA_flags       2
508#define DATA_mouse       3
509
510int SpaceShip::writeBytes( const byte * data, int length, int sender )
511{
512  SYNCHELP_READ_BEGIN();
513
514  byte b;
515  SYNCHELP_READ_BYTE( b );
516
517  if ( b == DATA_state /*&& (this->getHostID()!=this->getOwner() || sender==0)*/ )
518  {
519    PRINTF(0)("GOT STATE %d\n", this->getUniqueID());
520    setRequestedSync( false );
521    setIsOutOfSync( false );
522    SYNCHELP_READ_FKT( WorldEntity::writeState );
523    //SYNCHELP_READ_FLOAT( cycle );
524
525    return SYNCHELP_READ_N;
526  }
527
528
529  if ( b == DATA_flags /*&& this->getHostID()!=this->getOwner()*/ )
530  {
531    int flags = 0;
532    SYNCHELP_READ_INT( flags );
533
534    bUp = (flags & MASK_bUp) != 0;
535    bDown = (flags & MASK_bDown) != 0;
536    bLeft = (flags & MASK_bLeft) != 0;
537    bRight = (flags & MASK_bRight) != 0;
538    bAscend = (flags & MASK_bAscend) != 0;
539    bDescend = (flags & MASK_bDescend) != 0;
540    bFire = (flags & MASK_bFire) != 0;
541    bRollL = (flags & MASK_bRollL) != 0;
542    bRollR = (flags & MASK_bRollR) != 0;
543
544  }
545
546  /*if ( b == DATA_mouse && this->getHostID()!=this->getOwner() )
547  {
548    SYNCHELP_READ_FLOAT( xMouse );
549    SYNCHELP_READ_FLOAT( yMouse );
550    SYNCHELP_READ_FLOAT( mouseSensitivity );
551    SYNCHELP_READ_FLOAT( cycle );
552}*/
553
554  if ( this->getOwner() != this->getHostID() )
555    SYNCHELP_READ_FKT( PNode::writeSync );
556
557  return SYNCHELP_READ_N;
558}
559
560
561
562int SpaceShip::readBytes( byte * data, int maxLength, int * reciever )
563{
564  SYNCHELP_WRITE_BEGIN();
565
566  if ( isOutOfSync() && !requestedSync() /*&& this->getHostID()!=this->getOwner()*/ )
567  {
568    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
569    setRequestedSync( true );
570    PRINTF(0)("REQUESTED STATE %d\n", this->getUniqueID());
571  }
572
573  int rec = this->getRequestSync();
574  if ( rec > 0 )
575  {
576    *reciever = rec;
577
578    PRINTF(0)("SEND STATE %d %d\n", this->getUniqueID(), rec);
579
580    SYNCHELP_WRITE_BYTE( (byte)DATA_state );
581
582    SYNCHELP_WRITE_FKT( WorldEntity::readState );
583    //SYNCHELP_WRITE_FLOAT( cycle );
584
585    return SYNCHELP_WRITE_N;
586  }
587
588  *reciever = 0;
589
590  if ( this->getHostID()==this->getOwner() )
591  {
592    int mask = 0;
593
594    if ( bUp )
595      mask |= MASK_bUp;
596    if ( bDown )
597      mask |= MASK_bDown;
598    if ( bLeft )
599      mask |= MASK_bLeft;
600    if ( bRight )
601      mask |= MASK_bRight;
602    if ( bAscend )
603      mask |= MASK_bAscend;
604    if ( bFire )
605      mask |= MASK_bFire;
606    if ( bRollL )
607      mask |= MASK_bRollL;
608    if ( bRollR )
609      mask |= MASK_bRollR;
610
611
612    //static float oldxMouse = xMouse + 1.0;
613    //static float oldyMouse = yMouse + 1.0;
614
615    if ( mask != oldMask )
616    {
617      oldMask = mask;
618      SYNCHELP_WRITE_BYTE( DATA_flags );
619      SYNCHELP_WRITE_INT( mask );
620    }
621    else
622    {
623      SYNCHELP_WRITE_BYTE( 0 );
624    }
625
626    /*if ( oldxMouse != xMouse || oldyMouse != yMouse )
627    {
628      oldxMouse = xMouse;
629      oldyMouse = yMouse;
630      SYNCHELP_WRITE_BYTE( DATA_mouse );
631      SYNCHELP_WRITE_FLOAT( xMouse );
632      SYNCHELP_WRITE_FLOAT( yMouse );
633      SYNCHELP_WRITE_FLOAT( mouseSensitivity );
634      SYNCHELP_WRITE_FLOAT( cycle );
635    }*/
636  }
637  else
638  {
639    SYNCHELP_WRITE_BYTE( 0 );
640  }
641
642  if ( this->getOwner() == this->getHostID() )
643    SYNCHELP_WRITE_FKT( PNode::readSync );
644
645  return SYNCHELP_WRITE_N;
646}
Note: See TracBrowser for help on using the repository browser.