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