Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

space-ship control modified

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 = 150;
150  shipInertia = M_PI_4;
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    mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity*yInvert, Vector(0,0,1))*pitchDir);
355
356    xMouse = yMouse = 0;
357   }
358
359 
360
361//   if( this != State::getPlayer()->getControllable())
362//     return;
363
364  // spaceship controlled movement
365  if (this->getOwner() == this->getHostID())
366    this->calculateVelocity(time);
367
368
369  Vector move = velocity*time;
370
371  //orient the velocity in the direction of the spaceship.
372  travelSpeed = velocity.len();
373  velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity;
374  velocity = (velocity.getNormalized())*travelSpeed;
375  this->burstEmitter->setEmissionRate(travelSpeed);
376  this->burstEmitter->setEmissionVelocity(travelSpeed*.5, travelSpeed *.1);
377
378  //orient the spaceship in direction of the mouse
379   rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir,fabsf(time)*shipInertia);
380   if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
381    this->setAbsDir( rotQuat);
382   //this->setAbsDirSoft(mouseDir,5);
383
384  // this is the air friction (necessary for a smooth control)
385  if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001;
386  else if (travelSpeed <= 80) velocity -= velocity.getNormalized()*travelSpeed*0.001;
387
388  //other physics (gravity)
389  //if(travelSpeed < 120)
390  //move += Vector(0,-1,0)*60*time + Vector(0,1,0)*travelSpeed/2*time;
391
392  //hoover effect
393  //cycle += time;
394  //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
395
396  //readjust
397  //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
398  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
399
400  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
401
402  this->shiftCoor(move);
403
404
405}
406
407/**
408 *  calculate the velocity
409 * @param time the timeslice since the last frame
410*/
411void SpaceShip::calculateVelocity (float time)
412{
413  Vector accel(0.0, 0.0, 0.0);
414  /*
415  Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter
416  */
417  //float rotVal = 0.0;
418  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
419  /* calculate the direction in which the craft is heading  */
420
421  //Plane plane(Vector(0,1,0), Vector(0,0,0));
422
423  if( this->bUp )
424   {
425     //this->shiftCoor(this->getAbsDirX());
426      //accel += (this->getAbsDirX())*2;
427      accel += (this->getAbsDirX())*acceleration;
428
429   }
430
431  if( this->bDown )
432   {
433     //this->shiftCoor((this->getAbsDirX())*-1);
434     //accel -= (this->getAbsDirX())*2;
435    //if(velocity.len() > 50)
436     accel -= (this->getAbsDirX())*0.5*acceleration;
437
438
439
440   }
441
442  if( this->bLeft/* > -this->getRelCoor().z*2*/)
443  {
444    this->shiftDir(Quaternion(time, Vector(0,1,0)));
445//    accel -= rightDirection;
446    //velocityDir.normalize();
447    //rot +=Vector(1,0,0);
448    //rotVal -= .4;
449  }
450  if( this->bRight /* > this->getRelCoor().z*2*/)
451  {
452    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
453
454    //    accel += rightDirection;
455    //velocityDir.normalize();
456    //rot += Vector(1,0,0);
457    //rotVal += .4;
458  }
459
460
461  if( this->bRollL /* > -this->getRelCoor().z*2*/)
462  {
463    if( fabsf(mouseDir.distance(this->getAbsDir())) < maxRudderAmplitude )//prevent 'jumping'
464    mouseDir *= Quaternion(-time*2, Vector(1,0,0));
465//    accel -= rightDirection;
466    //velocityDir.normalize();
467    //rot +=Vector(1,0,0);
468    //rotVal -= .4;
469  }
470  if( this->bRollR /* > this->getRelCoor().z*2*/)
471  {
472    if( fabsf(mouseDir.distance(this->getAbsDir())) < maxRudderAmplitude )//prevent 'jumping'
473    mouseDir *= Quaternion(time*2, Vector(1,0,0));
474
475    //    accel += rightDirection;
476    //velocityDir.normalize();
477    //rot += Vector(1,0,0);
478    //rotVal += .4;
479  }
480  if (this->bAscend )
481  {
482    this->shiftDir(Quaternion(time, Vector(0,0,1)));
483
484//    accel += upDirection;
485    //velocityDir.normalize();
486    //rot += Vector(0,0,1);
487    //rotVal += .4;
488  }
489  if (this->bDescend )
490  {
491    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
492
493    //    accel -= upDirection;
494    //velocityDir.normalize();
495    //rot += Vector(0,0,1);
496    //rotVal -= .4;
497  }
498
499  velocity += accel*time*10;
500  //rot.normalize();
501  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
502}
503
504/**
505 * @todo switch statement ??
506 */
507void SpaceShip::process(const Event &event)
508{
509  Playable::process(event);
510
511  if( event.type == KeyMapper::PEV_LEFT)
512      this->bRollL = event.bPressed;
513  else if( event.type == KeyMapper::PEV_RIGHT)
514      this->bRollR = event.bPressed;
515  else if( event.type == KeyMapper::PEV_FORWARD)
516    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
517  else if( event.type == KeyMapper::PEV_BACKWARD)
518    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
519  else if( event.type == EV_MOUSE_MOTION)
520  {
521    this->xMouse += event.xRel;
522    this->yMouse += event.yRel;
523  }
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_bRollL     64
534#define MASK_bRollR    128
535
536#define DATA_state       1
537#define DATA_flags       2
538#define DATA_mouse       3
539#define DATA_sync        4
540#define DATA_velocity    5
541#define DATA_playables   6
542#define DATA_collision   7
543
544int SpaceShip::writeBytes( const byte * data, int length, int sender )
545{
546  SYNCHELP_READ_BEGIN();
547
548  byte b;
549
550  while ( SYNCHELP_READ_REMAINING()>0 )
551  {
552    SYNCHELP_READ_BYTE( b, NWT_SS_B );
553
554    if ( b == DATA_state )
555    {
556      setRequestedSync( false );
557      setIsOutOfSync( false );
558      SYNCHELP_READ_FKT( WorldEntity::writeState, NWT_SS_WE_STATE );
559
560      continue;
561    }
562
563    if ( b == DATA_flags )
564    {
565      if ( this->getOwner() != this->getHostID() )
566      {
567        byte flags = 0;
568        SYNCHELP_READ_BYTE( flags, NWT_SS_FLAGS );
569
570        bUp = (flags & MASK_bUp) != 0;
571        bDown = (flags & MASK_bDown) != 0;
572        bLeft = (flags & MASK_bLeft) != 0;
573        bRight = (flags & MASK_bRight) != 0;
574        bAscend = (flags & MASK_bAscend) != 0;
575        bDescend = (flags & MASK_bDescend) != 0;
576        bRollL = (flags & MASK_bRollL) != 0;
577        bRollR = (flags & MASK_bRollR) != 0;
578
579      }
580      else
581        assert(false);
582
583      continue;
584    }
585
586    if ( b == DATA_mouse )
587    {
588      if ( this->getOwner() != this->getHostID() )
589      {
590        SYNCHELP_READ_FLOAT( mouseDir.w, NWT_SS_MOUSEDIRW );
591        SYNCHELP_READ_FLOAT( mouseDir.v.x, NWT_SS_MOUSEDIRX );
592        SYNCHELP_READ_FLOAT( mouseDir.v.y, NWT_SS_MOUSEDIRY );
593        SYNCHELP_READ_FLOAT( mouseDir.v.z, NWT_SS_MOUSEDIRZ );
594      }
595      else
596        assert(false);
597
598      continue;
599    }
600
601    if ( b == DATA_sync )
602    {
603      if ( this->getOwner() != this->getHostID() )
604      {
605        SYNCHELP_READ_FKT( PNode::writeSync, NWT_SS_PN_SYNC );
606      }
607      else
608        assert(false);
609
610      continue;
611    }
612
613    if ( b == DATA_velocity )
614    {
615      if ( this->getOwner() != this->getHostID() )
616      {
617        SYNCHELP_READ_FLOAT( velocity.x, NWT_SS_VELX );
618        SYNCHELP_READ_FLOAT( velocity.y, NWT_SS_VELY );
619        SYNCHELP_READ_FLOAT( velocity.z, NWT_SS_VELZ );
620      }
621      else
622        assert(false);
623
624      continue;
625    }
626
627    if ( b == DATA_playables )
628    {
629      if ( this->getOwner() != this->getHostID() )
630      {
631        SYNCHELP_READ_FKT( Playable::writeSync, NWT_SS_PL_SYNC );
632      }
633      else
634        assert(false);
635    }
636
637    if ( b == DATA_collision )
638    {
639      int n;
640      float energy;
641      SYNCHELP_READ_INT( n, NWT_SS_CO_N );
642
643      for ( int i = 0; i<n; i++ )
644      {
645        SYNCHELP_READ_FLOAT( energy, NWT_SS_CO_CLID );
646        doCollideNetwork( energy );
647      }
648    }
649  }
650
651  return SYNCHELP_READ_N;
652}
653
654
655
656int SpaceShip::readBytes( byte * data, int maxLength, int * reciever )
657{
658  SYNCHELP_WRITE_BEGIN();
659
660  if ( isOutOfSync() && !requestedSync() /*&& this->getHostID()!=this->getOwner()*/ )
661  {
662    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
663    setRequestedSync( true );
664    PRINTF(0)("REQUESTED STATE %d\n", this->getUniqueID());
665  }
666
667  int rec = this->getRequestSync();
668  if ( rec > 0 )
669  {
670    *reciever = rec;
671
672    PRINTF(0)("SEND STATE %d %d\n", this->getUniqueID(), rec);
673
674    SYNCHELP_WRITE_BYTE( (byte)DATA_state, NWT_SS_B );
675
676    SYNCHELP_WRITE_FKT( WorldEntity::readState, NWT_SS_WE_STATE );
677
678    return SYNCHELP_WRITE_N;
679  }
680
681  *reciever = 0 - this->getOwner();
682  //TODO: implement with SYNCHELP_WRITE_SENT()
683  bool sentSomething = false;
684
685  if ( PNode::needsReadSync() && ( this->getHostID()==0 || this->getOwner() == this->getHostID() ) )
686  {
687    PRINTF(0)("sending PNode::readSync\n");
688    SYNCHELP_WRITE_BYTE( DATA_sync, NWT_SS_B );
689    SYNCHELP_WRITE_FKT( PNode::readSync, NWT_SS_PN_SYNC );
690    sentSomething = true;
691  }
692
693  if ( this->getHostID()==0 || this->getHostID()==this->getOwner() )
694  {
695    byte mask = 0;
696
697    if ( bUp )
698      mask |= MASK_bUp;
699    if ( bDown )
700      mask |= MASK_bDown;
701    if ( bLeft )
702      mask |= MASK_bLeft;
703    if ( bRight )
704      mask |= MASK_bRight;
705    if ( bAscend )
706      mask |= MASK_bAscend;
707    if ( bRollL )
708      mask |= MASK_bRollL;
709    if ( bRollR )
710      mask |= MASK_bRollR;
711
712
713    if ( mask != oldMask )
714    {
715      oldMask = mask;
716      PRINTF(0)("sending mask\n");
717      sentSomething = true;
718      SYNCHELP_WRITE_BYTE( DATA_flags, NWT_SS_B );
719      SYNCHELP_WRITE_BYTE( mask, NWT_SS_FLAGS );
720    }
721#define __OFFSET_MDIR_W 0.01
722#define __OFFSET_MDIR_V 0.01
723    if ( fabs( oldMouseDir.w - mouseDir.w ) > __OFFSET_MDIR_W ||
724         fabs( oldMouseDir.v.x - mouseDir.v.x ) > __OFFSET_MDIR_V ||
725         fabs( oldMouseDir.v.y - mouseDir.v.y ) > __OFFSET_MDIR_V ||
726         fabs( oldMouseDir.v.z - mouseDir.v.z ) > __OFFSET_MDIR_V )
727    {
728      oldMouseDir = mouseDir;
729
730      SYNCHELP_WRITE_BYTE( DATA_mouse, NWT_SS_B );
731      PRINTF(0)("SENDING mousedir\n");
732      sentSomething = true;
733      SYNCHELP_WRITE_FLOAT( mouseDir.w, NWT_SS_MOUSEDIRW );
734      SYNCHELP_WRITE_FLOAT( mouseDir.v.x, NWT_SS_MOUSEDIRX );
735      SYNCHELP_WRITE_FLOAT( mouseDir.v.y, NWT_SS_MOUSEDIRY );
736      SYNCHELP_WRITE_FLOAT( mouseDir.v.z, NWT_SS_MOUSEDIRZ );
737    }
738#define __OFFSET_VEL 0.05
739    if ( fabs( oldVelocity.x - velocity.x ) > __OFFSET_VEL*fabs(oldVelocity.x)+0.1 ||
740         fabs( oldVelocity.y - velocity.y ) > __OFFSET_VEL*fabs(oldVelocity.y)+0.1 ||
741         fabs( oldVelocity.z - velocity.z ) > __OFFSET_VEL*fabs(oldVelocity.z)+0.1 )
742    {
743      oldVelocity.x = velocity.x;
744      oldVelocity.y = velocity.y;
745      oldVelocity.z = velocity.z;
746      PRINTF(0)("SENDING velocity\n");
747      sentSomething = true;
748      SYNCHELP_WRITE_BYTE( DATA_velocity, NWT_SS_B );
749      SYNCHELP_WRITE_FLOAT( velocity.x, NWT_SS_VELX );
750      SYNCHELP_WRITE_FLOAT( velocity.y, NWT_SS_VELY );
751      SYNCHELP_WRITE_FLOAT( velocity.z, NWT_SS_VELZ );
752    }
753
754    while ( Playable::needsReadSync() )
755    {
756      sentSomething = true;
757      PRINTF(0)("SYNCHELP_WRITE_FKT( Playable::readSync, NWT_SS_PL_SYNC )\n");
758      SYNCHELP_WRITE_BYTE( DATA_playables, NWT_SS_B );
759      SYNCHELP_WRITE_FKT( Playable::readSync, NWT_SS_PL_SYNC );
760    }
761
762  }
763
764  if ( !sentSomething )
765  {
766    *reciever = 0;
767
768    if ( networkCollisionList.size()>0 )
769    {
770      SYNCHELP_WRITE_BYTE( DATA_collision, NWT_SS_B );
771
772      SYNCHELP_WRITE_INT( networkCollisionList.size(), NWT_SS_CO_N );
773
774      for ( std::list<float>::iterator it = networkCollisionList.begin(); it!=networkCollisionList.end(); it++ )
775      {
776        SYNCHELP_WRITE_FLOAT( *it, NWT_SS_CO_CLID );
777      }
778
779      networkCollisionList.clear();
780    }
781  }
782
783  return SYNCHELP_WRITE_N;
784}
785
786
787void SpaceShip::doCollideNetwork( float energy )
788{
789  this->decreaseHealth( energy );
790  if( this->getHealth() <= 0)
791  {
792    this->die();
793  }
794}
795
796
797
Note: See TracBrowser for help on using the repository browser.