# use FileHandle;
use GD;
# use strict;

package htmlgraf;
use vars qw/$graftyp $initmap $regzahl $mapminX $mapmaxX $xmin $xmax $ymin $ymax/;
use vars qw/$transparent $berge $burg $ebene $feuer $gletscher $hochland $ozean $schiff $sumpf $unknown $wald $wueste/;

sub htmlgraf {
#  my %r = @_;
  my ($region, $x, $y, $z);

  print "\nHTMLGRAF ANFANG\n";

  $initmap = 0;
  $regzahl = 0;
  $graftyp = "gif";			# hier gfs. auf gif setzen

  foreach $region (keys %main::r) {
    $region =~ /\s*([-\d]+)\s+([-\d]+)\s*([-\d]+)?/;

    if (defined($1)) {			# Notpruefung, ob Region

      $x = $1;
      $y = $2;

      if (!defined($3)) {
        $z = 0;
      } else {
        $z = $3;
      }
    
      mapminmax($x,$y) if ($z == 0);

      # print "$x $y $z\n";  
      # print "$region ";
      # print "$main::r{$region}{Terrain}\n";
      $regzahl++;
    }
  }

  $xmin = quad2hex($mapminX,$ymin);
  $xmax = quad2hex($mapmaxX,$ymax);

  print "$regzahl $mapminX $xmin $ymin $mapmaxX $xmax $ymax\n";

  initgraf();
  blockmap($xmin,$ymin,$xmax,$ymax,0,"welt"); # nur Ebene 0

  print "HTMLGRAF ENDE\n";
}

sub initgraf {
  my ($transparent);
#  my $graf = new FileHandle;

  open(GRAF,"berge.".$graftyp) or die "$!\n";
  $berge = newFromGif GD::Image(GRAF);
  close(GRAF);
  $transparent = $berge->colorClosest(255,255,255);
  $berge->transparent($transparent);

  open(GRAF,"burg.".$graftyp) or die "$!\n";
  $burg = newFromGif GD::Image(GRAF);
  close(GRAF);
  $transparent = $burg->colorClosest(255,255,255);
  $burg->transparent($transparent);

  open(GRAF,"ebene.".$graftyp) or die "$!\n";
  $ebene = newFromGif GD::Image(GRAF);
  close(GRAF);
  $transparent = $ebene->colorClosest(255,255,255);
  $ebene->transparent($transparent);

  open(GRAF,"feuer.".$graftyp) or die "$!\n";
  $feuer = newFromGif GD::Image(GRAF);
  close(GRAF);
  $transparent = $feuer->colorClosest(255,255,255);
  $feuer->transparent($transparent);

  open(GRAF,"gletscher.".$graftyp) or die "$!\n";
  $gletscher = newFromGif GD::Image(GRAF);
  close(GRAF);
  $transparent = $gletscher->colorClosest(255,255,255);
  $gletscher->transparent($transparent);

  open(GRAF,"hochland.".$graftyp) or die "$!\n";
  $hochland = newFromGif GD::Image(GRAF);
  close(GRAF);
  $transparent = $hochland->colorClosest(255,255,255);
  $hochland->transparent($transparent);

  open(GRAF,"ozean.".$graftyp) or die "$!\n";
  $ozean = newFromGif GD::Image(GRAF);
  close(GRAF);
  $transparent = $ozean->colorClosest(255,255,255);
  $ozean->transparent($transparent);

  open(GRAF,"schiff.".$graftyp) or die "$!\n";
  $schiff = newFromGif GD::Image(GRAF);
  close(GRAF);
  $transparent = $schiff->colorClosest(255,255,255);
  $schiff->transparent($transparent);

  open(GRAF,"sumpf.".$graftyp) or die "$!\n";
  $sumpf = newFromGif GD::Image(GRAF);
  close(GRAF);
  $transparent = $sumpf->colorClosest(255,255,255);
  $sumpf->transparent($transparent);

  open(GRAF,"unknown.".$graftyp) or die "$!\n";
  $unknown = newFromGif GD::Image(GRAF);
  close(GRAF);
  $transparent = $unknown->colorClosest(255,255,255);
  $unknown->transparent($transparent);

  open(GRAF,"wald.".$graftyp) or die "$!\n";
  $wald = newFromGif GD::Image(GRAF);
  close(GRAF);
  $transparent = $wald->colorClosest(255,255,255);
  $wald->transparent($transparent);

  open(GRAF,"wueste.".$graftyp) or die "$!\n";
  $wueste = newFromGif GD::Image(GRAF);
  close(GRAF);
  $transparent = $wueste->colorClosest(255,255,255);
  $wueste->transparent($transparent);
}

