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