[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 | */ |
---|
[9667] | 43 | RegisterClass(DroneController); |
---|
[6847] | 44 | |
---|
[8729] | 45 | const float DroneController::ACTION_INTERVAL = 1.0f; |
---|
[6847] | 46 | |
---|
[9667] | 47 | DroneController::DroneController(Context* context) : ArtificialController(context) |
---|
[6847] | 48 | { |
---|
| 49 | RegisterObject(DroneController); |
---|
| 50 | |
---|
[11071] | 51 | this->owner_ = nullptr; |
---|
| 52 | this->drone_ = nullptr; |
---|
[7093] | 53 | this->isShooting_ = false; |
---|
[8891] | 54 | this->setAccuracy(10); |
---|
[6847] | 55 | |
---|
| 56 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&DroneController::action, this))); |
---|
[6891] | 57 | |
---|
| 58 | this->owner_.setCallback(createFunctor(&DroneController::ownerDied, this)); |
---|
[6847] | 59 | } |
---|
| 60 | |
---|
| 61 | DroneController::~DroneController() |
---|
| 62 | { |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | void DroneController::setOwner(Pawn* owner){ |
---|
| 66 | this->owner_ = owner; |
---|
[7082] | 67 | } |
---|
[6847] | 68 | |
---|
| 69 | void DroneController::setDrone(Drone* drone) |
---|
| 70 | { |
---|
| 71 | this->drone_ = drone; |
---|
| 72 | this->setControllableEntity(drone); |
---|
| 73 | } |
---|
[7082] | 74 | |
---|
[6847] | 75 | void DroneController::action() |
---|
| 76 | { |
---|
| 77 | float random; |
---|
| 78 | float maxrand = 100.0f / ACTION_INTERVAL; |
---|
[7093] | 79 | float distanceToTargetSquared = 0; |
---|
[6847] | 80 | |
---|
[7093] | 81 | if (this->target_) |
---|
| 82 | distanceToTargetSquared = (getDrone()->getWorldPosition() - this->target_->getWorldPosition()).squaredLength(); |
---|
[7082] | 83 | |
---|
[6891] | 84 | random = rnd(maxrand); |
---|
[7060] | 85 | if ( random < 30 || (!this->target_) || distanceToTargetSquared > (this->getDrone()->getMaxShootingRange()*this->getDrone()->getMaxShootingRange())) |
---|
[6891] | 86 | this->searchNewTarget(); |
---|
[6847] | 87 | } |
---|
| 88 | |
---|
[7038] | 89 | /** |
---|
[6847] | 90 | @brief |
---|
| 91 | The controlling happens here. This method defines what the controller has to do each tick. |
---|
| 92 | @param dt |
---|
| 93 | The duration of the tick. |
---|
| 94 | */ |
---|
[8891] | 95 | /* PORTALS workaround: |
---|
| 96 | if the owner uses a portal -> distance between owner and drone is huge -> is detected by drone |
---|
| 97 | -> drone searches for portal -> drone adds portal as waypoint -> drone flies towards portal //ignores owner |
---|
| 98 | -> if the drone used a portal, then the distance to the owner is small -> remove waypoint // back to normal mode |
---|
| 99 | |
---|
| 100 | */ |
---|
[6847] | 101 | void DroneController::tick(float dt) |
---|
| 102 | { |
---|
[7082] | 103 | if (this->getDrone() && this->getOwner()) |
---|
| 104 | { |
---|
[8891] | 105 | if (this->waypoints_.size() > 0 ) //Waypoint functionality: Drone should follow it's master through portals |
---|
| 106 | {// Idea: after using the the portal, the master is far away. |
---|
| 107 | WorldEntity* wPoint = this->waypoints_[this->waypoints_.size()-1]; |
---|
| 108 | if(wPoint) |
---|
[7093] | 109 | { |
---|
[8891] | 110 | float distanceToOwnerSquared = (this->getDrone()->getWorldPosition() - this->getOwner()->getWorldPosition()).squaredLength(); |
---|
| 111 | this->absoluteMoveToPosition(wPoint->getWorldPosition()); //simplified function - needs WORKAROUND |
---|
| 112 | if (distanceToOwnerSquared <= 90000.0f) //WORKAROUND: if the Drone is again near its owner, the portal has been used correctly. |
---|
| 113 | { |
---|
| 114 | this->waypoints_.pop_back(); // if goal is reached, remove it from the list |
---|
| 115 | this->positionReached(); //needed?? |
---|
| 116 | } |
---|
| 117 | |
---|
[7093] | 118 | } |
---|
[8891] | 119 | else |
---|
| 120 | this->waypoints_.pop_back(); // remove invalid waypoints |
---|
[7093] | 121 | } |
---|
[8891] | 122 | else |
---|
| 123 | { |
---|
| 124 | if (this->target_) |
---|
| 125 | { |
---|
| 126 | float distanceToTargetSquared = (this->getDrone()->getWorldPosition() - this->target_->getWorldPosition()).squaredLength(); |
---|
| 127 | if (distanceToTargetSquared < (this->getDrone()->getMaxShootingRange()*this->getDrone()->getMaxShootingRange())) |
---|
| 128 | { |
---|
| 129 | this->isShooting_ = true; |
---|
| 130 | this->aimAtTarget(); |
---|
| 131 | if(!this->friendlyFire()) |
---|
[11175] | 132 | this->getDrone()->push(0); |
---|
[8891] | 133 | } |
---|
| 134 | } |
---|
[7093] | 135 | |
---|
[8891] | 136 | |
---|
| 137 | float maxDistanceSquared = this->getDrone()->getMaxDistanceToOwner()*this->getDrone()->getMaxDistanceToOwner(); |
---|
| 138 | float minDistanceSquared = this->getDrone()->getMinDistanceToOwner()*this->getDrone()->getMinDistanceToOwner(); |
---|
| 139 | if((this->getDrone()->getWorldPosition() - this->getOwner()->getWorldPosition()).squaredLength() > 20.0f*maxDistanceSquared) |
---|
| 140 | {//FIX: if the drone's owner uses portal, the drone searches for the portal & adds it as a waypoint. |
---|
| 141 | this->updatePointsOfInterest("PortalEndPoint", 500.0f); //possible conflict: speed-pickup |
---|
| 142 | } |
---|
| 143 | if ((this->getDrone()->getWorldPosition() - this->getOwner()->getWorldPosition()).squaredLength() > maxDistanceSquared) |
---|
| 144 | { |
---|
| 145 | this->moveToPosition(this->getOwner()->getWorldPosition()); //fly towards owner |
---|
| 146 | } |
---|
| 147 | else if((this->getDrone()->getWorldPosition() - this->getOwner()->getWorldPosition()).squaredLength() < minDistanceSquared) |
---|
| 148 | { |
---|
| 149 | this->moveToPosition(-this->getOwner()->getWorldPosition()); //fly away from owner |
---|
| 150 | } |
---|
| 151 | else if (!this->isShooting_) |
---|
| 152 | { |
---|
| 153 | float random = rnd(2.0f); |
---|
| 154 | float randomSelection = rnd(6.0f); |
---|
| 155 | if((int)randomSelection==0) drone_->moveUpDown(random); |
---|
| 156 | else if((int)randomSelection==1) drone_->moveRightLeft(random); |
---|
| 157 | else if((int)randomSelection==2) drone_->moveFrontBack(random); |
---|
| 158 | else if((int)randomSelection==3) drone_->rotateYaw(random); |
---|
| 159 | else if((int)randomSelection==4) drone_->rotatePitch(random); |
---|
| 160 | else if((int)randomSelection==5) drone_->rotateRoll(random); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | this->isShooting_ = false; |
---|
[7082] | 164 | } |
---|
[6847] | 165 | } |
---|
| 166 | SUPER(AIController, tick, dt); |
---|
[6891] | 167 | } |
---|
[6847] | 168 | |
---|
[6891] | 169 | void DroneController::ownerDied() |
---|
| 170 | { |
---|
[7060] | 171 | // if (this->target_) { //Drone has some kind of Stockholm-Syndrom --> gets attached to Owners Killer |
---|
| 172 | // this->setOwner(target_); |
---|
| 173 | // this->searchNewTarget(); |
---|
| 174 | // } |
---|
[6891] | 175 | if (this->drone_) |
---|
| 176 | this->drone_->destroy(); |
---|
| 177 | else |
---|
| 178 | this->destroy(); |
---|
[6847] | 179 | } |
---|
[8891] | 180 | |
---|
| 181 | bool DroneController::friendlyFire() |
---|
| 182 | { ControllableEntity* droneEntity_ = this->getControllableEntity(); |
---|
| 183 | if (!droneEntity_) return false; |
---|
| 184 | if(!owner_) return false; |
---|
| 185 | if(this->bHasTargetPosition_) |
---|
| 186 | { |
---|
| 187 | Vector3 ownerPosition_ = owner_->getPosition(); |
---|
| 188 | Vector3 toOwner_ = owner_->getPosition() - droneEntity_->getPosition(); |
---|
| 189 | Vector3 toTarget_ = targetPosition_ - droneEntity_->getPosition(); |
---|
| 190 | if(toTarget_.length() < toOwner_.length()) return false; //owner is far away = in safty |
---|
| 191 | float angleToOwner = getAngle(droneEntity_->getPosition(), droneEntity_->getOrientation() * WorldEntity::FRONT, ownerPosition_); |
---|
| 192 | float angleToTarget = getAngle(droneEntity_->getPosition(), droneEntity_->getOrientation() * WorldEntity::FRONT, targetPosition_); |
---|
| 193 | float angle = angleToOwner - angleToTarget;//angle between target and owner, observed by the drone |
---|
| 194 | if(std::sin(angle)*toOwner_.length() < 5.0f)//calculate owner's distance to shooting line |
---|
| 195 | return true; |
---|
| 196 | } |
---|
| 197 | return false;//Default return value: Usually there is no friendlyFire |
---|
| 198 | } |
---|
[6847] | 199 | } |
---|