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