Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/ai/src/ai/ai_team.cc @ 10266

Last change on this file since 10266 was 10266, checked in by tfahrni, 17 years ago

fixed segfault

File size: 3.3 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_team.h"
17#include "swarm_gorel.h"
18#include "swarm_module.h"
19#include "swarm_wait.h"
20#include "swarm_attack.h"
21#include "debug.h"
22#include "player.h"
23#include "playable.h"
24#include "state.h"
25
26void AITeam::process(float dt)
27{
28        std::map<int,SwarmModule*>::iterator it;
29        for (it= swarms.begin(); it!= swarms.end(); it++ ){
30
31                if(it->second->taskDone()){
32                        std::cout << "Swarm Task Complete!\n";
33
34                        if(enemyList->size()==0){
35                                changeSwarmModule(it, new SwarmWait);
36                                it->second->orderMaxTime(2);    //sleep 2 seconds..
37                                it->second->process(dt);
38                                continue;
39                        }
40
41                        Vector position=it->second->getPosition();
42                        Vector newPosition;
43                        WorldEntity* target=enemyList->at(0);
44
45                        //check if enemy is the player..
46                        bool isPlayer=(State::getPlayer()->getPlayable()==target);
47                        float speed;
48                        float maxTime=10;
49
50                        //find new Position
51                        if(isPlayer){
52                                Vector targetPos=target->getAbsCoor();
53                                int zNorm=(position.z>targetPos.z)?1:-1;
54
55                                if((position.z-targetPos.z)*zNorm>80){  //go to start position
56                                        std::cout << "Go Start Position\n";
57                                        changeSwarmModule(it, new SwarmGoRel);
58                                        newPosition=Vector(220,0,zNorm*60);
59                                        speed=100;
60                                }else if(position.x > targetPos.x+150){ //go to attack position
61                                        std::cout << "Go Attack Position\n";
62                                        changeSwarmModule(it, new SwarmGoRel);
63                                        newPosition=Vector(100,0,0);
64                                        speed=60;
65                                }else if(position.x > targetPos.x+90){  //go to attack mode
66                                        std::cout << "Go Attack Mode\n";
67                                        changeSwarmModule(it, new SwarmAttack);
68                                        newPosition=Vector(80,0,0);
69                                        speed=80;
70                                        maxTime=(rand()%11);//0-10 Sekunden
71                                }else{                                                                                          //go to fallback point
72                                        std::cout << "Go  Fallback Point\n";
73                                        changeSwarmModule(it, new SwarmGoRel);
74                                        newPosition=Vector(0,0,zNorm*150);
75                                        speed=60;
76                                }
77                        }else{
78
79                        }
80
81
82
83                        if(enemyList->size()>0){
84                                it->second->setEnemyList(enemyList);
85                                it->second->orderRelObject(target);
86                                it->second->orderRelPos(newPosition);
87                                it->second->orderSpeed(speed);
88                                //it->second->orderView(Vector(0,0,1));
89                                it->second->orderMaxTime(maxTime); //5-10
90                                //it->second->newOrder();
91                        }
92                }
93                it->second->process(dt);
94        }
95}
96
97void AITeam::changeSwarmModule(std::map<int,SwarmModule*>::iterator it,SwarmModule* newAI)
98{
99                        SwarmModule* oldAI = it->second;
100                        newAI->getAttributesFrom(oldAI);
101                        it->second=newAI;
102                        delete oldAI;
103}
104
105
106void AITeam::addAI(int swarmNumber, WorldEntity* npc)
107{
108        std::pair<std::map<int,SwarmModule*>::iterator,bool> p;
109        SwarmModule* newSwarm=new SwarmGoRel();
110        p=swarms.insert(std::make_pair(swarmNumber,newSwarm));
111        if(!p.second)delete newSwarm;
112        p.first->second->addAI(npc);
113}
114
115
116void AITeam::removeAI(int swarmNumber, WorldEntity* npc)
117{
118        std::map<int,SwarmModule*>::iterator it = swarms.find(swarmNumber);
119        if(it==swarms.end())return;
120        it->second->removeAI(npc);
121        if(it->second->getSwarmSize()==0){
122                delete it->second;
123                swarms.erase(it);
124        }
125}
Note: See TracBrowser for help on using the repository browser.