<?php

  $date = date("Y-m-d");
  $t_file = "maps/{$date}_frontier_terrain.png";
  $m_file = "maps/{$date}_frontier_basic.png";

  $map_file = "/home/frontier/btmux/game.run/maps/currmap.map";
  if ($fh = fopen($map_file, "r")) {
    # Processing
    $dims = explode(" ", fgets($fh, 8));
    fclose($fh);
  } else {
     print 'Unable to open current map file.';
  }
  $map_size = $dims[0];

  $selected_file = $t_file;
  $draw_warps      = FALSE;
  $draw_bases      = FALSE;
  $draw_relays     = FALSE;
  $draw_generators = FALSE;
  $draw_stations   = FALSE;
  $mac_gamma       = FALSE;
  $draw_ids	   = FALSE;
  $draw_coors      = FALSE;
  $map_scale       = 1;
 
  if ( ( isset($_GET["dt"])  ) && ($_GET["dt"]  == 1 ) ) $selected_file = $m_file;
  if ( ( isset($_GET["dw"])  ) && ($_GET["dw"]  == 1 ) ) $draw_warps      = TRUE;
  if ( ( isset($_GET["db"])  ) && ($_GET["db"]  == 1 ) ) $draw_bases      = TRUE;
  if ( ( isset($_GET["dr"])  ) && ($_GET["dr"]  == 1 ) ) $draw_relays     = TRUE;
  if ( ( isset($_GET["dg"])  ) && ($_GET["dg"]  == 1 ) ) $draw_generators = TRUE;
  if ( ( isset($_GET["mac"]) ) && ($_GET["mac"] == 1 ) ) $mac_gamma       = TRUE;
  if ( ( isset($_GET["dc"])  ) && ($_GET["dc"]  == 1 ) ) $draw_coords     = TRUE;
  if ( ( isset($_GET["dl"])  ) && ($_GET["dl"]  == 1 ) ) $draw_labels     = TRUE;
  if ( (isset($_GET["scale"])) && ($_GET["scale"]  > 1) && ($_GET["scale"] < 2)) $map_scale = $_GET["scale"];

  include('../e107_config.php');

// SQL login information
  $server = $mySQLserver; 
  $user   = $mySQLuser;
  $pass   = $mySQLpassword;
  $dbname = $mySQLdefaultdb;

