| 1 | /* |
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2004 orx |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Thomas Fahrni |
|---|
| 13 | co-programmer: |
|---|
| 14 | */ |
|---|
| 15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_AI |
|---|
| 16 | #include "ai_swarm.h" |
|---|
| 17 | #include "ai_module.h" |
|---|
| 18 | #include "movement_module.h" |
|---|
| 19 | #include "swarm_module.h" |
|---|
| 20 | #include "debug.h" |
|---|
| 21 | #include <stdio.h> |
|---|
| 22 | #include "aabb.h" |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | void SwarmModule::addAI(WorldEntity* npc, float maxSpeed, float attackDistance) |
|---|
| 27 | { |
|---|
| 28 | std::pair< std::map<WorldEntity*,AIModule*>::iterator , bool > p; |
|---|
| 29 | AIModule* newAIModule=new MovementModule(npc); |
|---|
| 30 | newAIModule->setAttackDistance(attackDistance); |
|---|
| 31 | newAIModule->setMaxSpeed(maxSpeed); |
|---|
| 32 | this->attackDistance=attackDistance; |
|---|
| 33 | this->maxSpeed=maxSpeed; |
|---|
| 34 | p=members.insert(std::make_pair(npc,newAIModule)); |
|---|
| 35 | if(!p.second)delete newAIModule; |
|---|
| 36 | |
|---|
| 37 | this->initialize(); |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | void SwarmModule::removeAI(WorldEntity* npc) |
|---|
| 41 | { |
|---|
| 42 | std::map<WorldEntity*,AIModule*>::iterator it = members.find(npc); |
|---|
| 43 | if(it==members.end())return; //npc not found |
|---|
| 44 | delete it->second; //delete AIModule |
|---|
| 45 | members.erase(it); //remove AIModule from members |
|---|
| 46 | |
|---|
| 47 | this->initialize(); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | Vector SwarmModule::getSwarmPosition() |
|---|
| 51 | { |
|---|
| 52 | Vector center=Vector(0,0,0); |
|---|
| 53 | std::map<WorldEntity*,AIModule*>::iterator it; |
|---|
| 54 | for (it= members.begin(); it!= members.end(); it++ ) |
|---|
| 55 | center=center+it->second->getPosition(); |
|---|
| 56 | |
|---|
| 57 | return center/members.size(); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | float SwarmModule::getRadius(WorldEntity* object) |
|---|
| 61 | { |
|---|
| 62 | AABB* aabb = object->getModelAABB(); |
|---|
| 63 | if( aabb == NULL)return -1; |
|---|
| 64 | |
|---|
| 65 | float a = aabb->halfLength[0]; |
|---|
| 66 | float b = aabb->halfLength[1]; |
|---|
| 67 | float c = aabb->halfLength[2]; |
|---|
| 68 | |
|---|
| 69 | if(a>b){ |
|---|
| 70 | return (c>a)?c:a; |
|---|
| 71 | }else{ |
|---|
| 72 | return (c>b)?c:b; |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | void SwarmModule::getAttributesFrom(SwarmModule* other) |
|---|
| 77 | { |
|---|
| 78 | this->members=other->getMembers(); |
|---|
| 79 | this->speed=other->getSpeed(); |
|---|
| 80 | this->view=other->getView(); |
|---|
| 81 | this->enemys=other->getEnemyList(); |
|---|
| 82 | this->position=other->getPosition(); |
|---|
| 83 | this->maxSpeed=other->getMaxSpeed(); |
|---|
| 84 | this->attackDistance=other->getAttackDistance(); |
|---|
| 85 | |
|---|
| 86 | this->initialize(); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | void SwarmModule::changeAIModule(std::map<WorldEntity*,AIModule*>::iterator it,AIModule* newAI) |
|---|
| 90 | { |
|---|
| 91 | AIModule* oldAI = it->second; |
|---|
| 92 | newAI->getAttributesFrom(oldAI); |
|---|
| 93 | it->second=newAI; |
|---|
| 94 | delete oldAI; |
|---|
| 95 | } |
|---|