Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/campaignHS15/src/orxonox/controllers/ActionpointController.cc @ 10935

Last change on this file since 10935 was 10935, checked in by gania, 8 years ago

cleaned source up a bit

File size: 24.1 KB
Line 
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
35namespace 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        else if (this->bLookAtTarget_)
116        {
117            this->lookAtTarget(dt);
118        }
119       
120
121        if (!this || !this->getControllableEntity())
122            return;
123
124        if (timeout_ <= 0)
125            this->bFiredRocket_ = false;
126       
127
128        if (!this || !this->getControllableEntity())
129            return;
130        if (this->timeout_ > 0 && this->bFiredRocket_)
131        {
132            --this->timeout_;
133        }
134   
135
136        if (!this || !this->getControllableEntity())
137            return;
138        //maneuver every 0.25 sec ->
139        int step =  4;
140        if (ActionpointController::sTicks_ % 100 <= 10)
141        {
142            this->bDodge_ = false;
143        }
144        else
145        {
146            this->bDodge_ = true;
147        }
148        if (ActionpointController::sTicks_ % 100 == step * this->actionpointControllerId_ + 1)
149        {
150            //orxout (internal_error) << "Team " << this->getControllableEntity()->getTeam() << (this->hasTarget() ? ", got " : ", don't have") << " target" << endl;
151            this->action();
152        }
153
154
155        if (!this || !this->getControllableEntity())
156            return;
157       /* orxout (internal_error) << "id = " << this->actionpointControllerId_ << ", in total # ids = " << ActionpointController::nextActionpointControllerId_
158        << ", I " << (this->hasTarget() ? "have" : "don't have") << " a target, my team is " << this->getControllableEntity()->getTeam() << endl;
159       */ if (this->hasTarget() &&  ActionpointController::sTicks_ % (ActionpointController::nextActionpointControllerId_ + 1) == (this->actionpointControllerId_))
160        {
161
162         
163            if (!this || !this->getControllableEntity())
164                return;
165            this->maneuver();
166            if (!this || !this->getControllableEntity())
167                return;
168            this->bShooting_ = this->canFire();
169
170            if (!this || !this->getControllableEntity())
171                return;
172
173            if (this->bShooting_)
174            {
175                this->doFire();
176            }
177            this->deltaHp = orxonox_cast<Pawn*> (this->getControllableEntity())->getHealth() - this->previousHp;
178            this->previousHp = orxonox_cast<Pawn*> (this->getControllableEntity())->getHealth();
179        }
180       
181
182        SUPER(ActionpointController, tick, dt);
183    }
184     
185
186
187    void ActionpointController::action()
188    {
189        if (!this || !this->getControllableEntity())
190            return;
191
192        if (this->bDefaultPatrol_ || (this->action_ != Action::FLY && this->action_ != Action::NONE))
193        {
194            this->startAttackingEnemiesThatAreClose();
195        }
196        if (!this || !this->getControllableEntity())
197            return;
198
199        //No action -> pop one from stack
200        if (this->action_ == Action::NONE || this->bTakenOver_)
201        {
202            if (this->parsedActionpoints_.empty() && this->loopActionpoints_.empty() && this->bDefaultFightAll_)
203            {
204                Point p = { Action::FIGHTALL, "", Vector3::ZERO, false };
205                this->parsedActionpoints_.push_back (p);
206            }
207            this->executeActionpoint();
208            this->bTakenOver_ = false;
209        }
210        if (!this || !this->getControllableEntity())
211            return;
212
213        //Action fightall -> fight till nobody alive
214        if (this->action_ == Action::FIGHTALL)
215        {
216
217            if (!this->hasTarget())
218            {
219                ControllableEntity* newTarget = this->closestTarget();   
220                if (newTarget)
221                {
222                    this->setAction (Action::FIGHTALL, newTarget);
223                }
224                else
225                {
226                    this->nextActionpoint();
227                    this->executeActionpoint();
228   
229                }
230            }
231        }
232        //Action fight -> fight as long as enemies in range
233        else if (this->action_ == Action::FIGHT)
234        {
235
236            if (!this->hasTarget() )
237            {
238                //----find a target----
239                ControllableEntity* newTarget = this->closestTarget();   
240                if (newTarget && 
241                        CommonController::distance (this->getControllableEntity(), newTarget) < this->attackRange_)
242                {
243                    this->setAction (Action::FIGHT, newTarget);
244                }
245                else
246                {
247                    this->nextActionpoint();
248                    this->executeActionpoint();
249                }
250            }
251            else if (this->hasTarget())
252            {
253                //----fly in formation if far enough----
254                Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();         
255                   
256                if (diffVector.length() > this->attackRange_)
257                {
258                    ControllableEntity* newTarget = this->closestTarget();
259                   
260                    if (newTarget && 
261                        CommonController::distance (this->getControllableEntity(), newTarget) < this->attackRange_)
262                    {
263                        this->setAction (Action::FIGHT, newTarget);
264                    }
265                    else
266                    {
267                        this->nextActionpoint();
268                        this->executeActionpoint();
269                    }
270                }
271            }
272        }
273        else if (this->action_ == Action::FLY)
274        {
275            if (this->squaredDistanceToTarget() <= this->squaredaccuracy_)
276            {
277                this->nextActionpoint();   
278                this->executeActionpoint();
279            }
280        }
281        else if (this->action_ == Action::PROTECT)
282        {
283            if (!this->getProtect())
284            {
285                this->nextActionpoint();
286                this->executeActionpoint(); 
287            }
288            this->stayNearProtect();
289        }
290        else if (this->action_ == Action::ATTACK)
291        {   
292
293            if (!this->hasTarget())
294            {
295                this->nextActionpoint();
296                this->executeActionpoint();
297            }
298        }
299
300
301    }
302    void ActionpointController::setProtect (ControllableEntity* protect)
303    {
304        this->protect_ = protect;
305    }
306    ControllableEntity* ActionpointController::getProtect ()
307    {
308        return this->protect_;
309    }
310    void ActionpointController::addActionpoint(WorldEntity* actionpoint)
311    {
312        std::string actionName;
313        Vector3 position;
314        std::string targetName;
315        bool inLoop = false;
316        Point p;
317        if (actionpoint->getIdentifier()->getName() == "Actionpoint")
318        {
319            Actionpoint* ap = orxonox_cast<Actionpoint*> (actionpoint);
320            actionName = ap->getActionXML();
321            targetName = ap->getName();
322            position = ap->getWorldPosition();
323
324            if (this->bEndLoop_)
325            {
326                this->bInLoop_ = false;
327            }
328            if (!this->bInLoop_ && ap->getLoopStart())
329            {
330                this->bInLoop_ = true;
331            }
332            if (this->bInLoop_ && ap->getLoopEnd())
333            {
334                this->bEndLoop_ = true;
335            }
336            inLoop = this->bInLoop_;
337
338            Action::Value value;
339           
340            if ( actionName == "FIGHT" )
341            { value = Action::FIGHT; }
342            else if ( actionName == "FLY" )
343            { value = Action::FLY; }
344            else if ( actionName == "PROTECT" )
345            { value = Action::PROTECT; }
346            else if ( actionName == "NONE" )
347            { value = Action::NONE; }
348            else if ( actionName == "FIGHTALL" )
349            { value = Action::FIGHTALL; }
350            else if ( actionName == "ATTACK" )
351            { value = Action::ATTACK; }
352            else
353                ThrowException( ParseError, std::string( "Attempting to set an unknown Action: '" )+ actionName + "'." );
354            p.action = value; p.name = targetName; p.position = position; p.inLoop = inLoop;
355        }
356        else
357        {
358            inLoop = true;
359            p.action = Action::FLY; p.name = ""; p.position = actionpoint->getWorldPosition(); p.inLoop = inLoop;
360        }
361            parsedActionpoints_.push_back(p);
362            this->actionpoints_.push_back(actionpoint);
363    }
364    WorldEntity* ActionpointController::getActionpoint(unsigned int index) const
365    {
366        if (index < this->actionpoints_.size())
367            return this->actionpoints_[index];
368        else
369            return 0;
370    }
371
372    Action::Value ActionpointController::getAction ()
373    {
374        return this->action_;
375    }
376    std::string ActionpointController::getActionName()
377    {
378        switch ( this->action_ )
379        {
380            case Action::FIGHT:
381            { return "FIGHT"; }
382            case Action::FLY:
383            { return "FLY"; }
384            case Action::PROTECT:
385            { return "PROTECT"; }
386            case Action::NONE:
387            { return "NONE"; }
388            case Action::FIGHTALL:
389            { return "FIGHTALL"; }
390            case Action::ATTACK:
391            { return "ATTACK"; }
392            default:
393                return "NONE";
394                break;
395        }
396    }
397    void ActionpointController::setAction (Action::Value action)
398    {
399        this->action_ = action;
400    }
401    void ActionpointController::setAction (Action::Value action, ControllableEntity* target)
402    {
403        this->action_ = action;
404        if (action == Action::FIGHT || action == Action::FIGHTALL || action == Action::ATTACK)
405        {   
406            if (target)
407                this->setTarget (target);
408        }
409        else if (action == Action::PROTECT)
410        {
411            if (target)
412                this->setProtect (target);
413        }
414    }
415    void ActionpointController::setAction (Action::Value action, const Vector3& target)
416    {
417        this->action_ = action;
418        if (action == Action::FLY)
419        {
420            this->setTargetPosition (target);
421        }
422    }
423    void ActionpointController::setAction (Action::Value action, const Vector3& target,  const Quaternion& orient )
424    {
425        this->action_ = action;
426        if (action == Action::FLY)
427        {
428            this->setTargetPosition (target);
429            this->setTargetOrientation (orient);
430        } 
431    }
432   
433    //------------------------------------------------------------------------------
434    //------------------------------Actionpoint methods-----------------------------
435    //------------------------------------------------------------------------------
436
437    //POST: this starts doing what was asked by the last element of parsedActionpoints_,
438    //if last element was failed to be parsed, next element will be executed.
439    void ActionpointController::executeActionpoint()
440    {
441        if (!this || !this->getControllableEntity())
442            return;
443
444        Point p;
445        if (this->bLoop_ && !loopActionpoints_.empty())
446        {
447            p = loopActionpoints_.back();
448        }
449        else if (this->bLoop_)
450        {
451            this->bLoop_ = false;
452            return;
453        }
454        else if (!this->bLoop_ && !parsedActionpoints_.empty())
455        {
456            p = parsedActionpoints_.back();
457        }
458        else
459        {
460            if (!this || !this->getControllableEntity())
461                return;
462
463            this->setTarget(0);
464            this->setTargetPosition(this->getControllableEntity()->getWorldPosition());
465            this->action_ = Action::NONE;
466            return;
467        }
468        if (!this->bLoop_ && this->parsedActionpoints_.back().inLoop)
469        {
470            //MOVES all points that are in loop to a loop vector
471            this->fillLoop();
472            this->bLoop_ = true;
473            executeActionpoint();
474            return;
475        }
476        this->setAction (p.action);
477        if (!this || !this->getControllableEntity())
478            return;
479
480        switch (this->action_)
481        {
482            case Action::FIGHT:
483            {
484                std::string targetName = p.name;
485                if (targetName == "")
486                    break;
487                for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
488                {
489                    if (CommonController::getName(*itP) == targetName)
490                    {
491                        this->setTarget (static_cast<ControllableEntity*>(*itP));
492                    }
493                }
494                break;
495            }
496            case Action::FLY:
497            {
498                this->setTargetPosition( p.position );
499                if (this->squaredDistanceToTarget() <= this->squaredaccuracy_)
500                {
501                    this->nextActionpoint();
502                    this->executeActionpoint();
503                }
504                break;
505            }
506            case Action::PROTECT:
507            {
508                if (!this || !this->getControllableEntity())
509                    return;
510
511                std::string protectName = p.name;
512                if (protectName == "reservedKeyword:human")
513                {
514                    for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
515                    {
516                        if (orxonox_cast<ControllableEntity*>(*itP) && ((*itP)->getController()) && ((*itP)->getController()->getIdentifier()->getName() == "NewHumanController"))
517                        {
518                            this->setProtect (static_cast<ControllableEntity*>(*itP));
519                        }
520                    }
521                }
522                else
523                {
524                    for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
525                    {
526                        if (CommonController::getName(*itP) == protectName)
527                        {
528                            this->setProtect (static_cast<ControllableEntity*>(*itP));
529                        }
530                    }                           
531                }
532                if (!this->getProtect())
533                {
534                    this->nextActionpoint();
535                    this->executeActionpoint();
536                }
537                break;
538            }
539            case Action::NONE:
540            {
541                break;
542            }
543            case Action::FIGHTALL:
544            {
545                break;
546            }
547            case Action::ATTACK:
548            {
549                std::string targetName = p.name;
550
551                for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
552                {
553                    if (CommonController::getName(*itP) == targetName)
554                    {
555                        this->setTarget (static_cast<ControllableEntity*>(*itP));
556                    }
557                }
558                if (!this->hasTarget())
559                {
560                    this->nextActionpoint();
561                    this->executeActionpoint();
562                }
563                break;
564            }
565            default:
566                break;
567        }   
568    }
569
570   
571    void ActionpointController::stayNearProtect()
572    {
573        if (!this || !this->getControllableEntity())
574            return;
575
576        Vector3 targetRelativePosition(0, 300, 300);
577        if (!this->getProtect())
578        {
579            this->nextActionpoint();
580            return;
581        } 
582        Vector3 targetAbsolutePosition = ((this->getProtect()->getWorldPosition()) + 
583            (this->getProtect()->getWorldOrientation()* (targetRelativePosition)));
584        this->setTargetPosition(targetAbsolutePosition);
585        if (!this->getProtect())
586        {
587            this->nextActionpoint();
588            return;
589        } 
590        this->setTargetOrientation(this->getProtect()->getWorldOrientation());
591    }
592    void ActionpointController::nextActionpoint()
593    {
594        if (!this || !this->getControllableEntity())
595            return;
596        if (this->bLoop_)
597        {
598            if (this->bPatrolling_)
599            {
600                this->loopActionpoints_.pop_back();
601                this->bPatrolling_ = false;
602            }
603            else if (!this->loopActionpoints_.empty())
604            {
605                this->moveBackToTop();
606            }
607        }
608        else
609        {
610            if (!this->parsedActionpoints_.empty())
611            {
612                this->parsedActionpoints_.pop_back();
613            }           
614        }
615        this->setAction(Action::NONE);
616        this->bHasTargetPosition_ = false;
617    }
618    void ActionpointController::moveBackToTop()
619    {
620        if (!this || !this->getControllableEntity())
621            return;
622
623        Point temp = loopActionpoints_.back();
624        loopActionpoints_.pop_back();
625        std::reverse (loopActionpoints_.begin(), loopActionpoints_.end());
626        loopActionpoints_.push_back(temp);
627        std::reverse (loopActionpoints_.begin(), loopActionpoints_.end());
628    }
629    void ActionpointController::fillLoop()
630    {
631        loopActionpoints_.clear();
632        fillLoopReversed();
633        std::reverse (loopActionpoints_.begin(), loopActionpoints_.end());
634    }
635    void ActionpointController::fillLoopReversed()
636    {
637        if (parsedActionpoints_.back().inLoop)
638        {
639            loopActionpoints_.push_back(parsedActionpoints_.back());
640            parsedActionpoints_.pop_back();
641        }
642        if (parsedActionpoints_.back().inLoop)
643        {
644            fillLoopReversed();
645        }
646    }
647   
648    void ActionpointController::takeActionpoints (const std::vector<Point>& vector, const std::vector<Point>& loop, bool b)
649    {
650      this->parsedActionpoints_ = vector;
651      this->loopActionpoints_ = loop;
652      this->bLoop_ = b;
653      this->bTakenOver_ = true;
654    }
655    void ActionpointController::setClosestTarget()
656    {
657        this->setTarget (static_cast<ControllableEntity*>( closestTarget() ) ); 
658    }
659    Pawn* ActionpointController::closestTarget()
660    {
661        if (!this || !this->getControllableEntity())
662            return 0;
663
664        Pawn* closestTarget = 0;
665        float minDistance =  std::numeric_limits<float>::infinity();
666        Gametype* gt = this->getGametype();
667        for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
668        {
669            if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) )
670                continue;
671
672            float distance = CommonController::distance (*itP, this->getControllableEntity());
673            if (distance < minDistance)
674            {
675                closestTarget = *itP;
676                minDistance = distance;
677            }
678        }
679        if (closestTarget)
680        {
681           return closestTarget;
682        } 
683        return 0; 
684    }
685    void ActionpointController::startAttackingEnemiesThatAreClose()
686    {
687        if (!this || !this->getControllableEntity())
688            return;
689
690        //if (this->action_ != Action::FIGHT && this->action_ != Action::FIGHTALL)
691        {
692            if (!this->target_ || (this->target_ && CommonController::distance (this->getControllableEntity(), this->target_) > this->attackRange_))
693            {
694                Pawn* newTarget = this->closestTarget();
695                if ( newTarget && 
696                    CommonController::distance (this->getControllableEntity(), static_cast<ControllableEntity*>(newTarget))
697                        <= this->attackRange_ )
698                {
699                    if (!this || !this->getControllableEntity())
700                        return;
701                    this->setTarget(newTarget);
702                    if (this->bLoop_ && !this->bPatrolling_)
703                    {
704                        Point p = { Action::FIGHT, "", Vector3::ZERO, true };
705                        this->loopActionpoints_.push_back(p);
706                    }
707                    else if (!this->bPatrolling_)
708                    {
709                        //orxout (internal_error) << "found new target " << CommonController::getName(newTarget) <<endl;
710                        Point p = { Action::FIGHT, "", Vector3::ZERO, false };
711                        this->parsedActionpoints_.push_back(p);
712                    }
713                    this->bPatrolling_ = true;
714                    this->executeActionpoint();
715                }
716            }
717        }
718    }
719    unsigned int ActionpointController::nextActionpointControllerId_ = 0;
720    unsigned int ActionpointController::sTicks_ = 0;
721}   
Note: See TracBrowser for help on using the repository browser.