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

$slice = 10;

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());
    
    $req = $bdd->prepare('TRUNCATE TABLE cDeposits');
	$req->execute(array());
}

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

$temperatureImage = imagecreatefrompng("resources/map/temperature.png");
$precipitationImage = imagecreatefrompng("resources/map/precipitation.png");

$mineralSolidityImage = imagecreatefrompng("resources/map/temperature.png");
$mineralValueImage = imagecreatefrompng("resources/map/precipitation.png");

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

$width = getMapWidth();
$height = getMapHeight();

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

for($xR = $beg / getRegionWidth(); $xR < $end / getRegionWidth(); $xR++) {
    for($yR = 0 / getRegionHeight(); $yR < $height / getRegionHeight(); $yR++) {
        
        $temperature = 0;
        $precipitation = 0;
        
        $mineralSolidity = 0;
        $mineralValue = 0;
        
        $meadowsCount = 0;

        for($x = 0; $x < getRegionWidth(); $x++) {
            for($y = 0; $y < getRegionHeight(); $y++) {
                
                $rgb = imagecolorat($terrainImage, $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;
                }
                
                if ($type == 1) {
                    $meadowsCount ++;
                }
                
                $temperature += imagecolorat($temperatureImage, $xR * getRegionWidth() + $x, $yR * getRegionHeight() + $y) & 0xFF;
                $precipitation += imagecolorat($precipitationImage, $xR * getRegionWidth() + $x, $yR * getRegionHeight() + $y) & 0xFF;
                
                $mineralSolidity += imagecolorat($mineralSolidityImage, $xR * getRegionWidth() + $x, $yR * getRegionHeight() + $y) & 0xFF;
                $mineralValue += imagecolorat($mineralValueImage, $xR * getRegionWidth() + $x, $yR * getRegionHeight() + $y) & 0xFF;
                
                $rgbDeposit = imagecolorat($depositImage, $xR * getRegionWidth() + $x, $yR * getRegionHeight() + $y);
                $rDeposit = ($rgbDeposit >> 16) & 0xFF;
                $gDeposit = ($rgbDeposit >> 8) & 0xFF;
                $bDeposit = $rgbDeposit & 0xFF;
                
                if ($bDeposit != 0) {
                    $depositType = 0;
                    if ($rDeposit == 0 && $gDeposit == 255){ // uranium
                        $depositType = getResourceId("uranium");
                    }
                    
                    $req = $bdd->prepare('INSERT INTO cDeposits (territory, resource, concentration) VALUES(?, ?, ?)');
                    $req->execute(array(getTerritoryId($xR * getRegionWidth() + $x, $yR * getRegionHeight() + $y), $depositType, $bDeposit * 100 / 255));
                }
                
                $req = $bdd->prepare('INSERT INTO cTerritories (type) VALUES(?)');
                $req->execute(array($type));
            }
        }
        
        $temperature *= 100 / (255 * getRegionWidth() * getRegionHeight());
        $precipitation *= 100 / (255 * getRegionWidth() * getRegionHeight());
        
        $mineralSolidity *= 100 / (255 * getRegionWidth() * getRegionHeight());
        $mineralValue *= 100 / (255 * getRegionWidth() * getRegionHeight());
        
        $cradle = false;
        
        if ($meadowsCount > getRegionWidth() * getRegionHeight() * 1 / 2) {
            $cradle = true;
        }
        
        $req = $bdd->prepare('INSERT INTO cRegions (owner, cradle, name, temperature, precipitation, mineralSolidity, mineralValue, time) VALUES(?, ?, ?, ?, ?, ?, ?, ?)');
        $req->execute(array(0, $cradle, "Uncharted lands", $temperature, $precipitation, $mineralSolidity, $mineralValue, time()));
    }
}

if ($_SESSION['indeGenMap'] < $slice-1) {
	$_SESSION['indeGenMap'] = $_SESSION['indeGenMap'] + 1;
	header('Location: genMap.php#');
}
else {
    header('Location: initMapSvg.php');
}
?>