# vsTASKER #5 – Using events : Flock of Birds

This tutorial deals with a simulation of a flock of 3000 birds, sharing the same simple Logic.
The behavior of each bird is given by the Logic according to 3 rules:

• The first one is to get attracted by one of the blue birds, without going too far;
• The second rule is to avoid getting close to one of the red birds;
• The last rule is to go back to the initial position when there is nothing in sight.

Flock Logic:

``````if (L:threat) {
if (E:pos.distanceTo(L:threat->pos) > L:threshold) {
E:setSpeed(10);
return DONE;
}
else {
float hdg = E:pos.azimuthTo(L:threat->pos);
E:setSpeed(L:threat->getSpeed()*2);
}
return AGAIN;
}``````
``````    E:dyn->setHeading(L:init_pos);
E:dyn->setSpeed(20);

if (E:pos.distanceTo(L:init_pos) < 50) {
E:setSpeed(0);
return DONE;
}``````
``````if (L:leader->pos.distanceTo(L:init_pos) > L:threshold * 2) return YES;
else return NO;
``````
``````  if (L:leader) {
if (E:pos.distanceTo(L:init_pos) > L:threshold * 2) {
return DONE;
}
else {
disp += RANDOM(1,10);
if (disp > 90) disp = 0;
E:setSpeed(RANDOM(30,50));
}
}``````
``````private:
Array threats;
``````case RESET: {
for (int i=0; i<S:entities.count(); i++) {
Vt_Entity* e=S:entities[i];
}
for (int i=0; i<S:entities.count(); i++) {
Vt_Entity* e=S:entities[i];
}
} break;``````
``````    float closest = L:threshold;
Vt_Entity* threat = NULL;

for (int i=0; i<threats.count(); i++) {
Vt_Entity* e=threats[i];
if (E:pos.distanceto(e->pos) < closest) {
closest = E:pos.distanceTo(e->pos);
threat = e;
}
}
if (threat) {
L:threat = threat;
L:raiseEvent("Detect!");
return AGAIN;
}

closest = L:threshold;

for (int i=0; i<leaders.count(); i++) {
if (E:pos.distanceto(e->pos) < closest) {
closest = E:pos.distanceTo(e->pos);
}
}
L:threat = NULL;
L:raiseEvent("Follow!");
}
}``````

Declaration
``````private:
Vt_Entity* e;
int wait, cpt, threat;``````
Initialization
``````  case RESET: {
for (int i=0; i<S:entities.count(); i++) {
e=S:entities[i];
if (e->getStatus()->isForce(_Blue)) break;
}
cpt = 0;
wait = 0;
threat = 0;
} break;``````
Runtime
``````    if (++cpt > wait) {
if (!threat) {
threat = true;
e->setColor(clRed);
wait = RANDOM(1,5);
}
else {
threat = false;
e->setColor(clBlue);
wait = RANDOM(20,40);
}
cpt = 0;
}
if (threat) S:raiseEvent("Detect!", T_Entity, e);
else S:raiseEvent("Follow!", T_Entity, e);``````