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