All pastes #93454 Raw Edit

Anonymous

public text v1 · immutable
#93454 ·published 2006-07-20 13:16 UTC
rendered paste body
<pre>
<?
error_reporting(E_ERROR);
set_time_limit(99999999);
$missed_spawns=0;
$total_spawn_mobs_missed=0;
// list of fields, seperate each field with a | no spaces inbetween
// try to spell them exactly as is, but can also use php-style regex
// ID IS A REQUIRED FIELD :P
$fields = "npc|pos|total|respawn";

// read file to memoery
$contents = file_get_contents('npc1.txt');
// explode contents into array, each element = 1 line
$arr1 = explode("\n", $contents);
// loop thru each line
foreach ($arr1 as $key => $val) {

        // explode each line by tab seperation
        $arr2[$key] = explode("\t", $val);

        foreach($arr2[$key] as $keye => $vale){
          $arr2[$key][$keye] = str_replace("\t", "", $vale);
        }

        if ($arr2[$key][0] == "territory_begin") $area = $arr2[$key][2];

        if ($arr2[$key][0] == "npcmaker_begin" || $arr2[$key][0] == "npcmaker_ex_begin") {
          $temp = explode(";", $arr2[$key][1], 2);
          $territory = trim($temp[0], "[]");
          //if ($territory != "")
          //echo "\nTerritory = ".$territory."\n";
        }

        //if ($territory == "")

        else if ($arr2[$key][0] == "npc_begin" || $arr2[$key][0] == "npc_ex_begin") {

        $arr2[$key][1] = "npc=".$arr2[$key][1];

        if ($arr2[$key][2] != "pos=anywhere") continue;

        //echo "".$arr2[$key][1]." ".$arr2[$key][2]."\n";

        foreach ($arr2[$key] as $k => $v) {
                // allow only specified fields to be recorded...
                if ( ereg($fields, $v) ) {
                        // explode by = sign
                        $temp = explode("=", $v, 2);
                        // trim the characters [,],\n and whitespace from left and right
                        // if neccassary and add to our final array
                        $output = trim($temp[1], "[]");
                        $finalArray[$territory][$area][$temp[0]][] = $output;
                        // territory[area][name][orc];
                        // territory[area]][pos][{position}]
                }
        }
 }
}

function isInTriangle($xa,$ya,$xb,$yb,$xc,$yc,$xd,$yd)
{
    $delta_ab=($xa-$xb)^2+($ya-$yb)^2;
    $delta_ac=($xa-$xc)^2+($ya-$yc)^2;
    $delta_bc=($xb-$xc)^2+($yb-$yc)^2;
    $delta_ad=($xa-$xd)^2+($ya-$yd)^2;
    $delta_bd=($xb-$xd)^2+($yb-$yd)^2;
    $delta_cd=($xc-$xd)^2+($yc-$yd)^2;

    if ($delta_ac+$delta_bc<$delta_ad+$delta_bd) return 0;
    if ($delta_bc+$delta_ab<$delta_ad+$delta_cd) return 0;
    if ($delta_ab+$delta_ac<$delta_bd+$delta_cd) return 0;

    return 1;
}

function TestDistance($ma,$x,$y) {
    if (count($ma)==0) return 1;
    foreach ($ma as $kma => $mav)
        if ((($mav[0]-$x)^2+($mav[1]-$y)^2)<400) return 0;
    return 1;
}

function AlreadyUsed($ma, $x1, $y1) {
    if (count($ma)==0) return 0;
    foreach ($ma as $kma => $mav)
        if ($mav[0]==$x1 and $mav[1]==$y1) return 1;
    return 0;
}

