diff --git a/attackplace.php b/attackplace.php index 3c8c299..2696e66 100644 --- a/attackplace.php +++ b/attackplace.php @@ -3,11 +3,13 @@ require 'required.php'; require 'onlyloggedin.php'; +use AnthonyMartin\GeoLocation\GeoLocation as GeoLocation; + if (is_empty($VARS['locationid'])) { sendError("No target!", true); } -$place = $database->select('locations', ['locationid', 'teamid', 'owneruuid', 'currentlife', 'maxlife'], ['locationid' => $VARS['locationid']])[0]; +$place = $database->select('locations', ['locationid', 'teamid', 'owneruuid', 'currentlife', 'maxlife', 'osmid'], ['locationid' => $VARS['locationid']])[0]; $user = $database->select('players', ['level', 'teamid', 'energy', 'maxenergy', 'latitude', 'longitude'], ['uuid' => $_SESSION['uuid']])[0]; // This (probably) shouldn't happen in normal play @@ -27,30 +29,57 @@ if ($type_mod == 0.5) { $terrain_mod = 1.0; $weather_mod = 1.0; +$distance_mod = 1.0; if (!is_empty($VARS['lat']) && !is_empty($VARS['long'])) { - require 'weather_inc.php'; - $terrain = json_decode(file_get_contents("http://gis.terranquest.net/terrain.php?key=" . GIS_API_KEY . "&lat=" . $VARS['lat'] . "&long=" . $VARS['long']), TRUE); - if ($terrain['status'] == 'OK') { - $terraintype = $terrain['type']; - $terrain_mod = $TERRAIN_GRID[$terraintype][$user['teamid'] - 1]; - if ($terrain_mod == 0.5) { - $terrain_mod = .9; - } else if ($terrain_mod == 2) { - $terrain_mod = 1.1; - } else if ($terrain_mod == 3) { - $terrain_mod = 1.3; + try { + require_once 'latlong_validate.php'; + if (!is_empty(DARKSKY_APIKEY)) { + require 'weather_inc.php'; + $weather_icon = $currently['icon']; + $weather_mod = $WEATHER_GRID[$weather_icon][$user['teamid'] - 1]; + if ($weather_mod == 0.5) { + $weather_mod = .9; + } else if ($weather_mod == 2) { + $weather_mod = 1.1; + } } + } catch (Exception $e) { + $weather_mod = 1.0; } - $weather_icon = $currently['icon']; - $weather_mod = $WEATHER_GRID[$weather_icon][$user['teamid'] - 1]; - if ($weather_mod == 0.5) { - $weather_mod = .9; - } else if ($weather_mod == 2) { - $weather_mod = 1.1; + + try { + $terrain = json_decode(file_get_contents("http://gis.terranquest.net/terrain.php?key=" . GIS_API_KEY . "&lat=" . $VARS['lat'] . "&long=" . $VARS['long']), TRUE); + if ($terrain['status'] == 'OK') { + $terraintype = $terrain['type']; + $terrain_mod = $TERRAIN_GRID[$terraintype][$user['teamid'] - 1]; + if ($terrain_mod == 0.5) { + $terrain_mod = .9; + } else if ($terrain_mod == 2) { + $terrain_mod = 1.1; + } else if ($terrain_mod == 3) { + $terrain_mod = 1.3; + } } + } catch (Exception $e) { + $terrain_mod = 1.0; + } + + try { + // Round to 2 digits (approx. 1.11 meters/3.6 feet) + $lat = number_format((float) $VARS['lat'], 5, '.', ''); + $long = number_format((float) $VARS['long'], 5, '.', ''); + $placedata = json_decode(file_get_contents("http://gis.terranquest.net/place.php?key=" . GIS_API_KEY . "&osmid=" . $place['osmid']), true); + $placelong = $placedata['features'][0]['geometry']['coordinates'][0]; + $placelat = $placedata['features'][0]['geometry']['coordinates'][1]; + $playerlocation = GeoLocation::fromDegrees($lat, $long); + $placelocation = GeoLocation::fromDegrees($placelat, $placelong); + $distance_mod = DISTANCE_GRID($playerlocation->distanceTo($placelocation, 'miles')); + } catch (Exception $e) { + $distance_mod = 1.0; + } } -$damage = pow(floor($user['level']), 0.5) * 4 * $type_mod * $terrain_mod * $weather_mod; +$damage = pow(floor($user['level']), 0.5) * 4 * $type_mod * $terrain_mod * $weather_mod * $distance_mod; //$damage = 2 * $userdrain * $TYPE_GRID[$user['teamid']][$place['teamid']]; // Check if action possible if ($user['energy'] < $userdrain) { diff --git a/type_grid.php b/type_grid.php index 747c73d..5adca3e 100644 --- a/type_grid.php +++ b/type_grid.php @@ -46,3 +46,25 @@ $WEATHER_GRID = [ "wind" => [1, 1, 1, 2, 1, 1], "fog" => [1, 1, 1, 1, 1, 1] ]; + +/** + * Get a multiplier for the distance between the player and place. + * @param float $d the number of miles between + * @return float The damage multiplier + */ +function DISTANCE_GRID($d) { + $distance = floor($d * 100.0); + if ($distance <= 5) { // ~250 feet + return 1; + } else if ($distance <= 10) { // ~500 feet + return 0.95; + } else if ($distance <= 20) { // ~1000 feet + return 0.8; + } else if ($distance <= 50) { // ~2500 feet + return 0.6; + } else if ($distance <= 100) { // 1 mile (5280 feet) + return 0.4; + } else { // Greater than 1 mile + return 0.2; + } +} \ No newline at end of file diff --git a/weather_inc.php b/weather_inc.php index c4d5acd..4041550 100644 --- a/weather_inc.php +++ b/weather_inc.php @@ -5,7 +5,7 @@ * Get the contents of the current weather with $currently */ -require 'latlong_validate.php'; +require_once 'latlong_validate.php'; // Round to 2 digits (approx. 1.1km) $lat = number_format((float) $VARS['lat'], 2, '.', '');