[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 "OrxonoxStableHeaders.h" |
---|
| 30 | #include "ArtificialController.h" |
---|
| 31 | |
---|
| 32 | #include "core/CoreIncludes.h" |
---|
| 33 | #include "objects/worldentities/ControllableEntity.h" |
---|
| 34 | #include "objects/worldentities/pawns/Pawn.h" |
---|
| 35 | |
---|
[3049] | 36 | #include "objects/gametypes/TeamDeathmatch.h" |
---|
| 37 | #include "objects/controllers/WaypointPatrolController.h" |
---|
| 38 | |
---|
[2362] | 39 | namespace orxonox |
---|
| 40 | { |
---|
| 41 | ArtificialController::ArtificialController(BaseObject* creator) : Controller(creator) |
---|
| 42 | { |
---|
| 43 | RegisterObject(ArtificialController); |
---|
| 44 | |
---|
| 45 | this->target_ = 0; |
---|
| 46 | this->bShooting_ = false; |
---|
| 47 | this->bHasTargetPosition_ = false; |
---|
| 48 | this->targetPosition_ = Vector3::ZERO; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | ArtificialController::~ArtificialController() |
---|
| 52 | { |
---|
| 53 | } |
---|
| 54 | |
---|
[3049] | 55 | void ArtificialController::moveToPosition(const Vector3& target) |
---|
[2362] | 56 | { |
---|
| 57 | if (!this->getControllableEntity()) |
---|
| 58 | return; |
---|
| 59 | |
---|
[3049] | 60 | Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target); |
---|
| 61 | float distance = (target - this->getControllableEntity()->getPosition()).length(); |
---|
[2362] | 62 | |
---|
[2493] | 63 | if (this->target_ || distance > 10) |
---|
[2362] | 64 | { |
---|
| 65 | // Multiply with 0.8 to make them a bit slower |
---|
[2493] | 66 | this->getControllableEntity()->rotateYaw(-0.8 * sgn(coord.x) * coord.x*coord.x); |
---|
[2362] | 67 | this->getControllableEntity()->rotatePitch(0.8 * sgn(coord.y) * coord.y*coord.y); |
---|
| 68 | } |
---|
| 69 | |
---|
[2493] | 70 | if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength()) |
---|
[2362] | 71 | this->getControllableEntity()->moveFrontBack(-0.5); // They don't brake with full power to give the player a chance |
---|
| 72 | else |
---|
| 73 | this->getControllableEntity()->moveFrontBack(0.8); |
---|
| 74 | } |
---|
| 75 | |
---|
[3049] | 76 | void ArtificialController::moveToTargetPosition() |
---|
| 77 | { |
---|
| 78 | this->moveToPosition(this->targetPosition_); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | void ArtificialController::setTargetPosition(const Vector3& target) |
---|
| 82 | { |
---|
| 83 | this->targetPosition_ = target; |
---|
| 84 | this->bHasTargetPosition_ = true; |
---|
| 85 | } |
---|
| 86 | |
---|
[2362] | 87 | void ArtificialController::searchRandomTargetPosition() |
---|
| 88 | { |
---|
[2493] | 89 | this->targetPosition_ = Vector3(rnd(-2000,2000), rnd(-2000,2000), rnd(-2000,2000)); |
---|
[2362] | 90 | this->bHasTargetPosition_ = true; |
---|
| 91 | } |
---|
| 92 | |
---|
[3049] | 93 | void ArtificialController::setTarget(Pawn* target) |
---|
| 94 | { |
---|
| 95 | this->target_ = target; |
---|
| 96 | |
---|
| 97 | if (target) |
---|
| 98 | this->targetPosition_ = target->getPosition(); |
---|
| 99 | } |
---|
| 100 | |
---|
[2362] | 101 | void ArtificialController::searchNewTarget() |
---|
| 102 | { |
---|
| 103 | if (!this->getControllableEntity()) |
---|
| 104 | return; |
---|
| 105 | |
---|
| 106 | this->targetPosition_ = this->getControllableEntity()->getPosition(); |
---|
| 107 | this->forgetTarget(); |
---|
| 108 | |
---|
| 109 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
| 110 | { |
---|
[3049] | 111 | if (ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype())) |
---|
| 112 | continue; |
---|
| 113 | |
---|
[2506] | 114 | if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity()) |
---|
[2362] | 115 | { |
---|
| 116 | float speed = this->getControllableEntity()->getVelocity().length(); |
---|
| 117 | Vector3 distanceCurrent = this->targetPosition_ - this->getControllableEntity()->getPosition(); |
---|
| 118 | Vector3 distanceNew = it->getPosition() - this->getControllableEntity()->getPosition(); |
---|
| 119 | 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)) |
---|
| 120 | < 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)) |
---|
| 121 | { |
---|
| 122 | this->target_ = (*it); |
---|
| 123 | this->targetPosition_ = it->getPosition(); |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | void ArtificialController::forgetTarget() |
---|
| 130 | { |
---|
| 131 | this->target_ = 0; |
---|
| 132 | this->bShooting_ = false; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | void ArtificialController::aimAtTarget() |
---|
| 136 | { |
---|
| 137 | if (!this->target_ || !this->getControllableEntity()) |
---|
| 138 | return; |
---|
| 139 | |
---|
[2493] | 140 | static const float hardcoded_projectile_speed = 1250; |
---|
[2362] | 141 | |
---|
[2493] | 142 | this->targetPosition_ = getPredictedPosition(this->getControllableEntity()->getPosition(), hardcoded_projectile_speed, this->target_->getPosition(), this->target_->getVelocity()); |
---|
[2362] | 143 | this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | bool ArtificialController::isCloseAtTarget(float distance) const |
---|
| 147 | { |
---|
| 148 | if (!this->getControllableEntity()) |
---|
| 149 | return false; |
---|
| 150 | |
---|
| 151 | if (!this->target_) |
---|
| 152 | return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_) < distance*distance); |
---|
| 153 | else |
---|
| 154 | return (this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | bool ArtificialController::isLookingAtTarget(float angle) const |
---|
| 158 | { |
---|
| 159 | if (!this->getControllableEntity()) |
---|
| 160 | return false; |
---|
| 161 | |
---|
| 162 | return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle); |
---|
| 163 | } |
---|
| 164 | |
---|
[2493] | 165 | void ArtificialController::destroyedPawn(Pawn* ship) |
---|
[2362] | 166 | { |
---|
| 167 | if (ship == this->target_) |
---|
| 168 | { |
---|
| 169 | this->forgetTarget(); |
---|
| 170 | this->searchRandomTargetPosition(); |
---|
| 171 | } |
---|
| 172 | } |
---|
[3049] | 173 | |
---|
| 174 | bool ArtificialController::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype) |
---|
| 175 | { |
---|
| 176 | if (entity1 == entity2) |
---|
| 177 | return true; |
---|
| 178 | |
---|
| 179 | int team1 = -1; |
---|
| 180 | int team2 = -1; |
---|
| 181 | |
---|
| 182 | if (entity1->getXMLController()) |
---|
| 183 | { |
---|
| 184 | WaypointPatrolController* wpc = dynamic_cast<WaypointPatrolController*>(entity1->getXMLController()); |
---|
| 185 | if (wpc) |
---|
| 186 | team1 = wpc->getTeam(); |
---|
| 187 | } |
---|
| 188 | if (entity2->getXMLController()) |
---|
| 189 | { |
---|
| 190 | WaypointPatrolController* wpc = dynamic_cast<WaypointPatrolController*>(entity2->getXMLController()); |
---|
| 191 | if (wpc) |
---|
| 192 | team2 = wpc->getTeam(); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | TeamDeathmatch* tdm = dynamic_cast<TeamDeathmatch*>(gametype); |
---|
| 196 | if (tdm) |
---|
| 197 | { |
---|
| 198 | if (entity1->getPlayer()) |
---|
| 199 | team1 = tdm->getTeam(entity1->getPlayer()); |
---|
| 200 | |
---|
| 201 | if (entity2->getPlayer()) |
---|
| 202 | team2 = tdm->getTeam(entity2->getPlayer()); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | return (team1 == team2 && team1 != -1); |
---|
| 206 | } |
---|
[2362] | 207 | } |
---|