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