Index: branches/ai/src/ai/Makefile.am
===================================================================
--- branches/ai/src/ai/Makefile.am	(revision 10225)
+++ branches/ai/src/ai/Makefile.am	(revision 10226)
@@ -11,5 +11,6 @@
 			ai_team_member.cc \
 			movement_module.cc \
-			shooting_module.cc
+			shooting_module.cc \
+			attack_module.cc
 
 noinst_HEADERS =        \
@@ -20,3 +21,4 @@
 			ai_team_member.h \
 			movement_module.h \
-			shooting_module.h
+			shooting_module.h \
+			attack_module.h
Index: branches/ai/src/ai/ai_engine.cc
===================================================================
--- branches/ai/src/ai/ai_engine.cc	(revision 10225)
+++ branches/ai/src/ai/ai_engine.cc	(revision 10226)
@@ -27,5 +27,5 @@
 	std::map<int,AITeam*>::iterator it;
 	std::vector<WorldEntity*>* enemyList=new std::vector<WorldEntity*>;
-	
+
 	for (it=teams.begin(); it!= teams.end(); it++ )
 	{
@@ -33,5 +33,5 @@
 		for(ObjectList<NPC2>::const_iterator npc = NPC2::objectList().begin(); npc != NPC2::objectList().end(); ++npc)
 			if((*npc)->getTeam() != it->first)enemyList->push_back(*npc);
-		
+
 		//add player to enemys (player belongs to team 0)
 		if(it->first!=0){
@@ -40,5 +40,5 @@
 			if(pl != NULL)enemyList->push_back(pl->getPlayable());
 		}
-		
+
 		//process the team
 		it->second->setEnemyList(enemyList);
@@ -46,9 +46,9 @@
 		enemyList->clear();
 	}
-	
+
 	delete enemyList;
 }
 
-void AIEngine::addAI(int teamNumber, int swarmNumber, AIModule* aiModule)
+void AIEngine::addAI(int teamNumber, int swarmNumber, WorldEntity* npc)
 {
 	std::pair<std::map<int,AITeam*>::iterator,bool> p;
@@ -56,12 +56,12 @@
 	p=teams.insert(std::make_pair(teamNumber,newTeam));
 	if(!p.second)delete newTeam;
-	p.first->second->addAI(swarmNumber, aiModule);
+	p.first->second->addAI(swarmNumber, npc);
 }
 
