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