Index: branches/ai/src/ai/ai_swarm.cc
===================================================================
--- branches/ai/src/ai/ai_swarm.cc	(revision 10177)
+++ 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;
+	}
+}
