`isPathClear`

checks if obstacles are in the way. Yaks are no obstacles, so that won’t work.

How familiar are you with vectors? They can help you a lot here. As these solutions need a bit more mathematical knowledge I will not formulate them here until requested.

Possible solution:

Imagine a plus placed on your hero. This cross is obviously dividing the field in four areas. The current enemy has to be in one of these quarters. Every Yak that is not in the same quarter can’t be blocking the enemy (not entirely true, see below). So we can remove them from the list.

Now we place a plus on the enemy. Every Yak that is not in the same quarter as your hero can’t block the way.

In the best case there is no Yak left and you can happily fire away.

This won’t always be the case (sadly).

Now comes the math part.

Let`a`

be the distance from the hero to the yak, `b`

the distance from the enemy to the yak and `c`

be the distance between the hero and the enemy. Everyone knows that `a^2+b^2=c^2`

. We are more interested in `a+b>=c`

. If `a>x*c`

and `b>x*c`

then we are safe. If one of the two is false, the yak is either to close to the hero, the enemy or both. But which value to give to`x`

? We can’t go below 0.5, because the Yak could stand exactly between us. We also can’t go above `1/sqrt(2)`

(we sorted that out earlier, some mathematical trick). I can’t give you an exact value here, just try some out. The smaller the value, the less you care about the Yaks.

```
var self = this;
/*
* Implement the this.removeByType-Method, we're going to need it! And it is the task of the level, so you will have to do it yourself.
*/
this.findQuarter = function(obj, reference){
var quarter = 0;
if (obj.pos.x > reference.pos.x)
quarter+=1;
if (obj.pos.y > reference.pos.y)
quarter+=2;
return quarter;
// Can you tell me which quarter got which number?
};
//Javascript
loop{
var possibleYaks = [];
var yaks = this.findByType("sand-yak");
var enemy = this.findNearest(this.removeByType(this.findEnemies(), "sand-yak"));
if(!enemy){
continue; //There might be no enemy, nothing to do here.
}
var enemyForHeroQuarter = this.findQuarter (enemy, this);
var heroForEnemyQuarter = this.findQuarter (this, enemy);
var c = this.distanceTo (enemy);
var x = 0.6; //tweak this!
for (var i = 0; i < yaks.length; i++){
if (enemyForHeroQuarter == this.findQuarter (yaks [i], this) && heroForEnemyQuarter == this.findQuarter ( yaks[i], enemy))
possibleYaks.push(yaks[i]);
}
var canShoot = true;
for (var j=0; j < possibleYaks.length; j++){
if ( this.distanceTo (possibleYaks[j]) <= x*c || enemy.distanceTo (possibleYaks[j] ) <= x*c){
canShoot=false;
break;
}
}
if (canShoot)
this.attack (enemy);
else
this.say ("Too dangerous, I could hit a Yak!");
}
```

*Completely typed on a smartphone, please ignore typos and syntax-errors*

EDIT: *Revised on computer, code ***should** be correct now