Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10621 was 10376, checked in by patrick, 18 years ago

merged branche ai to trunk

File size: 2.6 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=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
43void 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        if(taskRelObject!=NULL && taskMaxTime>0){
52                destination=taskRelObject->getAbsCoor()+taskRelPos;
53                correction=(destination-position)-view*speed;
54                correction.y=0;
55                taskMaxTime-=dt;
56        }else{
57                taskComplete=true;
58        }
59
60        float correctionLen=correction.len();
61        if(correctionLen>aMax*dt)correction=correction/correctionLen*aMax*dt;
62
63        //std::cout << angleRad(correction,movement) << "\n";
64        Vector movement=view*speed+correction;
65
66        float movementLen=movement.len();
67        if(movementLen>vMax)movement=movement/movementLen*vMax;
68
69        float newSpeed=movement.len();
70        Vector newView=movement.getNormalized();
71
72        /*if((newSpeed<=taskSpeed && speed>=taskSpeed)||(newSpeed>=taskSpeed && speed<=taskSpeed)){
73                newSpeed=taskSpeed;
74        }else if((newSpeed>speed && speed<taskSpeed) || (newSpeed < speed && speed < taskSpeed)){
75                newSpeed=taskSpeed;
76        }*/
77
78        /*if(angleDeg(view,newView)>viewChangeMax){
79                std::cout << "alarm\n";
80        }*/
81        if(newSpeed<40)newSpeed=40;
82        if(newSpeed>vMax)newSpeed=vMax;
83
84        speed=newSpeed;
85        view=newView;
86        position=position+view*speed*dt;
87
88        //tell orders to swarm-members...
89        for (it= members.begin(); it!= members.end(); it++ ){
90                it->second->setDestination(position);
91                it->second->setDestinationMovement(view*speed);
92                it->second->process(dt);
93        }
94
95        //check if destination reached
96        if(!taskComplete){
97                //swarmPosition=this->getPosition();
98                if((destination-position).len()<10)taskComplete=true;
99        }
100}
Note: See TracBrowser for help on using the repository browser.