Exemple #1
0
void do_stuff(msg *received_message){
    if(received_message->msg_type==1){
        //Broadcast message.
        if(check_history(received_message->message_id))
        	return;
        insert_into_history_table(received_message->message_id);
        int *neighbours=get_neighbours(ROUTER_ID);
        int size=neighbours[0];
        int i;
        printf("received by router id:%d from %d.\nSending to others.\n", ROUTER_ID,received_message->coming_from);
        received_message->coming_from=ROUTER_BASE_PORT+ROUTER_ID;
        for(i=1;i<=size;i++){
        	write_to_socket(ROUTER_BASE_PORT+neighbours[i],code_message(received_message));
        }
    }
		//unicast message
    else if(received_message->msg_type==2)
        if(received_message->receiver_id!=check_node_list(ROUTER_ID,received_message->receiver_id)) {
            int *path=find_shorest_path_between_routers(ROUTER_ID,get_parent_id(received_message->receiver_id));
            int next_node_to_send_to=path[1];
            if(next_node_to_send_to==-1){
                printf("Next node not found.Exiting...\n");
                exit(0);
            }
            received_message->coming_from=ROUTER_BASE_PORT+ROUTER_ID;
            printf("Passing from router %d to router %d\n",ROUTER_ID,next_node_to_send_to);
            write_to_socket(ROUTER_BASE_PORT+next_node_to_send_to,code_message(received_message));
        }
        else{
            received_message->coming_from=ROUTER_BASE_PORT+ROUTER_ID;
            printf("Passing from router %d to end node %d\n",ROUTER_ID,received_message->receiver_id );
            write_to_socket(NODE_BASE_PORT+received_message->receiver_id,code_message(received_message));
        }
    //Control message
    else if(received_message->msg_type==3){
      	//operations supported==> print the count of the messages received and forwarded on each link.
    	if(check_history(received_message->message_id))		//node is already visited.
        	return;
        insert_into_history_table(received_message->message_id);
        int *neighbours=get_neighbours(ROUTER_ID);
        int size=neighbours[0];
        int i;
      // printf("received by router id:%d.\nSending to others.\n", ROUTER_ID);
        received_message->coming_from=ROUTER_BASE_PORT+ROUTER_ID;
        printf("Message received by Router # %d are %llu (including the current one!)\n",ROUTER_ID,message_count+1 );
        for(i=1;i<=size;i++){
        	write_to_socket(ROUTER_BASE_PORT+neighbours[i],code_message(received_message));
        }
    }
    message_count++;


}
Exemple #2
0
static quicklist * get_island(region * root)
{
    quicklist * ql, *result = 0;
    int qi = 0;

    fset(root, RF_MARK);
    ql_push(&result, root);

    for (ql = result, qi = 0; ql; ql_advance(&ql, &qi, 1)) {
        int dir;
        region *r = (region *)ql_get(ql, qi);
        region * next[MAXDIRECTIONS];

        get_neighbours(r, next);

        for (dir = 0; dir != MAXDIRECTIONS; ++dir) {
            region *rn = next[dir];
            if (rn != NULL && rn->land && !fval(rn, RF_MARK)) {
                fset(rn, RF_MARK);
                ql_push(&result, rn);
            }
        }
    }

    for (ql = result, qi = 0; ql; ql_advance(&ql, &qi, 1)) {
        region *r = (region *)ql_get(ql, qi);
        freset(r, RF_MARK);
    }
    return result;
}
Exemple #3
0
static direction_t random_neighbour(region * r, unit * u)
{
    int i;
    region *rc;
    region * next[MAXDIRECTIONS];
    int rr, c = 0, c2 = 0;

    get_neighbours(r, next);
    /* Nachsehen, wieviele Regionen in Frage kommen */

    for (i = 0; i != MAXDIRECTIONS; i++) {
        rc = next[i];
        if (rc && can_survive(u, rc)) {
            if (room_for_race_in_region(rc, u_race(u))) {
                c++;
            }
            c2++;
        }
    }

    if (c == 0) {
        if (c2 == 0) {
            return NODIRECTION;
        }
        else {
            c = c2;
            c2 = 0;                   /* c2 == 0 -> room_for_race nicht beachten */
        }
    }

    /* Zufällig eine auswählen */

    rr = rng_int() % c;

    /* Durchzählen */

    c = -1;
    for (i = 0; i != MAXDIRECTIONS; i++) {
        rc = next[i];
        if (rc && can_survive(u, rc)) {
            if (c2 == 0) {
                c++;
            }
            else if (room_for_race_in_region(rc, u_race(u))) {
                c++;
            }
            if (c == rr)
                return (direction_t)i;
        }
    }

    assert(1 == 0);               /* Bis hierhin sollte er niemals kommen. */
    return NODIRECTION;
}
Exemple #4
0
static void smooth_island(region_list * island)
{
    region *rn[MAXDIRECTIONS];
    region_list *rlist = NULL;
    for (rlist = island; rlist; rlist = rlist->next) {
        region *r = rlist->data;
        int n, nland = 0;

        if (r->land) {
            get_neighbours(r, rn);
            for (n = 0; n != MAXDIRECTIONS && nland <= 1; ++n) {
                if (rn[n]->land) {
                    ++nland;
                    r = rn[n];
                }
            }

            if (nland == 1) {
                get_neighbours(r, rn);
                oceans_around(r, rn);
                for (n = 0; n != MAXDIRECTIONS; ++n) {
                    int n1 = (n + 1) % MAXDIRECTIONS;
                    int n2 = (n + 1 + MAXDIRECTIONS) % MAXDIRECTIONS;
                    if (!rn[n]->land && rn[n1] != r && rn[n2] != r) {
                        r = rlist->data;
                        runhash(r);
                        runhash(rn[n]);
                        SWAP_INTS(r->x, rn[n]->x);
                        SWAP_INTS(r->y, rn[n]->y);
                        rhash(r);
                        rhash(rn[n]);
                        rlist->data = r;
                        oceans_around(r, rn);
                        break;
                    }
                }
            }
        }
    }
}
Exemple #5
0
static int tolua_region_get_adj(lua_State * L)
{
    region *r = (region *)tolua_tousertype(L, 1, 0);
    region *rn[MAXDIRECTIONS];
    int d, idx;
    get_neighbours(r, rn);

    lua_createtable(L, MAXDIRECTIONS, 0);
    for (d = 0, idx = 0; d != MAXDIRECTIONS; ++d) {
        if (rn[d]) {
            tolua_pushusertype(L, rn[d], TOLUA_CAST "region");
            lua_rawseti(L, -2, ++idx);
        }
    }
    return 1;
}
Exemple #6
0
void expand_frontier() {
  int i;
  board_node **neighbours;

  neighbours = get_neighbours(get_head()->node);

  for(i=0; i<4; i++) {
    board_node *neighbour = neighbours[i];

    if (neighbour == NULL) {
      free(neighbour);
      continue;
    }

    frontier_push(neighbour);
  }
}
Exemple #7
0
static direction_t treeman_neighbour(region * r)
{
    int i;
    int rr;
    int c = 0;
    region * next[MAXDIRECTIONS];

    get_neighbours(r, next);
    /* Nachsehen, wieviele Regionen in Frage kommen */

    for (i = 0; i != MAXDIRECTIONS; i++) {
        if (next[i] && rterrain(next[i]) != T_OCEAN
            && rterrain(next[i]) != T_GLACIER && rterrain(next[i]) != T_DESERT) {
            ++c;
        }
    }

    if (c == 0) {
        return NODIRECTION;
    }
    /* Zufällig eine auswählen */

    rr = rng_int() % c;

    /* Durchzählen */

    c = -1;
    for (i = 0; i != MAXDIRECTIONS; i++) {
        if (next[i] && rterrain(next[i]) != T_OCEAN
            && rterrain(next[i]) != T_GLACIER && rterrain(next[i]) != T_DESERT) {
            if (++c == rr) {
                return (direction_t)i;
            }
        }
    }

    assert(!"this should never happen");               /* Bis hierhin sollte er niemals kommen. */
    return NODIRECTION;
}
Exemple #8
0
//function to find goal from a given start point
void findGoal(int start_x, int start_y, int goal_x, int goal_y){
	int lowestx;
	int lowesty;

	init_map();

	createopen();
	insert_by_priority(node_map[start_y][start_x]);

	while(true){
		get_lowest_f_cost(lowestx, lowesty);
		add_to_closed(lowestx, lowesty);
		if(is_goal(lowestx, lowesty,goal_x,goal_y)){
			displayTextLine(2, "found path");
			wait1Msec(100);
			//get_path();
			get_distance(start_x, start_y, goal_x, goal_y);
			return;
		}
		//get neighbours
		get_neighbours(lowestx, lowesty);
		for(int i=0; i<CONNECTIVITY;i++){
			if((current_neighbours[i].val==OBST)||(current_neighbours[i].onclosed==true)){}
			else if((current_neighbours[i].g>(get_g_cost(current_neighbours[i].x,current_neighbours[i].y,lowestx, lowesty)))||
				current_neighbours[i].onopen==false){
				set_cost(current_neighbours[i].x,current_neighbours[i].y,lowestx, lowesty);
				current_neighbours[i].parentx=lowestx;
				current_neighbours[i].parenty=lowesty;
				memcpy(&node_map[current_neighbours[i].y][current_neighbours[i].x],&current_neighbours[i],sizeof(node_map[current_neighbours[i].y][current_neighbours[i].x]));
				if(current_neighbours[i].onopen==false){
					insert_by_priority(current_neighbours[i]);
				}
			}
		}
	}

}
Exemple #9
0
int main(int argc, char *argv[])
{

#ifdef HAVE_MPI
  MPI_Init(&argc, &argv);
  Epetra_MpiComm Comm(MPI_COMM_WORLD);
#else
  Epetra_SerialComm Comm;
#endif

  // number of nodes in the x- and y-direction
  int nx = 5;
  int ny = 6;
  int NumGlobalElements = nx * ny;

  // create a linear map
  Epetra_Map Map(NumGlobalElements,0,Comm);
  
  // local number of rows
  int NumMyElements = Map.NumMyElements();
  // get update list
  int * MyGlobalElements = Map.MyGlobalElements( );

  // Create an integer vector NumNz that is used to build the Petra Matrix.
  // NumNz[i] is the Number of OFF-DIAGONAL term for the ith global equation 
  // on this processor.
  // NOTE: NumNz can be specified to be an interfer, of value 5.
  // However, the procedure here reported is more general, and it is
  // representative of more complex situations, where the number of
  // nonzero per row can vary consistently.

  int * NumNz = new int[NumMyElements];
  
  double off_left  = -1.0;
  double off_right = -1.0;
  double off_lower = -1.0;
  double off_upper = -1.0;
  double diag      =  4.0;
  int left, right, lower, upper;
  
  for ( int i=0; i<NumMyElements; i++) {
    NumNz[i] = 1;
    get_neighbours( MyGlobalElements[i], nx, ny, 
		    left, right, lower, upper); 
    if( left  != -1 ) ++NumNz[i];
    if( right != -1 ) ++NumNz[i];
    if( lower != -1 ) ++NumNz[i];
    if( upper != -1 ) ++NumNz[i];
  }
  
  // Create a Epetra_Matrix
  // create a CRS matrix

  Epetra_CrsMatrix A(Copy,Map,NumNz);

  // Add  rows one-at-a-time

  double Values[4];
  int Indices[4];

  for( int i=0 ; i<NumMyElements; ++i ) {
    int NumEntries=0;
    get_neighbours(  MyGlobalElements[i], nx, ny, 
		     left, right, lower, upper);
    if( left != -1 ) {
	Indices[NumEntries] = left;
	Values[NumEntries] = off_left;
	++NumEntries;
    }
    if( right != -1 ) {
      Indices[NumEntries] = right;
      Values[NumEntries] = off_right;
      ++NumEntries;
    }
    if( lower != -1 ) {
      Indices[NumEntries] = lower;
      Values[NumEntries] = off_lower;
      ++NumEntries;
    }
    if( upper != -1 ) {
      Indices[NumEntries] = upper;
      Values[NumEntries] = off_upper;
      ++NumEntries;
    }
    // put the off-diagonal entries
    A.InsertGlobalValues(MyGlobalElements[i], NumEntries, Values, Indices);
    // Put in the diagonal entry
    A.InsertGlobalValues(MyGlobalElements[i], 1, &diag, MyGlobalElements+i);
  }
  cout <<  A;
  
#ifdef HAVE_MPI
  MPI_Finalize();
#endif

  delete NumNz;
  
  return(EXIT_SUCCESS);

}
Exemple #10
0
static int queue_pixel_core ( move *movements, queue **redo_segments,
                              cell_map *dist, cell_map *elev,
                              cell_map *dir, cell_map *up, cell_map *dw,
                              seg_map *segment_info, int seg, int *count,
                              int **neighbours )
{
    FCELL dist_cell = 0, dist_cell_neig = 0, up_cell = 0, dw_cell = 0;
    FCELL elev_cell = 0, elev_cell_neig = 0;
    //CELL dir_cell = 0;

    elem *el = pop( redo_segments[seg] ), *prev;
    //int *neighbours[NMV] = {{0, 0},{0, 0},};

    G_debug ( 4, "Segment number: %d\n", seg );

    // check if there is pixel to do in the row
    while (el != NULL)
    {
        int row = el->point.row;
        int col = el->point.col;
        segment_get( &dist->seg, &dist_cell, row, col );

        if ( dist_cell >= 0 )
        {
            // get cell neighbours
            get_neighbours ( row, col, movements, neighbours,
                             segment_info->nrows, segment_info->ncols );

            for ( int n = 0; n < ( int ) sizeof ( movements ) ; n++ )
            {
                // TODO: after check if there are performce consegunece to declaire here or not
                int nx = neighbours[n][0];
                int ny = neighbours[n][1];

                //Check if neighbours are inside the region AND the domain
                if ( nx != NULL_NEIG){
                    // get distance value
                    segment_get( &dist->seg, &dist_cell_neig, nx, ny );
                    //Check if distance is in the domain
                    if (dist_cell_neig>0 )
                    {
                        float new_dist = dist_cell + movements[n].dist;
                        if ( dist_cell_neig > new_dist )
                        {
                            // assign the smaller one
                            segment_put ( &dist->seg, &new_dist, nx, ny );
                            segment_put ( &dir->seg, &movements[n].dir, nx, ny );
                            //check if drop is positive or negative
                            segment_get( &elev->seg, &elev_cell, row, col );
                            segment_get( &elev->seg, &elev_cell_neig, nx, ny );
                            float drop = elev_cell_neig - elev_cell;

                            if ( drop >= 0 )
                            {
                                segment_get( &up->seg, &up_cell, row, col );
                                up_cell += drop;
                                segment_put ( &up->seg, &up_cell, nx, ny );
                                //up_cache[nx][ny] = up_cache[1][col] + drop;
                                segment_get( &dw->seg, &dw_cell, row, col );
                                segment_put ( &dw->seg, &dw_cell, nx, ny );
                                //dw_cache[nx][ny] = dw_cache[1][col];
                            }
                            else
                            {
                                segment_get( &up->seg, &up_cell, row, col );
                                segment_put ( &up->seg, &up_cell, nx, ny );
                                //up_cache[nx][ny] = up_cache[1][col];
                                segment_get( &dw->seg, &dw_cell, row, col );
                                dw_cell += drop;
                                segment_put ( &dw->seg, &dw_cell, nx, ny );
                                //dw_cache[nx][ny] = dw_cache[1][col] + drop;
                            }
                            int seg_numb = get_segment_number(nx, ny, segment_info);
                            array_append(seg_numb, nx, ny, redo_segments);
                            *count += 1;
                        }
                    }
                }
            }
        }
        prev = el;
        free(prev);
        el = pop( redo_segments[seg] );
    }
    return 0;
}
Exemple #11
0
// Validates input and changes the board 
// or returns error
bool HexBoard::take_input(pair<int, int> p, int player, Graph& g, string str)
{

    if((p.second < 65) || (p.second)>(65+dimention-1))
    {
	cout << "Move (" << p.first << static_cast<char>(p.second) << ") ERROR: Invalid 2nd coordinate" << endl;
	cout << '\n' << endl;
	return false;
    }

    if((p.first < 1) || p.first > (1+dimention-1))
    {
	cout << "Move (" << p.first << static_cast<char>(p.second) << ") ERROR: Invalid 1st coordinate" << endl;
	cout << '\n' << endl;
	return false;
    }

    //Check if it's already occupied
    if (blueVertices[((dimention*(p.first - 1))+(p.second - 65))])
    {
	cout << "Move " << str << " ERROR: Already occupied by blue player/ player 0";
	cout << '\n' << endl;
	return false;
    }

    if (redVertices[((dimention*(p.first - 1))+(p.second - 65))])
    {
	cout << "Move " << str << " ERROR: Already occupied by red player/ player 1";
	cout << '\n' << endl;
	return false;
    }

    if(player == 0)
    {
	blueVertices[((dimention*(p.first - 1))+(p.second - 65))] = 1;
	cout << "Move " << str << " SUCCESS" << endl;
	cout << '\n' << endl;
	moveNumber++;

	// Get all possible neighours and see if they are already occupied
	// If occupied, and there's no edge between them in the graph g,
	// add edge.
	vector<pair<int, int> > neighbours = get_neighbours(p);
	for(int i=0; i<neighbours.size(); i++)
	{
	    int dstVertex = Helper::xy_to_linear(neighbours[i], dimention);
	    int srcVertex = Helper::xy_to_linear(p, dimention);
	    // If neighbour position was occupied and
	    // graph edge, didn't exist
	    if(blueVertices[dstVertex] == 1)
	    {
		// Add edge to neighbours
		if(!g.is_adjacent(srcVertex, dstVertex))
		{
		    g.add_edge(srcVertex, dstVertex);
		    g.set_edge_value(srcVertex, dstVertex, 1.0);

		    g.add_edge(dstVertex, srcVertex);
		    g.set_edge_value(dstVertex, srcVertex, 1.0);
		}
	    }
	}
	return true;
    }

    if(player == 1)
    {
	redVertices[((dimention*(p.first - 1))+(p.second - 65))] = 1;
	cout << "Move " << str << " SUCCESS" << endl;
	cout << '\n' << endl;
	moveNumber++;

	// Get all possible neighours and see if they are already occupied
	// If occupied, and there's no edge between them in the graph g,
	// add edge.
	vector<pair<int, int> > neighbours = get_neighbours(p);
	for(int i=0; i<neighbours.size(); i++)
	{
	    int dstVertex = Helper::xy_to_linear(neighbours[i], dimention);
	    int srcVertex = Helper::xy_to_linear(p, dimention);
	    // If neighbour position was occupied and
	    // graph edge, didn't exist
	    if(redVertices[dstVertex] == 1)
	    {
		// Add edge to neighbours
		if(!g.is_adjacent(srcVertex, dstVertex))
		{
		    g.add_edge(srcVertex, dstVertex);
		    g.set_edge_value(srcVertex, dstVertex, 1.0);

		    g.add_edge(dstVertex, srcVertex);
		    g.set_edge_value(dstVertex, srcVertex, 1.0);
		}
	    }
	}
	return true;
    }

}
Exemple #12
0
void analize_tree(TREEPTR root, short condition, short reg, int level)
{
    char    neigh[10],path[10];
    PATHPTR path_list=NULL,
	    neigh_list=NULL,
	    breath_h=NULL,
	    breath_q=NULL;
    short   length, merged, end;
    TREEPTR node,neigh_node;
    MERGEPTR node1, node2;


    initialize_label(path,0);
    node=root;

    end=False;
    while (!end) {
	node=get_leaf(path);

	/*Compute merge operations*/
	if (node->region!=NULL) {    /*A leaf has been found*/
	    /*Merge its neighbours if condition is satisfied*/
	    if (node->included!=yes) {
		/*Add a new element to the list of merged regions*/
		add_new_merged_region(&merged_region_list);
		/*Insert the region in the list of merged regions*/
		insert_leaf_list(&merged_region_list, node);
		node->included=yes;
		/*Actualize the pointer to the merged region which includes it*/
		node->pmerge = merged_region_list;
	    }
	    /* Obtain the list of neighbours of the node referenced by path */
	    get_neighbours(&neigh_list,root,path);
	    while (neigh_list!=NULL) {
		initialize_label(neigh,0);
		/* Get and delete the first element in the list of neighbours */
		delete_first(&neigh_list,neigh);
		neigh_node=get_leaf(neigh);
		/* Get the pointers to the candidate regions */
		node1=*get_location(node);
		node2=*get_location(neigh_node);
		if ( node1!=node2 ) {
		    /* The neighbour region doesn't belong to a merged region yet*/
		    if (neigh_node->included!=yes) {
			/* Check whether both regions are homogeneous or not */ 
			if (similar(path,neigh,condition)) {
			    /* Insert the neighbour in the same list than path */
			    /* All the regions that are homogeneous belong to */
			    /* the same list */ 
			    insert_leaf_list(get_location(node),neigh_node);
			    neigh_node->included=yes;
			    /* Set pmerge field to point to the list */
			    /* where it has been inserted */
			    neigh_node->pmerge=*get_location(node);
			}
		    } else {  /* Each region belongs to a different merged region */
			/* Check whether both regions are homogeneous or not */ 
			if (similar(path,neigh,condition)) {
			    /* Check whether both merged regions are homogeneous */
			    if (abs( node1->mean - node2->mean ) < condition)
				/* Merge both lists of merged regions */
				merge_lists(&node1,&node2);
			}
		    }
		}
	    }
	} else {
	    length = strlen(path);
	    /* Insert new nodes to be analised using the breath first method */
	    path[length]='0'; insert_path(&breath_h,&breath_q,path);
	    path[length]='1'; insert_path(&breath_h,&breath_q,path);
	    path[length]='2'; insert_path(&breath_h,&breath_q,path);
	    path[length]='3'; insert_path(&breath_h,&breath_q,path);
	}
	/* Take a node from the list to analize it if there are nodes left */
	if (breath_h==NULL) end=True;
	    else delete_first(&breath_h,path);
    }
}
Exemple #13
0
// Validates input and changes the board 
// or returns error
bool HexBoard::take_input(string str, int player, Graph& g)
{

	if(str.size()!=2)
	{
		cout << "Move " << str << " ERROR: Size of the entered coordinates string is not equal to 2" << endl;
		cout << '\n' << endl;
		return false;
	}

	if((static_cast<int>(str[1])<65) || (static_cast<int>(str[1])>(65+dimention-1)))
	{
		cout << "Move " << str << " ERROR: Invalid 2nd coordinate" << endl;
		cout << '\n' << endl;
		return false;
	}

	if(static_cast<int>(str[0])<49 || static_cast<int>(str[0])>(49+dimention-1))
	{
		cout << "Move " << str << " ERROR: Invalid 1st coordinate" << endl;
		cout << '\n' << endl;
		return false;
	}

	//Check if it's already occupied
	if (blueVertices[((dimention*(static_cast<int>(str[0])-49))+(static_cast<int>(str[1])-65))])
	{
		cout << "Move " << str << " ERROR: Already occupied by blue player/ player 0";
		cout << '\n' << endl;
		return false;
	}

	if (redVertices[((dimention*(static_cast<int>(str[0])-49))+(static_cast<int>(str[1])-65))])
	{
		cout << "Move " << str << " ERROR: Already occupied by red player/ player 1";
		cout << '\n' << endl;
		return false;
	}

	if(player == 0)
	{
		blueVertices[((dimention*(static_cast<int>(str[0])-49))+(static_cast<int>(str[1])-65))] = 1;
		cout << "Move " << str << " SUCCESS" << endl;
		cout << '\n' << endl;
		moveNumber++;

		// Get all possible neighours and see if they are already occupied
		// If occupied, and there's no edge between them in the graph g,
		// add edge.
		vector<string> neighbours = get_neighbours(str);
		for(int i=0; i<neighbours.size(); i++)
		{
			int dstVertex = Helper::xy_to_linear(neighbours[i], dimention);
			int srcVertex = Helper::xy_to_linear(str, dimention);
			// If neighbour position was occupied and
			// graph edge, didn't exist
			if(blueVertices[dstVertex] == 1)
			{
				// Add edge to neighbours
				if(!g.is_adjacent(srcVertex, dstVertex))
				{
					g.add_edge(srcVertex, dstVertex);
					g.set_edge_value(srcVertex, dstVertex, 1.0);

					g.add_edge(dstVertex, srcVertex);
					g.set_edge_value(dstVertex, srcVertex, 1.0);
				}
			}
		}
		return true;
	}

	if(player == 1)
	{
		redVertices[((dimention*(static_cast<int>(str[0])-49))+(static_cast<int>(str[1])-65))] = 1;
		cout << "Move " << str << " SUCCESS" << endl;
		cout << '\n' << endl;
		moveNumber++;

		// Get all possible neighours and see if they are already occupied
		// If occupied, and there's no edge between them in the graph g,
		// add edge.
		vector<string> neighbours = get_neighbours(str);
		for(int i=0; i<neighbours.size(); i++)
		{
			int dstVertex = Helper::xy_to_linear(neighbours[i], dimention);
			int srcVertex = Helper::xy_to_linear(str, dimention);
			// If neighbour position was occupied and
			// graph edge, didn't exist
			if(redVertices[dstVertex] == 1)
			{
				// Add edge to neighbours
				if(!g.is_adjacent(srcVertex, dstVertex))
				{
					g.add_edge(srcVertex, dstVertex);
					g.set_edge_value(srcVertex, dstVertex, 1.0);

					g.add_edge(dstVertex, srcVertex);
					g.set_edge_value(dstVertex, srcVertex, 1.0);
				}
			}
		}
		return true;
	}

}
Exemple #14
0
/* E3A island generation */
int build_island_e3(newfaction ** players, int x, int y, int numfactions, int minsize)
{
#define MIN_QUALITY 1000
    int nfactions = 0;
    region_list *rlist = NULL;
    region_list *island = NULL;
    plane *pl = findplane(x, y);
    region *r = findregion(x, y);
    int nsize = 1;
    int q, maxq = INT_MIN, minq = INT_MAX;

    if (!r)
        r = new_region(x, y, pl, 0);
    assert(!r->units);
    do {
        terraform_region(r, random_terrain_e3(NODIRECTION));
    } while (!r->land);

    while (r) {
        fset(r, RF_MARK);
        if (r->land) {
            if (nsize < minsize) {
                nsize += random_neighbours(r, &rlist, &random_terrain_e3);
            }
            else {
                nsize += random_neighbours(r, &rlist, &get_ocean);
            }
        }
        regionqueue_push(&island, r);
        r = regionqueue_pop(&rlist);
    }

    smooth_island(island);

    if (nsize > minsize / 2) {
        for (rlist = island; rlist; rlist = rlist->next) {
            r = rlist->data;
            if (r->land && fval(r, RF_MARK)) {
                region *rn[MAXDIRECTIONS];

                get_neighbours(r, rn);
                q = region_quality(r, rn);
                if (q >= MIN_QUALITY && nfactions < numfactions && *players) {
                    starting_region(players, r, rn);
                    minq = _min(minq, q);
                    maxq = _max(maxq, q);
                    ++nfactions;
                }
            }
        }

        for (rlist = island; rlist && nfactions < numfactions; rlist = rlist->next) {
            r = rlist->data;
            if (!r->land && fval(r, RF_MARK)) {
                region *rn[MAXDIRECTIONS];
                get_neighbours(r, rn);
                q = region_quality(r, rn);
                if (q >= MIN_QUALITY * 4 / 3 && nfactions < numfactions && *players) {
                    starting_region(players, r, rn);
                    minq = _min(minq, q);
                    maxq = _max(maxq, q);
                    ++nfactions;
                }
            }
        }
    }

    for (rlist = island; rlist; rlist = rlist->next) {
        r = rlist->data;
        if (r->units) {
            region *rn[MAXDIRECTIONS];
            get_neighbours(r, rn);
            q = region_quality(r, rn);
            if (q - minq > (maxq - minq) * 2 / 3) {
                terraform_region(r, newterrain(T_HIGHLAND));
                prepare_starting_region(r);
            }
            r->land->money = 50000;   /* 2% = 1000 silver */
        }
        else if (r->land) {
            r->land->money *= 4;
        }
    }
    return nfactions;
}