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