Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6994 was 6994, checked in by bensch, 18 years ago

orxonox/trunk: merged the Network branche back here

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