function RotationFind($c,&$ma,$idx) {
    $d1=400;
    $d2=282;
    $x1=$ma[$idx][0]+$d1;
    $y1=$ma[$idx][1];
    if (!AlreadyUsed($ma,$x1,$y1)) {
        $b=0;
        for ($a=1; $a<count($c)-2; $a++)
            $b=$b+isInTriangle($c[0][0],$c[0][1],
                    $c[$a][0],$c[$a][1],
                    $c[$a+1][0],$c[$a+1][1],
                    $x1,$y1);
        if ($b>0) {
            $ma[]=array($x1,$y1,$c[0][3],0);
        };
    };
    $x1=$ma[$idx][0]+$d2;
    $y1=$ma[$idx][1]+$d2;
    if (!AlreadyUsed($ma,$x1,$y1)) {
        $b=0;
        for ($a=1; $a<count($c)-2; $a++)
            $b=$b+isInTriangle($c[0][0],$c[0][1],
                    $c[$a][0],$c[$a][1],
                    $c[$a+1][0],$c[$a+1][1],
                    $x1,$y1);
        if ($b>0) {
            $ma[]=array($x1,$y1,$c[0][3],0);
        };
    };
    $x1=$ma[$idx][0];
    $y1=$ma[$idx][1]+$d1;
    if (!AlreadyUsed($ma,$x1,$y1)) {
        $b=0;
        for ($a=1; $a<count($c)-2; $a++)
            $b=$b+isInTriangle($c[0][0],$c[0][1],
                    $c[$a][0],$c[$a][1],
                    $c[$a+1][0],$c[$a+1][1],
                    $x1,$y1);
        if ($b>0) {
            $ma[]=array($x1,$y1,$c[0][3],0);
        };
    };
    $x1=$ma[$idx][0]-$d2;
    $y1=$ma[$idx][1]+$d2;
    if (!AlreadyUsed($ma,$x1,$y1)) {
        $b=0;
        for ($a=1; $a<count($c)-2; $a++)
            $b=$b+isInTriangle($c[0][0],$c[0][1],
                    $c[$a][0],$c[$a][1],
                    $c[$a+1][0],$c[$a+1][1],
                    $x1,$y1);
        if ($b>0) {
            $ma[]=array($x1,$y1,$c[0][3],0);
        };
    };
    $x1=$ma[$idx][0]-$d1;
    $y1=$ma[$idx][1];
    if (!AlreadyUsed($ma,$x1,$y1)) {
        $b=0;
        for ($a=1; $a<count($c)-2; $a++)
            $b=$b+isInTriangle($c[0][0],$c[0][1],
                    $c[$a][0],$c[$a][1],
                    $c[$a+1][0],$c[$a+1][1],
                    $x1,$y1);
        if ($b>0) {
            $ma[]=array($x1,$y1,$c[0][3],0);
        };
    };
    $x1=$ma[$idx][0]-$d2;
    $y1=$ma[$idx][1]-$d2;
    if (!AlreadyUsed($ma,$x1,$y1)) {
        $b=0;
        for ($a=1; $a<count($c)-2; $a++)
            $b=$b+isInTriangle($c[0][0],$c[0][1],
                    $c[$a][0],$c[$a][1],
                    $c[$a+1][0],$c[$a+1][1],
                    $x1,$y1);
        if ($b>0) {
            $ma[]=array($x1,$y1,$c[0][3],0);
        };
    };
    $x1=$ma[$idx][0];
    $y1=$ma[$idx][1]-$d1;
    if (!AlreadyUsed($ma,$x1,$y1)) {
        $b=0;
        for ($a=1; $a<count($c)-2; $a++)
            $b=$b+isInTriangle($c[0][0],$c[0][1],
                    $c[$a][0],$c[$a][1],
                    $c[$a+1][0],$c[$a+1][1],
                    $x1,$y1);
        if ($b>0) {
            $ma[]=array($x1,$y1,$c[0][3],0);
        };
    };
    $x1=$ma[$idx][0]+$d2;
    $y1=$ma[$idx][1]-$d2;
    if (!AlreadyUsed($ma,$x1,$y1)) {
        $b=0;
        for ($a=1; $a<count($c)-2; $a++)
            $b=$b+isInTriangle($c[0][0],$c[0][1],
                    $c[$a][0],$c[$a][1],
                    $c[$a+1][0],$c[$a+1][1],
                    $x1,$y1);
        if ($b>0) {
            $ma[]=array($x1,$y1,$c[0][3],0);
        };
    };
    $ma[$idx][3]=1;
}

function FindUnchecked($ma) {
    if (count($ma)==0) return -1;
    foreach($ma as $kam => $mav) {
        if ($mav[3]==0) return $kam;
    };
    return -1;
};

// display final array
// if you DO NOT want to see the array output, just comment the next line out
//var_dump($finalArray);

