[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); |
---|
[10719] | 36 | static const int RADIUS_TO_SEARCH_FOR_LEADER = 7000; |
---|
| 37 | static const float ACTION_INTERVAL = 1.0f; |
---|
[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; |
---|
| 43 | this->desiredRelativePosition_ = 0; |
---|
[10678] | 44 | } |
---|
| 45 | |
---|
| 46 | WingmanController::~WingmanController() |
---|
| 47 | { |
---|
[10725] | 48 | |
---|
[10678] | 49 | } |
---|
| 50 | |
---|
[10717] | 51 | CommonController* WingmanController::findNewLeader() |
---|
| 52 | { |
---|
| 53 | |
---|
| 54 | if (!this->getControllableEntity()) |
---|
[10722] | 55 | return 0; |
---|
[10717] | 56 | |
---|
[10722] | 57 | CommonController* closestLeader = 0; |
---|
| 58 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
| 59 | |
---|
[10719] | 60 | for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it) |
---|
[10717] | 61 | { |
---|
[10722] | 62 | //0ptr? |
---|
| 63 | if (!it || !it->isLeader() || !(it->getControllableEntity())) |
---|
| 64 | continue; |
---|
[10717] | 65 | //same team? |
---|
[10722] | 66 | if (this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam()) |
---|
[10717] | 67 | continue; |
---|
| 68 | //is equal to this? |
---|
[10719] | 69 | if (it->getControllableEntity() == this->getControllableEntity()) |
---|
[10717] | 70 | continue; |
---|
| 71 | |
---|
[10719] | 72 | float distance = (it->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length(); |
---|
[10722] | 73 | if (distance < minDistance && !(it->hasWingman())) |
---|
| 74 | { |
---|
| 75 | closestLeader = *it; |
---|
| 76 | minDistance = distance; |
---|
| 77 | } |
---|
[10725] | 78 | |
---|
[10717] | 79 | } |
---|
[10722] | 80 | if (closestLeader) |
---|
| 81 | { |
---|
| 82 | if (closestLeader->setWingman(this)) |
---|
| 83 | return closestLeader; |
---|
| 84 | } |
---|
| 85 | return 0; |
---|
[10717] | 86 | } |
---|
[10722] | 87 | |
---|
[10678] | 88 | void WingmanController::action() |
---|
| 89 | { |
---|
[10709] | 90 | //this->target_ = this->sectionTarget_; |
---|
[10722] | 91 | if (!this->myLeader_) |
---|
[10717] | 92 | { |
---|
| 93 | CommonController* newLeader = findNewLeader(); |
---|
[10722] | 94 | this->myLeader_ = newLeader; |
---|
| 95 | if (newLeader) |
---|
[10719] | 96 | orxout(internal_error) << "new Leader set" << endl; |
---|
| 97 | else |
---|
[10722] | 98 | orxout(internal_error) << "0 leader" << endl; |
---|
| 99 | |
---|
[10717] | 100 | } |
---|
[10719] | 101 | else |
---|
| 102 | { |
---|
[10722] | 103 | |
---|
[10719] | 104 | } |
---|
[10678] | 105 | } |
---|
[10719] | 106 | /*//collect data for AI behaviour |
---|
[10717] | 107 | Vector3* meanOfEnemiesPtr = new Vector3(0.0,0.0,0.0); |
---|
| 108 | Vector3* meanOfAlliesPtr = new Vector3(0.0,0.0,0.0); |
---|
| 109 | Vector3 meanOfAllies = *meanOfAlliesPtr; |
---|
| 110 | Vector3 meanOfEnemies = *meanOfEnemiesPtr; |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | for (ObjectList<AIController>::iterator it = ObjectList<AIController>::begin(); it; ++it) |
---|
| 114 | { |
---|
| 115 | |
---|
| 116 | Gametype* gt=this->getGametype(); |
---|
| 117 | if (!gt) |
---|
| 118 | { |
---|
| 119 | gt=it->getGametype(); |
---|
| 120 | } |
---|
| 121 | if (!FormationController::sameTeam(this->getControllableEntity(), it->getControllableEntity(),gt)) |
---|
| 122 | { |
---|
| 123 | enemies_.push_back(*it); |
---|
| 124 | } |
---|
| 125 | else { |
---|
| 126 | allies_.push_back(*it); |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | if (enemies_.size() != 0 && allies_.size() != 0){ |
---|
| 130 | for (std::vector<WeakPtr<AIController> >::iterator it = enemies_.begin() ; it != enemies_.end(); ++it) |
---|
| 131 | meanOfEnemies += (*it)->getControllableEntity()->getWorldPosition(); |
---|
| 132 | |
---|
| 133 | meanOfEnemies /= enemies_.size(); |
---|
| 134 | |
---|
| 135 | for (std::vector<WeakPtr<AIController> >::iterator it = allies_.begin() ; it != allies_.end(); ++it) |
---|
| 136 | meanOfAllies += (*it)->getControllableEntity()->getWorldPosition(); |
---|
| 137 | |
---|
| 138 | meanOfAllies /= allies_.size(); |
---|
| 139 | |
---|
| 140 | //orxout(internal_error) << "There are " << enemies_Counter << " enemies_, mean position is " << meanOfEnemies << endl; |
---|
| 141 | orxout(internal_error) << "Distance is " << (meanOfEnemies-meanOfAllies).length() << endl; |
---|
| 142 | orxout(internal_error) << "mean of allies_ is " << meanOfAllies << ", with a size " << allies_.size() << endl; |
---|
| 143 | orxout(internal_error) << "mean of enemies_ is " << meanOfEnemies << ", with a size " << enemies_.size() << endl; |
---|
| 144 | }*/ |
---|
[10709] | 145 | |
---|
[10725] | 146 | |
---|
| 147 | /*void WingmanController::keepSectionTick() |
---|
[10717] | 148 | { |
---|
[10725] | 149 | if (this->myLeader_ && this->myLeader_->getControllableEntity()) |
---|
| 150 | //orxout(internal_error) << "MOVING" << endl; |
---|
| 151 | |
---|
| 152 | if (this->myLeader_ && this->myLeader_->getControllableEntity() && desiredRelativePosition_) |
---|
| 153 | { |
---|
| 154 | Vector3 desiredAbsolutePosition = ((this->myLeader_->getControllableEntity()->getWorldPosition()) + |
---|
| 155 | (this->myLeader_->getControllableEntity()->getWorldOrientation()* (*desiredRelativePosition_))); |
---|
| 156 | this->moveToPosition (desiredAbsolutePosition); |
---|
[10717] | 157 | } |
---|
| 158 | }*/ |
---|
[10719] | 159 | void WingmanController::tick(float dt) |
---|
| 160 | { |
---|
[10725] | 161 | /* //------------------------------------------------------- |
---|
[10719] | 162 | |
---|
[10725] | 163 | |
---|
[10719] | 164 | if (!this->isActive()) |
---|
| 165 | return; |
---|
| 166 | //--------------------------Stay in formation-------------------------- |
---|
[10725] | 167 | this->keepSectionTick();*/ |
---|
[10719] | 168 | |
---|
[10725] | 169 | |
---|
| 170 | //--------------------------Attack same target as the Leader-------------------------- |
---|
[10719] | 171 | |
---|
[10725] | 172 | /*if (this->target_) |
---|
| 173 | { |
---|
| 174 | this->aimAtTarget(); |
---|
| 175 | this->doFire(); |
---|
[10719] | 176 | } |
---|
[10725] | 177 | */ |
---|
| 178 | |
---|
| 179 | //orxout(internal_error) << "I am " << this << endl; |
---|
[10719] | 180 | |
---|
[10709] | 181 | |
---|
[10678] | 182 | SUPER(WingmanController, tick, dt); |
---|
| 183 | } |
---|
[10719] | 184 | |
---|
| 185 | void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 186 | { |
---|
| 187 | SUPER(WingmanController, XMLPort, xmlelement, mode); |
---|
| 188 | |
---|
| 189 | //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); |
---|
| 190 | } |
---|
| 191 | |
---|
[10678] | 192 | //**********************************************NEW |
---|
[10681] | 193 | /*void WingmanController::defaultBehaviour(float maxrand) |
---|
[10678] | 194 | { |
---|
| 195 | |
---|
[10681] | 196 | }*/ |
---|
[10678] | 197 | |
---|
| 198 | } |
---|