这份代码可以判断一个目标是否可以在一定角度和距离范围内被玩家发现(类似英雄联盟的技能范围判定效果)
stock bool:IsTargetInSightRange(client, target, Float:angle=90.0, Float:distance=0.0, bool:heightcheck=true, bool:negativeangle=false)
{
if(angle > 360.0 || angle < 0.0)
ThrowError("Angle Max : 360 & Min : 0. %d isn't proper angle.", angle);
if(!isClientConnectedIngameAlive(client))
ThrowError("Client is not Alive.");
if(!isClientConnectedIngameAlive(target))
ThrowError("Target is not Alive.");
decl Float:clientpos[3], Float:targetpos[3], Float:anglevector[3], Float:targetvector[3], Float:resultangle, Float:resultdistance;
GetClientEyeAngles(client, anglevector);
anglevector[0] = anglevector[2] = 0.0;
GetAngleVectors(anglevector, anglevector, NULL_VECTOR, NULL_VECTOR);
NormalizeVector(anglevector, anglevector);
if(negativeangle)
NegateVector(anglevector);
GetClientAbsOrigin(client, clientpos);
GetClientAbsOrigin(target, targetpos);
if(heightcheck && distance > 0)
resultdistance = GetVectorDistance(clientpos, targetpos);
clientpos[2] = targetpos[2] = 0.0;
MakeVectorFromPoints(clientpos, targetpos, targetvector);
NormalizeVector(targetvector, targetvector);
resultangle = RadToDeg(ArcCosine(GetVectorDotProduct(targetvector, anglevector)));
if(resultangle <= angle/2)
{
if(distance > 0)
{
if(!heightcheck)
resultdistance = GetVectorDistance(clientpos, targetpos);
if(distance >= resultdistance)
return true;
else
return false;
}
else
return true;
}
else
return false;
}
参考: