[10678] | 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 "WingmanController.h" |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | namespace orxonox |
---|
| 33 | { |
---|
| 34 | |
---|
| 35 | RegisterClass(WingmanController); |
---|
[10729] | 36 | |
---|
[10864] | 37 | //ActionpointController contains all common functionality of AI Controllers |
---|
| 38 | WingmanController::WingmanController(Context* context) : ActionpointController(context) |
---|
[10678] | 39 | { |
---|
| 40 | RegisterObject(WingmanController); |
---|
[10719] | 41 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&WingmanController::action, this))); |
---|
[10725] | 42 | this->myLeader_ = 0; |
---|
[10759] | 43 | this->rank_ = Rank::WINGMAN; |
---|
[10851] | 44 | this->bFirstAction_ = true; |
---|
| 45 | |
---|
[10678] | 46 | } |
---|
| 47 | |
---|
| 48 | WingmanController::~WingmanController() |
---|
| 49 | { |
---|
[10854] | 50 | for (size_t i = 0; i < this->actionpoints_.size(); ++i) |
---|
| 51 | { |
---|
| 52 | if(this->actionpoints_[i]) |
---|
| 53 | this->actionpoints_[i]->destroy(); |
---|
| 54 | } |
---|
| 55 | this->parsedActionpoints_.clear(); |
---|
| 56 | this->actionpoints_.clear(); |
---|
[10678] | 57 | } |
---|
[10826] | 58 | |
---|
| 59 | void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 60 | { |
---|
| 61 | SUPER(WingmanController, XMLPort, xmlelement, mode); |
---|
[10678] | 62 | |
---|
[10826] | 63 | //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | //----in tick, move (or look) and shoot---- |
---|
[10731] | 67 | void WingmanController::tick(float dt) |
---|
| 68 | { |
---|
| 69 | if (!this->isActive()) |
---|
| 70 | return; |
---|
| 71 | |
---|
[10843] | 72 | |
---|
[10731] | 73 | SUPER(WingmanController, tick, dt); |
---|
| 74 | } |
---|
| 75 | |
---|
[10826] | 76 | //----action for hard calculations---- |
---|
[10731] | 77 | void WingmanController::action() |
---|
| 78 | { |
---|
[10851] | 79 | |
---|
[10826] | 80 | //----If no leader, find one---- |
---|
[10731] | 81 | if (!this->myLeader_) |
---|
| 82 | { |
---|
[10864] | 83 | ActionpointController* newLeader = findNewLeader(); |
---|
[10731] | 84 | this->myLeader_ = newLeader; |
---|
[10832] | 85 | |
---|
[10731] | 86 | } |
---|
[10826] | 87 | //----If have leader, he will deal with logic---- |
---|
[10731] | 88 | else |
---|
| 89 | { |
---|
[10851] | 90 | |
---|
[10731] | 91 | } |
---|
[10851] | 92 | if (!this->myLeader_) |
---|
| 93 | { |
---|
[10864] | 94 | ActionpointController::action(); |
---|
[10805] | 95 | } |
---|
[10854] | 96 | else if (this->myLeader_) |
---|
[10805] | 97 | { |
---|
[10856] | 98 | switch (this->myLeader_->getAction()) |
---|
| 99 | { |
---|
[10858] | 100 | case Action::FIGHT: |
---|
| 101 | { |
---|
| 102 | if (!this->hasTarget()) |
---|
| 103 | { |
---|
| 104 | this->setTarget(this->myLeader_->getTarget()); |
---|
| 105 | } |
---|
| 106 | break; |
---|
| 107 | } |
---|
| 108 | case Action::FIGHTALL: |
---|
| 109 | { |
---|
| 110 | if (!this->hasTarget()) |
---|
| 111 | { |
---|
| 112 | this->setTarget(this->myLeader_->getTarget()); |
---|
| 113 | } |
---|
| 114 | break; |
---|
| 115 | } |
---|
| 116 | case Action::ATTACK: |
---|
| 117 | { |
---|
| 118 | if (!this->hasTarget()) |
---|
| 119 | { |
---|
| 120 | this->setTarget(this->myLeader_->getTarget()); |
---|
| 121 | } |
---|
| 122 | break; |
---|
| 123 | } |
---|
[10856] | 124 | default: |
---|
| 125 | { |
---|
| 126 | ControllableEntity* myEntity = this->getControllableEntity(); |
---|
| 127 | Vector3 myPosition = myEntity->getWorldPosition(); |
---|
| 128 | if (!this->myLeader_) |
---|
| 129 | { |
---|
| 130 | return; |
---|
| 131 | } |
---|
[10859] | 132 | |
---|
[10856] | 133 | ControllableEntity* leaderEntity = this->myLeader_->getControllableEntity(); |
---|
| 134 | Quaternion orient = leaderEntity->getWorldOrientation(); |
---|
| 135 | Vector3 leaderPosition = leaderEntity->getWorldPosition(); |
---|
| 136 | |
---|
| 137 | Vector3 targetRelativePosition = getFormationPosition(); |
---|
| 138 | if (!this->myLeader_) |
---|
| 139 | { |
---|
| 140 | return; |
---|
| 141 | } |
---|
| 142 | Vector3 targetAbsolutePosition = |
---|
| 143 | (leaderPosition + (orient*WorldEntity::FRONT) * (leaderEntity->getVelocity().length()/5) |
---|
| 144 | + (orient* (targetRelativePosition))); |
---|
| 145 | |
---|
| 146 | this->setAction (Action::FLY, targetAbsolutePosition, orient); |
---|
| 147 | if ((targetAbsolutePosition - myPosition).length() > this->tolerance_ * 1.5f) |
---|
| 148 | { |
---|
| 149 | this->boostControl(); |
---|
| 150 | } |
---|
| 151 | else |
---|
| 152 | { |
---|
| 153 | this->getControllableEntity()->boost(false); |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | } |
---|
[10858] | 157 | if (this->hasTarget()) |
---|
| 158 | { |
---|
| 159 | //----choose where to go---- |
---|
| 160 | this->maneuver(); |
---|
| 161 | //----fire if you can---- |
---|
| 162 | this->bShooting_ = this->canFire(); |
---|
| 163 | } |
---|
[10805] | 164 | } |
---|
[10854] | 165 | |
---|
[10731] | 166 | } |
---|
| 167 | |
---|
| 168 | |
---|
[10856] | 169 | Vector3 WingmanController::getFormationPosition () |
---|
| 170 | { |
---|
| 171 | this->setFormationMode( this->myLeader_->getFormationMode() ); |
---|
| 172 | Vector3* targetRelativePosition; |
---|
| 173 | |
---|
| 174 | if (this->myLeader_->getRank() == Rank::DIVISIONLEADER) |
---|
| 175 | { |
---|
| 176 | switch (this->formationMode_){ |
---|
| 177 | case FormationMode::WALL: |
---|
| 178 | { |
---|
[10859] | 179 | targetRelativePosition = new Vector3 (400, 0, 0); |
---|
[10856] | 180 | break; |
---|
| 181 | } |
---|
| 182 | case FormationMode::FINGER4: |
---|
| 183 | { |
---|
[10859] | 184 | targetRelativePosition = new Vector3 (400, 0, 200); |
---|
[10856] | 185 | break; |
---|
| 186 | } |
---|
| 187 | case FormationMode::DIAMOND: |
---|
| 188 | { |
---|
[10859] | 189 | targetRelativePosition = new Vector3 (400, 0, 200); |
---|
[10856] | 190 | break; |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | else |
---|
| 195 | { |
---|
[10859] | 196 | |
---|
[10856] | 197 | switch (this->formationMode_){ |
---|
| 198 | case FormationMode::WALL: |
---|
| 199 | { |
---|
[10859] | 200 | targetRelativePosition = new Vector3 (-400, 0, 0); |
---|
[10856] | 201 | break; |
---|
| 202 | } |
---|
| 203 | case FormationMode::FINGER4: |
---|
| 204 | { |
---|
[10859] | 205 | targetRelativePosition = new Vector3 (-400, 0, 200); |
---|
[10856] | 206 | break; |
---|
| 207 | } |
---|
| 208 | case FormationMode::DIAMOND: |
---|
| 209 | { |
---|
[10859] | 210 | targetRelativePosition = new Vector3 (400, -200, 0); |
---|
[10856] | 211 | break; |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | return *targetRelativePosition; |
---|
| 217 | } |
---|
[10826] | 218 | //----POST: closest leader that is ready to take a new wingman is returned---- |
---|
[10864] | 219 | ActionpointController* WingmanController::findNewLeader() |
---|
[10717] | 220 | { |
---|
| 221 | |
---|
| 222 | if (!this->getControllableEntity()) |
---|
[10722] | 223 | return 0; |
---|
[10717] | 224 | |
---|
[10826] | 225 | //----vars for finding the closest leader---- |
---|
[10864] | 226 | ActionpointController* closestLeader = 0; |
---|
[10722] | 227 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
[10838] | 228 | Gametype* gt = this->getGametype(); |
---|
[10864] | 229 | for (ObjectList<ActionpointController>::iterator it = ObjectList<ActionpointController>::begin(); it; ++it) |
---|
[10717] | 230 | { |
---|
[10826] | 231 | //----0ptr or not a leader or dead?---- |
---|
[10731] | 232 | if (!it || |
---|
[10759] | 233 | (it->getRank() != Rank::SECTIONLEADER && it->getRank() != Rank::DIVISIONLEADER) || |
---|
[10731] | 234 | !(it->getControllableEntity())) |
---|
[10722] | 235 | continue; |
---|
[10826] | 236 | |
---|
| 237 | //----same team?---- |
---|
[10838] | 238 | if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity(), gt) ) |
---|
[10717] | 239 | continue; |
---|
[10826] | 240 | |
---|
| 241 | //----check distance---- |
---|
| 242 | float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity()); |
---|
[10722] | 243 | if (distance < minDistance && !(it->hasWingman())) |
---|
| 244 | { |
---|
| 245 | closestLeader = *it; |
---|
| 246 | minDistance = distance; |
---|
| 247 | } |
---|
[10725] | 248 | |
---|
[10717] | 249 | } |
---|
[10722] | 250 | if (closestLeader) |
---|
| 251 | { |
---|
[10826] | 252 | //----Racing conditions---- |
---|
[10722] | 253 | if (closestLeader->setWingman(this)) |
---|
| 254 | return closestLeader; |
---|
| 255 | } |
---|
| 256 | return 0; |
---|
[10717] | 257 | } |
---|
[10722] | 258 | |
---|
[10719] | 259 | |
---|
| 260 | |
---|
[10678] | 261 | |
---|
| 262 | } |
---|