[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; |
---|
| 63 | |
---|
[10843] | 64 | |
---|
[10731] | 65 | SUPER(WingmanController, tick, dt); |
---|
| 66 | } |
---|
| 67 | |
---|
[10826] | 68 | //----action for hard calculations---- |
---|
[10731] | 69 | void WingmanController::action() |
---|
| 70 | { |
---|
[10826] | 71 | //----If no leader, find one---- |
---|
[10731] | 72 | if (!this->myLeader_) |
---|
| 73 | { |
---|
| 74 | CommonController* newLeader = findNewLeader(); |
---|
| 75 | this->myLeader_ = newLeader; |
---|
[10832] | 76 | |
---|
[10731] | 77 | } |
---|
[10826] | 78 | //----If have leader, he will deal with logic---- |
---|
[10731] | 79 | else |
---|
| 80 | { |
---|
[10832] | 81 | this->action_ = this->myLeader_->getAction(); |
---|
[10731] | 82 | } |
---|
[10826] | 83 | |
---|
| 84 | |
---|
| 85 | //----action was set to fight---- |
---|
[10805] | 86 | if (this->action_ == Action::FIGHT) |
---|
| 87 | { |
---|
[10832] | 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 | } |
---|
[10805] | 100 | } |
---|
[10826] | 101 | //----action was set to fly, leader handles the logic---- |
---|
[10805] | 102 | else if (this->action_ == Action::FLY) |
---|
| 103 | { |
---|
| 104 | |
---|
| 105 | } |
---|
[10832] | 106 | //----gani-TODO: implement protect---- |
---|
[10805] | 107 | else if (this->action_ == Action::PROTECT) |
---|
| 108 | { |
---|
| 109 | |
---|
| 110 | } |
---|
[10780] | 111 | |
---|
[10731] | 112 | } |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | |
---|
[10826] | 116 | //----POST: closest leader that is ready to take a new wingman is returned---- |
---|
[10717] | 117 | CommonController* WingmanController::findNewLeader() |
---|
| 118 | { |
---|
| 119 | |
---|
| 120 | if (!this->getControllableEntity()) |
---|
[10722] | 121 | return 0; |
---|
[10717] | 122 | |
---|
[10826] | 123 | //----vars for finding the closest leader---- |
---|
[10722] | 124 | CommonController* closestLeader = 0; |
---|
| 125 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
[10838] | 126 | Gametype* gt = this->getGametype(); |
---|
[10719] | 127 | for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it) |
---|
[10717] | 128 | { |
---|
[10826] | 129 | //----0ptr or not a leader or dead?---- |
---|
[10731] | 130 | if (!it || |
---|
[10759] | 131 | (it->getRank() != Rank::SECTIONLEADER && it->getRank() != Rank::DIVISIONLEADER) || |
---|
[10731] | 132 | !(it->getControllableEntity())) |
---|
[10722] | 133 | continue; |
---|
[10826] | 134 | |
---|
| 135 | //----same team?---- |
---|
[10838] | 136 | if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity(), gt) ) |
---|
[10717] | 137 | continue; |
---|
[10826] | 138 | |
---|
| 139 | //----check distance---- |
---|
| 140 | float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity()); |
---|
[10722] | 141 | if (distance < minDistance && !(it->hasWingman())) |
---|
| 142 | { |
---|
| 143 | closestLeader = *it; |
---|
| 144 | minDistance = distance; |
---|
| 145 | } |
---|
[10725] | 146 | |
---|
[10717] | 147 | } |
---|
[10722] | 148 | if (closestLeader) |
---|
| 149 | { |
---|
[10826] | 150 | //----Racing conditions---- |
---|
[10722] | 151 | if (closestLeader->setWingman(this)) |
---|
| 152 | return closestLeader; |
---|
| 153 | } |
---|
| 154 | return 0; |
---|
[10717] | 155 | } |
---|
[10722] | 156 | |
---|
[10719] | 157 | |
---|
| 158 | |
---|
[10678] | 159 | |
---|
| 160 | } |
---|