| 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 |  | 
|---|
| 20 | void SwarmGoRel::initialize() | 
|---|
| 21 | { | 
|---|
| 22 |         std::map<WorldEntity*,AIModule*>::iterator it; | 
|---|
| 23 |  | 
|---|
| 24 |         //calculate  movement parameters.. | 
|---|
| 25 |         float averageRadius; | 
|---|
| 26 |         for (it=members.begin(); it!= members.end(); it++ ){ | 
|---|
| 27 |                 averageRadius+=it->second->getNPCRadius(); | 
|---|
| 28 |         } | 
|---|
| 29 |         averageRadius=averageRadius/members.size(); | 
|---|
| 30 |         aMax=80;//400/averageRadius; | 
|---|
| 31 |         vMax=maxSpeed;//1000/averageRadius;//300/averageRadius; | 
|---|
| 32 |         viewChangeMax=90/averageRadius; | 
|---|
| 33 |  | 
|---|
| 34 |         //load correct ai-module.. | 
|---|
| 35 |         for (it= members.begin(); it!= members.end(); it++ ){ | 
|---|
| 36 |                 changeAIModule(it, new MovementModule()); | 
|---|
| 37 |         } | 
|---|
| 38 |  | 
|---|
| 39 |         //get swarm position.. | 
|---|
| 40 |         //position=this->getPosition(); | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | void SwarmGoRel::process(float dt) | 
|---|
| 44 | { | 
|---|
| 45 |         std::map<WorldEntity*,AIModule*>::iterator it; | 
|---|
| 46 |  | 
|---|
| 47 |         //Vector swarmPosition=this->getPosition(); | 
|---|
| 48 |         Vector correction=Vector(0,0,0); | 
|---|
| 49 |         Vector destination; | 
|---|
| 50 |          | 
|---|
| 51 |         WorldEntity* taskRelObject = NULL; | 
|---|
| 52 |         if ( taskRelObjectName != "" ) | 
|---|
| 53 |           taskRelObject = dynamic_cast<WorldEntity*>(WorldEntity::objectList().getBaseObject( taskRelObjectName )); | 
|---|
| 54 |  | 
|---|
| 55 |         if(taskRelObject!=NULL && taskMaxTime>0){ | 
|---|
| 56 |                 destination=taskRelObject->getAbsCoor()+taskRelPos; | 
|---|
| 57 |                 correction=(destination-position)-view*speed; | 
|---|
| 58 |                 correction.y=0; | 
|---|
| 59 |                 taskMaxTime-=dt; | 
|---|
| 60 |         }else{ | 
|---|
| 61 |                 taskComplete=true; | 
|---|
| 62 |         } | 
|---|
| 63 |  | 
|---|
| 64 |         float correctionLen=correction.len(); | 
|---|
| 65 |         if(correctionLen>aMax*dt)correction=correction/correctionLen*aMax*dt; | 
|---|
| 66 |  | 
|---|
| 67 |         //std::cout << angleRad(correction,movement) << "\n"; | 
|---|
| 68 |         Vector movement=view*speed+correction; | 
|---|
| 69 |  | 
|---|
| 70 |         float movementLen=movement.len(); | 
|---|
| 71 |         if(movementLen>vMax)movement=movement/movementLen*vMax; | 
|---|
| 72 |  | 
|---|
| 73 |         float newSpeed=movement.len(); | 
|---|
| 74 |         Vector newView=movement.getNormalized(); | 
|---|
| 75 |  | 
|---|
| 76 |         /*if((newSpeed<=taskSpeed && speed>=taskSpeed)||(newSpeed>=taskSpeed && speed<=taskSpeed)){ | 
|---|
| 77 |                 newSpeed=taskSpeed; | 
|---|
| 78 |         }else if((newSpeed>speed && speed<taskSpeed) || (newSpeed < speed && speed < taskSpeed)){ | 
|---|
| 79 |                 newSpeed=taskSpeed; | 
|---|
| 80 |         }*/ | 
|---|
| 81 |  | 
|---|
| 82 |         /*if(angleDeg(view,newView)>viewChangeMax){ | 
|---|
| 83 |                 std::cout << "alarm\n"; | 
|---|
| 84 |         }*/ | 
|---|
| 85 |         if(newSpeed<40)newSpeed=40; | 
|---|
| 86 |         if(newSpeed>vMax)newSpeed=vMax; | 
|---|
| 87 |  | 
|---|
| 88 |         speed=newSpeed; | 
|---|
| 89 |         view=newView; | 
|---|
| 90 |         position=position+view*speed*dt; | 
|---|
| 91 |  | 
|---|
| 92 |         //tell orders to swarm-members... | 
|---|
| 93 |         for (it= members.begin(); it!= members.end(); it++ ){ | 
|---|
| 94 |                 it->second->setDestination(position); | 
|---|
| 95 |                 it->second->setDestinationMovement(view*speed); | 
|---|
| 96 |                 it->second->process(dt); | 
|---|
| 97 |         } | 
|---|
| 98 |  | 
|---|
| 99 |         //check if destination reached | 
|---|
| 100 |         if(!taskComplete){ | 
|---|
| 101 |                 //swarmPosition=this->getPosition(); | 
|---|
| 102 |                 if((destination-position).len()<10)taskComplete=true; | 
|---|
| 103 |         } | 
|---|
| 104 | } | 
|---|