[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 | |
---|
[10717] | 31 | #include "core/CoreIncludes.h" |
---|
[10678] | 32 | |
---|
[10717] | 33 | #include "core/XMLPort.h" |
---|
| 34 | #include "core/command/ConsoleCommandIncludes.h" |
---|
| 35 | |
---|
| 36 | #include "worldentities/ControllableEntity.h" |
---|
| 37 | #include "worldentities/pawns/Pawn.h" |
---|
| 38 | |
---|
[10678] | 39 | namespace orxonox |
---|
| 40 | { |
---|
| 41 | |
---|
| 42 | RegisterClass(WingmanController); |
---|
[10719] | 43 | static const int RADIUS_TO_SEARCH_FOR_LEADER = 7000; |
---|
| 44 | static const float ACTION_INTERVAL = 1.0f; |
---|
[10717] | 45 | WingmanController::WingmanController(Context* context) : CommonController(context) |
---|
[10678] | 46 | { |
---|
| 47 | RegisterObject(WingmanController); |
---|
[10719] | 48 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&WingmanController::action, this))); |
---|
[10678] | 49 | } |
---|
| 50 | |
---|
| 51 | WingmanController::~WingmanController() |
---|
| 52 | { |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | /* void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 56 | { |
---|
| 57 | SUPER(WingmanController, XMLPort, xmlelement, mode); |
---|
| 58 | |
---|
| 59 | XMLPortParam(WingmanController, "accuracy", setAccuracy, getAccuracy, xmlelement, mode).defaultValues(100.0f); |
---|
| 60 | XMLPortObject(WingmanController, WorldEntity, "waypoints", addWaypoint, getWaypoint, xmlelement, mode); |
---|
| 61 | }*/ |
---|
[10717] | 62 | CommonController* WingmanController::findNewLeader() |
---|
| 63 | { |
---|
| 64 | |
---|
| 65 | if (!this->getControllableEntity()) |
---|
| 66 | return NULL; |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | //go through all pawns |
---|
[10719] | 70 | for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it) |
---|
[10717] | 71 | { |
---|
| 72 | //same team? |
---|
[10719] | 73 | if ((this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam())) |
---|
[10717] | 74 | continue; |
---|
| 75 | //is equal to this? |
---|
[10719] | 76 | if (it->getControllableEntity() == this->getControllableEntity()) |
---|
[10717] | 77 | continue; |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | //nullptr? |
---|
[10719] | 82 | if (!it || !it->isLeader()) |
---|
[10717] | 83 | continue; |
---|
| 84 | |
---|
[10719] | 85 | float distance = (it->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length(); |
---|
[10717] | 86 | |
---|
| 87 | // is pawn in range? |
---|
| 88 | if (distance < RADIUS_TO_SEARCH_FOR_LEADER) |
---|
| 89 | { |
---|
| 90 | |
---|
[10719] | 91 | if (it->setWingman(this)) |
---|
| 92 | return *it; |
---|
[10717] | 93 | } |
---|
| 94 | } |
---|
| 95 | return NULL; |
---|
| 96 | } |
---|
[10678] | 97 | void WingmanController::action() |
---|
| 98 | { |
---|
[10709] | 99 | //this->target_ = this->sectionTarget_; |
---|
[10717] | 100 | if (!myLeader_) |
---|
| 101 | { |
---|
| 102 | CommonController* newLeader = findNewLeader(); |
---|
| 103 | myLeader_ = newLeader; |
---|
[10719] | 104 | /* if (newLeader) |
---|
| 105 | orxout(internal_error) << "new Leader set" << endl; |
---|
| 106 | else |
---|
| 107 | orxout(internal_error) << "null leader" << endl; |
---|
| 108 | */ |
---|
[10717] | 109 | } |
---|
[10719] | 110 | else |
---|
| 111 | { |
---|
| 112 | //orxout(internal_error) << "already have a Leader" << endl; |
---|
| 113 | |
---|
| 114 | } |
---|
[10678] | 115 | } |
---|
[10719] | 116 | /*//collect data for AI behaviour |
---|
[10717] | 117 | Vector3* meanOfEnemiesPtr = new Vector3(0.0,0.0,0.0); |
---|
| 118 | Vector3* meanOfAlliesPtr = new Vector3(0.0,0.0,0.0); |
---|
| 119 | Vector3 meanOfAllies = *meanOfAlliesPtr; |
---|
| 120 | Vector3 meanOfEnemies = *meanOfEnemiesPtr; |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | for (ObjectList<AIController>::iterator it = ObjectList<AIController>::begin(); it; ++it) |
---|
| 124 | { |
---|
| 125 | |
---|
| 126 | Gametype* gt=this->getGametype(); |
---|
| 127 | if (!gt) |
---|
| 128 | { |
---|
| 129 | gt=it->getGametype(); |
---|
| 130 | } |
---|
| 131 | if (!FormationController::sameTeam(this->getControllableEntity(), it->getControllableEntity(),gt)) |
---|
| 132 | { |
---|
| 133 | enemies_.push_back(*it); |
---|
| 134 | } |
---|
| 135 | else { |
---|
| 136 | allies_.push_back(*it); |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | if (enemies_.size() != 0 && allies_.size() != 0){ |
---|
| 140 | for (std::vector<WeakPtr<AIController> >::iterator it = enemies_.begin() ; it != enemies_.end(); ++it) |
---|
| 141 | meanOfEnemies += (*it)->getControllableEntity()->getWorldPosition(); |
---|
| 142 | |
---|
| 143 | meanOfEnemies /= enemies_.size(); |
---|
| 144 | |
---|
| 145 | for (std::vector<WeakPtr<AIController> >::iterator it = allies_.begin() ; it != allies_.end(); ++it) |
---|
| 146 | meanOfAllies += (*it)->getControllableEntity()->getWorldPosition(); |
---|
| 147 | |
---|
| 148 | meanOfAllies /= allies_.size(); |
---|
| 149 | |
---|
| 150 | //orxout(internal_error) << "There are " << enemies_Counter << " enemies_, mean position is " << meanOfEnemies << endl; |
---|
| 151 | orxout(internal_error) << "Distance is " << (meanOfEnemies-meanOfAllies).length() << endl; |
---|
| 152 | orxout(internal_error) << "mean of allies_ is " << meanOfAllies << ", with a size " << allies_.size() << endl; |
---|
| 153 | orxout(internal_error) << "mean of enemies_ is " << meanOfEnemies << ", with a size " << enemies_.size() << endl; |
---|
| 154 | }*/ |
---|
[10709] | 155 | |
---|
[10719] | 156 | /* void FormationController::setDesiredPositionOfSlaves() |
---|
[10717] | 157 | { |
---|
| 158 | if (this->state_ != MASTER) |
---|
| 159 | return; |
---|
| 160 | switch (this->formationMode_){ |
---|
| 161 | case ATTACK: |
---|
| 162 | { |
---|
| 163 | float i = 0; |
---|
| 164 | for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++) |
---|
| 165 | { |
---|
| 166 | (*it)->desiredRelativePosition_ = new Vector3 ((i-slaves_.size()/2)*200, 0, 0); |
---|
| 167 | i++; |
---|
| 168 | } |
---|
| 169 | break; |
---|
| 170 | } |
---|
| 171 | case NORMAL: |
---|
| 172 | { |
---|
| 173 | break; |
---|
| 174 | } |
---|
| 175 | case DEFEND: |
---|
| 176 | { |
---|
| 177 | break; |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | }*/ |
---|
[10719] | 182 | void WingmanController::tick(float dt) |
---|
| 183 | { |
---|
| 184 | //------------------------------------------------------- |
---|
| 185 | |
---|
| 186 | /* |
---|
| 187 | if (!this->isActive()) |
---|
| 188 | return; |
---|
| 189 | //--------------------------Stay in formation-------------------------- |
---|
| 190 | if (bFollowLeader_) |
---|
| 191 | { |
---|
| 192 | this->keepSectionTick(); |
---|
| 193 | |
---|
| 194 | keepSectionTick(){ |
---|
| 195 | if (this->sectionLeader_ && this->sectionLeader_->getControllableEntity() && desiredRelativePosition_){ |
---|
| 196 | Vector3 desiredAbsolutePosition = ((this->sectionLeader_->getControllableEntity()->getWorldPosition()) + |
---|
| 197 | (this->sectionLeader_->getControllableEntity()->getWorldOrientation()* (*desiredRelativePosition_))); |
---|
| 198 | this->moveToPosition (desiredAbsolutePosition); |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | |
---|
| 203 | //--------------------------Attack same target as the Leader-------------------------- |
---|
| 204 | |
---|
| 205 | if (this->target_) |
---|
| 206 | { |
---|
| 207 | this->aimAtTarget(); |
---|
| 208 | this->doFire(); |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | */ |
---|
| 212 | //orxout(internal_error) << "I am " << this << endl; |
---|
| 213 | |
---|
[10709] | 214 | |
---|
[10678] | 215 | SUPER(WingmanController, tick, dt); |
---|
| 216 | } |
---|
[10719] | 217 | |
---|
| 218 | void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 219 | { |
---|
| 220 | SUPER(WingmanController, XMLPort, xmlelement, mode); |
---|
| 221 | |
---|
| 222 | //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); |
---|
| 223 | } |
---|
| 224 | |
---|
[10678] | 225 | //**********************************************NEW |
---|
[10681] | 226 | /*void WingmanController::defaultBehaviour(float maxrand) |
---|
[10678] | 227 | { |
---|
| 228 | |
---|
[10681] | 229 | }*/ |
---|
[10678] | 230 | |
---|
| 231 | } |
---|