Example #1
0
void ParticleSpecies::Order2D() {
  // Zero ordcount
  for (size_t j=0; j<nr; j++) {
    for (size_t k=0; k<nz; k++){
      ordcount[j*(nz+1) + k]= 0;
    }
  }

  //Sort particles
  for (size_t n=0; n<GetN(); n++) {
    int ir = (int)r[n];
    int iz = (int)z[n];

    //Sanity check -- we should be inside the grid
    if ( (ir<0) || (ir>=nr) || (iz<0) || (iz>=nz) ) {
      //Edge cases
      if (r[n] == (double) nr) {
        ir--;
      }
      else if (z[n] == (double) nz) {
        iz--;
      }
      else {
      //Over the edge; it's just wrong!
        fprintf(stderr," !!!ERROR!!! ir=%d iz=%d\n", ir, iz);
        fflush(stderr);

        printf("Error in ParticleSpecies::order_2D(): particle position out-of-range, r=%g, z=%g, (ir,iz)= (%d,%d)\n", r[n], z[n], ir, iz);

        exit(1);
      }
    }

    temP[ir*nz+iz].push_back(n);
    ordcount[ir*(nz+1) + iz]++;
  }

  //Stuff them back into the particle arrays, but now in the correct order
  ShuffleArray(z);
  ShuffleArray(r);
  ShuffleArray(vz);
  ShuffleArray(vr);
  ShuffleArray(vt);
  ShuffleArray(m);
  for (int cidx = 0; cidx < nr*nz; cidx++) {
    temP[cidx].clear();
  }

}
Example #2
0
public func Place(int amount, proplist rectangle)
{
	// No calls to objects, only definitions
	if (GetType(this) == C4V_C4Object) return;
	// Default parameters
	if (!amount) amount = 1;
	if (!rectangle) rectangle = Rectangle(0,0, LandscapeWidth(), LandscapeHeight());

	var trees = FindObjects(Find_InRect(rectangle.x, rectangle.y, rectangle.w, rectangle.h), Find_OCF(OCF_Fullcon), Find_Func("IsTree"), Find_Func("IsStanding"));
	var hives = CreateArray(), hive;
	while (amount)
	{
		hive = nil;
		ShuffleArray(trees);
		for (var tree in trees)
		{
			hive = tree->CreateObjectInTreetop(this);
			if (hive) break;
		}
		if (hive)
			hives[GetLength(hives)] = hive;
		amount--;
	}
Example #3
0
//done - rename to dfs
void RecursiveBacktrack(int **a, dimension_t dimension, settings_t settings)
{

	int **minMatrix;
	int w, h, x, y, i;
	int un, rn, dn, ln;
	coordList_t *stack = NULL, *element;
	int randDir[] = {1, 2, 3, 4};
	int done;
	int print = 0;

	minMatrix = GenerateMinMatrix(dimension);

	w = dimension.x / 2;
	h = dimension.y / 2;

	x = w / 2;
	y = h / 2;

	

	minMatrix[y][x] |= IN_MAZE;

	// insert 
	element = (coordList_t*) malloc( sizeof(coordList_t) );
	if ( element == NULL ) { printf("\nOut of memory\n"); exit(9); }
	element->i = y;
	element->j = x;
	element->next = stack;
	stack = element;
	//

	while ( stack != NULL )
	{

		x = stack->j;
		y = stack->i;
		
		element = stack;
		stack = stack->next;
		free(element);

		while ( 1 )
		{

			un = y - 1 >= 0 && !(minMatrix[y-1][x] & IN_MAZE);
			rn = x + 1 < w && !(minMatrix[y][x+1] & IN_MAZE);
			dn = y + 1 < h && !(minMatrix[y+1][x] & IN_MAZE);
			ln = x - 1 >= 0 && !(minMatrix[y][x-1] & IN_MAZE);

			if ( !(un || rn || dn || ln) ) break;

			ShuffleArray(randDir);

			done = 0;

			for ( i = 0; i < 4; i++ )
			{

				switch ( randDir[i] )
				{

				case 1: //up
					if ( un )
					{
						minMatrix[y][x] &= ~UP_WALL;
						minMatrix[y-1][x] &= ~DOWN_WALL;

						y = y - 1;

						minMatrix[y][x] |= IN_MAZE;
						recursive_push(&stack, x, y);

						done = 1;

					}


					break;

				case 2: //right
					if ( rn )
					{
						minMatrix[y][x] &= ~RIGHT_WALL;
						minMatrix[y][x+1] &= ~LEFT_WALL;

						x = x + 1;

						minMatrix[y][x] |= IN_MAZE;
						recursive_push(&stack, x, y);

						done = 1;

					}
					break;

				case 3: //down
					if ( dn )
					{
						minMatrix[y][x] &= ~DOWN_WALL;
						minMatrix[y+1][x] &= ~UP_WALL;

						y = y + 1;

						minMatrix[y][x] |= IN_MAZE;
						recursive_push(&stack, x, y);

						done = 1;

					}
					break;

				case 4: //left
					if ( ln )
					{
						minMatrix[y][x] &= ~LEFT_WALL;
						minMatrix[y+1][x] &= ~RIGHT_WALL;

						x = x - 1;

						minMatrix[y][x] |= IN_MAZE;
						recursive_push(&stack, x, y);

						done = 1;

					}
					break;

				} // end_switch

				if ( done ) break;

			} // end_for_rand

			if ( ++print % 5 == 0 ) { ConvertFromMin(minMatrix, a, dimension); LivePrint(a, dimension, settings); }

		} // end_walk

		if ( ++print % 5 == 0 ) { ConvertFromMin(minMatrix, a, dimension); LivePrint(a, dimension, settings); }

	} // end_while_everything




	ConvertFromMin(minMatrix, a, dimension);

}
Example #4
0
void Prim(int **a, dimension_t dimension, settings_t settings)
{

	int w, h, x, y, i, j;
	int **primMatrix, count = 0;
	int randDir[] = {1, 2, 3, 4};
	int done = 0, randNum = 0;
	int print = 0;
	coordList_t *rear, *element;

	rear = NULL; element = NULL;

	primMatrix = GenerateMinMatrix(dimension);

	w = dimension.x / 2;
	h = dimension.y / 2;

	x = w / 2; y = h / 2;

	primMatrix[y][x] |= IN_MAZE;


	InsertElement(&rear, x, y);

	while ( rear != NULL )
	{
		//delete
		count = rand() % 42; // if you ask why, you sir, aren't a geek!
		i = 0;
		while ( i < count )
		{
			rear = rear->next;
			i++;
		}
		
		element = rear->next;
		rear->next = element->next;
		x = element->j;
		y = element->i;
		if ( rear == element ) rear = NULL;
		free(element);
		//delete

		primMatrix[y][x] |= IN_MAZE;

		// ako ima komsije koji su deo lavirinta odaberi jedan od komsija i probij put

		ShuffleArray(randDir);

		for ( i = 0, done = 0; i < 4; i++ )
		{

			switch ( randDir[i] )
			{

			case 1:
				if ( y - 1 >= 0 && (primMatrix[y-1][x] & IN_MAZE) )
				{// up
					primMatrix[y-1][x] &= ~DOWN_WALL;
					primMatrix[y][x] &= ~UP_WALL;
					done = 1;
				
				}
				break;

			case 2:
				if ( y + 1 < h && (primMatrix[y+1][x] & IN_MAZE) )
				{//down
					primMatrix[y+1][x] &= ~UP_WALL;
					primMatrix[y][x] &= ~DOWN_WALL;
					done = 1;
					//printf("\n Probijen zid na dole  %d %d", x, y+1);
				}
				break;

			case 3:
				if ( x - 1 >= 0 && (primMatrix[y][x-1] & IN_MAZE) )
				{//left
					primMatrix[y][x-1] &= ~RIGHT_WALL;
					primMatrix[y][x] &= ~LEFT_WALL;
					done = 1;
					//printf("\n Probijen zid na levo  %d %d", x-1, y);
				}
				break;

			case 4:
				if ( x + 1 < w && (primMatrix[y][x+1] & IN_MAZE) )
				{//right
					primMatrix[y][x+1] &= ~LEFT_WALL;
					primMatrix[y][x] &= ~RIGHT_WALL;
					done = 1;
					//printf("\n Probijen zid na desno  %d %d", x+1, y);
				}
				break;

			default: printf("ZABOLO"), exit(64);

			}


			if ( done == 1 ) break;

		}

		if ( ++print % 5 == 0 ) { ConvertFromMin(primMatrix, a, dimension); LivePrint(a, dimension, settings); }

		// dodaj sve komsije koje nisu deo lafirinta u redic
		if ( y - 1 >= 0 && !(primMatrix[y-1][x] & IN_MAZE) ) 
		{// up

			if ( !FindInList(rear, x, y-1) )
				InsertElement(&rear, x, y-1);

		}
		if ( y + 1 < h && !(primMatrix[y+1][x] & IN_MAZE) ) 
		{//down

			if ( !FindInList(rear, x, y+1) )
				InsertElement(&rear, x, y+1);

		}
		if ( x - 1 >= 0 && !(primMatrix[y][x-1] & IN_MAZE) ) 
		{//left

			if ( !FindInList(rear, x-1, y) )
				InsertElement(&rear, x-1, y);

		}
			
			
		if ( x + 1 < w && !(primMatrix[y][x+1] & IN_MAZE) )
		{//right

			if ( !FindInList(rear, x+1, y) )
				InsertElement(&rear, x+1, y);

		}

	}

	// promeni primovu u nasu sugavu reprezentaciju
	for ( i = 0; i < dimension.y; i++ )
		for ( j = 0; j < dimension.x; j++ )
			if ( i == 0 || i == dimension.y-1 || j == 0 || j == dimension.x-1 ) a[i][j] = 1;
			else a[i][j] = 0;
		
	for ( i = 0; i < h; i++ )
		for ( j = 0; j < w; j++ )
		{

			if ( primMatrix[i][j] & UP_WALL )
			{
				a[i*2][j*2] = 1;
				a[i*2][j*2 + 1] = 1;
				a[i*2][j*2 + 2] = 1;
			}
			if ( primMatrix[i][j] & LEFT_WALL )
			{
				a[i*2][j*2] = 1;
				a[i*2 + 1][j*2] = 1;
				a[i*2 + 2][j*2] = 1;

			}

		}
		
}