[10029] | 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: |
---|
[10045] | 12 | main-programmer: Thomas Fahrni |
---|
[10029] | 13 | co-programmer: |
---|
| 14 | */ |
---|
[10041] | 15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_AI |
---|
[10029] | 16 | #include "ai_team.h" |
---|
[10244] | 17 | #include "swarm_gorel.h" |
---|
| 18 | #include "swarm_module.h" |
---|
[10249] | 19 | #include "swarm_wait.h" |
---|
[10266] | 20 | #include "swarm_attack.h" |
---|
[10137] | 21 | #include "debug.h" |
---|
[10249] | 22 | #include "player.h" |
---|
| 23 | #include "playable.h" |
---|
| 24 | #include "state.h" |
---|
[10029] | 25 | |
---|
[10137] | 26 | void AITeam::process(float dt) |
---|
[10041] | 27 | { |
---|
[10244] | 28 | std::map<int,SwarmModule*>::iterator it; |
---|
[10138] | 29 | for (it= swarms.begin(); it!= swarms.end(); it++ ){ |
---|
[10266] | 30 | |
---|
[10244] | 31 | if(it->second->taskDone()){ |
---|
[10349] | 32 | //std::cout << "Swarm Task Complete!\n"; |
---|
[10266] | 33 | |
---|
[10249] | 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 | } |
---|
[10266] | 40 | |
---|
[10249] | 41 | Vector position=it->second->getPosition(); |
---|
| 42 | Vector newPosition; |
---|
| 43 | WorldEntity* target=enemyList->at(0); |
---|
[10266] | 44 | |
---|
[10249] | 45 | //check if enemy is the player.. |
---|
| 46 | bool isPlayer=(State::getPlayer()->getPlayable()==target); |
---|
| 47 | float speed; |
---|
[10266] | 48 | float maxTime=10; |
---|
| 49 | |
---|
[10275] | 50 | |
---|
[10249] | 51 | //find new Position |
---|
| 52 | if(isPlayer){ |
---|
[10349] | 53 | float attackDistance=it->second->getAttackDistance(); |
---|
| 54 | |
---|
[10266] | 55 | Vector targetPos=target->getAbsCoor(); |
---|
| 56 | int zNorm=(position.z>targetPos.z)?1:-1; |
---|
| 57 | |
---|
[10275] | 58 | if((position.z-targetPos.z)*zNorm>60){ //go to start position |
---|
[10349] | 59 | //std::cout << "Go Start Position\n"; |
---|
[10266] | 60 | changeSwarmModule(it, new SwarmGoRel); |
---|
[10283] | 61 | zNorm=1-(rand()%2)*2; //1 or -1 |
---|
[10349] | 62 | newPosition=Vector(attackDistance+60,0,zNorm*10); |
---|
[10283] | 63 | speed=60; |
---|
[10349] | 64 | }else if(position.x > targetPos.x+attackDistance+40){ //go to attack position |
---|
| 65 | //std::cout << "Go Attack Position\n"; |
---|
[10266] | 66 | changeSwarmModule(it, new SwarmGoRel); |
---|
[10349] | 67 | newPosition=Vector(attackDistance+30,0,0); |
---|
[10283] | 68 | speed=60; |
---|
[10349] | 69 | }else if(position.x > targetPos.x+attackDistance+20){ //go to attack mode |
---|
| 70 | //std::cout << "Go Attack Mode\n"; |
---|
[10266] | 71 | changeSwarmModule(it, new SwarmAttack); |
---|
[10349] | 72 | newPosition=Vector(attackDistance,0,0); |
---|
[10283] | 73 | speed=60; |
---|
[10290] | 74 | maxTime=(rand()%11)+4;//4-14 Sekunden |
---|
[10266] | 75 | }else{ //go to fallback point |
---|
[10349] | 76 | //std::cout << "Go Fallback Point\n"; |
---|
[10266] | 77 | changeSwarmModule(it, new SwarmGoRel); |
---|
[10283] | 78 | newPosition=Vector(80,0,zNorm*90); |
---|
[10275] | 79 | speed=80; |
---|
[10249] | 80 | } |
---|
| 81 | }else{ |
---|
[10266] | 82 | |
---|
[10249] | 83 | } |
---|
| 84 | |
---|
[10283] | 85 | speed=0; |
---|
[10266] | 86 | |
---|
[10244] | 87 | if(enemyList->size()>0){ |
---|
| 88 | it->second->setEnemyList(enemyList); |
---|
[10249] | 89 | it->second->orderRelObject(target); |
---|
| 90 | it->second->orderRelPos(newPosition); |
---|
| 91 | it->second->orderSpeed(speed); |
---|
| 92 | //it->second->orderView(Vector(0,0,1)); |
---|
[10266] | 93 | it->second->orderMaxTime(maxTime); //5-10 |
---|
[10249] | 94 | //it->second->newOrder(); |
---|
[10244] | 95 | } |
---|
[10226] | 96 | } |
---|
[10137] | 97 | it->second->process(dt); |
---|
[10138] | 98 | } |
---|
[10041] | 99 | } |
---|
| 100 | |
---|
[10244] | 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 | } |
---|
[10177] | 108 | |
---|
[10244] | 109 | |
---|
[10349] | 110 | void AITeam::addAI(int swarmNumber, WorldEntity* npc, float maxSpeed, float attackDistance) |
---|
[10041] | 111 | { |
---|
[10244] | 112 | std::pair<std::map<int,SwarmModule*>::iterator,bool> p; |
---|
| 113 | SwarmModule* newSwarm=new SwarmGoRel(); |
---|
[10137] | 114 | p=swarms.insert(std::make_pair(swarmNumber,newSwarm)); |
---|
| 115 | if(!p.second)delete newSwarm; |
---|
[10349] | 116 | p.first->second->addAI(npc, maxSpeed, attackDistance); |
---|
[10041] | 117 | } |
---|
| 118 | |
---|
[10177] | 119 | |
---|
[10226] | 120 | void AITeam::removeAI(int swarmNumber, WorldEntity* npc) |
---|
[10041] | 121 | { |
---|
[10244] | 122 | std::map<int,SwarmModule*>::iterator it = swarms.find(swarmNumber); |
---|
[10137] | 123 | if(it==swarms.end())return; |
---|
[10226] | 124 | it->second->removeAI(npc); |
---|
[10137] | 125 | if(it->second->getSwarmSize()==0){ |
---|
| 126 | delete it->second; |
---|
| 127 | swarms.erase(it); |
---|
| 128 | } |
---|
[10041] | 129 | } |
---|