| 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 |  | 
|---|
| 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_ = WINGMAN; | 
|---|
| 44 |  | 
|---|
| 45 |     } | 
|---|
| 46 |  | 
|---|
| 47 |     WingmanController::~WingmanController() | 
|---|
| 48 |     { | 
|---|
| 49 |  | 
|---|
| 50 |     } | 
|---|
| 51 |  | 
|---|
| 52 |     void WingmanController::tick(float dt) | 
|---|
| 53 |     {    | 
|---|
| 54 |         //------------------------------------------------------- | 
|---|
| 55 |             | 
|---|
| 56 |          | 
|---|
| 57 |         if (!this->isActive()) | 
|---|
| 58 |             return; | 
|---|
| 59 |         //--------------------------Stay in formation-------------------------- | 
|---|
| 60 |         if (this->bHasTargetPosition_) | 
|---|
| 61 |         { | 
|---|
| 62 |             //targetPosition_ and targetOrientation_ are set by the Leader in its action() | 
|---|
| 63 |             this->moveToTargetPosition(); | 
|---|
| 64 |         }  | 
|---|
| 65 |          | 
|---|
| 66 |         //--------------------------Attack same target as the Leader-------------------------- | 
|---|
| 67 |  | 
|---|
| 68 |         /*if (this->target_) | 
|---|
| 69 |         { | 
|---|
| 70 |             this->aimAtTarget(); | 
|---|
| 71 |             this->doFire(); | 
|---|
| 72 |         } | 
|---|
| 73 |         */ | 
|---|
| 74 |          | 
|---|
| 75 |         //orxout(internal_error) << "I am " << this << endl; | 
|---|
| 76 |  | 
|---|
| 77 |          | 
|---|
| 78 |         SUPER(WingmanController, tick, dt); | 
|---|
| 79 |     } | 
|---|
| 80 |      | 
|---|
| 81 |     void WingmanController::action() | 
|---|
| 82 |     { | 
|---|
| 83 |         if (!this->myLeader_) | 
|---|
| 84 |         { | 
|---|
| 85 |             CommonController* newLeader = findNewLeader(); | 
|---|
| 86 |             this->myLeader_ = newLeader; | 
|---|
| 87 |             if (newLeader) | 
|---|
| 88 |                 orxout(internal_error) << "new Leader set" << endl; | 
|---|
| 89 |             else | 
|---|
| 90 |                 orxout(internal_error) << "0 leader" << endl; | 
|---|
| 91 |  | 
|---|
| 92 |         } | 
|---|
| 93 |         else | 
|---|
| 94 |         { | 
|---|
| 95 |  | 
|---|
| 96 |         } | 
|---|
| 97 |     } | 
|---|
| 98 |       | 
|---|
| 99 |     | 
|---|
| 100 |      | 
|---|
| 101 |  | 
|---|
| 102 |     CommonController* WingmanController::findNewLeader() | 
|---|
| 103 |     { | 
|---|
| 104 |  | 
|---|
| 105 |         if (!this->getControllableEntity()) | 
|---|
| 106 |             return 0; | 
|---|
| 107 |  | 
|---|
| 108 |         CommonController* closestLeader = 0; | 
|---|
| 109 |         float minDistance =  std::numeric_limits<float>::infinity(); | 
|---|
| 110 |  | 
|---|
| 111 |         for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it) | 
|---|
| 112 |         { | 
|---|
| 113 |             //0ptr? | 
|---|
| 114 |             if (!it ||  | 
|---|
| 115 |                 (it->getRank() != SECTIONLEADER && it->getRank() != DIVISIONLEADER) ||  | 
|---|
| 116 |                 !(it->getControllableEntity())) | 
|---|
| 117 |                 continue; | 
|---|
| 118 |             //same team? | 
|---|
| 119 |             if (this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam()) | 
|---|
| 120 |                 continue; | 
|---|
| 121 |             //is equal to this? | 
|---|
| 122 |             if (it->getControllableEntity() == this->getControllableEntity()) | 
|---|
| 123 |                 continue; | 
|---|
| 124 |  | 
|---|
| 125 |             float distance = (it->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length(); | 
|---|
| 126 |             if (distance < minDistance && !(it->hasWingman())) | 
|---|
| 127 |             { | 
|---|
| 128 |                 closestLeader = *it; | 
|---|
| 129 |                 minDistance = distance; | 
|---|
| 130 |             } | 
|---|
| 131 |             | 
|---|
| 132 |         } | 
|---|
| 133 |         if (closestLeader) | 
|---|
| 134 |         { | 
|---|
| 135 |             if (closestLeader->setWingman(this)) | 
|---|
| 136 |                 return closestLeader; | 
|---|
| 137 |         } | 
|---|
| 138 |         return 0; | 
|---|
| 139 |     } | 
|---|
| 140 |  | 
|---|
| 141 |     void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 142 |     { | 
|---|
| 143 |         SUPER(WingmanController, XMLPort, xmlelement, mode); | 
|---|
| 144 |  | 
|---|
| 145 |         //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); | 
|---|
| 146 |     } | 
|---|
| 147 |  | 
|---|
| 148 |  | 
|---|
| 149 |  | 
|---|
| 150 | } | 
|---|