| [10135] | 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 | 
|---|
| [10244] | 16 | #include "swarm_gorel.h" | 
|---|
| [10226] | 17 | #include "ai_module.h" | 
|---|
 | 18 | #include "movement_module.h" | 
|---|
 | 19 | #include "attack_module.h" | 
|---|
| [10137] | 20 | #include "debug.h" | 
|---|
| [10158] | 21 | #include <stdio.h> | 
|---|
| [10226] | 22 | #include "aabb.h" | 
|---|
| [10135] | 23 |  | 
|---|
| [10177] | 24 | AISwarm::AISwarm() | 
|---|
 | 25 | { | 
|---|
 | 26 |         tickCount=0; | 
|---|
 | 27 |         randomFreq=100; | 
|---|
 | 28 | } | 
|---|
 | 29 |  | 
|---|
 | 30 |  | 
|---|
| [10135] | 31 | void AISwarm::process(float dt) | 
|---|
 | 32 | { | 
|---|
| [10226] | 33 |         std::map<WorldEntity*,AIModule*>::iterator it; | 
|---|
 | 34 |  | 
|---|
| [10244] | 35 |         if(enemys==NULL || enemys->size()<1)return; | 
|---|
 | 36 |         target=enemys->at(0); | 
|---|
 | 37 |          | 
|---|
| [10226] | 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){ | 
|---|
| [10177] | 72 |                 tickCount=0; | 
|---|
| [10226] | 73 |                 //int x = (rand()%141)+10;                              //10-150 | 
|---|
 | 74 |                 int x = (rand()%241)-120;                       //-120-120 | 
|---|
| [10177] | 75 |                 int z = (rand()%241)-120;                       //-120-120 | 
|---|
 | 76 |                 randomVector=Vector(x,0,z); | 
|---|
 | 77 |  | 
|---|
| [10226] | 78 |                 //std::cout << "change to: ||" << randomVector.x << "   ==" << randomVector.z << "\n"; | 
|---|
| [10177] | 79 |         } | 
|---|
 | 80 |         tickCount++; | 
|---|
 | 81 |  | 
|---|
 | 82 |  | 
|---|
| [10244] | 83 |         destination=taskRelObject->getAbsCoor()+taskRelPos; | 
|---|
| [10177] | 84 |         Vector correction=(destination+randomVector-swarmPosition)-movement; | 
|---|
| [10158] | 85 |         correction.y=0; | 
|---|
| [10177] | 86 |  | 
|---|
| [10158] | 87 |         float correctionLen=correction.len(); | 
|---|
 | 88 |         if(correctionLen>aMax*dt)correction=correction/correctionLen*aMax*dt; | 
|---|
| [10177] | 89 |  | 
|---|
| [10158] | 90 |         //std::cout << angleRad(correction,movement) << "\n"; | 
|---|
 | 91 |         movement+=correction; | 
|---|
| [10177] | 92 |  | 
|---|
| [10158] | 93 |         float movementLen=movement.len(); | 
|---|
 | 94 |         if(movementLen>vMax)movement=movement/movementLen*vMax; | 
|---|
| [10177] | 95 |  | 
|---|
 | 96 |  | 
|---|
| [10226] | 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); | 
|---|
| [10138] | 103 |         } | 
|---|
| [10226] | 104 | } | 
|---|