/* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * 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 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * Fabian 'x3n' Landau * Co-authors: * Dominik Solenicki * */ #include "SectionController.h" namespace orxonox { RegisterClass(SectionController); //Leaders share the fact that they have Wingmans SectionController::SectionController(Context* context) : LeaderController(context) { RegisterObject(SectionController); this->setFormationMode(FormationMode::FINGER4); this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&SectionController::action, this))); this->myWingman_ = 0; this->myDivisionLeader_ = 0; this->rank_ = Rank::SECTIONLEADER; //orxout(internal_error) << this << "Was created" << endl; } SectionController::~SectionController() { } void SectionController::XMLPort(Element& xmlelement, XMLPort::Mode mode) { SUPER(SectionController, XMLPort, xmlelement, mode); //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); } //----in tick, move (or look) and shoot---- void SectionController::tick(float dt) { if (!this->isActive()) return; if (this->bHasTargetPosition_) { this->moveToTargetPosition(dt); } else if (this->bLookAtTarget_) { this->lookAtTarget(dt); } if (bShooting_) { this->doFire(); } SUPER(SectionController, tick, dt); } void SectionController::action() { //----If no leader, find one---- if (!myDivisionLeader_) { LeaderController* newDivisionLeader = findNewDivisionLeader(); this->myDivisionLeader_ = newDivisionLeader; } //----If have leader---- else { } //----action was set to fight---- if (this->action_ == Action::FIGHT) { if (!this->hasTarget()) { if (this->myDivisionLeader_) { this->chooseTarget(); } else { this->setClosestTarget(); } } else { //----fly in formation if far enough---- Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition(); if (diffVector.length() > 3000) { this->setTargetPositionOfWingman(); } else { //----wingmans shall support the fire of their leaders---- if (this->myWingman_) { this->myWingman_->setAction (Action::FIGHT, this->target_); } } } if (this->hasTarget()) { //----choose where to go---- this->maneuver(); //----fire if you can---- this->bShooting_ = this->canFire(); } } //----action was set to fly---- else if (this->action_ == Action::FLY) { this->setTargetPositionOfWingman(); } //----action was set to protect---- else if (this->action_ == Action::PROTECT) { } } //PRE: myDivisionLeader_ != 0 && myDivisionLeader_->action_ == Action::FIGHT //POST: this->target_ is set unless division leader doesn't have one void SectionController::chooseTarget() { //----If division leader fights, cover him by fighting emenies close to his target---- if (this->myDivisionLeader_->getAction() == Action::FIGHT) { //----if he has a target---- if (this->myDivisionLeader_->hasTarget()) { //----try to find a new target if division leader has wingman (doing fine) and no good target already set---- if ( this->myDivisionLeader_->hasWingman() && !( this->hasTarget() && this->getTarget() != this->myDivisionLeader_->getTarget() ) ) { bool foundTarget = false; //----new target should be close to division's target---- Vector3 divisionTargetPosition = this->myDivisionLeader_->getTarget()->getWorldPosition(); for (ObjectList::iterator itP = ObjectList::begin(); itP; ++itP) { //----is enemy?---- if ( CommonController::sameTeam (this->getControllableEntity(), static_cast(*itP)) ) continue; //----in range?---- if (((*itP)->getWorldPosition() - divisionTargetPosition).length() < 3000 && (*itP) != this->myDivisionLeader_->getTarget()) { foundTarget = true; this->setAction(Action::FIGHT, (*itP)); //orxout(internal_error) << "Found target" << endl; break; } } //----no target? then attack same target as division leader---- if (!foundTarget) { this->setAction(Action::FIGHT, this->myDivisionLeader_->getTarget()); } } //----if division leader doesn't have a wingman, support his fire---- else { this->setAction(Action::FIGHT, this->myDivisionLeader_->getTarget()); } } //----If he fights but doesn't have a target, wait for him to get one---- else { } } } //----stay in formation---- //gani-TODO: sum targetAbso... and this->predicted position void SectionController::setTargetPositionOfWingman() { if (!this->myWingman_) return; Vector3* targetRelativePositionOfWingman; switch (this->formationMode_){ case FormationMode::WALL: { targetRelativePositionOfWingman = new Vector3 (-400, 0, 0); break; } case FormationMode::FINGER4: { targetRelativePositionOfWingman = new Vector3 (-400, 0, 200); break; } case FormationMode::DIAMOND: { targetRelativePositionOfWingman = new Vector3 (400, -200, 0); break; } } Quaternion orient = this->getControllableEntity()->getWorldOrientation(); Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman))); myWingman_->setAction (Action::FLY, targetAbsolutePositionOfWingman, orient); } LeaderController* SectionController::findNewDivisionLeader() { if (!this->getControllableEntity()) return 0; LeaderController* closestLeader = 0; float minDistance = std::numeric_limits::infinity(); //go through all pawns for (ObjectList::iterator it = ObjectList::begin(); it; ++it) { //0ptr or not DivisionController? if (!(it) || !((it)->getRank() == Rank::DIVISIONLEADER) || !(it->getControllableEntity())) continue; //same team? if ((this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam())) continue; //is equal to this? if (orxonox_cast(*it) == this->getControllableEntity()) continue; float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity()); if (distance < minDistance && !(it->hasFollower())) { closestLeader = *it; minDistance = distance; } } if (closestLeader) { if (closestLeader->setFollower(this)) return closestLeader; } return 0; } bool SectionController::setWingman(CommonController* cwingman) { WeakPtr wingman = orxonox_cast(cwingman); if (!this->myWingman_) { this->myWingman_ = wingman; return true; } else { return false; } } bool SectionController::hasWingman() { if (this->myWingman_) return true; else return false; } }