Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10138 in orxonox.OLD for branches/ai


Ignore:
Timestamp:
Dec 21, 2006, 10:14:15 PM (18 years ago)
Author:
tfahrni
Message:

startet to implement swarming

Location:
branches/ai/src
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • branches/ai/src/ai/ai_engine.cc

    r10137 r10138  
    1616#include "ai_engine.h"
    1717#include "debug.h"
     18#include "npcs/npc_test.h"
     19#include "player.h"
     20#include "playable.h"
     21#include "state.h"
    1822
    1923AIEngine* AIEngine::singletonRef = NULL;
     
    2226{
    2327        std::map<int,AITeam*>::iterator it;
    24         for (it= teams.begin(); it!= teams.end(); it++ )
     28        std::vector<WorldEntity*>* enemyList=new std::vector<WorldEntity*>;
     29       
     30        for (it=teams.begin(); it!= teams.end(); it++ )
     31        {
     32                //find enemys for the team
     33                for(ObjectList<NPC2>::const_iterator npc = NPC2::objectList().begin(); npc != NPC2::objectList().end(); ++npc)
     34                        if((*npc)->getTeam() != it->first)enemyList->push_back(*npc);
     35               
     36                //add player to enemys (player belongs to team 0)
     37                if(it->first!=0){
     38                        //std::cout << "adding player to enemyList\n";
     39                        Player* pl = State::getPlayer();
     40                        if(pl != NULL)enemyList->push_back(pl->getPlayable());
     41                }
     42               
     43                //process the team
     44                it->second->setEnemyList(enemyList);
    2545                it->second->process(dt);
     46                enemyList->clear();
     47        }
     48       
     49        delete enemyList;
    2650}
    2751
  • branches/ai/src/ai/ai_module.cc

    r10061 r10138  
    1616*/
    1717#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_AI
    18 
    1918#include "ai_module.h"
    20 //#include "ai_engine.h"
    21 //#include "debug.h"
    22 
    23 
    24 
    25 AIModule::AIModule()
    26 {
    27 }
    28 
    29 
    30 void AIModule::setDifficulty(int newDifficulty)
    31 {
    32         difficulty=newDifficulty;
    33 }
    34 
    35 
    36 
    37 void AIModule::setOwner(AITeamMember* newOwner)
    38 {
    39         owner=newOwner;
    40 }
    41 
     19#include "debug.h"
  • branches/ai/src/ai/ai_module.h

    r10135 r10138  
    22#ifndef _AI_MODULE_H
    33#define _AI_MODULE_H
    4 
    5 
     4#include "world_entity.h"
    65class NPC2;
    7 class AITeamMember;
    8 class WorldEntity;
    9 
    106
    117class AIModule {
    128 public:
    13    AIModule();
    14    virtual ~AIModule() {}
    15 
    16    virtual void process() {}
    17    virtual void process(float dt) {}
    18 
     9   AIModule(){}
     10   virtual ~AIModule(){}
     11   virtual void process(float dt){}
     12       
    1913   void setDifficulty(int newDifficulty);
    20    void setOwner(AITeamMember* newOwner);
    21 
    22 
     14        inline void setEnemyList(std::vector<WorldEntity*>* enemyList){this->enemyList=enemyList;}
     15        inline Vector getPosition(){return myWorldEntity->getAbsCoor();}
     16        inline void setDestination(Vector destination){this->destination=destination;}
     17       
    2318 protected:
    24    int difficulty;
    25         int myTeam;
    26         int mySwarm;
    27 
    28    AITeamMember* owner;
    2919   NPC2* myNPC;
    30         WorldEntity* myWorldEnity;
     20        WorldEntity* myWorldEntity;
     21        std::vector<WorldEntity*>* enemyList;
     22        Vector destination;
    3123};
    3224
  • branches/ai/src/ai/ai_swarm.cc

    r10137 r10138  
    2020{
    2121        std::set<AIModule*>::iterator it;
    22         for (it= swarmMembers.begin(); it!= swarmMembers.end(); it++ )
     22        it= swarmMembers.begin();
     23        Vector leaderPosition=(*it)->getPosition();
     24        (*it)->setDestination(destination);
     25        (*it)->setEnemyList(enemyList);
     26        (*it)->process(dt);
     27               
     28        for (it++; it!= swarmMembers.end(); it++ ){
     29                (*it)->setDestination(leaderPosition);
     30                (*it)->setEnemyList(enemyList);
    2331                (*it)->process(dt);
     32        }
    2433}
    2534
     
    3645}
    3746
    38 int AISwarm::getSwarmSize()
     47Vector AISwarm::getPosition()
    3948{
    40         return swarmMembers.size();
     49        Vector center=Vector(0,0,0);
     50        std::set<AIModule*>::iterator it;
     51        for (it= swarmMembers.begin(); it!= swarmMembers.end(); it++ )
     52                center=center+(*it)->getPosition();
     53
     54        return center/swarmMembers.size();
    4155}
  • branches/ai/src/ai/ai_swarm.h

    r10137 r10138  
    33#define _AI_SWARM_H
    44
    5 //class AITeamMember;
    6 #include "ai_team_member.h"
    7 class NPC2;
     5#include "world_entity.h"
     6#include "ai_module.h"
    87
    98class AISwarm{
     
    1413                void addAI(AIModule* newMember);
    1514                void removeAI(AIModule* oldMember);
    16                 int getSwarmSize();
     15                inline int getSwarmSize(){return swarmMembers.size();}
     16                inline void setEnemyList(std::vector<WorldEntity*>* enemyList){this->enemyList=enemyList;}
     17                inline void setDestination(Vector destination){this->destination=destination;}
     18                Vector getPosition();
    1719        private:
     20                Vector destination;
     21                std::vector<WorldEntity*>* enemyList;
    1822                std::set<AIModule*> swarmMembers;
    1923};
  • branches/ai/src/ai/ai_team.cc

    r10137 r10138  
    2020{
    2121        std::map<int,AISwarm*>::iterator it;
    22         for (it= swarms.begin(); it!= swarms.end(); it++ )
     22        for (it= swarms.begin(); it!= swarms.end(); it++ ){
     23                it->second->setEnemyList(enemyList);
     24                if(enemyList->size()>0)
     25                        it->second->setDestination(enemyList->at(0)->getAbsCoor());
    2326                it->second->process(dt);
     27        }
    2428}
    2529
     
    4347        }
    4448}
    45 
    46 int AITeam::getTeamSize()
    47 {
    48         return swarms.size();
    49 }
  • branches/ai/src/ai/ai_team.h

    r10137 r10138  
    99        ~AITeam(){}
    1010        AITeam(){}
     11        std::vector<WorldEntity*>* getEnemyList();
    1112   void process(float dt);
    1213        void addAI(int swarmNumber, AIModule* aiModule);
    1314        void removeAI(int swarmNumber, AIModule* aiModule);
    14         int getTeamSize();
    15        
     15        inline int getTeamSize(){ return swarms.size(); }
     16        inline void setEnemyList(std::vector<WorldEntity*>* enemyList){this->enemyList=enemyList;}
    1617 private:
     18        std::vector<WorldEntity*>* enemyList;
    1719        std::map<int,AISwarm*> swarms;
    1820};
  • branches/ai/src/ai/movement_module.cc

    r10135 r10138  
    2525
    2626#include "shell_command.h"
    27 // SHELL_COMMAND(setDistanceToPlayer, MovementModule, setDistanceToPlayer);
    28 // SHELL_COMMAND(setDistanceToNPC, MovementModule, setDistanceToNPC);
    29 // SHELL_COMMAND(setMaxAccleartion, MovementModule, setMaxAccleartion);
    30 //
    31 // float MovementModule::distanceToPlayer=15;
    32 // float MovementModule::distanceToNPC=2;
    33 // float MovementModule::maxAccleration=200.0f;
    34 //
    35 // void MovementModule::setDistanceToPlayer(float newValue){ distanceToPlayer=newValue; }
    36 // void MovementModule::setDistanceToNPC(float newValue){ distanceToNPC=newValue; }
    37 // void MovementModule::setMaxAccleartion(float newValue){ maxAccleration=newValue; }
    38 //
    39 //
    40 //
    41 //
    42 //
    43 //
     27SHELL_COMMAND(setDistanceToPlayer, MovementModule, setDistanceToPlayer);
     28SHELL_COMMAND(setDistanceToNPC, MovementModule, setDistanceToNPC);
     29SHELL_COMMAND(setMaxAccleartion, MovementModule, setMaxAccleartion);
     30
     31float MovementModule::distanceToPlayer=15;
     32float MovementModule::distanceToNPC=2;
     33float MovementModule::maxAccleration=200.0f;
     34
     35void MovementModule::setDistanceToPlayer(float newValue){ distanceToPlayer=newValue; }
     36void MovementModule::setDistanceToNPC(float newValue){ distanceToNPC=newValue; }
     37void MovementModule::setMaxAccleartion(float newValue){ maxAccleration=newValue; }
     38
     39
     40
     41
     42
     43
    4444// std::vector<Vector>  MovementModule::hidingPoint;
    4545// std::vector<float>   MovementModule::hidingPointSize;
     
    6262//
    6363//
    64 MovementModule::MovementModule(NPC2* object){ this->myNPC=object; }
    65 MovementModule::~MovementModule(){}
    66 
    6764
    6865
     
    156153        Player* pl = State::getPlayer();
    157154        if( pl == NULL)return;
    158 
    159155        Vector playerPosition = pl->getPlayable()->getAbsCoor();
    160156        float playerRadius=getRadius( pl->getPlayable() );
    161 
    162 
    163 // collectInformation(dt);
    164 
     157       
     158
     159        //get information about myself
    165160        Vector myPosition = myNPC->getAbsCoor();
    166161        float myRadius = getRadius(myNPC);
     
    171166        Vector tmpVector;
    172167        float tmpFloat;
    173 
    174 //      Vector npcCollision;
    175 //      Vector swarmVector;
     168       
     169        Vector npcCollision;
    176170        Vector playerCollision;
    177171
     
    180174        //float a=200.0f;
    181175        float vMax=200.0f;
    182         float maxAccleration=300.0f;
     176        //float maxAccleration=300.0f;
    183177        //float safetyDistance=2.0f;
    184178
    185179
    186180        //Anti Player Collision
    187         tmpFloat=vectorToPlayer.len()-playerRadius-myRadius-30;//distanceToPlayer;
     181        tmpFloat=vectorToPlayer.len()-playerRadius-myRadius-distanceToPlayer;
    188182        if(tmpFloat<0.1)tmpFloat=0.1;
    189183        playerCollision=vectorToPlayer/(tmpFloat*tmpFloat)*(-1);
    190184
    191185
    192 
    193 //      for (unsigned int i=0;i<npcList.size();i++)
    194 //      {
    195 //              if(npcList.at(i)==myNPC)continue;
    196 //
    197 //              //Anti NPC Collision
    198 //              tmpVector=myPosition-npcPosition.at(i);
    199 //              tmpFloat=tmpVector.len()-myRadius-npcRadius.at(i)-distanceToNPC;
    200 //
    201 //              if(tmpFloat<0.1)tmpFloat=0.1;
    202 //              tmpVector=tmpVector/(tmpFloat*tmpFloat);
    203 //
    204 //              npcCollision=npcCollision+tmpVector;
    205 //
    206 //              //Schwarmverhalten
    207 //              if(npcSwarm.at(i) == mySwarm){
    208 //
    209 //
    210 //              }
    211 //      }
    212 
    213 
    214 
    215 
    216 
    217         //Vector correction=playerCollision*50+npcCollision*50+vectorToPlayer+Vector(70,0,0)-myMovement;
    218         Vector correction=playerCollision*50+vectorToPlayer+Vector(50,0,0)-myMovement;
     186        //Anti NPC Collision
     187        for(ObjectList<WorldEntity>::const_iterator it = WorldEntity::objectList().begin(); it != WorldEntity::objectList().end(); ++it)
     188        {
     189                if(*it==myNPC)continue;
     190
     191                tmpVector=myPosition-(*it)->getAbsCoor();
     192                tmpFloat=tmpVector.len()-myRadius-distanceToNPC-getRadius(*it);
     193
     194                if(tmpFloat<0.1)tmpFloat=0.1;
     195                tmpVector=tmpVector/(tmpFloat*tmpFloat);
     196
     197                npcCollision=npcCollision+tmpVector;
     198        }
     199
     200
     201
     202        Vector vectorToDestination=destination-myPosition;
     203
     204        Vector correction=playerCollision*50+npcCollision*50+vectorToDestination+Vector(0,0,0)-myMovement;
    219205
    220206        correction.y=0;
     
    234220        //      view = v.cross( Vector(0,1,0) ).getNormalized();
    235221        //}else{
    236         view = myMovement.cross( Vector(0,-1,0) ).getNormalized();
     222        view = myMovement.cross( Vector(0,1,0) ).getNormalized();
    237223        //}
    238224        //if(dist<keepDist)view=view*-1;
  • branches/ai/src/ai/movement_module.h

    r10135 r10138  
    1414        public:
    1515                MovementModule() {}
    16                 MovementModule(NPC2* object);
    17                 virtual ~MovementModule();
     16                inline MovementModule(NPC2* object){ this->myNPC=object; this->myWorldEntity=(WorldEntity*)object;}
     17                virtual ~MovementModule(){}
    1818                virtual void process(float dt);
    1919
    20 //    static void setDistanceToPlayer(float newValue);
    21 //    static void setDistanceToNPC(float newValue);
    22 //    static void setMaxAccleartion(float newValue);
     20                static void setDistanceToPlayer(float newValue);
     21                static void setDistanceToNPC(float newValue);
     22                static void setMaxAccleartion(float newValue);
    2323//
    2424        private:
    25 //      void collectInformation(float dt);
    26 //
    27 //
    28 //      static std::vector<Vector>      hidingPoint;
    29 //      static std::vector<float>       hidingPointSize;
    30 //
     25
    3126//      static std::vector<NPC2*> npcList;
    3227//      static std::vector<Vector> npcPosition;
     
    3833//      static Vector playerMovement;
    3934//      static float playerRadius;
    40 //
    41 //      static std::vector<Vector> swarmCenter;
    42 //      static std::vector<int> swarmMemberCount;
     35
    4336
    4437                Vector myMovement;
     
    4639                float myMaxSpeed;
    4740
     41                float getRadius(WorldEntity* object);
    4842
    49                 float getRadius(WorldEntity* object);
    50 //      static float aa;
    51 //      float oldDT;
    52 //
    53 //      static float maxAccleration;
    54 //      static float distanceToPlayer;
    55 //      static float distanceToNPC;
     43                static float maxAccleration;
     44                static float distanceToPlayer;
     45                static float distanceToNPC;
    5646};
    5747
  • branches/ai/src/world_entities/npcs/npc.h

    r10041 r10138  
    44
    55#include "world_entity.h"
    6 #include "ai_team_member.h"
    76
    87class AI;
    98
    10 class NPC : public AITeamMember {
     9class NPC : public WorldEntity {
    1110  ObjectListDeclaration(NPC);
    1211 public:
  • branches/ai/src/world_entities/npcs/npc_test.cc

    r10135 r10138  
    5151        std::cout << "Team Number: " << teamNumber << "\n";
    5252        std::cout << "Swarm Number:" << swarmNumber << "\n";
    53 
    54         AITeam* myTeam=AIEngine::getInstance()->getCreateTeam(teamNumber);
    55         //std::cout << "Testpoint 4: " << myTeam << "\n";
    56         AISwarm* mySwarm=myTeam->getCreateSwarm(swarmNumber);
    57         //std::cout << "Testpoint 5: " << mySwarm << "\n";
    58         mySwarm->addToSwarm(new MovementModule(this));
    59 
     53        aiModule=new MovementModule(this);
     54        AIEngine::getInstance()->addAI(teamNumber,swarmNumber,aiModule);
    6055}
    6156
    62 
     57NPC2::~NPC2()
     58{
     59        AIEngine::getInstance()->removeAI(teamNumber,swarmNumber,aiModule);
     60}
    6361
    6462void NPC2::loadParams(const TiXmlElement* root)
  • branches/ai/src/world_entities/npcs/npc_test.h

    r10135 r10138  
    1515        public:
    1616                NPC2 (const TiXmlElement* root);
    17                 virtual ~NPC2 (){};
     17                virtual ~NPC2 ();
    1818                virtual void loadParams(const TiXmlElement* root);
    1919                virtual void tick(float dt);
     20                inline int getTeam(){return teamNumber;}
    2021
    2122
  • branches/ai/src/world_entities/space_ships/spacecraft_2d.cc

    r9947 r10138  
    151151  this->travelNode = new PNode();
    152152
    153   this->loadModel("models/ships/mantawing.obj", 5.0f);
     153  this->loadModel("models/ships/mantawing.obj", 2.5f);
    154154
    155155  // camera - issue
Note: See TracChangeset for help on using the changeset viewer.