// create image
  if ( (! file_exists($t_file)) || (! file_exists($m_file)) ) {
    system('rm maps/*_frontier_terrain.png');
    system('rm maps/*_frontier_basic.png');

    $image_t = imagecreatetruecolor($map_size,$map_size);
    $image_m = imagecreatetruecolor($map_size,$map_size);

    $trans = 50; // 0 = opaque, 127 = transparent

// Get all the colors we need here...
// Colors for the basic map file
    $print_m  = imageColorAllocateAlpha($image_m, 0xFF, 0xFF, 0xFF,  0);
    $build_m  = ImageColorAllocateAlpha($image_m, 0x80, 0x80, 0x80, $trans);
    $grass_m  = ImageColorAllocateAlpha($image_m, 0x70, 0x70, 0x70, $trans);
    $land_m   = ImageColorAllocateAlpha($image_m, 0x60, 0x60, 0x60, $trans);
    $rough_m  = ImageColorAllocateAlpha($image_m, 0x50, 0x50, 0x50, $trans);
    $sea_m    = ImageColorAllocateAlpha($image_m, 0x00, 0x00, 0x00, $trans);
    $water_m  = ImageColorAllocateAlpha($image_m, 0x00, 0x00, 0x50, $trans);
    $ice_m    = ImageColorAllocateAlpha($image_t, 0x66, 0x66, 0xD0, $trans);

// Colors for the terrain map
    $print_t  =        ImageColorAllocateAlpha($image_t, 0xFF, 0xFF, 0xFF,  0);
    $wall     = array( ImageColorAllocateAlpha($image_t, round(0xC0 * 0.82), round(0xB0 * 0.82), round(0xB0 * 0.82), $trans),
                       ImageColorAllocateAlpha($image_t, round(0xC0 * 0.84), round(0xB0 * 0.84), round(0xB0 * 0.84), $trans),
                       ImageColorAllocateAlpha($image_t, round(0xC0 * 0.86), round(0xB0 * 0.86), round(0xB0 * 0.86), $trans),
                       ImageColorAllocateAlpha($image_t, round(0xC0 * 0.88), round(0xB0 * 0.88), round(0xB0 * 0.88), $trans),
                       ImageColorAllocateAlpha($image_t, round(0xC0 * 0.90), round(0xB0 * 0.90), round(0xB0 * 0.90), $trans),
                       ImageColorAllocateAlpha($image_t, round(0xC0 * 0.92), round(0xB0 * 0.92), round(0xB0 * 0.92), $trans),
                       ImageColorAllocateAlpha($image_t, round(0xC0 * 0.94), round(0xB0 * 0.94), round(0xB0 * 0.94), $trans),
                       ImageColorAllocateAlpha($image_t, round(0xC0 * 0.96), round(0xB0 * 0.96), round(0xB0 * 0.96), $trans),
                       ImageColorAllocateAlpha($image_t, round(0xC0 * 0.98), round(0xB0 * 0.98), round(0xB0 * 0.98), $trans),
                       ImageColorAllocateAlpha($image_t, round(0xC0 * 1.00), round(0xB0 * 1.00), round(0xB0 * 1.00), $trans),
                     );
    $lt_woods = array( ImageColorAllocateAlpha($image_t, 0x00, round(0x70 * 1.00), 0x00, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, round(0x70 * 0.92), 0x00, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, round(0x70 * 0.84), 0x00, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, round(0x70 * 0.76), 0x00, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, round(0x70 * 0.68), 0x00, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, round(0x70 * 0.60), 0x00, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, round(0x70 * 0.52), 0x00, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, round(0x70 * 0.44), 0x00, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, round(0x70 * 0.36), 0x00, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, round(0x70 * 0.28), 0x00, $trans)
                     );
    $hv_woods = array( ImageColorAllocateAlpha($image_t, 0x00, round(0x50 * 1.00), 0x00, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, round(0x50 * 0.92), 0x00, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, round(0x50 * 0.84), 0x00, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, round(0x50 * 0.76), 0x00, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, round(0x50 * 0.68), 0x00, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, round(0x50 * 0.60), 0x00, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, round(0x50 * 0.52), 0x00, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, round(0x50 * 0.44), 0x00, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, round(0x50 * 0.36), 0x00, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, round(0x50 * 0.28), 0x00, $trans)
                     );
    $rough    = array( ImageColorAllocateAlpha($image_t, round(0x70 * 1.00), round(0x80 * 1.00), 0x10, $trans),
                       ImageColorAllocateAlpha($image_t, round(0x70 * 0.92), round(0x80 * 0.92), 0x10, $trans),
                       ImageColorAllocateAlpha($image_t, round(0x70 * 0.84), round(0x80 * 0.84), 0x10, $trans),
                       ImageColorAllocateAlpha($image_t, round(0x70 * 0.76), round(0x80 * 0.76), 0x10, $trans),
                       ImageColorAllocateAlpha($image_t, round(0x70 * 0.68), round(0x80 * 0.68), 0x10, $trans),
                       ImageColorAllocateAlpha($image_t, round(0x70 * 0.60), round(0x80 * 0.60), 0x10, $trans),
                       ImageColorAllocateAlpha($image_t, round(0x70 * 0.52), round(0x80 * 0.52), 0x10, $trans),
                       ImageColorAllocateAlpha($image_t, round(0x70 * 0.44), round(0x80 * 0.44), 0x10, $trans),
                       ImageColorAllocateAlpha($image_t, round(0x70 * 0.36), round(0x80 * 0.36), 0x10, $trans),
                       ImageColorAllocateAlpha($image_t, round(0x70 * 0.28), round(0x80 * 0.28), 0x10, $trans)
                     );
    $mountain = array( ImageColorAllocateAlpha($image_t, round(0x70 * 1.00), round(0x40 * 1.00), 0x10, $trans),
                       ImageColorAllocateAlpha($image_t, round(0x70 * 0.92), round(0x40 * 0.92), 0x10, $trans),
                       ImageColorAllocateAlpha($image_t, round(0x70 * 0.84), round(0x40 * 0.84), 0x10, $trans),
                       ImageColorAllocateAlpha($image_t, round(0x70 * 0.76), round(0x40 * 0.76), 0x10, $trans),
                       ImageColorAllocateAlpha($image_t, round(0x70 * 0.68), round(0x40 * 0.68), 0x10, $trans),
                       ImageColorAllocateAlpha($image_t, round(0x70 * 0.60), round(0x40 * 0.60), 0x10, $trans),
                       ImageColorAllocateAlpha($image_t, round(0x70 * 0.52), round(0x40 * 0.52), 0x10, $trans),
                       ImageColorAllocateAlpha($image_t, round(0x70 * 0.44), round(0x40 * 0.44), 0x10, $trans),
                       ImageColorAllocateAlpha($image_t, round(0x70 * 0.36), round(0x40 * 0.36), 0x10, $trans),
                       ImageColorAllocateAlpha($image_t, round(0x70 * 0.28), round(0x40 * 0.28), 0x10, $trans)
                     );
    $plain    = array( ImageColorAllocateAlpha($image_t, round(0x60 * 1.00), round(0x60 * 1.00), round(0x60 * 1.00), $trans),
                       ImageColorAllocateAlpha($image_t, round(0x60 * 0.92), round(0x60 * 0.92), round(0x60 * 0.92), $trans),
                       ImageColorAllocateAlpha($image_t, round(0x60 * 0.84), round(0x60 * 0.84), round(0x60 * 0.84), $trans),
                       ImageColorAllocateAlpha($image_t, round(0x60 * 0.76), round(0x60 * 0.76), round(0x60 * 0.76), $trans),
                       ImageColorAllocateAlpha($image_t, round(0x60 * 0.68), round(0x60 * 0.68), round(0x60 * 0.68), $trans),
                       ImageColorAllocateAlpha($image_t, round(0x60 * 0.60), round(0x60 * 0.60), round(0x60 * 0.60), $trans),
                       ImageColorAllocateAlpha($image_t, round(0x60 * 0.52), round(0x60 * 0.52), round(0x60 * 0.52), $trans),
                       ImageColorAllocateAlpha($image_t, round(0x60 * 0.44), round(0x60 * 0.44), round(0x60 * 0.44), $trans),
                       ImageColorAllocateAlpha($image_t, round(0x60 * 0.36), round(0x60 * 0.36), round(0x60 * 0.36), $trans),
                       ImageColorAllocateAlpha($image_t, round(0x60 * 0.28), round(0x60 * 0.28), round(0x60 * 0.28), $trans)
                     );
    $water    = array( ImageColorAllocateAlpha($image_t, 0x00, 0x00, round(0x30 * 1.90) % 255, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, 0x00, round(0x30 * 1.80) % 255, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, 0x00, round(0x30 * 1.70) % 255, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, 0x00, round(0x30 * 1.60) % 255, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, 0x00, round(0x30 * 1.50) % 255, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, 0x00, round(0x30 * 1.40) % 255, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, 0x00, round(0x30 * 1.30) % 255, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, 0x00, round(0x30 * 1.20) % 255, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, 0x00, round(0x30 * 1.10) % 255, $trans),
                       ImageColorAllocateAlpha($image_t, 0x00, 0x00, round(0x30 * 1.00) % 255, $trans)
                     );
    $fire     = ImageColorAllocateAlpha($image_t, 0xA0, 0x20, 0x20, $trans);
    $smoke    = ImageColorAllocateAlpha($image_t, 0x70, 0x70, 0x70, $trans);
    $snow     = ImageColorAllocateAlpha($image_t, 0xD0, 0xD0, 0xD0, $trans);
    $building = ImageColorAllocateAlpha($image_t, 0x90, 0x90, 0x90, $trans);
    $road     = ImageColorAllocateAlpha($image_t, 0x28, 0x28, 0x28, $trans);
    $bridge   = ImageColorAllocateAlpha($image_t, 0x40, 0x40, 0x40, $trans);
    $ice      = ImageColorAllocateAlpha($image_t, 0x66, 0x66, 0xD0, $trans);


    $lines = file($map_file);

    foreach ($lines as $line_num => $line) {
      if ( strlen($line) >= 300 ) {
        $chars = preg_split('//', rtrim($line), -1, PREG_SPLIT_NO_EMPTY);

        for ( $hex = 0; $hex <= 599; $hex++ ) {
          $color = $hex*2;
          $elevation = $hex*2 + 1; 

// Black/Grey map
          switch ($chars{$color}) {
            case "~":  if ( (0+$chars{$elevation}) < 2 ) {
                         imagesetpixel($image_m, $hex, $line_num, $water_m);
                       } else {
                         imagesetpixel($image_m, $hex, $line_num, $sea_m);
                       }
                       break;
            case ".":  imagesetpixel($image_m, $hex, $line_num, $grass_m);
                       break;
            case "-":  imagesetpixel($image_m, $hex, $line_num, $ice_m); 
                       break;
            case "@":  imagesetpixel($image_m, $hex, $line_num, $build_m);
                       break;
            case "=":  imagesetpixel($image_m, $hex, $line_num, $build_m);
                       break;
            case "^":  imagesetpixel($image_m, $hex, $line_num, $rough_m);
                       break;
            case "%":  imagesetpixel($image_m, $hex, $line_num, $rough_m);
                       break;
            case "&":  imagesetpixel($image_m, $hex, $line_num, $rough_m);
                       break;
            case "\"": imagesetpixel($image_m, $hex, $line_num, $rough_m);
                       break;
            case "`":  imagesetpixel($image_m, $hex, $line_num, $rough_m);
                       break;
			case "}":  imagesetpixel($image_m, $hex, $line_num, $rough_m);
                       break;
            default:   imagesetpixel($image_m, $hex, $line_num, $land_m);
                       break;
          }


// Terrain map
          switch ($chars{$color}) {
            case "=":  
                       imagesetpixel($image_t, $hex, $line_num, $wall{0+$chars{$elevation}}); 
                       break;
            case "#":  
                       imagesetpixel($image_t, $hex, $line_num, $road); 
                       break;
            case "/":  
                       imagesetpixel($image_t, $hex, $line_num, $bridge); 
                       break;
            case "`":  
                       imagesetpixel($image_t, $hex, $line_num, $lt_woods{0+$chars{$elevation}}); 
                       break;
            case "\"": 
                       imagesetpixel($image_t, $hex, $line_num, $hv_woods{0+$chars{$elevation}}); 
                       break;
            case "&":  
                       imagesetpixel($image_t, $hex, $line_num, $fire);  
                       break;
            case ":":  
                       imagesetpixel($image_t, $hex, $line_num, $smoke); 
                       break;
            case "+":  
                       imagesetpixel($image_t, $hex, $line_num, $snow); 
                       break;
            case "@":  
                       imagesetpixel($image_t, $hex, $line_num, $building); 
                       break;
            case "%":  
                       imagesetpixel($image_t, $hex, $line_num, $rough{0+$chars{$elevation}}); 
                       break;
            case "^":  
                       imagesetpixel($image_t, $hex, $line_num, $mountain{0+$chars{$elevation}}); 
                       break;
            case ".":  
                       imagesetpixel($image_t, $hex, $line_num, $plain{0+$chars{$elevation}}); 
                       break;
            case "~":  
                       imagesetpixel($image_t, $hex, $line_num, $water{0+$chars{$elevation}}); 
                       break;
            case "-":  
                       imagesetpixel($image_t, $hex, $line_num, $ice); 
                       break;
            default:   imagesetpixel($image_t, $hex, $line_num, $fire);
          }
        }
      }
    }

// Timestamp maps
  //  imageString($image_m,1,10,190,$date,$print_m);
  //  imageString($image_t,1,10,190,$date,$print_t);


// Write terrain and basic maps
  imagepng($image_t,$t_file);
  imagepng($image_m,$m_file);
  imagedestroy($image_t);
  imagedestroy($image_m);

  } // end of if ( $new_files ) ...


  $image = imagecreatefrompng($selected_file);

  $image2 = imagecreatetruecolor($map_size, $map_size);
  imagecopyresampled($image2,$image,0,0,0,0,500,500,200,200);

  $print_m  = imageColorAllocateAlpha($image2, 0xFF, 0xFF, 0xFF,  0);
  imageString($image2,1,10,490,$date,$print_m);

