function build_wmo_map($folder, $map_name, $only_chunks=array(), $exclude_chunks=array(), $alt_name='', $rotate=1){

		$folder = strtolower($folder);
		$map_name = strtolower($map_name);

		global $blps, $pngs, $flats;

		$out_name = $alt_name ? $alt_name : $map_name;

		echo "$out_name: ";

		$wmo = "$blps/world/wmo/$folder/{$map_name}.wmo";
		if ($map_name == 'karazhan') $wmo = "$blps/world/wmo/$folder/kharazan.wmo";

		if (!file_exists($wmo)){
			echo "can't find $wmo\n";
			return;
		}
		if (!filesize($wmo)){
			echo "zero-length wmo - $wmo\n";
			return;
		}

		#echo "WMO: $wmo\n";
		#exit;

		$chunks = extract_mogi($wmo);

		if ($GLOBALS['dump_bounds']){
			echo "\n";
			foreach ($chunks as $k => $v){
				echo "$k,$v[0],$v[1],$v[2]\n";
			}
			exit;
		}

		#$chunks[49][5] = -9999;
		#$chunks[19][5] = -9999;

		if ($alt_name == 'inst_uk_lower'){
			$chunks[10][5] = -9999;
		}
		if ($alt_name == 'inst_an_lower'){
			$chunks[1][5] = -9999;
		}


		#
		# filter chunks?
		#

		if (count($only_chunks)){
			foreach ($chunks as $k => $v){
				if (!in_array($k, $only_chunks)) unset($chunks[$k]);
			}
		}else if (count($exclude_chunks)){
			foreach ($chunks as $k => $v){
				if (in_array($k, $exclude_chunks)) unset($chunks[$k]);
			}
		}
#print_r($chunks);

		#
		# find out size of all group chunks
		#

		$png_folder = str_replace('/', '_', $folder);
		$png_prefix = $map_name.'_';

		$files = glob("$pngs/wmo_$png_folder/$png_prefix*");
		$pieces = array();
		$rx = preg_quote($png_prefix, '!').'(\d\d\d)_(\d\d)_(\d\d)\.png$';
		foreach ($files as $file){
			if (preg_match("!$rx!", $file, $m)){
				list($w, $h) = getimagesize($file);
				$pieces[intval($m[1])][] = array(
					'path'	=> $file,
					'group'	=> intval($m[1]),
					'x'	=> intval($m[2]),
					'y'	=> intval($m[3]),
					'w'	=> $w,
					'h'	=> $h,
				);
			}
		}


		#
		# build a list of group chunks with the total size and the positions of all the subchunks
		#

		$total_pngs = 0;

		$groups = array();
		foreach ($pieces as $idx => &$group){

			$total_h = 0;
			$total_w = 0;
			$chunk_w = 0;
			$chunk_h = 0;

			foreach ($group as $row){
				if ($row['x'] == 0) $total_h += $row['h'];
				if ($row['y'] == 0) $total_w += $row['w'];
				$chunk_w = max($chunk_w, $row['x']+1);
				$chunk_h = max($chunk_h, $row['y']+1);
			}

			$our_pngs = array();

			$y_pos = $total_h;
			for ($y=0; $y<$chunk_h; $y++){
				$x_pos = 0;
				for ($x=0; $x<$chunk_w; $x++){
					foreach ($group as $row){
						if ($row['x'] == $x && $row['y'] == $y){
							$our_pngs[] = array($row['path'], $row['w'], $row['h'], $x_pos, $y_pos-$row['h']);
						}
					}

					$x_pos += 256;
				}
				$y_pos -= 256;
			}

			$total_pngs += count($our_pngs);

			$groups[$idx] = array(
				'w'	=> $total_w,
				'h'	=> $total_h,
				'pngs'	=> $our_pngs,
			);
		}

		if ($GLOBALS['dump_chunk_counts']){
			echo "\n";
			foreach ($groups as $k => $v){
				echo "group $k : ".count($v['pngs'])." pngs\n";
			}
			exit;
		}

		#print_r($groups);
		#exit;


		#
		# need to double chunk coords
		#

		foreach ($chunks as $k => $pos){
			$chunks[$k][0] *= 1.993;
			$chunks[$k][1] *= 1.993;
		}


		#
		# adjust chunk positions so they start in top left
		#

		$x1s = array();
		$y1s = array();
		$x2s = array();
		$y2s = array();

		foreach ($chunks as $k => $pos){
			$chunks[$k][1] -= $groups[$k]['h'];

			$x1s[] = $chunks[$k][0];
			$y1s[] = $chunks[$k][1];

			$x2s[] = $chunks[$k][0] + $groups[$k]['w'];
			$y2s[] = $chunks[$k][1] + $groups[$k]['h'];
		}

		$min_x = min($x1s);
		$min_y = min($y1s);

		foreach ($chunks as $k => $pos){
			$chunks[$k][0] -= $min_x;
			$chunks[$k][1] -= $min_y;
		}

		$canvas_w = ceil(max($x2s) - $min_x);
		$canvas_h = ceil(max($y2s) - $min_y);


		$total_chunks = count($chunks);
		echo "($total_chunks chunks, $total_pngs pngs) ";

#print_r($chunks);
#print_r($groups);
		$dst = "$flats/{$out_name}.png";

		echo shell_exec("convert -size !{$canvas_w}x{$canvas_h} null: -matte -compose Clear -composite -compose Over $dst");
		#echo shell_exec("convert xc:{$bg_color} -geometry !{$canvas_w}x{$canvas_h} $dst");

		uasort($chunks, 'zsort_chunks');

		foreach ($chunks as $k => $chunk){

			if (is_array($groups[$k]['pngs']))
			foreach ($groups[$k]['pngs'] as $row){

				$x = floor($chunk[0] + $row[3]);
				$y = floor($chunk[1] + $row[4]);

				$cmd = "composite $row[0] -geometry +{$x}+{$y} $dst $dst";
				#echo "$cmd\n";
				echo shell_exec($cmd);

				echo '.';
			}
		}


		#
		# add labels last, so they're on the top
		#

		if ($GLOBALS['add_labels']){
			foreach ($chunks as $k => $chunk){

				$g_w = $groups[$k]['w'];
				$g_h = $groups[$k]['h'];
				$tx = $chunk[0] + 1;
				$ty = $chunk[1] + 20;
				$gx2 = $chunk[0]+$g_w;
				$gy2 = $chunk[1]+$g_h;

				echo shell_exec("convert $dst -stroke red -pointsize 20 -undercolor dodgerblue -draw \"text $tx,$ty '$k'\" ".
					"-stroke '#333333' -fill none -draw \"rectangle $chunk[0],$chunk[1] $gx2,$gy2\" $dst");
			}
		}

		if (!$GLOBALS['add_labels']){
			if ($rotate==1){
				echo shell_exec("convert $dst -rotate 90 $dst");
			}
			if ($rotate==2){
				echo shell_exec("convert $dst -rotate 180 $dst");
			}
			if ($rotate==3){
				echo shell_exec("convert $dst -rotate 270 $dst");
			}
			if ($rotate=='kara'){
				echo shell_exec("convert $dst -rotate 135 $dst");
			}
		}

		echo " done\n";
	}
Exemple #2
0
#$data = fread($fh, 204);
#echo str_replace("\0","\n",$data)."\n";
exit;


#	$fh = fopen('OrgrimmarInstance.wdt', 'r');
#chunk_map($fh);

	#seek_to_chunk($fh, 'MAOF');
	#$data = fread($fh, 4096*4);
	#echo urlencode($data);
#exit;



extract_mogi('LavaDungeon.wmo'	);

$fh = fopen('LavaDungeon.wmo', 'r');
chunk_map($fh);

echo extract_pos('LavaDungeon_000.wmo')."\n";
echo extract_pos('LavaDungeon_001.wmo')."\n";
echo extract_pos('LavaDungeon_002.wmo')."\n";
echo extract_pos('LavaDungeon_011.wmo')."\n";


	#chunk_map($fh);



	function extract_pos($name){