[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; |
---|
[6695] | 46 | this->myMaster_ = 0; |
---|
| 47 | //this->slaveNumber_ = -1; |
---|
[6640] | 48 | this->team_ = -1;//new |
---|
[6683] | 49 | this->state_ = FREE;//new |
---|
[2362] | 50 | this->bShooting_ = false; |
---|
| 51 | this->bHasTargetPosition_ = false; |
---|
| 52 | this->targetPosition_ = Vector3::ZERO; |
---|
[6417] | 53 | |
---|
[5929] | 54 | this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this)); |
---|
[2362] | 55 | } |
---|
| 56 | |
---|
| 57 | ArtificialController::~ArtificialController() |
---|
| 58 | { |
---|
| 59 | } |
---|
| 60 | |
---|
[6601] | 61 | void ArtificialController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 62 | { |
---|
| 63 | SUPER(ArtificialController, XMLPort, xmlelement, mode); |
---|
| 64 | |
---|
| 65 | XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(0); |
---|
| 66 | } |
---|
| 67 | |
---|
[3049] | 68 | void ArtificialController::moveToPosition(const Vector3& target) |
---|
[2362] | 69 | { |
---|
| 70 | if (!this->getControllableEntity()) |
---|
| 71 | return; |
---|
| 72 | |
---|
[3049] | 73 | Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target); |
---|
| 74 | float distance = (target - this->getControllableEntity()->getPosition()).length(); |
---|
[2362] | 75 | |
---|
[2493] | 76 | if (this->target_ || distance > 10) |
---|
[2362] | 77 | { |
---|
| 78 | // Multiply with 0.8 to make them a bit slower |
---|
[6502] | 79 | this->getControllableEntity()->rotateYaw(-0.8f * sgn(coord.x) * coord.x*coord.x); |
---|
| 80 | this->getControllableEntity()->rotatePitch(0.8f * sgn(coord.y) * coord.y*coord.y); |
---|
[2362] | 81 | } |
---|
| 82 | |
---|
[2493] | 83 | if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength()) |
---|
[6502] | 84 | this->getControllableEntity()->moveFrontBack(-0.5f); // They don't brake with full power to give the player a chance |
---|
[2362] | 85 | else |
---|
[6502] | 86 | this->getControllableEntity()->moveFrontBack(0.8f); |
---|
[2362] | 87 | } |
---|
| 88 | |
---|
[3049] | 89 | void ArtificialController::moveToTargetPosition() |
---|
| 90 | { |
---|
| 91 | this->moveToPosition(this->targetPosition_); |
---|
| 92 | } |
---|
| 93 | |
---|
[6640] | 94 | int ArtificialController::getState() |
---|
| 95 | { |
---|
| 96 | return this->state_; |
---|
| 97 | } |
---|
| 98 | |
---|
[6695] | 99 | void ArtificialController::unregisterSlave() { |
---|
| 100 | if(myMaster_) |
---|
| 101 | { |
---|
| 102 | myMaster_->slaves.remove(this); |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
[6640] | 106 | void ArtificialController::searchNewMaster() |
---|
| 107 | { |
---|
[6695] | 108 | |
---|
[6640] | 109 | if (!this->getControllableEntity()) |
---|
| 110 | return; |
---|
| 111 | |
---|
| 112 | this->targetPosition_ = this->getControllableEntity()->getPosition(); |
---|
| 113 | this->forgetTarget(); |
---|
| 114 | |
---|
| 115 | //go through all pawns |
---|
| 116 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
| 117 | { |
---|
[6695] | 118 | |
---|
| 119 | //same team? |
---|
[6640] | 120 | if (!ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype())) |
---|
| 121 | continue; |
---|
| 122 | |
---|
[6695] | 123 | //has it an ArtificialController and is it a master? |
---|
| 124 | if (!it->getController()) |
---|
| 125 | continue; |
---|
[6640] | 126 | |
---|
| 127 | ArtificialController *controller = static_cast<ArtificialController*>(it->getController()); |
---|
[6695] | 128 | if (!controller || controller->getState() != MASTER) |
---|
[6640] | 129 | continue; |
---|
| 130 | |
---|
| 131 | //is pawn oneself? && is pawn in range? |
---|
[6695] | 132 | if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity()) //&& it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) < 1000 |
---|
[6640] | 133 | { |
---|
[6696] | 134 | if(controller->slaves.size() > 9) continue; |
---|
| 135 | |
---|
| 136 | this->freeAllSlaves(); |
---|
| 137 | this->slaves.clear(); |
---|
[6683] | 138 | this->state_ = SLAVE; |
---|
[6696] | 139 | |
---|
[6695] | 140 | this->myMaster_ = controller; |
---|
| 141 | controller->slaves.push_back(this); |
---|
[6696] | 142 | |
---|
[6695] | 143 | break; |
---|
[6640] | 144 | } |
---|
| 145 | }//for |
---|
| 146 | |
---|
| 147 | //hasn't encountered any masters in range? -> become a master |
---|
[6695] | 148 | if (state_!=SLAVE) state_ = MASTER; // keep in mind: what happens when two masters encounter eache other? -> has to be evaluated in the for loop within master mode in AIcontroller... |
---|
| 149 | |
---|
[6640] | 150 | } |
---|
| 151 | |
---|
[6683] | 152 | void ArtificialController::commandSlaves() { |
---|
| 153 | |
---|
[6695] | 154 | for(std::list<ArtificialController*>::iterator it = slaves.begin(); it != slaves.end(); it++) |
---|
| 155 | { |
---|
| 156 | (*it)->setTargetPosition(this->getControllableEntity()->getPosition()); |
---|
| 157 | } |
---|
| 158 | /* |
---|
[6683] | 159 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
| 160 | { |
---|
[6695] | 161 | |
---|
| 162 | if (!it->getController()) |
---|
| 163 | continue; |
---|
| 164 | |
---|
[6683] | 165 | ArtificialController *controller = static_cast<ArtificialController*>(it->getController()); |
---|
[6695] | 166 | if (!controller || controller->getState() != SLAVE) |
---|
[6683] | 167 | continue; |
---|
[6695] | 168 | //controller->setTargetPosition(this->getControllableEntity()->getPosition()); |
---|
[6683] | 169 | controller->targetPosition_ = this->getControllableEntity()->getPosition(); |
---|
[6695] | 170 | controller->bHasTargetPosition_ = true; |
---|
[6683] | 171 | } |
---|
[6695] | 172 | */ |
---|
[6683] | 173 | } |
---|
| 174 | |
---|
| 175 | void ArtificialController::freeAllSlaves() |
---|
| 176 | { |
---|
[6695] | 177 | |
---|
[6696] | 178 | |
---|
| 179 | for(std::list<ArtificialController*>::iterator it = slaves.begin(); it != slaves.end(); it++) |
---|
| 180 | { |
---|
| 181 | (*it)->state_ = FREE; |
---|
| 182 | } |
---|
| 183 | /* |
---|
[6683] | 184 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
| 185 | { |
---|
| 186 | ArtificialController *controller = static_cast<ArtificialController*>(it->getController()); |
---|
[6695] | 187 | if (controller && controller->getState() != SLAVE) |
---|
[6683] | 188 | continue; |
---|
| 189 | |
---|
| 190 | controller->state_ = FREE; |
---|
| 191 | } |
---|
[6696] | 192 | */ |
---|
| 193 | } |
---|
[6683] | 194 | |
---|
[6696] | 195 | void ArtificialController::loseMasterState() |
---|
| 196 | { |
---|
| 197 | this->freeAllSlaves(); |
---|
| 198 | this->state_ = FREE; |
---|
[6683] | 199 | } |
---|
| 200 | |
---|
[3049] | 201 | void ArtificialController::setTargetPosition(const Vector3& target) |
---|
| 202 | { |
---|
| 203 | this->targetPosition_ = target; |
---|
| 204 | this->bHasTargetPosition_ = true; |
---|
| 205 | } |
---|
| 206 | |
---|
[2362] | 207 | void ArtificialController::searchRandomTargetPosition() |
---|
| 208 | { |
---|
[2493] | 209 | this->targetPosition_ = Vector3(rnd(-2000,2000), rnd(-2000,2000), rnd(-2000,2000)); |
---|
[2362] | 210 | this->bHasTargetPosition_ = true; |
---|
| 211 | } |
---|
| 212 | |
---|
[3049] | 213 | void ArtificialController::setTarget(Pawn* target) |
---|
| 214 | { |
---|
| 215 | this->target_ = target; |
---|
| 216 | |
---|
| 217 | if (target) |
---|
| 218 | this->targetPosition_ = target->getPosition(); |
---|
| 219 | } |
---|
| 220 | |
---|
[2362] | 221 | void ArtificialController::searchNewTarget() |
---|
| 222 | { |
---|
| 223 | if (!this->getControllableEntity()) |
---|
| 224 | return; |
---|
| 225 | |
---|
| 226 | this->targetPosition_ = this->getControllableEntity()->getPosition(); |
---|
| 227 | this->forgetTarget(); |
---|
| 228 | |
---|
| 229 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
| 230 | { |
---|
[3049] | 231 | if (ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype())) |
---|
| 232 | continue; |
---|
| 233 | |
---|
[2506] | 234 | if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity()) |
---|
[2362] | 235 | { |
---|
| 236 | float speed = this->getControllableEntity()->getVelocity().length(); |
---|
| 237 | Vector3 distanceCurrent = this->targetPosition_ - this->getControllableEntity()->getPosition(); |
---|
| 238 | Vector3 distanceNew = it->getPosition() - this->getControllableEntity()->getPosition(); |
---|
| 239 | 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)) |
---|
| 240 | < 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)) |
---|
| 241 | { |
---|
| 242 | this->target_ = (*it); |
---|
| 243 | this->targetPosition_ = it->getPosition(); |
---|
| 244 | } |
---|
| 245 | } |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | void ArtificialController::forgetTarget() |
---|
| 250 | { |
---|
| 251 | this->target_ = 0; |
---|
| 252 | this->bShooting_ = false; |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | void ArtificialController::aimAtTarget() |
---|
| 256 | { |
---|
| 257 | if (!this->target_ || !this->getControllableEntity()) |
---|
| 258 | return; |
---|
| 259 | |
---|
[2493] | 260 | static const float hardcoded_projectile_speed = 1250; |
---|
[2362] | 261 | |
---|
[2493] | 262 | this->targetPosition_ = getPredictedPosition(this->getControllableEntity()->getPosition(), hardcoded_projectile_speed, this->target_->getPosition(), this->target_->getVelocity()); |
---|
[2362] | 263 | this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO); |
---|
[6417] | 264 | |
---|
| 265 | Pawn* pawn = dynamic_cast<Pawn*>(this->getControllableEntity()); |
---|
| 266 | if (pawn) |
---|
| 267 | pawn->setAimPosition(this->targetPosition_); |
---|
[2362] | 268 | } |
---|
| 269 | |
---|
| 270 | bool ArtificialController::isCloseAtTarget(float distance) const |
---|
| 271 | { |
---|
| 272 | if (!this->getControllableEntity()) |
---|
| 273 | return false; |
---|
| 274 | |
---|
| 275 | if (!this->target_) |
---|
| 276 | return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_) < distance*distance); |
---|
| 277 | else |
---|
| 278 | return (this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance); |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | bool ArtificialController::isLookingAtTarget(float angle) const |
---|
| 282 | { |
---|
| 283 | if (!this->getControllableEntity()) |
---|
| 284 | return false; |
---|
| 285 | |
---|
| 286 | return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle); |
---|
| 287 | } |
---|
| 288 | |
---|
[5929] | 289 | void ArtificialController::abandonTarget(Pawn* target) |
---|
[2362] | 290 | { |
---|
[5929] | 291 | if (target == this->target_) |
---|
| 292 | this->targetDied(); |
---|
[2362] | 293 | } |
---|
[3049] | 294 | |
---|
[5929] | 295 | void ArtificialController::targetDied() |
---|
| 296 | { |
---|
| 297 | this->forgetTarget(); |
---|
| 298 | this->searchRandomTargetPosition(); |
---|
| 299 | } |
---|
| 300 | |
---|
[3049] | 301 | bool ArtificialController::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype) |
---|
| 302 | { |
---|
| 303 | if (entity1 == entity2) |
---|
| 304 | return true; |
---|
| 305 | |
---|
| 306 | int team1 = -1; |
---|
| 307 | int team2 = -1; |
---|
| 308 | |
---|
[6601] | 309 | Controller* controller = 0; |
---|
| 310 | if (entity1->getController()) |
---|
| 311 | controller = entity1->getController(); |
---|
| 312 | else |
---|
| 313 | controller = entity1->getXMLController(); |
---|
| 314 | if (controller) |
---|
[3049] | 315 | { |
---|
[6601] | 316 | ArtificialController* ac = orxonox_cast<ArtificialController*>(controller); |
---|
| 317 | if (ac) |
---|
| 318 | team1 = ac->getTeam(); |
---|
[3049] | 319 | } |
---|
[6601] | 320 | |
---|
| 321 | if (entity1->getController()) |
---|
| 322 | controller = entity1->getController(); |
---|
| 323 | else |
---|
| 324 | controller = entity1->getXMLController(); |
---|
| 325 | if (controller) |
---|
[3049] | 326 | { |
---|
[6601] | 327 | ArtificialController* ac = orxonox_cast<ArtificialController*>(controller); |
---|
| 328 | if (ac) |
---|
| 329 | team2 = ac->getTeam(); |
---|
[3049] | 330 | } |
---|
| 331 | |
---|
[3325] | 332 | TeamDeathmatch* tdm = orxonox_cast<TeamDeathmatch*>(gametype); |
---|
[3049] | 333 | if (tdm) |
---|
| 334 | { |
---|
| 335 | if (entity1->getPlayer()) |
---|
| 336 | team1 = tdm->getTeam(entity1->getPlayer()); |
---|
| 337 | |
---|
| 338 | if (entity2->getPlayer()) |
---|
| 339 | team2 = tdm->getTeam(entity2->getPlayer()); |
---|
| 340 | } |
---|
| 341 | |
---|
[3086] | 342 | TeamBaseMatchBase* base = 0; |
---|
[3325] | 343 | base = orxonox_cast<TeamBaseMatchBase*>(entity1); |
---|
[3086] | 344 | if (base) |
---|
| 345 | { |
---|
| 346 | switch (base->getState()) |
---|
| 347 | { |
---|
[3280] | 348 | case BaseState::ControlTeam1: |
---|
[3086] | 349 | team1 = 0; |
---|
| 350 | break; |
---|
[3280] | 351 | case BaseState::ControlTeam2: |
---|
[3086] | 352 | team1 = 1; |
---|
| 353 | break; |
---|
[3280] | 354 | case BaseState::Uncontrolled: |
---|
[3086] | 355 | default: |
---|
| 356 | team1 = -1; |
---|
| 357 | } |
---|
| 358 | } |
---|
[3325] | 359 | base = orxonox_cast<TeamBaseMatchBase*>(entity2); |
---|
[3086] | 360 | if (base) |
---|
| 361 | { |
---|
| 362 | switch (base->getState()) |
---|
| 363 | { |
---|
[3280] | 364 | case BaseState::ControlTeam1: |
---|
[3086] | 365 | team2 = 0; |
---|
| 366 | break; |
---|
[3280] | 367 | case BaseState::ControlTeam2: |
---|
[3086] | 368 | team2 = 1; |
---|
| 369 | break; |
---|
[3280] | 370 | case BaseState::Uncontrolled: |
---|
[3086] | 371 | default: |
---|
| 372 | team2 = -1; |
---|
| 373 | } |
---|
| 374 | } |
---|
| 375 | |
---|
[3049] | 376 | return (team1 == team2 && team1 != -1); |
---|
| 377 | } |
---|
[2362] | 378 | } |
---|