-void AIEngine::removeAI(int teamNumber, int swarmNumber, AIModule* aiModule)
+void AIEngine::removeAI(int teamNumber, int swarmNumber, WorldEntity* npc)
 {
 	std::map<int,AITeam*>::iterator it = teams.find(swarmNumber);
 	if(it==teams.end())return;
-	it->second->removeAI(swarmNumber,aiModule);
+	it->second->removeAI(swarmNumber,npc);
 	if(it->second->getTeamSize()==0){
 		delete it->second;
Index: branches/ai/src/ai/ai_engine.h
===================================================================
--- branches/ai/src/ai/ai_engine.h	(revision 10225)
+++ branches/ai/src/ai/ai_engine.h	(revision 10226)
@@ -14,7 +14,7 @@
 
    void tick(float dt);
-	void addAI(int teamNumber, int swarmNumber, AIModule* aiModule);
-	void removeAI(int teamNumber, int swarmNumber, AIModule* aiModule);
-	
+	void addAI(int teamNumber, int swarmNumber, WorldEntity* npc);
+	void removeAI(int teamNumber, int swarmNumber, WorldEntity* npc);
+
  private:
 	AIEngine(){}
Index: branches/ai/src/ai/ai_module.cc
===================================================================
--- branches/ai/src/ai/ai_module.cc	(revision 10225)
+++ branches/ai/src/ai/ai_module.cc	(revision 10226)
@@ -18,2 +18,30 @@
 #include "ai_module.h"
 #include "debug.h"
+#include "aabb.h"
+
+float AIModule::getRadius(WorldEntity* object)
+{
+	AABB* aabb = object->getModelAABB();
+	if( aabb == NULL)return -1;
+
+	float a = aabb->halfLength[0];
+	float b = aabb->halfLength[1];
+	float c = aabb->halfLength[2];
+
+	if(a>b){
+		return (c>a)?c:a;
+	}else{
+		return (c>b)?c:b;
+	}
+}
+
+void AIModule::getAttributesFrom(AIModule* other)
+{
+	this->view = other->getView();
+	this->movement = other->getMovement();
+	this->npc = other->getNPC();
+	this->target = other->getTarget();
+	this->weight = other->getWeight();
+	this->speedMax = other->getMaxSpeed();
+	this->accelerationMax = other->getMaxAcceleration();
+}
Index: branches/ai/src/ai/ai_module.h
===================================================================
--- branches/ai/src/ai/ai_module.h	(revision 10225)
+++ branches/ai/src/ai/ai_module.h	(revision 10226)
@@ -7,30 +7,42 @@
 class AIModule {
  public:
-   AIModule(){}
+	AIModule(){}
    virtual ~AIModule(){}
    virtual void process(float dt){}
 
-   void setDifficulty(int newDifficulty);
-	inline void setEnemyList(std::vector<WorldEntity*>* enemyList){this->enemyList=enemyList;}
-	inline Vector getPosition(){return myWorldEntity->getAbsCoor();}
-	inline Vector getMovement(){return movement;}
 	inline void setDestination(Vector destination){this->destination=destination;}
 	inline void setDestinationMovement(Vector destinationMovement){this->destinationMovement=destinationMovement;}
 	inline void setWeight(int weight){this->weight=weight;}
 	inline void setTarget(WorldEntity* target){this->target=target;}
+	inline void setNPC(WorldEntity* npc){this->npc=npc;}
+
+	inline Vector getPosition(){return npc->getAbsCoor();}
+	inline Vector getMovement(){return movement;}
+	inline Vector getView(){return view;}
+	inline WorldEntity* getNPC(){return npc;}
+	inline WorldEntity* getTarget(){return target;}
+	inline float getWeight(){return weight;}
+	inline float getMaxAcceleration(){return accelerationMax;}
+	inline float getMaxSpeed(){return speedMax;}
+
+	void getAttributesFrom(AIModule* oldModule);
 
  protected:
-   NPC2* myNPC;
-	WorldEntity* myWorldEntity;
-	std::vector<WorldEntity*>* enemyList;
+	float getRadius(WorldEntity* object);
 
 	Vector destination;
 	Vector destinationMovement;
+	Vector destinationView;
+
+
 	Vector movement;
+	Vector view;
+
+	WorldEntity* npc;
+	WorldEntity* target;
 
 	float weight;
 	float speedMax;
-
-	WorldEntity* target;
+	float accelerationMax;
 };
 
Index: branches/ai/src/ai/ai_swarm.cc
===================================================================
--- branches/ai/src/ai/ai_swarm.cc	(revision 10225)
+++ branches/ai/src/ai/ai_swarm.cc	(revision 10226)
@@ -15,6 +15,10 @@
 #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_AI
 #include "ai_swarm.h"
+#include "ai_module.h"
+#include "movement_module.h"
+#include "attack_module.h"
 #include "debug.h"
 #include <stdio.h>
+#include "aabb.h"
 
 AISwarm::AISwarm()
@@ -27,21 +31,51 @@
 void AISwarm::process(float dt)
 {
-	if(tickCount>=randomFreq && movement.len()<60){
+	std::map<WorldEntity*,AIModule*>::iterator it;
+
+	Vector swarmPosition=this->getPosition();
+	Vector targetPosition=target->getAbsCoor();
+	float distanceToTarget=(swarmPosition-targetPosition).len();
+
+	float aMax=70.0f;
+	float vMax=1000.0f;
+	float deltaDistance=30;
+	float attackDistance=100;
+
+
+
+	if(distanceToTarget+deltaDistance<=attackDistance && status!=ATTACKING){
+		for (it= members.begin(); it!= members.end(); it++ ){
+			AIModule* oldAI = it->second;
+			AIModule* newAI = new AttackModule();
+			newAI->getAttributesFrom(oldAI);
+			it->second=newAI;
+			delete oldAI;
+			status=ATTACKING;
+		}
+	}else if(distanceToTarget-deltaDistance>=attackDistance && status!=MOVING){
+		for (it= members.begin(); it!= members.end(); it++ ){
+			AIModule* oldAI = it->second;
+			AIModule* newAI = new MovementModule();
+			newAI->getAttributesFrom(oldAI);
+			it->second=newAI;
+			delete oldAI;
+			status=MOVING;
+		}
+	}
+
+
+
+	if(status!=ATTACKING && tickCount>=randomFreq){
 		tickCount=0;
-		int x = (rand()%141)+10;				//10-150
+		//int x = (rand()%141)+10;				//10-150
+		int x = (rand()%241)-120;			//-120-120
 		int z = (rand()%241)-120;			//-120-120
 		randomVector=Vector(x,0,z);
 
-		std::cout << "change to: ||" << randomVector.x << "   ==" << randomVector.z << "\n";
+		//std::cout << "change to: ||" << randomVector.x << "   ==" << randomVector.z << "\n";
 	}
 	tickCount++;
 
 
-	std::set<AIModule*>::iterator it;
-	Vector swarmPosition=this->getPosition();
-	//std::cout << swarmPosition.x << " " << swarmPosition.z << "\n";
-
-	float aMax=70.0f;
-	float vMax=1000.0f;
 
 	Vector correction=(destination+randomVector-swarmPosition)-movement;
@@ -58,24 +92,30 @@
 
 
-	for (it= swarmMembers.begin(); it!= swarmMembers.end(); it++ ){
-		(*it)->setDestination(swarmPosition);
-		(*it)->setDestinationMovement(movement);
-		(*it)->setEnemyList(enemyList);
-		(*it)->process(dt);
+
+	for (it= members.begin(); it!= members.end(); it++ ){
+		it->second->setDestination(swarmPosition);
+		it->second->setDestinationMovement(movement);
+		it->second->setTarget(target);
+		it->second->process(dt);
 	}
 }
 
 
-void AISwarm::addAI(AIModule* aiModule)
+
+void AISwarm::addAI(WorldEntity* npc)
 {
-	swarmMembers.insert(aiModule);
+	std::pair< std::map<WorldEntity*,AIModule*>::iterator , bool > p;
+	AIModule* newAIModule=new MovementModule(npc);
+	p=members.insert(std::make_pair(npc,newAIModule));
+	if(!p.second)delete newAIModule;
 }
 
 
-void AISwarm::removeAI(AIModule* aiModule)
+void AISwarm::removeAI(WorldEntity* npc)
 {
-	std::set<AIModule*>::iterator it=swarmMembers.find(aiModule);
-	if(it==swarmMembers.end())return;
-	delete (*it);
+	std::map<WorldEntity*,AIModule*>::iterator it = members.find(npc);
+	if(it==members.end())return; 		//npc not found
+	delete it->second; 					//delete AIModule
+	members.erase(it); 					//remove AIModule from members
 }
 
@@ -84,8 +124,24 @@
 {
 	Vector center=Vector(0,0,0);
-	std::set<AIModule*>::iterator it;
-	for (it= swarmMembers.begin(); it!= swarmMembers.end(); it++ )
-		center=center+(*it)->getPosition();
+	std::map<WorldEntity*,AIModule*>::iterator it;
+	for (it= members.begin(); it!= members.end(); it++ )
+		center=center+it->second->getPosition();
 
-	return center/swarmMembers.size();
+	return center/members.size();
 }
+
+float AISwarm::getRadius(WorldEntity* object)
+{
+	AABB* aabb = object->getModelAABB();
+	if( aabb == NULL)return -1;
+
+	float a = aabb->halfLength[0];
+	float b = aabb->halfLength[1];
+	float c = aabb->halfLength[2];
+
+	if(a>b){
+		return (c>a)?c:a;
+	}else{
+		return (c>b)?c:b;
+	}
+}
Index: branches/ai/src/ai/ai_swarm.h
===================================================================
--- branches/ai/src/ai/ai_swarm.h	(revision 10225)
+++ branches/ai/src/ai/ai_swarm.h	(revision 10226)
@@ -6,4 +6,6 @@
 #include "ai_module.h"
 
+
+
 class AISwarm{
 	public:
@@ -11,15 +13,25 @@
 		~AISwarm(){}
 		void process(float dt);
-		void addAI(AIModule* newMember);
-		void removeAI(AIModule* oldMember);
-		inline int getSwarmSize(){return swarmMembers.size();}
-		inline void setEnemyList(std::vector<WorldEntity*>* enemyList){this->enemyList=enemyList;}
+		void addAI(WorldEntity*);
+		void removeAI(WorldEntity*);
+
+		inline int getSwarmSize(){return members.size();}
 		inline void setDestination(Vector destination){this->destination=destination;}
+		inline void setTarget(WorldEntity* target){this->target=target;}
 		Vector getPosition();
+
+		enum statusType{ATTACKING,MOVING,WAITING};
+
 	private:
+		float getRadius(WorldEntity* object);
+
+
 		Vector destination;
 		Vector movement;
-		std::vector<WorldEntity*>* enemyList;
-		std::set<AIModule*> swarmMembers;
+		WorldEntity* target;
+		statusType status;
+
+		std::map<WorldEntity*,AIModule*> members;
+
 		int tickCount;
 		int randomFreq;
Index: branches/ai/src/ai/ai_team.cc
===================================================================
--- branches/ai/src/ai/ai_team.cc	(revision 10225)
+++ branches/ai/src/ai/ai_team.cc	(revision 10226)
@@ -21,8 +21,8 @@
 	std::map<int,AISwarm*>::iterator it;
 	for (it= swarms.begin(); it!= swarms.end(); it++ ){
-
-		it->second->setEnemyList(enemyList);
-		if(enemyList->size()>0)
+		if(enemyList->size()>0){
+			it->second->setTarget(enemyList->at(0));
 			it->second->setDestination(enemyList->at(0)->getAbsCoor());
+		}
 		it->second->process(dt);
 	}
@@ -30,5 +30,5 @@
 
 
-void AITeam::addAI(int swarmNumber, AIModule* aiModule)
+void AITeam::addAI(int swarmNumber, WorldEntity* npc)
 {
 	std::pair<std::map<int,AISwarm*>::iterator,bool> p;
@@ -36,13 +36,13 @@
 	p=swarms.insert(std::make_pair(swarmNumber,newSwarm));
 	if(!p.second)delete newSwarm;
-	p.first->second->addAI(aiModule);
+	p.first->second->addAI(npc);
 }
 
 
-void AITeam::removeAI(int swarmNumber, AIModule* aiModule)
+void AITeam::removeAI(int swarmNumber, WorldEntity* npc)
 {
 	std::map<int,AISwarm*>::iterator it = swarms.find(swarmNumber);
 	if(it==swarms.end())return;
-	it->second->removeAI(aiModule);
+	it->second->removeAI(npc);
 	if(it->second->getSwarmSize()==0){
 		delete it->second;
Index: branches/ai/src/ai/ai_team.h
===================================================================
--- branches/ai/src/ai/ai_team.h	(revision 10225)
+++ branches/ai/src/ai/ai_team.h	(revision 10226)
@@ -11,6 +11,6 @@
 	std::vector<WorldEntity*>* getEnemyList();
 	void process(float dt);
-	void addAI(int swarmNumber, AIModule* aiModule);
-	void removeAI(int swarmNumber, AIModule* aiModule);
+	void addAI(int swarmNumber, WorldEntity* npc);
+	void removeAI(int swarmNumber, WorldEntity* npc);
 	inline int getTeamSize(){ return swarms.size(); }
 	inline void setEnemyList(std::vector<WorldEntity*>* enemyList){this->enemyList=enemyList;}
Index: branches/ai/src/ai/movement_module.cc
===================================================================
--- branches/ai/src/ai/movement_module.cc	(revision 10225)
+++ branches/ai/src/ai/movement_module.cc	(revision 10226)
@@ -21,5 +21,4 @@
 #include "player.h"
 #include "playable.h"
-#include "aabb.h"
 #include "npcs/npc_test.h"
 
@@ -50,25 +49,7 @@
 
 
-
-float MovementModule::getRadius(WorldEntity* object)
-{
-	AABB* aabb = object->getModelAABB();
-	if( aabb == NULL)return -1;
-
-	float a = aabb->halfLength[0];
-	float b = aabb->halfLength[1];
-	float c = aabb->halfLength[2];
-
-	if(a>b){
-		return (c>a)?c:a;
-	}else{
-		return (c>b)?c:b;
-	}
-}
-
-
 void MovementModule::process(float dt)
 {
-	if(myNPC == NULL)return;
+	if(npc == NULL)return;
 
 	Vector tmpVector;
@@ -76,9 +57,6 @@
 	Vector npcCollision;
 	Vector playerCollision;
-	bool autoRotate=true;
-	target=enemyList->at(0);
 
 	weight=1;
-	speedMax=1000.0f;
 
 
@@ -91,6 +69,10 @@
 
 	//get information about myself
-	Vector myPosition = myNPC->getAbsCoor();
-	float myRadius = getRadius(myNPC);
+	Vector myPosition = npc->getAbsCoor();
+	float myRadius = getRadius(npc);
+
+
+	float aMax=maxAccleration;
+	float vMax=800.0f/myRadius;
 
 
@@ -106,5 +88,5 @@
 	for(ObjectList<WorldEntity>::const_iterator it = WorldEntity::objectList().begin(); it != WorldEntity::objectList().end(); ++it)
 	{
-		if(*it==myNPC)continue;
+		if(*it==npc)continue;
 
 		tmpVector=myPosition-(*it)->getAbsCoor();
@@ -115,15 +97,4 @@
 
 		npcCollision=npcCollision+tmpVector;
-	}
-
-
-	//random movement
-	//randomFreq=testValue2;
-	if(++tickCount>=randomFreq && movement.len()<60){
-		tickCount=0;
-		int x = (rand()%101)-50;			//-50-50
-		int z = (rand()%101)-50;			//-50-50
-		randomVector=Vector(x,0,z);
-		randomFreq=(rand()%81)+70;			//70-150 Ticks
 	}
 
@@ -138,10 +109,4 @@
 								+	(vectorToDestination-movement)*3;
 
-	if(movement.len()<testValue2){
-	//if(testValue2){
-		correction=correction + randomVector * testValue;
-		autoRotate=false;
-	}
-
 	correction.y=0;
 
@@ -149,5 +114,5 @@
 	//limit accleration
 	float correctionLen=correction.len();
-	if(correctionLen>maxAccleration*dt)correction=correction/correctionLen*maxAccleration*dt;
+	if(correctionLen>aMax*dt)correction=correction/correctionLen*aMax*dt;
 	movement+=correction;
 
@@ -155,20 +120,14 @@
 	//limit speed
 	float movementLen=movement.len();
-	if(movementLen>speedMax)movement=movement/movementLen*speedMax;
+	if(movementLen>vMax)movement=movement/movementLen*vMax;
 
 
 	//move NPC...
-	myNPC->shiftCoor(movement * dt);
+	npc->shiftCoor(movement * dt);
 
 
 	//rotate NPC
-	Vector view;
-	if(autoRotate){
-		view = movement.cross( Vector(0,1,0) ).getNormalized();
-	}else{
-		view = target->getAbsCoor()-myPosition;
-		view = view.cross( Vector(0,1,0) ).getNormalized();
-	}
-	myNPC->setAbsDirSoft( Quaternion( view, Vector(0,1,0)),3);
+	view = movement.cross( Vector(0,1,0) ).getNormalized();
+	npc->setAbsDirSoft( Quaternion( view, Vector(0,1,0)),3);
 
 }
Index: branches/ai/src/ai/movement_module.h
===================================================================
--- branches/ai/src/ai/movement_module.h	(revision 10225)
+++ branches/ai/src/ai/movement_module.h	(revision 10226)
@@ -14,5 +14,5 @@
 	public:
 		MovementModule();
-		inline MovementModule(NPC2* object){ this->myNPC=object; this->myWorldEntity=(WorldEntity*)object;}
+		inline MovementModule(WorldEntity* object){this->npc=object;}
 		virtual ~MovementModule(){}
 		virtual void process(float dt);
@@ -29,6 +29,4 @@
 		float myMaxSpeed;
 
-		float getRadius(WorldEntity* object);
-
 		static float maxAccleration;
 		static float distanceToPlayer;
Index: branches/ai/src/world_entities/npcs/npc_test.cc
===================================================================
--- branches/ai/src/world_entities/npcs/npc_test.cc	(revision 10225)
+++ branches/ai/src/world_entities/npcs/npc_test.cc	(revision 10226)
@@ -51,11 +51,12 @@
 	std::cout << "Team Number: " << teamNumber << "\n";
 	std::cout << "Swarm Number:" << swarmNumber << "\n";
-	aiModule=new MovementModule(this);
-	AIEngine::getInstance()->addAI(teamNumber,swarmNumber,aiModule);
+	//aiModule=new MovementModule(this);
+	//AIEngine::getInstance()->addAI(teamNumber,swarmNumber,aiModule);
+	AIEngine::getInstance()->addAI(teamNumber,swarmNumber,(WorldEntity*)this);
 }
 
 NPC2::~NPC2()
 {
-	AIEngine::getInstance()->removeAI(teamNumber,swarmNumber,aiModule);
+	AIEngine::getInstance()->removeAI(teamNumber,swarmNumber,(WorldEntity*)this);
 }
 
