Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed bugs that are not supposed to even exist by initializing a variable in the constructor

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