<?php

require_once("connect.inc.php");
require_once ('../../common/scripts/persoGD.inc.php');

/* ------------------------------------------------------------------------------------
creationCarte

construction du code XHTML de la carte pour les terrains dans la zone demandee
------------------------------------------------------------------------------------ */

function getChars( $xCentre, $yCentre, $width, $height, $caseSize, $imgW, $imgH , $uid)
{
	global $connexion;
	$html = '';
// calcul du rectangle de coordonnees --------------------------------------------------
	
	$width2  = abs($width/2);
	$height2 = abs($height/2);
	
	$xMin = $xCentre - $width2;
	$xMax = $xCentre + $width2;
	$yMin = $yCentre - $height2;
	$yMax = $yCentre + $height2;

// icones d'occupation ------------------------------------------------------------------
	
	// cherche les cases contenant au moins un mimichants et les marque d'un icone group
	$select = ' WHERE char_pos[0]>='.$xMin.' AND char_pos[0]<'.$xMax.' AND char_pos[1]>='.$yMin.' AND char_pos[1]<'.$yMax;
	$req = 'SELECT char_pos[0] AS x, char_pos[1] AS y FROM characters'.$select.' GROUP BY char_pos[0], char_pos[1]';
	$res = pg_query($connexion, $req) or die( pg_last_error().'<br />'.$req );
	
	while( $c = pg_fetch_array($res) )
	{
		$x = ($c['x']-$xMin +1) * $caseSize - 16;
		$y = ($c['y']-$yMin)    * $caseSize;
		$pos = 'style="left:'.$x.'px; top:'.$y.'px;"';
		$html .= '<img '.$pos.' alt="" src="img/group.gif" />
';
	}
	
// images mimichants perso --------------------------------------------------------------
	
	$deltax = abs( ($caseSize - $imgW)/2 );
	$deltay = $caseSize - $imgH;
	
	// cherche les cases contenant les mimichants de l'utilisateur
	$req = 'SELECT char_id, char_name, char_head, char_ears, char_corps, char_tail, char_skincolor, char_pos[0] AS x, char_pos[1] AS y FROM characters '.$select.' AND user_id='.$uid;
	$res = pg_query($connexion, $req) or die( pg_last_error().'<br />'.$req );
	
	while( $c = pg_fetch_array($res) )
	{
		$x = ($c['x']-$xMin) * $caseSize + $deltax;
		$y = ($c['y']-$yMin) * $caseSize + $deltay;
		$cid = $c['char_id'];
		
		// cree l'image du mimichant si elle n'existe pas deja
		createMimichant($c['char_tail'], $c['char_corps'] , $c['char_head'] ,$c['char_ears'], $c['char_skincolor']);
		
		$src = '../mimichants/t'.$c['char_head'].'o'.$c['char_ears'].'c'.$c['char_corps'].'q'.$c['char_tail'].'c'.$c['char_skincolor'].'.gif';
		$pos = 'style="left:'.$x.'px; top:'.$y.'px;"';
		//$script = 'onclick="afficherMenu('.$cid.')"';
		$script = ' onclick="clicChar('.$c['x'].','.$c['y'].','.$cid.')"';
		$html .= '<img id="m'.$cid.'" '.$pos.$script.' alt="" title="'.$c['char_name'].'" src="'.$src.'"/>';
	}
	echo $html;
}
//echo '<link href="carte.css" rel="stylesheet" type="text/css" />';
//getChars( 0, 0, 8, 8, 56, 46, 65, 11 );
getChars( $_GET['x'], $_GET['y'], $_GET['w'], $_GET['h'], $_GET['caseSize'], $_GET['imgW'], $_GET['imgH'], $_GET['uid'] );
?>