| 1 | /* |
|---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
|---|
| 3 | * > www.orxonox.net < |
|---|
| 4 | * |
|---|
| 5 | * |
|---|
| 6 | * License notice: |
|---|
| 7 | * |
|---|
| 8 | * This program is free software; you can redistribute it and/or |
|---|
| 9 | * modify it under the terms of the GNU General Public License |
|---|
| 10 | * as published by the Free Software Foundation; either version 2 |
|---|
| 11 | * of the License, or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, |
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | * GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * You should have received a copy of the GNU General Public License |
|---|
| 19 | * along with this program; if not, write to the Free Software |
|---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 21 | * |
|---|
| 22 | * Author: |
|---|
| 23 | * Fabian 'x3n' Landau |
|---|
| 24 | * Co-authors: |
|---|
| 25 | * Dominik Solenicki |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | #include "WingmanController.h" |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | namespace orxonox |
|---|
| 33 | { |
|---|
| 34 | |
|---|
| 35 | RegisterClass(WingmanController); |
|---|
| 36 | |
|---|
| 37 | //CommonController contains all common functionality of AI Controllers |
|---|
| 38 | WingmanController::WingmanController(Context* context) : CommonController(context) |
|---|
| 39 | { |
|---|
| 40 | RegisterObject(WingmanController); |
|---|
| 41 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&WingmanController::action, this))); |
|---|
| 42 | this->myLeader_ = 0; |
|---|
| 43 | this->rank_ = Rank::WINGMAN; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | WingmanController::~WingmanController() |
|---|
| 47 | { |
|---|
| 48 | |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
|---|
| 52 | { |
|---|
| 53 | SUPER(WingmanController, XMLPort, xmlelement, mode); |
|---|
| 54 | |
|---|
| 55 | //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | //----in tick, move (or look) and shoot---- |
|---|
| 59 | void WingmanController::tick(float dt) |
|---|
| 60 | { |
|---|
| 61 | if (!this->isActive()) |
|---|
| 62 | return; |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | SUPER(WingmanController, tick, dt); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | //----action for hard calculations---- |
|---|
| 69 | void WingmanController::action() |
|---|
| 70 | { |
|---|
| 71 | //----If no leader, find one---- |
|---|
| 72 | if (!this->myLeader_) |
|---|
| 73 | { |
|---|
| 74 | CommonController* newLeader = findNewLeader(); |
|---|
| 75 | this->myLeader_ = newLeader; |
|---|
| 76 | |
|---|
| 77 | } |
|---|
| 78 | //----If have leader, he will deal with logic---- |
|---|
| 79 | else |
|---|
| 80 | { |
|---|
| 81 | this->action_ = this->myLeader_->getAction(); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | //----action was set to fight---- |
|---|
| 86 | if (this->action_ == Action::FIGHT) |
|---|
| 87 | { |
|---|
| 88 | //----If no leader found, attack someone---- |
|---|
| 89 | if (!this->hasTarget() && !this->myLeader_) |
|---|
| 90 | { |
|---|
| 91 | this->setClosestTarget(); |
|---|
| 92 | } |
|---|
| 93 | if (this->hasTarget()) |
|---|
| 94 | { |
|---|
| 95 | //----choose where to go---- |
|---|
| 96 | this->maneuver(); |
|---|
| 97 | //----fire if you can---- |
|---|
| 98 | this->bShooting_ = this->canFire(); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | //----action was set to fly, leader handles the logic---- |
|---|
| 102 | else if (this->action_ == Action::FLY) |
|---|
| 103 | { |
|---|
| 104 | |
|---|
| 105 | } |
|---|
| 106 | //----gani-TODO: implement protect---- |
|---|
| 107 | else if (this->action_ == Action::PROTECT) |
|---|
| 108 | { |
|---|
| 109 | |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | //----POST: closest leader that is ready to take a new wingman is returned---- |
|---|
| 117 | CommonController* WingmanController::findNewLeader() |
|---|
| 118 | { |
|---|
| 119 | |
|---|
| 120 | if (!this->getControllableEntity()) |
|---|
| 121 | return 0; |
|---|
| 122 | |
|---|
| 123 | //----vars for finding the closest leader---- |
|---|
| 124 | CommonController* closestLeader = 0; |
|---|
| 125 | float minDistance = std::numeric_limits<float>::infinity(); |
|---|
| 126 | Gametype* gt = this->getGametype(); |
|---|
| 127 | for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it) |
|---|
| 128 | { |
|---|
| 129 | //----0ptr or not a leader or dead?---- |
|---|
| 130 | if (!it || |
|---|
| 131 | (it->getRank() != Rank::SECTIONLEADER && it->getRank() != Rank::DIVISIONLEADER) || |
|---|
| 132 | !(it->getControllableEntity())) |
|---|
| 133 | continue; |
|---|
| 134 | |
|---|
| 135 | //----same team?---- |
|---|
| 136 | if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity(), gt) ) |
|---|
| 137 | continue; |
|---|
| 138 | |
|---|
| 139 | //----check distance---- |
|---|
| 140 | float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity()); |
|---|
| 141 | if (distance < minDistance && !(it->hasWingman())) |
|---|
| 142 | { |
|---|
| 143 | closestLeader = *it; |
|---|
| 144 | minDistance = distance; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | } |
|---|
| 148 | if (closestLeader) |
|---|
| 149 | { |
|---|
| 150 | //----Racing conditions---- |
|---|
| 151 | if (closestLeader->setWingman(this)) |
|---|
| 152 | return closestLeader; |
|---|
| 153 | } |
|---|
| 154 | return 0; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | } |
|---|