Skip to content

Instantly share code, notes, and snippets.

@DavidEdwards
Created June 28, 2017 15:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidEdwards/c1f281e17ad3672645422365e76f67b2 to your computer and use it in GitHub Desktop.
Save DavidEdwards/c1f281e17ad3672645422365e76f67b2 to your computer and use it in GitHub Desktop.
EVE Map Generator
<?php
define('DESIRED_SIZE', 1024);
/*
Two dependencies on SDE csv files.
https://www.fuzzwork.co.uk/dump/latest/
mapSolarSystems.csv
mapSolarSystemJumps.csv
Both to be stored in a subdirectory called "dump"
*/
class EveUniverseMap {
private $region_id = 0;
private $fit = FALSE;
private $draw_all_solar = TRUE;
private $draw_all_solar_names = TRUE;
private $draw_all_jumps = TRUE;
private $bg_rgb = array(0, 0, 0);
private $color_bg = NULL;
private $solar_rgb = array(255, 0, 0);
private $solar_name_rgb = array(255, 180, 0);
private $jump_rgb = array(0, 180, 255);
private $radius = 4;
private $margin = 64;
private $solars = array();
private $width = 0;
private $height = 0;
private $lowest_x = 99999999999999999999999;
private $lowest_y = 99999999999999999999999;
private $lowest_z = 99999999999999999999999;
private $highest_x = -99999999999999999999999;
private $highest_y = -99999999999999999999999;
private $highest_z = -99999999999999999999999;
private $reduction_factor = 200000000000000; // 200000000000000 == 4354
function __construct() {
if(DESIRED_SIZE <= 1024) {
$this->radius = 1;
} else if(DESIRED_SIZE <= 2048) {
$this->radius = 3;
} else if(DESIRED_SIZE <= 4096) {
$this->radius = 6;
} else {
$this->radius = 8;
}
}
public function setRegionId($id) {
$this->region_id = $id;
}
public function setForceFit() {
$this->fit = TRUE;
}
public function setMargin($margin) {
$this->margin = $margin;
}
public function setDrawNames($enabled) {
$this->draw_all_solar_names = $enabled;
}
public function setDrawJumps($enabled) {
$this->draw_all_jumps = $enabled;
}
public function generate() {
$gd = imagecreate($this->width, $this->height);
$this->color_bg = ImageColorAllocate($gd, $this->bg_rgb[0], $this->bg_rgb[1], $this->bg_rgb[2]);
imagefill($gd, 0, 0, $this->color_bg);
$this->drawAllJumps($gd);
$this->drawAllSolars($gd);
header('Content-Type: image/png');
imagepng($gd);
}
// Scan the solarsystem dump to fill the solar system array and find the width and height (amongst other things).
public function parseMap() {
// Read in solar system data from SDE dump
$handle = fopen("dump/mapSolarSystems.csv", "r");
if ($handle) {
fgets($handle); // ignore first line
while (($line = fgets($handle)) !== false) {
if((substr($line, 0, 1) == "#")) continue; // ignore any comments
$solar = explode(',', $line);
if($this->region_id == 0) {
if(intval($solar[0]) > 10000069) continue;
} else {
if(intval($solar[0]) != $this->region_id) continue;
}
$raw_x = floatval($solar[4]);
$raw_y = floatval($solar[5]);
$raw_z = floatval($solar[6]) * -1; // Invert the map. Otherwise !FUN!
// Naughty code that makes the gigantic numbers from the SDE not cause errors
$reduced_x = $raw_x / 200000000000000;
$reduced_y = $raw_y / 200000000000000;
$reduced_z = $raw_z / 200000000000000;
if($reduced_x < $this->lowest_x) $this->lowest_x = $reduced_x;
if($reduced_y < $this->lowest_y) $this->lowest_y = $reduced_y;
if($reduced_z < $this->lowest_z) $this->lowest_z = $reduced_z;
if($reduced_x > $this->highest_x) $this->highest_x = $reduced_x;
if($reduced_y > $this->highest_y) $this->highest_y = $reduced_y;
if($reduced_z > $this->highest_z) $this->highest_z = $reduced_z;
$this->solars["{$solar[2]}"] = array(
'region_id' => $solar[0],
'id' => $solar[2],
'name' => $solar[3],
'x' => $reduced_x,
'y' => $reduced_y,
'z' => $reduced_z
);
}
fclose($handle);
} else {
// error opening the file.
}
$this->generateSize();
$this->adjustSolarsToCanvas();
}
private function generateSize() {
if($this->fit) {
$this->reduction_factor = (DESIRED_SIZE - ($this->margin * 2)) / ($this->highest_x - $this->lowest_x);
$this->width = ($this->highest_x - $this->lowest_x) * $this->reduction_factor + ($this->margin * 2);
$this->height = ($this->highest_z - $this->lowest_z) * $this->reduction_factor + ($this->margin * 2);
} else {
if(($this->highest_x - $this->lowest_x) > ($this->highest_z - $this->lowest_z)) {
$this->reduction_factor = (DESIRED_SIZE - ($this->margin * 2)) / ($this->highest_x - $this->lowest_x);
} else {
$this->reduction_factor = (DESIRED_SIZE - ($this->margin * 2)) / ($this->highest_z - $this->lowest_z);
}
$this->width = DESIRED_SIZE;
$this->height = DESIRED_SIZE;
}
}
private function adjustSolarsToCanvas() {
foreach($this->solars as &$solar) {
$solar['x'] = ($solar['x'] - $this->lowest_x) * $this->reduction_factor + $this->margin;
$solar['y'] = ($solar['y'] - $this->lowest_y) * $this->reduction_factor + $this->margin;
$solar['z'] = ($solar['z'] - $this->lowest_z) * $this->reduction_factor + $this->margin;
}
}
private function drawAllJumps($gd) {
if(!$this->draw_all_jumps) return;
$color_jump = imagecolorallocate($gd, $this->jump_rgb[0], $this->jump_rgb[1], $this->jump_rgb[2]);
$jump_line_separation = 3;
// Read in solar system jump data from SDE dump
$handle = fopen("dump/mapSolarSystemJumps.csv", "r");
if ($handle) {
fgets($handle); // ignore first line
while (($line = fgets($handle)) !== false) {
if((substr($line, 0, 1) == "#")) continue; // ignore any comments
$jump = explode(',', $line);
if(!array_key_exists($jump[2], $this->solars) || !array_key_exists($jump[3], $this->solars)) continue;
$from = $this->solars[$jump[2]];
$to = $this->solars[$jump[3]];
$this->imagelinedotted($gd, $from['x'], $from['z'], $to['x'], $to['z'], $jump_line_separation, $color_jump);
}
fclose($handle);
} else {
// error opening the file.
}
}
private function drawAllSolars($gd) {
if(!$this->draw_all_solar && !$this->draw_all_solar_names) return;
$color_solar = imagecolorallocate($gd, $this->solar_rgb[0], $this->solar_rgb[1], $this->solar_rgb[2]);
$color_solar_name = imagecolorallocate($gd, $this->solar_name_rgb[0], $this->solar_name_rgb[1], $this->solar_name_rgb[2]);
foreach($this->solars as $solar) {
$x = $solar['x'];
$y = $solar['z'];
$this->drawSolar($gd, $color_solar, $color_solar_name, $solar, $x, $y, $this->radius, $this->radius);
}
}
private function drawSolar($gd, $color_solar, $color_solar_name, $solar, $center_x, $center_y, $radius) {
if($this->draw_all_solar) {
imagefilledellipse($gd, $center_x, $center_y, $radius, $radius*2, $this->color_bg);
imagefilledellipse($gd, $center_x, $center_y, $radius, $radius, $color_solar);
}
if($this->draw_all_solar_names) {
imagestring($gd, 1, $center_x + $this->radius + 2, $center_y - ($this->radius / 2), $solar['name'], $color_solar_name);
}
}
// Originally: http://php.net/manual/de/function.imageline.php#62606
function imagelinedotted($im, $x1, $y1, $x2, $y2, $dist, $col) {
$style = array ($col);
for ($i = 0; $i < $dist; $i++) {
array_push($style, $this->color_bg);
}
imagesetstyle ($im, $style);
return (integer) imageline ($im, $x1, $y1, $x2, $y2, IMG_COLOR_STYLED);
imagesetstyle ($im, array($col));
}
}
$map = new EveUniverseMap();
$map->setRegionId(10000060); // Delve
$map->setForceFit();
$map->setMargin(16);
//$map->setDrawNames(false);
//$map->setDrawJumps(false);
$map->parseMap();
$map->generate();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment