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_swarm.h" |
---|
17 | #include "debug.h" |
---|
18 | #include <stdio.h> |
---|
19 | |
---|
20 | void AISwarm::process(float dt) |
---|
21 | { |
---|
22 | std::set<AIModule*>::iterator it; |
---|
23 | Vector swarmPosition=this->getPosition(); |
---|
24 | //std::cout << swarmPosition.x << " " << swarmPosition.z << "\n"; |
---|
25 | |
---|
26 | float aMax=70.0f; |
---|
27 | float vMax=1000.0f; |
---|
28 | |
---|
29 | Vector correction=(destination-swarmPosition)-movement; |
---|
30 | correction.y=0; |
---|
31 | |
---|
32 | float correctionLen=correction.len(); |
---|
33 | if(correctionLen>aMax*dt)correction=correction/correctionLen*aMax*dt; |
---|
34 | |
---|
35 | //std::cout << angleRad(correction,movement) << "\n"; |
---|
36 | movement+=correction; |
---|
37 | |
---|
38 | float movementLen=movement.len(); |
---|
39 | if(movementLen>vMax)movement=movement/movementLen*vMax; |
---|
40 | |
---|
41 | |
---|
42 | for (it= swarmMembers.begin(); it!= swarmMembers.end(); it++ ){ |
---|
43 | (*it)->setDestination(swarmPosition); |
---|
44 | (*it)->setDestinationMovement(movement); |
---|
45 | (*it)->setEnemyList(enemyList); |
---|
46 | (*it)->process(dt); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | void AISwarm::addAI(AIModule* aiModule) |
---|
51 | { |
---|
52 | swarmMembers.insert(aiModule); |
---|
53 | } |
---|
54 | |
---|
55 | void AISwarm::removeAI(AIModule* aiModule) |
---|
56 | { |
---|
57 | std::set<AIModule*>::iterator it=swarmMembers.find(aiModule); |
---|
58 | if(it==swarmMembers.end())return; |
---|
59 | delete (*it); |
---|
60 | } |
---|
61 | |
---|
62 | Vector AISwarm::getPosition() |
---|
63 | { |
---|
64 | Vector center=Vector(0,0,0); |
---|
65 | std::set<AIModule*>::iterator it; |
---|
66 | for (it= swarmMembers.begin(); it!= swarmMembers.end(); it++ ) |
---|
67 | center=center+(*it)->getPosition(); |
---|
68 | |
---|
69 | return center/swarmMembers.size(); |
---|
70 | } |
---|