// Link to database 
  $link = mysql_connect($server, $user, $pass) or die("Could not connect : " . mysql_error());
  mysql_select_db($dbname)                     or die("Could not select database");

// Create map to draw on
  $image = imagecreatefrompng($selected_file);

// Gamma correct
  $any_selected = $draw_bases || $draw_relays || $draw_generators || $draw_stations;
  if (  $mac_gamma &&                                !$any_selected ) imagegammacorrect ($image, 1.0, 0.7 );
  if ( !$mac_gamma && ($selected_file == $t_file) &&  $any_selected ) imagegammacorrect ($image, 1.0, 0.5 );
  if (  $mac_gamma && ($selected_file == $t_file) &&  $any_selected ) imagegammacorrect ($image, 1.0, 0.4 );
 
// Drawing colors
  $draw  =       imageColorAllocateAlpha($image, 255, 255, 255, 96);

  $baseC[93]   = imageColorAllocateAlpha($image,   0, 255, 255, 48); // Admin
  $baseC[839]  = imageColorAllocateAlpha($image, 255,   0,   0, 48); // DC
  $baseC[94]   = imageColorAllocateAlpha($image,  43,  38, 254, 48); // LC
  $baseC[91]   = imageColorAllocateAlpha($image, 127, 255,   0, 48); // CC
  $baseC[1044] = imageColorAllocateAlpha($image, 173, 216, 230, 48); // Pirate

  $statC[93]   = imageColorAllocateAlpha($image,   0, 255, 255, 48); // Admin
  $statC[839]  = imageColorAllocateAlpha($image, 255,   0,   0, 48); // DC
  $statC[94]   = imageColorAllocateAlpha($image,  43,  38, 254, 48); // LC
  $statC[91]   = imageColorAllocateAlpha($image, 127, 255,   0, 48); // CC
  $statC[1044] = imageColorAllocateAlpha($image, 173, 216, 230, 48); // Pirate

