| 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" |
|---|
| 34 | #include "core/Executor.h" |
|---|
| 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 | // - do any kind of initialisation |
|---|
| 50 | |
|---|
| 51 | // this checks that our creator really is a drone |
|---|
| 52 | // and saves the pointer to the drone for the controlling commands |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | RegisterObject(DroneController); |
|---|
| 56 | |
|---|
| 57 | this->owner_ = 0; |
|---|
| 58 | this->drone_ = 0; |
|---|
| 59 | |
|---|
| 60 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&DroneController::action, this))); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | DroneController::~DroneController() |
|---|
| 64 | { |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | void DroneController::setOwner(Pawn* owner){ |
|---|
| 68 | this->owner_ = owner; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | void DroneController::setDrone(Drone* drone) |
|---|
| 72 | { |
|---|
| 73 | this->drone_ = drone; |
|---|
| 74 | this->setControllableEntity(drone); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | void DroneController::action() |
|---|
| 78 | { |
|---|
| 79 | float random; |
|---|
| 80 | float maxrand = 100.0f / ACTION_INTERVAL; |
|---|
| 81 | |
|---|
| 82 | const Vector3& ownerPosition = getOwner()->getWorldPosition(); |
|---|
| 83 | const Vector3& dronePosition = getDrone()->getWorldPosition(); |
|---|
| 84 | |
|---|
| 85 | const Vector3& locOwnerDir = getDrone()->getOrientation().UnitInverse()*(dronePosition-ownerPosition); //Vector from Drone To Owner out of drones local coordinate system |
|---|
| 86 | |
|---|
| 87 | int distance = sqrt( (ownerPosition.x-dronePosition.x)*(ownerPosition.x-dronePosition.x) |
|---|
| 88 | + (ownerPosition.y-dronePosition.y)*(ownerPosition.y-dronePosition.y) |
|---|
| 89 | + (ownerPosition.z-dronePosition.z)*(ownerPosition.z-dronePosition.z)); //distance to Owner |
|---|
| 90 | |
|---|
| 91 | if (distance > 500) { //TODO: variable implementation of maxdistance |
|---|
| 92 | drone_->moveUpDown(-locOwnerDir.y); |
|---|
| 93 | drone_->moveFrontBack(locOwnerDir.z); |
|---|
| 94 | drone_->moveRightLeft(-locOwnerDir.x); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | COUT(0) << "Owner: " << ownerPosition << endl; |
|---|
| 98 | COUT(0) << "Drone: " << dronePosition << endl; |
|---|
| 99 | COUT(0) << "Distance: " << distance << endl; |
|---|
| 100 | COUT(0) << "locDrone: " << locOwnerDir << endl; |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | /* |
|---|
| 104 | // search enemy |
|---|
| 105 | random = rnd(maxrand); |
|---|
| 106 | if (random < 15 && (!this->target_)) |
|---|
| 107 | this->searchNewTarget(); |
|---|
| 108 | |
|---|
| 109 | // forget enemy |
|---|
| 110 | random = rnd(maxrand); |
|---|
| 111 | if (random < 5 && (this->target_)) |
|---|
| 112 | this->forgetTarget(); |
|---|
| 113 | |
|---|
| 114 | // next enemy |
|---|
| 115 | random = rnd(maxrand); |
|---|
| 116 | if (random < 10 && (this->target_)) |
|---|
| 117 | this->searchNewTarget(); |
|---|
| 118 | |
|---|
| 119 | //fly somewhere |
|---|
| 120 | random = rnd(maxrand); |
|---|
| 121 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
|---|
| 122 | this->searchRandomTargetPosition(); |
|---|
| 123 | |
|---|
| 124 | // stop flying |
|---|
| 125 | random = rnd(maxrand); |
|---|
| 126 | if (random < 10 && (this->bHasTargetPosition_ && !this->target_)) |
|---|
| 127 | this->bHasTargetPosition_ = false; |
|---|
| 128 | |
|---|
| 129 | // fly somewhere else |
|---|
| 130 | random = rnd(maxrand); |
|---|
| 131 | if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) |
|---|
| 132 | this->searchRandomTargetPosition(); |
|---|
| 133 | |
|---|
| 134 | // shoot |
|---|
| 135 | random = rnd(maxrand); |
|---|
| 136 | if (random < 75 && (this->target_ && !this->bShooting_)) |
|---|
| 137 | this->bShooting_ = true; |
|---|
| 138 | |
|---|
| 139 | // stop shooting |
|---|
| 140 | random = rnd(maxrand); |
|---|
| 141 | if (random < 25 && (this->bShooting_)) |
|---|
| 142 | this->bShooting_ = false; */ |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | /** |
|---|
| 147 | @brief |
|---|
| 148 | The controlling happens here. This method defines what the controller has to do each tick. |
|---|
| 149 | @param dt |
|---|
| 150 | The duration of the tick. |
|---|
| 151 | */ |
|---|
| 152 | void DroneController::tick(float dt) |
|---|
| 153 | { |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | Drone *myDrone = static_cast<Drone*>(this->getControllableEntity()); |
|---|
| 157 | |
|---|
| 158 | if(myDrone != NULL) { |
|---|
| 159 | |
|---|
| 160 | setTargetPosition(this->getControllableEntity()->getPosition()); |
|---|
| 161 | |
|---|
| 162 | /* myDrone->setRotationThrust(25); |
|---|
| 163 | myDrone->setAuxilaryThrust(30); |
|---|
| 164 | myDrone->rotateYaw(10*dt); */ |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | SUPER(AIController, tick, dt); |
|---|
| 168 | |
|---|
| 169 | // you can use the following commands for steering |
|---|
| 170 | // - moveFrontBack, moveRightLeft, moveUpDown |
|---|
| 171 | // - rotatePitch, rotateYaw, rotateRoll |
|---|
| 172 | // - apply the to myDrone (e.g. myDrone->rotateYaw(..) ) |
|---|
| 173 | |
|---|
| 174 | } |
|---|
| 175 | } |
|---|