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