From 81d56dfdc01ee61a650ec80539262faacb4cc1c7 Mon Sep 17 00:00:00 2001 From: Arkshine Date: Thu, 31 Jul 2014 11:33:58 +0200 Subject: [PATCH] Geoip: Add geoip_distance() native. --- dlls/geoip/geoip_amxx.cpp | 14 ++++++++++++++ plugins/include/geoip.inc | 18 +++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/dlls/geoip/geoip_amxx.cpp b/dlls/geoip/geoip_amxx.cpp index e335dd59..0ae6ba27 100755 --- a/dlls/geoip/geoip_amxx.cpp +++ b/dlls/geoip/geoip_amxx.cpp @@ -279,6 +279,19 @@ static cell AMX_NATIVE_CALL amx_geoip_longitude(AMX *amx, cell *params) return amx_ftoc(longitude); } +// native Float:geoip_distance(Float:lat1, Float:lon1, Float:lat2, Float:lon2, system = SYSTEM_METRIC); +static cell AMX_NATIVE_CALL amx_geoip_distance(AMX *amx, cell *params) +{ + float earthRadius = params[5] ? 3958.0 : 6370.997; // miles / km + + float lat1 = amx_ctof(params[1]) * (M_PI / 180); + float lon1 = amx_ctof(params[2]) * (M_PI / 180); + float lat2 = amx_ctof(params[3]) * (M_PI / 180); + float lon2 = amx_ctof(params[4]) * (M_PI / 180); + + return amx_ftoc(earthRadius * acos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1))); +} + void OnAmxxAttach() { @@ -352,6 +365,7 @@ AMX_NATIVE_INFO geoip_natives[] = { "geoip_timezone" , amx_geoip_timezone }, { "geoip_latitude" , amx_geoip_latitude }, { "geoip_longitude", amx_geoip_longitude }, + { "geoip_distance" , amx_geoip_distance }, { NULL, NULL }, }; diff --git a/plugins/include/geoip.inc b/plugins/include/geoip.inc index eec43d56..ef1295e5 100755 --- a/plugins/include/geoip.inc +++ b/plugins/include/geoip.inc @@ -150,4 +150,20 @@ native Float:geoip_latitude(const ip[]); * * @return The result of the geoip look up, 0 if longitude is not found. */ -native Float:geoip_longitude(const ip[]); \ No newline at end of file +native Float:geoip_longitude(const ip[]); + +/** + * Calculate the distance between geographical coordinates, latitude and longitude. + * + * @param lat1 The first IP latitude. + * @param lon1 The first IP longitude. + * @param lat2 The second IP latitude. + * @param lon2 The second IP longitude. + * @param system The system of measurement, 0 = Metric(kilometers) or 1 = English(miles). + * + * @return The distance as result in specified system of measurement. + */ +#define SYSTEM_METRIC 0 // kilometers +#define SYSTEM_IMPERIAL 1 // statute miles + +native Float:geoip_distance(Float:lat1, Float:lon1, Float:lat2, Float:lon2, system = SYSTEM_METRIC); \ No newline at end of file