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