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 | * Gani Aliguzhinov |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "ActionpointController.h" |
---|
30 | |
---|
31 | #include "core/XMLPort.h" |
---|
32 | #include <algorithm> |
---|
33 | #include "worldentities/Actionpoint.h" |
---|
34 | |
---|
35 | namespace orxonox |
---|
36 | { |
---|
37 | |
---|
38 | RegisterClass(ActionpointController); |
---|
39 | |
---|
40 | ActionpointController::ActionpointController(Context* context) : FightingController(context) |
---|
41 | { |
---|
42 | this->actionpointControllerId_ = 0; |
---|
43 | ActionpointController::sTicks_ = 0; |
---|
44 | ActionpointController::nextActionpointControllerId_ = 0; |
---|
45 | this->ticks_ = 0; |
---|
46 | this->bPatrolling_ = false; |
---|
47 | this->bInLoop_ = false; |
---|
48 | this->bLoop_ = false; |
---|
49 | this->bEndLoop_ = false; |
---|
50 | loopActionpoints_.clear(); |
---|
51 | parsedActionpoints_.clear(); |
---|
52 | actionpoints_.clear(); |
---|
53 | this->bTakenOver_ = false; |
---|
54 | this->action_ = Action::NONE; |
---|
55 | this->squaredaccuracy_ = 2500; |
---|
56 | this->bStartedDodging_ = false; |
---|
57 | this->bDefaultPatrol_ = true; |
---|
58 | this->bDefaultFightAll_ = true; |
---|
59 | this->stop_ = false; |
---|
60 | RegisterObject(ActionpointController); |
---|
61 | |
---|
62 | } |
---|
63 | void ActionpointController::XMLPort( Element& xmlelement, XMLPort::Mode mode ) |
---|
64 | { |
---|
65 | SUPER( ActionpointController, XMLPort, xmlelement, mode ); |
---|
66 | |
---|
67 | XMLPortObject(ActionpointController, WorldEntity, "actionpoints", addActionpoint, getActionpoint, xmlelement, mode); |
---|
68 | XMLPortParam(ActionpointController, "defaultFightAll", setDefaultFightAll, getDefaultFightAll, xmlelement, mode).defaultValues(true); |
---|
69 | XMLPortParam(ActionpointController, "defaultPatrol", setDefaultPatrol, getDefaultPatrol, xmlelement, mode).defaultValues(true); |
---|
70 | } |
---|
71 | |
---|
72 | ActionpointController::~ActionpointController() |
---|
73 | { |
---|
74 | loopActionpoints_.clear(); |
---|
75 | parsedActionpoints_.clear(); |
---|
76 | actionpoints_.clear(); |
---|
77 | this->actionpointControllerId_ = 0; |
---|
78 | |
---|
79 | } |
---|
80 | void ActionpointController::tick(float dt) |
---|
81 | { |
---|
82 | if (!this || !this->getControllableEntity() || !this->isActive() || this->stop_) |
---|
83 | return; |
---|
84 | ++this->ticks_; |
---|
85 | // orxout (internal_error) << "this id = " << this->actionpointControllerId_ << endl; |
---|
86 | if (ActionpointController::sTicks_ < this->ticks_) |
---|
87 | { |
---|
88 | // orxout (internal_error) << "total id's = " << ActionpointController::nextActionpointControllerId_ << endl; |
---|
89 | ++ActionpointController::sTicks_; |
---|
90 | } |
---|
91 | |
---|
92 | if (!this || !this->getControllableEntity()) |
---|
93 | return; |
---|
94 | |
---|
95 | if (ActionpointController::sTicks_ == 1) |
---|
96 | { |
---|
97 | |
---|
98 | this->actionpointControllerId_ = ActionpointController::nextActionpointControllerId_++; |
---|
99 | std::reverse(parsedActionpoints_.begin(), parsedActionpoints_.end()); |
---|
100 | std::reverse(actionpoints_.begin(), actionpoints_.end()); |
---|
101 | if (this->parsedActionpoints_.empty()) |
---|
102 | { |
---|
103 | this->action_ = Action::FIGHTALL; |
---|
104 | } |
---|
105 | //orxout (internal_error) << "first tick was called by id = " << this->actionpointControllerId_ << ", total = " << ActionpointController::nextActionpointControllerId_ << endl; |
---|
106 | } |
---|
107 | |
---|
108 | if (!this || !this->getControllableEntity()) |
---|
109 | return; |
---|
110 | |
---|
111 | if (this->bHasTargetPosition_) |
---|
112 | { |
---|
113 | this->moveToTargetPosition(dt); |
---|
114 | } |
---|
115 | if (!this || !this->getControllableEntity()) |
---|
116 | return; |
---|
117 | else if (this->bLookAtTarget_) |
---|
118 | { |
---|
119 | this->lookAtTarget(dt); |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | if (!this || !this->getControllableEntity()) |
---|
124 | return; |
---|
125 | |
---|
126 | if (timeout_ <= 0) |
---|
127 | this->bFiredRocket_ = false; |
---|
128 | |
---|
129 | |
---|
130 | if (!this || !this->getControllableEntity()) |
---|
131 | return; |
---|
132 | if (this->timeout_ > 0 && this->bFiredRocket_) |
---|
133 | { |
---|
134 | --this->timeout_; |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | if (!this || !this->getControllableEntity()) |
---|
139 | return; |
---|
140 | //maneuver every 0.25 sec -> |
---|
141 | int step = 4; |
---|
142 | if (ActionpointController::sTicks_ % 100 <= 10) |
---|
143 | { |
---|
144 | this->bDodge_ = false; |
---|
145 | } |
---|
146 | else |
---|
147 | { |
---|
148 | this->bDodge_ = true; |
---|
149 | } |
---|
150 | if (!this || !this->getControllableEntity()) |
---|
151 | return; |
---|
152 | if (ActionpointController::sTicks_ % 100 == step * this->actionpointControllerId_ + 1) |
---|
153 | { |
---|
154 | //orxout (internal_error) << "Team " << this->getControllableEntity()->getTeam() << (this->hasTarget() ? ", got " : ", don't have") << " target" << endl; |
---|
155 | this->action(); |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | if (!this || !this->getControllableEntity()) |
---|
160 | return; |
---|
161 | /* orxout (internal_error) << "id = " << this->actionpointControllerId_ << ", in total # ids = " << ActionpointController::nextActionpointControllerId_ |
---|
162 | << ", I " << (this->hasTarget() ? "have" : "don't have") << " a target, my team is " << this->getControllableEntity()->getTeam() << endl; |
---|
163 | */ if (this->hasTarget() && ActionpointController::sTicks_ % (ActionpointController::nextActionpointControllerId_ + 1) == (this->actionpointControllerId_)) |
---|
164 | { |
---|
165 | |
---|
166 | |
---|
167 | if (!this || !this->getControllableEntity()) |
---|
168 | return; |
---|
169 | this->maneuver(); |
---|
170 | if (!this || !this->getControllableEntity()) |
---|
171 | return; |
---|
172 | this->bShooting_ = this->canFire(); |
---|
173 | |
---|
174 | if (!this || !this->getControllableEntity()) |
---|
175 | return; |
---|
176 | |
---|
177 | if (this->bShooting_) |
---|
178 | { |
---|
179 | this->doFire(); |
---|
180 | } |
---|
181 | if (!this || !this->getControllableEntity()) |
---|
182 | return; |
---|
183 | this->deltaHp = orxonox_cast<Pawn*> (this->getControllableEntity())->getHealth() - this->previousHp; |
---|
184 | this->previousHp = orxonox_cast<Pawn*> (this->getControllableEntity())->getHealth(); |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | SUPER(ActionpointController, tick, dt); |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | |
---|
193 | void ActionpointController::action() |
---|
194 | { |
---|
195 | if (!this || !this->getControllableEntity()) |
---|
196 | return; |
---|
197 | |
---|
198 | if (this->bDefaultPatrol_ || (this->action_ != Action::FLY && this->action_ != Action::NONE)) |
---|
199 | { |
---|
200 | this->startAttackingEnemiesThatAreClose(); |
---|
201 | } |
---|
202 | if (!this || !this->getControllableEntity()) |
---|
203 | return; |
---|
204 | |
---|
205 | //No action -> pop one from stack |
---|
206 | if (this->action_ == Action::NONE || this->bTakenOver_) |
---|
207 | { |
---|
208 | if (this->parsedActionpoints_.empty() && this->loopActionpoints_.empty() && this->bDefaultFightAll_) |
---|
209 | { |
---|
210 | if (!this || !this->getControllableEntity()) |
---|
211 | return; |
---|
212 | Point p = { Action::FIGHTALL, "", Vector3::ZERO, false }; |
---|
213 | this->parsedActionpoints_.push_back (p); |
---|
214 | } |
---|
215 | if (!this || !this->getControllableEntity()) |
---|
216 | return; |
---|
217 | this->executeActionpoint(); |
---|
218 | if (!this || !this->getControllableEntity()) |
---|
219 | return; |
---|
220 | this->bTakenOver_ = false; |
---|
221 | } |
---|
222 | if (!this || !this->getControllableEntity()) |
---|
223 | return; |
---|
224 | |
---|
225 | //Action fightall -> fight till nobody alive |
---|
226 | if (this->action_ == Action::FIGHTALL) |
---|
227 | { |
---|
228 | |
---|
229 | if (!this->hasTarget()) |
---|
230 | { |
---|
231 | ControllableEntity* newTarget = this->closestTarget(); |
---|
232 | if (newTarget) |
---|
233 | { |
---|
234 | if (!this || !this->getControllableEntity()) |
---|
235 | return; |
---|
236 | this->setAction (Action::FIGHTALL, newTarget); |
---|
237 | } |
---|
238 | else |
---|
239 | { |
---|
240 | if (!this || !this->getControllableEntity()) |
---|
241 | return; |
---|
242 | this->nextActionpoint(); |
---|
243 | if (!this || !this->getControllableEntity()) |
---|
244 | return; |
---|
245 | this->executeActionpoint(); |
---|
246 | |
---|
247 | } |
---|
248 | } |
---|
249 | } |
---|
250 | //Action fight -> fight as long as enemies in range |
---|
251 | else if (this->action_ == Action::FIGHT) |
---|
252 | { |
---|
253 | if (!this->hasTarget() ) |
---|
254 | { |
---|
255 | //----find a target---- |
---|
256 | ControllableEntity* newTarget = this->closestTarget(); |
---|
257 | if (!this || !this->getControllableEntity()) |
---|
258 | return; |
---|
259 | if (newTarget && |
---|
260 | CommonController::distance (this->getControllableEntity(), newTarget) < this->attackRange_) |
---|
261 | { |
---|
262 | if (!this || !this->getControllableEntity()) |
---|
263 | return; |
---|
264 | this->setAction (Action::FIGHT, newTarget); |
---|
265 | } |
---|
266 | else |
---|
267 | { |
---|
268 | if (!this || !this->getControllableEntity()) |
---|
269 | return; |
---|
270 | this->nextActionpoint(); |
---|
271 | if (!this || !this->getControllableEntity()) |
---|
272 | return; |
---|
273 | this->executeActionpoint(); |
---|
274 | } |
---|
275 | } |
---|
276 | else if (this->hasTarget()) |
---|
277 | { |
---|
278 | //----fly in formation if far enough---- |
---|
279 | Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition(); |
---|
280 | |
---|
281 | if (diffVector.length() > this->attackRange_) |
---|
282 | { |
---|
283 | ControllableEntity* newTarget = this->closestTarget(); |
---|
284 | if (!this || !this->getControllableEntity()) |
---|
285 | return; |
---|
286 | if (newTarget && |
---|
287 | CommonController::distance (this->getControllableEntity(), newTarget) < this->attackRange_) |
---|
288 | { |
---|
289 | if (!this || !this->getControllableEntity()) |
---|
290 | return; |
---|
291 | this->setAction (Action::FIGHT, newTarget); |
---|
292 | } |
---|
293 | else |
---|
294 | { |
---|
295 | if (!this || !this->getControllableEntity()) |
---|
296 | return; |
---|
297 | this->nextActionpoint(); |
---|
298 | if (!this || !this->getControllableEntity()) |
---|
299 | return; |
---|
300 | this->executeActionpoint(); |
---|
301 | } |
---|
302 | } |
---|
303 | } |
---|
304 | } |
---|
305 | else if (this->action_ == Action::FLY) |
---|
306 | { |
---|
307 | if (this->squaredDistanceToTarget() <= this->squaredaccuracy_) |
---|
308 | { |
---|
309 | if (!this || !this->getControllableEntity()) |
---|
310 | return; |
---|
311 | this->nextActionpoint(); |
---|
312 | if (!this || !this->getControllableEntity()) |
---|
313 | return; |
---|
314 | this->executeActionpoint(); |
---|
315 | } |
---|
316 | } |
---|
317 | else if (this->action_ == Action::PROTECT) |
---|
318 | { |
---|
319 | if (!this->getProtect()) |
---|
320 | { |
---|
321 | if (!this || !this->getControllableEntity()) |
---|
322 | return; |
---|
323 | this->nextActionpoint(); |
---|
324 | if (!this || !this->getControllableEntity()) |
---|
325 | return; |
---|
326 | this->executeActionpoint(); |
---|
327 | } |
---|
328 | if (!this || !this->getControllableEntity()) |
---|
329 | return; |
---|
330 | this->stayNearProtect(); |
---|
331 | } |
---|
332 | else if (this->action_ == Action::ATTACK) |
---|
333 | { |
---|
334 | if (!this || !this->getControllableEntity()) |
---|
335 | return; |
---|
336 | if (!this->hasTarget()) |
---|
337 | { |
---|
338 | if (!this || !this->getControllableEntity()) |
---|
339 | return; |
---|
340 | this->nextActionpoint(); |
---|
341 | if (!this || !this->getControllableEntity()) |
---|
342 | return; |
---|
343 | this->executeActionpoint(); |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | |
---|
348 | } |
---|
349 | void ActionpointController::setProtect (ControllableEntity* protect) |
---|
350 | { |
---|
351 | |
---|
352 | this->protect_ = protect; |
---|
353 | } |
---|
354 | ControllableEntity* ActionpointController::getProtect () |
---|
355 | { |
---|
356 | |
---|
357 | return this->protect_; |
---|
358 | } |
---|
359 | void ActionpointController::addActionpoint(WorldEntity* actionpoint) |
---|
360 | { |
---|
361 | std::string actionName; |
---|
362 | Vector3 position; |
---|
363 | std::string targetName; |
---|
364 | bool inLoop = false; |
---|
365 | Point p; |
---|
366 | if (actionpoint->getIdentifier()->getName() == "Actionpoint") |
---|
367 | { |
---|
368 | Actionpoint* ap = orxonox_cast<Actionpoint*> (actionpoint); |
---|
369 | actionName = ap->getActionXML(); |
---|
370 | targetName = ap->getName(); |
---|
371 | position = ap->getWorldPosition(); |
---|
372 | |
---|
373 | if (this->bEndLoop_) |
---|
374 | { |
---|
375 | this->bInLoop_ = false; |
---|
376 | } |
---|
377 | if (!this->bInLoop_ && ap->getLoopStart()) |
---|
378 | { |
---|
379 | this->bInLoop_ = true; |
---|
380 | } |
---|
381 | if (this->bInLoop_ && ap->getLoopEnd()) |
---|
382 | { |
---|
383 | this->bEndLoop_ = true; |
---|
384 | } |
---|
385 | inLoop = this->bInLoop_; |
---|
386 | |
---|
387 | Action::Value value; |
---|
388 | |
---|
389 | if ( actionName == "FIGHT" ) |
---|
390 | { value = Action::FIGHT; } |
---|
391 | else if ( actionName == "FLY" ) |
---|
392 | { value = Action::FLY; } |
---|
393 | else if ( actionName == "PROTECT" ) |
---|
394 | { value = Action::PROTECT; } |
---|
395 | else if ( actionName == "NONE" ) |
---|
396 | { value = Action::NONE; } |
---|
397 | else if ( actionName == "FIGHTALL" ) |
---|
398 | { value = Action::FIGHTALL; } |
---|
399 | else if ( actionName == "ATTACK" ) |
---|
400 | { value = Action::ATTACK; } |
---|
401 | else |
---|
402 | ThrowException( ParseError, std::string( "Attempting to set an unknown Action: '" )+ actionName + "'." ); |
---|
403 | p.action = value; p.name = targetName; p.position = position; p.inLoop = inLoop; |
---|
404 | } |
---|
405 | else |
---|
406 | { |
---|
407 | inLoop = true; |
---|
408 | p.action = Action::FLY; p.name = ""; p.position = actionpoint->getWorldPosition(); p.inLoop = inLoop; |
---|
409 | } |
---|
410 | parsedActionpoints_.push_back(p); |
---|
411 | this->actionpoints_.push_back(actionpoint); |
---|
412 | } |
---|
413 | WorldEntity* ActionpointController::getActionpoint(unsigned int index) const |
---|
414 | { |
---|
415 | if (index < this->actionpoints_.size()) |
---|
416 | return this->actionpoints_[index]; |
---|
417 | else |
---|
418 | return 0; |
---|
419 | } |
---|
420 | |
---|
421 | Action::Value ActionpointController::getAction () |
---|
422 | { |
---|
423 | return this->action_; |
---|
424 | } |
---|
425 | std::string ActionpointController::getActionName() |
---|
426 | { |
---|
427 | switch ( this->action_ ) |
---|
428 | { |
---|
429 | case Action::FIGHT: |
---|
430 | { return "FIGHT"; } |
---|
431 | case Action::FLY: |
---|
432 | { return "FLY"; } |
---|
433 | case Action::PROTECT: |
---|
434 | { return "PROTECT"; } |
---|
435 | case Action::NONE: |
---|
436 | { return "NONE"; } |
---|
437 | case Action::FIGHTALL: |
---|
438 | { return "FIGHTALL"; } |
---|
439 | case Action::ATTACK: |
---|
440 | { return "ATTACK"; } |
---|
441 | default: |
---|
442 | return "NONE"; |
---|
443 | break; |
---|
444 | } |
---|
445 | } |
---|
446 | void ActionpointController::setAction (Action::Value action) |
---|
447 | { |
---|
448 | this->action_ = action; |
---|
449 | } |
---|
450 | void ActionpointController::setAction (Action::Value action, ControllableEntity* target) |
---|
451 | { |
---|
452 | if (!this || !this->getControllableEntity()) |
---|
453 | return; |
---|
454 | this->action_ = action; |
---|
455 | if (action == Action::FIGHT || action == Action::FIGHTALL || action == Action::ATTACK) |
---|
456 | { |
---|
457 | if (target) |
---|
458 | this->setTarget (target); |
---|
459 | } |
---|
460 | else if (action == Action::PROTECT) |
---|
461 | { |
---|
462 | if (target) |
---|
463 | this->setProtect (target); |
---|
464 | } |
---|
465 | } |
---|
466 | void ActionpointController::setAction (Action::Value action, const Vector3& target) |
---|
467 | { |
---|
468 | if (!this || !this->getControllableEntity()) |
---|
469 | return; |
---|
470 | this->action_ = action; |
---|
471 | if (action == Action::FLY) |
---|
472 | { |
---|
473 | this->setTargetPosition (target); |
---|
474 | } |
---|
475 | } |
---|
476 | void ActionpointController::setAction (Action::Value action, const Vector3& target, const Quaternion& orient ) |
---|
477 | { |
---|
478 | if (!this || !this->getControllableEntity()) |
---|
479 | return; |
---|
480 | this->action_ = action; |
---|
481 | if (action == Action::FLY) |
---|
482 | { |
---|
483 | this->setTargetPosition (target); |
---|
484 | this->setTargetOrientation (orient); |
---|
485 | } |
---|
486 | } |
---|
487 | |
---|
488 | //------------------------------------------------------------------------------ |
---|
489 | //------------------------------Actionpoint methods----------------------------- |
---|
490 | //------------------------------------------------------------------------------ |
---|
491 | |
---|
492 | //POST: this starts doing what was asked by the last element of parsedActionpoints_, |
---|
493 | //if last element was failed to be parsed, next element will be executed. |
---|
494 | void ActionpointController::executeActionpoint() |
---|
495 | { |
---|
496 | if (!this || !this->getControllableEntity()) |
---|
497 | return; |
---|
498 | |
---|
499 | Point p; |
---|
500 | if (this->bLoop_ && !loopActionpoints_.empty()) |
---|
501 | { |
---|
502 | p = loopActionpoints_.back(); |
---|
503 | } |
---|
504 | else if (this->bLoop_) |
---|
505 | { |
---|
506 | this->bLoop_ = false; |
---|
507 | return; |
---|
508 | } |
---|
509 | else if (!this->bLoop_ && !parsedActionpoints_.empty()) |
---|
510 | { |
---|
511 | p = parsedActionpoints_.back(); |
---|
512 | } |
---|
513 | else |
---|
514 | { |
---|
515 | if (!this || !this->getControllableEntity()) |
---|
516 | return; |
---|
517 | |
---|
518 | this->setTarget(0); |
---|
519 | this->setTargetPosition(this->getControllableEntity()->getWorldPosition()); |
---|
520 | this->action_ = Action::NONE; |
---|
521 | return; |
---|
522 | } |
---|
523 | if (!this || !this->getControllableEntity()) |
---|
524 | return; |
---|
525 | if (!this->bLoop_ && this->parsedActionpoints_.back().inLoop) |
---|
526 | { |
---|
527 | //MOVES all points that are in loop to a loop vector |
---|
528 | this->fillLoop(); |
---|
529 | if (!this || !this->getControllableEntity()) |
---|
530 | return; |
---|
531 | this->bLoop_ = true; |
---|
532 | executeActionpoint(); |
---|
533 | return; |
---|
534 | } |
---|
535 | if (!this || !this->getControllableEntity()) |
---|
536 | return; |
---|
537 | this->setAction (p.action); |
---|
538 | if (!this || !this->getControllableEntity()) |
---|
539 | return; |
---|
540 | |
---|
541 | switch (this->action_) |
---|
542 | { |
---|
543 | case Action::FIGHT: |
---|
544 | { |
---|
545 | std::string targetName = p.name; |
---|
546 | if (targetName == "") |
---|
547 | break; |
---|
548 | for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) |
---|
549 | { |
---|
550 | if (!this || !this->getControllableEntity()) |
---|
551 | return; |
---|
552 | if (CommonController::getName(*itP) == targetName) |
---|
553 | { |
---|
554 | this->setTarget (static_cast<ControllableEntity*>(*itP)); |
---|
555 | } |
---|
556 | } |
---|
557 | break; |
---|
558 | } |
---|
559 | case Action::FLY: |
---|
560 | { |
---|
561 | this->setTargetPosition( p.position ); |
---|
562 | if (!this || !this->getControllableEntity()) |
---|
563 | return; |
---|
564 | if (this->squaredDistanceToTarget() <= this->squaredaccuracy_) |
---|
565 | { |
---|
566 | if (!this || !this->getControllableEntity()) |
---|
567 | return; |
---|
568 | this->nextActionpoint(); |
---|
569 | if (!this || !this->getControllableEntity()) |
---|
570 | return; |
---|
571 | this->executeActionpoint(); |
---|
572 | } |
---|
573 | break; |
---|
574 | } |
---|
575 | case Action::PROTECT: |
---|
576 | { |
---|
577 | if (!this || !this->getControllableEntity()) |
---|
578 | return; |
---|
579 | |
---|
580 | std::string protectName = p.name; |
---|
581 | if (protectName == "reservedKeyword:human") |
---|
582 | { |
---|
583 | for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) |
---|
584 | { |
---|
585 | if (orxonox_cast<ControllableEntity*>(*itP) && ((*itP)->getController()) && ((*itP)->getController()->getIdentifier()->getName() == "NewHumanController")) |
---|
586 | { |
---|
587 | this->setProtect (static_cast<ControllableEntity*>(*itP)); |
---|
588 | } |
---|
589 | } |
---|
590 | } |
---|
591 | else |
---|
592 | { |
---|
593 | for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) |
---|
594 | { |
---|
595 | if (CommonController::getName(*itP) == protectName) |
---|
596 | { |
---|
597 | this->setProtect (static_cast<ControllableEntity*>(*itP)); |
---|
598 | } |
---|
599 | } |
---|
600 | } |
---|
601 | if (!this->getProtect()) |
---|
602 | { |
---|
603 | this->nextActionpoint(); |
---|
604 | this->executeActionpoint(); |
---|
605 | } |
---|
606 | break; |
---|
607 | } |
---|
608 | case Action::NONE: |
---|
609 | { |
---|
610 | break; |
---|
611 | } |
---|
612 | case Action::FIGHTALL: |
---|
613 | { |
---|
614 | break; |
---|
615 | } |
---|
616 | case Action::ATTACK: |
---|
617 | { |
---|
618 | std::string targetName = p.name; |
---|
619 | |
---|
620 | for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) |
---|
621 | { |
---|
622 | if (CommonController::getName(*itP) == targetName) |
---|
623 | { |
---|
624 | if (!this || !this->getControllableEntity()) |
---|
625 | return; |
---|
626 | this->setTarget (static_cast<ControllableEntity*>(*itP)); |
---|
627 | } |
---|
628 | } |
---|
629 | if (!this->hasTarget()) |
---|
630 | { |
---|
631 | this->nextActionpoint(); |
---|
632 | if (!this || !this->getControllableEntity()) |
---|
633 | return; |
---|
634 | this->executeActionpoint(); |
---|
635 | } |
---|
636 | break; |
---|
637 | } |
---|
638 | default: |
---|
639 | break; |
---|
640 | } |
---|
641 | } |
---|
642 | |
---|
643 | |
---|
644 | void ActionpointController::stayNearProtect() |
---|
645 | { |
---|
646 | if (!this || !this->getControllableEntity()) |
---|
647 | return; |
---|
648 | |
---|
649 | Vector3 targetRelativePosition(0, 300, 300); |
---|
650 | if (!this->getProtect()) |
---|
651 | { |
---|
652 | this->nextActionpoint(); |
---|
653 | return; |
---|
654 | } |
---|
655 | Vector3 targetAbsolutePosition = ((this->getProtect()->getWorldPosition()) + |
---|
656 | (this->getProtect()->getWorldOrientation()* (targetRelativePosition))); |
---|
657 | this->setTargetPosition(targetAbsolutePosition); |
---|
658 | if (!this->getProtect()) |
---|
659 | { |
---|
660 | this->nextActionpoint(); |
---|
661 | return; |
---|
662 | } |
---|
663 | this->setTargetOrientation(this->getProtect()->getWorldOrientation()); |
---|
664 | } |
---|
665 | void ActionpointController::nextActionpoint() |
---|
666 | { |
---|
667 | if (!this || !this->getControllableEntity()) |
---|
668 | return; |
---|
669 | if (this->bLoop_) |
---|
670 | { |
---|
671 | if (this->bPatrolling_) |
---|
672 | { |
---|
673 | this->loopActionpoints_.pop_back(); |
---|
674 | this->bPatrolling_ = false; |
---|
675 | } |
---|
676 | else if (!this->loopActionpoints_.empty()) |
---|
677 | { |
---|
678 | this->moveBackToTop(); |
---|
679 | } |
---|
680 | } |
---|
681 | else |
---|
682 | { |
---|
683 | if (!this->parsedActionpoints_.empty()) |
---|
684 | { |
---|
685 | this->parsedActionpoints_.pop_back(); |
---|
686 | } |
---|
687 | } |
---|
688 | this->setAction(Action::NONE); |
---|
689 | this->bHasTargetPosition_ = false; |
---|
690 | } |
---|
691 | void ActionpointController::moveBackToTop() |
---|
692 | { |
---|
693 | if (!this || !this->getControllableEntity()) |
---|
694 | return; |
---|
695 | |
---|
696 | Point temp = loopActionpoints_.back(); |
---|
697 | loopActionpoints_.pop_back(); |
---|
698 | std::reverse (loopActionpoints_.begin(), loopActionpoints_.end()); |
---|
699 | loopActionpoints_.push_back(temp); |
---|
700 | std::reverse (loopActionpoints_.begin(), loopActionpoints_.end()); |
---|
701 | } |
---|
702 | void ActionpointController::fillLoop() |
---|
703 | { |
---|
704 | loopActionpoints_.clear(); |
---|
705 | fillLoopReversed(); |
---|
706 | std::reverse (loopActionpoints_.begin(), loopActionpoints_.end()); |
---|
707 | } |
---|
708 | void ActionpointController::fillLoopReversed() |
---|
709 | { |
---|
710 | if (parsedActionpoints_.back().inLoop) |
---|
711 | { |
---|
712 | loopActionpoints_.push_back(parsedActionpoints_.back()); |
---|
713 | parsedActionpoints_.pop_back(); |
---|
714 | } |
---|
715 | if (parsedActionpoints_.back().inLoop) |
---|
716 | { |
---|
717 | fillLoopReversed(); |
---|
718 | } |
---|
719 | } |
---|
720 | |
---|
721 | void ActionpointController::takeActionpoints (const std::vector<Point>& vector, const std::vector<Point>& loop, bool b) |
---|
722 | { |
---|
723 | if (!this || !this->getControllableEntity()) |
---|
724 | return; |
---|
725 | this->parsedActionpoints_ = vector; |
---|
726 | if (!this || !this->getControllableEntity()) |
---|
727 | return; |
---|
728 | this->loopActionpoints_ = loop; |
---|
729 | this->bLoop_ = b; |
---|
730 | this->bTakenOver_ = true; |
---|
731 | } |
---|
732 | void ActionpointController::setClosestTarget() |
---|
733 | { |
---|
734 | this->setTarget (static_cast<ControllableEntity*>( closestTarget() ) ); |
---|
735 | } |
---|
736 | Pawn* ActionpointController::closestTarget() |
---|
737 | { |
---|
738 | if (!this || !this->getControllableEntity()) |
---|
739 | return 0; |
---|
740 | |
---|
741 | Pawn* closestTarget = 0; |
---|
742 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
743 | Gametype* gt = this->getGametype(); |
---|
744 | for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) |
---|
745 | { |
---|
746 | if (!this || !this->getControllableEntity()) |
---|
747 | return 0; |
---|
748 | if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) ) |
---|
749 | continue; |
---|
750 | |
---|
751 | float distance = CommonController::distance (*itP, this->getControllableEntity()); |
---|
752 | if (distance < minDistance) |
---|
753 | { |
---|
754 | closestTarget = *itP; |
---|
755 | minDistance = distance; |
---|
756 | } |
---|
757 | } |
---|
758 | if (closestTarget) |
---|
759 | { |
---|
760 | return closestTarget; |
---|
761 | } |
---|
762 | return 0; |
---|
763 | } |
---|
764 | void ActionpointController::startAttackingEnemiesThatAreClose() |
---|
765 | { |
---|
766 | if (!this || !this->getControllableEntity()) |
---|
767 | return; |
---|
768 | |
---|
769 | //if (this->action_ != Action::FIGHT && this->action_ != Action::FIGHTALL) |
---|
770 | { |
---|
771 | if (!this->target_ || (this->target_ && CommonController::distance (this->getControllableEntity(), this->target_) > this->attackRange_)) |
---|
772 | { |
---|
773 | if (!this || !this->getControllableEntity()) |
---|
774 | return; |
---|
775 | Pawn* newTarget = this->closestTarget(); |
---|
776 | if ( newTarget && |
---|
777 | CommonController::distance (this->getControllableEntity(), static_cast<ControllableEntity*>(newTarget)) |
---|
778 | <= this->attackRange_ ) |
---|
779 | { |
---|
780 | if (!this || !this->getControllableEntity()) |
---|
781 | return; |
---|
782 | this->setTarget(newTarget); |
---|
783 | if (this->bLoop_ && !this->bPatrolling_) |
---|
784 | { |
---|
785 | Point p = { Action::FIGHT, "", Vector3::ZERO, true }; |
---|
786 | this->loopActionpoints_.push_back(p); |
---|
787 | } |
---|
788 | else if (!this->bPatrolling_) |
---|
789 | { |
---|
790 | //orxout (internal_error) << "found new target " << CommonController::getName(newTarget) <<endl; |
---|
791 | Point p = { Action::FIGHT, "", Vector3::ZERO, false }; |
---|
792 | this->parsedActionpoints_.push_back(p); |
---|
793 | } |
---|
794 | this->bPatrolling_ = true; |
---|
795 | this->executeActionpoint(); |
---|
796 | } |
---|
797 | } |
---|
798 | } |
---|
799 | } |
---|
800 | unsigned int ActionpointController::nextActionpointControllerId_ = 0; |
---|
801 | unsigned int ActionpointController::sTicks_ = 0; |
---|
802 | } |
---|