// generating sql statements and stuffs
echo "\n\n\nSQL STATEMENTS:\n";
$key = $val = $k = $v = null;
echo "INSERT INTO `spawnlist` values\n";
foreach ($finalArray as $key => $val) {
        // example of the sql statement formation
        // --------------------------------------------------

        // take all the descriptions in case of multiple descriptions and put it into one var.
        foreach($val as $ky => $vl) {
          /*foreach($vl['npc'] as $keke => $vele)
          {
            echo $vele."\n";
          }*/
          unset($arrPoints);
          preg_match_all("/{(-?\d+);(-?\d+);(-?\d+);(-?\d+)}/", $ky, $arrPoints, PREG_SET_ORDER);
          unset($xx);
          unset($xy);
          unset($c);
          $xx = array();
          $xy = array();
          $c= array();
          foreach ( $arrPoints as $point => $arrStuff )
          {
            $xx[]=intval($arrStuff[1]);
        $xy[]=intval($arrStuff[2]);
            $c[]=array(intval($arrStuff[1]),intval($arrStuff[2]),intval($arrStuff[3]),intval($arrStuff[4]));
      }
      sort($xx);
          sort($xy);
          $minx=$xx[0];
          $maxx=$xx[count($xx)-1];
          $miny=$xy[0];
          $maxy=$xy[count($xy)-1];
          $z = $c[0][3];
          $heading = $c[0][4];

          $total = 0;

          foreach($vl['total'] as $pk=> $vk) {
            echo "Name: ".$vl['npc'][$pk]."\n";
            $total += $vk;
        }

                //$count = count($vl['total']);
        $index = 0;
        $i=0;
        $check = 0;
        unset($mob);
        $mob=array();
        srand();
        //add poly edges as valid spawns
        foreach ($c as $i => $j)
            $mob[]=array($j[0],$j[1],$j[2],0);
        //do we need more spawn points?
        if (count($mob)<$total) {
            $a=FindUnchecked($mob);
            while ($a>-1) {
                RotationFind($c,$mob,$a);
                $a=FindUnchecked($mob);
                if (count($mob)>=$total) break;
            };
        }
        if (count($mob)<$total) {
            $missed_spawns++;
            $total_spawn_mobs_missed=$total_spawn_mobs_missed+($total-count($mob));
        };
        if (count($mob)>=$total) {
              for ($ii=0; $ii<$total; $ii++) {
                srand();
                $jj=rand(0,count($mobs)-1);
                while ($mob[$jj][0]==0) $jj=rand(0,count($mob)-1);

                $x=$mob[$jj][0];
                $y=$mob[$jj][1];
                $z=$mob[$jj][3];
                $mob[$jj]=0;
              $respawn = $vl['respawn'][$index];

                          if (strpos($respawn, "hour"))
                          {
                            $respawn = str_replace("hour", "", $respawn);
                                $respawn = $respawn * 3600;
                          }
                          else if (strpos($respawn, "min"))
                          {
                            $respawn = str_replace("min", "", $respawn);
                                $respawn = $respawn * 60;
                          }
                          else
                          {
                            $respawn = str_replace("sec", "", $respawn);
                          }

                          if ($respawn == "no" || $respawn == 0) $respawn = 60;

                          echo "('null', '".$key."', '1', '".$vl['npc'][$index]."', '".$x."', '".$y."', '".$z."', '0', '0', '".$heading."', '".$respawn."', '0', '0'), \n";

                          $i++;
                          $check++;

                          if ($check == $vl['total'][$index])
                          {
                                $index++;
                                $check = 0;
                          }
                  }
                } else {
                echo "Adding partial spawn: ".count($mob)." from total of: $total\n";
              for ($ii=0; $ii<count($mob); $ii++) {

                $x=$mob[$ii][0];
                $y=$mob[$ii][1];
                $z=$mob[$ii][3];
                $mob[$ii]=0;
              $respawn = $vl['respawn'][$index];

                          if (strpos($respawn, "hour"))
                          {
                            $respawn = str_replace("hour", "", $respawn);
                                $respawn = $respawn * 3600;
                          }
                          else if (strpos($respawn, "min"))
                          {
                            $respawn = str_replace("min", "", $respawn);
                                $respawn = $respawn * 60;
                          }
                          else
                          {
                            $respawn = str_replace("sec", "", $respawn);
                          }

                          if ($respawn == "no" || $respawn == 0) $respawn = 60;

                          echo "('null', '".$key."', '1', '".$vl['npc'][$index]."', '".$x."', '".$y."', '".$z."', '0', '0', '".$heading."', '".$respawn."', '0', '0'), \n";

                          $i++;
                          $check++;

                          if ($check == $vl['total'][$index])
                          {
                                $index++;
                                $check = 0;
                          }
                  }



                }
        }

}
echo "missed_spawns: $missed_spawns, total_spawn_points_missed: $total_spawn_mobs_missed\n";
?>
</pre>