Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 26, 2015, 9:08:11 AM (8 years ago)
Author:
gania
Message:

split up some code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/campaignHS15/src/orxonox/controllers/CommonController.cc

    r10863 r10864  
    6868    CommonController::CommonController( Context* context ): Controller( context )
    6969    {
    70         this->squaredaccuracy_ = 2500;
    7170        this->bFirstTick_ = true;
    7271        this->tolerance_ = 50;
    73         this->action_ = Action::NONE;
    7472        this->stopLookingAtTarget();
    7573        this->attackRange_ = 2500;
    76         this->bInLoop_ = false;
    77         this->bLoop_ = false;
    78         this->bEndLoop_ = false;
    79         this->parsedActionpoints_.clear();
    8074        RegisterObject( CommonController );
    81         this->bTakenOver_ = false;
     75
    8276    }
    8377    CommonController::~CommonController()
    8478    {
    85         parsedActionpoints_.clear();
    86         actionpoints_.clear();
     79       
    8780    }
    8881    void CommonController::tick(float dt)
    8982    {
    90         if (this->bHasTargetPosition_)
    91         {
    92             this->moveToTargetPosition(dt);
    93         }
    94         else if (this->bLookAtTarget_)
    95         {
    96             this->lookAtTarget(dt);
    97         }
    98         if (bShooting_)
    99         {
    100             this->doFire();
    101         }
    102         if (this->bFirstTick_)
    103         {
    104             std::reverse(parsedActionpoints_.begin(), parsedActionpoints_.end());
    105             std::reverse(actionpoints_.begin(), actionpoints_.end());
    106             if (this->parsedActionpoints_.empty())
    107             {
    108                 this->action_ = Action::FIGHTALL;
    109             }
    110         }
    111         if (this->bFirstTick_)
    112             this->bFirstTick_ = false;
     83       
    11384        SUPER(CommonController, tick, dt);
    11485    }
     
    12293        SUPER( CommonController, XMLPort, xmlelement, mode );
    12394        XMLPortParam( CommonController, "formationMode", setFormationModeXML, getFormationModeXML,  xmlelement, mode );
    124         XMLPortObject(CommonController, WorldEntity, "actionpoints", addActionpoint, getActionpoint,  xmlelement, mode);
    12595    }
    12696    void CommonController::setFormationModeXML( std::string val )
     
    169139        return this->rank_;
    170140    }
    171     void CommonController::addActionpoint(WorldEntity* actionpoint)
    172     {
    173         std::string actionName;
    174         Vector3 position;
    175         std::string targetName;
    176         bool inLoop = false;
    177         Point p;
    178         if (static_cast<Actionpoint*> (actionpoint))
    179         {
    180             Actionpoint* ap = static_cast<Actionpoint*> (actionpoint);
    181             actionName = ap->getActionXML();
    182             targetName = ap->getName();
    183             position = ap->getWorldPosition();
    184 
    185             if (this->bEndLoop_)
    186             {
    187                 this->bInLoop_ = false;
    188             }
    189             if (!this->bInLoop_ && ap->getLoopStart())
    190             {
    191                 this->bInLoop_ = true;
    192             }
    193             if (this->bInLoop_ && ap->getLoopEnd())
    194             {
    195                 this->bEndLoop_ = true;
    196             }
    197             inLoop = this->bInLoop_;
    198 
    199             Action::Value value;
    200            
    201             if ( actionName == "FIGHT" )
    202             { value = Action::FIGHT; }
    203             else if ( actionName == "FLY" )
    204             { value = Action::FLY; }
    205             else if ( actionName == "PROTECT" )
    206             { value = Action::PROTECT; }
    207             else if ( actionName == "NONE" )
    208             { value = Action::NONE; }
    209             else if ( actionName == "FIGHTALL" )
    210             { value = Action::FIGHTALL; }
    211             else if ( actionName == "ATTACK" )
    212             { value = Action::ATTACK; }
    213             else
    214                 ThrowException( ParseError, std::string( "Attempting to set an unknown Action: '" )+ actionName + "'." );
    215             p.action = value; p.name = targetName; p.position = position; p.inLoop = inLoop;
    216         }
    217         else
    218         {
    219             p.action = Action::FLY; p.name = ""; p.position = actionpoint->getWorldPosition(); p.inLoop = inLoop;
    220         }
    221             parsedActionpoints_.push_back(p);
    222             this->actionpoints_.push_back(actionpoint);
    223     }
    224     WorldEntity* CommonController::getActionpoint(unsigned int index) const
    225     {
    226         if (index < this->actionpoints_.size())
    227             return this->actionpoints_[index];
    228         else
    229             return 0;
    230     }
    231 
     141   
    232142    //------------------------------------------------------------------------------
    233143    //-------------------------------World interaction------------------------------
     
    249159        return this->target_;
    250160    }
    251     Action::Value CommonController::getAction ()
    252     {
    253         return this->action_;
    254     }
    255     std::string CommonController::getActionName()
    256     {
    257         switch ( this->action_ )
    258         {
    259             case Action::FIGHT:
    260             { return "FIGHT"; break; }
    261             case Action::FLY:
    262             { return "FLY"; break; }
    263             case Action::PROTECT:
    264             { return "PROTECT"; break; }
    265             case Action::NONE:
    266             { return "NONE"; break; }
    267             case Action::FIGHTALL:
    268             { return "FIGHTALL"; break; }
    269             case Action::ATTACK:
    270             { return "ATTACK"; break; }
    271             default:
    272                 return "NONE";
    273                 break;
    274         }
    275     }
    276     void CommonController::setAction (Action::Value action)
    277     {
    278         this->action_ = action;
    279     }
    280     void CommonController::setAction (Action::Value action, ControllableEntity* target)
    281     {
    282         this->action_ = action;
    283         if (action == Action::FIGHT || action == Action::FIGHTALL || action == Action::ATTACK)
    284         {   
    285             if (target)
    286                 this->setTarget (target);
    287         }
    288         else if (action == Action::PROTECT)
    289         {
    290             if (target)
    291                 this->setProtect (target);
    292         }
    293     }
    294     void CommonController::setAction (Action::Value action, const Vector3& target)
    295     {
    296         this->action_ = action;
    297         if (action == Action::FLY)
    298         {
    299             this->setTargetPosition (target);
    300         }
    301     }
    302     void CommonController::setAction (Action::Value action, const Vector3& target,  const Quaternion& orient )
    303     {
    304         this->action_ = action;
    305         if (action == Action::FLY)
    306         {
    307             this->setTargetPosition (target);
    308             this->setTargetOrientation (orient);
    309         } 
    310     }
     161 
    311162
    312163    //------------------------------------------------------------------------------
     
    603454        this->bHasOrientationOfTarget_=true;
    604455    }
    605     void CommonController::setProtect (ControllableEntity* protect)
    606     {
    607         this->protect_ = protect;
    608     }
    609     ControllableEntity* CommonController::getProtect ()
    610     {
    611         return this->protect_;
    612     }
     456   
    613457    void CommonController::maneuver()
    614458    {
     
    739583        this->getControllableEntity() ->fire( 0 );
    740584    }
    741     void CommonController::setClosestTarget()
    742     {
    743         this->setTarget (static_cast<ControllableEntity*>( closestTarget() ) );
    744     }
    745     Pawn* CommonController::closestTarget()
    746     {
    747         if (!this->getControllableEntity())
    748             return 0;
    749 
    750         Pawn* closestTarget = 0;
    751         float minDistance =  std::numeric_limits<float>::infinity();
    752         Gametype* gt = this->getGametype();
    753         for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
    754         {
    755             if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) )
    756                 continue;
    757 
    758             float distance = CommonController::distance (*itP, this->getControllableEntity());
    759             if (distance < minDistance)
    760             {
    761                 closestTarget = *itP;
    762                 minDistance = distance;
    763             }
    764         }
    765         if (closestTarget)
    766         {
    767            return closestTarget;
    768         }
    769         return 0; 
    770     }
    771     void CommonController::startAttackingEnemiesThatAreClose()
    772     {
    773         if (this->action_ != Action::FIGHT && this->action_ != Action::FIGHTALL)
    774         {
    775             if ( (this->target_ && this->distance (this->getControllableEntity(), this->target_) > this->attackRange_)
    776                 || !this->target_ )
    777             {
    778                 Pawn* newTarget = this->closestTarget();
    779                 if ( newTarget &&
    780                     this->distance (this->getControllableEntity(), static_cast<ControllableEntity*>(newTarget))
    781                         <= this->attackRange_ )
    782                 {
    783                     Point p = { Action::FIGHT, this->getName(newTarget), Vector3::ZERO, false };
    784                     this->parsedActionpoints_.push_back(p);
    785                     this->executeActionpoint();
    786                 }
    787             }
    788         }
    789     }
    790 
    791     //------------------------------------------------------------------------------
    792     //------------------------------Actionpoint methods-----------------------------
    793     //------------------------------------------------------------------------------
    794 
    795     //POST: this starts doing what was asked by the last element of parsedActionpoints_,
    796     //if last element was failed to be parsed, next element will be executed.
    797     void CommonController::executeActionpoint()
    798     {
    799         if (this->bLoop_)
    800         {
    801             if (!this->loopActionpoints_.empty())
    802             {
    803                 this->action_ = this->loopActionpoints_.back().action;
    804                 switch ( this->action_ )
    805                 {
    806                     case Action::FIGHT:
    807                     {
    808                         std::string targetName = this->loopActionpoints_.back().name;
    809                         if (targetName == "")
    810                         {
    811                             break;
    812                         }
    813                         for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
    814                         {
    815                             if (CommonController::getName(*itP) == targetName)
    816                             {
    817                                 this->setTarget (static_cast<ControllableEntity*>(*itP));
    818                             }
    819                         }         
    820                         break;
    821                     }
    822                     case Action::FLY:
    823                     {
    824                         this->setTargetPosition( this->loopActionpoints_.back().position );
    825                         if (this->squaredDistanceToTarget() <= this->squaredaccuracy_)
    826                         {
    827                             this->nextActionpoint();
    828                             this->executeActionpoint();
    829                         }
    830                         break;
    831                     }
    832                     case Action::PROTECT:
    833                     {
    834                         std::string protectName = this->loopActionpoints_.back().name;
    835                         if (protectName == "reservedKeyword:human")
    836                         {
    837                             for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
    838                             {
    839                                 if (orxonox_cast<ControllableEntity*>(*itP) && ((*itP)->getController()) && orxonox_cast <NewHumanController*> ((*itP)->getController()))
    840                                 {
    841                                     this->setProtect (static_cast<ControllableEntity*>(*itP));
    842                                 }
    843                             }
    844                         }
    845                         else
    846                         {
    847                             for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
    848                             {
    849                                 if (CommonController::getName(*itP) == protectName)
    850                                 {
    851                                     this->setProtect (static_cast<ControllableEntity*>(*itP));
    852                                 }
    853                             }                           
    854                         }
    855                         if (!this->getProtect())
    856                         {
    857                             this->nextActionpoint();
    858                             this->executeActionpoint();
    859                         }
    860                         break;
    861                     }
    862                     case Action::NONE:
    863                     {
    864                         break;
    865                     }
    866                     case Action::FIGHTALL:
    867                     {
    868                         break;
    869                     }
    870                     case Action::ATTACK:
    871                     {
    872                         std::string targetName = this->loopActionpoints_.back().name;
    873 
    874                         for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
    875                         {
    876                             if (CommonController::getName(*itP) == targetName)
    877                             {
    878                                 this->setTarget (static_cast<ControllableEntity*>(*itP));
    879                             }
    880                         }
    881                         if (!this->hasTarget())
    882                         {
    883                             this->nextActionpoint();
    884                             this->executeActionpoint();
    885                         }
    886                         break;
    887                     }
    888                     default:
    889                         break;
    890                 }
    891             }
    892             else
    893             {
    894                 this->bLoop_ = false;
    895             }
    896         }
    897         else
    898         {
    899             if (!this->parsedActionpoints_.empty())
    900             {
    901                 if (this->parsedActionpoints_.back().inLoop)
    902                 {
    903                     //MOVES all points that are in loop to a loop vector
    904                     this->fillLoop();
    905                     this->bLoop_ = true;
    906                     executeActionpoint();
    907                     return;
    908                 }
    909                 this->action_ = this->parsedActionpoints_.back().action;
    910                 switch ( this->action_ )
    911                 {
    912                     case Action::FIGHT:
    913                     {
    914                         std::string targetName = this->parsedActionpoints_.back().name;
    915                         if (targetName == "")
    916                         {
    917                             break;
    918                         }
    919                         for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
    920                         {
    921                             if (CommonController::getName(*itP) == targetName)
    922                             {
    923                                 this->setTarget (static_cast<ControllableEntity*>(*itP));
    924                             }
    925                         }         
    926                         break;
    927                     }
    928                     case Action::FLY:
    929                     {
    930                         this->setTargetPosition( this->parsedActionpoints_.back().position );
    931                         if (this->squaredDistanceToTarget() <= this->squaredaccuracy_)
    932                         {
    933                             this->nextActionpoint();
    934                             this->executeActionpoint();
    935                         }
    936                         break;
    937                     }
    938                     case Action::PROTECT:
    939                     {
    940                        
    941                         std::string protectName = this->parsedActionpoints_.back().name;
    942                         if (protectName == "reservedKeyword:human")
    943                         {
    944                             for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
    945                             {
    946                                 if (orxonox_cast<ControllableEntity*>(*itP) && ((*itP)->getController()) && orxonox_cast <NewHumanController*> ((*itP)->getController()))
    947                                 {
    948                                     this->setProtect (static_cast<ControllableEntity*>(*itP));
    949                                 }
    950                             }
    951                         }
    952                         else
    953                         {
    954                             for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
    955                             {
    956                                 if (CommonController::getName(*itP) == protectName)
    957                                 {
    958                                     this->setProtect (static_cast<ControllableEntity*>(*itP));
    959                                 }
    960                             }                           
    961                         }
    962                         if (!this->getProtect())
    963                         {
    964                             this->nextActionpoint();
    965                             this->executeActionpoint();
    966                         }
    967                         break;
    968                     }
    969                     case Action::NONE:
    970                     {
    971                         break;
    972                     }
    973                     case Action::FIGHTALL:
    974                     {
    975                         break;
    976                     }
    977                     case Action::ATTACK:
    978                     {
    979                         std::string targetName = this->parsedActionpoints_.back().name;
    980 
    981                         for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
    982                         {
    983                             if (CommonController::getName(*itP) == targetName)
    984                             {
    985                                 this->setTarget (static_cast<ControllableEntity*>(*itP));
    986                             }
    987                         }
    988                         if (!this->hasTarget())
    989                         {
    990                             this->nextActionpoint();
    991                             this->executeActionpoint();
    992                         }
    993                         break;
    994                     }
    995                     default:
    996                         break;
    997                 }
    998             }
    999             else
    1000             {
    1001                 this->setTarget(0);
    1002                 this->setTargetPosition(this->getControllableEntity()->getWorldPosition());
    1003                 this->action_ = Action::NONE;
    1004             }
    1005         }
    1006 
    1007     }
    1008     void CommonController::stayNearProtect()
    1009     {
    1010         Vector3* targetRelativePosition;
    1011         targetRelativePosition = new Vector3 (0, 300, 300); 
    1012         Vector3 targetAbsolutePosition = ((this->getProtect()->getWorldPosition()) +
    1013             (this->getProtect()->getWorldOrientation()* (*targetRelativePosition)));
    1014         this->setTargetPosition(targetAbsolutePosition);
    1015     }
    1016     void CommonController::nextActionpoint()
    1017     {
    1018         if (!this || !this->getControllableEntity())
    1019             return;
    1020         if (this->bLoop_)
    1021         {
    1022             if (!this->loopActionpoints_.empty())
    1023             {
    1024                 this->moveBackToTop();
    1025             }
    1026         }
    1027         else
    1028         {
    1029             if (!this->parsedActionpoints_.empty())
    1030             {
    1031                 this->parsedActionpoints_.pop_back();
    1032             }           
    1033         }
    1034         this->setAction(Action::NONE);
    1035     }
    1036     void CommonController::moveBackToTop()
    1037     {
    1038         Point temp = loopActionpoints_.back();
    1039         loopActionpoints_.pop_back();
    1040         std::reverse (loopActionpoints_.begin(), loopActionpoints_.end());
    1041         loopActionpoints_.push_back(temp);
    1042         std::reverse (loopActionpoints_.begin(), loopActionpoints_.end());
    1043     }
    1044     void CommonController::fillLoop()
    1045     {
    1046         loopActionpoints_.clear();
    1047         fillLoopReversed();
    1048         std::reverse (loopActionpoints_.begin(), loopActionpoints_.end());
    1049     }
    1050     void CommonController::fillLoopReversed()
    1051     {
    1052         if (parsedActionpoints_.back().inLoop)
    1053         {
    1054             loopActionpoints_.push_back(parsedActionpoints_.back());
    1055             parsedActionpoints_.pop_back();
    1056         }
    1057         if (parsedActionpoints_.back().inLoop)
    1058         {
    1059             fillLoopReversed();
    1060         }
    1061     }
    1062     void CommonController::action()
    1063     {
    1064         if (!this || !this->getControllableEntity())
    1065             return;
    1066         // orxout (internal_error) << "Size of actions is " << this->parsedActionpoints_.size() << endl;
    1067         this->startAttackingEnemiesThatAreClose();
    1068         //No action -> pop one from stack
    1069         if (this->action_ == Action::NONE || this->bTakenOver_)
    1070         {
    1071             if (this->parsedActionpoints_.empty() && this->loopActionpoints_.empty())
    1072             {
    1073                 Point p = { Action::FIGHTALL, "", Vector3::ZERO, false };
    1074                 this->parsedActionpoints_.push_back (p);
    1075             }
    1076             this->executeActionpoint();
    1077             this->bTakenOver_ = false;
    1078         }
    1079         //Action fightall -> fight till nobody alive
    1080         if (this->action_ == Action::FIGHTALL)
    1081         {
    1082             if (!this->hasTarget())
    1083             {
    1084                 //----find a target----
    1085                 ControllableEntity* newTarget = this->closestTarget();   
    1086                 if (newTarget)
    1087                 {
    1088                     this->setAction (Action::FIGHTALL, newTarget);
    1089                 }
    1090                 else
    1091                 {
    1092                     this->nextActionpoint();
    1093                     return;
    1094                 }
    1095             }
    1096             else if (this->hasTarget())
    1097             {
    1098 
    1099             }
    1100         }
    1101         //Action fight -> fight as long as enemies in range
    1102         else if (this->action_ == Action::FIGHT)
    1103         {
    1104             if (!this->hasTarget())
    1105             {
    1106                 //----find a target----
    1107                 ControllableEntity* newTarget = this->closestTarget();   
    1108                 if (newTarget &&
    1109                         CommonController::distance (this->getControllableEntity(), newTarget) < this->attackRange_)
    1110                 {
    1111                     this->setAction (Action::FIGHT, newTarget);
    1112                 }
    1113                 else
    1114                 {
    1115                     this->nextActionpoint();
    1116                     return;
    1117                 }
    1118             }
    1119             else if (this->hasTarget())
    1120             {
    1121                 //----fly in formation if far enough----
    1122                 Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();         
    1123                    
    1124                 if (diffVector.length() > this->attackRange_)
    1125                 {
    1126                     ControllableEntity* newTarget = this->closestTarget();
    1127                    
    1128                     if (newTarget &&
    1129                         CommonController::distance (this->getControllableEntity(), newTarget) < this->attackRange_)
    1130                     {
    1131                         this->setAction (Action::FIGHT, newTarget);
    1132                     }
    1133                     else
    1134                     {
    1135                         this->nextActionpoint();
    1136                         return;
    1137                     }
    1138                 }
    1139             }
    1140         }
    1141         else if (this->action_ == Action::FLY)
    1142         {
    1143             if (this->squaredDistanceToTarget() <= this->squaredaccuracy_)
    1144             {
    1145                 this->nextActionpoint();   
    1146                 return;
    1147             }
    1148         }
    1149         else if (this->action_ == Action::PROTECT)
    1150         {
    1151             if (!this->getProtect())
    1152             {
    1153                 this->nextActionpoint();
    1154                 return;
    1155             }
    1156             this->stayNearProtect();
    1157         }
    1158         else if (this->action_ == Action::ATTACK)
    1159         {   
    1160             if (!this->hasTarget())
    1161             {
    1162                 this->nextActionpoint();
    1163                 return;
    1164             }
    1165         }
    1166         if (this->hasTarget())
    1167         {
    1168             //----choose where to go----
    1169             this->maneuver();
    1170             //----fire if you can----
    1171             this->bShooting_ = this->canFire();               
    1172         }
    1173     }
    1174     void CommonController::takeActionpoints (std::vector<Point > vector, std::vector<Point > loop, bool b)
    1175     {
    1176       this->parsedActionpoints_ = vector;
    1177       this->loopActionpoints_ = loop;
    1178       this->bLoop_ = this->bLoop_;
    1179       this->bTakenOver_ = true;
    1180       // orxout(internal_error) << "Top action is " << this->parsedActionpoints_.back().action << endl;
    1181     }
     585
     586
     587   
    1182588    void CommonController::boostControl()
    1183589    {
Note: See TracChangeset for help on using the changeset viewer.