Jump to content
MakeWebGames

Recommended Posts

Posted

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.

Screenshot2025-12-02135021.thumb.png.63765526788db75921c587ef6874bd50.png

 

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)),
Posted

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"])
    );
}

 

  • Like 2

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...