// Draw warp lines
  if ( $draw_warps ) {
    imagesetthickness($image,2);

    $query = "SELECT * FROM objective_warpgate WHERE id <> 0";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    $num = mysql_num_rows($result);

    for ($x = 0; $x < $num; $x++) {
       $dat = mysql_fetch_array($result);
       $warp_id = $dat['id'];
       $warp_x1 = $dat['xcoord1'];
       $warp_x2 = $dat['xcoord2'];
       $warp_y1 = $dat['ycoord1'];
       $warp_y2 = $dat['ycoord2'];
       imageline($image,$warp_x1,$warp_y1,$warp_x2,$warp_y2,$draw);

       if ( $draw_labels ) {
          imageString($image, 1, ($warp_id > 9 ? $warp_x1-4 : $warp_x1-2), $warp_y1-3, $warp_id, $print_m); 
          imageString($image, 1, ($warp_id > 9 ? $warp_x2-4 : $warp_x2-2), $warp_y2-3, $warp_id, $print_m);
       }
    }
    mysql_free_result($result);
  } 

$draw_hqs = 1;

  if ( $draw_hqs) {
    $query = "SELECT * FROM player_faction WHERE ishidden = 0";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    $num = mysql_num_rows($result);

    for ($x = 0; $x < $num; $x++) {
       $dat = mysql_fetch_array($result);
       $facid = $dat['id'];
       $facalias = $dat['alias'];
       $hq_x = $dat['hqx'];
       $hq_y = $dat['hqy'];

       imageString($image, 5, $hq_x-6, $hq_y-6, $facalias, $print_m);
    }

  }

