示例#1
0
 * @param json $data The JSON encoded data from the tree
 *
 * @package modx
 * @subpackage processors.layout.tree.element
 */
if (!$modx->hasPermission('element_tree')) {
    return $modx->error->failure($modx->lexicon('permission_denied'));
}
$modx->lexicon->load('category');
$data = urldecode($scriptProperties['data']);
$data = $modx->fromJSON($data);
sortNodes($modx, 'modTemplate', 'template', $data);
sortNodes($modx, 'modTemplateVar', 'tv', $data);
sortNodes($modx, 'modChunk', 'chunk', $data);
sortNodes($modx, 'modSnippet', 'snippet', $data);
sortNodes($modx, 'modPlugin', 'plugin', $data);
if (!empty($data['n_category']) && is_array($data['n_category'])) {
    /* if dropping an element onto a category, do that here */
    foreach ($data['n_category'] as $key => $elements) {
        if (!is_array($elements) || empty($elements)) {
            continue;
        }
        $key = explode('_', $key);
        if (empty($key[1]) || empty($key[2]) || $key[1] != 'category') {
            continue;
        }
        foreach ($elements as $elKey => $elArray) {
            $elKey = explode('_', $elKey);
            if (empty($elKey[1]) || empty($elKey[3])) {
                continue;
            }
/**
 * function runAstar
 * 
 * traverse the grid useing the A* algorithm  
 * 
 * @param Grid      $grid will hold the map of the world we are searching 
 * @param int       $start_row the row of the start node 
 * @param int       $start_col the col of the start node
 * @param int       $goal_row  the row of the goal node
 * @param int       $goal_col the col of the goal node
 * @param int       $max_row the last row index
 * @param int       $max_col the last col index
 * @return bool     true if path exsit, false otherwise
 */
function runAstar($grid, $start_row, $start_col, $goal_row, $goal_col, $max_row, $max_col)
{
    //initialize the open list
    $open_list = array();
    //initialize the closed list
    $close_list = array();
    $b_end_search = false;
    //if true we must end the search
    $goal_node =& $grid->nodes[$goal_row][$goal_col];
    $start_node =& $grid->nodes[$start_row][$start_col];
    $start_node->f = 0;
    $open_list[] =& $start_node;
    sortNodes($open_list, 'ASCE');
    //while the open list is not empty
    while (!empty($open_list)) {
        $q = null;
        //current_node
        //find the node with the least f on the open list, call it "q"
        //pop q off the open list
        sortNodes($open_list, 'ASCE');
        pop($open_list, 0, $q);
        //generate q's 8 successors and set their parents to q
        $successors = findSuccessors($grid->nodes, $q->row, $q->col, $max_row, $max_col);
        foreach ($successors as $key => $successor) {
            $temp = clone $successor;
            $temp->parent_node =& $q;
            //if the successor is an obstacle
            if (isObstacle($temp)) {
                continue;
                // skip this successor if it is an obstacle
            }
            if (isGoal($temp, $goal_node)) {
                $successor->parent_node = $temp->parent_node;
                $b_end_search = true;
                break;
            }
            $temp->g = $q->g + 1;
            //
            $temp->h = sqrt(pow($goal_node->row - $temp->row, 2) + pow($goal_node->col - $temp->col, 2));
            $temp->f = $temp->g + $temp->h;
            //
            //            $successor->g = $q->g + 1; //
            //            $successor->h = sqrt(pow(($goal_node->row - $successor->row), 2) + pow(($goal_node->col - $successor->col), 2));
            //            $successor->f = $successor->g + $successor->h;
            //does exist in open list
            if (doesExist($successor, $open_list) && $successor->f < $temp->f) {
                continue;
            }
            //does exist in close list
            if (doesExist($successor, $close_list) && $successor->f < $temp->f) {
                continue;
            }
            //
            $successor->parent_node = $temp->parent_node;
            $successor->g = $temp->g;
            $successor->h = $temp->h;
            $successor->f = $temp->f;
            $open_list[] =& $successors[$key];
            //&$successor;
        }
        $close_list[] =& $grid->nodes[$q->row][$q->col];
        //&$q;
        if ($b_end_search == true) {
            break;
        }
    }
    return $b_end_search ? true : false;
    //return true if there is a path, false otherwise
}