Example #1
0
void generate_maze(long col, uint16_t (*cell)[col], long row, struct node *head
                  ,long cl, char (*maze)[cl], long rl)
{
        set_seed();
        long cell_count = row*col;
        long visited_count = 1;

        // selected col/row
        long sc = get_rand(col);
        long sr = get_rand(row);

        // keep 1 before for visual (knock down wall)
        long sc_old = sc;
        long sr_old = sr;

        /* printf("AT (%ld, %ld)\n", sc, sr); */
        int *dir_good = malloc(sizeof(int) * 4);
        update_maze(cl, maze, rl, sr, sc, sr_old, sc_old, START);
        while (visited_count < cell_count) {
                int neighbor_count = find_neighbors(col, cell, row, sc, sr, dir_good);
                if (neighbor_count > 0) {
                        // pick a direction to go
                        sc_old = sc;
                        sr_old = sr;
                        select_dir(col, cell, &sc, &sr, dir_good, head);
                        update_maze(cl, maze, rl, sr, sc, sr_old, sc_old, PATH);
                        visited_count++;
                } else {
                        // backtrack
                        struct coord loc = stack_pop(head);
                        if (loc.col > sc) cell[sr][sc] = cell[sr][sc] | 0x2000; // east
                        if (loc.col < sc) cell[sr][sc] = cell[sr][sc] | 0x8000; // west
                        if (loc.row > sr) cell[sr][sc] = cell[sr][sc] | 0x4000; // south
                        if (loc.row < sr) cell[sr][sc] = cell[sr][sc] | 0x1000; // north

                        // new loc
                        update_maze(cl, maze, rl, loc.row, loc.col, sr, sc, BACK);
                        sc = loc.col;
                        sr = loc.row;
                        /* printf("BA (%ld, %ld)\n", sc, sr); */
                }
        }
        update_maze(cl, maze, rl, sr, sc, sr_old, sc_old, END);
        free(dir_good);
}
Example #2
0
void resolve(int fd, int nl, struct band3 *bnd)
{
    CELL cvalue;
    int *active;
    int offset, isz, i, j, pass, activity, goagain, done;

    active = (int *)G_calloc(nl, sizeof(int));

    isz = sizeof(CELL);

    /* select a direction when there are multiple non-flat links */
    lseek(fd, bnd->sz, SEEK_SET);
    for (i = 1; i < nl - 1; i += 1) {
	read(fd, bnd->b[0], bnd->sz);
	for (j = 1; j < bnd->ns - 1; j += 1) {
	    offset = j * isz;
	    if (G_is_c_null_value((void *)(bnd->b[0] + offset)))
		continue;
	    memcpy(&cvalue, bnd->b[0] + offset, isz);
	    if (cvalue > 0)
		cvalue = select_dir(cvalue);
	    memcpy(bnd->b[0] + offset, &cvalue, isz);
	}
	lseek(fd, -bnd->sz, SEEK_CUR);
	write(fd, bnd->b[0], bnd->sz);
    }

    pass = 0;
    for (i = 1; i < nl - 1; i += 1)
	active[i] = 1;

    /* select a direction when there are multiple flat links */
    do {
	done = 1;
	pass += 1;

	activity = 0;

	lseek(fd, 0, SEEK_SET);
	advance_band3(fd, bnd);
	advance_band3(fd, bnd);
	for (i = 1; i < nl - 1; i++) {
	    lseek(fd, (off_t) (i + 1) * bnd->sz, SEEK_SET);
	    advance_band3(fd, bnd);

	    if (!active[i])
		continue;

	    done = 0;
	    active[i] = 0;
	    do {
		goagain = 0;
		for (j = 1; j < bnd->ns - 1; j += 1) {
		    flink(i, j, nl, bnd->ns,
			  (void *)bnd->b[0], (void *)bnd->b[1],
			  (void *)bnd->b[2], &active[i], &goagain);
		    if (goagain)
			activity = 1;
		}
	    } while (goagain);

	    lseek(fd, (off_t) i * bnd->sz, SEEK_SET);
	    write(fd, bnd->b[1], bnd->sz);

	}

	if (!activity) {
	    done = 1;
	    continue;
	}

	activity = 0;

	lseek(fd, (off_t) (nl - 1) * bnd->sz, SEEK_SET);
	retreat_band3(fd, bnd);
	retreat_band3(fd, bnd);
	for (i = nl - 2; i >= 1; i -= 1) {
	    lseek(fd, (off_t) (i - 1) * bnd->sz, SEEK_SET);
	    retreat_band3(fd, bnd);

	    if (!active[i])
		continue;

	    done = 0;
	    active[i] = 0;
	    do {
		goagain = 0;
		for (j = 1; j < bnd->ns - 1; j++) {
		    flink(i, j, nl, bnd->ns,
			  (void *)bnd->b[0], (void *)bnd->b[1],
			  (void *)bnd->b[2], &active[i], &goagain);
		    if (goagain)
			activity = 1;
		}
	    } while (goagain);

	    lseek(fd, (off_t) i * bnd->sz, SEEK_SET);
	    write(fd, bnd->b[1], bnd->sz);
	}

	if (!activity) {
	    done = 1;
	}

    } while (!done);

    G_free(active);

    return;

}
Example #3
0
void flink(int i, int j, int nl, int ns, CELL * p1, CELL * p2, CELL * p3,
	   int *active, int *goagain)
{
    CELL bitmask[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
    CELL outflow, cwork, c[8];
    int k;

    cwork = p2[j];
    if (G_is_c_null_value(p2 + j) || cwork >= 0 || cwork == -256)
	return;
    cwork = -cwork;

    for (k = 7; k >= 0; k--) {
	c[k] = 0;
	if (cwork >= bitmask[k]) {
	    c[k] = 1;
	    cwork -= bitmask[k];
	}
    }

    outflow = 0;

    /* There's no need to resolve directions at cells adjacent to null cells,
     * as those directions will be resolved before we get here */
    /* look one row back */
    cwork = p1[j - 1];
    if (cwork > 0 && cwork != 4 && c[6])
	outflow += 64;
    cwork = p1[j];
    if (cwork > 0 && cwork != 8 && c[7])
	outflow += 128;
    cwork = p1[j + 1];
    if (cwork > 0 && cwork != 16 && c[0])
	outflow += 1;

    /* look at this row */
    cwork = p2[j - 1];
    if (cwork > 0 && cwork != 2 && c[5])
	outflow += 32;
    cwork = p2[j + 1];
    if (cwork > 0 && cwork != 32 && c[1])
	outflow += 2;

    /* look one row forward */
    cwork = p3[j - 1];
    if (cwork > 0 && cwork != 1 && c[4])
	outflow += 16;
    cwork = p3[j];
    if (cwork > 0 && cwork != 128 && c[3])
	outflow += 8;
    cwork = p3[j + 1];
    if (cwork > 0 && cwork != 64 && c[2])
	outflow += 4;

    if (outflow == 0) {
	*active = 1;
    }
    else {
	*goagain = 1;
	p2[j] = select_dir(outflow);
    }
    return;
}
Example #4
0
//*******************************************************************
// QBtFinder                                             CONSTRUCTOR
//*******************************************************************
QBtFinder::QBtFinder( QWidget* const in_parent ) : QDialog( in_parent )
, settings_gbox_     ( new QGroupBox( tr( SETTINGS ) ) )
, file_label_        ( new QLabel( tr( FILE ) ) )
, from_label_        ( new QLabel( tr( FROM ) ) )
, what_label_        ( new QLabel( tr( WHAT ) ) )
, file_              ( new QLineEdit )
, from_              ( new QLineEdit )
, what_              ( new QLineEdit )
, case_              ( new QCheckBox( tr( CASE ) ) )
, file_regex_        ( new QCheckBox( tr( REGEX ) ) )
, from_points_       ( new QBtPointsButton )
, what_regex_        ( new QCheckBox( tr( REGEX ) ) )
, run_               ( new QPushButton( tr( RUN    ) ) )
, break_             ( new QPushButton( tr( BREAK  ) ) )
, goto_              ( new QPushButton( tr( GOTO   ) ) )
, cancel_            ( new QPushButton( tr( CANCEL ) ) )
, result_            ( new QTreeWidget )
, fpath_             ( new QLineEdit )
, font_metrics_      ( fontMetrics() )
, selected_file_path_( QString() )
{
   setWindowTitle( tr( CAPTION ) );

   QTreeWidgetItem* const header = new QTreeWidgetItem;
   header->setText( 0, tr( FILE_NAME ) );
   header->setText( 1, tr( FILE_PATH ) );
   result_->setHeaderItem( header );
   result_->setRootIsDecorated( false );
   result_->setSortingEnabled( true );
   result_->sortByColumn( 0, Qt::AscendingOrder );

   run_->setDefault( true );
   break_->setEnabled( false );
   goto_->setEnabled( false );
   from_->setText( QDir::homePath() );
   
   fpath_->setFrame( false );
   fpath_->setReadOnly( true );
   file_label_->setBuddy( file_ );
   from_label_->setBuddy( from_ );
   what_label_->setBuddy( what_ );

   QGridLayout* const input_layout = new QGridLayout( settings_gbox_ );
   input_layout->addWidget( file_label_ , 0, 0 );
   input_layout->addWidget( from_label_ , 1, 0 );
   input_layout->addWidget( what_label_ , 2, 0 );
   input_layout->addWidget( file_       , 0, 1 );
   input_layout->addWidget( from_       , 1, 1 );
   input_layout->addWidget( what_       , 2, 1 );
   input_layout->addWidget( file_regex_ , 0, 2 );
   input_layout->addWidget( from_points_, 1, 2 );
   input_layout->addWidget( case_       , 2, 2 );
   input_layout->addWidget( what_regex_ , 3, 2 );
   
   QVBoxLayout* const btn_layout = new QVBoxLayout;
   btn_layout->addWidget( run_ );
   btn_layout->addWidget( break_ );
   btn_layout->addWidget( goto_ );
   btn_layout->addWidget( cancel_ );
   btn_layout->addStretch( 100 );
   
   QHBoxLayout* const settings_layout = new QHBoxLayout;
   settings_layout->addWidget( settings_gbox_ );
   settings_layout->addLayout( btn_layout );

   QGroupBox* const result_gbox = new QGroupBox( tr( RESULT ) );
   QHBoxLayout* const result_layout = new QHBoxLayout( result_gbox );
   result_layout->addWidget( result_ );
   
   QVBoxLayout* const main_layout = new QVBoxLayout;
   main_layout->addLayout( settings_layout );
   main_layout->addWidget( result_gbox );
   main_layout->addWidget( fpath_ );
   setLayout( main_layout );
   main_layout->setStretchFactor( result_gbox, 100 );

   connect( from_points_, SIGNAL( clicked() ), this, SLOT( select_dir() ) );
   connect( cancel_     , SIGNAL( clicked() ), this, SLOT( reject    () ) );
   connect( run_        , SIGNAL( clicked() ), this, SLOT( run_find  () ) );
   connect( break_      , SIGNAL( clicked() ), this, SLOT( break_find() ) );
   connect( goto_       , SIGNAL( clicked() ), this, SLOT( goto_find () ) );

   connect( &parser_, SIGNAL( change_dir  ( const QString& ) ), this, SLOT( parser_dir_changed ( const QString& ) ) );
   connect( &parser_, SIGNAL( current_item( const QString& ) ), this, SLOT( parser_current_item( const QString& ) ) );
   connect( &parser_, SIGNAL( finished    ()                 ), this, SLOT( parser_finished    () ) );
}