Example #1
0
function create($data)
{
    $o = decode($data['data']);
    $exists = doesExist($o['id']);
    $id = $exists ? 'null' : $o['id'];
    $sql = sprintf("insert into `drafts` values(%d, '%s', '%s', %d, %d, %d)", mysql_real_escape_string($id), mysql_real_escape_string($o['timestamp']), mysql_real_escape_string($o['content']), mysql_real_escape_string($o['x']), mysql_real_escape_string($o['y']), mysql_real_escape_string($o['z']));
    mysql_query($sql) or die(mysql_error());
    headercode(201);
    // If there was a conflict, we ignored the existing id, and created
    // a new id. Send back the new id.
    if ($exists) {
        echo mysql_insert_id();
    }
}
/**
 * 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
}