// File: /workspace/github_c_makefile_star2_O2/kernfel[P]fNEAT/main.c
// Function: bots_in_a_maze
void bots_in_a_maze() {
	int err=0;

	struct NEAT_Params params;
	get_neat_params( &params );
	params.allowed_funcs = (enum CPPNFunc[]){CF_GAUSS, CF_SIGMOID, CF_SINE, CF_LINEAR};
	params.num_allowed_funcs = 4;

	struct Robot_Params bot_params;
	get_robot_params( &bot_params );
	bot_params.num_dist_sensors = 5;
	bot_params.dist_sensor_pos = (double[]){-M_PI/2, -M_PI/4, 0, M_PI/4, M_PI/2};

	TileMaze maze;
	maze.tile_width = 2;
	maze.x = 8;
	maze.y = 8;
	maze.tiles = (unsigned char []){
	1, 1, 1, 0, 1, 1, 1, 1,
	1, 0, 1, 0, 1, 0, 1, 0,
	1, 0, 1, 0, 1, 0, 1, 1,
	1, 0, 1, 0, 1, 1, 0, 1,
	1, 1, 1, 0, 0, 1, 1, 1,
	1, 0, 1, 1, 1, 1, 0, 1,
	1, 0, 0, 0, 1, 0, 0, 1,
	1, 0, 1, 1, 1, 1, 1, 1,
	};

	CPPN cppn;
	if (( err = create_CPPN( &cppn, &params ) ))
		exit(err);

	Population pop;
	if (( err = create_Population( &pop, &params, &cppn ) ))
		exit(err);
	randomise_population( &pop, &params );

	pNetwork net;
	if (( err = create_pNetwork( &net ) ))
		exit(err);

	eNetwork controller;
	if (( err = create_eNetwork( &controller ) ))
		exit(err);

	pNode sensors[NUM_SENSORS], motors[NUM_MOTORS];
/*	pNode sensors[NUM_SENSORS+1], motors[NUM_MOTORS];*/
/*	sensors[NUM_SENSORS].x[0] = 0;*/
/*	sensors[NUM_SENSORS].x[1] = -1;*/
	m_get_spread( sensors, NUM_SENSORS, -0.5 );
	m_get_spread( motors, NUM_MOTORS, 0.5 );

	FILE *fp = fopen( "logs/champions.txt", "w" );

	int i, gen;
	int behaviour[maze.x*maze.y], best_behaviour[maze.x*maze.y], best_score, winner, peak, peak_score=0;
	for ( gen=0; 1; gen++ ) {
		best_score = 0;
		for ( i=0; i<params.population_size; i++ ) {
			reset_pNetwork( &net );
			if (( err = build_pNetwork( &net, &pop.members[i].genotype, &params, NUM_SENSORS, sensors, NUM_MOTORS, motors ) ))
/*			if (( err = build_pNetwork( &net, &pop.members[i].genotype, &params, NUM_SENSORS+1, sensors, NUM_MOTORS, motors ) ))*/
				exit(err);

			int s=0;
			if ( ! net.num_used_links || ! net.num_used_nodes ) {
				pop.members[i].score = 0;
			} else {
				memset( behaviour, 0, maze.x*maze.y*sizeof *behaviour );
				if (( err = run_trial( &net, &params, &bot_params, &maze, behaviour ) ))
					exit(err);
				s = analyse_maze( &maze, behaviour, 0 );
				pop.members[i].score = s*s;

				if ( s > best_score ) {
					best_score = s;
					memcpy( best_behaviour, behaviour, maze.x*maze.y*sizeof *behaviour );
					winner = i;
				}

/*				printf( "indiv %5x | sp %3x | %2d tiles | %4d nodes | %6d links | inputs used: %d%d%d%d%d | bias used: %d\n",*/
/*					pop.members[i].id,*/
/*					pop.members[i].species_id,*/
/*					s,*/
/*					net.num_used_nodes,*/
/*					net.num_used_links,*/
/*					net.p_nodes[0][0].used,*/
/*					net.p_nodes[0][1].used,*/
/*					net.p_nodes[0][2].used,*/
/*					net.p_nodes[0][3].used,*/
/*					net.p_nodes[0][4].used,*/
/*					net.p_nodes[0][5].used*/
/*				*/
/*				);*/
			}

/*			printf( "%5x (%3x): ", pop.members[i].id, pop.members[i].species_id );*/
			printf( "%2d  ", s);
			fflush(stdout);
		}

		printf( "\n\nGeneration %d's champion explorer, #%5x of species #%3x, had a good look at %d tiles:",
			gen,
			pop.members[winner].id,
			pop.members[winner].species_id,
			best_score
		);
		analyse_maze( &maze, best_behaviour, 1 );
		
/*		if ( peak_score > best_score ) {*/
/*			fprintf( fp, "\n>>> Generation %d | #%5x | species #%3x | %2d tiles\n",*/
/*				gen,*/
/*				pop.members[peak].id,*/
/*				pop.members[peak].species_id,*/
/*				(int) sqrt(pop.members[peak].score)*/
/*			);*/
/*			dump_CPPN( &pop.members[peak].genotype, fp );*/

/*			reset_pNetwork( &net );*/
/*			if (( err = build_pNetwork( &net, &pop.members[peak].genotype, &params, NUM_SENSORS+1, sensors, NUM_MOTORS, motors ) ))*/
/*				exit(err);*/
/*			//dump_pNetwork( &net, fp );*/
/*			if (( err = build_eNetwork( &controller, &net, &params ) ))*/
/*				exit(err);*/
/*			dump_eNetwork( &controller, fp );*/
/*		} else {*/
/*			peak_score = best_score;*/
/*			peak = winner;*/
/*		}*/

/*		fprintf( fp, "\nGeneration %d | #%5x | species #%3x | %2d tiles\n",*/
/*			gen,*/
/*			pop.members[winner].id,*/
/*			pop.members[winner].species_id,*/
/*			best_score*/
/*		);*/
/*		dump_CPPN( &pop.members[winner].genotype, fp );*/
/*		reset_pNetwork( &net );*/
/*		if (( err = build_pNetwork( &net, &pop.members[winner].genotype, &params, NUM_SENSORS+1, sensors, NUM_MOTORS, motors ) ))*/
/*			exit(err);*/
/*		//dump_pNetwork( &net, fp );*/
/*		if (( err = build_eNetwork( &controller, &net, &params ) ))*/
/*			exit(err);*/
/*		dump_eNetwork( &controller, fp );*/

/*		if ( ! (gen%5) ) {*/
/*			fflush(fp);*/
/*			printf( "\nPaused... e=exit, anything else=continue: " );*/
/*			unsigned char c=getchar();*/
/*			if ( c == 'e' ) {*/
/*				fclose( fp );*/
/*				delete_eNetwork( &controller );*/
/*				delete_pNetwork( &net );*/
/*				delete_Population( &pop );*/
/*				delete_CPPN( &cppn );*/
/*				exit(0);*/
/*			}*/
/*		}*/

/*		while(!getchar());*/

		epoch( &pop, &params );
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x398, %rsp            # imm = 0x398
movaps 0x493c(%rip), %xmm0     # 0x6010
leaq -0x2d0(%rbp), %rsi
movaps %xmm0, (%rsi)
movaps 0x493b(%rip), %xmm0     # 0x6020
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, 0x20(%rsi)
movabsq $0x3fc999999999999a, %rax # imm = 0x3FC999999999999A
movq %rax, 0x30(%rsi)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x38(%rsi)
movabsq $0xf00000001, %rax      # imm = 0xF00000001
movq %rax, 0x58(%rsi)
movaps 0x4922(%rip), %xmm0     # 0x6040
movaps %xmm0, 0x60(%rsi)
movaps 0x4927(%rip), %xmm0     # 0x6050
movaps %xmm0, 0x70(%rsi)
movaps 0x492c(%rip), %xmm0     # 0x6060
movaps %xmm0, 0x80(%rsi)
movabsq $0x3fd999999999999a, %rax # imm = 0x3FD999999999999A
movq %rax, 0x90(%rsi)
movl $0xf, 0x98(%rsi)
movaps 0x4913(%rip), %xmm0     # 0x6070
movaps %xmm0, 0xa0(%rsi)
movaps 0x4915(%rip), %xmm0     # 0x6080
movaps %xmm0, 0xb0(%rsi)
movaps 0x4917(%rip), %xmm0     # 0x6090
movaps %xmm0, 0xc0(%rsi)
movaps 0x4919(%rip), %xmm0     # 0x60a0
movaps %xmm0, 0xd0(%rsi)
movaps 0x491b(%rip), %xmm0     # 0x60b0
movaps %xmm0, 0xe0(%rsi)
movaps 0x491d(%rip), %xmm0     # 0x60c0
movaps %xmm0, 0xf0(%rsi)
movabsq $0x4004000000000000, %rax # imm = 0x4004000000000000
movq %rax, 0x100(%rsi)
andq $0x0, 0x108(%rsi)
andl $0x0, 0x110(%rsi)
movaps 0x491f(%rip), %xmm0     # 0x60f0
leaq -0x110(%rbp), %rax
movaps %xmm0, (%rax)
movq %rax, 0x40(%rsi)
movaps 0x491a(%rip), %xmm0     # 0x6100
movups %xmm0, 0x48(%rsi)
movaps 0x48df(%rip), %xmm0     # 0x60d0
movaps %xmm0, -0xc0(%rbp)
movaps 0x48e1(%rip), %xmm0     # 0x60e0
movaps %xmm0, -0xb0(%rbp)
movabsq $0x3fb999999999999a, %rax # imm = 0x3FB999999999999A
movq %rax, -0xa0(%rbp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, -0x90(%rbp)
movb $0x1, -0x80(%rbp)
movl $0x5, -0x98(%rbp)
movabsq $0x3ff921fb54442d18, %rax # imm = 0x3FF921FB54442D18
leaq -0x1b0(%rbp), %rcx
movq %rax, 0x20(%rcx)
movups 0x48ee(%rip), %xmm0     # 0x6140
movaps %xmm0, (%rcx)
movups 0x48f4(%rip), %xmm0     # 0x6150
movaps %xmm0, 0x10(%rcx)
movq %rcx, -0x88(%rbp)
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
movq %rax, -0x58(%rbp)
movabsq $0x800000008, %rax      # imm = 0x800000008
movq %rax, -0x50(%rbp)
movups 0x490e(%rip), %xmm0     # 0x6198
leaq -0x3c0(%rbp), %rax
movaps %xmm0, 0x30(%rax)
movups 0x48ec(%rip), %xmm0     # 0x6188
movaps %xmm0, 0x20(%rax)
movups 0x48d1(%rip), %xmm0     # 0x6178
movaps %xmm0, 0x10(%rax)
movupd 0x48b5(%rip), %xmm0     # 0x6168
movapd %xmm0, (%rax)
movq %rax, -0x48(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x1b50
testl %eax, %eax
jne 0x1b32
leaq -0x78(%rbp), %rdi
leaq -0x2d0(%rbp), %rsi
leaq -0x138(%rbp), %rdx
callq 0x3044
testl %eax, %eax
jne 0x1b32
leaq -0x78(%rbp), %rdi
leaq -0x2d0(%rbp), %rsi
callq 0x31a7
leaq -0xf8(%rbp), %rdi
callq 0x4734
testl %eax, %eax
jne 0x1b32
leaq -0x308(%rbp), %rdi
callq 0x5b24
testl %eax, %eax
jne 0x1b32
leaq -0x380(%rbp), %rdi
pushq $0x5
popq %rsi
movsd 0x47e0(%rip), %xmm0     # 0x6118
callq 0x1598
leaq -0x180(%rbp), %rdi
pushq $0x3
popq %rsi
movsd 0x47d1(%rip), %xmm0     # 0x6120
callq 0x1598
leaq 0x484d(%rip), %rdi      # 0x61a8
leaq 0x4859(%rip), %rsi      # 0x61bb
callq 0x1120
movq %rsp, %r13
addq $-0x100, %r13
movq %r13, %rsp
movq %rsp, %rax
addq $-0x100, %rax
movq %rax, -0x40(%rbp)
movq %rax, %rsp
leaq -0xf8(%rbp), %rbx
leaq -0x58(%rbp), %r12
movl $0x0, -0x30(%rbp)
xorl %r14d, %r14d
xorl %r15d, %r15d
movl $0x0, -0x2c(%rbp)
movslq -0x280(%rbp), %rax
cmpq %rax, %r15
jge 0x1ada
movq %rbx, %rdi
callq 0x4837
movq -0x78(%rbp), %rsi
addq %r14, %rsi
subq $0x8, %rsp
movq %rbx, %rdi
leaq -0x2d0(%rbp), %rdx
pushq $0x5
popq %rcx
leaq -0x380(%rbp), %r8
pushq $0x3
popq %r9
leaq -0x180(%rbp), %rax
pushq %rax
callq 0x4cc7
addq $0x10, %rsp
testl %eax, %eax
jne 0x1b32
cmpl $0x0, -0xd4(%rbp)
je 0x1a9d
cmpl $0x0, -0xf4(%rbp)
je 0x1a9d
movslq -0x50(%rbp), %rax
movslq -0x4c(%rbp), %rdx
imulq %rax, %rdx
shlq $0x2, %rdx
movq %r13, %rdi
xorl %esi, %esi
callq 0x1060
movq %rbx, %rdi
leaq -0x2d0(%rbp), %rsi
leaq -0xc0(%rbp), %rdx
movq %r12, %rcx
movq %r13, %r8
callq 0x13f2
testl %eax, %eax
jne 0x1b32
movq %r12, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x15df
movl %eax, %r12d
imull %eax, %eax
cvtsi2sd %eax, %xmm0
movq -0x78(%rbp), %rax
movsd %xmm0, 0x28(%rax,%r14)
cmpl -0x2c(%rbp), %r12d
jle 0x1aaa
movslq -0x50(%rbp), %rax
movslq -0x4c(%rbp), %rdx
imulq %rax, %rdx
shlq $0x2, %rdx
movq -0x40(%rbp), %rdi
movq %r13, %rsi
callq 0x10c0
movl %r12d, -0x2c(%rbp)
movl %r15d, -0x34(%rbp)
jmp 0x1aaa
movq -0x78(%rbp), %rax
andq $0x0, 0x28(%rax,%r14)
xorl %r12d, %r12d
leaq 0x470c(%rip), %rdi      # 0x61bd
movl %r12d, %esi
xorl %eax, %eax
callq 0x1050
movq 0x751e(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
callq 0x10f0
incq %r15
addq $0x38, %r14
leaq -0x58(%rbp), %r12
jmp 0x19a3
movq -0x78(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x38, %rcx, %rdx
movl 0x30(%rax,%rdx), %ecx
movl 0x34(%rax,%rdx), %edx
leaq 0x46ce(%rip), %rdi      # 0x61c3
movl -0x30(%rbp), %r14d
movl %r14d, %esi
movl -0x2c(%rbp), %r8d
xorl %eax, %eax
callq 0x1050
movq %r12, %rdi
movq -0x40(%rbp), %rsi
pushq $0x1
popq %rdx
callq 0x15df
leaq -0x78(%rbp), %rdi
leaq -0x2d0(%rbp), %rsi
callq 0x31e7
incl %r14d
movl %r14d, -0x30(%rbp)
jmp 0x1996
movl %eax, %edi
callq 0x1170