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 | |
---|
26 | void 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 | |
---|
51 | //find new Position |
---|
52 | if(isPlayer){ |
---|
53 | float attackDistance=it->second->getAttackDistance(); |
---|
54 | |
---|
55 | Vector targetPos=target->getAbsCoor(); |
---|
56 | float distanceToPlayer=(targetPos-position).len(); |
---|
57 | // int zNorm=(position.z>targetPos.z)?1:-1; // unused var |
---|
58 | |
---|
59 | if(distanceToPlayer<attackDistance+60){ //### change wakeup distance here |
---|
60 | std::cout << "AI Sleeps \n"; |
---|
61 | changeSwarmModule(it, new SwarmWait); |
---|
62 | maxTime=1000; //sleep 2 seconds.. |
---|
63 | }else{ |
---|
64 | /*if((position.z-targetPos.z)*zNorm>60){ //go to start position |
---|
65 | std::cout << "Go Start Position\n"; |
---|
66 | changeSwarmModule(it, new SwarmGoRel); |
---|
67 | zNorm=1-(rand()%2)*2; //1 or -1 |
---|
68 | newPosition=Vector(attackDistance+60,0,zNorm*10); |
---|
69 | speed=60; |
---|
70 | }else if(position.x > targetPos.x+attackDistance+40){ //go to attack position |
---|
71 | std::cout << "Go Attack Position\n"; |
---|
72 | changeSwarmModule(it, new SwarmGoRel); |
---|
73 | newPosition=Vector(attackDistance+30,0,0) |
---|
74 | speed=60; |
---|
75 | }else if(position.x > targetPos.x+attackDistance+20){ //go to attack mode; |
---|
76 | std::cout << "Go Attack Mode \n";*/ |
---|
77 | std::cout << "AI Attacks \n"; |
---|
78 | changeSwarmModule(it, new SwarmAttack); |
---|
79 | newPosition=Vector(attackDistance,0,0); |
---|
80 | speed=60; |
---|
81 | maxTime=(1000);//rand()%11)+4;//4-14 Sekunden |
---|
82 | /*}else{ //go to fallback point |
---|
83 | std::cout << "Go Fallback Point\n"; |
---|
84 | changeSwarmModule(it, new SwarmGoRel); |
---|
85 | newPosition=Vector(80,0,zNorm*90); |
---|
86 | speed=80;*/ |
---|
87 | } |
---|
88 | //}else{ |
---|
89 | |
---|
90 | } |
---|
91 | |
---|
92 | speed=0; |
---|
93 | |
---|
94 | if(enemyList->size()>0){ |
---|
95 | it->second->setEnemyList(enemyList); |
---|
96 | it->second->orderRelObject(target); |
---|
97 | it->second->orderRelPos(newPosition); |
---|
98 | it->second->orderSpeed(speed); |
---|
99 | //it->second->orderView(Vector(0,0,1)); |
---|
100 | it->second->orderMaxTime(maxTime); //5-10 |
---|
101 | //it->second->newOrder(); |
---|
102 | } |
---|
103 | } |
---|
104 | it->second->process(dt); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | void AITeam::changeSwarmModule(std::map<int,SwarmModule*>::iterator it,SwarmModule* newAI) |
---|
109 | { |
---|
110 | SwarmModule* oldAI = it->second; |
---|
111 | newAI->getAttributesFrom(oldAI); |
---|
112 | it->second=newAI; |
---|
113 | delete oldAI; |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | void AITeam::addAI(int swarmNumber, WorldEntity* npc, float maxSpeed, float attackDistance) |
---|
118 | { |
---|
119 | std::pair<std::map<int,SwarmModule*>::iterator,bool> p; |
---|
120 | SwarmModule* newSwarm=new SwarmGoRel(); |
---|
121 | p=swarms.insert(std::make_pair(swarmNumber,newSwarm)); |
---|
122 | if(!p.second)delete newSwarm; |
---|
123 | p.first->second->addAI(npc, maxSpeed, attackDistance); |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | void AITeam::removeAI(int swarmNumber, WorldEntity* npc) |
---|
128 | { |
---|
129 | std::map<int,SwarmModule*>::iterator it = swarms.find(swarmNumber); |
---|
130 | if(it==swarms.end())return; |
---|
131 | it->second->removeAI(npc); |
---|
132 | if(it->second->getSwarmSize()==0){ |
---|
133 | delete it->second; |
---|
134 | swarms.erase(it); |
---|
135 | } |
---|
136 | } |
---|