sub blockmap {
  my ($xmin,$ymin,$xmax,$ymax,$level,$name) = @_;
  my ($minX,$maxX,$xsize,$ysize);
  my ($mapimg,$region,$x,$y,$z,$indX,$indY,$key,$terrain,$stempel);

#  my $graf = new FileHandle;
#  my $html = new FileHandle;

  $minX = hex2quad($xmin,$ymin);
  $maxX = hex2quad($xmax,$ymax);

  $xsize = ($maxX-$minX+1) * 24;
  $ysize = ($ymax-$ymin+1) * 22;

  $mapimg = new GD::Image($xsize,$ysize) or die "$!\n";

  open (HTML,">" . $name . ".htm")
	or die "blockmap: konnte $name\.htm nicht zum schreiben oeffnen\n";
  open (GRAF,">" . $name . "." . $graftyp)
	or die "blockmap: konnte $name\.$graftyp nicht zum schreiben oeffnen\n";
  binmode GRAF;

my ($black,$grey,$white);
#$black = $mapimg->colorAllocate(255,255,255);
$grey  = $mapimg->colorAllocate(192,192,192);
#$white = $mapimg->colorAllocate(0,0,0);
#$mapimg->setPixel(1,1,$black);
#$mapimg->setPixel(2,2,$white);
$mapimg->fill(1,1,$grey);

  for ($indY = $ymax; $indY > $ymin; $indY--) {
    for ($indX = $minX; $indX < $maxX; $indX++) {
      $key = quad2hex($indX,$indY);
      $key .= " " . $indY;
      if ($level != 0) {
        $key .= " " . $level;
      }

      if (defined $main::r{$key}{Terrain}) {
        $terrain = $main::r{$key}{Terrain};

        if ($terrain =~ /Berge/) {
          $stempel = $berge;
        } elsif ($terrain =~ /Ebene/) {
          $stempel = $ebene;
        } elsif ($terrain =~ /Feuer/) {
          $stempel = $feuer;
        } elsif ($terrain =~ /Gletscher/) {
          $stempel = $gletscher;
        } elsif ($terrain =~ /Hochland/) {
          $stempel = $hochland;
        } elsif ($terrain =~ /Ozean/) {
          $stempel = $ozean;
        } elsif ($terrain =~ /Sumpf/) {
          $stempel = $sumpf;
        } elsif ($terrain =~ /Wald/) {
          $stempel = $wald;
        } elsif ($terrain =~ /Wüste/ || $terrain =~ /Wueste/) {
          $stempel = $wueste;
        }
      } else {
	$stempel = $unknown;
      }

      $mapimg->setBrush($stempel);
      $mapimg->setPixel(($indX-$minX)*24+12+12*($indY%2),
			 $ysize-($indY-$ymin)*22-11,GD::gdBrushed);

    }  
  }  

  foreach $region (keys %main::r) {
  }

  print GRAF $mapimg->gif;

  close GRAF;
  close HTML;
}


sub hex2quad
{
  my ($x, $y) = @_; 

# print STDERR "hex2quad $x $y ";

  if (($y % 2) == 0)
  {
    $x = $x + ($y/2);
  }
  else
  {
    $x = $x + (($y-1)/2);
  }

  return $x;
}


sub quad2hex
{
  my $x;
  my $y;

  ($x, $y) = @_;

# print STDERR "quad2hex $x $y ";

  if (($y % 2) == 0)
  {
    $x = $x - ($y/2);
  }
  else
  {
    $x = $x - (($y-1)/2);
  }

# print STDERR "$x\n";

  return $x;
}


sub mapminmax
{
  my($xpos, $ypos) = @_;

  $xpos = hex2quad($xpos,$ypos);

  if ($initmap == 0 || $mapminX > $xpos) {
    $mapminX = $xpos;
  }

  if ($initmap == 0 || $mapmaxX < $xpos) {
    $mapmaxX = $xpos;
  }

  if ($initmap == 0 || $ymin > $ypos) {
    $ymin = $ypos;
  }

  if ($initmap == 0 || $ymax < $ypos) {
    $ymax = $ypos;
  }

#print STDERR "mapminmax $xpos $ypos $mapminX $mapminY $mapmaxX $mapmaxY $initmap\n";  

  $initmap = 1;

  return;
}

1;

