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 | namespace orxonox |
---|
38 | { |
---|
39 | const float AIController::ACTION_INTERVAL = 1.0f; |
---|
40 | |
---|
41 | CreateFactory(AIController); |
---|
42 | |
---|
43 | AIController::AIController(Context* context) : ArtificialController(context) |
---|
44 | { |
---|
45 | RegisterObject(AIController); |
---|
46 | |
---|
47 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&AIController::action, this))); |
---|
48 | } |
---|
49 | |
---|
50 | AIController::~AIController() |
---|
51 | { |
---|
52 | } |
---|
53 | |
---|
54 | void AIController::action() |
---|
55 | { |
---|
56 | float random; |
---|
57 | float maxrand = 100.0f / ACTION_INTERVAL; |
---|
58 | |
---|
59 | if (this->state_ == FREE) |
---|
60 | { |
---|
61 | |
---|
62 | if (this->formationFlight_) |
---|
63 | { |
---|
64 | |
---|
65 | //changed order -> searchNewMaster MUSTN'T be called in SLAVE-state (bugfix for internal-error messages at quit) |
---|
66 | random = rnd(maxrand); |
---|
67 | if (random < 90 && (((!this->target_) || (random < 50 && this->target_)) && !this->forcedFree())) |
---|
68 | this->searchNewMaster(); |
---|
69 | |
---|
70 | // return to Master after being forced free |
---|
71 | if (this->freedomCount_ == 1) |
---|
72 | { |
---|
73 | this->state_ = SLAVE; |
---|
74 | this->freedomCount_ = 0; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | this->defaultBehaviour(maxrand); |
---|
79 | |
---|
80 | } |
---|
81 | |
---|
82 | if (this->state_ == SLAVE && this->formationMode_ == ATTACK) |
---|
83 | { |
---|
84 | // search enemy |
---|
85 | random = rnd(maxrand); |
---|
86 | if (random < (botlevel_*100) && (!this->target_)) |
---|
87 | this->searchNewTarget(); |
---|
88 | |
---|
89 | // next enemy |
---|
90 | random = rnd(maxrand); |
---|
91 | if (random < (botlevel_*30) && (this->target_)) |
---|
92 | this->searchNewTarget(); |
---|
93 | |
---|
94 | // shoot |
---|
95 | random = rnd(maxrand); |
---|
96 | if (!(this->passive_) && random < (botlevel_*100) && (this->target_ && !this->bShooting_)) |
---|
97 | this->bShooting_ = true; |
---|
98 | |
---|
99 | // stop shooting |
---|
100 | random = rnd(maxrand); |
---|
101 | if (random < (1-botlevel_)*50 && (this->bShooting_)) |
---|
102 | this->bShooting_ = false; |
---|
103 | |
---|
104 | } |
---|
105 | |
---|
106 | if (this->state_ == MASTER) |
---|
107 | { |
---|
108 | this->commandSlaves(); |
---|
109 | |
---|
110 | if (this->specificMasterAction_ != NONE) |
---|
111 | this->specificMasterActionHold(); |
---|
112 | |
---|
113 | else { |
---|
114 | |
---|
115 | // make 180 degree turn - a specific Master Action |
---|
116 | random = rnd(1000.0f); |
---|
117 | if (random < 5) |
---|
118 | this->turn180Init(); |
---|
119 | |
---|
120 | // spin around - a specific Master Action |
---|
121 | random = rnd(1000.0f); |
---|
122 | if (random < 5) |
---|
123 | this->spinInit(); |
---|
124 | |
---|
125 | /*// follow a randomly chosen human - a specific Master Action |
---|
126 | random = rnd(1000.0f); |
---|
127 | if (random < 1) |
---|
128 | this->followRandomHumanInit(); |
---|
129 | */ |
---|
130 | // lose master status (only if less than 4 slaves in formation) |
---|
131 | random = rnd(maxrand); |
---|
132 | if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 ) |
---|
133 | this->loseMasterState(); |
---|
134 | |
---|
135 | // look out for outher masters if formation is small |
---|
136 | random = rnd(maxrand); |
---|
137 | if(this->slaves_.size() < 3 && random < 20) |
---|
138 | this->searchNewMaster(); |
---|
139 | |
---|
140 | this->defaultBehaviour(maxrand); |
---|
141 | |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | } |
---|
146 | |
---|
147 | void AIController::tick(float dt) |
---|
148 | { |
---|
149 | if (!this->isActive()) |
---|
150 | return; |
---|
151 | |
---|
152 | float random; |
---|
153 | float maxrand = 100.0f / ACTION_INTERVAL; |
---|
154 | ControllableEntity* controllable = this->getControllableEntity(); |
---|
155 | //DOES: Either move to the waypoint or search for a Point of interest |
---|
156 | if (controllable && this->mode_ == DEFAULT)// bot is ready to move to a target |
---|
157 | { |
---|
158 | if (this->waypoints_.size() > 0 ) //Waypoint functionality. |
---|
159 | { |
---|
160 | WorldEntity* wPoint = this->waypoints_[this->waypoints_.size()-1]; |
---|
161 | if(wPoint) |
---|
162 | { |
---|
163 | this->moveToPosition(wPoint->getWorldPosition()); //BUG ?? sometime wPoint->getWorldPosition() causes crash |
---|
164 | if (wPoint->getWorldPosition().squaredDistance(controllable->getPosition()) <= this->squaredaccuracy_) |
---|
165 | this->waypoints_.pop_back(); // if goal is reached, remove it from the list |
---|
166 | } |
---|
167 | else |
---|
168 | this->waypoints_.pop_back(); // remove invalid waypoints |
---|
169 | |
---|
170 | } |
---|
171 | else if(this->defaultWaypoint_ && ((this->defaultWaypoint_->getPosition()-controllable->getPosition()).length() > 200.0f)) |
---|
172 | { |
---|
173 | this->moveToPosition(this->defaultWaypoint_->getPosition()); // stay within a certain range of the defaultWaypoint_ |
---|
174 | random = rnd(maxrand); |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | if (this->mode_ == DEFAULT) |
---|
179 | { |
---|
180 | if (this->state_ == MASTER) |
---|
181 | { |
---|
182 | if (this->specificMasterAction_ == NONE) |
---|
183 | { |
---|
184 | if (this->target_) |
---|
185 | { |
---|
186 | if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */ |
---|
187 | this->forgetTarget(); |
---|
188 | else |
---|
189 | { |
---|
190 | this->aimAtTarget(); |
---|
191 | random = rnd(maxrand); |
---|
192 | if(this->botlevel_*70 > random && !this->isCloseAtTarget(100)) |
---|
193 | this->follow(); //If a bot is shooting a player, it shouldn't let him go away easily. |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | if (this->bHasTargetPosition_) |
---|
198 | this->moveToTargetPosition(); |
---|
199 | this->doFire(); |
---|
200 | } |
---|
201 | |
---|
202 | if (this->specificMasterAction_ == TURN180) |
---|
203 | this->turn180(); |
---|
204 | |
---|
205 | if (this->specificMasterAction_ == SPIN) |
---|
206 | this->spin(); |
---|
207 | if (this->specificMasterAction_ == FOLLOW) |
---|
208 | this->follow(); |
---|
209 | } |
---|
210 | |
---|
211 | if (this->state_ == SLAVE && this->formationMode_ != ATTACK) |
---|
212 | { |
---|
213 | if (this->bHasTargetPosition_) |
---|
214 | this->moveToTargetPosition(); |
---|
215 | } |
---|
216 | |
---|
217 | if (this->state_ == FREE || (this->state_==SLAVE && this->formationMode_ == ATTACK) ) |
---|
218 | { |
---|
219 | if (this->target_) |
---|
220 | { |
---|
221 | if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */ |
---|
222 | this->forgetTarget(); |
---|
223 | else this->aimAtTarget(); |
---|
224 | } |
---|
225 | |
---|
226 | if (this->bHasTargetPosition_) |
---|
227 | this->moveToTargetPosition(); |
---|
228 | |
---|
229 | this->doFire(); |
---|
230 | } |
---|
231 | } |
---|
232 | else if (this->mode_ == ROCKET)//Rockets do not belong to a group of bots -> bot states are not relevant. |
---|
233 | { //Vector-implementation: mode_.back() == ROCKET; |
---|
234 | if(controllable) |
---|
235 | {//Check wether the bot is controlling the rocket and if the timeout is over. |
---|
236 | if(controllable->getIdentifier() == ClassByString("Rocket")) |
---|
237 | |
---|
238 | { |
---|
239 | this->follow(); |
---|
240 | this->timeout_ -= dt; |
---|
241 | if((timeout_< 0)||(!target_))//Check if the timeout is over or target died. |
---|
242 | { |
---|
243 | controllable->fire(0);//kill the rocket |
---|
244 | this->setPreviousMode();//get out of rocket mode |
---|
245 | } |
---|
246 | } |
---|
247 | else |
---|
248 | this->setPreviousMode();//no rocket entity -> get out of rocket mode |
---|
249 | } |
---|
250 | else |
---|
251 | this->setPreviousMode();//If bot dies -> getControllableEntity == NULL -> get out of ROCKET mode |
---|
252 | }//END_OF ROCKET MODE |
---|
253 | |
---|
254 | SUPER(AIController, tick, dt); |
---|
255 | } |
---|
256 | //**********************************************NEW |
---|
257 | void AIController::defaultBehaviour(float maxrand) |
---|
258 | { float random; |
---|
259 | // search enemy |
---|
260 | random = rnd(maxrand); |
---|
261 | if (random < (botlevel_* 100) && (!this->target_)) |
---|
262 | this->searchNewTarget(); |
---|
263 | |
---|
264 | // forget enemy |
---|
265 | random = rnd(maxrand); |
---|
266 | if (random < ((1-botlevel_)*20) && (this->target_)) |
---|
267 | this->forgetTarget(); |
---|
268 | |
---|
269 | // next enemy |
---|
270 | random = rnd(maxrand); |
---|
271 | if (random < (botlevel_*30) && (this->target_)) |
---|
272 | this->searchNewTarget(); |
---|
273 | |
---|
274 | // fly somewhere |
---|
275 | random = rnd(maxrand); |
---|
276 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
---|
277 | this->searchRandomTargetPosition(); |
---|
278 | |
---|
279 | // stop flying |
---|
280 | random = rnd(maxrand); |
---|
281 | if (random < 10 && (this->bHasTargetPosition_ && !this->target_)) |
---|
282 | this->bHasTargetPosition_ = false; |
---|
283 | |
---|
284 | // fly somewhere else |
---|
285 | random = rnd(maxrand); |
---|
286 | if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) |
---|
287 | this->searchRandomTargetPosition(); |
---|
288 | |
---|
289 | if (this->state_ == MASTER) // master: shoot |
---|
290 | { |
---|
291 | random = rnd(maxrand); |
---|
292 | if (!(this->passive_) && random < (100*botlevel_) && (this->target_ && !this->bShooting_)) |
---|
293 | { |
---|
294 | this->bShooting_ = true; |
---|
295 | this->forceFreeSlaves(); |
---|
296 | } |
---|
297 | } |
---|
298 | else |
---|
299 | { |
---|
300 | // shoot |
---|
301 | random = rnd(maxrand); |
---|
302 | if (!(this->passive_) && random < (botlevel_*100) && (this->target_ && !this->bShooting_)) |
---|
303 | this->bShooting_ = true; |
---|
304 | } |
---|
305 | |
---|
306 | // stop shooting |
---|
307 | random = rnd(maxrand); |
---|
308 | if (random < ((1 - botlevel_)*50) && (this->bShooting_)) |
---|
309 | this->bShooting_ = false; |
---|
310 | |
---|
311 | // boost |
---|
312 | random = rnd(maxrand); |
---|
313 | if (random < botlevel_*50 ) |
---|
314 | this->boostControl(); |
---|
315 | |
---|
316 | // update Checkpoints |
---|
317 | /*random = rnd(maxrand); |
---|
318 | if (this->defaultWaypoint_ && random > (maxrand-10)) |
---|
319 | this->manageWaypoints(); |
---|
320 | else //if(random > maxrand-10) //CHECK USABILITY!!*/ |
---|
321 | if (this->waypoints_.size() == 0 ) |
---|
322 | this->manageWaypoints(); |
---|
323 | } |
---|
324 | |
---|
325 | } |
---|