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