[2362] | 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 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "ArtificialController.h" |
---|
| 30 | |
---|
| 31 | #include "core/CoreIncludes.h" |
---|
[6601] | 32 | #include "core/XMLPort.h" |
---|
[5735] | 33 | #include "worldentities/ControllableEntity.h" |
---|
| 34 | #include "worldentities/pawns/Pawn.h" |
---|
| 35 | #include "worldentities/pawns/TeamBaseMatchBase.h" |
---|
| 36 | #include "gametypes/TeamDeathmatch.h" |
---|
| 37 | #include "controllers/WaypointPatrolController.h" |
---|
[3049] | 38 | |
---|
[2362] | 39 | namespace orxonox |
---|
| 40 | { |
---|
| 41 | ArtificialController::ArtificialController(BaseObject* creator) : Controller(creator) |
---|
| 42 | { |
---|
| 43 | RegisterObject(ArtificialController); |
---|
| 44 | |
---|
| 45 | this->target_ = 0; |
---|
[6601] | 46 | this->team_ = 0;//new |
---|
| 47 | this->isMaster_ = false;//new |
---|
[2362] | 48 | this->bShooting_ = false; |
---|
| 49 | this->bHasTargetPosition_ = false; |
---|
| 50 | this->targetPosition_ = Vector3::ZERO; |
---|
[6417] | 51 | |
---|
[5929] | 52 | this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this)); |
---|
[2362] | 53 | } |
---|
| 54 | |
---|
| 55 | ArtificialController::~ArtificialController() |
---|
| 56 | { |
---|
| 57 | } |
---|
| 58 | |
---|
[6601] | 59 | void ArtificialController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 60 | { |
---|
| 61 | SUPER(ArtificialController, XMLPort, xmlelement, mode); |
---|
| 62 | |
---|
| 63 | XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(0); |
---|
| 64 | } |
---|
| 65 | |
---|
[3049] | 66 | void ArtificialController::moveToPosition(const Vector3& target) |
---|
[2362] | 67 | { |
---|
| 68 | if (!this->getControllableEntity()) |
---|
| 69 | return; |
---|
| 70 | |
---|
[3049] | 71 | Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target); |
---|
| 72 | float distance = (target - this->getControllableEntity()->getPosition()).length(); |
---|
[2362] | 73 | |
---|
[2493] | 74 | if (this->target_ || distance > 10) |
---|
[2362] | 75 | { |
---|
| 76 | // Multiply with 0.8 to make them a bit slower |
---|
[6502] | 77 | this->getControllableEntity()->rotateYaw(-0.8f * sgn(coord.x) * coord.x*coord.x); |
---|
| 78 | this->getControllableEntity()->rotatePitch(0.8f * sgn(coord.y) * coord.y*coord.y); |
---|
[2362] | 79 | } |
---|
| 80 | |
---|
[2493] | 81 | if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength()) |
---|
[6502] | 82 | this->getControllableEntity()->moveFrontBack(-0.5f); // They don't brake with full power to give the player a chance |
---|
[2362] | 83 | else |
---|
[6502] | 84 | this->getControllableEntity()->moveFrontBack(0.8f); |
---|
[2362] | 85 | } |
---|
| 86 | |
---|
[3049] | 87 | void ArtificialController::moveToTargetPosition() |
---|
| 88 | { |
---|
| 89 | this->moveToPosition(this->targetPosition_); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | void ArtificialController::setTargetPosition(const Vector3& target) |
---|
| 93 | { |
---|
| 94 | this->targetPosition_ = target; |
---|
| 95 | this->bHasTargetPosition_ = true; |
---|
| 96 | } |
---|
| 97 | |
---|
[2362] | 98 | void ArtificialController::searchRandomTargetPosition() |
---|
| 99 | { |
---|
[2493] | 100 | this->targetPosition_ = Vector3(rnd(-2000,2000), rnd(-2000,2000), rnd(-2000,2000)); |
---|
[2362] | 101 | this->bHasTargetPosition_ = true; |
---|
| 102 | } |
---|
| 103 | |
---|
[3049] | 104 | void ArtificialController::setTarget(Pawn* target) |
---|
| 105 | { |
---|
| 106 | this->target_ = target; |
---|
| 107 | |
---|
| 108 | if (target) |
---|
| 109 | this->targetPosition_ = target->getPosition(); |
---|
| 110 | } |
---|
| 111 | |
---|
[2362] | 112 | void ArtificialController::searchNewTarget() |
---|
| 113 | { |
---|
| 114 | if (!this->getControllableEntity()) |
---|
| 115 | return; |
---|
| 116 | |
---|
| 117 | this->targetPosition_ = this->getControllableEntity()->getPosition(); |
---|
| 118 | this->forgetTarget(); |
---|
| 119 | |
---|
| 120 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
| 121 | { |
---|
[3049] | 122 | if (ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype())) |
---|
| 123 | continue; |
---|
| 124 | |
---|
[2506] | 125 | if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity()) |
---|
[2362] | 126 | { |
---|
| 127 | float speed = this->getControllableEntity()->getVelocity().length(); |
---|
| 128 | Vector3 distanceCurrent = this->targetPosition_ - this->getControllableEntity()->getPosition(); |
---|
| 129 | Vector3 distanceNew = it->getPosition() - this->getControllableEntity()->getPosition(); |
---|
| 130 | if (!this->target_ || it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceNew) / speed / distanceNew.length()) / (2 * Ogre::Math::PI)) |
---|
| 131 | < this->targetPosition_.squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceCurrent) / speed / distanceCurrent.length()) / (2 * Ogre::Math::PI)) + rnd(-250, 250)) |
---|
| 132 | { |
---|
| 133 | this->target_ = (*it); |
---|
| 134 | this->targetPosition_ = it->getPosition(); |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | void ArtificialController::forgetTarget() |
---|
| 141 | { |
---|
| 142 | this->target_ = 0; |
---|
| 143 | this->bShooting_ = false; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | void ArtificialController::aimAtTarget() |
---|
| 147 | { |
---|
| 148 | if (!this->target_ || !this->getControllableEntity()) |
---|
| 149 | return; |
---|
| 150 | |
---|
[2493] | 151 | static const float hardcoded_projectile_speed = 1250; |
---|
[2362] | 152 | |
---|
[2493] | 153 | this->targetPosition_ = getPredictedPosition(this->getControllableEntity()->getPosition(), hardcoded_projectile_speed, this->target_->getPosition(), this->target_->getVelocity()); |
---|
[2362] | 154 | this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO); |
---|
[6417] | 155 | |
---|
| 156 | Pawn* pawn = dynamic_cast<Pawn*>(this->getControllableEntity()); |
---|
| 157 | if (pawn) |
---|
| 158 | pawn->setAimPosition(this->targetPosition_); |
---|
[2362] | 159 | } |
---|
| 160 | |
---|
| 161 | bool ArtificialController::isCloseAtTarget(float distance) const |
---|
| 162 | { |
---|
| 163 | if (!this->getControllableEntity()) |
---|
| 164 | return false; |
---|
| 165 | |
---|
| 166 | if (!this->target_) |
---|
| 167 | return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_) < distance*distance); |
---|
| 168 | else |
---|
| 169 | return (this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | bool ArtificialController::isLookingAtTarget(float angle) const |
---|
| 173 | { |
---|
| 174 | if (!this->getControllableEntity()) |
---|
| 175 | return false; |
---|
| 176 | |
---|
| 177 | return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle); |
---|
| 178 | } |
---|
| 179 | |
---|
[5929] | 180 | void ArtificialController::abandonTarget(Pawn* target) |
---|
[2362] | 181 | { |
---|
[5929] | 182 | if (target == this->target_) |
---|
| 183 | this->targetDied(); |
---|
[2362] | 184 | } |
---|
[3049] | 185 | |
---|
[5929] | 186 | void ArtificialController::targetDied() |
---|
| 187 | { |
---|
| 188 | this->forgetTarget(); |
---|
| 189 | this->searchRandomTargetPosition(); |
---|
| 190 | } |
---|
| 191 | |
---|
[3049] | 192 | bool ArtificialController::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype) |
---|
| 193 | { |
---|
| 194 | if (entity1 == entity2) |
---|
| 195 | return true; |
---|
| 196 | |
---|
| 197 | int team1 = -1; |
---|
| 198 | int team2 = -1; |
---|
| 199 | |
---|
[6601] | 200 | Controller* controller = 0; |
---|
| 201 | if (entity1->getController()) |
---|
| 202 | controller = entity1->getController(); |
---|
| 203 | else |
---|
| 204 | controller = entity1->getXMLController(); |
---|
| 205 | if (controller) |
---|
[3049] | 206 | { |
---|
[6601] | 207 | ArtificialController* ac = orxonox_cast<ArtificialController*>(controller); |
---|
| 208 | if (ac) |
---|
| 209 | team1 = ac->getTeam(); |
---|
[3049] | 210 | } |
---|
[6601] | 211 | |
---|
| 212 | if (entity1->getController()) |
---|
| 213 | controller = entity1->getController(); |
---|
| 214 | else |
---|
| 215 | controller = entity1->getXMLController(); |
---|
| 216 | if (controller) |
---|
[3049] | 217 | { |
---|
[6601] | 218 | ArtificialController* ac = orxonox_cast<ArtificialController*>(controller); |
---|
| 219 | if (ac) |
---|
| 220 | team2 = ac->getTeam(); |
---|
[3049] | 221 | } |
---|
| 222 | |
---|
[3325] | 223 | TeamDeathmatch* tdm = orxonox_cast<TeamDeathmatch*>(gametype); |
---|
[3049] | 224 | if (tdm) |
---|
| 225 | { |
---|
| 226 | if (entity1->getPlayer()) |
---|
| 227 | team1 = tdm->getTeam(entity1->getPlayer()); |
---|
| 228 | |
---|
| 229 | if (entity2->getPlayer()) |
---|
| 230 | team2 = tdm->getTeam(entity2->getPlayer()); |
---|
| 231 | } |
---|
| 232 | |
---|
[3086] | 233 | TeamBaseMatchBase* base = 0; |
---|
[3325] | 234 | base = orxonox_cast<TeamBaseMatchBase*>(entity1); |
---|
[3086] | 235 | if (base) |
---|
| 236 | { |
---|
| 237 | switch (base->getState()) |
---|
| 238 | { |
---|
[3280] | 239 | case BaseState::ControlTeam1: |
---|
[3086] | 240 | team1 = 0; |
---|
| 241 | break; |
---|
[3280] | 242 | case BaseState::ControlTeam2: |
---|
[3086] | 243 | team1 = 1; |
---|
| 244 | break; |
---|
[3280] | 245 | case BaseState::Uncontrolled: |
---|
[3086] | 246 | default: |
---|
| 247 | team1 = -1; |
---|
| 248 | } |
---|
| 249 | } |
---|
[3325] | 250 | base = orxonox_cast<TeamBaseMatchBase*>(entity2); |
---|
[3086] | 251 | if (base) |
---|
| 252 | { |
---|
| 253 | switch (base->getState()) |
---|
| 254 | { |
---|
[3280] | 255 | case BaseState::ControlTeam1: |
---|
[3086] | 256 | team2 = 0; |
---|
| 257 | break; |
---|
[3280] | 258 | case BaseState::ControlTeam2: |
---|
[3086] | 259 | team2 = 1; |
---|
| 260 | break; |
---|
[3280] | 261 | case BaseState::Uncontrolled: |
---|
[3086] | 262 | default: |
---|
| 263 | team2 = -1; |
---|
| 264 | } |
---|
| 265 | } |
---|
| 266 | |
---|
[3049] | 267 | return (team1 == team2 && team1 != -1); |
---|
| 268 | } |
---|
[2362] | 269 | } |
---|