<?
function waypoints_draw(){

    include('util.php');
    checkAccess('listnpc', '', 'read');

    header("Content-type: image/png");

    $sector = $_GET['sector'];
    $image_name = $sector.".gif";

    $data = getDataFromArea($sector);
    $sectors = $data[0];
    $centerx = $data[1];
    $centery = $data[2];
    $scalefactor = $data[3];

    #Open gif and copy into truecolor png.
    $gifim     = imagecreatefromgif($image_name);
    $w         = imagesx($gifim);
    $h         = imagesy($gifim);
    $im        = imagecreatetruecolor($w,$h);
    imagecopy($im,$gifim,0,0,0,0,$w,$h);

    $orange = imagecolorallocate($im, 220, 210, 60);

    $query = $query . " select id,x,y,z,radius from sc_waypoints where " . $sectors;
    //echo $query;
    $res = mysql_query2($query);

    $i=0;
    while ($line = mysql_fetch_array($res, MYSQL_NUM)){
        $id      = $line[0];
        $x       = $line[1];
        $y       = $line[2];
        $z       = $line[3];
        $radius  = $line[4];

	$ix = $centerx+($x*$scalefactor);
	$iy = $centery-($z*$scalefactor);
        $ir = $radius*$scalefactor;
        imagearc($im,$ix,$iy,$ir,$ir,0,360,$orange);

	#imagestring($im,3,0,$i*9,$id,$orange);$i++;

        $query2 = "select wp1.x,wp1.y,wp1.z,wp2.x,wp2.y,wp2.z,l.flags from sc_waypoint_links l, sc_waypoints wp1, sc_waypoints wp2 where l.wp1 = wp1.id and l.wp2 = wp2.id and wp1.id = ".$id;
        $res2=mysql_query2($query2); 
        while ($line2 = mysql_fetch_array($res2, MYSQL_NUM)){
            $x1 = $line2[0];
            $y1 = $line2[1];
            $z1 = $line2[2];
            $x2 = $line2[3];
            $y2 = $line2[4];
            $z2 = $line2[5];
            $flags = $line2[6];
            #imagestring($im,3,0,$i*9,"Link: ".$x1." ".$y1." ".$z1,$orange);$i++;

            $ix1 = $centerx+($x1*$scalefactor);
            $iy1 = $centery-($z1*$scalefactor);
            $ix2 = $centerx+($x2*$scalefactor);
            $iy2 = $centery-($z2*$scalefactor);

            #imagestring($im,3,0,$i*9,"Link: ".$ix1." ".$iy1." ".$ix2." ".$iy2." ".$flags,$orange);$i++;

            imageline($im,$ix1,$iy1,$ix2,$iy2 , $orange);
            if (stristr($flags, 'ONEWAY') != FALSE)
            {
                 $cx = ($ix1+$ix2)/2;
                 $cy = ($iy1+$iy2)/2;
                 $a = atan2($iy1-$iy2,$ix1-$ix2);
                 $dx = 10*cos($a+0.6);
                 $dy = 10*sin($a+0.6);
                 imageline($im,$cx,$cy,$cx+$dx,$cy+$dy,$orange);
                 $dx = 10*cos($a-0.6);
                 $dy = 10*sin($a-0.6);
                 imageline($im,$cx,$cy,$cx+$dx,$cy+$dy,$orange);

                 #imagestring($im,3,0,$i*9,"Oneway",$orange);$i++;
            }
        }
    }


    imagegif($im);
    imagedestroy($im);
}
?>
