/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Thomas Fahrni co-programmer: */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_AI #include "ai_swarm.h" #include "ai_module.h" #include "movement_module.h" #include "swarm_module.h" #include "debug.h" #include #include "aabb.h" void SwarmModule::addAI(WorldEntity* npc) { std::pair< std::map::iterator , bool > p; AIModule* newAIModule=new MovementModule(npc); p=members.insert(std::make_pair(npc,newAIModule)); if(!p.second)delete newAIModule; this->initialize(); } void SwarmModule::removeAI(WorldEntity* npc) { std::map::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 this->initialize(); } Vector SwarmModule::getSwarmPosition() { Vector center=Vector(0,0,0); std::map::iterator it; for (it= members.begin(); it!= members.end(); it++ ) center=center+it->second->getPosition(); return center/members.size(); } float SwarmModule::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 SwarmModule::getAttributesFrom(SwarmModule* other) { this->members=other->getMembers(); this->speed=other->getSpeed(); this->view=other->getView(); this->enemys=other->getEnemyList(); this->position=other->getPosition(); this->initialize(); } void SwarmModule::changeAIModule(std::map::iterator it,AIModule* newAI) { AIModule* oldAI = it->second; newAI->getAttributesFrom(oldAI); it->second=newAI; delete oldAI; }