Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10177 in orxonox.OLD


Ignore:
Timestamp:
Jan 3, 2007, 6:39:09 PM (17 years ago)
Author:
tfahrni
Message:
 
Location:
branches/ai/src/ai
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/ai/src/ai/ai_module.h

    r10158 r10177  
    1010   virtual ~AIModule(){}
    1111   virtual void process(float dt){}
    12        
     12
    1313   void setDifficulty(int newDifficulty);
    1414        inline void setEnemyList(std::vector<WorldEntity*>* enemyList){this->enemyList=enemyList;}
    1515        inline Vector getPosition(){return myWorldEntity->getAbsCoor();}
    16         inline Vector getMovement(){return this->movement;}
     16        inline Vector getMovement(){return movement;}
    1717        inline void setDestination(Vector destination){this->destination=destination;}
    1818        inline void setDestinationMovement(Vector destinationMovement){this->destinationMovement=destinationMovement;}
    19        
     19        inline void setWeight(int weight){this->weight=weight;}
     20        inline void setTarget(WorldEntity* target){this->target=target;}
     21
    2022 protected:
    2123   NPC2* myNPC;
    2224        WorldEntity* myWorldEntity;
    2325        std::vector<WorldEntity*>* enemyList;
     26
    2427        Vector destination;
    2528        Vector destinationMovement;
    2629        Vector movement;
     30
     31        float weight;
     32        float speedMax;
     33
     34        WorldEntity* target;
    2735};
    2836
  • branches/ai/src/ai/ai_swarm.cc

    r10158 r10177  
    1818#include <stdio.h>
    1919
     20AISwarm::AISwarm()
     21{
     22        tickCount=0;
     23        randomFreq=100;
     24}
     25
     26
    2027void AISwarm::process(float dt)
    2128{
     29        if(tickCount>=randomFreq && movement.len()<60){
     30                tickCount=0;
     31                int x = (rand()%141)+10;                                //10-150
     32                int z = (rand()%241)-120;                       //-120-120
     33                randomVector=Vector(x,0,z);
     34
     35                std::cout << "change to: ||" << randomVector.x << "   ==" << randomVector.z << "\n";
     36        }
     37        tickCount++;
     38
     39
    2240        std::set<AIModule*>::iterator it;
    2341        Vector swarmPosition=this->getPosition();
    2442        //std::cout << swarmPosition.x << " " << swarmPosition.z << "\n";
    25        
     43
    2644        float aMax=70.0f;
    2745        float vMax=1000.0f;
    28        
    29         Vector correction=(destination-swarmPosition)-movement;
     46
     47        Vector correction=(destination+randomVector-swarmPosition)-movement;
    3048        correction.y=0;
    31        
     49
    3250        float correctionLen=correction.len();
    3351        if(correctionLen>aMax*dt)correction=correction/correctionLen*aMax*dt;
    34        
     52
    3553        //std::cout << angleRad(correction,movement) << "\n";
    3654        movement+=correction;
    37        
     55
    3856        float movementLen=movement.len();
    3957        if(movementLen>vMax)movement=movement/movementLen*vMax;
    40        
    41        
     58
     59
    4260        for (it= swarmMembers.begin(); it!= swarmMembers.end(); it++ ){
    4361                (*it)->setDestination(swarmPosition);
     
    4866}
    4967
     68
    5069void AISwarm::addAI(AIModule* aiModule)
    5170{
    5271        swarmMembers.insert(aiModule);
    5372}
     73
    5474
    5575void AISwarm::removeAI(AIModule* aiModule)
     
    5979        delete (*it);
    6080}
     81
    6182
    6283Vector AISwarm::getPosition()
  • branches/ai/src/ai/ai_swarm.h

    r10158 r10177  
    88class AISwarm{
    99        public:
    10                 AISwarm(){}
     10                AISwarm();
    1111                ~AISwarm(){}
    1212                void process(float dt);
     
    2222                std::vector<WorldEntity*>* enemyList;
    2323                std::set<AIModule*> swarmMembers;
     24                int tickCount;
     25                int randomFreq;
     26                Vector randomVector;
    2427};
    2528
  • branches/ai/src/ai/ai_team.cc

    r10158 r10177  
    1919void AITeam::process(float dt)
    2020{
    21         //int x;
    22         //int z;
    23         //Vector random;
    24        
    2521        std::map<int,AISwarm*>::iterator it;
    2622        for (it= swarms.begin(); it!= swarms.end(); it++ ){
    27        
    28                 //x = 20*(rand()%10 + 1)-100;
    29                 //z = 20*(rand()%10 + 1)-100;
    30                 //random=Vector(x,0,z);
    31                
     23
    3224                it->second->setEnemyList(enemyList);
    3325                if(enemyList->size()>0)
     
    3628        }
    3729}
     30
    3831
    3932void AITeam::addAI(int swarmNumber, AIModule* aiModule)
     
    4538        p.first->second->addAI(aiModule);
    4639}
     40
    4741
    4842void AITeam::removeAI(int swarmNumber, AIModule* aiModule)
  • branches/ai/src/ai/ai_team.h

    r10138 r10177  
    88 public:
    99        ~AITeam(){}
    10         AITeam(){}
    11         std::vector<WorldEntity*>* getEnemyList();
    12    void process(float dt);
     10        AITeam(){}
     11        std::vector<WorldEntity*>* getEnemyList();
     12        void process(float dt);
    1313        void addAI(int swarmNumber, AIModule* aiModule);
    1414        void removeAI(int swarmNumber, AIModule* aiModule);
     
    1616        inline void setEnemyList(std::vector<WorldEntity*>* enemyList){this->enemyList=enemyList;}
    1717 private:
    18         std::vector<WorldEntity*>* enemyList;
     18        std::vector<WorldEntity*>* enemyList;
    1919        std::map<int,AISwarm*> swarms;
    2020};
  • branches/ai/src/ai/movement_module.cc

    r10158 r10177  
    2828SHELL_COMMAND(setDistanceToNPC, MovementModule, setDistanceToNPC);
    2929SHELL_COMMAND(setMaxAccleartion, MovementModule, setMaxAccleartion);
     30SHELL_COMMAND(setTestValue, MovementModule, setTestValue);
     31SHELL_COMMAND(setTestValue2, MovementModule, setTestValue2);
    3032
    3133float MovementModule::distanceToPlayer=15;
    3234float MovementModule::distanceToNPC=2;
    33 float MovementModule::maxAccleration=200.0f;
     35float MovementModule::maxAccleration=300.0f;
     36float MovementModule::testValue=2;
     37float MovementModule::testValue2=40;
    3438
    3539void MovementModule::setDistanceToPlayer(float newValue){ distanceToPlayer=newValue; }
    3640void MovementModule::setDistanceToNPC(float newValue){ distanceToNPC=newValue; }
    3741void MovementModule::setMaxAccleartion(float newValue){ maxAccleration=newValue; }
     42void MovementModule::setTestValue(float newValue){ testValue=newValue; }
     43void MovementModule::setTestValue2(float newValue){ testValue2=newValue; }
    3844
    39 
    40 
    41 
    42 
    43 
    44 // std::vector<Vector>  MovementModule::hidingPoint;
    45 // std::vector<float>   MovementModule::hidingPointSize;
    46 //
    47 // std::vector<NPC2*> MovementModule::npcList;
    48 // std::vector<Vector> MovementModule::npcPosition;
    49 // std::vector<float> MovementModule::npcRadius;
    50 // std::vector<int> MovementModule::npcSwarm;
    51 // std::vector<int> MovementModule::npcTeam;
    52 //
    53 // Vector MovementModule::playerPosition;
    54 // Vector MovementModule::playerMovement;
    55 // float MovementModule::playerRadius;
    56 //
    57 // std::vector<Vector> MovementModule::swarmCenter;
    58 // std::vector<int> MovementModule::swarmMemberCount;
    59 //
    60 //
    61 //
    62 //
    63 //
     45MovementModule::MovementModule()
     46{
     47        tickCount=0;
     48        randomFreq=100;
     49}
    6450
    6551
     
    8268
    8369
    84 
    85 
    86 // void MovementModule::collectInformation(float dt)
    87 // {
    88 //      //return if already processed..
    89 //      if(dt==this->oldDT)return;
    90 //      this->oldDT=dt;
    91 //
    92 //
    93 //      //clear old Information..
    94 //      hidingPoint.clear();
    95 //      hidingPointSize.clear();
    96 //
    97 //      npcList.clear();
    98 //      npcPosition.clear();
    99 //      npcRadius.clear();
    100 //      npcSwarm.clear();
    101 //      npcTeam.clear();
    102 //
    103 //      swarmCenter.clear();
    104 //      swarmMemberCount.clear();
    105 //
    106 //
    107 //      //get all NPCs..
    108 //      for(ObjectList<NPC2>::const_iterator it = NPC2::objectList().begin(); it != NPC2::objectList().end(); ++it)
    109 //      {
    110 //              npcList.push_back(*it);
    111 //              npcRadius.push_back(getRadius(*it));
    112 //              npcPosition.push_back((*it)->getAbsCoor());
    113 //              npcSwarm.push_back((*it)->swarm);
    114 //              npcTeam.push_back((*it)->team);
    115 //      }
    116 //
    117 //
    118 //      //Swarm Information
    119 //      unsigned int tmpSwarm;
    120 //      for(unsigned int i=0;i<npcList.size();i++){
    121 //              tmpSwarm = npcSwarm.at(i);
    122 //              if(tmpSwarm > swarmMemberCount.size()){
    123 //                      //swarmMemberCount.insert(swarmMemberCount.size(), tmpSwarm - swarmMemberCount.size(), 0);
    124 //                      //swarmCenter.insert(swarmCenter.size(), tmpSwarm - swarmCenter.size(), Vector(0,0,0));
    125 //              }
    126 //              //swarmMemberCount.at(tmpSwarm)++;
    127 //              //swarmCenter.at(tmpSwarm)=swarmCenter.at(tmpSwarm)+npcPosition.at(i);
    128 //      }
    129 //      for(unsigned int i=0;i<swarmCenter.size();i++){
    130 //              //swarmCenter.at(i)=swarmCenter.at(i)/swarmMemberCount.at(i);
    131 //      }
    132 //
    133 //
    134 //      //get information about Player
    135 //      Player* pl = State::getPlayer();
    136 //      if( pl != NULL){
    137 //              playerPosition = pl->getPlayable()->getAbsCoor();
    138 //              playerRadius=getRadius( pl->getPlayable() );
    139 //              //PRINTF(0)("Player Radius: %f\n",playerRadius);
    140 //      }
    141 //
    142 //
    143 //      //calculate hiding Points..
    144 //
    145 // }
    146 
    147 
    14870void MovementModule::process(float dt)
    14971{
    15072        if(myNPC == NULL)return;
    15173
    152         //get information about Player
     74        Vector tmpVector;
     75        float tmpFloat;
     76        Vector npcCollision;
     77        Vector playerCollision;
     78        bool autoRotate=true;
     79        target=enemyList->at(0);
     80
     81        weight=1;
     82        speedMax=1000.0f;
     83
     84
     85        //get information about player
    15386        Player* pl = State::getPlayer();
    15487        if( pl == NULL)return;
    15588        Vector playerPosition = pl->getPlayable()->getAbsCoor();
    15689        float playerRadius=getRadius( pl->getPlayable() );
    157        
     90
    15891
    15992        //get information about myself
     
    16295
    16396
     97        //anti player collision
    16498        Vector vectorToPlayer = playerPosition - myPosition;
    16599
    166         Vector tmpVector;
    167         float tmpFloat;
    168        
    169         Vector npcCollision;
    170         Vector playerCollision;
    171 
    172 
    173 
    174         //float a=200.0f;
    175         float vMax=200.0f;
    176         //float maxAccleration=300.0f;
    177         //float safetyDistance=2.0f;
    178 
    179 
    180         //Anti Player Collision
    181100        tmpFloat=vectorToPlayer.len()-playerRadius-myRadius-distanceToPlayer;
    182101        if(tmpFloat<0.1)tmpFloat=0.1;
     
    184103
    185104
    186         //Anti NPC Collision
     105        //anti NPC collision
    187106        for(ObjectList<WorldEntity>::const_iterator it = WorldEntity::objectList().begin(); it != WorldEntity::objectList().end(); ++it)
    188107        {
     
    199118
    200119
     120        //random movement
     121        //randomFreq=testValue2;
     122        if(++tickCount>=randomFreq && movement.len()<60){
     123                tickCount=0;
     124                int x = (rand()%101)-50;                        //-50-50
     125                int z = (rand()%101)-50;                        //-50-50
     126                randomVector=Vector(x,0,z);
     127                randomFreq=(rand()%81)+70;                      //70-150 Ticks
     128        }
    201129
     130
     131        //calculate correction vector
    202132        Vector vectorToDestination=destination-myPosition;
    203133
     
    205135                                                                +       npcCollision*50*3
    206136                                                                +       Vector(0,0,0)
    207                                                                 +       destinationMovement*2//-myMovement
    208                                                                 +       (vectorToDestination-myMovement)*3;
     137                                                                +       destinationMovement*2//-movement
     138                                                                +       (vectorToDestination-movement)*3;
     139
     140        if(movement.len()<testValue2){
     141        //if(testValue2){
     142                correction=correction + randomVector * testValue;
     143                autoRotate=false;
     144        }
    209145
    210146        correction.y=0;
     147
     148
     149        //limit accleration
    211150        float correctionLen=correction.len();
    212151        if(correctionLen>maxAccleration*dt)correction=correction/correctionLen*maxAccleration*dt;
    213         myMovement+=correction;
     152        movement+=correction;
    214153
    215         float movementLen=myMovement.len();
    216         if(movementLen>vMax)myMovement/movementLen*vMax;
    217154
    218         //Move NPC...
    219         myNPC->shiftCoor(myMovement * dt);
     155        //limit speed
     156        float movementLen=movement.len();
     157        if(movementLen>speedMax)movement=movement/movementLen*speedMax;
    220158
    221         //Rotate NPC
    222         Vector view = myMovement;
    223         //if(vectorToPlayer.dot(v)<0){
    224         //      view = v.cross( Vector(0,1,0) ).getNormalized();
    225         //}else{
    226         view = myMovement.cross( Vector(0,1,0) ).getNormalized();
    227         //}
    228         //if(dist<keepDist)view=view*-1;
    229         //myNPC->setAbsDir( Quaternion( view, Vector(0,1,0)));
     159
     160        //move NPC...
     161        myNPC->shiftCoor(movement * dt);
     162
     163
     164        //rotate NPC
     165        Vector view;
     166        if(autoRotate){
     167                view = movement.cross( Vector(0,1,0) ).getNormalized();
     168        }else{
     169                view = target->getAbsCoor()-myPosition;
     170                view = view.cross( Vector(0,1,0) ).getNormalized();
     171        }
    230172        myNPC->setAbsDirSoft( Quaternion( view, Vector(0,1,0)),3);
    231         movement=myMovement;
     173
    232174}
    233175
  • branches/ai/src/ai/movement_module.h

    r10138 r10177  
    1313
    1414        public:
    15                 MovementModule() {}
     15                MovementModule();
    1616                inline MovementModule(NPC2* object){ this->myNPC=object; this->myWorldEntity=(WorldEntity*)object;}
    1717                virtual ~MovementModule(){}
     
    2121                static void setDistanceToNPC(float newValue);
    2222                static void setMaxAccleartion(float newValue);
    23 //
     23                static void setTestValue(float newValue);
     24                static void setTestValue2(float newValue);
     25
    2426        private:
    25 
    26 //      static std::vector<NPC2*> npcList;
    27 //      static std::vector<Vector> npcPosition;
    28 //      static std::vector<float> npcRadius;
    29 //      static std::vector<int> npcSwarm;
    30 //      static std::vector<int> npcTeam;
    31 //
    32 //      static Vector playerPosition;
    33 //      static Vector playerMovement;
    34 //      static float playerRadius;
    35 
    36 
    3727                Vector myMovement;
    3828                float myMaxAccleration;
     
    4434                static float distanceToPlayer;
    4535                static float distanceToNPC;
     36                static float testValue;
     37                static float testValue2;
     38
     39                int tickCount;
     40                int randomFreq;
     41                Vector randomVector;
    4642};
    4743
Note: See TracChangeset for help on using the changeset viewer.