[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" |
---|
| 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))); |
---|
[6891] | 61 | |
---|
| 62 | this->owner_.setCallback(createFunctor(&DroneController::ownerDied, this)); |
---|
[6847] | 63 | } |
---|
| 64 | |
---|
| 65 | DroneController::~DroneController() |
---|
| 66 | { |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | void DroneController::setOwner(Pawn* owner){ |
---|
| 70 | this->owner_ = owner; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | void DroneController::setDrone(Drone* drone) |
---|
| 74 | { |
---|
| 75 | this->drone_ = drone; |
---|
| 76 | this->setControllableEntity(drone); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | void DroneController::action() |
---|
| 80 | { |
---|
| 81 | float random; |
---|
| 82 | float maxrand = 100.0f / ACTION_INTERVAL; |
---|
| 83 | |
---|
| 84 | const Vector3& ownerPosition = getOwner()->getWorldPosition(); |
---|
| 85 | const Vector3& dronePosition = getDrone()->getWorldPosition(); |
---|
| 86 | |
---|
[6891] | 87 | const Vector3& locOwnerDir = getDrone()->getOrientation().UnitInverse()*(ownerPosition-dronePosition); //Vector from Drone To Owner out of drones local coordinate system |
---|
[6847] | 88 | |
---|
| 89 | int distance = sqrt( (ownerPosition.x-dronePosition.x)*(ownerPosition.x-dronePosition.x) |
---|
| 90 | + (ownerPosition.y-dronePosition.y)*(ownerPosition.y-dronePosition.y) |
---|
| 91 | + (ownerPosition.z-dronePosition.z)*(ownerPosition.z-dronePosition.z)); //distance to Owner |
---|
| 92 | |
---|
| 93 | if (distance > 500) { //TODO: variable implementation of maxdistance |
---|
[6891] | 94 | drone_->moveUpDown(locOwnerDir.y); |
---|
| 95 | drone_->moveFrontBack(-locOwnerDir.z); |
---|
| 96 | drone_->moveRightLeft(locOwnerDir.x); |
---|
[6847] | 97 | } |
---|
| 98 | |
---|
[6891] | 99 | |
---|
| 100 | random = rnd(maxrand); |
---|
| 101 | if ( random < 30 && (!this->target_)) |
---|
| 102 | this->searchNewTarget(); |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | this->aimAtTarget(); |
---|
| 106 | drone_->fire(0); |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | //COUT(0) << "Drone: " << dronePosition << endl; |
---|
| 112 | //COUT(0) << "Distance: " << distance << endl; |
---|
| 113 | COUT(0) << "locDrone: " << locOwnerDir << endl; |
---|
| 114 | COUT(0) << "target: " << targetPosition_ << endl; |
---|
[6847] | 115 | COUT(0) << "Owner: " << ownerPosition << endl; |
---|
[6891] | 116 | COUT(0) << "Rand: " << random << endl; |
---|
[6847] | 117 | |
---|
| 118 | /* |
---|
| 119 | // search enemy |
---|
| 120 | random = rnd(maxrand); |
---|
| 121 | if (random < 15 && (!this->target_)) |
---|
| 122 | this->searchNewTarget(); |
---|
| 123 | |
---|
| 124 | // forget enemy |
---|
| 125 | random = rnd(maxrand); |
---|
| 126 | if (random < 5 && (this->target_)) |
---|
| 127 | this->forgetTarget(); |
---|
| 128 | |
---|
| 129 | // next enemy |
---|
| 130 | random = rnd(maxrand); |
---|
| 131 | if (random < 10 && (this->target_)) |
---|
| 132 | this->searchNewTarget(); |
---|
| 133 | |
---|
| 134 | //fly somewhere |
---|
| 135 | random = rnd(maxrand); |
---|
| 136 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
---|
| 137 | this->searchRandomTargetPosition(); |
---|
| 138 | |
---|
| 139 | // stop flying |
---|
| 140 | random = rnd(maxrand); |
---|
| 141 | if (random < 10 && (this->bHasTargetPosition_ && !this->target_)) |
---|
| 142 | this->bHasTargetPosition_ = false; |
---|
| 143 | |
---|
| 144 | // fly somewhere else |
---|
| 145 | random = rnd(maxrand); |
---|
| 146 | if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) |
---|
| 147 | this->searchRandomTargetPosition(); |
---|
| 148 | |
---|
| 149 | // shoot |
---|
| 150 | random = rnd(maxrand); |
---|
| 151 | if (random < 75 && (this->target_ && !this->bShooting_)) |
---|
| 152 | this->bShooting_ = true; |
---|
| 153 | |
---|
| 154 | // stop shooting |
---|
| 155 | random = rnd(maxrand); |
---|
| 156 | if (random < 25 && (this->bShooting_)) |
---|
| 157 | this->bShooting_ = false; */ |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | /** |
---|
| 162 | @brief |
---|
| 163 | The controlling happens here. This method defines what the controller has to do each tick. |
---|
| 164 | @param dt |
---|
| 165 | The duration of the tick. |
---|
| 166 | */ |
---|
| 167 | void DroneController::tick(float dt) |
---|
| 168 | { |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | Drone *myDrone = static_cast<Drone*>(this->getControllableEntity()); |
---|
| 172 | |
---|
| 173 | if(myDrone != NULL) { |
---|
| 174 | |
---|
| 175 | setTargetPosition(this->getControllableEntity()->getPosition()); |
---|
| 176 | |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | SUPER(AIController, tick, dt); |
---|
| 180 | |
---|
[6891] | 181 | } |
---|
[6847] | 182 | |
---|
[6891] | 183 | void DroneController::ownerDied() |
---|
| 184 | { |
---|
| 185 | if (this->drone_) |
---|
| 186 | this->drone_->destroy(); |
---|
| 187 | else |
---|
| 188 | this->destroy(); |
---|
[6847] | 189 | } |
---|
| 190 | } |
---|