| [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); |
|---|
| [10759] | 39 | this->setFormationMode(FormationMode::FINGER4); |
|---|
| [10725] | 40 | |
|---|
| [10719] | 41 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&SectionController::action, this))); |
|---|
| [10725] | 42 | this->myWingman_ = 0; |
|---|
| 43 | this->myDivisionLeader_ = 0; |
|---|
| [10759] | 44 | this->rank_ = Rank::SECTIONLEADER; |
|---|
| [10719] | 45 | |
|---|
| [10725] | 46 | orxout(internal_error) << this << "Was created" << endl; |
|---|
| [10719] | 47 | |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | SectionController::~SectionController() |
|---|
| 51 | { |
|---|
| [10725] | 52 | |
|---|
| 53 | } |
|---|
| [10731] | 54 | |
|---|
| 55 | void SectionController::tick(float dt) |
|---|
| 56 | { |
|---|
| 57 | if (!this->isActive()) |
|---|
| 58 | return; |
|---|
| 59 | |
|---|
| [10762] | 60 | |
|---|
| [10731] | 61 | if (this->bHasTargetPosition_) |
|---|
| 62 | { |
|---|
| [10789] | 63 | this->moveToTargetPosition(dt); |
|---|
| [10731] | 64 | } |
|---|
| [10780] | 65 | if (this->bShooting_) |
|---|
| 66 | doFire(); |
|---|
| [10731] | 67 | |
|---|
| 68 | SUPER(SectionController, tick, dt); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | void SectionController::action() |
|---|
| 72 | { |
|---|
| 73 | //this->target_ = this->sectionTarget_; |
|---|
| 74 | if (!myDivisionLeader_) |
|---|
| 75 | { |
|---|
| 76 | LeaderController* newDivisionLeader = findNewDivisionLeader(); |
|---|
| 77 | this->myDivisionLeader_ = newDivisionLeader; |
|---|
| 78 | if (newDivisionLeader) |
|---|
| 79 | orxout(internal_error) << "new DivisionLeader set" << endl; |
|---|
| [10759] | 80 | else |
|---|
| 81 | { |
|---|
| 82 | |
|---|
| 83 | } |
|---|
| [10731] | 84 | |
|---|
| 85 | } |
|---|
| 86 | setTargetPositionOfWingman(); |
|---|
| [10759] | 87 | if (this->target_ && this->myWingman_) |
|---|
| 88 | this->myWingman_->setTarget(this->target_); |
|---|
| [10762] | 89 | if (canFire()) |
|---|
| [10780] | 90 | this->bShooting_ = true; |
|---|
| 91 | else |
|---|
| 92 | this->bShooting_ = false; |
|---|
| [10759] | 93 | |
|---|
| [10731] | 94 | |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| [10729] | 97 | void SectionController::setTargetPositionOfWingman() |
|---|
| [10725] | 98 | { |
|---|
| 99 | if (!this->myWingman_) |
|---|
| 100 | return; |
|---|
| [10729] | 101 | Vector3* targetRelativePositionOfWingman; |
|---|
| [10725] | 102 | switch (this->formationMode_){ |
|---|
| [10759] | 103 | case FormationMode::WALL: |
|---|
| [10725] | 104 | { |
|---|
| [10729] | 105 | targetRelativePositionOfWingman = new Vector3 (-400, 0, 0); |
|---|
| [10725] | 106 | break; |
|---|
| 107 | } |
|---|
| [10759] | 108 | case FormationMode::FINGER4: |
|---|
| [10725] | 109 | { |
|---|
| [10759] | 110 | targetRelativePositionOfWingman = new Vector3 (-400, 0, -200); |
|---|
| [10725] | 111 | break; |
|---|
| 112 | } |
|---|
| [10759] | 113 | case FormationMode::VEE: |
|---|
| [10725] | 114 | { |
|---|
| 115 | break; |
|---|
| 116 | } |
|---|
| [10759] | 117 | case FormationMode::DIAMOND: |
|---|
| [10725] | 118 | { |
|---|
| [10759] | 119 | targetRelativePositionOfWingman = new Vector3 (400, -200, 0); |
|---|
| [10725] | 120 | break; |
|---|
| 121 | } |
|---|
| [10719] | 122 | } |
|---|
| [10729] | 123 | Quaternion orient = this->getControllableEntity()->getWorldOrientation(); |
|---|
| 124 | |
|---|
| 125 | Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + |
|---|
| 126 | (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman))); |
|---|
| 127 | |
|---|
| 128 | myWingman_->setTargetOrientation(orient); |
|---|
| 129 | myWingman_->setTargetPosition(targetAbsolutePositionOfWingman); |
|---|
| [10725] | 130 | |
|---|
| [10719] | 131 | } |
|---|
| [10722] | 132 | LeaderController* SectionController::findNewDivisionLeader() |
|---|
| [10719] | 133 | { |
|---|
| 134 | |
|---|
| 135 | if (!this->getControllableEntity()) |
|---|
| [10722] | 136 | return 0; |
|---|
| [10719] | 137 | |
|---|
| [10722] | 138 | LeaderController* closestLeader = 0; |
|---|
| 139 | float minDistance = std::numeric_limits<float>::infinity(); |
|---|
| [10719] | 140 | //go through all pawns |
|---|
| 141 | for (ObjectList<LeaderController>::iterator it = ObjectList<LeaderController>::begin(); it; ++it) |
|---|
| 142 | { |
|---|
| [10722] | 143 | //0ptr or not DivisionController? |
|---|
| [10759] | 144 | if (!(it) || !((it)->getRank() == Rank::DIVISIONLEADER) || !(it->getControllableEntity())) |
|---|
| [10722] | 145 | continue; |
|---|
| [10719] | 146 | //same team? |
|---|
| 147 | if ((this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam())) |
|---|
| 148 | continue; |
|---|
| 149 | |
|---|
| 150 | //is equal to this? |
|---|
| 151 | if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity()) |
|---|
| 152 | continue; |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | float distance = ((it)->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length(); |
|---|
| [10722] | 156 | |
|---|
| 157 | if (distance < minDistance && !(it->hasFollower())) |
|---|
| 158 | { |
|---|
| 159 | closestLeader = *it; |
|---|
| 160 | minDistance = distance; |
|---|
| 161 | } |
|---|
| [10729] | 162 | |
|---|
| [10719] | 163 | } |
|---|
| [10722] | 164 | if (closestLeader) |
|---|
| 165 | { |
|---|
| 166 | if (closestLeader->setFollower(this)) |
|---|
| 167 | return closestLeader; |
|---|
| 168 | } |
|---|
| 169 | return 0; |
|---|
| [10719] | 170 | |
|---|
| 171 | } |
|---|
| [10731] | 172 | bool SectionController::setWingman(CommonController* cwingman) |
|---|
| 173 | { |
|---|
| 174 | WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman); |
|---|
| [10719] | 175 | |
|---|
| [10731] | 176 | if (!this->myWingman_) |
|---|
| [10719] | 177 | { |
|---|
| [10731] | 178 | this->myWingman_ = wingman; |
|---|
| 179 | return true; |
|---|
| [10719] | 180 | } |
|---|
| [10731] | 181 | else |
|---|
| 182 | { |
|---|
| 183 | return false; |
|---|
| 184 | } |
|---|
| [10729] | 185 | } |
|---|
| 186 | |
|---|
| [10731] | 187 | bool SectionController::hasWingman() |
|---|
| [10725] | 188 | { |
|---|
| [10731] | 189 | if (this->myWingman_) |
|---|
| 190 | return true; |
|---|
| 191 | else |
|---|
| 192 | return false; |
|---|
| [10719] | 193 | } |
|---|
| 194 | |
|---|
| 195 | void SectionController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
|---|
| 196 | { |
|---|
| 197 | SUPER(SectionController, XMLPort, xmlelement, mode); |
|---|
| 198 | |
|---|
| 199 | //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | } |
|---|