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