[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 | |
---|
| 36 | SectionController::SectionController(Context* context) : LeaderController(context) |
---|
| 37 | { |
---|
| 38 | RegisterObject(SectionController); |
---|
[10729] | 39 | this->setFormationMode(WALL); |
---|
[10725] | 40 | |
---|
[10719] | 41 | bIsDivisionLeader_ = false; |
---|
| 42 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&SectionController::action, this))); |
---|
[10725] | 43 | this->myWingman_ = 0; |
---|
| 44 | this->myDivisionLeader_ = 0; |
---|
[10719] | 45 | |
---|
[10725] | 46 | orxout(internal_error) << this << "Was created" << endl; |
---|
[10719] | 47 | |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | SectionController::~SectionController() |
---|
| 51 | { |
---|
[10725] | 52 | |
---|
| 53 | } |
---|
[10729] | 54 | void SectionController::setTargetPositionOfWingman() |
---|
[10725] | 55 | { |
---|
| 56 | if (!this->myWingman_) |
---|
| 57 | return; |
---|
[10729] | 58 | Vector3* targetRelativePositionOfWingman; |
---|
[10725] | 59 | switch (this->formationMode_){ |
---|
| 60 | case WALL: |
---|
| 61 | { |
---|
[10729] | 62 | targetRelativePositionOfWingman = new Vector3 (-400, 0, 0); |
---|
[10725] | 63 | break; |
---|
| 64 | } |
---|
| 65 | case FINGER4: |
---|
| 66 | { |
---|
| 67 | break; |
---|
| 68 | } |
---|
| 69 | case VEE: |
---|
| 70 | { |
---|
| 71 | break; |
---|
| 72 | } |
---|
| 73 | case DIAMOND: |
---|
| 74 | { |
---|
| 75 | break; |
---|
| 76 | } |
---|
[10719] | 77 | } |
---|
[10729] | 78 | Quaternion orient = this->getControllableEntity()->getWorldOrientation(); |
---|
| 79 | |
---|
| 80 | Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + |
---|
| 81 | (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman))); |
---|
| 82 | |
---|
| 83 | myWingman_->setTargetOrientation(orient); |
---|
| 84 | myWingman_->setTargetPosition(targetAbsolutePositionOfWingman); |
---|
[10725] | 85 | |
---|
[10719] | 86 | } |
---|
[10729] | 87 | |
---|
[10722] | 88 | LeaderController* SectionController::findNewDivisionLeader() |
---|
[10719] | 89 | { |
---|
| 90 | |
---|
| 91 | if (!this->getControllableEntity()) |
---|
[10722] | 92 | return 0; |
---|
[10719] | 93 | |
---|
[10722] | 94 | LeaderController* closestLeader = 0; |
---|
| 95 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
[10719] | 96 | //go through all pawns |
---|
| 97 | for (ObjectList<LeaderController>::iterator it = ObjectList<LeaderController>::begin(); it; ++it) |
---|
| 98 | { |
---|
[10722] | 99 | //0ptr or not DivisionController? |
---|
| 100 | if (!(it) || !(it)->bIsDivisionLeader_ || !(it->getControllableEntity())) |
---|
| 101 | continue; |
---|
[10719] | 102 | //same team? |
---|
| 103 | if ((this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam())) |
---|
| 104 | continue; |
---|
| 105 | |
---|
| 106 | //is equal to this? |
---|
| 107 | if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity()) |
---|
| 108 | continue; |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | float distance = ((it)->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length(); |
---|
[10722] | 112 | |
---|
| 113 | if (distance < minDistance && !(it->hasFollower())) |
---|
| 114 | { |
---|
| 115 | closestLeader = *it; |
---|
| 116 | minDistance = distance; |
---|
| 117 | } |
---|
[10729] | 118 | |
---|
[10719] | 119 | } |
---|
[10722] | 120 | if (closestLeader) |
---|
| 121 | { |
---|
| 122 | if (closestLeader->setFollower(this)) |
---|
| 123 | return closestLeader; |
---|
| 124 | } |
---|
| 125 | return 0; |
---|
[10719] | 126 | |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | void SectionController::action() |
---|
| 130 | { |
---|
| 131 | //this->target_ = this->sectionTarget_; |
---|
| 132 | if (!myDivisionLeader_) |
---|
| 133 | { |
---|
| 134 | LeaderController* newDivisionLeader = findNewDivisionLeader(); |
---|
[10722] | 135 | this->myDivisionLeader_ = newDivisionLeader; |
---|
[10725] | 136 | if (newDivisionLeader) |
---|
[10719] | 137 | orxout(internal_error) << "new DivisionLeader set" << endl; |
---|
[10725] | 138 | |
---|
[10719] | 139 | } |
---|
[10729] | 140 | setTargetPositionOfWingman(); |
---|
| 141 | |
---|
| 142 | } |
---|
[10719] | 143 | /* |
---|
| 144 | Wingmen and Leaders attack target_, which is a member variable of their classes. |
---|
| 145 | Wingmen's target_ is set to sectionTarget_, which is a member variable of SectionController class, unless |
---|
| 146 | Wingman covers Leader's rear. |
---|
| 147 | Leader's target_ must always equal sectionTarget_. |
---|
| 148 | if section has a target, its Leader shoots at it, but doesn't follow. |
---|
| 149 | Every section is a part of division. Division consisting of one Section is still a division. |
---|
| 150 | Division's leader's target_ must always equal divisionTarget_, which is a member variable of DivisionController. |
---|
| 151 | Division leader ONLY can follow target_ while in formation flight. |
---|
| 152 | If Division doesn't have a target, Division Leader stays in place, unless it has a waypoint. |
---|
| 153 | Division Leader's sectionTarget_ must equal divisionTarget_, |
---|
| 154 | but the other section, that is not a leading section, can attack any target that is near divisonTarget_ |
---|
| 155 | |
---|
| 156 | */ |
---|
[10729] | 157 | |
---|
[10719] | 158 | void SectionController::tick(float dt) |
---|
[10725] | 159 | { |
---|
[10719] | 160 | if (!this->isActive()) |
---|
| 161 | return; |
---|
| 162 | |
---|
[10729] | 163 | if (this->bHasTargetPosition_) |
---|
| 164 | { |
---|
| 165 | this->moveToTargetPosition(); |
---|
| 166 | } |
---|
[10719] | 167 | |
---|
| 168 | |
---|
| 169 | SUPER(SectionController, tick, dt); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | |
---|
| 173 | void SectionController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 174 | { |
---|
| 175 | SUPER(SectionController, XMLPort, xmlelement, mode); |
---|
| 176 | |
---|
| 177 | //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | } |
---|