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