File size: 2,115 Bytes
8be366a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | #include <iostream>
#include <sstream>
#include <string>
#include <gmtl/VecOps.h>
#include <ufo/Debug.h>
#include <ufo/Pilot.h>
#include <ufo/SteeringDecision.h>
#include <ufo/Plugin_def.h>
#include "StrongestBehaviour.h"
using namespace gmtl;
using namespace ufo;
using namespace std;
ufoplugin::StrongestBehaviour::StrongestBehaviour( const std::vector<ufo::Behaviour *> children)
: children(children)
{
}
ufoplugin::StrongestBehaviour::~StrongestBehaviour()
{
}
SteeringDecision ufoplugin::StrongestBehaviour::yield ( const float elapsedTime)
{
assert( myPilot != 0 );
SteeringDecision retval;
float maxls=0;
// whenever we find a "stronger" Behaviour, use that instead of the current one:
for (vector<Behaviour *>::const_iterator it = children.begin();
it != children.end(); ++it)
{
SteeringDecision tmp = (*it)->yield(elapsedTime);
float tmpls = lengthSquared(tmp.direction);
if ( tmpls > maxls )
{
retval= tmp;
maxls = tmpls;
}
}
return retval;
}
void ufoplugin::StrongestBehaviour::print () const
{
cout << "BEGIN StrongestBehaviour " <<endl;
cout << "Children (" << children.size() << "):" <<endl;
for (vector<Behaviour *>::const_iterator it = children.begin();
it != children.end(); ++it)
{
(*it)->print();
}
cout << "END StrongestBehaviour" <<endl;
}
void ufoplugin::StrongestBehaviour::registerPilot( Pilot *p )
{
assert(p != 0);
// call default implementation:
ufo::Behaviour::registerPilot( p );
// propagate call:
for (vector<Behaviour *>::const_iterator it = children.begin();
it != children.end(); ++it)
{
(*it)->registerPilot(p);
}
}
ufo::Behaviour *ufoplugin::StrongestBehaviourFactory(std::vector<ufo::Behaviour *> children, std::vector<std::pair<std::string,std::string> > *params)
{
if ( children.size() == 0 )
{
cout << "ERROR: StrongestBehaviour without children!\n" << endl;
return 0;
}
if ( params->size() > 0 )
{
cout << "WARNING: StrongestBehaviour has parameters" << endl;
}
return new StrongestBehaviour(children);
}
MAKEPLUGIN(BEHAVIOUR, Behaviour, StrongestBehaviour, &ufoplugin::StrongestBehaviourFactory)
|