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