| 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: |
|---|
| 12 | main-programmer: Thomas Fahrni |
|---|
| 13 | co-programmer: |
|---|
| 14 | */ |
|---|
| 15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_AI |
|---|
| 16 | #include "ai_engine.h" |
|---|
| 17 | #include "debug.h" |
|---|
| 18 | #include "npcs/npc_test.h" |
|---|
| 19 | #include "player.h" |
|---|
| 20 | #include "playable.h" |
|---|
| 21 | #include "state.h" |
|---|
| 22 | |
|---|
| 23 | AIEngine* AIEngine::singletonRef = NULL; |
|---|
| 24 | |
|---|
| 25 | void AIEngine::tick(float dt) |
|---|
| 26 | { |
|---|
| 27 | std::map<int,AITeam*>::iterator it; |
|---|
| 28 | std::vector<WorldEntity*>* enemyList=new std::vector<WorldEntity*>; |
|---|
| 29 | |
|---|
| 30 | for (it=teams.begin(); it!= teams.end(); it++ ) |
|---|
| 31 | { |
|---|
| 32 | //add player to enemys (player belongs to team 0) |
|---|
| 33 | if(it->first!=0){ |
|---|
| 34 | Player* pl = State::getPlayer(); |
|---|
| 35 | if( pl!=NULL && pl->getPlayable()!=NULL ) |
|---|
| 36 | enemyList->push_back(pl->getPlayable()); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | //find other enemys for this team |
|---|
| 40 | for(ObjectList<NPC2>::const_iterator npc = NPC2::objectList().begin(); npc != NPC2::objectList().end(); ++npc) |
|---|
| 41 | if((*npc)->getTeam() != it->first)enemyList->push_back(*npc); |
|---|
| 42 | |
|---|
| 43 | //process the team |
|---|
| 44 | it->second->setEnemyList(enemyList); |
|---|
| 45 | it->second->process(dt); |
|---|
| 46 | enemyList->clear(); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | delete enemyList; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | void AIEngine::addAI(int teamNumber, int swarmNumber, WorldEntity* npc, float maxSpeed, float attackDistance) |
|---|
| 53 | { |
|---|
| 54 | std::pair<std::map<int,AITeam*>::iterator,bool> p; |
|---|
| 55 | AITeam* newTeam=new AITeam(); |
|---|
| 56 | p=teams.insert(std::make_pair(teamNumber,newTeam)); |
|---|
| 57 | if(!p.second)delete newTeam; |
|---|
| 58 | p.first->second->addAI(swarmNumber, npc, maxSpeed, attackDistance); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | void AIEngine::removeAI(int teamNumber, int swarmNumber, WorldEntity* npc) |
|---|
| 62 | { |
|---|
| 63 | std::map<int,AITeam*>::iterator it = teams.find(teamNumber); |
|---|
| 64 | if(it==teams.end())return; |
|---|
| 65 | it->second->removeAI(swarmNumber,npc); |
|---|
| 66 | if(it->second->getTeamSize()==0){ |
|---|
| 67 | delete it->second; |
|---|
| 68 | teams.erase(it); |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | |
|---|