/** * Return the correct path between two points, * avoiding all objects (except same side objects) and * unpassable locations. */ public function getPath(Dolumar_Underworld_Models_Side $side, Neuron_GameServer_Map_Location $start, Neuron_GameServer_Map_Location $end, $maxRadius, $isTargetAnObject = false) { // Prepare thze area that needs to be inspected $minx = $start->x() - $maxRadius; $miny = $start->y() - $maxRadius; $maxx = $start->x() + $maxRadius; $maxy = $start->y() + $maxRadius; $area = array(); for ($y = 0; $y < $maxRadius * 2; $y++) { $area[$y] = array(); for ($x = 0; $x < $maxRadius * 2; $x++) { $area[$y][$x] = $this->getPassCost($minx + $x, $miny + $y); } } // Mark all objects as not passable $center = array($minx + ($maxx - $minx) / 2, $miny + ($maxy - $miny) / 2); $range = $maxRadius * 2; $objarea = new Neuron_GameServer_Map_Area(new Neuron_GameServer_Map_Location($center[0], $center[1]), $range); $objects = $this->getMapObjectManager()->getDisplayObjects($objarea); foreach ($objects as $v) { $location = $v->getLocation(); $area[floor($location[1]) - $miny][floor($location[0]) - $minx] = 0; } $transformed_start = $start->transform(-$minx, -$miny); $transformed_end = $end->transform(-$minx, -$miny); // If the target is an object, // it should always be passable if ($isTargetAnObject) { $area[$transformed_end->y()][$transformed_end->x()] = 1; } // Path finder $pathfinder = new Neuron_GameServer_Map_Pathfinder(); $pathfinder->setMap($area); $path = $pathfinder->getPath($transformed_start, $transformed_end, $maxRadius); if ($path) { $path->transform(+$minx, +$miny); } return $path; }
$row = 0; $col = 0; foreach (str_split($input) as $v) { if ($v == PHP_EOL) { $col = 0; $row++; $map[$row] = array(); } else { $map[$row][] = $v; $col++; } } $rows = count($map); $cols = count($map[0]); // Set the map $pathfinder = new Neuron_GameServer_Map_Pathfinder(); $pathfinder->setMap($map); // Get the path $path = $pathfinder->getPath($startlocation, $endlocation); // Display the map (just to make it easier) $squarewidth = 10; $sizex = $cols * $squarewidth; $sizey = $rows * $squarewidth; $img = imagecreatetruecolor($sizex, $sizey); $white = imagecolorallocate($img, 255, 255, 255); imagefilledrectangle($img, 0, 0, $sizex, $sizey, $white); $colors = array(); $colors[0] = imagecolorallocate($img, 0, 0, 0); $colors[1] = imagecolorallocate($img, 220, 220, 250); $colors[2] = imagecolorallocate($img, 200, 200, 230); $colors[3] = imagecolorallocate($img, 180, 180, 210);