//---------- Begin of function StateArray::fill_region -----// // // fill state_recno from 0 to fill_state_recno // void StateArray::fill_state(int x, int y) { err_when( x < 0 || x >= max_x_loc || y < 0 || y >= max_y_loc); int left, right; // Location *locPtr; // extent x to left and right for( left = x; left >= 0 && !get_loc(left,y)->state_recno; --left) { get_loc(left,y)->state_recno = fill_state_recno; } ++left; for( right=x+1; right < max_x_loc && !get_loc(right,y)->state_recno; ++right) { get_loc(right,y)->state_recno = fill_state_recno; } --right; // ------- scan line below ---------// y++; if( y < max_y_loc ) { for( x = left>0?left-1:0 ; x <= right+1 && x < max_x_loc; ++x ) { if( !get_loc(x,y)->state_recno ) { fill_state(x,y); } } } // ------- scan line above -------- // y -= 2; if( y >= 0) { for( x = left>0?left-1:0 ; x <= right+1 && x < max_x_loc; ++x ) { if( !get_loc(x,y)->state_recno ) { fill_state(x,y); } } } }
BOOL CActorMP::net_Relevant () { if (OnClient()) { if (!g_Alive()) return (false); if (m_i_am_dead) return (false); } if (character_physics_support()->IsRemoved()) return (false); actor_mp_state state; fill_state (state); return (m_state_holder.relevant(state)); }
int main(int argc, const char *argv[]) { int x_max,y_max; init_curses(); draw_bottom(); #ifdef debug fprintf(moon_log,"hello yaomoon\n"); #endif fill_state(); display_win(&state); fill_error(); display_win(&error); //wprintw(state.win,"%s %s\n","name","yaomoon"); //wrefresh(state.win); while(1); endwin(); return 0; }
int main(int argc, char *argv[]) { char c; int i,len; int server_sock; socklen_t client_len; struct sockaddr_in server,client; struct in_addr in; struct my_socket my_socket; init_curses(); draw_bottom(); init_socket(&my_socket); server_sock = get_udpsocket(&my_socket); // communication with client while(1) { // wait for client request client_len = sizeof(client); len = recvfrom(server_sock, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&client, &client_len); if (len < 0) { close(server_sock); fprintf(stderr, "%s\n", strerror(errno)); exit(EXIT_FAILURE); } else { //printf("recvfrom client ok!\n"); in.s_addr = client.sin_addr.s_addr; //printf("client ip : %s\n", inet_ntoa(in)); //printf("client port: %d\n", ntohs(client.sin_port)); //printf("\n"); } // Quit flag if(buffer[0] == '.') break; // lower to upper /* for(i=0; i<len; i++) { c = buffer[i]; buffer[i] = toupper(c); } */ // send back to client //printf("%s\n",buffer); fill_state("newmessage",buffer); refresh_state(); sendto(server_sock, buffer, len, 0, (struct sockaddr *)&client, client_len); memset(buffer,0,sizeof(buffer)); } //printf("Client close the socket\n"); close(server_sock); exit(EXIT_SUCCESS); }
//-------- begin of function StateArray::generate ----------// // void StateArray::generate( int stateCount ) { err_when( stateCount > MAX_STATE ); //-------- allocate memory ------- // state_count = stateCount; state_info_array = (StateInfo *)mem_add( sizeof(StateInfo) * stateCount ); //-------- generate area ----------// int needRetry = 0; int avgSeparation = misc.sqrt( max_x_loc * max_y_loc / state_count ); int loopCount = 0; do { if( ++loopCount > 1000 ) { err.run( "Fail generating states" ); } needRetry = 0; //------ reset array --------// memset( state_info_array, 0, sizeof(StateInfo) * state_count ); //------- randomize center location --------// int s; for( s = 1; s <= state_count; ++s ) { StateInfo *state = operator[](s); state->state_recno = s; int trial = s * 16 + 1; int xLoc, yLoc; while( --trial >= 0 ) { xLoc = random(max_x_loc); yLoc = random(max_y_loc); int closeFlag = 0; // check separation for( int t = 1; t < s; ++t ) { if( misc.diagonal_distance(xLoc, yLoc, operator[](t)->center_x, operator[](t)->center_y) * 2 < avgSeparation ) closeFlag = 1; } if( !closeFlag ) break; } if( trial < 0 ) { needRetry = 1; continue; } state->o_center_x = state->center_x = xLoc; state->o_center_y = state->center_y = yLoc; state->strength_x1 = (max_x_loc+random(max_x_loc)) * (max_y_loc+random(max_y_loc)) * (max_y_loc+random(max_y_loc)) / 8; state->strength_y1 = (max_x_loc+random(max_x_loc)) * (max_x_loc+random(max_x_loc)) * (max_y_loc+random(max_y_loc)) / 8; // state->strength_x2 = (max_y_loc+random(max_y_loc)) * (max_y_loc+random(max_y_loc)) / 4; // state->strength_y2 = (max_x_loc+random(max_x_loc)) * (max_x_loc+random(max_x_loc)) / 4; // state->strength_xy = (max_x_loc+random(max_x_loc)) * (max_y_loc+random(max_y_loc)) / 4; state->strength_x2 = (max_y_loc+random(max_y_loc/2)) * (max_y_loc+random(max_y_loc/2)) / 4; state->strength_y2 = (max_x_loc+random(max_x_loc/2)) * (max_x_loc+random(max_x_loc/2)) / 4; state->strength_xy = (max_x_loc+random(max_x_loc/2)) * (max_y_loc+random(max_y_loc/2)) / 8; // this coeff is smaller } } while(needRetry); //------ first pass, set area closest to a capital to that state ----- // { int x, y; for( y = 0; y < max_y_loc; ++y ) { StateLocation *locPtr = get_loc(0, y); for( x = 0; x < max_x_loc; ++x, ++locPtr ) { int s; unsigned char minState = 0; double minDist = 0.0; for( s = 1; s <= state_count; ++s ) { double d = operator[](s)->distance_func( (float)x, (float)y ); if( !minState || d < minDist ) { minDist = d; minState = s; } } locPtr->state_recno = minState; } } } // ----- eliminate seperated area -------// { int s; for( s = 1; s <= state_count; ++s ) { // replace all loc of state state to zero int x, y; for( y = 0; y < max_y_loc; ++y ) { StateLocation *locPtr = get_loc(0, y); for( x = 0; x < max_x_loc; ++x, ++locPtr ) { if( locPtr->state_recno == s ) { locPtr->state_recno = 0; } } } // fill again at center fill_state_recno = (unsigned char) s; StateInfo *state = operator[](s); fill_state( state->center_x, state->center_y ); } // do in reverse direction to remove odded out for( s = state_count; s >= 1; --s ) { // replace all loc of state state to zero int x, y; for( y = 0; y < max_y_loc; ++y ) { StateLocation *locPtr = get_loc(0, y); for( x = 0; x < max_x_loc; ++x, ++locPtr ) { if( locPtr->state_recno == s ) { locPtr->state_recno = 0; } } } // fill again at center fill_state_recno = (unsigned char) s; StateInfo *state = operator[](s); fill_state( state->center_x, state->center_y ); } } //------ set connection -------// { int x, y; for( y = 0; y < max_y_loc; ++y ) { for( x = 0; x < max_x_loc; ++x ) { unsigned char s0 = get_loc(x,y)->state_recno; if( x+1 < max_x_loc ) // check right location { unsigned char s1 = get_loc(x+1,y)->state_recno; if( s1 != s0 ) set_adjacent(s0, s1); } if( y+1 < max_y_loc ) // check down location { unsigned char s2 = get_loc(x,y+1)->state_recno; if( s2 != s0 ) set_adjacent(s0, s2); } if( x+1 < max_x_loc && y+1 < max_y_loc ) // check down right location { unsigned char s3 = get_loc(x+1,y+1)->state_recno; if( s3 != s0 ) set_adjacent(s0, s3); } } } } //------- update center as centroid of each area --------// { long sumX[MAX_STATE]; long sumY[MAX_STATE]; long stateArea[MAX_STATE]; memset( sumX, 0, sizeof(sumX) ); memset( sumY, 0, sizeof(sumY) ); memset( stateArea, 0, sizeof(stateArea) ); //------ find centroid of each state -------// int x, y; for( y = 0; y < max_y_loc; ++y ) { StateLocation *locPtr = get_loc(0, y); for( x = 0; x < max_x_loc; ++x, ++locPtr ) { if( locPtr->state_recno ) { ++stateArea[locPtr->state_recno -1]; sumX[locPtr->state_recno -1] += x; sumY[locPtr->state_recno -1] += y; } } } //------ update new center of each state ------// int s; int centerRadius = avgSeparation / 8; for( s = 1; s <= state_count; ++s ) { int centerX = sumX[s-1] / stateArea[s-1]; int centerY = sumY[s-1] / stateArea[s-1]; //----- if centroid is not of that state, do not update center -----// if( centerX >= 0 && centerX < max_x_loc && centerY >= 0 && centerY < max_y_loc && get_loc( centerX, centerY)->state_recno == s ) { operator[](s)->center_x = centerX; operator[](s)->center_y = centerY; } } } //-------- set bound flags of state --------// { int x, y, s; // set west/east bound flag for( y = 0; y < max_y_loc; ++y ) { s = get_loc(0, y)->state_recno; if(s) { operator[](s)->flags |= STATE_WEST_BOUND; operator[](s)->west_bound_count++; } s = get_loc(max_x_loc-1, y)->state_recno; if(s) { operator[](s)->flags |= STATE_EAST_BOUND; operator[](s)->east_bound_count++; } } // set north/south bound flag for( x = 0; x < max_x_loc; ++x ) { s = get_loc(x, 0)->state_recno; if(s) { operator[](s)->flags |= STATE_NORTH_BOUND; operator[](s)->north_bound_count++; } s = get_loc(x, max_y_loc-1)->state_recno; if(s) { operator[](s)->flags |= STATE_SOUTH_BOUND; operator[](s)->south_bound_count++; } } } }