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