/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Thomas Fahrni co-programmer: */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_AI #include "swarm_gorel.h" #include "ai_module.h" #include "movement_module.h" #include "attack_module.h" #include "debug.h" #include #include "aabb.h" AISwarm::AISwarm() { tickCount=0; randomFreq=100; } void AISwarm::process(float dt) { std::map::iterator it; if(enemys==NULL || enemys->size()<1)return; target=enemys->at(0); Vector swarmPosition=this->getPosition(); Vector targetPosition=target->getAbsCoor(); float distanceToTarget=(swarmPosition-targetPosition).len(); float aMax=70.0f; float vMax=1000.0f; float deltaDistance=30; float attackDistance=100; if(distanceToTarget+deltaDistance<=attackDistance && status!=ATTACKING){ for (it= members.begin(); it!= members.end(); it++ ){ AIModule* oldAI = it->second; AIModule* newAI = new AttackModule(); newAI->getAttributesFrom(oldAI); it->second=newAI; delete oldAI; status=ATTACKING; } }else if(distanceToTarget-deltaDistance>=attackDistance && status!=MOVING){ for (it= members.begin(); it!= members.end(); it++ ){ AIModule* oldAI = it->second; AIModule* newAI = new MovementModule(); newAI->getAttributesFrom(oldAI); it->second=newAI; delete oldAI; status=MOVING; } } if(status!=ATTACKING && tickCount>=randomFreq){ tickCount=0; //int x = (rand()%141)+10; //10-150 int x = (rand()%241)-120; //-120-120 int z = (rand()%241)-120; //-120-120 randomVector=Vector(x,0,z); //std::cout << "change to: ||" << randomVector.x << " ==" << randomVector.z << "\n"; } tickCount++; destination=taskRelObject->getAbsCoor()+taskRelPos; Vector correction=(destination+randomVector-swarmPosition)-movement; correction.y=0; float correctionLen=correction.len(); if(correctionLen>aMax*dt)correction=correction/correctionLen*aMax*dt; //std::cout << angleRad(correction,movement) << "\n"; movement+=correction; float movementLen=movement.len(); if(movementLen>vMax)movement=movement/movementLen*vMax; for (it= members.begin(); it!= members.end(); it++ ){ it->second->setDestination(swarmPosition); it->second->setDestinationMovement(movement); it->second->setTarget(target); it->second->process(dt); } }