/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Knecht co-programmer: Silvan Nellen */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY #include "executor/executor.h" #include "space_ship.h" #include "util/loading/resource_manager.h" #include "weapons/test_gun.h" #include "weapons/light_blaster.h" #include "weapons/medium_blaster.h" #include "weapons/heavy_blaster.h" #include "weapons/boomerang_gun.h" #include "weapons/turret.h" #include "weapons/cannon.h" #include "particles/dot_emitter.h" #include "particles/sprite_particles.h" #include "util/loading/factory.h" #include "key_mapper.h" #include "network_game_manager.h" #include "shared_network_data.h" #include "power_ups/weapon_power_up.h" #include "power_ups/param_power_up.h" #include "graphics_engine.h" #include "plane.h" #include "state.h" #include "player.h" #include "util/loading/load_param.h" // #include "lib/gui/gl_gui/glgui_bar.h" // #include "lib/gui/gl_gui/glgui_pushbutton.h" #include "class_id_DEPRECATED.h" ObjectListDefinitionID(SpaceShip, CL_SPACE_SHIP); CREATE_FACTORY(SpaceShip); #include "script_class.h" CREATE_SCRIPTABLE_CLASS(SpaceShip, addMethod("hasPlayer", Executor0ret(&Playable::hasPlayer)) ->addMethod("fire", Executor1(&Playable::fire)) ->addMethod("loadModel", Executor2(&WorldEntity::loadModel2)) ->addMethod("setName", Executor1(&BaseObject::setName)) ->addMethod("hide", Executor0(&WorldEntity::hide)) ->addMethod("unhide", Executor0(&WorldEntity::unhide)) //Coordinates ->addMethod("setAbsCoor", Executor3(&PNode::setAbsCoor)) ->addMethod("getAbsCoorX", Executor0ret(&PNode::getAbsCoorX)) ->addMethod("getAbsCoorY", Executor0ret(&PNode::getAbsCoorY)) ->addMethod("getAbsCoorZ", Executor0ret(&PNode::getAbsCoorZ)) ); /** * destructs the spaceship, deletes alocated memory */ SpaceShip::~SpaceShip () { this->setPlayer(NULL); } /** * loads a Spaceships information from a specified file. * @param fileName the name of the File to load the spaceship from (absolute path) */ SpaceShip::SpaceShip(const std::string& fileName) : secWeaponMan(this), supportedPlaymodes(Playable::Full3D || Playable::Horizontal || Playable::Vertical), playmode(Playable::Full3D) { this->init(); TiXmlDocument doc(fileName); if(!doc.LoadFile()) { PRINTF(2)("Loading file %s failed for spaceship.\n", fileName.c_str()); return; } this->loadParams(doc.RootElement()); } /** * creates a new Spaceship from Xml Data * @param root the xml element containing spaceship data @todo add more parameters to load */ SpaceShip::SpaceShip(const TiXmlElement* root) : secWeaponMan(this), supportedPlaymodes(Playable::Full3D || Playable::Horizontal || Playable::Vertical), playmode(Playable::Full3D) { this->init(); if (root != NULL) this->loadParams(root); } /** * initializes a Spaceship */ void SpaceShip::init() { // this->setRelDir(Quaternion(M_PI, Vector(1,0,0))); this->registerObject(this, SpaceShip::_objectList); PRINTF(4)("SPACESHIP INIT\n"); secWeaponMan.showCrosshair(); //weapons: Weapon* wpRight1 = new LightBlaster (); wpRight1->setName("Light Blaster Right"); Weapon* wpLeft1 = new LightBlaster (); wpLeft1->setName("Medium Blaster Left"); Weapon* wpRight2 = new MediumBlaster (); wpRight2->setName("Medium Blaster Right"); Weapon* wpLeft2 = new MediumBlaster (); wpLeft2->setName("Medium Blaster Left"); Weapon* wpRight3 = new HeavyBlaster (); wpRight3->setName("Heavy Blaster Right"); Weapon* wpLeft3 = new HeavyBlaster (); wpLeft3->setName("Heavy Blaster Left"); Weapon* cannon = new BoomerangGun();//Cannon(); cannon->setName("End of World"); this->weaponMan.addWeapon( wpLeft1, 0, 0); this->weaponMan.addWeapon( wpRight1, 0, 1); this->weaponMan.addWeapon( wpLeft2, 0, 2); this->weaponMan.addWeapon( wpRight2, 0, 3); this->weaponMan.addWeapon( wpLeft3, 0, 4); this->weaponMan.addWeapon( wpRight3, 0, 5); this->secWeaponMan.addWeapon( cannon, 0, 0); this->weaponMan.changeWeaponConfig(0); wpRight1->requestAction(WA_ACTIVATE); wpLeft1->requestAction(WA_ACTIVATE); wpRight2->requestAction(WA_ACTIVATE); wpLeft2->requestAction(WA_ACTIVATE); wpRight3->requestAction(WA_ACTIVATE); wpLeft3->requestAction(WA_ACTIVATE); cannon->requestAction(WA_ACTIVATE); curWeaponPrimary = 0; curWeaponSecondary = 0; Playable::weaponConfigChanged(); reactorOutput = 10; weaponEnergyRegen = 10; // 10 einheiten pro Sekunde engineSpeedBase = 5; shieldRegen = 2; shieldEnergyShare = 0.3; weaponEnergyShare = 0.3; engineEnergyShare = 0.4; shieldCur = 20; shieldMax = 100; /* this->addWeapon(wpLeft, 1, 0); this->addWeapon(wpRight,1 ,1); //this->addWeapon(cannon, 0, 6); this->getWeaponManager().changeWeaponConfig(1); */ this->loadModel("models/ships/mantawing.obj"); bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bFire = bSecFire = false; xMouse = yMouse = 0; yInvert = 1; mouseSensitivity = 0.001; airViscosity = 0.9; controlVelocityX = 25; controlVelocityY = 150; shipInertia = 1.5; // cycle = 0.0; this->setHealthMax(shieldMax); this->setHealth(shieldCur); travelSpeed = 0.0; acceleration = 3; this->velocity = this->getAbsDirX()*travelSpeed; this->mouseDir = this->getAbsDir(); this->pitchDir = this->getAbsDir(); // GLGuiButton* button = new GLGuiPushButton(); // button->show(); // button->setLabel("orxonox"); // button->setBindNode(this); // GLGuiBar* bar = new GLGuiBar(); // bar->show(); // bar->setValue(7.0); // bar->setMaximum(10); // bar->setSize2D( 20, 100); // bar->setAbsCoor2D( 10, 200); //add events to the eventlist registerEvent(KeyMapper::PEV_FORWARD); registerEvent(KeyMapper::PEV_BACKWARD); registerEvent(KeyMapper::PEV_LEFT); registerEvent(KeyMapper::PEV_RIGHT); //registerEvent(SDLK_q); //registerEvent(SDLK_e); registerEvent(KeyMapper::PEV_FIRE1); registerEvent(KeyMapper::PEV_FIRE2); // Added for secondary weapon support registerEvent(KeyMapper::PEV_NEXT_WEAPON); registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON); //registerEvent(SDLK_PAGEUP); //registerEvent(SDLK_PAGEDOWN); registerEvent(EV_MOUSE_MOTION); this->weaponMan.setSlotCount(6); this->weaponMan.setSlotPosition(0, Vector(-2.6, .1, -3.0)); this->weaponMan.setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); this->weaponMan.setSlotPosition(1, Vector(-2.6, .1, 3.0)); this->weaponMan.setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); this->weaponMan.setSlotPosition(2, Vector(-1.5, .5, -.5)); this->weaponMan.setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0))); this->weaponMan.setSlotPosition(3, Vector(-1.5, .5, .5)); this->weaponMan.setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0))); this->weaponMan.setSlotPosition(4, Vector(-1.5, -.5, .5)); this->weaponMan.setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0))); this->weaponMan.setSlotPosition(5, Vector(-1.5, -.5, -.5)); this->weaponMan.setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0))); this->secWeaponMan.setSlotCount(6); this->secWeaponMan.setSlotPosition(0, Vector(2.6, -2.5, 0)); this->secWeaponMan.setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); this->secWeaponMan.setSlotPosition(1, Vector(2.6, .1, 3.0)); this->secWeaponMan.setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); this->secWeaponMan.setSlotPosition(2, Vector(1.5, .5, -.5)); this->secWeaponMan.setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0))); this->secWeaponMan.setSlotPosition(3, Vector(1.5, .5, .5)); this->secWeaponMan.setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0))); this->secWeaponMan.setSlotPosition(4, Vector(1.5, -.5, .5)); this->secWeaponMan.setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0))); this->secWeaponMan.setSlotPosition(5, Vector(1.5, -.5, -.5)); this->secWeaponMan.setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0))); // // this->getWeaponManager().setSlotPosition(8, Vector(-2.5, -0.3, -2.0)); // this->getWeaponManager().setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0))); // // this->getWeaponManager().setSlotPosition(9, Vector(-2.5, -0.3, 2.0)); // this->getWeaponManager().setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));: this->weaponMan.getFixedTarget()->setParent(this); this->weaponMan.getFixedTarget()->setRelCoor(100000,0,0); this->secWeaponMan.getFixedTarget()->setParent(this); this->secWeaponMan.getFixedTarget()->setRelCoor(100000,0,0); dynamic_cast(this->weaponMan.getFixedTarget())->setVisibility( false); this->burstEmitter = new DotEmitter(200, 0.0, .01); this->burstEmitter->setParent(this); this->burstEmitter->setRelCoor(-1, .5, 0); this->burstEmitter->setName("SpaceShip_Burst_emitter"); this->burstSystem = new SpriteParticles(1000); this->burstSystem->addEmitter(this->burstEmitter); this->burstSystem->setName("SpaceShip_Burst_System"); ((SpriteParticles*)this->burstSystem)->setMaterialTexture("maps/radial-trans-noise.png"); this->burstSystem->setLifeSpan(1.0, .3); this->burstSystem->setRadius(0.0, 1.0); this->burstSystem->setRadius(0.05, 1.0); this->burstSystem->setRadius(.5, .8); this->burstSystem->setRadius(1.0, 0); this->burstSystem->setColor(0.0, .7,.7,1,.7); this->burstSystem->setColor(0.2, 0,0,0.8,.5); this->burstSystem->setColor(0.5, .5,.5,.8,.8); this->burstSystem->setColor(1.0, .8,.8,.8,.0); registerVar( new SynchronizeableVector( &velocity, &velocity, "velocity", PERMISSION_MASTER_SERVER ) ); registerVar( new SynchronizeableQuaternion( &mouseDir, &mouseDir, "mousedir", PERMISSION_OWNER ) ); registerVar( new SynchronizeableBool( &bUp, &bUp, "bUp", PERMISSION_OWNER ) ); registerVar( new SynchronizeableBool( &bDown, &bDown, "bDown", PERMISSION_OWNER ) ); registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) ); registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) ); registerVar( new SynchronizeableBool( &bAscend, &bAscend, "bAscend", PERMISSION_OWNER ) ); registerVar( new SynchronizeableBool( &bDescend, &bDescend, "bDescend", PERMISSION_OWNER ) ); registerVar( new SynchronizeableBool( &bRollL, &bRollL, "bRollL", PERMISSION_OWNER ) ); registerVar( new SynchronizeableBool( &bRollR, &bRollR, "bRollR", PERMISSION_OWNER ) ); } /** * loads the Settings of a SpaceShip from an XML-element. * @param root the XML-element to load the Spaceship's properties from */ void SpaceShip::loadParams(const TiXmlElement* root) { Playable::loadParams(root); } void SpaceShip::setPlayDirection(const Quaternion& quat, float speed) { this->mouseDir = quat; } void SpaceShip::reset() { bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bFire = bSecFire = false; xMouse = yMouse = 0; this->setHealth(80); this->velocity = Vector(0.0, 0.0, 0.0); } void SpaceShip::enter() { dynamic_cast(this->getWeaponManager().getFixedTarget())->setVisibility( true); this->attachCamera(); } void SpaceShip::leave() { dynamic_cast(this->getWeaponManager().getFixedTarget())->setVisibility( false); this->detachCamera(); } /** * effect that occurs after the SpaceShip is spawned */ void SpaceShip::postSpawn () { //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); } /** * the action occuring if the spaceship left the game */ void SpaceShip::leftWorld () {} WorldEntity* ref = NULL; /** * this function is called, when two entities collide * @param entity: the world entity with whom it collides * * Implement behaviour like damage application or other miscellaneous collision stuff in this function */ void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location) { } /** * draws the spaceship after transforming it. */ void SpaceShip::draw () const { WorldEntity::draw(); //this->debug(0); } /** * the function called for each passing timeSnap * @param time The timespan passed since last update */ void SpaceShip::tick (float time) { // Playable::tick(time);$ // Own Tick Setup, as a different fire routine is used on the weapon manager this->weaponMan.tick(time); this->secWeaponMan.tick(time); if( this->bFire) { this->weaponMan.fire(); } if( this->bSecFire) { this->secWeaponMan.fire(); this->bSecFire = !this->bSecFire; // FIXME This currently is needed to prevent "echo fires" of a second rocket after its cooldown has passed } // Shield Regeneration and other regular calculations on the ship this->regen(time); // Weapon Regeneration and other regular calculations on the ship this->weaponRegen(time); // current engine speed output this->engineSpeedCur = this->engineSpeedBase + this->reactorOutput * this->engineEnergyShare; if( ( xMouse != 0 || yMouse != 0 ) && this->getOwner() == SharedNetworkData::getInstance()->getHostID() ) { if (xMouse > controlVelocityX) xMouse = controlVelocityX; else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX; if (yMouse > controlVelocityY) yMouse = controlVelocityY; else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY; pitchDir = (Quaternion(xMouse*mouseSensitivity*0.5, Vector(1,0,0))); mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity*yInvert, Vector(0,0,1))*pitchDir); xMouse = yMouse = 0; } // if( this != State::getPlayer()->getControllable()) // return; // spaceship controlled movement fire(bool bF){ this->bFire = bF;} //if (this->getOwner() == this->getHostID()) this->calculateVelocity(time); Vector move = velocity*time; //orient the velocity in the direction of the spaceship. travelSpeed = velocity.len(); velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity; velocity = (velocity.getNormalized())*travelSpeed; this->burstEmitter->setEmissionRate(travelSpeed); this->burstEmitter->setEmissionVelocity(travelSpeed*.5, travelSpeed *.1); //orient the spaceship in direction of the mouse rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, 0.5);//fabsf(time)*shipInertia); if (this->getAbsDir().distance(rotQuat) > 0.00000000000001) this->setAbsDir( rotQuat); //this->setAbsDirSoft(mouseDir,5); // this is the air friction (necessary for a smooth control) if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001; else if (travelSpeed <= 80) velocity -= velocity.getNormalized()*travelSpeed*0.001; //other physics (gravity) //if(travelSpeed < 120) //move += Vector(0,-1,0)*60*time + Vector(0,1,0)*travelSpeed/2*time; //hoover effect //cycle += time; //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02); //readjust //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0))); //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0))); //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2); this->shiftCoor(move); // PRINTF(0)("id of %s is: %i\n", this->getName(), this->getOMListNumber()); } /** * calculate the velocity * @param time the timeslice since the last frame */ void SpaceShip::calculateVelocity (float time) { Vector accel(0.0, 0.0, 0.0); /* Vector rot(0.0, 0.0, 0.0); // wird ben�igt fr Helicopter */ //float rotVal = 0.0; /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ /* calculate the direction in which the craft is heading */ //Plane plane(Vector(0,1,0), Vector(0,0,0)); if( this->bUp ) { //this->shiftCoor(this->getAbsDirX()); //accel += (this->getAbsDirX())*2; accel += (this->getAbsDirX())*acceleration; } if( this->bDown ) { //this->shiftCoor((this->getAbsDirX())*-1); //accel -= (this->getAbsDirX())*2; //if(velocity.len() > 50) accel -= (this->getAbsDirX())*0.5*acceleration; } if( this->bLeft/* > -this->getRelCoor().z*2*/) { this->shiftDir(Quaternion(time, Vector(0,1,0))); // accel -= rightDirection; //velocityDir.normalize(); //rot +=Vector(1,0,0); //rotVal -= .4; } if( this->bRight /* > this->getRelCoor().z*2*/) { this->shiftDir(Quaternion(-time, Vector(0,1,0))); // accel += rightDirection; //velocityDir.normalize(); //rot += Vector(1,0,0); //rotVal += .4; } if( this->bRollL /* > -this->getRelCoor().z*2*/) { mouseDir *= Quaternion(-time*2, Vector(1,0,0)); // accel -= rightDirection; //velocityDir.normalize(); //rot +=Vector(1,0,0); //rotVal -= .4; } if( this->bRollR /* > this->getRelCoor().z*2*/) { mouseDir *= Quaternion(time*2, Vector(1,0,0)); // accel += rightDirection; //velocityDir.normalize(); //rot += Vector(1,0,0); //rotVal += .4; } if (this->bAscend ) { this->shiftDir(Quaternion(time, Vector(0,0,1))); // accel += upDirection; //velocityDir.normalize(); //rot += Vector(0,0,1); //rotVal += .4; } if (this->bDescend ) { this->shiftDir(Quaternion(-time, Vector(0,0,1))); // accel -= upDirection; //velocityDir.normalize(); //rot += Vector(0,0,1); //rotVal -= .4; } velocity += accel*time*10; //rot.normalize(); //this->setRelDirSoft(Quaternion(rotVal, rot), 5); } /** * @todo switch statement ?? */ void SpaceShip::process(const Event &event) { //Playable::process(event); if( event.type == KeyMapper::PEV_LEFT) this->bRollL = event.bPressed; else if( event.type == KeyMapper::PEV_RIGHT) this->bRollR = event.bPressed; else if( event.type == KeyMapper::PEV_FORWARD) this->bUp = event.bPressed; //this->shiftCoor(0,.1,0); else if( event.type == KeyMapper::PEV_BACKWARD) this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0); else if( event.type == KeyMapper::PEV_FIRE2) this->bSecFire = event.bPressed; else if( event.type == KeyMapper::PEV_FIRE1) this->bFire = event.bPressed; else if( event.type == EV_MOUSE_MOTION) { this->xMouse += event.xRel; this->yMouse += event.yRel; } } void SpaceShip::destroy( WorldEntity* killer ) { PRINTF(0)("spaceship destroy\n"); } void SpaceShip::respawn( ) { toList( OM_PLAYERS ); } void SpaceShip::damage(float pDamage, float eDamage){ if( this->shieldActive) { if( this->shieldCur > pDamage) { this->shieldCur = this->shieldCur - pDamage; } else { // shield <= pDamage this->shieldCur -=pDamage; this->shieldActive = false; //shield collapses pDamage = pDamage - this->shieldCur; if( !this->shieldActive) { this->armorCur -= pDamage / 2; // remaining damages hits armor at half rate this->electronicCur -= eDamage; } } } else { this->armorCur = this->armorCur - pDamage; this->electronicCur = this->electronicCur - eDamage; } if( this->armorCur <= 0) { /* FIXME implement shipcrash*/ } } void SpaceShip::regen(float time){ float tmp; if (this->armorCur != this->armorMax || this->armorRegen != 0){ tmp = this->armorCur + this->armorRegen * time; if ( tmp > electronicMax) this->armorCur = this->armorMax; else this->armorCur = tmp; } if (this->shieldCur != this->shieldMax || this->shieldRegen != 0){ tmp = this->shieldCur + (this->shieldRegen + this->reactorOutput * this->shieldEnergyShare) * time; if( tmp > shieldMax) this->shieldCur = this->shieldMax; else this->shieldCur = tmp; this->shieldActive = ( this->shieldActive || this->shieldCur > shieldTH); } this->setHealth( this->shieldCur); // FIXME currently just to test share system if (this->electronicCur != this->electronicMax || this->electronicRegen != 0){ tmp = this->electronicCur + this->electronicRegen * time; if ( tmp > electronicMax) this->electronicCur = this->electronicMax; else this->electronicCur = tmp; } } /** * Weapon regeneration * does not use any reactor capacity, as it wouldn't work in a consistent way. */ void SpaceShip::weaponRegen(float time) { float energy = ( this->reactorOutput * this->weaponEnergyShare + this->weaponEnergyRegen) * time; Weapon* weapon; for( unsigned int i=0; i < this->weaponMan.getSlotCount(); i++) { weapon = this->weaponMan.getWeapon(i); if( weapon != NULL && weapon->isActive()) { weapon->increaseEnergy( energy); } } // weaponMan.increaseAmmunition( weapon, energy); } void SpaceShip::enterPlaymode(Playable::Playmode playmode) { } void SpaceShip::movement (float dt) { }