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