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