Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/ai/src/ai/swarm_module.cc @ 10249

Last change on this file since 10249 was 10249, checked in by tfahrni, 17 years ago
File size: 2.1 KB
Line 
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
26void SwarmModule::addAI(WorldEntity* npc)
27{
28        std::pair< std::map<WorldEntity*,AIModule*>::iterator , bool > p;
29        AIModule* newAIModule=new MovementModule(npc);
30        p=members.insert(std::make_pair(npc,newAIModule));
31        if(!p.second)delete newAIModule;
32       
33        this->initialize();
34}
35
36void SwarmModule::removeAI(WorldEntity* npc)
37{
38        std::map<WorldEntity*,AIModule*>::iterator it = members.find(npc);
39        if(it==members.end())return;            //npc not found
40        delete it->second;                                      //delete AIModule
41        members.erase(it);                                      //remove AIModule from members
42       
43        this->initialize();
44}
45
46Vector SwarmModule::getSwarmPosition()
47{
48        Vector center=Vector(0,0,0);
49        std::map<WorldEntity*,AIModule*>::iterator it;
50        for (it= members.begin(); it!= members.end(); it++ )
51                center=center+it->second->getPosition();
52
53        return center/members.size();
54}
55
56float SwarmModule::getRadius(WorldEntity* object)
57{
58        AABB* aabb = object->getModelAABB();
59        if( aabb == NULL)return -1;
60
61        float a = aabb->halfLength[0];
62        float b = aabb->halfLength[1];
63        float c = aabb->halfLength[2];
64
65        if(a>b){
66                return (c>a)?c:a;
67        }else{
68                return (c>b)?c:b;
69        }
70}
71
72void SwarmModule::getAttributesFrom(SwarmModule* other)
73{
74        this->members=other->getMembers();
75        this->speed=other->getSpeed();
76        this->view=other->getView();
77        this->enemys=other->getEnemyList();
78        this->position=other->getPosition();
79       
80        this->initialize();
81}
82
83void SwarmModule::changeAIModule(std::map<WorldEntity*,AIModule*>::iterator it,AIModule* newAI)
84{
85                AIModule* oldAI = it->second;
86                newAI->getAttributesFrom(oldAI);
87                it->second=newAI;
88                delete oldAI;
89}
Note: See TracBrowser for help on using the repository browser.