/* * 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" //TODO: formation vectors are wrong, fix it. // split classes. // weaponsystem. //-> Math ? 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; this->bFirstAction_ = true; //orxout(internal_error) << this << "Was created" << endl; } SectionController::~SectionController() { // if (this->myWingman_) // { // this->myWingman_->takeActionpoints(this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_); // } for (size_t i = 0; i < this->actionpoints_.size(); ++i) { if(this->actionpoints_[i]) this->actionpoints_[i]->destroy(); } this->parsedActionpoints_.clear(); this->actionpoints_.clear(); } 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; SUPER(SectionController, tick, dt); } void SectionController::action() { if (!this || !this->getControllableEntity()) return; //----If no leader, find one---- if (!myDivisionLeader_) { LeaderController* newDivisionLeader = findNewDivisionLeader(); this->myDivisionLeader_ = newDivisionLeader; } //----If have leader---- else { } if (!myDivisionLeader_) { ActionpointController::action(); if (!this || !this->getControllableEntity()) return; if (!(this->parsedActionpoints_.empty() && this->loopActionpoints_.empty())) { if (this->myWingman_) { this->myWingman_->takeActionpoints(this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_); } } } else if (myDivisionLeader_) { switch (myDivisionLeader_->getAction()) { case Action::FIGHT: { if (!this->hasTarget()) { this->chooseTarget(); } break; } case Action::FIGHTALL: { if (!this->hasTarget()) { this->chooseTarget(); } break; } case Action::ATTACK: { if (!this->hasTarget()) { this->chooseTarget(); } break; } default: { ControllableEntity* myEntity = this->getControllableEntity(); Vector3 myPosition = myEntity->getWorldPosition(); if (!this->myDivisionLeader_) { return; } ControllableEntity* leaderEntity = this->myDivisionLeader_->getControllableEntity(); Quaternion orient = leaderEntity->getWorldOrientation(); Vector3 leaderPosition = leaderEntity->getWorldPosition(); Vector3 targetRelativePosition = getFormationPosition(); if (!this->myDivisionLeader_) { return; } Vector3 targetAbsolutePosition = (leaderPosition + (orient*WorldEntity::FRONT) * (leaderEntity->getVelocity().length()/5) + (orient* (targetRelativePosition))); this->setAction (Action::FLY, targetAbsolutePosition, orient); if ((targetAbsolutePosition - myPosition).length() > this->tolerance_ * 1.5f) { this->boostControl(); } else { this->getControllableEntity()->boost(false); } } } if (this->hasTarget()) { //----choose where to go---- this->maneuver(); //----fire if you can---- this->bShooting_ = this->canFire(); } } } //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---- Action::Value action = this->myDivisionLeader_->getAction(); Pawn* target; if (action == Action::FIGHT || action == Action::FIGHTALL || action == Action::ATTACK) { //----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(); Gametype* gt = this->getGametype(); for (ObjectList::iterator itP = ObjectList::begin(); itP; ++itP) { //----is enemy?---- if ( CommonController::sameTeam (this->getControllableEntity(), static_cast(*itP), gt) ) continue; //----in range?---- if (((*itP)->getWorldPosition() - divisionTargetPosition).length() < 3000 && (*itP) != this->myDivisionLeader_->getTarget()) { foundTarget = true; target = (*itP); //orxout(internal_error) << "Found target" << endl; break; } } //----no target? then attack same target as division leader---- if (!foundTarget) { target = orxonox_cast(this->myDivisionLeader_->getTarget()); } } //----if division leader doesn't have a wingman, support his fire---- else { target = orxonox_cast(this->myDivisionLeader_->getTarget()); } } //----If he fights but doesn't have a target, wait for him to get one---- else { } this->setTarget (orxonox_cast(target)); } else { } } Vector3 SectionController::getFormationPosition () { this->setFormationMode( this->myDivisionLeader_->getFormationMode() ); Vector3* targetRelativePosition; switch (this->formationMode_){ case FormationMode::WALL: { targetRelativePosition = new Vector3 (-400, 0, 0); break; } case FormationMode::FINGER4: { targetRelativePosition = new Vector3 (-400, 0, 200); break; } case FormationMode::DIAMOND: { targetRelativePosition = new Vector3 (-400, 0, 200); break; } } return *targetRelativePosition; } 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(ActionpointController* 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; } }