[10678] | 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); |
---|
[10729] | 36 | |
---|
[10826] | 37 | //CommonController contains all common functionality of AI Controllers |
---|
[10717] | 38 | WingmanController::WingmanController(Context* context) : CommonController(context) |
---|
[10678] | 39 | { |
---|
| 40 | RegisterObject(WingmanController); |
---|
[10719] | 41 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&WingmanController::action, this))); |
---|
[10725] | 42 | this->myLeader_ = 0; |
---|
[10759] | 43 | this->rank_ = Rank::WINGMAN; |
---|
[10678] | 44 | } |
---|
| 45 | |
---|
| 46 | WingmanController::~WingmanController() |
---|
| 47 | { |
---|
[10725] | 48 | |
---|
[10678] | 49 | } |
---|
[10826] | 50 | |
---|
| 51 | void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 52 | { |
---|
| 53 | SUPER(WingmanController, XMLPort, xmlelement, mode); |
---|
[10678] | 54 | |
---|
[10826] | 55 | //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | //----in tick, move (or look) and shoot---- |
---|
[10731] | 59 | void WingmanController::tick(float dt) |
---|
| 60 | { |
---|
| 61 | if (!this->isActive()) |
---|
| 62 | return; |
---|
[10805] | 63 | if (this->bHasTargetPosition_) |
---|
[10761] | 64 | { |
---|
[10805] | 65 | this->moveToTargetPosition(dt); |
---|
[10761] | 66 | } |
---|
[10805] | 67 | else if (this->bLookAtTarget_) |
---|
[10761] | 68 | { |
---|
[10805] | 69 | this->lookAtTarget(dt); |
---|
[10761] | 70 | } |
---|
[10805] | 71 | if (bShooting_) |
---|
[10731] | 72 | { |
---|
[10805] | 73 | this->doFire(); |
---|
| 74 | } |
---|
[10731] | 75 | |
---|
| 76 | SUPER(WingmanController, tick, dt); |
---|
| 77 | } |
---|
| 78 | |
---|
[10826] | 79 | //----action for hard calculations---- |
---|
[10731] | 80 | void WingmanController::action() |
---|
| 81 | { |
---|
[10826] | 82 | //----If no leader, find one---- |
---|
[10731] | 83 | if (!this->myLeader_) |
---|
| 84 | { |
---|
| 85 | CommonController* newLeader = findNewLeader(); |
---|
| 86 | this->myLeader_ = newLeader; |
---|
[10832] | 87 | |
---|
[10731] | 88 | } |
---|
[10826] | 89 | //----If have leader, he will deal with logic---- |
---|
[10731] | 90 | else |
---|
| 91 | { |
---|
[10832] | 92 | this->action_ = this->myLeader_->getAction(); |
---|
[10731] | 93 | } |
---|
[10826] | 94 | |
---|
| 95 | |
---|
| 96 | //----action was set to fight---- |
---|
[10805] | 97 | if (this->action_ == Action::FIGHT) |
---|
| 98 | { |
---|
[10832] | 99 | //----If no leader found, attack someone---- |
---|
| 100 | if (!this->hasTarget() && !this->myLeader_) |
---|
| 101 | { |
---|
| 102 | this->setClosestTarget(); |
---|
| 103 | } |
---|
| 104 | if (this->hasTarget()) |
---|
| 105 | { |
---|
| 106 | //----choose where to go---- |
---|
| 107 | this->maneuver(); |
---|
| 108 | //----fire if you can---- |
---|
| 109 | this->bShooting_ = this->canFire(); |
---|
| 110 | } |
---|
[10805] | 111 | } |
---|
[10826] | 112 | //----action was set to fly, leader handles the logic---- |
---|
[10805] | 113 | else if (this->action_ == Action::FLY) |
---|
| 114 | { |
---|
| 115 | |
---|
| 116 | } |
---|
[10832] | 117 | //----gani-TODO: implement protect---- |
---|
[10805] | 118 | else if (this->action_ == Action::PROTECT) |
---|
| 119 | { |
---|
| 120 | |
---|
| 121 | } |
---|
[10780] | 122 | |
---|
[10731] | 123 | } |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | |
---|
[10826] | 127 | //----POST: closest leader that is ready to take a new wingman is returned---- |
---|
[10717] | 128 | CommonController* WingmanController::findNewLeader() |
---|
| 129 | { |
---|
| 130 | |
---|
| 131 | if (!this->getControllableEntity()) |
---|
[10722] | 132 | return 0; |
---|
[10717] | 133 | |
---|
[10826] | 134 | //----vars for finding the closest leader---- |
---|
[10722] | 135 | CommonController* closestLeader = 0; |
---|
| 136 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
| 137 | |
---|
[10719] | 138 | for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it) |
---|
[10717] | 139 | { |
---|
[10826] | 140 | //----0ptr or not a leader or dead?---- |
---|
[10731] | 141 | if (!it || |
---|
[10759] | 142 | (it->getRank() != Rank::SECTIONLEADER && it->getRank() != Rank::DIVISIONLEADER) || |
---|
[10731] | 143 | !(it->getControllableEntity())) |
---|
[10722] | 144 | continue; |
---|
[10826] | 145 | |
---|
| 146 | //----same team?---- |
---|
| 147 | if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity()) ) |
---|
[10717] | 148 | continue; |
---|
[10826] | 149 | |
---|
| 150 | //----check distance---- |
---|
| 151 | float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity()); |
---|
[10722] | 152 | if (distance < minDistance && !(it->hasWingman())) |
---|
| 153 | { |
---|
| 154 | closestLeader = *it; |
---|
| 155 | minDistance = distance; |
---|
| 156 | } |
---|
[10725] | 157 | |
---|
[10717] | 158 | } |
---|
[10722] | 159 | if (closestLeader) |
---|
| 160 | { |
---|
[10826] | 161 | //----Racing conditions---- |
---|
[10722] | 162 | if (closestLeader->setWingman(this)) |
---|
| 163 | return closestLeader; |
---|
| 164 | } |
---|
| 165 | return 0; |
---|
[10717] | 166 | } |
---|
[10722] | 167 | |
---|
[10719] | 168 | |
---|
| 169 | |
---|
[10678] | 170 | |
---|
| 171 | } |
---|