<?php
session_start();
include("serverConnect.php");
include("functions.php");

$slice = 10;
$smallImageSize = 3;

if (!isset($_SESSION['indeGenMap']) || $_SESSION['indeGenMap'] == 0) {
	$_SESSION['indeGenMap'] = 0;
	
	$req = $bdd->prepare('TRUNCATE TABLE cRegions');
	$req->execute(array());

	$req = $bdd->prepare('TRUNCATE TABLE cTerritories');
	$req->execute(array());
}

$image = imagecreatefrompng("resources/map.png");

$width = imagesx($image);
$height = imagesy($image);

$size = $width/$slice;
$beg = $_SESSION['indeGenMap'] * $size;
$end = ($_SESSION['indeGenMap'] + 1) * $size;

$hexaMask = imagecreatefrompng("resources/small-territories/hexaMask.png");
$hexaWidth = 25;
$hexaHalfWidth = 19;
$hexaHeight = 22;
$hexaHalfHeight = 10;

$smallImages = array();
$textures = array();
$textureSize = 512;

for ($i = 1; $i <=3; $i++) {
    $smallImages[$i] = imagecreatefrompng("resources/small-territories/" . $i . ".png");
    $textures[$i] = imagecreatefrompng("resources/textures/" . $i . ".png");
}

if ($_SESSION['indeGenMap'] == 0) {
    $imageGlobal = imagecreatetruecolor($width * $hexaHalfWidth, $height * ($hexaHeight - 1));
}
else {
    $imageGlobal = imagecreatefrompng("resources/global.png");
}

for($xR = $beg / getRegionWidth(); $xR < $end / getRegionWidth(); $xR++) {
    for($yR = 0 / getRegionHeight(); $yR < $height / getRegionHeight(); $yR++) {
        
        $req = $bdd->prepare('INSERT INTO cRegions (owner) VALUES(?)');
        $req->execute(array(0));
        
        $imageRegion = imagecreatetruecolor(getRegionWidth() * $smallImageSize, getRegionHeight() * $smallImageSize);
        
        for($x = 0; $x < getRegionWidth(); $x++) {
            for($y = 0; $y < getRegionHeight(); $y++) {
                
                $rgb = imagecolorat($image, $xR * getRegionWidth() + $x, $yR * getRegionHeight() + $y);
                $r = ($rgb >> 16) & 0xFF;
                $g = ($rgb >> 8) & 0xFF;
                $b = $rgb & 0xFF;
                
                if ($r == 105 && $g == 212 && $b == 52){ // meadow
                    $type = 1;
                }
                else if ($r == 86 && $g == 155 && $b == 255){ // sea
                    $type = 2;
                }
                else if ($r == 73 && $g == 60 && $b == 15){ // mountain
                    $type = 3;
                }
                else {
                    $type = 1;
                }
                
                imagecopy($imageRegion, $smallImages[$type], $x * $smallImageSize, $y * $smallImageSize, 0, 0, $smallImageSize, $smallImageSize);
                //imagecopy($imageGlobal, $smallImages[$type], ($xR * getRegionWidth() + $x) * $smallImageSize, ($yR * getRegionHeight() + $y) * $smallImageSize, 0, 0, $smallImageSize, $smallImageSize);
                
                $baseX = ($xR * getRegionWidth() + $x) * $hexaHalfWidth;
                $baseY = ($yR * getRegionHeight() + $y) * ($hexaHeight - 1);
                
                if (($xR * getRegionWidth() + $x) % 2 == 1) {
                    $baseY += $hexaHalfHeight;
                }
                
                for ($dx = 0; $dx < $hexaWidth; $dx++) {
                    for ($dy = 0; $dy < $hexaHeight; $dy++) {
                        $maskVal = imagecolorat($hexaMask, $dx, $dy) & 0xFF;
                        
                        if ($maskVal == 255) {
                            imagesetpixel($imageGlobal, $baseX + $dx, $baseY + $dy, imagecolorat($textures[$type], ($baseX + $dx) % $textureSize, ($baseY + $dy) % $textureSize));
                        }
                    }
                }
                
                $req = $bdd->prepare('INSERT INTO cTerritories (type) VALUES(?)');
                $req->execute(array($type));
            }
        }
        
        imagepng($imageRegion, "resources/regions/" . $xR . "-" . $yR . ".png");
    }
}

imagepng($imageGlobal, "resources/global.png");

if ($_SESSION['indeGenMap'] < $slice-1) {
	$_SESSION['indeGenMap'] = $_SESSION['indeGenMap'] + 1;
	header('Location: genMap.php#');
}
else {
    imagejpeg($imageGlobal, "resources/globalFinal.jpg");
    
}

echo "DONE";
?>