18.01.2012, 05:48
Is there a way to get the closest dynamic object near you?
stock GetClosestDynamicObject(playerid)
{
new Float:px,Float:py,Float:pz,
Float:ox,Float:oy,Float:oz,Float:dist,result = 12345;
GetPlayerPos(playerid,px,py,pz);
for(new o; o < CountDynamicObjects(); o++)
{
GetDynamicObjectPos(o,ox,oy,oz);
dist = floatsqroot(floatpower(floatabs(floatsub(px,ox)),2)+floatpower(floatabs(floatsub(py,oy)),2)+floatpower(floatabs(floatsub(pz,oz)),2));
if(dist < result)
{
result = dist;
return o;
}
}
return 1;
}
new objectid = GetClosestDynamicObject(playerid);
stock GetClosestDynamicObject(playerid)
{
new px, py, pz, currentobject = -1, Float:distance = -1;
GetPlayerPos( playerid, px, py, pz );
for( new index = 0; index < CountDynamicObjects(); index++ )
{
if ( !IsValidDynamicObject( index ) )
continue;
Float:ox, Float:oy, Float:oz;
GetDynamicObjectPos( index, ox, oy, oz );
new Float:odist = floatsqroot(
floatpower( floatabs( floatsub( ox, px ) ), 2.0 ) +
floatpower( floatabs( floatsub( oy, py ) ), 2.0 ) +
floatpower( floatabs( floatsub( oz, pz ) ), 2.0 )
);
if ( currentobject == -1 )
{
currentobject = index;
distance = odist;
} else if ( odist < distance )
{
currentobject = index;
distance = odist;
}
}
return currentobject;
}
Simplified version
pawn Код:
|
stock GetClosestDynamicObject(playerid)
{
new
Float: px, Float: py, Float: pz,
currentobject = -1,
Float:distance = -1
;
GetPlayerPos( playerid, px, py, pz );
for( new index = 0; index < CountDynamicObjects(); index++ )
{
if ( !IsValidDynamicObject( index ) ) continue;
Float:ox, Float:oy, Float:oz;
GetDynamicObjectPos( index, ox, oy, oz );
new Float:odist = floatsqroot(
floatpower( floatabs( floatsub( ox, px ) ), 2.0 ) +
floatpower( floatabs( floatsub( oy, py ) ), 2.0 ) +
floatpower( floatabs( floatsub( oz, pz ) ), 2.0 )
);
if ( currentobject == -1 )
{
currentobject = index;
distance = odist;
} else if ( odist < distance )
{
currentobject = index;
distance = odist;
}
}
return currentobject;
}