| 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: |
|---|
| 25 | * Dominik Solenicki |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | #include "AIController.h" |
|---|
| 30 | |
|---|
| 31 | #include "util/Math.h" |
|---|
| 32 | #include "core/CoreIncludes.h" |
|---|
| 33 | #include "core/command/Executor.h" |
|---|
| 34 | #include "worldentities/ControllableEntity.h" |
|---|
| 35 | #include "worldentities/pawns/Pawn.h" |
|---|
| 36 | |
|---|
| 37 | //Todo: Bot soll pickupspawner besuchen können, falls Pickup vorhanden |
|---|
| 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 | |
|---|
| 48 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&AIController::action, this))); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | AIController::~AIController() |
|---|
| 52 | { |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | void AIController::action() |
|---|
| 56 | { |
|---|
| 57 | float random; |
|---|
| 58 | float maxrand = 100.0f / ACTION_INTERVAL; |
|---|
| 59 | |
|---|
| 60 | if (this->state_ == FREE) |
|---|
| 61 | { |
|---|
| 62 | |
|---|
| 63 | if (this->formationFlight_) |
|---|
| 64 | { |
|---|
| 65 | // return to Master after being forced free |
|---|
| 66 | if (this->freedomCount_ == 1) |
|---|
| 67 | { |
|---|
| 68 | this->state_ = SLAVE; |
|---|
| 69 | this->freedomCount_ = 0; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | random = rnd(maxrand); |
|---|
| 73 | if (random < 90 && (((!this->target_) || (random < 50 && this->target_)) && !this->forcedFree())) |
|---|
| 74 | this->searchNewMaster(); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | // search enemy |
|---|
| 78 | random = rnd(maxrand); |
|---|
| 79 | if (random < (15 + botlevel_* 20) && (!this->target_)) |
|---|
| 80 | this->searchNewTarget(); |
|---|
| 81 | |
|---|
| 82 | // forget enemy |
|---|
| 83 | random = rnd(maxrand); |
|---|
| 84 | if (random < ((1-botlevel_)*5) && (this->target_)) |
|---|
| 85 | this->forgetTarget(); |
|---|
| 86 | |
|---|
| 87 | // next enemy |
|---|
| 88 | random = rnd(maxrand); |
|---|
| 89 | if (random < ((1-botlevel_)*10) && (this->target_)) |
|---|
| 90 | this->searchNewTarget(); |
|---|
| 91 | |
|---|
| 92 | // fly somewhere |
|---|
| 93 | random = rnd(maxrand); |
|---|
| 94 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
|---|
| 95 | this->searchRandomTargetPosition(); |
|---|
| 96 | |
|---|
| 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); |
|---|
| 109 | if (!(this->passive_) && random < (75 + botlevel_*25) && (this->target_ && !this->bShooting_)) |
|---|
| 110 | this->bShooting_ = true; |
|---|
| 111 | |
|---|
| 112 | // stop shooting |
|---|
| 113 | random = rnd(maxrand); |
|---|
| 114 | if (random < ((1 - botlevel_)*25) && (this->bShooting_)) |
|---|
| 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); |
|---|
| 162 | if (random < (botlevel_)*25 && (!this->target_)) |
|---|
| 163 | this->searchNewTarget(); |
|---|
| 164 | |
|---|
| 165 | // forget enemy |
|---|
| 166 | random = rnd(maxrand); |
|---|
| 167 | if (random < (1-botlevel_)*6 && (this->target_)) |
|---|
| 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); |
|---|
| 187 | if (!(this->passive_) && random < 25*(botlevel_)+1 && (this->target_ && !this->bShooting_)) |
|---|
| 188 | { |
|---|
| 189 | this->bShooting_ = true; |
|---|
| 190 | this->forceFreeSlaves(); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | // stop shooting |
|---|
| 194 | random = rnd(maxrand); |
|---|
| 195 | if (random < ( (1- botlevel_)*25 ) && (this->bShooting_)) |
|---|
| 196 | this->bShooting_ = false; |
|---|
| 197 | |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | void AIController::tick(float dt) |
|---|
| 204 | { |
|---|
| 205 | float random; |
|---|
| 206 | float maxrand = 100.0f / ACTION_INTERVAL; |
|---|
| 207 | |
|---|
| 208 | if (!this->isActive()) |
|---|
| 209 | return; |
|---|
| 210 | |
|---|
| 211 | if (this->state_ == MASTER) |
|---|
| 212 | { |
|---|
| 213 | if (this->specificMasterAction_ == NONE) |
|---|
| 214 | { |
|---|
| 215 | if (this->target_) |
|---|
| 216 | { |
|---|
| 217 | if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */ |
|---|
| 218 | this->forgetTarget(); |
|---|
| 219 | else |
|---|
| 220 | { |
|---|
| 221 | this->aimAtTarget(); |
|---|
| 222 | random = rnd(maxrand); |
|---|
| 223 | if(this->botlevel_*100 > random) |
|---|
| 224 | this->follow();//If a bot is shooting a player, it shouldn't let him go away easily. |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | if (this->bHasTargetPosition_) |
|---|
| 229 | this->moveToTargetPosition(); |
|---|
| 230 | |
|---|
| 231 | this->doFire(); |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 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 | |
|---|
| 249 | if (this->state_ == FREE) |
|---|
| 250 | { |
|---|
| 251 | if (this->target_) |
|---|
| 252 | { |
|---|
| 253 | if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */ |
|---|
| 254 | this->forgetTarget(); |
|---|
| 255 | else |
|---|
| 256 | { |
|---|
| 257 | this->aimAtTarget(); |
|---|
| 258 | random = rnd(maxrand); |
|---|
| 259 | if(this->botlevel_*100 > random) |
|---|
| 260 | this->follow();//If a bot is shooting a player, it shouldn't let him go away easily. |
|---|
| 261 | } |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | if (this->bHasTargetPosition_) |
|---|
| 265 | this->moveToTargetPosition(); |
|---|
| 266 | |
|---|
| 267 | this->doFire(); |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | SUPER(AIController, tick, dt); |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | } |
|---|