[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: |
---|
[7163] | 25 | * Dominik Solenicki |
---|
[2362] | 26 | * |
---|
| 27 | */ |
---|
[7843] | 28 | |
---|
[2362] | 29 | #include "AIController.h" |
---|
| 30 | |
---|
[3196] | 31 | #include "util/Math.h" |
---|
[2362] | 32 | #include "core/CoreIncludes.h" |
---|
[7284] | 33 | #include "core/command/Executor.h" |
---|
[5735] | 34 | #include "worldentities/ControllableEntity.h" |
---|
[7163] | 35 | #include "worldentities/pawns/Pawn.h" |
---|
[2362] | 36 | |
---|
[7843] | 37 | //Todo: Bot soll pickupspawner besuchen können, falls Pickup vorhanden |
---|
[2362] | 38 | namespace orxonox |
---|
| 39 | { |
---|
| 40 | static const float ACTION_INTERVAL = 1.0f; |
---|
| 41 | |
---|
| 42 | CreateFactory(AIController); |
---|
| 43 | |
---|
| 44 | AIController::AIController(BaseObject* creator) : ArtificialController(creator) |
---|
| 45 | { |
---|
| 46 | RegisterObject(AIController); |
---|
| 47 | |
---|
[5929] | 48 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&AIController::action, this))); |
---|
[2362] | 49 | } |
---|
| 50 | |
---|
| 51 | AIController::~AIController() |
---|
| 52 | { |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | void AIController::action() |
---|
| 56 | { |
---|
| 57 | float random; |
---|
| 58 | float maxrand = 100.0f / ACTION_INTERVAL; |
---|
[2493] | 59 | |
---|
[7163] | 60 | if (this->state_ == FREE) |
---|
| 61 | { |
---|
[2362] | 62 | |
---|
[7163] | 63 | if (this->formationFlight_) |
---|
| 64 | { |
---|
| 65 | // return to Master after being forced free |
---|
| 66 | if (this->freedomCount_ == 1) |
---|
| 67 | { |
---|
[8238] | 68 | this->state_ = SLAVE; |
---|
| 69 | this->freedomCount_ = 0; |
---|
[7163] | 70 | } |
---|
[2362] | 71 | |
---|
[7163] | 72 | random = rnd(maxrand); |
---|
| 73 | if (random < 90 && (((!this->target_) || (random < 50 && this->target_)) && !this->forcedFree())) |
---|
| 74 | this->searchNewMaster(); |
---|
| 75 | } |
---|
[2493] | 76 | |
---|
[7163] | 77 | // search enemy |
---|
| 78 | random = rnd(maxrand); |
---|
[7842] | 79 | if (random < (15 + botlevel_* 20) && (!this->target_)) |
---|
[7163] | 80 | this->searchNewTarget(); |
---|
[2362] | 81 | |
---|
[7163] | 82 | // forget enemy |
---|
| 83 | random = rnd(maxrand); |
---|
[8238] | 84 | if (random < ((1-botlevel_)*6) && (this->target_)) |
---|
[7163] | 85 | this->forgetTarget(); |
---|
[2362] | 86 | |
---|
[7163] | 87 | // next enemy |
---|
| 88 | random = rnd(maxrand); |
---|
[8238] | 89 | if (random < (botlevel_*20) && (this->target_)) |
---|
[7163] | 90 | this->searchNewTarget(); |
---|
[2493] | 91 | |
---|
[7163] | 92 | // fly somewhere |
---|
| 93 | random = rnd(maxrand); |
---|
| 94 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
---|
| 95 | this->searchRandomTargetPosition(); |
---|
[2362] | 96 | |
---|
[7163] | 97 | // stop flying |
---|
| 98 | random = rnd(maxrand); |
---|
| 99 | if (random < 10 && (this->bHasTargetPosition_ && !this->target_)) |
---|
| 100 | this->bHasTargetPosition_ = false; |
---|
| 101 | |
---|
| 102 | // fly somewhere else |
---|
| 103 | random = rnd(maxrand); |
---|
| 104 | if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) |
---|
| 105 | this->searchRandomTargetPosition(); |
---|
| 106 | |
---|
| 107 | // shoot |
---|
| 108 | random = rnd(maxrand); |
---|
[7842] | 109 | if (!(this->passive_) && random < (75 + botlevel_*25) && (this->target_ && !this->bShooting_)) |
---|
[7163] | 110 | this->bShooting_ = true; |
---|
| 111 | |
---|
| 112 | // stop shooting |
---|
| 113 | random = rnd(maxrand); |
---|
[7842] | 114 | if (random < ((1 - botlevel_)*25) && (this->bShooting_)) |
---|
[7163] | 115 | this->bShooting_ = false; |
---|
| 116 | |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | if (this->state_ == SLAVE) |
---|
| 120 | { |
---|
| 121 | |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | if (this->state_ == MASTER) |
---|
| 125 | { |
---|
| 126 | |
---|
| 127 | |
---|
| 128 | this->commandSlaves(); |
---|
| 129 | |
---|
| 130 | if (this->specificMasterAction_ != NONE) |
---|
| 131 | this->specificMasterActionHold(); |
---|
| 132 | |
---|
| 133 | else { |
---|
| 134 | |
---|
| 135 | // make 180 degree turn - a specific Master Action |
---|
| 136 | random = rnd(1000.0f); |
---|
| 137 | if (random < 5) |
---|
| 138 | this->turn180Init(); |
---|
| 139 | |
---|
| 140 | // spin around - a specific Master Action |
---|
| 141 | random = rnd(1000.0f); |
---|
| 142 | if (random < 5) |
---|
| 143 | this->spinInit(); |
---|
| 144 | |
---|
| 145 | // follow a randomly chosen human - a specific Master Action |
---|
| 146 | random = rnd(1000.0f); |
---|
| 147 | if (random < 1) |
---|
| 148 | this->followRandomHumanInit(); |
---|
| 149 | |
---|
| 150 | // lose master status (only if less than 4 slaves in formation) |
---|
| 151 | random = rnd(maxrand); |
---|
| 152 | if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 ) |
---|
| 153 | this->loseMasterState(); |
---|
| 154 | |
---|
| 155 | // look out for outher masters if formation is small |
---|
| 156 | random = rnd(maxrand); |
---|
| 157 | if(this->slaves_.size() < 3 && random < 20) |
---|
| 158 | this->searchNewMaster(); |
---|
| 159 | |
---|
| 160 | // search enemy |
---|
| 161 | random = rnd(maxrand); |
---|
[7843] | 162 | if (random < (botlevel_)*25 && (!this->target_)) |
---|
[7163] | 163 | this->searchNewTarget(); |
---|
| 164 | |
---|
| 165 | // forget enemy |
---|
| 166 | random = rnd(maxrand); |
---|
[7843] | 167 | if (random < (1-botlevel_)*6 && (this->target_)) |
---|
[7163] | 168 | this->forgetTarget(); |
---|
| 169 | |
---|
| 170 | // next enemy |
---|
| 171 | random = rnd(maxrand); |
---|
| 172 | if (random < 10 && (this->target_)) |
---|
| 173 | this->searchNewTarget(); |
---|
| 174 | |
---|
| 175 | // fly somewhere |
---|
| 176 | random = rnd(maxrand); |
---|
| 177 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
---|
| 178 | this->searchRandomTargetPosition(); |
---|
| 179 | |
---|
| 180 | // fly somewhere else |
---|
| 181 | random = rnd(maxrand); |
---|
| 182 | if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) |
---|
| 183 | this->searchRandomTargetPosition(); |
---|
| 184 | |
---|
| 185 | // shoot |
---|
| 186 | random = rnd(maxrand); |
---|
[7843] | 187 | if (!(this->passive_) && random < 25*(botlevel_)+1 && (this->target_ && !this->bShooting_)) |
---|
[7163] | 188 | { |
---|
[7843] | 189 | this->bShooting_ = true; |
---|
| 190 | this->forceFreeSlaves(); |
---|
[7163] | 191 | } |
---|
| 192 | |
---|
| 193 | // stop shooting |
---|
| 194 | random = rnd(maxrand); |
---|
[7842] | 195 | if (random < ( (1- botlevel_)*25 ) && (this->bShooting_)) |
---|
[7163] | 196 | this->bShooting_ = false; |
---|
| 197 | |
---|
| 198 | } |
---|
| 199 | } |
---|
| 200 | |
---|
[2362] | 201 | } |
---|
| 202 | |
---|
| 203 | void AIController::tick(float dt) |
---|
| 204 | { |
---|
[7843] | 205 | float random; |
---|
| 206 | float maxrand = 100.0f / ACTION_INTERVAL; |
---|
| 207 | |
---|
[2362] | 208 | if (!this->isActive()) |
---|
| 209 | return; |
---|
| 210 | |
---|
[7163] | 211 | if (this->state_ == MASTER) |
---|
| 212 | { |
---|
| 213 | if (this->specificMasterAction_ == NONE) |
---|
| 214 | { |
---|
| 215 | if (this->target_) |
---|
| 216 | { |
---|
[7168] | 217 | if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */ |
---|
| 218 | this->forgetTarget(); |
---|
[7843] | 219 | else |
---|
| 220 | { |
---|
| 221 | this->aimAtTarget(); |
---|
[8238] | 222 | random = rnd(maxrand); |
---|
| 223 | if(this->botlevel_*100 > random) |
---|
[7843] | 224 | this->follow();//If a bot is shooting a player, it shouldn't let him go away easily. |
---|
| 225 | } |
---|
[7168] | 226 | } |
---|
[2362] | 227 | |
---|
[7163] | 228 | if (this->bHasTargetPosition_) |
---|
| 229 | this->moveToTargetPosition(); |
---|
[2362] | 230 | |
---|
[7832] | 231 | this->doFire(); |
---|
[7163] | 232 | } |
---|
[2362] | 233 | |
---|
[7163] | 234 | if (this->specificMasterAction_ == TURN180) |
---|
| 235 | this->turn180(); |
---|
| 236 | |
---|
| 237 | if (this->specificMasterAction_ == SPIN) |
---|
| 238 | this->spin(); |
---|
| 239 | if (this->specificMasterAction_ == FOLLOW) |
---|
| 240 | this->follow(); |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | if (this->state_ == SLAVE) |
---|
| 244 | { |
---|
| 245 | if (this->bHasTargetPosition_) |
---|
| 246 | this->moveToTargetPosition(); |
---|
| 247 | } |
---|
| 248 | |
---|
[7832] | 249 | if (this->state_ == FREE) |
---|
[7163] | 250 | { |
---|
| 251 | if (this->target_) |
---|
| 252 | { |
---|
[7168] | 253 | if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */ |
---|
| 254 | this->forgetTarget(); |
---|
[7843] | 255 | else |
---|
[8238] | 256 | { |
---|
| 257 | this->aimAtTarget(); |
---|
| 258 | random = rnd(maxrand); |
---|
| 259 | if(this->botlevel_*100 > random) |
---|
[7843] | 260 | this->follow();//If a bot is shooting a player, it shouldn't let him go away easily. |
---|
[8238] | 261 | } |
---|
[7163] | 262 | } |
---|
| 263 | |
---|
| 264 | if (this->bHasTargetPosition_) |
---|
| 265 | this->moveToTargetPosition(); |
---|
| 266 | |
---|
[7832] | 267 | this->doFire(); |
---|
[7163] | 268 | } |
---|
| 269 | |
---|
[2362] | 270 | SUPER(AIController, tick, dt); |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | } |
---|