A Flamescale Wyrmkin
Join Date: Jul 2012
Posts: 110
|
Here's the PHP script that uses information from WorldMapOverlay.dbc and WorldMapArea.dbc, to generate a single map from tiles and overlays.
Code:
<?
header("Content-Type: image/png");
$name=$_GET['n'];
$mysqli = new mysqli("localhost", "root", "password", "WoWMapStuff");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$query1="SELECT ID,MapID FROM worldmaparea WHERE AreaName='$name'";
$res=$mysqli->query($query1);
$row=$res->fetch_assoc();
$id=$row['ID'];
$mapID=$row['MapID'];
$mapData = Array();
$query2 = "SELECT DISTINCT Name,Width,Height,X,Y FROM worldmapoverlay WHERE WorldMapAreaID='$id' AND Name <> '' AND Width > 0 AND Height >0";
$res=$mysqli->query($query2);
while ($row = $res->fetch_assoc()) {
$mapData[]=Array($row['Name'],$row['Width'],$row['Height'],$row['X'],$row['Y']);
}
if(file_exists("WorldMap/$name/".$name."1.png"))
{
$phase="";
}else{
$phase="1_";
}
$im = imagecreatetruecolor(1002,668);
$im1 = imagecreatefrompng("WorldMap/$name/".$name.$phase."1.png");
$im2 = imagecreatefrompng("WorldMap/$name/".$name.$phase."2.png");
$im3 = imagecreatefrompng("WorldMap/$name/".$name.$phase."3.png");
$im4 = imagecreatefrompng("WorldMap/$name/".$name.$phase."4.png");
$im5 = imagecreatefrompng("WorldMap/$name/".$name.$phase."5.png");
$im6 = imagecreatefrompng("WorldMap/$name/".$name.$phase."6.png");
$im7 = imagecreatefrompng("WorldMap/$name/".$name.$phase."7.png");
$im8 = imagecreatefrompng("WorldMap/$name/".$name.$phase."8.png");
$im9 = imagecreatefrompng("WorldMap/$name/".$name.$phase."9.png");
$im10 = imagecreatefrompng("WorldMap/$name/".$name.$phase."10.png");
$im11 = imagecreatefrompng("WorldMap/$name/".$name.$phase."11.png");
$im12 = imagecreatefrompng("WorldMap/$name/".$name.$phase."12.png");
imagecopy($im,$im1,0,0,0,0,256,256);
imagecopy($im,$im2,256,0,0,0,256,256);
imagecopy($im,$im3,512,0,0,0,256,256);
imagecopy($im,$im4,768,0,0,0,234,256);
imagecopy($im,$im5,0,256,0,0,256,256);
imagecopy($im,$im6,256,256,0,0,256,256);
imagecopy($im,$im7,512,256,0,0,256,256);
imagecopy($im,$im8,768,256,0,0,234,256);
imagecopy($im,$im9,0,512,0,0,256,156);
imagecopy($im,$im10,256,512,0,0,256,156);
imagecopy($im,$im11,512,512,0,0,256,156);
imagecopy($im,$im12,768,512,0,0,234,156);
$index=1;
$ix=0;
$iy=0;
function mapChunk($width,$height,$name,$zone)
{
$im=imagecreatetruecolor($width,$height);
$black = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($im, $black);
imagefill($im,0,0,$black);
imagealphablending($im, false);
imagesavealpha($im, true);
$tw=ceil($width/256);
$th=ceil($height/256);
$index=1;
for($y=0;$y < $th;$y++)
{
for($x=0;$x < $tw;$x++)
{
$cwidth = ((($x+1)*256) > $width ? $width - $x*256 :256);
$cheight = ((($y+1)*256) > $height ? $height - $y*256 :256);
$imx=imagecreatefrompng("Worldmap/$zone/".$name.$index.".png");
imagecopy($im,$imx,($x*256),($y*256),0,0,$cwidth,$cheight);
$index++;
}
}
return $im;
}
for($i=0;$i< count($mapData);$i++)
{
imagecopy($im,mapChunk($mapData[$i][1],$mapData[$i][2],$mapData[$i][0],$name),$mapData[$i][3],$mapData[$i][4],0,0,$mapData[$i][1],$mapData[$i][2]);
}
imagepng($im);
imagedestroy($im);
?>
Finally, this is the script used to parse the dbc files and insert it into the database
Code:
<?
function sUnpack($format,$data)
{
$temp=unpack($format,$data);
return $temp[1];
}
function prepStrings($stringTable)
{
$offsets=Array();
$strings=Array();
for($x=0;$x< strlen($stringTable);$x++)
{
if($stringTable[$x]=="\0")
{
$offsets[]=$x+1;
}
}
for($x=0;$x < count($offsets)-1;$x++)
{
$strings[$offsets[$x]]=substr($stringTable,$offsets[$x],$offsets[$x+1]-$offsets[$x]-1);
}
return $strings;
}
function makeTable($data,$fields,$rowSize)
{
$dataTable=Array();
$rows=str_split($data,$rowSize);
for($x=0;$x<count($rows);$x++)
{
$dataTable[]=str_split($rows[$x],4);
}
return $dataTable;
}
function formatTable($dataTable,$stringCols,$floatCols,$strings)
{
for($x=0;$x < count($dataTable[0]);$x++)
{
for($y=0;$y < count($dataTable);$y++)
{
if(in_array($x,$stringCols)){
@$dataTable[$y][$x]=$strings[sUnpack("l",$dataTable[$y][$x])];
}elseif(in_array($x,$floatCols))
{
$dataTable[$y][$x] = sUnpack("f",$dataTable[$y][$x]);
}
else
{
$dataTable[$y][$x] = sUnpack("l",$dataTable[$y][$x]);
}
}
}
return $dataTable;
}
/* DBC files don't contain any type information or field names,
so this info needs to be artificially provided to the function via
arrays containing 0-indexed column maps. For instance, if column 0
and column 3 are string type, $stringCols would equal Array(0,3)
field names/types courtesy of the WoWDev wiki*/
function readDBC($filename,$stringCols,$floatCols)
{
$handle = fopen($filename,"rb");
$test = fread($handle,4);
$records = sUnpack("L",fread($handle,4));
$fields = sUnpack("L",fread($handle,4));
$rowSize= sUnpack("L",fread($handle,4));
$stringSize= sUnpack("L",fread($handle,4));
if($test=="WDB2")
{
fseek($handle,28,SEEK_CUR);
}
$data=fread($handle,$records*$rowSize);
fseek($handle,-$stringSize,SEEK_END);
$stringTable=fread($handle,$stringSize);
$strings=prepStrings($stringTable);
$dataTable=makeTable($data,$fields,$rowSize);
$dataTable=formatTable($dataTable,$stringCols,$floatCols,$strings);
return $dataTable;
}
$overlay = readDBC("WorldMapOverlay.dbc",Array(6),Array());
$area = readDBC("WorldMapArea.dbc",Array(3),Array(4,5,6,7));
$mysqli = new mysqli("localhost", "root", "password", "WoWMapStuff");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
for($y=0;$y < count($area);$y++)
{
$valstring="";
for($x=0;$x < count($area[$y]);$x++)
{
$valstring.="'".$area[$y][$x]."'";
if($x < count($area[$y])-1)$valstring.=",";
}
$query1 = "INSERT INTO worldmaparea (ID,MapID,AreaTableID,AreaName,Y1,Y2,X1,X2,Flag,Flag2,Flag3,UNK1,UNK2,UNK3) VALUES($valstring)";
if(!$mysqli->query($query1))
{
echo $mysqli->error;
}
}
for($y=0;$y < count($overlay);$y++)
{
$valstring="";
for($x=0;$x < count($overlay[$y]);$x++)
{
$valstring.="'".$overlay[$y][$x]."'";
if($x < count($overlay[$y])-1)$valstring.=",";
}
$query1 = "INSERT INTO worldmapoverlay (ID,WorldMapAreaID,AreaTableID1,AreaTableID2,AreaTableID3,AreaTableID4,Name,Width,Height,X,Y,Y1,X1,Y2,X2,UNK) VALUES($valstring)";
if(!$mysqli->query($query1))
{
echo $mysqli->error;
}
}
?>
|