Browse Source

Attack strength now decreases far away from a location (closes #6)

Skylar Ittner 2 years ago
parent
commit
6d13916c23
3 changed files with 71 additions and 20 deletions
  1. 48
    19
      attackplace.php
  2. 22
    0
      type_grid.php
  3. 1
    1
      weather_inc.php

+ 48
- 19
attackplace.php View File

@@ -3,11 +3,13 @@
3 3
 require 'required.php';
4 4
 require 'onlyloggedin.php';
5 5
 
6
+use AnthonyMartin\GeoLocation\GeoLocation as GeoLocation;
7
+
6 8
 if (is_empty($VARS['locationid'])) {
7 9
     sendError("No target!", true);
8 10
 }
9 11
 
10
-$place = $database->select('locations', ['locationid', 'teamid', 'owneruuid', 'currentlife', 'maxlife'], ['locationid' => $VARS['locationid']])[0];
12
+$place = $database->select('locations', ['locationid', 'teamid', 'owneruuid', 'currentlife', 'maxlife', 'osmid'], ['locationid' => $VARS['locationid']])[0];
11 13
 $user = $database->select('players', ['level', 'teamid', 'energy', 'maxenergy', 'latitude', 'longitude'], ['uuid' => $_SESSION['uuid']])[0];
12 14
 
13 15
 // This (probably) shouldn't happen in normal play
@@ -27,30 +29,57 @@ if ($type_mod == 0.5) {
27 29
 
28 30
 $terrain_mod = 1.0;
29 31
 $weather_mod = 1.0;
32
+$distance_mod = 1.0;
30 33
 if (!is_empty($VARS['lat']) && !is_empty($VARS['long'])) {
31
-    require 'weather_inc.php';
32
-    $terrain = json_decode(file_get_contents("http://gis.terranquest.net/terrain.php?key=" . GIS_API_KEY . "&lat=" . $VARS['lat'] . "&long=" . $VARS['long']), TRUE);
33
-    if ($terrain['status'] == 'OK') {
34
-        $terraintype = $terrain['type'];
35
-        $terrain_mod = $TERRAIN_GRID[$terraintype][$user['teamid'] - 1];
36
-        if ($terrain_mod == 0.5) {
37
-            $terrain_mod = .9;
38
-        } else if ($terrain_mod == 2) {
39
-            $terrain_mod = 1.1;
40
-        } else if ($terrain_mod == 3) {
41
-            $terrain_mod = 1.3;
34
+    try {
35
+        require_once 'latlong_validate.php';
36
+        if (!is_empty(DARKSKY_APIKEY)) {
37
+            require 'weather_inc.php';
38
+            $weather_icon = $currently['icon'];
39
+            $weather_mod = $WEATHER_GRID[$weather_icon][$user['teamid'] - 1];
40
+            if ($weather_mod == 0.5) {
41
+                $weather_mod = .9;
42
+            } else if ($weather_mod == 2) {
43
+                $weather_mod = 1.1;
44
+            }
42 45
         }
46
+    } catch (Exception $e) {
47
+        $weather_mod = 1.0;
43 48
     }
44
-    $weather_icon = $currently['icon'];
45
-    $weather_mod = $WEATHER_GRID[$weather_icon][$user['teamid'] - 1];
46
-    if ($weather_mod == 0.5) {
47
-            $weather_mod = .9;
48
-        } else if ($weather_mod == 2) {
49
-            $weather_mod = 1.1;
49
+
50
+    try {
51
+        $terrain = json_decode(file_get_contents("http://gis.terranquest.net/terrain.php?key=" . GIS_API_KEY . "&lat=" . $VARS['lat'] . "&long=" . $VARS['long']), TRUE);
52
+        if ($terrain['status'] == 'OK') {
53
+            $terraintype = $terrain['type'];
54
+            $terrain_mod = $TERRAIN_GRID[$terraintype][$user['teamid'] - 1];
55
+            if ($terrain_mod == 0.5) {
56
+                $terrain_mod = .9;
57
+            } else if ($terrain_mod == 2) {
58
+                $terrain_mod = 1.1;
59
+            } else if ($terrain_mod == 3) {
60
+                $terrain_mod = 1.3;
61
+            }
50 62
         }
63
+    } catch (Exception $e) {
64
+        $terrain_mod = 1.0;
65
+    }
66
+
67
+    try {
68
+        // Round to 2 digits (approx. 1.11 meters/3.6 feet)
69
+        $lat = number_format((float) $VARS['lat'], 5, '.', '');
70
+        $long = number_format((float) $VARS['long'], 5, '.', '');
71
+        $placedata = json_decode(file_get_contents("http://gis.terranquest.net/place.php?key=" . GIS_API_KEY . "&osmid=" . $place['osmid']), true);
72
+        $placelong = $placedata['features'][0]['geometry']['coordinates'][0];
73
+        $placelat = $placedata['features'][0]['geometry']['coordinates'][1];
74
+        $playerlocation = GeoLocation::fromDegrees($lat, $long);
75
+        $placelocation = GeoLocation::fromDegrees($placelat, $placelong);
76
+        $distance_mod = DISTANCE_GRID($playerlocation->distanceTo($placelocation, 'miles'));
77
+    } catch (Exception $e) {
78
+        $distance_mod = 1.0;
79
+    }
51 80
 }
52 81
 
53
-$damage = pow(floor($user['level']), 0.5) * 4 * $type_mod * $terrain_mod * $weather_mod;
82
+$damage = pow(floor($user['level']), 0.5) * 4 * $type_mod * $terrain_mod * $weather_mod * $distance_mod;
54 83
 //$damage = 2 * $userdrain * $TYPE_GRID[$user['teamid']][$place['teamid']];
55 84
 // Check if action possible
56 85
 if ($user['energy'] < $userdrain) {

+ 22
- 0
type_grid.php View File

@@ -46,3 +46,25 @@ $WEATHER_GRID = [
46 46
     "wind" => [1, 1, 1, 2, 1, 1],
47 47
     "fog" => [1, 1, 1, 1, 1, 1]
48 48
 ];
49
+
50
+/**
51
+ * Get a multiplier for the distance between the player and place.
52
+ * @param float $d the number of miles between
53
+ * @return float The damage multiplier
54
+ */
55
+function DISTANCE_GRID($d) {
56
+    $distance = floor($d * 100.0);
57
+    if ($distance <= 5) { // ~250 feet
58
+        return 1;
59
+    } else if ($distance <= 10) { // ~500 feet
60
+        return 0.95;
61
+    } else if ($distance <= 20) { // ~1000 feet
62
+        return 0.8;
63
+    } else if ($distance <= 50) { // ~2500 feet
64
+        return 0.6;
65
+    } else if ($distance <= 100) { // 1 mile (5280 feet)
66
+        return 0.4;
67
+    } else { // Greater than 1 mile
68
+        return 0.2;
69
+    }
70
+}

+ 1
- 1
weather_inc.php View File

@@ -5,7 +5,7 @@
5 5
  * Get the contents of the current weather with $currently
6 6
  */
7 7
 
8
-require 'latlong_validate.php';
8
+require_once 'latlong_validate.php';
9 9
 
10 10
 // Round to 2 digits (approx. 1.1km)
11 11
 $lat = number_format((float) $VARS['lat'], 2, '.', '');

Loading…
Cancel
Save