Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

spaceship: hack: load default model

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