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 | |
---|
54 | using namespace std; |
---|
55 | |
---|
56 | CREATE_FACTORY(SpaceShip, CL_SPACE_SHIP); |
---|
57 | |
---|
58 | |
---|
59 | /** |
---|
60 | * destructs the spaceship, deletes alocated memory |
---|
61 | */ |
---|
62 | SpaceShip::~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 | */ |
---|
71 | SpaceShip::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 | */ |
---|
91 | SpaceShip::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 | */ |
---|
118 | void 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_FORWARD); |
---|
173 | registerEvent(KeyMapper::PEV_BACKWARD); |
---|
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 | */ |
---|
245 | void SpaceShip::loadParams(const TiXmlElement* root) |
---|
246 | { |
---|
247 | Playable::loadParams(root); |
---|
248 | } |
---|
249 | |
---|
250 | |
---|
251 | void SpaceShip::enter() |
---|
252 | { |
---|
253 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true); |
---|
254 | this->attachCamera(); |
---|
255 | } |
---|
256 | |
---|
257 | void 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 | */ |
---|
267 | void 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 | */ |
---|
275 | void SpaceShip::leftWorld () |
---|
276 | {} |
---|
277 | |
---|
278 | WorldEntity* 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 | */ |
---|
285 | void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location) |
---|
286 | { |
---|
287 | Playable::collidesWith(entity, location); |
---|
288 | |
---|
289 | if( entity->isA(CL_PROJECTILE) && entity != ref) |
---|
290 | { |
---|
291 | if ( isServer() ) |
---|
292 | { |
---|
293 | networkCollisionList.push_back( entity->getHealth() ); |
---|
294 | doCollideNetwork( entity->getHealth() ); |
---|
295 | } |
---|
296 | } |
---|
297 | // PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z); |
---|
298 | } |
---|
299 | |
---|
300 | /** |
---|
301 | * draws the spaceship after transforming it. |
---|
302 | */ |
---|
303 | void SpaceShip::draw () const |
---|
304 | { |
---|
305 | WorldEntity::draw(); |
---|
306 | this->getWeaponManager()->draw(); |
---|
307 | |
---|
308 | //this->debug(0); |
---|
309 | } |
---|
310 | |
---|
311 | /** |
---|
312 | * the function called for each passing timeSnap |
---|
313 | * @param time The timespan passed since last update |
---|
314 | */ |
---|
315 | void SpaceShip::tick (float time) |
---|
316 | { |
---|
317 | Playable::tick(time); |
---|
318 | |
---|
319 | if( ( xMouse != 0 || yMouse != 0 ) && this->getOwner() == this->getHostID() ) |
---|
320 | { |
---|
321 | if (xMouse > controlVelocityX) xMouse = controlVelocityX; |
---|
322 | else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX; |
---|
323 | if (yMouse > controlVelocityY) yMouse = controlVelocityY; |
---|
324 | else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY; |
---|
325 | |
---|
326 | pitchDir = (Quaternion(xMouse*mouseSensitivity*0.5, Vector(1,0,0))); |
---|
327 | |
---|
328 | mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity*yInvert, Vector(0,0,1))*pitchDir); |
---|
329 | xMouse = yMouse = 0; |
---|
330 | } |
---|
331 | |
---|
332 | |
---|
333 | // if( this != State::getPlayer()->getControllable()) |
---|
334 | // return; |
---|
335 | |
---|
336 | // spaceship controlled movement |
---|
337 | if (this->getOwner() == this->getHostID()) |
---|
338 | this->calculateVelocity(time); |
---|
339 | |
---|
340 | |
---|
341 | Vector move = velocity*time; |
---|
342 | |
---|
343 | //orient the velocity in the direction of the spaceship. |
---|
344 | travelSpeed = velocity.len(); |
---|
345 | velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity; |
---|
346 | velocity = (velocity.getNormalized())*travelSpeed; |
---|
347 | this->burstEmitter->setEmissionRate(travelSpeed); |
---|
348 | this->burstEmitter->setEmissionVelocity(travelSpeed*.5, travelSpeed *.1); |
---|
349 | |
---|
350 | //orient the spaceship in direction of the mouse |
---|
351 | rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, 0.5);//fabsf(time)*shipInertia); |
---|
352 | if (this->getAbsDir().distance(rotQuat) > 0.00000000000001) |
---|
353 | this->setAbsDir( rotQuat); |
---|
354 | //this->setAbsDirSoft(mouseDir,5); |
---|
355 | |
---|
356 | // this is the air friction (necessary for a smooth control) |
---|
357 | if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001; |
---|
358 | else if (travelSpeed <= 80) velocity -= velocity.getNormalized()*travelSpeed*0.001; |
---|
359 | |
---|
360 | //other physics (gravity) |
---|
361 | //if(travelSpeed < 120) |
---|
362 | //move += Vector(0,-1,0)*60*time + Vector(0,1,0)*travelSpeed/2*time; |
---|
363 | |
---|
364 | //hoover effect |
---|
365 | //cycle += time; |
---|
366 | //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02); |
---|
367 | |
---|
368 | //readjust |
---|
369 | //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0))); |
---|
370 | //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0))); |
---|
371 | |
---|
372 | //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2); |
---|
373 | |
---|
374 | this->shiftCoor(move); |
---|
375 | |
---|
376 | |
---|
377 | } |
---|
378 | |
---|
379 | /** |
---|
380 | * calculate the velocity |
---|
381 | * @param time the timeslice since the last frame |
---|
382 | */ |
---|
383 | void SpaceShip::calculateVelocity (float time) |
---|
384 | { |
---|
385 | Vector accel(0.0, 0.0, 0.0); |
---|
386 | /* |
---|
387 | Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter |
---|
388 | */ |
---|
389 | //float rotVal = 0.0; |
---|
390 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
391 | /* calculate the direction in which the craft is heading */ |
---|
392 | |
---|
393 | //Plane plane(Vector(0,1,0), Vector(0,0,0)); |
---|
394 | |
---|
395 | if( this->bUp ) |
---|
396 | { |
---|
397 | //this->shiftCoor(this->getAbsDirX()); |
---|
398 | //accel += (this->getAbsDirX())*2; |
---|
399 | accel += (this->getAbsDirX())*acceleration; |
---|
400 | |
---|
401 | } |
---|
402 | |
---|
403 | if( this->bDown ) |
---|
404 | { |
---|
405 | //this->shiftCoor((this->getAbsDirX())*-1); |
---|
406 | //accel -= (this->getAbsDirX())*2; |
---|
407 | //if(velocity.len() > 50) |
---|
408 | accel -= (this->getAbsDirX())*0.5*acceleration; |
---|
409 | |
---|
410 | |
---|
411 | |
---|
412 | } |
---|
413 | |
---|
414 | if( this->bLeft/* > -this->getRelCoor().z*2*/) |
---|
415 | { |
---|
416 | this->shiftDir(Quaternion(time, Vector(0,1,0))); |
---|
417 | // accel -= rightDirection; |
---|
418 | //velocityDir.normalize(); |
---|
419 | //rot +=Vector(1,0,0); |
---|
420 | //rotVal -= .4; |
---|
421 | } |
---|
422 | if( this->bRight /* > this->getRelCoor().z*2*/) |
---|
423 | { |
---|
424 | this->shiftDir(Quaternion(-time, Vector(0,1,0))); |
---|
425 | |
---|
426 | // accel += rightDirection; |
---|
427 | //velocityDir.normalize(); |
---|
428 | //rot += Vector(1,0,0); |
---|
429 | //rotVal += .4; |
---|
430 | } |
---|
431 | |
---|
432 | |
---|
433 | if( this->bRollL /* > -this->getRelCoor().z*2*/) |
---|
434 | { |
---|
435 | mouseDir *= Quaternion(-time*2, Vector(1,0,0)); |
---|
436 | // accel -= rightDirection; |
---|
437 | //velocityDir.normalize(); |
---|
438 | //rot +=Vector(1,0,0); |
---|
439 | //rotVal -= .4; |
---|
440 | } |
---|
441 | if( this->bRollR /* > this->getRelCoor().z*2*/) |
---|
442 | { |
---|
443 | mouseDir *= Quaternion(time*2, Vector(1,0,0)); |
---|
444 | |
---|
445 | // accel += rightDirection; |
---|
446 | //velocityDir.normalize(); |
---|
447 | //rot += Vector(1,0,0); |
---|
448 | //rotVal += .4; |
---|
449 | } |
---|
450 | if (this->bAscend ) |
---|
451 | { |
---|
452 | this->shiftDir(Quaternion(time, Vector(0,0,1))); |
---|
453 | |
---|
454 | // accel += upDirection; |
---|
455 | //velocityDir.normalize(); |
---|
456 | //rot += Vector(0,0,1); |
---|
457 | //rotVal += .4; |
---|
458 | } |
---|
459 | if (this->bDescend ) |
---|
460 | { |
---|
461 | this->shiftDir(Quaternion(-time, Vector(0,0,1))); |
---|
462 | |
---|
463 | // accel -= upDirection; |
---|
464 | //velocityDir.normalize(); |
---|
465 | //rot += Vector(0,0,1); |
---|
466 | //rotVal -= .4; |
---|
467 | } |
---|
468 | |
---|
469 | velocity += accel*time*10; |
---|
470 | //rot.normalize(); |
---|
471 | //this->setRelDirSoft(Quaternion(rotVal, rot), 5); |
---|
472 | } |
---|
473 | |
---|
474 | /** |
---|
475 | * @todo switch statement ?? |
---|
476 | */ |
---|
477 | void SpaceShip::process(const Event &event) |
---|
478 | { |
---|
479 | Playable::process(event); |
---|
480 | |
---|
481 | if( event.type == KeyMapper::PEV_LEFT) |
---|
482 | this->bRollL = event.bPressed; |
---|
483 | else if( event.type == KeyMapper::PEV_RIGHT) |
---|
484 | this->bRollR = event.bPressed; |
---|
485 | else if( event.type == KeyMapper::PEV_FORWARD) |
---|
486 | this->bUp = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
487 | else if( event.type == KeyMapper::PEV_BACKWARD) |
---|
488 | this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
489 | else if( event.type == EV_MOUSE_MOTION) |
---|
490 | { |
---|
491 | this->xMouse += event.xRel; |
---|
492 | this->yMouse += event.yRel; |
---|
493 | } |
---|
494 | } |
---|
495 | |
---|
496 | |
---|
497 | #define MASK_bUp 1 |
---|
498 | #define MASK_bDown 2 |
---|
499 | #define MASK_bLeft 4 |
---|
500 | #define MASK_bRight 8 |
---|
501 | #define MASK_bAscend 16 |
---|
502 | #define MASK_bDescend 32 |
---|
503 | #define MASK_bRollL 64 |
---|
504 | #define MASK_bRollR 128 |
---|
505 | |
---|
506 | #define DATA_state 1 |
---|
507 | #define DATA_flags 2 |
---|
508 | #define DATA_mouse 3 |
---|
509 | #define DATA_sync 4 |
---|
510 | #define DATA_velocity 5 |
---|
511 | #define DATA_playables 6 |
---|
512 | #define DATA_collision 7 |
---|
513 | |
---|
514 | int SpaceShip::writeBytes( const byte * data, int length, int sender ) |
---|
515 | { |
---|
516 | SYNCHELP_READ_BEGIN(); |
---|
517 | |
---|
518 | byte b; |
---|
519 | |
---|
520 | while ( SYNCHELP_READ_REMAINING()>0 ) |
---|
521 | { |
---|
522 | SYNCHELP_READ_BYTE( b, NWT_SS_B ); |
---|
523 | |
---|
524 | if ( b == DATA_state ) |
---|
525 | { |
---|
526 | setRequestedSync( false ); |
---|
527 | setIsOutOfSync( false ); |
---|
528 | SYNCHELP_READ_FKT( WorldEntity::writeState, NWT_SS_WE_STATE ); |
---|
529 | |
---|
530 | continue; |
---|
531 | } |
---|
532 | |
---|
533 | if ( b == DATA_flags ) |
---|
534 | { |
---|
535 | if ( this->getOwner() != this->getHostID() ) |
---|
536 | { |
---|
537 | byte flags = 0; |
---|
538 | SYNCHELP_READ_BYTE( flags, NWT_SS_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 | bRollL = (flags & MASK_bRollL) != 0; |
---|
547 | bRollR = (flags & MASK_bRollR) != 0; |
---|
548 | |
---|
549 | } |
---|
550 | else |
---|
551 | assert(false); |
---|
552 | |
---|
553 | continue; |
---|
554 | } |
---|
555 | |
---|
556 | if ( b == DATA_mouse ) |
---|
557 | { |
---|
558 | if ( this->getOwner() != this->getHostID() ) |
---|
559 | { |
---|
560 | SYNCHELP_READ_FLOAT( mouseDir.w, NWT_SS_MOUSEDIRW ); |
---|
561 | SYNCHELP_READ_FLOAT( mouseDir.v.x, NWT_SS_MOUSEDIRX ); |
---|
562 | SYNCHELP_READ_FLOAT( mouseDir.v.y, NWT_SS_MOUSEDIRY ); |
---|
563 | SYNCHELP_READ_FLOAT( mouseDir.v.z, NWT_SS_MOUSEDIRZ ); |
---|
564 | } |
---|
565 | else |
---|
566 | assert(false); |
---|
567 | |
---|
568 | continue; |
---|
569 | } |
---|
570 | |
---|
571 | if ( b == DATA_sync ) |
---|
572 | { |
---|
573 | if ( this->getOwner() != this->getHostID() ) |
---|
574 | { |
---|
575 | SYNCHELP_READ_FKT( PNode::writeSync, NWT_SS_PN_SYNC ); |
---|
576 | } |
---|
577 | else |
---|
578 | assert(false); |
---|
579 | |
---|
580 | continue; |
---|
581 | } |
---|
582 | |
---|
583 | if ( b == DATA_velocity ) |
---|
584 | { |
---|
585 | if ( this->getOwner() != this->getHostID() ) |
---|
586 | { |
---|
587 | SYNCHELP_READ_FLOAT( velocity.x, NWT_SS_VELX ); |
---|
588 | SYNCHELP_READ_FLOAT( velocity.y, NWT_SS_VELY ); |
---|
589 | SYNCHELP_READ_FLOAT( velocity.z, NWT_SS_VELZ ); |
---|
590 | } |
---|
591 | else |
---|
592 | assert(false); |
---|
593 | |
---|
594 | continue; |
---|
595 | } |
---|
596 | |
---|
597 | if ( b == DATA_playables ) |
---|
598 | { |
---|
599 | if ( this->getOwner() != this->getHostID() ) |
---|
600 | { |
---|
601 | SYNCHELP_READ_FKT( Playable::writeSync, NWT_SS_PL_SYNC ); |
---|
602 | } |
---|
603 | else |
---|
604 | assert(false); |
---|
605 | } |
---|
606 | |
---|
607 | if ( b == DATA_collision ) |
---|
608 | { |
---|
609 | int n; |
---|
610 | float energy; |
---|
611 | SYNCHELP_READ_INT( n, NWT_SS_CO_N ); |
---|
612 | |
---|
613 | for ( int i = 0; i<n; i++ ) |
---|
614 | { |
---|
615 | SYNCHELP_READ_FLOAT( energy, NWT_SS_CO_CLID ); |
---|
616 | doCollideNetwork( energy ); |
---|
617 | } |
---|
618 | } |
---|
619 | } |
---|
620 | |
---|
621 | return SYNCHELP_READ_N; |
---|
622 | } |
---|
623 | |
---|
624 | |
---|
625 | |
---|
626 | int SpaceShip::readBytes( byte * data, int maxLength, int * reciever ) |
---|
627 | { |
---|
628 | SYNCHELP_WRITE_BEGIN(); |
---|
629 | |
---|
630 | if ( isOutOfSync() && !requestedSync() /*&& this->getHostID()!=this->getOwner()*/ ) |
---|
631 | { |
---|
632 | (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() ); |
---|
633 | setRequestedSync( true ); |
---|
634 | PRINTF(0)("REQUESTED STATE %d\n", this->getUniqueID()); |
---|
635 | } |
---|
636 | |
---|
637 | int rec = this->getRequestSync(); |
---|
638 | if ( rec > 0 ) |
---|
639 | { |
---|
640 | *reciever = rec; |
---|
641 | |
---|
642 | PRINTF(0)("SEND STATE %d %d\n", this->getUniqueID(), rec); |
---|
643 | |
---|
644 | SYNCHELP_WRITE_BYTE( (byte)DATA_state, NWT_SS_B ); |
---|
645 | |
---|
646 | SYNCHELP_WRITE_FKT( WorldEntity::readState, NWT_SS_WE_STATE ); |
---|
647 | |
---|
648 | return SYNCHELP_WRITE_N; |
---|
649 | } |
---|
650 | |
---|
651 | *reciever = 0 - this->getOwner(); |
---|
652 | //TODO: implement with SYNCHELP_WRITE_SENT() |
---|
653 | bool sentSomething = false; |
---|
654 | |
---|
655 | if ( PNode::needsReadSync() && ( this->getHostID()==0 || this->getOwner() == this->getHostID() ) ) |
---|
656 | { |
---|
657 | PRINTF(0)("sending PNode::readSync\n"); |
---|
658 | SYNCHELP_WRITE_BYTE( DATA_sync, NWT_SS_B ); |
---|
659 | SYNCHELP_WRITE_FKT( PNode::readSync, NWT_SS_PN_SYNC ); |
---|
660 | sentSomething = true; |
---|
661 | } |
---|
662 | |
---|
663 | if ( this->getHostID()==0 || this->getHostID()==this->getOwner() ) |
---|
664 | { |
---|
665 | byte mask = 0; |
---|
666 | |
---|
667 | if ( bUp ) |
---|
668 | mask |= MASK_bUp; |
---|
669 | if ( bDown ) |
---|
670 | mask |= MASK_bDown; |
---|
671 | if ( bLeft ) |
---|
672 | mask |= MASK_bLeft; |
---|
673 | if ( bRight ) |
---|
674 | mask |= MASK_bRight; |
---|
675 | if ( bAscend ) |
---|
676 | mask |= MASK_bAscend; |
---|
677 | if ( bRollL ) |
---|
678 | mask |= MASK_bRollL; |
---|
679 | if ( bRollR ) |
---|
680 | mask |= MASK_bRollR; |
---|
681 | |
---|
682 | |
---|
683 | if ( mask != oldMask ) |
---|
684 | { |
---|
685 | oldMask = mask; |
---|
686 | PRINTF(0)("sending mask\n"); |
---|
687 | sentSomething = true; |
---|
688 | SYNCHELP_WRITE_BYTE( DATA_flags, NWT_SS_B ); |
---|
689 | SYNCHELP_WRITE_BYTE( mask, NWT_SS_FLAGS ); |
---|
690 | } |
---|
691 | #define __OFFSET_MDIR_W 0.01 |
---|
692 | #define __OFFSET_MDIR_V 0.01 |
---|
693 | if ( fabs( oldMouseDir.w - mouseDir.w ) > __OFFSET_MDIR_W || |
---|
694 | fabs( oldMouseDir.v.x - mouseDir.v.x ) > __OFFSET_MDIR_V || |
---|
695 | fabs( oldMouseDir.v.y - mouseDir.v.y ) > __OFFSET_MDIR_V || |
---|
696 | fabs( oldMouseDir.v.z - mouseDir.v.z ) > __OFFSET_MDIR_V ) |
---|
697 | { |
---|
698 | oldMouseDir = mouseDir; |
---|
699 | |
---|
700 | SYNCHELP_WRITE_BYTE( DATA_mouse, NWT_SS_B ); |
---|
701 | PRINTF(0)("SENDING mousedir\n"); |
---|
702 | sentSomething = true; |
---|
703 | SYNCHELP_WRITE_FLOAT( mouseDir.w, NWT_SS_MOUSEDIRW ); |
---|
704 | SYNCHELP_WRITE_FLOAT( mouseDir.v.x, NWT_SS_MOUSEDIRX ); |
---|
705 | SYNCHELP_WRITE_FLOAT( mouseDir.v.y, NWT_SS_MOUSEDIRY ); |
---|
706 | SYNCHELP_WRITE_FLOAT( mouseDir.v.z, NWT_SS_MOUSEDIRZ ); |
---|
707 | } |
---|
708 | #define __OFFSET_VEL 0.05 |
---|
709 | if ( fabs( oldVelocity.x - velocity.x ) > __OFFSET_VEL*fabs(oldVelocity.x)+0.1 || |
---|
710 | fabs( oldVelocity.y - velocity.y ) > __OFFSET_VEL*fabs(oldVelocity.y)+0.1 || |
---|
711 | fabs( oldVelocity.z - velocity.z ) > __OFFSET_VEL*fabs(oldVelocity.z)+0.1 ) |
---|
712 | { |
---|
713 | oldVelocity.x = velocity.x; |
---|
714 | oldVelocity.y = velocity.y; |
---|
715 | oldVelocity.z = velocity.z; |
---|
716 | PRINTF(0)("SENDING velocity\n"); |
---|
717 | sentSomething = true; |
---|
718 | SYNCHELP_WRITE_BYTE( DATA_velocity, NWT_SS_B ); |
---|
719 | SYNCHELP_WRITE_FLOAT( velocity.x, NWT_SS_VELX ); |
---|
720 | SYNCHELP_WRITE_FLOAT( velocity.y, NWT_SS_VELY ); |
---|
721 | SYNCHELP_WRITE_FLOAT( velocity.z, NWT_SS_VELZ ); |
---|
722 | } |
---|
723 | |
---|
724 | while ( Playable::needsReadSync() ) |
---|
725 | { |
---|
726 | sentSomething = true; |
---|
727 | PRINTF(0)("SYNCHELP_WRITE_FKT( Playable::readSync, NWT_SS_PL_SYNC )\n"); |
---|
728 | SYNCHELP_WRITE_BYTE( DATA_playables, NWT_SS_B ); |
---|
729 | SYNCHELP_WRITE_FKT( Playable::readSync, NWT_SS_PL_SYNC ); |
---|
730 | } |
---|
731 | |
---|
732 | } |
---|
733 | |
---|
734 | if ( !sentSomething ) |
---|
735 | { |
---|
736 | *reciever = 0; |
---|
737 | |
---|
738 | if ( networkCollisionList.size()>0 ) |
---|
739 | { |
---|
740 | SYNCHELP_WRITE_BYTE( DATA_collision, NWT_SS_B ); |
---|
741 | |
---|
742 | SYNCHELP_WRITE_INT( networkCollisionList.size(), NWT_SS_CO_N ); |
---|
743 | |
---|
744 | for ( std::list<float>::iterator it = networkCollisionList.begin(); it!=networkCollisionList.end(); it++ ) |
---|
745 | { |
---|
746 | SYNCHELP_WRITE_FLOAT( *it, NWT_SS_CO_CLID ); |
---|
747 | } |
---|
748 | |
---|
749 | networkCollisionList.clear(); |
---|
750 | } |
---|
751 | } |
---|
752 | |
---|
753 | return SYNCHELP_WRITE_N; |
---|
754 | } |
---|
755 | |
---|
756 | void SpaceShip::doCollideNetwork( float energy ) |
---|
757 | { |
---|
758 | this->decreaseHealth( energy ); |
---|
759 | if( this->getHealth() <= 0) |
---|
760 | { |
---|
761 | this->die(); |
---|
762 | } |
---|
763 | } |
---|
764 | |
---|
765 | |
---|
766 | |
---|