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