Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/control/src/world_entities/space_ships/space_ship.cc @ 7141

Last change on this file since 7141 was 7141, checked in by snellen, 18 years ago

changed some parameters of the spaceship control

File size: 20.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 "dot_emitter.h"
31#include "sprite_particles.h"
32
33#include "factory.h"
34#include "key_mapper.h"
35#include "event_handler.h"
36
37#include "network_game_manager.h"
38
39#include "power_ups/weapon_power_up.h"
40#include "power_ups/param_power_up.h"
41
42#include "graphics_engine.h"
43
44#include "plane.h"
45
46#include "state.h"
47#include "player.h"
48
49#include "load_param.h"
50
51
52// #include "lib/gui/gl_gui/glgui_bar.h"
53// #include "lib/gui/gl_gui/glgui_pushbutton.h"
54
55
56using namespace std;
57
58CREATE_FACTORY(SpaceShip, CL_SPACE_SHIP);
59
60
61/**
62 *  destructs the spaceship, deletes alocated memory
63 */
64SpaceShip::~SpaceShip ()
65{
66  this->setPlayer(NULL);
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  else
99  {
100    //this->loadModel("models/ships/reap_#.obj");
101    //TODO HACK this is only for network multiplayer games.
102    if( this->getOwner()%2 == 0)
103    {
104      this->loadModel("models/ships/reap_#.obj");
105      this->toList(OM_GROUP_00);
106    }
107    else
108    {
109      this->loadModel( "models/ships/fighter.obj" );
110      this->toList(OM_GROUP_01);
111    }
112  }
113
114}
115
116
117/**
118 * initializes a Spaceship
119 */
120void SpaceShip::init()
121{
122//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
123  this->setClassID(CL_SPACE_SHIP, "SpaceShip");
124
125  PRINTF(4)("SPACESHIP INIT\n");
126
127  //weapons:
128  Weapon* wpRight = new TestGun(0);
129  wpRight->setName("testGun Right");
130  Weapon* wpLeft = new TestGun(1);
131  wpLeft->setName("testGun Left");
132  //Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON));
133
134  //cannon->setName("BFG");
135
136  this->addWeapon(wpLeft, 1, 0);
137  this->addWeapon(wpRight,1 ,1);
138  //this->addWeapon(cannon, 0, 6);
139
140  this->getWeaponManager()->changeWeaponConfig(1);
141
142  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
143
144  xMouse = yMouse = 0;
145  yInvert = 1;
146  mouseSensitivity = 0.01;
147  airViscosity = 0.8;
148  controlVelocityX = 25;
149  controlVelocityY = 100;
150  shipInertia = M_PI_2;
151  maxRudderAmplitude = 0.6 ;
152//  cycle = 0.0;
153
154  this->setHealthMax(100);
155  this->setHealth(80);
156
157  travelSpeed = 0.0;
158  acceleration = 3;
159  this->velocity = this->getAbsDirX()*travelSpeed;
160  this->mouseDir = this->getAbsDir();
161  this->pitchDir = this->getAbsDir();
162
163//   GLGuiButton* button = new GLGuiPushButton();
164//    button->show();
165//    button->setLabel("orxonox");
166//    button->setBindNode(this);
167//     GLGuiBar* bar = new GLGuiBar();
168//     bar->show();
169//     bar->setValue(7.0);
170//     bar->setMaximum(10);
171//     bar->setSize2D( 20, 100);
172//     bar->setAbsCoor2D( 10, 200);
173
174  //add events to the eventlist
175  registerEvent(KeyMapper::PEV_FORWARD);
176  registerEvent(KeyMapper::PEV_BACKWARD);
177  registerEvent(KeyMapper::PEV_LEFT);
178  registerEvent(KeyMapper::PEV_RIGHT);
179
180  //registerEvent(SDLK_e);
181  //registerEvent(SDLK_e);
182  //registerEvent(SDLK_e);
183  //registerEvent(SDLK_e);
184
185  registerEvent(KeyMapper::PEV_FIRE1);
186  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
187  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
188  //registerEvent(SDLK_PAGEUP);
189  //registerEvent(SDLK_PAGEDOWN);
190  registerEvent(EV_MOUSE_MOTION);
191
192  this->getWeaponManager()->setSlotCount(7);
193
194  this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0));
195  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
196
197  this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0));
198  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
199
200  this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5));
201  this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
202
203  this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5));
204  this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
205
206  this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5));
207  this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
208
209  this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5));
210  this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
211//
212   this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
213   this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
214   //
215//   this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
216//   this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
217//
218//   this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
219//   this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
220
221  this->getWeaponManager()->getFixedTarget()->setParent(this);
222  this->getWeaponManager()->getFixedTarget()->setRelCoor(100000,0,0);
223
224  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
225
226  this->burstEmitter = new DotEmitter(200, 0.0, .01);
227  this->burstEmitter->setParent(this);
228  this->burstEmitter->setRelCoor(-1, .5, 0);
229  this->burstEmitter->setName("SpaceShip_Burst_emitter");
230
231  this->burstSystem = new SpriteParticles(1000);
232  this->burstSystem->addEmitter(this->burstEmitter);
233  this->burstSystem->setName("SpaceShip_Burst_System");
234  ((SpriteParticles*)this->burstSystem)->setMaterialTexture("maps/radial-trans-noise.png");
235  this->burstSystem->setLifeSpan(1.0, .3);
236  this->burstSystem->setRadius(0.0, 1.0);
237  this->burstSystem->setRadius(0.05, 1.0);
238  this->burstSystem->setRadius(.5, .8);
239  this->burstSystem->setRadius(1.0, 0);
240  this->burstSystem->setColor(0.0, .7,.7,1,.7);
241  this->burstSystem->setColor(0.2, 0,0,0.8,.5);
242  this->burstSystem->setColor(0.5, .5,.5,.8,.8);
243  this->burstSystem->setColor(1.0, .8,.8,.8,.0);
244
245}
246
247
248/**
249 * loads the Settings of a SpaceShip from an XML-element.
250 * @param root the XML-element to load the Spaceship's properties from
251 */
252void SpaceShip::loadParams(const TiXmlElement* root)
253{
254  Playable::loadParams(root);
255}
256
257void SpaceShip::setAbsDirPlay(const Quaternion& quat)
258{
259  this->mouseDir = quat;
260}
261
262
263void SpaceShip::reset()
264{
265  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
266
267  xMouse = yMouse = 0;
268
269  this->setHealth(80);
270  this->velocity = Vector(0.0, 0.0, 0.0);
271}
272
273
274void SpaceShip::enter()
275{
276  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
277  this->attachCamera();
278}
279
280void SpaceShip::leave()
281{
282  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
283  this->detachCamera();
284}
285
286
287/**
288 *  effect that occurs after the SpaceShip is spawned
289*/
290void SpaceShip::postSpawn ()
291{
292  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
293}
294
295/**
296 *  the action occuring if the spaceship left the game
297*/
298void SpaceShip::leftWorld ()
299{}
300
301WorldEntity* ref = NULL;
302/**
303 *  this function is called, when two entities collide
304 * @param entity: the world entity with whom it collides
305 *
306 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
307 */
308void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location)
309{
310  Playable::collidesWith(entity, location);
311
312  if( entity->isA(CL_PROJECTILE) && entity != ref)
313  {
314    if ( isServer() )
315    {
316      networkCollisionList.push_back( entity->getHealth() );
317      doCollideNetwork( entity->getHealth() );
318    }
319  }
320//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
321}
322
323/**
324 *  draws the spaceship after transforming it.
325*/
326void SpaceShip::draw () const
327{
328  WorldEntity::draw();
329  this->getWeaponManager()->draw();
330
331  //this->debug(0);
332}
333
334/**
335 *  the function called for each passing timeSnap
336 * @param time The timespan passed since last update
337*/
338void SpaceShip::tick (float time)
339{
340  Playable::tick(time);
341 
342  if( ( xMouse != 0 
343        || yMouse != 0 )
344        && this->getOwner() == this->getHostID()  )
345   {
346    if (xMouse > controlVelocityX) xMouse = controlVelocityX;
347    else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX;
348    if (yMouse > controlVelocityY) yMouse = controlVelocityY;
349    else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY;
350
351    pitchDir = (Quaternion(xMouse*mouseSensitivity*0.5, Vector(1,0,0)));
352
353    if( fabsf(mouseDir.distance(this->getAbsDir())) < maxRudderAmplitude )//prevent 'jumping'
354    {
355     
356     mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity*yInvert, Vector(0,0,1))*pitchDir);
357    }
358    xMouse = yMouse = 0;
359   }
360
361 
362
363//   if( this != State::getPlayer()->getControllable())
364//     return;
365
366  // spaceship controlled movement
367  if (this->getOwner() == this->getHostID())
368    this->calculateVelocity(time);
369
370
371  Vector move = velocity*time;
372
373  //orient the velocity in the direction of the spaceship.
374  travelSpeed = velocity.len();
375  velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity;
376  velocity = (velocity.getNormalized())*travelSpeed;
377  this->burstEmitter->setEmissionRate(travelSpeed);
378  this->burstEmitter->setEmissionVelocity(travelSpeed*.5, travelSpeed *.1);
379
380  //orient the spaceship in direction of the mouse
381   rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir,fabsf(time)*shipInertia);
382   if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
383    this->setAbsDir( rotQuat);
384   //this->setAbsDirSoft(mouseDir,5);
385
386  // this is the air friction (necessary for a smooth control)
387  if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001;
388  else if (travelSpeed <= 80) velocity -= velocity.getNormalized()*travelSpeed*0.001;
389
390  //other physics (gravity)
391  //if(travelSpeed < 120)
392  //move += Vector(0,-1,0)*60*time + Vector(0,1,0)*travelSpeed/2*time;
393
394  //hoover effect
395  //cycle += time;
396  //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
397
398  //readjust
399  //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
400  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
401
402  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
403
404  this->shiftCoor(move);
405
406
407}
408
409/**
410 *  calculate the velocity
411 * @param time the timeslice since the last frame
412*/
413void SpaceShip::calculateVelocity (float time)
414{
415  Vector accel(0.0, 0.0, 0.0);
416  /*
417  Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter
418  */
419  //float rotVal = 0.0;
420  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
421  /* calculate the direction in which the craft is heading  */
422
423  //Plane plane(Vector(0,1,0), Vector(0,0,0));
424
425  if( this->bUp )
426   {
427     //this->shiftCoor(this->getAbsDirX());
428      //accel += (this->getAbsDirX())*2;
429      accel += (this->getAbsDirX())*acceleration;
430
431   }
432
433  if( this->bDown )
434   {
435     //this->shiftCoor((this->getAbsDirX())*-1);
436     //accel -= (this->getAbsDirX())*2;
437    //if(velocity.len() > 50)
438     accel -= (this->getAbsDirX())*0.5*acceleration;
439
440
441
442   }
443
444  if( this->bLeft/* > -this->getRelCoor().z*2*/)
445  {
446    this->shiftDir(Quaternion(time, Vector(0,1,0)));
447//    accel -= rightDirection;
448    //velocityDir.normalize();
449    //rot +=Vector(1,0,0);
450    //rotVal -= .4;
451  }
452  if( this->bRight /* > this->getRelCoor().z*2*/)
453  {
454    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
455
456    //    accel += rightDirection;
457    //velocityDir.normalize();
458    //rot += Vector(1,0,0);
459    //rotVal += .4;
460  }
461
462
463  if( this->bRollL /* > -this->getRelCoor().z*2*/)
464  {
465    if( fabsf(mouseDir.distance(this->getAbsDir())) < maxRudderAmplitude )//prevent 'jumping'
466    mouseDir *= Quaternion(-time*2, Vector(1,0,0));
467//    accel -= rightDirection;
468    //velocityDir.normalize();
469    //rot +=Vector(1,0,0);
470    //rotVal -= .4;
471  }
472  if( this->bRollR /* > this->getRelCoor().z*2*/)
473  {
474    if( fabsf(mouseDir.distance(this->getAbsDir())) < maxRudderAmplitude )//prevent 'jumping'
475    mouseDir *= Quaternion(time*2, Vector(1,0,0));
476
477    //    accel += rightDirection;
478    //velocityDir.normalize();
479    //rot += Vector(1,0,0);
480    //rotVal += .4;
481  }
482  if (this->bAscend )
483  {
484    this->shiftDir(Quaternion(time, Vector(0,0,1)));
485
486//    accel += upDirection;
487    //velocityDir.normalize();
488    //rot += Vector(0,0,1);
489    //rotVal += .4;
490  }
491  if (this->bDescend )
492  {
493    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
494
495    //    accel -= upDirection;
496    //velocityDir.normalize();
497    //rot += Vector(0,0,1);
498    //rotVal -= .4;
499  }
500
501  velocity += accel*time*10;
502  //rot.normalize();
503  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
504}
505
506/**
507 * @todo switch statement ??
508 */
509void SpaceShip::process(const Event &event)
510{
511  Playable::process(event);
512
513  if( event.type == KeyMapper::PEV_LEFT)
514      this->bRollL = event.bPressed;
515  else if( event.type == KeyMapper::PEV_RIGHT)
516      this->bRollR = event.bPressed;
517  else if( event.type == KeyMapper::PEV_FORWARD)
518    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
519  else if( event.type == KeyMapper::PEV_BACKWARD)
520    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
521  else if( event.type == EV_MOUSE_MOTION)
522  {
523    this->xMouse += event.xRel;
524    this->yMouse += event.yRel;
525  }
526}
527
528
529#define MASK_bUp         1
530#define MASK_bDown       2
531#define MASK_bLeft       4
532#define MASK_bRight      8
533#define MASK_bAscend    16
534#define MASK_bDescend   32
535#define MASK_bRollL     64
536#define MASK_bRollR    128
537
538#define DATA_state       1
539#define DATA_flags       2
540#define DATA_mouse       3
541#define DATA_sync        4
542#define DATA_velocity    5
543#define DATA_playables   6
544#define DATA_collision   7
545
546int SpaceShip::writeBytes( const byte * data, int length, int sender )
547{
548  SYNCHELP_READ_BEGIN();
549
550  byte b;
551
552  while ( SYNCHELP_READ_REMAINING()>0 )
553  {
554    SYNCHELP_READ_BYTE( b, NWT_SS_B );
555
556    if ( b == DATA_state )
557    {
558      setRequestedSync( false );
559      setIsOutOfSync( false );
560      SYNCHELP_READ_FKT( WorldEntity::writeState, NWT_SS_WE_STATE );
561
562      continue;
563    }
564
565    if ( b == DATA_flags )
566    {
567      if ( this->getOwner() != this->getHostID() )
568      {
569        byte flags = 0;
570        SYNCHELP_READ_BYTE( flags, NWT_SS_FLAGS );
571
572        bUp = (flags & MASK_bUp) != 0;
573        bDown = (flags & MASK_bDown) != 0;
574        bLeft = (flags & MASK_bLeft) != 0;
575        bRight = (flags & MASK_bRight) != 0;
576        bAscend = (flags & MASK_bAscend) != 0;
577        bDescend = (flags & MASK_bDescend) != 0;
578        bRollL = (flags & MASK_bRollL) != 0;
579        bRollR = (flags & MASK_bRollR) != 0;
580
581      }
582      else
583        assert(false);
584
585      continue;
586    }
587
588    if ( b == DATA_mouse )
589    {
590      if ( this->getOwner() != this->getHostID() )
591      {
592        SYNCHELP_READ_FLOAT( mouseDir.w, NWT_SS_MOUSEDIRW );
593        SYNCHELP_READ_FLOAT( mouseDir.v.x, NWT_SS_MOUSEDIRX );
594        SYNCHELP_READ_FLOAT( mouseDir.v.y, NWT_SS_MOUSEDIRY );
595        SYNCHELP_READ_FLOAT( mouseDir.v.z, NWT_SS_MOUSEDIRZ );
596      }
597      else
598        assert(false);
599
600      continue;
601    }
602
603    if ( b == DATA_sync )
604    {
605      if ( this->getOwner() != this->getHostID() )
606      {
607        SYNCHELP_READ_FKT( PNode::writeSync, NWT_SS_PN_SYNC );
608      }
609      else
610        assert(false);
611
612      continue;
613    }
614
615    if ( b == DATA_velocity )
616    {
617      if ( this->getOwner() != this->getHostID() )
618      {
619        SYNCHELP_READ_FLOAT( velocity.x, NWT_SS_VELX );
620        SYNCHELP_READ_FLOAT( velocity.y, NWT_SS_VELY );
621        SYNCHELP_READ_FLOAT( velocity.z, NWT_SS_VELZ );
622      }
623      else
624        assert(false);
625
626      continue;
627    }
628
629    if ( b == DATA_playables )
630    {
631      if ( this->getOwner() != this->getHostID() )
632      {
633        SYNCHELP_READ_FKT( Playable::writeSync, NWT_SS_PL_SYNC );
634      }
635      else
636        assert(false);
637    }
638
639    if ( b == DATA_collision )
640    {
641      int n;
642      float energy;
643      SYNCHELP_READ_INT( n, NWT_SS_CO_N );
644
645      for ( int i = 0; i<n; i++ )
646      {
647        SYNCHELP_READ_FLOAT( energy, NWT_SS_CO_CLID );
648        doCollideNetwork( energy );
649      }
650    }
651  }
652
653  return SYNCHELP_READ_N;
654}
655
656
657
658int SpaceShip::readBytes( byte * data, int maxLength, int * reciever )
659{
660  SYNCHELP_WRITE_BEGIN();
661
662  if ( isOutOfSync() && !requestedSync() /*&& this->getHostID()!=this->getOwner()*/ )
663  {
664    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
665    setRequestedSync( true );
666    PRINTF(0)("REQUESTED STATE %d\n", this->getUniqueID());
667  }
668
669  int rec = this->getRequestSync();
670  if ( rec > 0 )
671  {
672    *reciever = rec;
673
674    PRINTF(0)("SEND STATE %d %d\n", this->getUniqueID(), rec);
675
676    SYNCHELP_WRITE_BYTE( (byte)DATA_state, NWT_SS_B );
677
678    SYNCHELP_WRITE_FKT( WorldEntity::readState, NWT_SS_WE_STATE );
679
680    return SYNCHELP_WRITE_N;
681  }
682
683  *reciever = 0 - this->getOwner();
684  //TODO: implement with SYNCHELP_WRITE_SENT()
685  bool sentSomething = false;
686
687  if ( PNode::needsReadSync() && ( this->getHostID()==0 || this->getOwner() == this->getHostID() ) )
688  {
689    PRINTF(0)("sending PNode::readSync\n");
690    SYNCHELP_WRITE_BYTE( DATA_sync, NWT_SS_B );
691    SYNCHELP_WRITE_FKT( PNode::readSync, NWT_SS_PN_SYNC );
692    sentSomething = true;
693  }
694
695  if ( this->getHostID()==0 || this->getHostID()==this->getOwner() )
696  {
697    byte mask = 0;
698
699    if ( bUp )
700      mask |= MASK_bUp;
701    if ( bDown )
702      mask |= MASK_bDown;
703    if ( bLeft )
704      mask |= MASK_bLeft;
705    if ( bRight )
706      mask |= MASK_bRight;
707    if ( bAscend )
708      mask |= MASK_bAscend;
709    if ( bRollL )
710      mask |= MASK_bRollL;
711    if ( bRollR )
712      mask |= MASK_bRollR;
713
714
715    if ( mask != oldMask )
716    {
717      oldMask = mask;
718      PRINTF(0)("sending mask\n");
719      sentSomething = true;
720      SYNCHELP_WRITE_BYTE( DATA_flags, NWT_SS_B );
721      SYNCHELP_WRITE_BYTE( mask, NWT_SS_FLAGS );
722    }
723#define __OFFSET_MDIR_W 0.01
724#define __OFFSET_MDIR_V 0.01
725    if ( fabs( oldMouseDir.w - mouseDir.w ) > __OFFSET_MDIR_W ||
726         fabs( oldMouseDir.v.x - mouseDir.v.x ) > __OFFSET_MDIR_V ||
727         fabs( oldMouseDir.v.y - mouseDir.v.y ) > __OFFSET_MDIR_V ||
728         fabs( oldMouseDir.v.z - mouseDir.v.z ) > __OFFSET_MDIR_V )
729    {
730      oldMouseDir = mouseDir;
731
732      SYNCHELP_WRITE_BYTE( DATA_mouse, NWT_SS_B );
733      PRINTF(0)("SENDING mousedir\n");
734      sentSomething = true;
735      SYNCHELP_WRITE_FLOAT( mouseDir.w, NWT_SS_MOUSEDIRW );
736      SYNCHELP_WRITE_FLOAT( mouseDir.v.x, NWT_SS_MOUSEDIRX );
737      SYNCHELP_WRITE_FLOAT( mouseDir.v.y, NWT_SS_MOUSEDIRY );
738      SYNCHELP_WRITE_FLOAT( mouseDir.v.z, NWT_SS_MOUSEDIRZ );
739    }
740#define __OFFSET_VEL 0.05
741    if ( fabs( oldVelocity.x - velocity.x ) > __OFFSET_VEL*fabs(oldVelocity.x)+0.1 ||
742         fabs( oldVelocity.y - velocity.y ) > __OFFSET_VEL*fabs(oldVelocity.y)+0.1 ||
743         fabs( oldVelocity.z - velocity.z ) > __OFFSET_VEL*fabs(oldVelocity.z)+0.1 )
744    {
745      oldVelocity.x = velocity.x;
746      oldVelocity.y = velocity.y;
747      oldVelocity.z = velocity.z;
748      PRINTF(0)("SENDING velocity\n");
749      sentSomething = true;
750      SYNCHELP_WRITE_BYTE( DATA_velocity, NWT_SS_B );
751      SYNCHELP_WRITE_FLOAT( velocity.x, NWT_SS_VELX );
752      SYNCHELP_WRITE_FLOAT( velocity.y, NWT_SS_VELY );
753      SYNCHELP_WRITE_FLOAT( velocity.z, NWT_SS_VELZ );
754    }
755
756    while ( Playable::needsReadSync() )
757    {
758      sentSomething = true;
759      PRINTF(0)("SYNCHELP_WRITE_FKT( Playable::readSync, NWT_SS_PL_SYNC )\n");
760      SYNCHELP_WRITE_BYTE( DATA_playables, NWT_SS_B );
761      SYNCHELP_WRITE_FKT( Playable::readSync, NWT_SS_PL_SYNC );
762    }
763
764  }
765
766  if ( !sentSomething )
767  {
768    *reciever = 0;
769
770    if ( networkCollisionList.size()>0 )
771    {
772      SYNCHELP_WRITE_BYTE( DATA_collision, NWT_SS_B );
773
774      SYNCHELP_WRITE_INT( networkCollisionList.size(), NWT_SS_CO_N );
775
776      for ( std::list<float>::iterator it = networkCollisionList.begin(); it!=networkCollisionList.end(); it++ )
777      {
778        SYNCHELP_WRITE_FLOAT( *it, NWT_SS_CO_CLID );
779      }
780
781      networkCollisionList.clear();
782    }
783  }
784
785  return SYNCHELP_WRITE_N;
786}
787
788
789void SpaceShip::doCollideNetwork( float energy )
790{
791  this->decreaseHealth( energy );
792  if( this->getHealth() <= 0)
793  {
794    this->die();
795  }
796}
797
798
799
Note: See TracBrowser for help on using the repository browser.