// Draw objectives

// --- draw bases
  if ( $draw_bases ) {
    $query = "SELECT * FROM objective_base WHERE id <> 0";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    $num = mysql_num_rows($result);

    for ($x = 0; $x < $num; $x++) {
       $dat = mysql_fetch_array($result);
       $base_id = $dat['id'];
       $base_x = $dat['coord_x'];
       $base_y = $dat['coord_y'];
       $own = $dat['owner_id'];
       $claimby = $dat['claimby_id'];
       
       if ($claimby == 0) {
          imagefilledrectangle($image, $base_x-4, $base_y-4, $base_x+4, $base_y+4, $baseC[$own]);
       } else {
          imagefilledellipse($image, $base_x, $base_y, 30, 30, $statC[$claimby]);
       } // end ifelse

       if ($draw_labels) {
          imageString($image, 1, ($base_id > 9 ? $base_x-4 : $base_x-2), $base_y-3, $base_id, $print_m);
       }

       if ($draw_coords) {
          imageString($image, 1, ($base_x > 99 ? $base_x-17: $base_x-13), $base_y+4, $base_x .",". $base_y, $print_m);
       }
    } // end for
   
    mysql_free_result($result);
  }

// --- draw generators
  if ( $draw_generators ) {
    $query = "SELECT * FROM objective_generator WHERE id <> 0";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    $num = mysql_num_rows($result);

    for ($x = 0; $x < $num; $x++) {
	$dat = mysql_fetch_array($result);
        $base_id = $dat['id'];
        $base_x = $dat['xcoord'];
        $base_y = $dat['ycoord'];
        $own    = $dat['owner_id'];
        $status = $dat['status'];
        $engoutput = $dat['engoutput'];

        $gen_b = array( $base_x+10, $base_y+10, $base_x-10, $base_y-10 );
        $gen_s = array( $base_x+ 5, $base_y+ 5, $base_x- 5, $base_y- 5 );
       
        // Is the gen operational? 
        if ( $status == 1 ) {
            imagepolygon(      $image, array( $base_x  , $base_y+9, 
                                              $base_x+9, $base_y,
                                              $base_x,   $base_y-9,
                                              $base_x-9, $base_y     ), 4, $baseC[$own]);
        }

        imagefilledpolygon($image, array( $base_x  , $base_y+6,
                                          $base_x+6,  $base_y,
                                          $base_x,   $base_y-6,
                                          $base_x-6,  $base_y     ), 4, $baseC[$own]);
        
        if ($draw_labels) {
           imageString($image, 1, ($base_id > 9 ? $base_x-4 : $base_x-2), $base_y-4, $base_id, $print_m);
        }

        if ($draw_coords) {
           imageString($image, 1, ($base_x > 99 ? $base_x-17: $base_x-13), $base_y+4, $base_x .",". $base_y, $print_m);
        }
    }
    mysql_free_result($result);
  }


