Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10871 for code/branches


Ignore:
Timestamp:
Nov 27, 2015, 9:25:52 AM (9 years ago)
Author:
gania
Message:

Split up CommonController, so it is easier to debug

Location:
code/branches/campaignHS15
Files:
4 added
6 edited

Legend:

Unmodified
Added
Removed
  • code/branches/campaignHS15/data/levels/AITest.oxw

    r10870 r10871  
    3535   
    3636<!-- HERE STARTS DEMO FOR THE "WAYPOINTS" -->
    37     <!-- 
     37      <!--
    3838    <Model mesh="cube.mesh" scale=8 position="  0,2000,-600" />
    39     <Model mesh="cube.mesh" scale=8 position="  0,2000,-700" />
    40     <Model mesh="cube.mesh" scale=8 position="100,2000,-700" />
    41     <Model mesh="cube.mesh" scale=8 position="100,2000,-600" />
     39    <Model mesh="cube.mesh" scale=8 position="  0,2000,-800" />
     40    <Model mesh="cube.mesh" scale=8 position="200,2000,-800" />
     41    <Model mesh="cube.mesh" scale=8 position="200,2000,-600" />
    4242    <Model mesh="cube.mesh" scale=8 position="0,0,0" />
    4343
     
    5050          <actionpoints>
    5151            <Actionpoint position="  0,2000,-600" action="FLY" loopStart=true/>
    52             <Actionpoint position="  0,2000,-700" action="FLY"  />
    53             <Actionpoint position="100,2000,-700" action="FLY" />
    54             <Actionpoint position="100,2000,-600" action="FLY" loopEnd=true />
     52            <Actionpoint position="  0,2000,-800" action="FLY"  />
     53            <Actionpoint position="200,2000,-800" action="FLY" />
     54            <Actionpoint position="200,2000,-600" action="FLY" loopEnd=true />
    5555          </actionpoints>
    5656        </DivisionController>
     
    6262<!-- HERE STARTS DEMO FOR THE ACTIONPOINTS.
    6363P.S. Never set protectMe in the first actionpoint: if human didn't spawn, that actionpoint will be skipped -->
    64        <!--
     64       
    6565    <Model mesh="cube.mesh" scale=8 position="0,0,0" />
    6666
     
    123123      </templates>
    124124    </SpaceShip>
    125   -->
     125 
    126126   
    127127<!-- HERE ENDS DEMO FOR THE ACTIONPOINTS -->
     
    210210<!-- HERE ENDS DEMO FOR FIGHTING -->
    211211<!-- HERE STARTS DEMO FOR FORMATIONS -->
    212    
     212    <!--
    213213    <Model mesh="cube.mesh" scale=8 position="   0,2000,    0" />
    214214    <Model mesh="cube.mesh" scale=8 position="   0,2000,-2000" />
     
    259259      </controller>
    260260    </SpaceShip>
    261    
     261    -->
    262262<!-- HERE ENDS DEMO FOR FORMATIONS -->
    263263   
  • code/branches/campaignHS15/src/orxonox/controllers/ActionpointController.cc

    r10864 r10871  
    3939
    4040    //CommonController contains all common functionality of AI Controllers
    41     ActionpointController::ActionpointController(Context* context) : CommonController(context)
     41    ActionpointController::ActionpointController(Context* context) : FightingController(context)
    4242    {
    4343              this->bInLoop_ = false;
     
    8787    ActionpointController::~ActionpointController()
    8888    {
    89       parsedActionpoints_.clear();
     89        loopActionpoints_.clear();
     90        parsedActionpoints_.clear();
    9091        actionpoints_.clear();
    9192    }
  • code/branches/campaignHS15/src/orxonox/controllers/ActionpointController.h

    r10864 r10871  
    3030#define _ActionpointController_H__
    3131
    32 #include "controllers/CommonController.h"
     32#include "controllers/FightingController.h"
    3333
    3434
     
    5151    } ;
    5252
    53     class _OrxonoxExport ActionpointController : public CommonController
     53    class _OrxonoxExport ActionpointController : public FightingController
    5454    {
    5555        public:
     
    9595                void setProtect (ControllableEntity* protect);
    9696                ControllableEntity* getProtect ();
     97                WeakPtr<ControllableEntity> protect_;     
     98
    9799                void setClosestTarget();
    98100                Pawn* closestTarget();
  • code/branches/campaignHS15/src/orxonox/controllers/CMakeLists.txt

    r10864 r10871  
    1717  CommonController.cc
    1818  ActionpointController.cc
     19  FlyingController.cc
     20  FightingController.cc
    1921)
  • code/branches/campaignHS15/src/orxonox/controllers/CommonController.cc

    r10869 r10871  
    3939#include "controllers/DroneController.h"
    4040
    41 //Today, on 25th of November, 2015, I hereby officially declare my code a huge pack of bullshit that I do not understand.
    42 //#Almost2KLines
    43 //#SpaghettiCode
    44 //#ISworeiWouldNever
    45 //#ItAllStartedSoWell
    46 
    4741namespace orxonox
    4842{
     
    5246    const float ROTATEFACTOR = 1.0f/0.02f;
    5347
    54     //Table of content:
    55     //Constructor, Destructor & tick
    56     //XML methods
    57     //World interaction
    58     //Helper methods
    59     //Flying methods
    60     //Fighting methods
    61     //Actionpoint methods
    62 
    63 
    64     //------------------------------------------------------------------------------
    65     //------------------------Constructor, Destructor & tick------------------------
    66     //------------------------------------------------------------------------------
    67    
     48 
    6849    CommonController::CommonController( Context* context ): Controller( context )
    6950    {
    7051        this->bFirstTick_ = true;
    71         this->tolerance_ = 50;
    72         this->stopLookingAtTarget();
    73         this->attackRange_ = 2500;
     52       
    7453        RegisterObject( CommonController );
    7554
     
    8564    }
    8665
    87     //------------------------------------------------------------------------------
    88     //----------------------------------XML methods---------------------------------
    89     //------------------------------------------------------------------------------
    90    
     66     
    9167    void CommonController::XMLPort( Element& xmlelement, XMLPort::Mode mode )
    9268    {
    9369        SUPER( CommonController, XMLPort, xmlelement, mode );
    94         XMLPortParam( CommonController, "formationMode", setFormationModeXML, getFormationModeXML,  xmlelement, mode );
    95     }
    96     void CommonController::setFormationModeXML( std::string val )
    97     {
    98         const std::string valUpper = getUppercase( val );
    99         FormationMode::Value value;
    100        
    101         if ( valUpper == "WALL" )
    102             value = FormationMode::WALL;
    103         else if ( valUpper == "FINGER4" )
    104             value = FormationMode::FINGER4;
    105         else if ( valUpper == "DIAMOND" )
    106             value = FormationMode::DIAMOND;
    107         else
    108             ThrowException( ParseError, std::string( "Attempting to set an unknown FormationMode: '" )+ val + "'." );
    109         this->setFormationMode( value );
    110     }
    111     std::string CommonController::getFormationModeXML()
    112     {
    113         switch ( this->formationMode_ )
    114         {
    115             case FormationMode::WALL:
    116             { return "WALL"; break; }
    117             case FormationMode::FINGER4:
    118             { return "FINGER4"; break; }
    119             case FormationMode::DIAMOND:
    120             { return "DIAMOND"; break; }
    121             default:
    122                 return "DIAMOND"; break;
    123         }
    124     }
    125     void CommonController::setFormationMode(FormationMode::Value val)
    126     {
    127         this->formationMode_ = val;
    128     }
    129     FormationMode::Value CommonController::getFormationMode() const
    130     {
    131         return this->formationMode_;
    132     }
    133    
    134     //------------------------------------------------------------------------------
    135     //-------------------------------World interaction------------------------------
    136     //------------------------------------------------------------------------------
    137 
     70    }
     71 
    13872    //"Virtual" methods
    13973    bool CommonController::setWingman ( CommonController* wingman )
     
    14175    bool CommonController::hasWingman()
    14276    { return true; }
    143     bool CommonController::hasTarget()
    144     {
    145         if ( this->target_ )
    146             return true;
    147         return false;
    148     }
    149     ControllableEntity* CommonController::getTarget()
    150     {
    151         return this->target_;
    152     }
    153  
    154 
    155     //------------------------------------------------------------------------------
    156     //--------------------------------Helper methods--------------------------------
    157     //------------------------------------------------------------------------------
    15877
    15978    float CommonController::randomInRange( float a, float b )
     
    300219        return name;
    301220    }
    302     float CommonController::squaredDistanceToTarget()  const
    303     {
    304         if ( !this->getControllableEntity()  )
    305             return 0;
    306         if ( !this->target_ || !this->getControllableEntity() )
    307             return ( this->getControllableEntity() ->getPosition() .squaredDistance( this->targetPosition_ ) );
    308         else
    309             return ( this->getControllableEntity() ->getPosition() .squaredDistance( this->positionOfTarget_ ) );
    310     }
    311     bool CommonController::isLookingAtTarget( float angle )
    312     {
    313         if ( !this->getControllableEntity()  || !this->target_ )
    314             return false;
    315         return this->isLooking(this->getControllableEntity(), this->getTarget(), angle);
    316     }
    317 
    318 
    319     //------------------------------------------------------------------------------
    320     //--------------------------------Flying methods--------------------------------
    321     //------------------------------------------------------------------------------
    322 
    323     void CommonController::stopMoving()
    324     {
    325         this->bHasTargetPosition_ = false;
    326     }
    327     void CommonController::stopLookingAtTarget()
    328     {
    329         this->bLookAtTarget_ = false;
    330     }
    331     void CommonController::startLookingAtTarget()
    332     {
    333         this->bLookAtTarget_ = true;
    334     }
    335     void CommonController::moveToPosition( const Vector3& target, float dt )
    336     {
    337         ControllableEntity* entity = this->getControllableEntity();
    338         Vector2 coord = get2DViewCoordinates
    339             ( entity->getPosition() ,
    340             entity->getOrientation()  * WorldEntity::FRONT,
    341             entity->getOrientation()  * WorldEntity::UP,
    342             target );
    343 
    344         float distance = ( target - this->getControllableEntity() ->getPosition() ).length();
    345         float rotateX = -clamp( coord.x * 10, -1.0f, 1.0f );
    346         float rotateY = clamp( coord.y * 10, -1.0f, 1.0f );
    347 
    348         if ( distance > this->tolerance_ )
    349         {
    350             this->getControllableEntity() ->rotateYaw( ROTATEFACTOR * rotateX * dt );
    351             this->getControllableEntity() ->rotatePitch( ROTATEFACTOR * rotateY * dt );
    352 
    353             if ( distance < 300 )
    354             {
    355                 if ( bHasTargetOrientation_ )
    356                 {
    357                     copyTargetOrientation( dt );
    358                 }
    359             }
    360             if (distance > this->tolerance_*1.5f || (rotateX > -0.01 && rotateX < 0.01 && rotateY > -0.01 && rotateY < 0.01))
    361                 this->getControllableEntity() ->moveFrontBack( SPEED * dt );
    362         }
    363         else
    364         {     
    365             bHasTargetPosition_ = false;
    366             bHasTargetOrientation_ = false;
    367         }
    368     }
    369     void CommonController::moveToTargetPosition(float dt)
    370     {
    371         this->moveToPosition (this->targetPosition_, dt);
    372     }
    373     void CommonController::copyOrientation( const Quaternion& orient, float dt )
    374     {
    375         //roll angle difference in radian
    376         float diff=orient.getRoll(false).valueRadians() -
    377                         ( this->getControllableEntity() ->getOrientation() .getRoll( false ).valueRadians() );
    378         while( diff>math::twoPi )diff-=math::twoPi;
    379         while( diff<-math::twoPi )diff+=math::twoPi;
    380         this->getControllableEntity() ->rotateRoll( diff*0.2f*ROTATEFACTOR * dt );
    381     }
    382     void CommonController::copyTargetOrientation( float dt )
    383     {
    384         if ( bHasTargetOrientation_ )
    385         {   
    386             copyOrientation( targetOrientation_, dt );
    387         }
    388     }
    389     void CommonController::lookAtTarget(float dt)
    390     {
    391         ControllableEntity* entity = this->getControllableEntity();
    392         if ( !entity )
    393             return;
    394         Vector2 coord = get2DViewCoordinates
    395             ( entity->getPosition() ,
    396             entity->getOrientation()  * WorldEntity::FRONT,
    397             entity->getOrientation()  * WorldEntity::UP,
    398             positionOfTarget_ );
    399 
    400         //rotates should be in range [-1,+1], clamp cuts off all that is not
    401         float rotateX = -clamp( coord.x * 10, -1.0f, 1.0f );
    402         float rotateY = clamp( coord.y * 10, -1.0f, 1.0f );
    403    
    404         //Yaw and Pitch are enough to start facing the target
    405         this->getControllableEntity() ->rotateYaw( ROTATEFACTOR * rotateX * dt );
    406         this->getControllableEntity() ->rotatePitch( ROTATEFACTOR * rotateY * dt );
    407     }
    408     void CommonController::setTargetPosition( const Vector3& target )
    409     {
    410         this->targetPosition_ = target;
    411         this->bHasTargetPosition_ = true;
    412     }
    413 
    414     void CommonController::setTargetOrientation( const Quaternion& orient )
    415     {
    416         this->targetOrientation_=orient;
    417         this->bHasTargetOrientation_=true;
    418     }
    419 
    420     void CommonController::setTargetOrientation( ControllableEntity* target )
    421     {
    422         if ( target )
    423             setTargetOrientation( target->getOrientation() );
    424     }
    425 
    426     //------------------------------------------------------------------------------
    427     //-------------------------------Fighting methods-------------------------------
    428     //------------------------------------------------------------------------------
    429 
    430     void CommonController::setTarget( ControllableEntity* target )
    431     {
    432         this->target_ = target;       
    433         if ( this->target_ )
    434         {
    435             this->setPositionOfTarget( target_->getWorldPosition() );
    436         }
    437     }
    438     void CommonController::setPositionOfTarget( const Vector3& target )
    439     {
    440         this->positionOfTarget_ = target;
    441         this->bHasPositionOfTarget_ = true;
    442     }
    443     void CommonController::setOrientationOfTarget( const Quaternion& orient )
    444     {
    445         this->orientationOfTarget_=orient;
    446         this->bHasOrientationOfTarget_=true;
    447     }
    448    
    449     void CommonController::maneuver()
    450     {
    451         maneuverCounter_++;
    452         if (maneuverCounter_ > 5)
    453             maneuverCounter_ = 0;
    454 
    455         if ( !this->target_ || !this->getControllableEntity())
    456             return;
    457        
    458         Vector3 thisPosition = this->getControllableEntity()->getWorldPosition();
    459         this->setPositionOfTarget( getPredictedPosition(
    460             thisPosition,
    461             hardcoded_projectile_speed,
    462             this->target_->getWorldPosition() ,
    463             this->target_->getVelocity() 
    464             )  );
    465         this->setOrientationOfTarget( this->target_->getOrientation() );
    466 
    467         Vector3 diffVector = this->positionOfTarget_ - thisPosition;
    468         float diffLength = diffVector.length();
    469         Vector3 diffUnit = diffVector/diffLength;
    470 
    471         bool bTargetIsLookingAtThis = this->isLooking ( this->target_, getControllableEntity(), math::pi/10.0f );
    472        
    473         //too far? well, come closer then
    474         if ( diffLength > this->attackRange_ )
    475         {
    476             this->setTargetPosition( this->positionOfTarget_ );
    477         }
    478         //too close? How do u expect to dodge anything? Just attack!
    479         else if ( diffLength < 500 )
    480         {   
    481             //at this point, just look and shoot
    482             if ( diffLength < 250 )
    483             {
    484                 this->stopMoving();
    485                 this->startLookingAtTarget();
    486             }
    487             else
    488             {
    489                 this->setTargetPosition( this->positionOfTarget_ );
    490             }
    491         }
    492         //Good distance? Check if target looks at us. It doesn't? Go hunt!
    493         else if ( !bTargetIsLookingAtThis )
    494         {
    495             this->setTargetPosition( this->positionOfTarget_ );
    496         }
    497         //That's unfortunate, he is looking and probably shooting... try to dodge what we can... 
    498         else
    499         {   
    500             if (maneuverCounter_ == 0)
    501             {
    502                 this->setTargetPosition( this->positionOfTarget_ );   
    503                 return;
    504             }
    505             dodge( thisPosition, diffUnit );
    506         }
    507     }
    508    
    509     void CommonController::dodge(Vector3& thisPosition, Vector3& diffUnit)
    510     {
    511         float factorX = 0, factorY = 0, factorZ = 0;
    512         float rand = randomInRange (0, 1);
    513    
    514         if (rand <= 0.5)
    515         { factorX = 1; }
    516         else
    517         { factorX = -1; }
    518         rand = randomInRange (0, 1);
    519         if (rand <= 0.5)
    520         { factorY = 1; }
    521         else
    522         { factorY = -1; }
    523         rand = randomInRange (0, 1);
    524         if (rand <= 0.5)
    525         { factorZ = 1; }
    526         else
    527         { factorZ = -1; }
    528 
    529         Vector3 target = ( diffUnit )* 8000.0f;
    530         Vector3* randVector = new Vector3(
    531             factorX * randomInRange( 10000, 40000 ),
    532             factorY * randomInRange( 10000, 40000 ),
    533             factorZ * randomInRange( 10000, 40000 )
    534         );
    535         Vector3 projection = randVector->dotProduct( diffUnit )* diffUnit;
    536         *randVector -= projection;
    537         target += *randVector;
    538         this->setTargetPosition( thisPosition + target );
    539     }
    540     bool CommonController::canFire()
    541     {
    542         //no target? why fire?
    543         if ( !this->target_ )
    544             return false;
    545 
    546         Vector3 newPositionOfTarget = getPredictedPosition( this->getControllableEntity() ->getWorldPosition() ,
    547             hardcoded_projectile_speed, this->target_->getWorldPosition() , this->target_->getVelocity() );
    548         if ( newPositionOfTarget != Vector3::ZERO )
    549         {
    550             this->setPositionOfTarget( newPositionOfTarget );
    551         }
    552 
    553         float squaredDistance = squaredDistanceToTarget();
    554 
    555         if ( squaredDistance < this->attackRange_*this->attackRange_ && this->isLookingAtTarget( math::pi / 20.0f))
    556         {
    557             return true;
    558         }
    559         else
    560         {
    561             return false;
    562         }
    563     }
    564     void CommonController::doFire()
    565     {
    566         if ( !this->target_ || !this->getControllableEntity() )
    567         {
    568             return;
    569         }
    570      
    571         Pawn* pawn = orxonox_cast<Pawn*>( this->getControllableEntity() );
    572 
    573         if ( pawn )
    574             pawn->setAimPosition( this->positionOfTarget_ );
    575         this->getControllableEntity() ->fire( 0 );
    576     }
    577 
    578 
    579    
    580     void CommonController::boostControl()
    581     {
    582         SpaceShip* ship = orxonox_cast<SpaceShip*>(this->getControllableEntity());
    583         if(ship == NULL) return;
    584         if(ship->getBoostPower()*1.5f > ship->getInitialBoostPower() ) //upper limit ->boost
    585         {
    586 
    587             this->getControllableEntity()->boost(true);
    588         }
    589         else if(ship->getBoostPower()*4.0f < ship->getInitialBoostPower()) //lower limit ->do not boost
    590         {
    591            this->getControllableEntity()->boost(false);
    592         }
    593     }
     221 
     222
    594223}
  • code/branches/campaignHS15/src/orxonox/controllers/CommonController.h

    r10869 r10871  
    4545namespace orxonox
    4646{
    47 
    48     namespace FormationMode
    49     {
    50         enum Value
    51         {
    52             FINGER4, DIAMOND, WALL
    53         };
    54     }
    55 
    56 
    57    
    58  
    59 
    6047    class _OrxonoxExport CommonController : public Controller, public Tickable
    6148    {
     
    7057            virtual void tick(float dt);
    7158
    72             //----[XML methods]----
    73                 virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
     59            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
    7460               
    75                 void setFormationModeXML(std::string val);
    76                 std::string getFormationModeXML();
    77                 void setFormationMode(FormationMode::Value val);
    78                 FormationMode::Value getFormationMode() const;
    79 
    80                
    81 
    82             //----[/XML methods]----
    83 
    84             //----[Interaction with other Controllers]----
    85                 virtual bool setWingman(CommonController* wingman);
    86                 virtual bool hasWingman();       
    87                
    88                 bool hasTarget();
    89                 ControllableEntity* getTarget();
    90                      
    91              //----[/Interaction with other Controllers]----
    92 
    93             //----[Helper methods]----
    94                 float randomInRange(float a, float b);
    95                 static float distance(ControllableEntity* entity1, ControllableEntity* entity2);
    96                 static bool sameTeam (ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gt);
    97                 static bool isLooking( ControllableEntity* entityThatLooks, ControllableEntity* entityBeingLookedAt, float angle ) ;
    98                 static std::string getName( Pawn* entity ) ;
    99 
    100                 float squaredDistanceToTarget() const;
    101                 bool isLookingAtTarget(float angle);
    102             //----[/Helper methods]----
    103 
    104  
    105         protected:
    106             //----[Flying methods]----
    107                 void stopMoving();
    108                 void stopLookingAtTarget();
    109                 void startLookingAtTarget();
    110 
    111                 void moveToPosition(const Vector3& target, float dt);
    112                 void moveToTargetPosition(float dt);
    113 
    114                 void copyOrientation(const Quaternion& orient, float dt);
    115                 void copyTargetOrientation(float dt);
    116 
    117                 void lookAtTarget(float dt);
    118 
    119                 void setTargetPosition(const Vector3& target);
    120                 void setTargetOrientation(const Quaternion& orient);
    121                 void setTargetOrientation(ControllableEntity* target);
    122                 virtual void boostControl();
    12361
    12462
    125             //----[/Flying methods]----
    126            
    127             //----[Fighting methods]----
    128                 void setTarget(ControllableEntity* target);
    129 
    130                 void setPositionOfTarget(const Vector3& target);
    131                 void setOrientationOfTarget(const Quaternion& orient);
     63            virtual bool setWingman(CommonController* wingman);
     64            virtual bool hasWingman();       
    13265
    13366
    134                 void maneuver();
    135                 void dodge(Vector3& thisPosition, Vector3& diffUnit);
    136                 bool canFire();
    137                 void doFire();
     67            float randomInRange(float a, float b);
     68            static float distance(ControllableEntity* entity1, ControllableEntity* entity2);
     69            static bool sameTeam (ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gt);
     70            static bool isLooking( ControllableEntity* entityThatLooks, ControllableEntity* entityBeingLookedAt, float angle ) ;
     71            static std::string getName( Pawn* entity ) ;
    13872
    139             //----[/Fighting methods]----           
     73        protected:
     74           
     75            bool bFirstTick_;
    14076           
    141             //----[where-to-fly information]----
    142                
    143                 bool bHasTargetPosition_;
    144                 Vector3 targetPosition_;
    145                 bool bHasTargetOrientation_;
    146                 Quaternion targetOrientation_;
    147                
    148             //----[/where-to-fly information]----
    149             //----[protect information]----
    150                 WeakPtr<ControllableEntity> protect_;
    151             //----[/protect information]----
    152             //----[who-to-kill information]----
    153                 WeakPtr<ControllableEntity> target_;
    154                
    155                 bool bHasPositionOfTarget_;
    156                 Vector3 positionOfTarget_;
    157                 bool bHasOrientationOfTarget_;
    158                 Quaternion orientationOfTarget_;
    159             //----[/who-to-kill information]----
    160 
    161 
    162             //----["Private" variables]----
    163                 FormationMode::Value formationMode_;
    164 
    165                 int attackRange_;
    166                 bool bLookAtTarget_;       
    167                 bool bShooting_;
    168                 int maneuverCounter_;
    169                 int tolerance_;
    170                 bool bFirstTick_;
    171            
    172             //----[/"Private" variables]----   
    17377           
    17478    };
Note: See TracChangeset for help on using the changeset viewer.