[6847] | 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 | * Oli Scheuss |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "DroneController.h" |
---|
| 30 | #include "worldentities/Drone.h" |
---|
| 31 | #include "util/Math.h" |
---|
| 32 | |
---|
| 33 | #include "core/CoreIncludes.h" |
---|
[7284] | 34 | #include "core/command/Executor.h" |
---|
[6847] | 35 | #include "worldentities/ControllableEntity.h" |
---|
| 36 | |
---|
| 37 | namespace orxonox |
---|
| 38 | { |
---|
| 39 | /** |
---|
| 40 | @brief |
---|
| 41 | Constructor. |
---|
| 42 | */ |
---|
| 43 | CreateFactory(DroneController); |
---|
| 44 | |
---|
| 45 | static const float ACTION_INTERVAL = 1.0f; |
---|
| 46 | |
---|
| 47 | DroneController::DroneController(BaseObject* creator) : ArtificialController(creator) |
---|
| 48 | { |
---|
| 49 | RegisterObject(DroneController); |
---|
| 50 | |
---|
| 51 | this->owner_ = 0; |
---|
| 52 | this->drone_ = 0; |
---|
[7093] | 53 | this->isShooting_ = false; |
---|
[6847] | 54 | |
---|
| 55 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&DroneController::action, this))); |
---|
[6891] | 56 | |
---|
| 57 | this->owner_.setCallback(createFunctor(&DroneController::ownerDied, this)); |
---|
[6847] | 58 | } |
---|
| 59 | |
---|
| 60 | DroneController::~DroneController() |
---|
| 61 | { |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | void DroneController::setOwner(Pawn* owner){ |
---|
| 65 | this->owner_ = owner; |
---|
[7082] | 66 | } |
---|
[6847] | 67 | |
---|
| 68 | void DroneController::setDrone(Drone* drone) |
---|
| 69 | { |
---|
| 70 | this->drone_ = drone; |
---|
| 71 | this->setControllableEntity(drone); |
---|
| 72 | } |
---|
[7082] | 73 | |
---|
[6847] | 74 | void DroneController::action() |
---|
| 75 | { |
---|
| 76 | float random; |
---|
| 77 | float maxrand = 100.0f / ACTION_INTERVAL; |
---|
[7093] | 78 | float distanceToTargetSquared = 0; |
---|
[6847] | 79 | |
---|
[7093] | 80 | if (this->target_) |
---|
| 81 | distanceToTargetSquared = (getDrone()->getWorldPosition() - this->target_->getWorldPosition()).squaredLength(); |
---|
[7082] | 82 | |
---|
[6891] | 83 | random = rnd(maxrand); |
---|
[7060] | 84 | if ( random < 30 || (!this->target_) || distanceToTargetSquared > (this->getDrone()->getMaxShootingRange()*this->getDrone()->getMaxShootingRange())) |
---|
[6891] | 85 | this->searchNewTarget(); |
---|
[6847] | 86 | } |
---|
| 87 | |
---|
[7038] | 88 | /** |
---|
[6847] | 89 | @brief |
---|
| 90 | The controlling happens here. This method defines what the controller has to do each tick. |
---|
| 91 | @param dt |
---|
| 92 | The duration of the tick. |
---|
| 93 | */ |
---|
| 94 | void DroneController::tick(float dt) |
---|
| 95 | { |
---|
[7082] | 96 | if (this->getDrone() && this->getOwner()) |
---|
| 97 | { |
---|
[7093] | 98 | if (this->target_) |
---|
| 99 | { |
---|
| 100 | float distanceToTargetSquared = (this->getDrone()->getWorldPosition() - this->target_->getWorldPosition()).squaredLength(); |
---|
| 101 | if (distanceToTargetSquared < (this->getDrone()->getMaxShootingRange()*this->getDrone()->getMaxShootingRange())) |
---|
| 102 | { |
---|
| 103 | this->isShooting_ = true; |
---|
| 104 | this->aimAtTarget(); |
---|
[8238] | 105 | if(!this->friendlyFire()) |
---|
| 106 | this->getDrone()->fire(0); |
---|
[7093] | 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
[7082] | 110 | float maxDistanceSquared = this->getDrone()->getMaxDistanceToOwner()*this->getDrone()->getMaxDistanceToOwner(); |
---|
| 111 | float minDistanceSquared = this->getDrone()->getMinDistanceToOwner()*this->getDrone()->getMinDistanceToOwner(); |
---|
[7093] | 112 | if ((this->getDrone()->getWorldPosition() - this->getOwner()->getWorldPosition()).squaredLength() > maxDistanceSquared) |
---|
| 113 | { |
---|
[7082] | 114 | this->moveToPosition(this->getOwner()->getWorldPosition()); //fly towards owner |
---|
| 115 | } |
---|
[7093] | 116 | else if((this->getDrone()->getWorldPosition() - this->getOwner()->getWorldPosition()).squaredLength() < minDistanceSquared) |
---|
| 117 | { |
---|
[7082] | 118 | this->moveToPosition(-this->getOwner()->getWorldPosition()); //fly away from owner |
---|
| 119 | } |
---|
[7093] | 120 | else if (!this->isShooting_) |
---|
| 121 | { |
---|
[7082] | 122 | float random = rnd(2.0f); |
---|
| 123 | float randomSelection = rnd(6.0f); |
---|
| 124 | if((int)randomSelection==0) drone_->moveUpDown(random); |
---|
| 125 | else if((int)randomSelection==1) drone_->moveRightLeft(random); |
---|
| 126 | else if((int)randomSelection==2) drone_->moveFrontBack(random); |
---|
| 127 | else if((int)randomSelection==3) drone_->rotateYaw(random); |
---|
| 128 | else if((int)randomSelection==4) drone_->rotatePitch(random); |
---|
| 129 | else if((int)randomSelection==5) drone_->rotateRoll(random); |
---|
| 130 | } |
---|
[7093] | 131 | |
---|
| 132 | this->isShooting_ = false; |
---|
[6847] | 133 | } |
---|
[7082] | 134 | |
---|
[6847] | 135 | SUPER(AIController, tick, dt); |
---|
| 136 | |
---|
[6891] | 137 | } |
---|
[6847] | 138 | |
---|
[6891] | 139 | void DroneController::ownerDied() |
---|
| 140 | { |
---|
[7060] | 141 | // if (this->target_) { //Drone has some kind of Stockholm-Syndrom --> gets attached to Owners Killer |
---|
| 142 | // this->setOwner(target_); |
---|
| 143 | // this->searchNewTarget(); |
---|
| 144 | // } |
---|
[6891] | 145 | if (this->drone_) |
---|
| 146 | this->drone_->destroy(); |
---|
| 147 | else |
---|
| 148 | this->destroy(); |
---|
[6847] | 149 | } |
---|
[8238] | 150 | |
---|
| 151 | bool DroneController::friendlyFire() |
---|
| 152 | { ControllableEntity* droneEntity_ = this->getControllableEntity(); |
---|
| 153 | if (!droneEntity_) return false; |
---|
| 154 | if(!owner_) return false; |
---|
| 155 | if(this->bHasTargetPosition_) |
---|
| 156 | { |
---|
| 157 | Vector3 ownerPosition_ = owner_->getPosition(); |
---|
| 158 | Vector3 toOwner_ = owner_->getPosition() - droneEntity_->getPosition(); |
---|
| 159 | Vector3 toTarget_ = targetPosition_ - droneEntity_->getPosition(); |
---|
| 160 | if(toTarget_.length() < toOwner_.length()) return false; //owner is far away = in safty |
---|
| 161 | float angleToOwner = getAngle(droneEntity_->getPosition(), droneEntity_->getOrientation() * WorldEntity::FRONT, ownerPosition_); |
---|
| 162 | float angleToTarget = getAngle(droneEntity_->getPosition(), droneEntity_->getOrientation() * WorldEntity::FRONT, targetPosition_); |
---|
| 163 | float angle = angleToOwner - angleToTarget;//angle between target and owner, observed by the drone |
---|
| 164 | if(std::sin(angle)*toOwner_.length() < 5.0f)//calculate owner's distance to shooting line |
---|
| 165 | return true; |
---|
| 166 | } |
---|
| 167 | return false;//Default return value: Usually there is no friendlyFire |
---|
| 168 | } |
---|
[6847] | 169 | } |
---|