| 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 "swarm_gorel.h" | 
|---|
| 17 | #include "ai_module.h" | 
|---|
| 18 | #include "movement_module.h" | 
|---|
| 19 | #include "attack_module.h" | 
|---|
| 20 | #include "debug.h" | 
|---|
| 21 | #include <stdio.h> | 
|---|
| 22 | #include "aabb.h" | 
|---|
| 23 |  | 
|---|
| 24 | AISwarm::AISwarm() | 
|---|
| 25 | { | 
|---|
| 26 | tickCount=0; | 
|---|
| 27 | randomFreq=100; | 
|---|
| 28 | } | 
|---|
| 29 |  | 
|---|
| 30 |  | 
|---|
| 31 | void AISwarm::process(float dt) | 
|---|
| 32 | { | 
|---|
| 33 | std::map<WorldEntity*,AIModule*>::iterator it; | 
|---|
| 34 |  | 
|---|
| 35 | if(enemys==NULL || enemys->size()<1)return; | 
|---|
| 36 | target=enemys->at(0); | 
|---|
| 37 |  | 
|---|
| 38 | Vector swarmPosition=this->getPosition(); | 
|---|
| 39 | Vector targetPosition=target->getAbsCoor(); | 
|---|
| 40 | float distanceToTarget=(swarmPosition-targetPosition).len(); | 
|---|
| 41 |  | 
|---|
| 42 | float aMax=70.0f; | 
|---|
| 43 | float vMax=1000.0f; | 
|---|
| 44 | float deltaDistance=30; | 
|---|
| 45 | float attackDistance=100; | 
|---|
| 46 |  | 
|---|
| 47 |  | 
|---|
| 48 |  | 
|---|
| 49 | if(distanceToTarget+deltaDistance<=attackDistance && status!=ATTACKING){ | 
|---|
| 50 | for (it= members.begin(); it!= members.end(); it++ ){ | 
|---|
| 51 | AIModule* oldAI = it->second; | 
|---|
| 52 | AIModule* newAI = new AttackModule(); | 
|---|
| 53 | newAI->getAttributesFrom(oldAI); | 
|---|
| 54 | it->second=newAI; | 
|---|
| 55 | delete oldAI; | 
|---|
| 56 | status=ATTACKING; | 
|---|
| 57 | } | 
|---|
| 58 | }else if(distanceToTarget-deltaDistance>=attackDistance && status!=MOVING){ | 
|---|
| 59 | for (it= members.begin(); it!= members.end(); it++ ){ | 
|---|
| 60 | AIModule* oldAI = it->second; | 
|---|
| 61 | AIModule* newAI = new MovementModule(); | 
|---|
| 62 | newAI->getAttributesFrom(oldAI); | 
|---|
| 63 | it->second=newAI; | 
|---|
| 64 | delete oldAI; | 
|---|
| 65 | status=MOVING; | 
|---|
| 66 | } | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 |  | 
|---|
| 70 |  | 
|---|
| 71 | if(status!=ATTACKING && tickCount>=randomFreq){ | 
|---|
| 72 | tickCount=0; | 
|---|
| 73 | //int x = (rand()%141)+10;                              //10-150 | 
|---|
| 74 | int x = (rand()%241)-120;                       //-120-120 | 
|---|
| 75 | int z = (rand()%241)-120;                       //-120-120 | 
|---|
| 76 | randomVector=Vector(x,0,z); | 
|---|
| 77 |  | 
|---|
| 78 | //std::cout << "change to: ||" << randomVector.x << "   ==" << randomVector.z << "\n"; | 
|---|
| 79 | } | 
|---|
| 80 | tickCount++; | 
|---|
| 81 |  | 
|---|
| 82 |  | 
|---|
| 83 | destination=taskRelObject->getAbsCoor()+taskRelPos; | 
|---|
| 84 | Vector correction=(destination+randomVector-swarmPosition)-movement; | 
|---|
| 85 | correction.y=0; | 
|---|
| 86 |  | 
|---|
| 87 | float correctionLen=correction.len(); | 
|---|
| 88 | if(correctionLen>aMax*dt)correction=correction/correctionLen*aMax*dt; | 
|---|
| 89 |  | 
|---|
| 90 | //std::cout << angleRad(correction,movement) << "\n"; | 
|---|
| 91 | movement+=correction; | 
|---|
| 92 |  | 
|---|
| 93 | float movementLen=movement.len(); | 
|---|
| 94 | if(movementLen>vMax)movement=movement/movementLen*vMax; | 
|---|
| 95 |  | 
|---|
| 96 |  | 
|---|
| 97 |  | 
|---|
| 98 | for (it= members.begin(); it!= members.end(); it++ ){ | 
|---|
| 99 | it->second->setDestination(swarmPosition); | 
|---|
| 100 | it->second->setDestinationMovement(movement); | 
|---|
| 101 | it->second->setTarget(target); | 
|---|
| 102 | it->second->process(dt); | 
|---|
| 103 | } | 
|---|
| 104 | } | 
|---|