Optimization: Use pre-squared value for distance calc

This commit is contained in:
Jackzie 2020-12-29 17:26:48 -06:00
parent 2eb2db1460
commit f75a8d8bed
No known key found for this signature in database
GPG key ID: 1E834FE36520537A
4 changed files with 8 additions and 6 deletions

Binary file not shown.

Binary file not shown.

View file

@ -4,6 +4,9 @@
#define PLUGIN_VERSION "1.5" #define PLUGIN_VERSION "1.5"
#pragma newdecls required #pragma newdecls required
#define MAX_TANK_DISTANCE_FROM_SURVIVOR 1000
#define MAX_TANK_DIST_SQUARED MAX_TANK_DISTANCE_FROM_SURVIVOR^2
//#define DEBUG //#define DEBUG
static bool bEscapeReady = false; static bool bEscapeReady = false;
@ -34,7 +37,6 @@ public void OnPluginStart()
HookEvent("round_start", Event_RoundStart, EventHookMode_PostNoCopy); HookEvent("round_start", Event_RoundStart, EventHookMode_PostNoCopy);
HookEvent("tank_spawn", Event_TankSpawn); HookEvent("tank_spawn", Event_TankSpawn);
HookEvent("tank_killed", Event_TankDeath); HookEvent("tank_killed", Event_TankDeath);
HookEvent("tank_killed", Event_RoundStart, EventHookMode_PostNoCopy);
HookEvent("finale_vehicle_incoming", Event_FinaleArriving, EventHookMode_PostNoCopy); HookEvent("finale_vehicle_incoming", Event_FinaleArriving, EventHookMode_PostNoCopy);
} }
@ -109,8 +111,8 @@ public Action BotControlTimerV2(Handle timer)
//Fetch the tank's position //Fetch the tank's position
GetClientAbsOrigin(tankID, TankPosition); GetClientAbsOrigin(tankID, TankPosition);
//Get distance to survivor, and compare to get closest tank //Get distance to survivor, and compare to get closest tank
distanceFromSurvivor = GetVectorDistance(BotPosition, TankPosition); distanceFromSurvivor = GetVectorDistance(BotPosition, TankPosition, true);
if(distanceFromSurvivor <= 1000 && smallestDistance > distanceFromSurvivor || smallestDistance == 0.0) { if(distanceFromSurvivor <= MAX_TANK_DIST_SQUARED && smallestDistance > distanceFromSurvivor || smallestDistance == 0.0) {
smallestDistance = distanceFromSurvivor; smallestDistance = distanceFromSurvivor;
closestTank = tankID; closestTank = tankID;
} }

View file

@ -71,7 +71,7 @@ public Action CheckTimer(Handle timer) {
//optimization: Only update player-based positions ever 5 loops (5 * 5 = 25 seconds) //optimization: Only update player-based positions ever 5 loops (5 * 5 = 25 seconds)
static int timer_update_pos; static int timer_update_pos;
if(GetClientCount(true) == 0) return Plugin_Continue; if(GetClientCount(true) == 0) return Plugin_Continue;
float pos[3], ang[3], botPos[3], center_distance; static float pos[3], ang[3], botPos[3], center_distance;
static float finalPos[3], checkPos[3]; static float finalPos[3], checkPos[3];
bool usingMinigun, usingMountedWeapon; bool usingMinigun, usingMountedWeapon;
@ -92,9 +92,9 @@ public Action CheckTimer(Handle timer) {
if(bIsSurvivorClient[bot] && IsClientInGame(bot) && IsFakeClient(bot) && IsPlayerAlive(bot)) { if(bIsSurvivorClient[bot] && IsClientInGame(bot) && IsFakeClient(bot) && IsPlayerAlive(bot)) {
GetClientAbsOrigin(bot, botPos); GetClientAbsOrigin(bot, botPos);
center_distance = GetVectorDistance(checkPos, botPos); center_distance = GetVectorDistance(checkPos, botPos, true);
if(center_distance <= 70) { if(center_distance <= 4900) {
L4D2_RunScript("CommandABot({cmd=1,bot=GetPlayerFromUserID(%i),pos=Vector(%f,%f,%f)})", GetClientUserId(bot), finalPos[0], finalPos[1], finalPos[2]); L4D2_RunScript("CommandABot({cmd=1,bot=GetPlayerFromUserID(%i),pos=Vector(%f,%f,%f)})", GetClientUserId(bot), finalPos[0], finalPos[1], finalPos[2]);
}else{ }else{
L4D2_RunScript("CommandABot({cmd=3,bot=GetPlayerFromUserID(%i)})", GetClientUserId(bot)); L4D2_RunScript("CommandABot({cmd=3,bot=GetPlayerFromUserID(%i)})", GetClientUserId(bot));