rednspirited Posted December 2 Posted December 2 I have azh code, it's similar to gl codes. The issue I have is with the travel map. Cities are lined up in a diagonal line. I have 23 cities, I might add more later. i would like to get them so they are randomly placed throughout the map. was wondering how i could go about that. Already figured out how to change the spacing placement so it would show all 23, but making it random, I am unsure. This is the section of code I am dealing with. $locationsCount = count($locations); foreach ($locations as $key => $location) { $hook = new Hook("alterModuleData"); $hookData = array( "module" => "travel", "user" => $this->user, "data" => $location ); $location = $hook->run($hookData, 1)["data"]; if (!isset($location['L_map']) || !file_exists($location['L_map'])) { $map = "themes/"._setting('theme')."/assets/img/pages/travel/map.png"; }else{ $map = $location['L_map']; } list($width, $height) = getimagesize($map); if ($location['L_leader'] > 0) { $gang = new Gang($location['L_leader']); $gangs = $gang->getGang(); $leader = $gangs['name']; }else{ $leader = "None"; } $data[] = array( "location" => $location["L_name"], "color" => $location['L_color'], "x" => ($width / $locationsCount) * ($location['L_id'] / $locationsCount) + (75 * ($key + 1)), "y" => ($height / ($locationsCount)) * ($location['L_id'] / $locationsCount) + (35 * ($key + 1)), "cost" => $location["L_cost"], "id" => $location["L_id"], "leader" => $leader, "cooldown" => $this->timeLeft($location["L_cooldown"]) ); } I changed the 75 to 40 and the 35 to 20 in these lines "x" => ($width / $locationsCount) * ($location['L_id'] / $locationsCount) + (75 * ($key + 1)), "y" => ($height / ($locationsCount)) * ($location['L_id'] / $locationsCount) + (35 * ($key + 1)), Quote
AlizHarb Posted Wednesday at 04:38 PM Posted Wednesday at 04:38 PM Hey, can you give a try for this please and let me know if that worked? $locationsCount = count($locations); $placed = []; foreach ($locations as $key => $location) { $hook = new Hook("alterModuleData"); $hookData = array( "module" => "travel", "user" => $this->user, "data" => $location ); $location = $hook->run($hookData, 1)["data"]; if (!isset($location['L_map']) || !file_exists($location['L_map'])) { $map = "themes/"._setting('theme')."/assets/img/pages/travel/map.png"; } else { $map = $location['L_map']; } list($width, $height) = getimagesize($map); if ($location['L_leader'] > 0) { $gang = new Gang($location['L_leader']); $gangs = $gang->getGang(); $leader = $gangs['name']; } else { $leader = "None"; } $marginX = 50; $marginY = 50; $minDistance = 80; $attempts = 0; do { $x = rand($marginX, $width - $marginX); $y = rand($marginY, $height - $marginY); $tooClose = false; foreach ($placed as $p) { $dx = $p['x'] - $x; $dy = $p['y'] - $y; $distance = sqrt($dx * $dx + $dy * $dy); if ($distance < $minDistance) { $tooClose = true; break; } } $attempts++; if ($attempts > 50) { $minDistance = max(20, $minDistance - 10); $attempts = 0; } } while ($tooClose); $placed[] = ['x' => $x, 'y' => $y]; $data[] = array( "location" => $location["L_name"], "color" => $location['L_color'], "x" => $x, "y" => $y, "cost" => $location["L_cost"], "id" => $location["L_id"], "leader" => $leader, "cooldown" => $this->timeLeft($location["L_cooldown"]) ); } 2 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.