Avoid square root using chrGetSquaredDistanceToPad

This commit is contained in:
Alexandre-Xavier Labonté-Lamoureux 2022-11-12 02:04:39 -05:00
parent 26b61b1914
commit be755f356b
3 changed files with 21 additions and 3 deletions

View File

@ -12863,6 +12863,23 @@ f32 chrGetDistanceToPad(struct chrdata *chr, s32 pad_id)
return distance;
}
f32 chrGetSquaredDistanceToPad(struct chrdata *chr, s32 pad_id)
{
struct prop *prop = chr->prop;
f32 xdiff, ydiff, zdiff;
f32 distance = 0;
pad_id = chrResolvePadId(chr, pad_id);
if (pad_id >= 0) {
xdiff = g_Pads[pad_id].pos.x - prop->pos.x;
ydiff = g_Pads[pad_id].pos.y - prop->pos.y;
zdiff = g_Pads[pad_id].pos.z - prop->pos.z;
distance = xdiff * xdiff + ydiff * ydiff + zdiff * zdiff;
}
return distance;
}
f32 chrGetSameFloorDistanceToPad(struct chrdata *chr, s32 pad_id)
{
struct prop *prop = chr->prop;

View File

@ -1495,7 +1495,7 @@ bool aiIfChrDistanceToPadGreaterThan(s32 chrref, s32 padnum, f32 distance)
padnum = g_Vars.chrdata->padpreset1;
}
return chr && padnum < 9000 && chrGetDistanceToPad(chr, padnum) > distance;
return chr && padnum < 9000 && chrGetSquaredDistanceToPad(chr, padnum) > distance * distance;
}
bool aiIfChrDistanceToPadLessThan(s32 chrref, s32 padnum, f32 distance)
@ -1506,7 +1506,7 @@ bool aiIfChrDistanceToPadLessThan(s32 chrref, s32 padnum, f32 distance)
padnum = g_Vars.chrdata->padpreset1;
}
return chr && padnum < 9000 && chrGetDistanceToPad(chr, padnum) < distance;
return chr && padnum < 9000 && chrGetSquaredDistanceToPad(chr, padnum) < distance * distance;
}
bool aiIfChrInjuredTarget(s32 chrref)
@ -1554,7 +1554,7 @@ bool aiIfEyespyNearG5Pad(s32 padnum)
if (stageGetIndex(g_Vars.stagenum) == STAGEINDEX_G5BUILDING) {
for (i = 0; i < PLAYERCOUNT(); i++) {
if (g_Vars.players[i]->eyespy && g_Vars.players[i]->eyespy->prop
&& chrGetDistanceToPad(g_Vars.players[i]->eyespy->prop->chr, padnum) < 150.0f) {
&& chrGetSquaredDistanceToPad(g_Vars.players[i]->eyespy->prop->chr, padnum) < 22500.0f) {
return true;
}
}

View File

@ -294,6 +294,7 @@ f32 chrGetDistanceToCurrentPlayer(struct chrdata *chr);
f32 propGetDistanceToProp(struct prop *a, struct prop *b);
f32 propGetLateralDistanceToProp(struct prop *a, struct prop *b);
f32 chrGetDistanceToPad(struct chrdata *chr, s32 pad_id);
f32 chrGetSquaredDistanceToPad(struct chrdata *chr, s32 pad_id);
f32 chrGetDistanceToCoord(struct chrdata *chr, struct coord *pos);
f32 chrGetLateralDistanceToCoord(struct chrdata *chr, struct coord *pos);
f32 chrGetLateralDistanceToPad(struct chrdata *chr, s32 pad_id);