| [10719] | 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 "SectionController.h" |
|---|
| 30 | |
|---|
| 31 | namespace orxonox |
|---|
| 32 | { |
|---|
| 33 | |
|---|
| 34 | RegisterClass(SectionController); |
|---|
| 35 | |
|---|
| [10826] | 36 | //Leaders share the fact that they have Wingmans |
|---|
| [10719] | 37 | SectionController::SectionController(Context* context) : LeaderController(context) |
|---|
| 38 | { |
|---|
| 39 | RegisterObject(SectionController); |
|---|
| [10759] | 40 | this->setFormationMode(FormationMode::FINGER4); |
|---|
| [10725] | 41 | |
|---|
| [10719] | 42 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&SectionController::action, this))); |
|---|
| [10725] | 43 | this->myWingman_ = 0; |
|---|
| 44 | this->myDivisionLeader_ = 0; |
|---|
| [10759] | 45 | this->rank_ = Rank::SECTIONLEADER; |
|---|
| [10719] | 46 | |
|---|
| [10826] | 47 | //orxout(internal_error) << this << "Was created" << endl; |
|---|
| [10719] | 48 | |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | SectionController::~SectionController() |
|---|
| 52 | { |
|---|
| [10725] | 53 | |
|---|
| 54 | } |
|---|
| [10826] | 55 | void SectionController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
|---|
| 56 | { |
|---|
| 57 | SUPER(SectionController, XMLPort, xmlelement, mode); |
|---|
| [10731] | 58 | |
|---|
| [10826] | 59 | //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | //----in tick, move (or look) and shoot---- |
|---|
| [10731] | 63 | void SectionController::tick(float dt) |
|---|
| 64 | { |
|---|
| [10805] | 65 | if (!this->isActive()) |
|---|
| [10731] | 66 | return; |
|---|
| 67 | if (this->bHasTargetPosition_) |
|---|
| 68 | { |
|---|
| [10789] | 69 | this->moveToTargetPosition(dt); |
|---|
| [10731] | 70 | } |
|---|
| [10805] | 71 | else if (this->bLookAtTarget_) |
|---|
| 72 | { |
|---|
| 73 | this->lookAtTarget(dt); |
|---|
| 74 | } |
|---|
| 75 | if (bShooting_) |
|---|
| 76 | { |
|---|
| 77 | this->doFire(); |
|---|
| 78 | } |
|---|
| [10731] | 79 | |
|---|
| 80 | SUPER(SectionController, tick, dt); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | void SectionController::action() |
|---|
| 84 | { |
|---|
| [10826] | 85 | //----If no leader, find one---- |
|---|
| [10731] | 86 | if (!myDivisionLeader_) |
|---|
| 87 | { |
|---|
| 88 | LeaderController* newDivisionLeader = findNewDivisionLeader(); |
|---|
| 89 | this->myDivisionLeader_ = newDivisionLeader; |
|---|
| [10826] | 90 | |
|---|
| [10731] | 91 | } |
|---|
| [10826] | 92 | //----If have leader---- |
|---|
| [10780] | 93 | else |
|---|
| [10805] | 94 | { |
|---|
| [10826] | 95 | } |
|---|
| [10805] | 96 | |
|---|
| [10826] | 97 | //----action was set to fight---- |
|---|
| [10805] | 98 | if (this->action_ == Action::FIGHT) |
|---|
| 99 | { |
|---|
| [10832] | 100 | if (!this->hasTarget()) |
|---|
| [10805] | 101 | { |
|---|
| [10832] | 102 | if (this->myDivisionLeader_) |
|---|
| [10805] | 103 | { |
|---|
| [10832] | 104 | this->chooseTarget(); |
|---|
| [10805] | 105 | } |
|---|
| [10832] | 106 | else |
|---|
| 107 | { |
|---|
| 108 | this->setClosestTarget(); |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | else |
|---|
| 112 | { |
|---|
| 113 | |
|---|
| [10826] | 114 | //----fly in formation if far enough---- |
|---|
| [10805] | 115 | Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition(); |
|---|
| 116 | if (diffVector.length() > 3000) |
|---|
| 117 | { |
|---|
| 118 | this->setTargetPositionOfWingman(); |
|---|
| 119 | } |
|---|
| [10832] | 120 | else |
|---|
| 121 | { |
|---|
| 122 | //----wingmans shall support the fire of their leaders---- |
|---|
| 123 | if (this->myWingman_) |
|---|
| 124 | { |
|---|
| 125 | this->myWingman_->setAction (Action::FIGHT, this->target_); |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| [10805] | 128 | } |
|---|
| [10832] | 129 | if (this->hasTarget()) |
|---|
| 130 | { |
|---|
| 131 | //----choose where to go---- |
|---|
| 132 | this->maneuver(); |
|---|
| 133 | //----fire if you can---- |
|---|
| 134 | this->bShooting_ = this->canFire(); |
|---|
| 135 | } |
|---|
| [10805] | 136 | } |
|---|
| [10826] | 137 | |
|---|
| 138 | //----action was set to fly---- |
|---|
| [10805] | 139 | else if (this->action_ == Action::FLY) |
|---|
| 140 | { |
|---|
| 141 | this->setTargetPositionOfWingman(); |
|---|
| 142 | } |
|---|
| [10826] | 143 | |
|---|
| 144 | //----action was set to protect---- |
|---|
| [10805] | 145 | else if (this->action_ == Action::PROTECT) |
|---|
| 146 | { |
|---|
| 147 | |
|---|
| 148 | } |
|---|
| [10759] | 149 | |
|---|
| [10731] | 150 | |
|---|
| 151 | } |
|---|
| [10832] | 152 | //PRE: myDivisionLeader_ != 0 && myDivisionLeader_->action_ == Action::FIGHT |
|---|
| [10826] | 153 | //POST: this->target_ is set unless division leader doesn't have one |
|---|
| 154 | void SectionController::chooseTarget() |
|---|
| 155 | { |
|---|
| 156 | //----If division leader fights, cover him by fighting emenies close to his target---- |
|---|
| 157 | if (this->myDivisionLeader_->getAction() == Action::FIGHT) |
|---|
| 158 | { |
|---|
| 159 | //----if he has a target---- |
|---|
| 160 | if (this->myDivisionLeader_->hasTarget()) |
|---|
| 161 | { |
|---|
| 162 | //----try to find a new target if division leader has wingman (doing fine) and no good target already set---- |
|---|
| 163 | if ( this->myDivisionLeader_->hasWingman() && |
|---|
| 164 | !( this->hasTarget() && this->getTarget() != this->myDivisionLeader_->getTarget() ) ) |
|---|
| 165 | { |
|---|
| 166 | |
|---|
| 167 | bool foundTarget = false; |
|---|
| 168 | //----new target should be close to division's target---- |
|---|
| 169 | Vector3 divisionTargetPosition = this->myDivisionLeader_->getTarget()->getWorldPosition(); |
|---|
| 170 | |
|---|
| 171 | for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) |
|---|
| 172 | { |
|---|
| 173 | //----is enemy?---- |
|---|
| 174 | if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP)) ) |
|---|
| 175 | continue; |
|---|
| 176 | //----in range?---- |
|---|
| 177 | if (((*itP)->getWorldPosition() - divisionTargetPosition).length() < 3000 && |
|---|
| 178 | (*itP) != this->myDivisionLeader_->getTarget()) |
|---|
| 179 | { |
|---|
| 180 | foundTarget = true; |
|---|
| 181 | this->setAction(Action::FIGHT, (*itP)); |
|---|
| 182 | //orxout(internal_error) << "Found target" << endl; |
|---|
| 183 | break; |
|---|
| 184 | } |
|---|
| 185 | } |
|---|
| 186 | //----no target? then attack same target as division leader---- |
|---|
| 187 | if (!foundTarget) |
|---|
| 188 | { |
|---|
| 189 | this->setAction(Action::FIGHT, this->myDivisionLeader_->getTarget()); |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | //----if division leader doesn't have a wingman, support his fire---- |
|---|
| 193 | else |
|---|
| 194 | { |
|---|
| 195 | this->setAction(Action::FIGHT, this->myDivisionLeader_->getTarget()); |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | //----If he fights but doesn't have a target, wait for him to get one---- |
|---|
| 199 | else |
|---|
| 200 | { |
|---|
| 201 | |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | //----stay in formation---- |
|---|
| [10832] | 207 | //gani-TODO: sum targetAbso... and this->predicted position |
|---|
| [10729] | 208 | void SectionController::setTargetPositionOfWingman() |
|---|
| [10725] | 209 | { |
|---|
| 210 | if (!this->myWingman_) |
|---|
| 211 | return; |
|---|
| [10729] | 212 | Vector3* targetRelativePositionOfWingman; |
|---|
| [10725] | 213 | switch (this->formationMode_){ |
|---|
| [10759] | 214 | case FormationMode::WALL: |
|---|
| [10725] | 215 | { |
|---|
| [10729] | 216 | targetRelativePositionOfWingman = new Vector3 (-400, 0, 0); |
|---|
| [10725] | 217 | break; |
|---|
| 218 | } |
|---|
| [10759] | 219 | case FormationMode::FINGER4: |
|---|
| [10725] | 220 | { |
|---|
| [10832] | 221 | targetRelativePositionOfWingman = new Vector3 (-400, 0, 200); |
|---|
| [10725] | 222 | break; |
|---|
| 223 | } |
|---|
| [10759] | 224 | case FormationMode::DIAMOND: |
|---|
| [10725] | 225 | { |
|---|
| [10759] | 226 | targetRelativePositionOfWingman = new Vector3 (400, -200, 0); |
|---|
| [10725] | 227 | break; |
|---|
| 228 | } |
|---|
| [10719] | 229 | } |
|---|
| [10729] | 230 | Quaternion orient = this->getControllableEntity()->getWorldOrientation(); |
|---|
| 231 | |
|---|
| 232 | Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + |
|---|
| 233 | (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman))); |
|---|
| 234 | |
|---|
| [10805] | 235 | myWingman_->setAction (Action::FLY, targetAbsolutePositionOfWingman, orient); |
|---|
| [10725] | 236 | |
|---|
| [10719] | 237 | } |
|---|
| [10826] | 238 | |
|---|
| [10722] | 239 | LeaderController* SectionController::findNewDivisionLeader() |
|---|
| [10719] | 240 | { |
|---|
| 241 | |
|---|
| 242 | if (!this->getControllableEntity()) |
|---|
| [10722] | 243 | return 0; |
|---|
| [10719] | 244 | |
|---|
| [10722] | 245 | LeaderController* closestLeader = 0; |
|---|
| 246 | float minDistance = std::numeric_limits<float>::infinity(); |
|---|
| [10719] | 247 | //go through all pawns |
|---|
| 248 | for (ObjectList<LeaderController>::iterator it = ObjectList<LeaderController>::begin(); it; ++it) |
|---|
| 249 | { |
|---|
| [10722] | 250 | //0ptr or not DivisionController? |
|---|
| [10759] | 251 | if (!(it) || !((it)->getRank() == Rank::DIVISIONLEADER) || !(it->getControllableEntity())) |
|---|
| [10722] | 252 | continue; |
|---|
| [10719] | 253 | //same team? |
|---|
| 254 | if ((this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam())) |
|---|
| 255 | continue; |
|---|
| 256 | |
|---|
| 257 | //is equal to this? |
|---|
| 258 | if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity()) |
|---|
| 259 | continue; |
|---|
| 260 | |
|---|
| [10826] | 261 | float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity()); |
|---|
| [10722] | 262 | |
|---|
| 263 | if (distance < minDistance && !(it->hasFollower())) |
|---|
| 264 | { |
|---|
| 265 | closestLeader = *it; |
|---|
| 266 | minDistance = distance; |
|---|
| 267 | } |
|---|
| [10729] | 268 | |
|---|
| [10719] | 269 | } |
|---|
| [10722] | 270 | if (closestLeader) |
|---|
| 271 | { |
|---|
| 272 | if (closestLeader->setFollower(this)) |
|---|
| 273 | return closestLeader; |
|---|
| 274 | } |
|---|
| 275 | return 0; |
|---|
| [10719] | 276 | |
|---|
| 277 | } |
|---|
| [10731] | 278 | bool SectionController::setWingman(CommonController* cwingman) |
|---|
| 279 | { |
|---|
| 280 | WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman); |
|---|
| [10719] | 281 | |
|---|
| [10731] | 282 | if (!this->myWingman_) |
|---|
| [10719] | 283 | { |
|---|
| [10731] | 284 | this->myWingman_ = wingman; |
|---|
| 285 | return true; |
|---|
| [10719] | 286 | } |
|---|
| [10731] | 287 | else |
|---|
| 288 | { |
|---|
| 289 | return false; |
|---|
| 290 | } |
|---|
| [10729] | 291 | } |
|---|
| 292 | |
|---|
| [10731] | 293 | bool SectionController::hasWingman() |
|---|
| [10725] | 294 | { |
|---|
| [10731] | 295 | if (this->myWingman_) |
|---|
| 296 | return true; |
|---|
| 297 | else |
|---|
| 298 | return false; |
|---|
| [10719] | 299 | } |
|---|
| 300 | |
|---|
| 301 | |
|---|
| [10826] | 302 | |
|---|
| [10719] | 303 | |
|---|
| 304 | |
|---|
| 305 | } |
|---|