Changeset 10226 in orxonox.OLD for branches/ai/src
- Timestamp:
- Jan 10, 2007, 6:31:30 PM (18 years ago)
- Location:
- branches/ai/src
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ai/src/ai/Makefile.am
r10135 r10226 11 11 ai_team_member.cc \ 12 12 movement_module.cc \ 13 shooting_module.cc 13 shooting_module.cc \ 14 attack_module.cc 14 15 15 16 noinst_HEADERS = \ … … 20 21 ai_team_member.h \ 21 22 movement_module.h \ 22 shooting_module.h 23 shooting_module.h \ 24 attack_module.h -
branches/ai/src/ai/ai_engine.cc
r10138 r10226 27 27 std::map<int,AITeam*>::iterator it; 28 28 std::vector<WorldEntity*>* enemyList=new std::vector<WorldEntity*>; 29 29 30 30 for (it=teams.begin(); it!= teams.end(); it++ ) 31 31 { … … 33 33 for(ObjectList<NPC2>::const_iterator npc = NPC2::objectList().begin(); npc != NPC2::objectList().end(); ++npc) 34 34 if((*npc)->getTeam() != it->first)enemyList->push_back(*npc); 35 35 36 36 //add player to enemys (player belongs to team 0) 37 37 if(it->first!=0){ … … 40 40 if(pl != NULL)enemyList->push_back(pl->getPlayable()); 41 41 } 42 42 43 43 //process the team 44 44 it->second->setEnemyList(enemyList); … … 46 46 enemyList->clear(); 47 47 } 48 48 49 49 delete enemyList; 50 50 } 51 51 52 void AIEngine::addAI(int teamNumber, int swarmNumber, AIModule* aiModule)52 void AIEngine::addAI(int teamNumber, int swarmNumber, WorldEntity* npc) 53 53 { 54 54 std::pair<std::map<int,AITeam*>::iterator,bool> p; … … 56 56 p=teams.insert(std::make_pair(teamNumber,newTeam)); 57 57 if(!p.second)delete newTeam; 58 p.first->second->addAI(swarmNumber, aiModule);58 p.first->second->addAI(swarmNumber, npc); 59 59 } 60 60 61 void AIEngine::removeAI(int teamNumber, int swarmNumber, AIModule* aiModule)61 void AIEngine::removeAI(int teamNumber, int swarmNumber, WorldEntity* npc) 62 62 { 63 63 std::map<int,AITeam*>::iterator it = teams.find(swarmNumber); 64 64 if(it==teams.end())return; 65 it->second->removeAI(swarmNumber, aiModule);65 it->second->removeAI(swarmNumber,npc); 66 66 if(it->second->getTeamSize()==0){ 67 67 delete it->second; -
branches/ai/src/ai/ai_engine.h
r10137 r10226 14 14 15 15 void tick(float dt); 16 void addAI(int teamNumber, int swarmNumber, AIModule* aiModule);17 void removeAI(int teamNumber, int swarmNumber, AIModule* aiModule);18 16 void addAI(int teamNumber, int swarmNumber, WorldEntity* npc); 17 void removeAI(int teamNumber, int swarmNumber, WorldEntity* npc); 18 19 19 private: 20 20 AIEngine(){} -
branches/ai/src/ai/ai_module.cc
r10138 r10226 18 18 #include "ai_module.h" 19 19 #include "debug.h" 20 #include "aabb.h" 21 22 float AIModule::getRadius(WorldEntity* object) 23 { 24 AABB* aabb = object->getModelAABB(); 25 if( aabb == NULL)return -1; 26 27 float a = aabb->halfLength[0]; 28 float b = aabb->halfLength[1]; 29 float c = aabb->halfLength[2]; 30 31 if(a>b){ 32 return (c>a)?c:a; 33 }else{ 34 return (c>b)?c:b; 35 } 36 } 37 38 void AIModule::getAttributesFrom(AIModule* other) 39 { 40 this->view = other->getView(); 41 this->movement = other->getMovement(); 42 this->npc = other->getNPC(); 43 this->target = other->getTarget(); 44 this->weight = other->getWeight(); 45 this->speedMax = other->getMaxSpeed(); 46 this->accelerationMax = other->getMaxAcceleration(); 47 } -
branches/ai/src/ai/ai_module.h
r10177 r10226 7 7 class AIModule { 8 8 public: 9 9 AIModule(){} 10 10 virtual ~AIModule(){} 11 11 virtual void process(float dt){} 12 12 13 void setDifficulty(int newDifficulty);14 inline void setEnemyList(std::vector<WorldEntity*>* enemyList){this->enemyList=enemyList;}15 inline Vector getPosition(){return myWorldEntity->getAbsCoor();}16 inline Vector getMovement(){return movement;}17 13 inline void setDestination(Vector destination){this->destination=destination;} 18 14 inline void setDestinationMovement(Vector destinationMovement){this->destinationMovement=destinationMovement;} 19 15 inline void setWeight(int weight){this->weight=weight;} 20 16 inline void setTarget(WorldEntity* target){this->target=target;} 17 inline void setNPC(WorldEntity* npc){this->npc=npc;} 18 19 inline Vector getPosition(){return npc->getAbsCoor();} 20 inline Vector getMovement(){return movement;} 21 inline Vector getView(){return view;} 22 inline WorldEntity* getNPC(){return npc;} 23 inline WorldEntity* getTarget(){return target;} 24 inline float getWeight(){return weight;} 25 inline float getMaxAcceleration(){return accelerationMax;} 26 inline float getMaxSpeed(){return speedMax;} 27 28 void getAttributesFrom(AIModule* oldModule); 21 29 22 30 protected: 23 NPC2* myNPC; 24 WorldEntity* myWorldEntity; 25 std::vector<WorldEntity*>* enemyList; 31 float getRadius(WorldEntity* object); 26 32 27 33 Vector destination; 28 34 Vector destinationMovement; 35 Vector destinationView; 36 37 29 38 Vector movement; 39 Vector view; 40 41 WorldEntity* npc; 42 WorldEntity* target; 30 43 31 44 float weight; 32 45 float speedMax; 33 34 WorldEntity* target; 46 float accelerationMax; 35 47 }; 36 48 -
branches/ai/src/ai/ai_swarm.cc
r10177 r10226 15 15 #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_AI 16 16 #include "ai_swarm.h" 17 #include "ai_module.h" 18 #include "movement_module.h" 19 #include "attack_module.h" 17 20 #include "debug.h" 18 21 #include <stdio.h> 22 #include "aabb.h" 19 23 20 24 AISwarm::AISwarm() … … 27 31 void AISwarm::process(float dt) 28 32 { 29 if(tickCount>=randomFreq && movement.len()<60){ 33 std::map<WorldEntity*,AIModule*>::iterator it; 34 35 Vector swarmPosition=this->getPosition(); 36 Vector targetPosition=target->getAbsCoor(); 37 float distanceToTarget=(swarmPosition-targetPosition).len(); 38 39 float aMax=70.0f; 40 float vMax=1000.0f; 41 float deltaDistance=30; 42 float attackDistance=100; 43 44 45 46 if(distanceToTarget+deltaDistance<=attackDistance && status!=ATTACKING){ 47 for (it= members.begin(); it!= members.end(); it++ ){ 48 AIModule* oldAI = it->second; 49 AIModule* newAI = new AttackModule(); 50 newAI->getAttributesFrom(oldAI); 51 it->second=newAI; 52 delete oldAI; 53 status=ATTACKING; 54 } 55 }else if(distanceToTarget-deltaDistance>=attackDistance && status!=MOVING){ 56 for (it= members.begin(); it!= members.end(); it++ ){ 57 AIModule* oldAI = it->second; 58 AIModule* newAI = new MovementModule(); 59 newAI->getAttributesFrom(oldAI); 60 it->second=newAI; 61 delete oldAI; 62 status=MOVING; 63 } 64 } 65 66 67 68 if(status!=ATTACKING && tickCount>=randomFreq){ 30 69 tickCount=0; 31 int x = (rand()%141)+10; //10-150 70 //int x = (rand()%141)+10; //10-150 71 int x = (rand()%241)-120; //-120-120 32 72 int z = (rand()%241)-120; //-120-120 33 73 randomVector=Vector(x,0,z); 34 74 35 std::cout << "change to: ||" << randomVector.x << " ==" << randomVector.z << "\n";75 //std::cout << "change to: ||" << randomVector.x << " ==" << randomVector.z << "\n"; 36 76 } 37 77 tickCount++; 38 78 39 79 40 std::set<AIModule*>::iterator it;41 Vector swarmPosition=this->getPosition();42 //std::cout << swarmPosition.x << " " << swarmPosition.z << "\n";43 44 float aMax=70.0f;45 float vMax=1000.0f;46 80 47 81 Vector correction=(destination+randomVector-swarmPosition)-movement; … … 58 92 59 93 60 for (it= swarmMembers.begin(); it!= swarmMembers.end(); it++ ){ 61 (*it)->setDestination(swarmPosition); 62 (*it)->setDestinationMovement(movement); 63 (*it)->setEnemyList(enemyList); 64 (*it)->process(dt); 94 95 for (it= members.begin(); it!= members.end(); it++ ){ 96 it->second->setDestination(swarmPosition); 97 it->second->setDestinationMovement(movement); 98 it->second->setTarget(target); 99 it->second->process(dt); 65 100 } 66 101 } 67 102 68 103 69 void AISwarm::addAI(AIModule* aiModule) 104 105 void AISwarm::addAI(WorldEntity* npc) 70 106 { 71 swarmMembers.insert(aiModule); 107 std::pair< std::map<WorldEntity*,AIModule*>::iterator , bool > p; 108 AIModule* newAIModule=new MovementModule(npc); 109 p=members.insert(std::make_pair(npc,newAIModule)); 110 if(!p.second)delete newAIModule; 72 111 } 73 112 74 113 75 void AISwarm::removeAI( AIModule* aiModule)114 void AISwarm::removeAI(WorldEntity* npc) 76 115 { 77 std::set<AIModule*>::iterator it=swarmMembers.find(aiModule); 78 if(it==swarmMembers.end())return; 79 delete (*it); 116 std::map<WorldEntity*,AIModule*>::iterator it = members.find(npc); 117 if(it==members.end())return; //npc not found 118 delete it->second; //delete AIModule 119 members.erase(it); //remove AIModule from members 80 120 } 81 121 … … 84 124 { 85 125 Vector center=Vector(0,0,0); 86 std:: set<AIModule*>::iterator it;87 for (it= swarmMembers.begin(); it!= swarmMembers.end(); it++ )88 center=center+ (*it)->getPosition();126 std::map<WorldEntity*,AIModule*>::iterator it; 127 for (it= members.begin(); it!= members.end(); it++ ) 128 center=center+it->second->getPosition(); 89 129 90 return center/ swarmMembers.size();130 return center/members.size(); 91 131 } 132 133 float AISwarm::getRadius(WorldEntity* object) 134 { 135 AABB* aabb = object->getModelAABB(); 136 if( aabb == NULL)return -1; 137 138 float a = aabb->halfLength[0]; 139 float b = aabb->halfLength[1]; 140 float c = aabb->halfLength[2]; 141 142 if(a>b){ 143 return (c>a)?c:a; 144 }else{ 145 return (c>b)?c:b; 146 } 147 } -
branches/ai/src/ai/ai_swarm.h
r10177 r10226 6 6 #include "ai_module.h" 7 7 8 9 8 10 class AISwarm{ 9 11 public: … … 11 13 ~AISwarm(){} 12 14 void process(float dt); 13 void addAI( AIModule* newMember);14 void removeAI( AIModule* oldMember);15 inline int getSwarmSize(){return swarmMembers.size();} 16 inline void setEnemyList(std::vector<WorldEntity*>* enemyList){this->enemyList=enemyList;}15 void addAI(WorldEntity*); 16 void removeAI(WorldEntity*); 17 18 inline int getSwarmSize(){return members.size();} 17 19 inline void setDestination(Vector destination){this->destination=destination;} 20 inline void setTarget(WorldEntity* target){this->target=target;} 18 21 Vector getPosition(); 22 23 enum statusType{ATTACKING,MOVING,WAITING}; 24 19 25 private: 26 float getRadius(WorldEntity* object); 27 28 20 29 Vector destination; 21 30 Vector movement; 22 std::vector<WorldEntity*>* enemyList; 23 std::set<AIModule*> swarmMembers; 31 WorldEntity* target; 32 statusType status; 33 34 std::map<WorldEntity*,AIModule*> members; 35 24 36 int tickCount; 25 37 int randomFreq; -
branches/ai/src/ai/ai_team.cc
r10177 r10226 21 21 std::map<int,AISwarm*>::iterator it; 22 22 for (it= swarms.begin(); it!= swarms.end(); it++ ){ 23 24 it->second->setEnemyList(enemyList); 25 if(enemyList->size()>0) 23 if(enemyList->size()>0){ 24 it->second->setTarget(enemyList->at(0)); 26 25 it->second->setDestination(enemyList->at(0)->getAbsCoor()); 26 } 27 27 it->second->process(dt); 28 28 } … … 30 30 31 31 32 void AITeam::addAI(int swarmNumber, AIModule* aiModule)32 void AITeam::addAI(int swarmNumber, WorldEntity* npc) 33 33 { 34 34 std::pair<std::map<int,AISwarm*>::iterator,bool> p; … … 36 36 p=swarms.insert(std::make_pair(swarmNumber,newSwarm)); 37 37 if(!p.second)delete newSwarm; 38 p.first->second->addAI( aiModule);38 p.first->second->addAI(npc); 39 39 } 40 40 41 41 42 void AITeam::removeAI(int swarmNumber, AIModule* aiModule)42 void AITeam::removeAI(int swarmNumber, WorldEntity* npc) 43 43 { 44 44 std::map<int,AISwarm*>::iterator it = swarms.find(swarmNumber); 45 45 if(it==swarms.end())return; 46 it->second->removeAI( aiModule);46 it->second->removeAI(npc); 47 47 if(it->second->getSwarmSize()==0){ 48 48 delete it->second; -
branches/ai/src/ai/ai_team.h
r10177 r10226 11 11 std::vector<WorldEntity*>* getEnemyList(); 12 12 void process(float dt); 13 void addAI(int swarmNumber, AIModule* aiModule);14 void removeAI(int swarmNumber, AIModule* aiModule);13 void addAI(int swarmNumber, WorldEntity* npc); 14 void removeAI(int swarmNumber, WorldEntity* npc); 15 15 inline int getTeamSize(){ return swarms.size(); } 16 16 inline void setEnemyList(std::vector<WorldEntity*>* enemyList){this->enemyList=enemyList;} -
branches/ai/src/ai/movement_module.cc
r10177 r10226 21 21 #include "player.h" 22 22 #include "playable.h" 23 #include "aabb.h"24 23 #include "npcs/npc_test.h" 25 24 … … 50 49 51 50 52 53 float MovementModule::getRadius(WorldEntity* object)54 {55 AABB* aabb = object->getModelAABB();56 if( aabb == NULL)return -1;57 58 float a = aabb->halfLength[0];59 float b = aabb->halfLength[1];60 float c = aabb->halfLength[2];61 62 if(a>b){63 return (c>a)?c:a;64 }else{65 return (c>b)?c:b;66 }67 }68 69 70 51 void MovementModule::process(float dt) 71 52 { 72 if( myNPC== NULL)return;53 if(npc == NULL)return; 73 54 74 55 Vector tmpVector; … … 76 57 Vector npcCollision; 77 58 Vector playerCollision; 78 bool autoRotate=true;79 target=enemyList->at(0);80 59 81 60 weight=1; 82 speedMax=1000.0f;83 61 84 62 … … 91 69 92 70 //get information about myself 93 Vector myPosition = myNPC->getAbsCoor(); 94 float myRadius = getRadius(myNPC); 71 Vector myPosition = npc->getAbsCoor(); 72 float myRadius = getRadius(npc); 73 74 75 float aMax=maxAccleration; 76 float vMax=800.0f/myRadius; 95 77 96 78 … … 106 88 for(ObjectList<WorldEntity>::const_iterator it = WorldEntity::objectList().begin(); it != WorldEntity::objectList().end(); ++it) 107 89 { 108 if(*it== myNPC)continue;90 if(*it==npc)continue; 109 91 110 92 tmpVector=myPosition-(*it)->getAbsCoor(); … … 115 97 116 98 npcCollision=npcCollision+tmpVector; 117 }118 119 120 //random movement121 //randomFreq=testValue2;122 if(++tickCount>=randomFreq && movement.len()<60){123 tickCount=0;124 int x = (rand()%101)-50; //-50-50125 int z = (rand()%101)-50; //-50-50126 randomVector=Vector(x,0,z);127 randomFreq=(rand()%81)+70; //70-150 Ticks128 99 } 129 100 … … 138 109 + (vectorToDestination-movement)*3; 139 110 140 if(movement.len()<testValue2){141 //if(testValue2){142 correction=correction + randomVector * testValue;143 autoRotate=false;144 }145 146 111 correction.y=0; 147 112 … … 149 114 //limit accleration 150 115 float correctionLen=correction.len(); 151 if(correctionLen> maxAccleration*dt)correction=correction/correctionLen*maxAccleration*dt;116 if(correctionLen>aMax*dt)correction=correction/correctionLen*aMax*dt; 152 117 movement+=correction; 153 118 … … 155 120 //limit speed 156 121 float movementLen=movement.len(); 157 if(movementLen> speedMax)movement=movement/movementLen*speedMax;122 if(movementLen>vMax)movement=movement/movementLen*vMax; 158 123 159 124 160 125 //move NPC... 161 myNPC->shiftCoor(movement * dt);126 npc->shiftCoor(movement * dt); 162 127 163 128 164 129 //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 } 172 myNPC->setAbsDirSoft( Quaternion( view, Vector(0,1,0)),3); 130 view = movement.cross( Vector(0,1,0) ).getNormalized(); 131 npc->setAbsDirSoft( Quaternion( view, Vector(0,1,0)),3); 173 132 174 133 } -
branches/ai/src/ai/movement_module.h
r10177 r10226 14 14 public: 15 15 MovementModule(); 16 inline MovementModule( NPC2* object){ this->myNPC=object; this->myWorldEntity=(WorldEntity*)object;}16 inline MovementModule(WorldEntity* object){this->npc=object;} 17 17 virtual ~MovementModule(){} 18 18 virtual void process(float dt); … … 29 29 float myMaxSpeed; 30 30 31 float getRadius(WorldEntity* object);32 33 31 static float maxAccleration; 34 32 static float distanceToPlayer; -
branches/ai/src/world_entities/npcs/npc_test.cc
r10138 r10226 51 51 std::cout << "Team Number: " << teamNumber << "\n"; 52 52 std::cout << "Swarm Number:" << swarmNumber << "\n"; 53 aiModule=new MovementModule(this); 54 AIEngine::getInstance()->addAI(teamNumber,swarmNumber,aiModule); 53 //aiModule=new MovementModule(this); 54 //AIEngine::getInstance()->addAI(teamNumber,swarmNumber,aiModule); 55 AIEngine::getInstance()->addAI(teamNumber,swarmNumber,(WorldEntity*)this); 55 56 } 56 57 57 58 NPC2::~NPC2() 58 59 { 59 AIEngine::getInstance()->removeAI(teamNumber,swarmNumber, aiModule);60 AIEngine::getInstance()->removeAI(teamNumber,swarmNumber,(WorldEntity*)this); 60 61 } 61 62
Note: See TracChangeset
for help on using the changeset viewer.