Esempio n. 1
0
static void _on_exit(void) {
	s32 c = 0;
	s32 i = 0;
	static bl destroyed = FALSE;

	if(!destroyed) {
		for(i = 0; i < AHAT_COUNT; ++i) {
			if(game()->audio[i]) {
				AGE_FREE(game()->audio[i]);
				game()->audio[i] = 0;
			}
		}
		game()->destroy_score_boards();
		game()->clear_board();

		amb_save_data("data/save.bas");

		destroy_world();

		destroyed = TRUE;
	}

	c = _CrtDumpMemoryLeaks();

	if(0 != c) {
#ifdef _DEBUG
		_asm int 3
#else
		printf("Memory leak detected.\n");
		system("pause");
#endif
	}
Esempio n. 2
0
void destroy_game_state(GameState * state) {
	register int i;
	for (i = 0; i < state->enemies_length; i++) {
		destroy_path(&state->enemies[i].enemy.path);
	}
	free(state->enemies);
	for (i = 0; i < state->towers_length; i++) {
		destroy_tower(&state->world, state->towers[i]);
	}
	free(state->towers);
	destroy_world(&state->world);
}
dReal evaluate_controller(Controller* controller,noveltyitem* ni,data_record* record,bool log, bool off)
{
    vector<float> k;
    dReal fitness;
    int timestep=0;
    const int simtime=1500;
    create_world(controller,log);
    while (!creatures[0]->abort() && timestep<simtime)
    {
        simulationStep();
        timestep++;
        if (timestep%100 == 0 && novelty_function % 2 == 1)
        {
            update_behavior(k,creatures[0]);
        }
/*        if (log && timestep%100==0)
            cout << creatures[0]->fitness() << endl;*/
    }
    int time=timestep;

    //for (int x=timestep+1; x<=simtime; x++)
    //    if (x%100==0)
    while (k.size()< (simtime/100*2))
        update_behavior(k,creatures[0]);

    fitness=creatures[0]->fitness();
    ((Biped*)creatures[0])->lft.push_back(timestep);
    ((Biped*)creatures[0])->rft.push_back(timestep);
    if (ni!=NULL)
    {
        //ni->time=time;
        ni->novelty_scale = 1.0;
        ni->data.push_back(k);
        ni->secondary=time;
    }

    if (record!=NULL)
    {
        dVector3 com;
        creatures[0]->CenterOfMass(com);
        record->ToRec[0]=fitness;
        record->ToRec[1]=com[0];
        record->ToRec[2]=com[1];
        record->ToRec[3]=com[2];
        record->ToRec[4]=timestep;
    }

    destroy_world();
    return fitness;
}
Esempio n. 4
0
int main(int argc, char *argv[])
{
	world_t *world;
	unsigned int x;
	unsigned int y;
	char c;
	char color;
	unsigned int each_iteration;
	unsigned int displayed_iteration = 0;
	unsigned int display_gene_start_address;
	unsigned int red_uint;
	unsigned int green_uint;
	unsigned int blue_uint;
	organism_t *organism;

	Display *display;
	GC gc;
	int screen_number;
	int window_x;
	int window_y;
	unsigned int each_x;
	unsigned int each_y;
	unsigned int window_border_width;
	unsigned int window_height;
	unsigned int window_width;
	unsigned long gc_value_mask;
	unsigned long window_background_color;
	unsigned long window_border_color;
	Window root_window;
	Window window;
	XGCValues gc_values;
	Visual* default_visual;
	Colormap colormap;
	XColor system_color_red;
	XColor system_color_green;
	XColor system_color_blue;
	XColor system_color_white;
	XColor system_color_black;
	XColor system_color;
	XColor exact_color;

	srandom(RANDOM_SEED);

#if CURSES_VISUALIZATION
	initscr();
	start_color();

#if CURSES_SOLID_COLORS
	init_pair(1, COLOR_BLACK, COLOR_RED);
	init_pair(2, COLOR_BLACK, COLOR_GREEN);
	init_pair(3, COLOR_BLACK, COLOR_BLUE);
	init_pair(4, COLOR_BLACK, COLOR_WHITE);
	init_pair(5, COLOR_BLACK, COLOR_BLACK);
#else
	init_pair(1, COLOR_RED, COLOR_BLACK);
	init_pair(2, COLOR_GREEN, COLOR_BLACK);
	init_pair(3, COLOR_BLUE, COLOR_BLACK);
	init_pair(4, COLOR_WHITE, COLOR_BLACK);
	init_pair(5, COLOR_BLACK, COLOR_BLACK);
#endif

#endif

#if X_VISUALIZATION
	display = XOpenDisplay(NULL);
	screen_number = DefaultScreen(display);
	root_window = RootWindow(display, screen_number);
	window_x = 0;
	window_y = 0;
	window_width = (WORLD_WIDTH * 4) + 16 + 3;
	window_height = (WORLD_HEIGHT * 9) + 16 + 8;
	window_border_width = 0;
	window_border_color = BlackPixel(display, screen_number);
	window_background_color = WhitePixel(display, screen_number);
	window = XCreateSimpleWindow(display, root_window, window_x, window_y,
			window_width, window_height, window_border_width,
			window_border_color, window_background_color);
	XMapWindow(display, window);
	XFlush(display);

	gc_value_mask = 0;
	gc = XCreateGC(display, window, gc_value_mask, &gc_values);
	XSync(display, False);

	default_visual = DefaultVisual(display, DefaultScreen(display));
	colormap = XCreateColormap(display, window, default_visual, AllocNone);
	XAllocNamedColor(display, colormap, "red", &system_color_red,
			&exact_color);
	XAllocNamedColor(display, colormap, "green", &system_color_green,
			&exact_color);
	XAllocNamedColor(display, colormap, "blue", &system_color_blue,
			&exact_color);
	XAllocNamedColor(display, colormap, "white", &system_color_white,
			&exact_color);
	XAllocNamedColor(display, colormap, "black", &system_color_black,
			&exact_color);
#endif

	world = new_world();
	for (each_iteration = 0; each_iteration < ITERATIONS; each_iteration++) {

		x = random_unsigned_int(WORLD_WIDTH);
		y = random_unsigned_int(WORLD_HEIGHT);
		each_x = x;
		each_y = y;

#if CURSES_VISUALIZATION
		for (x = 0; x < WORLD_WIDTH; x++) {
			for (y = 0; y < WORLD_HEIGHT; y++) {
				if (NULL == world->organisms[x][y]) {
					color = 'x';
					c = ' ';
				}
				else {
					color = display_color(world->organisms[x][y]);
					c = world->organisms[x][y]->face;
				}
				switch (color) {
					case 'r':
						mvaddch(y, x, c | COLOR_PAIR(1));
						break;
					case 'g':
						mvaddch(y, x, c | COLOR_PAIR(2));
						break;
					case 'b':
						mvaddch(y, x, c | COLOR_PAIR(3));
						break;
					case 'w':
						mvaddch(y, x, c | COLOR_PAIR(4));
						break;
					default:
						mvaddch(y, x, c | COLOR_PAIR(5));
						break;
				}
			}
		}
		refresh();
		usleep(SLEEP_US);
#endif

#if X_VISUALIZATION
		if (0 == (each_iteration % X_FRAME_SAMPLE)) {
			for (each_x = 0; each_x < WORLD_WIDTH; each_x++) {
				for (each_y = 0; each_y < WORLD_HEIGHT; each_y++) {
					organism = world->organisms[each_x][each_y];

					display_gene_start_address = gene_start_address(organism,
							GENE_INDEX_DISPLAY);
					red_uint = unsigned_int_from_genome(organism,
							display_gene_start_address + 0, 32);
					green_uint = unsigned_int_from_genome(organism,
							display_gene_start_address + 32, 32);
					blue_uint = unsigned_int_from_genome(organism,
							display_gene_start_address + 64, 32);

					system_color.red = red_uint % 8192;
					system_color.green = green_uint;
					system_color.blue = blue_uint;

					XAllocColor(display, colormap, &system_color);

					XSetForeground(display, gc, system_color.pixel);

					/* XDrawPoint(display, window, gc, each_x, each_y); */
					XDrawPoint(display, window, gc,
							((displayed_iteration % 4) * 128)
							+ (displayed_iteration % 4) + each_x,
							((displayed_iteration / 4) * 64) +
							 (displayed_iteration / 4) + each_y);
				}
			}
			displayed_iteration++;
			XFlush(display);
			usleep(SLEEP_US);
		}
#endif

		iterate_world(world);
	}

#if CURSES_VISUALIZATION
	endwin();
#endif

#if X_VISUALIZATION
	while (1) {
		usleep(SLEEP_US);
	}

	XUnmapWindow(display, window);
	XDestroyWindow(display, window);
	XCloseDisplay(display);
#endif

	destroy_world(world);

	return 0;
}
Esempio n. 5
0
/*--------------------------------------------------------------*/
int	main( int main_argc, char **main_argv)

{
	clock_t startClock = clock();
	/*--------------------------------------------------------------*/
	/*	Non-function definitions. 									*/
	/*--------------------------------------------------------------*/
	struct	command_line_object 	*command_line;
	struct	tec_object				*tec;
	struct	world_object			*world;
	struct	world_output_file_object	*output;
	struct	world_output_file_object	*growth_output;
	char	*prefix;
	
	/*--------------------------------------------------------------*/
	/* Local Function declarations 									*/
	/*--------------------------------------------------------------*/
	struct   command_line_object *construct_command_line(
		int,
		char **);
	
	struct   world_object *construct_world(
		struct command_line_object *);
	
	struct	world_output_file_object	*construct_output_files(
		char *,
		struct command_line_object	*);
	
	
	struct	tec_object	*construct_tec(
		struct command_line_object *,
		struct world_object * );
	
	void 	execute_tec(
		struct	tec_object	*,
		struct	command_line_object	*,
		struct	world_output_file_object *,
		struct	world_output_file_object *,
		struct	world_object *);
	
	void	destroy_tec(
		struct tec_object * );
	
	void	destroy_output_files(
		struct	command_line_object	*,
		struct	world_output_file_object	*);
	
	void	destroy_world(
		struct command_line_object *,
		struct world_object *);
	
	void	destroy_command_line(
		struct command_line_object * );

	void   add_headers(
		struct world_output_file_object *,
		struct command_line_object * );

	void   add_growth_headers(
		struct world_output_file_object *,
		struct command_line_object * );

	
	srand((unsigned)(time(0)));

	/*--------------------------------------------------------------*/
	/*	Command line parsing.										*/
	/*--------------------------------------------------------------*/
	command_line = construct_command_line(main_argc, main_argv);


	/*--------------------------------------------------------------*/
	/* Check if print version flag was set. If so, just print out   */
	/* the version and return.                                      */
	/*--------------------------------------------------------------*/
	if (command_line[0].version_flag > 0 ) {
		printf("RHESSys Version: %s\n", RHESSYS_VERSION);
		return(EXIT_SUCCESS);
	}


	if (command_line[0].verbose_flag > 0 )
		fprintf(stderr,"FINISHED CON COMMAND LINE ***\n");
	
	/*--------------------------------------------------------------*/
	/*	Construct the world object.									*/
	/*--------------------------------------------------------------*/
	world = construct_world( command_line );
	if (command_line[0].verbose_flag > 0  )
		fprintf(stderr,"FINISHED CON WORLD ***\n");
	/*--------------------------------------------------------------*/
	/*	Construct the output file objects.							*/
	/*--------------------------------------------------------------*/
	/*--------------------------------------------------------------*/
	/*      Make up the prefix for the output files.                */
	/*--------------------------------------------------------------*/
	prefix = (char *)calloc(256, sizeof(char));
	if ( command_line[0].output_prefix != NULL ){
		strcpy(prefix,command_line[0].output_prefix);
	}
	else{
		strcpy(prefix,PRE);
	}
	output = construct_output_files( prefix, command_line );
	if (command_line[0].grow_flag > 0) {
		strcat(prefix,"_grow");
		growth_output = construct_output_files(prefix, command_line );
	}
	else growth_output = NULL;

	add_headers(output, command_line);
		if (command_line[0].grow_flag > 0)
			add_growth_headers(growth_output, command_line);



	if(command_line[0].verbose_flag > 0 )
		fprintf(stderr,"FINISHED CON OUTPUT\n");
	
	/*--------------------------------------------------------------*/
	/*	Create the tec object (temporal event control)				*/
	/*																*/
	/*	This object specifies temporal events such as output.		*/
	/*--------------------------------------------------------------*/
	tec = construct_tec( command_line, world);
	
	if (command_line[0].verbose_flag > 0 )
		fprintf(stderr,"FINISHED CON TEC\n");
	/*--------------------------------------------------------------*/
	/*	AN EVENT LOOP WOULD GO HERE.								*/
	/*--------------------------------------------------------------*/
	fprintf(stderr,"Beginning Simulation\n");
	execute_tec( tec, command_line, output, growth_output, world );
	if (command_line[0].verbose_flag > 0 )
		fprintf(stderr,"FINISHED EXE TEC\n");
	
	/*--------------------------------------------------------------*/
	/*	Destroy the tec object.										*/
	/*--------------------------------------------------------------*/
	destroy_tec( tec );
	
	if (command_line[0].verbose_flag > 0 )
		fprintf(stderr,"FINISHED DES TEC\n");
	
	/*--------------------------------------------------------------*/
	/*	Destroy output file objects (close them)					*/
	/*--------------------------------------------------------------*/
	destroy_output_files( command_line, output );
	
	if (command_line[0].grow_flag > 0)
		destroy_output_files( command_line, growth_output );
	
	if (command_line[0].verbose_flag > 0 )
		fprintf(stderr,"FINISHED DES OUTPUT FILES\n");
	
	/*--------------------------------------------------------------*/
	/*	Destroy the world.											*/
	/*--------------------------------------------------------------*/
	destroy_world(command_line, world );
	
	if (command_line[0].verbose_flag > 0 )
		fprintf(stderr,"FINISHED DES WORLD\n");
	
	/*--------------------------------------------------------------*/
	/*	Destroy the command_line_object								*/
	/*--------------------------------------------------------------*/
	destroy_command_line( command_line );
	
	if (command_line[0].verbose_flag > 0 )
		fprintf(stderr,"FINISHED DES COMMAND LINE\n");
	
	/*--------------------------------------------------------------*/
	/*	The end.													*/
	/*--------------------------------------------------------------*/
	clock_t endClock =clock();
	
	printf("\ntime cost = %ld seconds\n",(endClock - startClock)/CLOCKS_PER_SEC);

	return(EXIT_SUCCESS);
	
} /*end main*/