// --- draw relay towers
  if ( $draw_relays ) {
    $query = "SELECT * FROM objective_relay WHERE id <> 0";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    $num = mysql_num_rows($result);

    for ($x = 0; $x < $num; $x++) {
       $dat = mysql_fetch_array($result);
       $base_id = $dat['id'];
       $base_x = $dat['xcoord'];
       $base_y = $dat['ycoord'];
       $own    = $dat['owner_id'];
       $r_cond   = $dat['integrity'];
       $r_stat   = $dat['status'];
       $lastclaim = $dat['lastclaim'];

       imagefilledellipse($image, $base_x, $base_y, 9, 9, $baseC[$own]);

       if ($draw_labels) {
           imageString($image, 1, ($base_id > 9 ? $base_x-4 : $base_x-2), $base_y-3, $base_id, $print_m);
       }       

       if ($draw_coords) {
           imageString($image, 1, ($base_x > 99 ? $base_x-17: $base_x-13), $base_y+4, $base_x .",". $base_y, $print_m);
       }

       if ( $r_stat <> 0 ) {
          if ( $r_cond == 100 ) { 
             imageEllipse($image, $base_x, $base_y, 13, 13, $statC[$own]);
          }
       }
    }
    mysql_free_result($result);
  }

// Closing connection
  mysql_close($link);



//  $image2 = imagecreatetruecolor(1000,1000);
//  imagecopyresampled($image2,$image,0,0,0,0,1000,1000,500,500);

  header("Content-type: image/png");

//  imagepng($image2);
  imagepng($image);
  imagedestroy($image);
//  imagedestroy($image2);
?>
