Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/ai/src/ai/swarm_gorel.cc @ 10244

Last change on this file since 10244 was 10244, checked in by tfahrni, 17 years ago

tired

File size: 2.1 KB
Line 
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
20void 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=1000/averageRadius;
31        vMax=1000/averageRadius;
32       
33        //load correct ai-module..
34        for (it= members.begin(); it!= members.end(); it++ ){
35                changeAIModule(it, new MovementModule());
36        }
37       
38}
39
40void SwarmGoRel::process(float dt)
41{
42        std::map<WorldEntity*,AIModule*>::iterator it;
43
44        Vector swarmPosition=this->getPosition();
45        Vector correction=Vector(0,0,0);
46        Vector destination;
47       
48        if(taskRelObject!=NULL && taskMaxTime>0){
49                destination=taskRelObject->getAbsCoor()+taskRelPos;
50                correction=(destination-swarmPosition)-view*speed;
51                correction.y=0;
52                taskMaxTime-=dt;
53        }else{
54                taskComplete=true;
55        }
56       
57        float correctionLen=correction.len();
58        if(correctionLen>aMax*dt)correction=correction/correctionLen*aMax*dt;
59
60        //std::cout << angleRad(correction,movement) << "\n";
61        Vector movement=view*speed+correction;
62
63        float movementLen=movement.len();
64        if(movementLen>vMax)movement=movement/movementLen*vMax;
65       
66        speed=movement.len();
67        view=movement.getNormalized();
68
69        //tell orders to swarm-members...
70        for (it= members.begin(); it!= members.end(); it++ ){
71                it->second->setDestination(swarmPosition);
72                it->second->setDestinationMovement(movement);
73                it->second->process(dt);
74        }
75       
76        //check if destination reached
77        if(!taskComplete){
78                swarmPosition=this->getPosition();
79                if((destination-swarmPosition).len()<10)taskComplete=true;
80        }
81}
Note: See TracBrowser for help on using the repository browser.