map util


/**
* Some utilities to deal with the map data
*/

/**
* Convert a encoded map string into a 2D array.
*
* @param string $str the encoded map data
* @return array the 2D map
*/
function map_extract($str)
{
$map=array();
$a=0;
$b=0;
$p=0;

for($i=0;$i < strlen($str);)
{
$c=substr($str,$i,1);
if($c == '-')
{
$i++;
$l='';
for(;$i < strlen($str);$i++)
{
$c=substr($str,$i,1);
if(ord($c) >= 48 && ord($c) <= 57)
{
$l.=$c;
continue;
}
else
break;
}
$nb=$l+0;
$v=(ord(substr($str,$i,1))-97)*26+(ord(substr($str,$i+1,1))-97);
for($j=0;$j < $nb;$j++)
{
$map[$a][$b]=$v;
$a++;
if($a >= 100)
{
$a=0;
$b++;
}
}
$i+=2;
}
else
{
$v=(ord(substr($str,$i,1))-97)*26+(ord(substr($str,$i+1,1))-97);
$map[$a][$b]=$v;
$a++;
if($a >= 100)
{
$a=0;
$b++;
}
$i+=2;
}
}
return $map;
}

/**
* Convert a 2D map array into a encoded map string.
*
* @param array $map
* @return string
*/
function map2str($map)
{
$str="";
$oldcell=$map[0][0];
$nb=0;

for($y=0;$y < 100;$y++)
{
for($x=0;$x < 100;$x++)
{
if($map[$x][$y] == $oldcell)
$nb++;
else
{
if($nb > 1)
$str.="-".$nb.chr(floor($oldcell/26)+97).chr(($oldcell%26)+97);
else
$str.=chr(floor($oldcell/26)+97).chr(($oldcell%26)+97);
$nb=1;
$oldcell=$map[$x][$y];
}
}
}

// Add the remaining...
if($nb > 0)
{
if($nb > 1)
$str.="-".$nb.chr(floor($oldcell/26)+97).chr(($oldcell%26)+97);
else
$str.=chr(floor($oldcell/26)+97).chr(($oldcell%26)+97);
}

return $str;
}

/**
* Run length encode the encoded map string. This saves space on
* the database as well as network during the transfer at
* the cost of some CPU.
*
* @param string $str
* @return string
*/
function lenght_code($str)
{
if(strpos($str,"-") !== false) // Already compressed, skip this step.
return $str;
$data=array();
for($p=0;$p < strlen($str);$p+=2)
{
if(substr($str,$p,1) == "")
$n=0;
else
$n=(ord(substr($str,$p,1))-97)*26+(ord(substr($str,$p+1,1))-97);
$data[]=$n;
}

$res="";
for($i=0;$i < count($data);)
{
$d=$data[$i];
$n=0;
for($j=$i;$j < count($data);$j++)
{
if($data[$j] != $d)
break;
$n++;
}
if($n > 2)
{
$res.="-".$n.chr(floor($d/26)+97).chr(($d%26)+97);
$i+=$n;
}
else
{
$res.=chr(floor($d/26)+97).chr(($d%26)+97);
$i++;
}
}
return $res;
}

/**
* Determine is a cell type is walkable or not.
*
* @param integer $type
* @param boolean $boat
* @return boolean
*/
function check_walkable_map($type,$boat = false)
{
global $objwalk,$bgwalk,$basedir;

if(!isset($bgwalk))
{
if(file_exists("$basedir/libs/walkable.php"))
include_once "$basedir/libs/walkable.php";
else
{
$objwalk=str_pad("",600,"Y");
$bgwalk=str_pad("",600,"Y");
}
}

if($boat == true && $type >= 52 && $type <= 64) // Water
return true;
if($boat == true && $type >= 121 && $type <= 132) // Ice & Water
return true;
if($flagboat == true && $type >= 165 && $type <= 172) // Ice & Water & Sand
return true;

if(substr($bgwalk,$type,1) == "N")
return false;
return true;
}

