[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(); |
---|
[10515] | 56 | float distanceToPlayer=(targetPos-position).len(); |
---|
[10730] | 57 | // int zNorm=(position.z>targetPos.z)?1:-1; // unused var |
---|
[10515] | 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"; |
---|
[10266] | 66 | changeSwarmModule(it, new SwarmGoRel); |
---|
[10283] | 67 | zNorm=1-(rand()%2)*2; //1 or -1 |
---|
[10349] | 68 | newPosition=Vector(attackDistance+60,0,zNorm*10); |
---|
[10283] | 69 | speed=60; |
---|
[10349] | 70 | }else if(position.x > targetPos.x+attackDistance+40){ //go to attack position |
---|
[10515] | 71 | std::cout << "Go Attack Position\n"; |
---|
[10266] | 72 | changeSwarmModule(it, new SwarmGoRel); |
---|
[10515] | 73 | newPosition=Vector(attackDistance+30,0,0) |
---|
[10283] | 74 | speed=60; |
---|
[10515] | 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"; |
---|
[10266] | 78 | changeSwarmModule(it, new SwarmAttack); |
---|
[10349] | 79 | newPosition=Vector(attackDistance,0,0); |
---|
[10283] | 80 | speed=60; |
---|
[10515] | 81 | maxTime=(1000);//rand()%11)+4;//4-14 Sekunden |
---|
| 82 | /*}else{ //go to fallback point |
---|
| 83 | std::cout << "Go Fallback Point\n"; |
---|
[10266] | 84 | changeSwarmModule(it, new SwarmGoRel); |
---|
[10283] | 85 | newPosition=Vector(80,0,zNorm*90); |
---|
[10515] | 86 | speed=80;*/ |
---|
[10249] | 87 | } |
---|
[10515] | 88 | //}else{ |
---|
[10266] | 89 | |
---|
[10249] | 90 | } |
---|
| 91 | |
---|
[10283] | 92 | speed=0; |
---|
[10266] | 93 | |
---|
[10244] | 94 | if(enemyList->size()>0){ |
---|
| 95 | it->second->setEnemyList(enemyList); |
---|
[10249] | 96 | it->second->orderRelObject(target); |
---|
| 97 | it->second->orderRelPos(newPosition); |
---|
| 98 | it->second->orderSpeed(speed); |
---|
| 99 | //it->second->orderView(Vector(0,0,1)); |
---|
[10266] | 100 | it->second->orderMaxTime(maxTime); //5-10 |
---|
[10249] | 101 | //it->second->newOrder(); |
---|
[10244] | 102 | } |
---|
[10226] | 103 | } |
---|
[10137] | 104 | it->second->process(dt); |
---|
[10138] | 105 | } |
---|
[10041] | 106 | } |
---|
| 107 | |
---|
[10244] | 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 | } |
---|
[10177] | 115 | |
---|
[10244] | 116 | |
---|
[10349] | 117 | void AITeam::addAI(int swarmNumber, WorldEntity* npc, float maxSpeed, float attackDistance) |
---|
[10041] | 118 | { |
---|
[10244] | 119 | std::pair<std::map<int,SwarmModule*>::iterator,bool> p; |
---|
| 120 | SwarmModule* newSwarm=new SwarmGoRel(); |
---|
[10137] | 121 | p=swarms.insert(std::make_pair(swarmNumber,newSwarm)); |
---|
| 122 | if(!p.second)delete newSwarm; |
---|
[10349] | 123 | p.first->second->addAI(npc, maxSpeed, attackDistance); |
---|
[10041] | 124 | } |
---|
| 125 | |
---|
[10177] | 126 | |
---|
[10226] | 127 | void AITeam::removeAI(int swarmNumber, WorldEntity* npc) |
---|
[10041] | 128 | { |
---|
[10244] | 129 | std::map<int,SwarmModule*>::iterator it = swarms.find(swarmNumber); |
---|
[10137] | 130 | if(it==swarms.end())return; |
---|
[10226] | 131 | it->second->removeAI(npc); |
---|
[10137] | 132 | if(it->second->getSwarmSize()==0){ |
---|
| 133 | delete it->second; |
---|
| 134 | swarms.erase(it); |
---|
| 135 | } |
---|
[10041] | 136 | } |
---|