/**
* retreives the current player location and returns it in the global variables sx,sy;
*
*/
function retreive_loc()
{
global $uservals,$db,$sx,$sy,$userid;

$mapstr="";
if($uservals["PREVLOC"] <> "")
$_GET["PREVLOC"]=$uservals["PREVLOC"];

if($_GET["X"] != "" && $_GET["Y"] != "")
{
$sx=$_GET["X"];
$sy=$_GET["Y"];
$db->Execute("UPDATE PLAYER SET MAPX=$sx, MAPY=$sy WHERE ID = $userid");
}
else if(($uservals['LOCATION']+0) < 0 && $_GET["PREVLOC"] <> "")
{
$sx=$sy=-1;
$sql="SELECT OBJDATA,MAPDATA FROM PLAYER_MAP WHERE USERID=$userid AND LEVEL = ".(-$uservals['LOCATION']);
$r=$db->Execute($sql);
$objstr=$r->fields[0];
$mapstr=$r->fields[1];
$r->Close();
$obj=map_extract($objstr);
$p=0;
for($j=0;$j < 100;$j++) // Convert the string into the map
{
for($i=0;$i < 100;$i++)
{
$n=$obj[$i][$j];
if(($n == 114 || $n == 118) && $_GET["DIR"] == "DOWN")
{
$sx=$i;
$sy=$j;
break;
}
if(($n == 115 || $n == 117) && $_GET["DIR"] == "UP")
{
$sx=$i;
$sy=$j;
break;
}
$p+=2;
}
if($sx != -1)
break;
}
if($sx == -1)
$sx=$sy=1;
$db->Execute("UPDATE PLAYER SET MAPX=$sx, MAPY=$sy WHERE ID = $userid");
}
else if($_GET["PREVLOC"] <> "" && $_GET["X"] <> "")
{
$sx=$_GET["X"];
$sy=$_GET["Y"];
$db->Execute("UPDATE PLAYER SET MAPX=$sx, MAPY=$sy WHERE ID = $userid");
}
else if($_GET["PREVLOC"] <> "")
{
if($_GET["X"] != "")
$sql="SELECT X,Y FROM MAPLINKS WHERE MAPID = {$uservals['LOCATION']} AND NEWLOC = {$_GET['PREVLOC']} AND X = ".$_GET["X"]." AND Y = ".$_GET["Y"];
else
$sql="SELECT X,Y FROM MAPLINKS WHERE MAPID = {$uservals['LOCATION']} AND NEWLOC = {$_GET['PREVLOC']}";
$r=$db->Execute($sql);
if($r->EOF)
{
$r->Close();
$db->Close();
echo "WHERE DO YOU COME FROM ?\n";
die();
}
$sx=$r->fields[0]+0;
$sy=$r->fields[1]+0;
$r->Close();
$db->Execute("UPDATE PLAYER SET MAPX=$sx, MAPY=$sy WHERE ID = $userid");
}
else
{
$sx=$uservals["MAPX"];
$sy=$uservals["MAPY"];
}

if(($uservals["LOCATION"]+0) < 0 && $mapstr == "")
{
$sql="SELECT MAPDATA FROM PLAYER_MAP WHERE USERID=$userid AND LEVEL = ".(-$uservals['LOCATION']);
$r=$db->Execute($sql);
$mapstr=$r->fields[0];
$r->Close();
}
else if(($uservals["LOCATION"]+0) > 0 && $mapstr == "")
{
$sql="SELECT MAPDATA FROM MAPS WHERE ID = ".$uservals['LOCATION'];
$r=$db->Execute($sql);
$mapstr=$r->fields[0];
$r->Close();
}
$map=map_extract($mapstr);
$boat=false;
if(inventory_check(4051) > 0 || inventory_check(4055) > 0)
$boat=true;
if(check_walkable_map($map[$sx][$sy],$boat) == false) // Some error occured here...
{
activity_performed(3);
$db->Execute("UPDATE PLAYER SET LOCATION=RESURECT WHERE ID = $userid");
ob_end_clean();
echo "Wrong position in the map.
\n";
echo "[Mmmmm....]\n";
$db->Close();
exit;
}
}

/**
* Insert an object on the map... used for a daily unique object.
*
* @param unknown_type $objid
*/
function unique_place($objid)
{
global $db;

$maps=array(8,9,17,21,42);
$mapid=$maps[mt_rand(0,count($maps)-1)];
$r=$db->Execute("SELECT MAPDATA,OBJECTDATA FROM MAPS WHERE ID = $mapid");
$map=map_extract($r->fields[0]);
$obj=map_extract($r->fields[1]);
$r->Close();

while(true)
{
$x=mt_rand(1,98);
$y=mt_rand(1,98);

if(check_walkable_map($map[$x][$y]) == true && $obj[$x][$y] == 0)
break;
}
$db->Execute("DELETE FROM UNIQUE_ITEMS WHERE OBJECTID=$objid");
$db->Execute("INSERT INTO UNIQUE_ITEMS(MAPID,OBJECTID,X,Y) VALUES($mapid,$objid,$x,$y)");
}

/**
* Returns the link to go to a neirboor map locations
*
* @param integer $x
* @param integer $y
* @return string
*/
function grid_link($x,$y)
{
global $db;
$r=$db->Execute("SELECT ID FROM MAP_GRID WHERE X = $x AND Y = $y");
$str="game.php?NEWLOC=".$r->fields[0];
$r->Close();
return $str;
}

/**
* Returns the number of cells with a given type
*
* @param array $map
* @param integer $type
* @return integer
*/
function map_count($map,$type)
{
if(is_array($type))
$types=$type;
else
$types[]=$type;

$nb=0;
for($i=0;$i < 99;$i++)
for($j=0;$j < 99;$j++)
if(in_array($map[$i][$j],$types))
$nb++;
return $nb;
}
?>


Wyszukiwarka

Podobne podstrony:
map util
image map util
image map util
image map util
image map util
map util
map util
Util js
browse map
map settings
view map
group util ?lay
Map js
TSM UTIL
Andorra mountain map
map settings

więcej podobnych podstron