// File: /workspace/github_cpp_makefile_star2_O2/samuel500[P]NEAT/individual.cpp
// Function: Individual::mutate_add_node(std::vector<Node*, std::allocator<Node*>>*, std::vector<Connection*, std::allocator<Connection*>>*)
bool Individual::mutate_add_node(vector<Node*> *all_nodes, vector<Connection*> *all_connections){

	bool found_active = false;
	int rand_connection_id;
	for(int i=0; i<15; i++){
		rand_connection_id = randint(0, connections.size()-1);
		if(connections[rand_connection_id]->is_active){
			found_active = true;
			break;
		}
	}
	if(!found_active){ 
		cout << "no active connection found"<< endl;
		return false;


	}

	Connection *chosen_con = connections[rand_connection_id];
	Node *init_node = chosen_con->in_node;
	Node *end_node = chosen_con->out_node;

	chosen_con->is_active = false;


	bool found = false;
	int new_node_innov_number, new_in_connection_innov_number, new_out_connection_innov_number;


	vector<Node*>::iterator nodePtr;

	for(nodePtr=all_nodes->begin(); nodePtr != all_nodes->end(); ++nodePtr){
		// this will only find those nodes that haven't gotten any new connections since added
		// (Stanley's implementation only looks for nodes mutated in the current generation)
		if((*nodePtr)->in_connections.size()==1 && (*nodePtr)->out_connections.size()==1){

			if((*nodePtr)->in_connections[0]->in_node->hist_marking==init_node->hist_marking && (*nodePtr)->out_connections[0]->out_node->hist_marking==end_node->hist_marking){
				found = true;
				new_node_innov_number = (*nodePtr)->hist_marking;
				new_in_connection_innov_number = (*nodePtr)->in_connections[0]->hist_marking;
				new_out_connection_innov_number = (*nodePtr)->out_connections[0]->hist_marking;
			}

		}

	}

	if(!found){
		new_node_innov_number = *innov_num;
		(*innov_num)++;
		new_in_connection_innov_number = *innov_num;
		(*innov_num)++;
		new_out_connection_innov_number = *innov_num;
		(*innov_num)++;
	}


	Node *new_node = new Node(HIDDEN, new_node_innov_number, *generation);
	
	Connection *new_in_connection = new Connection(init_node, new_node, new_in_connection_innov_number, *generation);
	Connection *new_out_connection = new Connection(new_node, end_node, new_out_connection_innov_number, *generation);
	
	new_in_connection->weight = 1.0;
	new_out_connection->weight = chosen_con->weight;


	connections.push_back(new_in_connection);
	connections.push_back(new_out_connection);

	// all_connections->push_back(new_in_connection);
	// all_connections->push_back(new_out_connection);

	nodes.insert(nodes.begin()+(nodes.size()-(*out_size)), new_node); // add before the output nodes
	// all_nodes->push_back(new_node);

	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x60(%rdi), %r13
xorl %eax, %eax
movl %eax, %r15d
cmpl $0xf, %eax
je 0x5aad
movq 0x68(%rbx), %r12
subq 0x60(%rbx), %r12
shrq $0x3, %r12
callq 0x1030
cltd
idivl %r12d
movslq %edx, %rax
movq 0x60(%rbx), %rcx
movq (%rcx,%rax,8), %r12
leal 0x1(%r15), %eax
cmpb $0x0, 0x18(%r12)
je 0x59f3
movq %r13, -0x40(%rbp)
movq (%r12), %r13
movq 0x8(%r12), %r10
movb $0x0, 0x18(%r12)
movq (%r14), %rax
movq 0x8(%r14), %rcx
xorl %edx, %edx
movq %rsi, -0x30(%rbp)
cmpq %rcx, %rax
je 0x5acd
movq (%rax), %rsi
movq 0x18(%rsi), %rdi
movq 0x20(%rsi), %r8
subq %rdi, %r8
cmpq $0x8, %r8
jne 0x5aa7
movq 0x30(%rsi), %r8
movq 0x38(%rsi), %r9
subq %r8, %r9
cmpq $0x8, %r9
jne 0x5aa7
movq (%rdi), %rdi
movq (%rdi), %r9
movl 0x4(%r9), %r9d
cmpl 0x4(%r13), %r9d
jne 0x5aa7
movq (%r8), %r8
movq 0x8(%r8), %r9
movl 0x4(%r9), %r9d
cmpl 0x4(%r10), %r9d
jne 0x5aa7
movl 0x4(%rsi), %edx
movq %rdx, -0x30(%rbp)
movl 0x1c(%rdi), %r14d
movl 0x1c(%r8), %r11d
movb $0x1, %dl
addq $0x8, %rax
jmp 0x5a4b
movq 0x5524(%rip), %rdi      # 0xafd8
leaq 0x1760(%rip), %rsi      # 0x721b
callq 0x10e0
movq %rax, %rdi
callq 0x1070
jmp 0x5be0
movq %r10, -0x48(%rbp)
testb $0x1, %dl
jne 0x5aed
movq 0x78(%rbx), %rax
movl (%rax), %ecx
leal 0x1(%rcx), %r14d
leal 0x2(%rcx), %r11d
movq %rcx, -0x30(%rbp)
addl $0x3, %ecx
movl %ecx, (%rax)
movl %r14d, -0x38(%rbp)
movl %r11d, -0x34(%rbp)
pushq $0x48
popq %rdi
callq 0x10f0
movq %rax, %r14
movq 0x90(%rbx), %rax
movl (%rax), %ecx
pushq $0x2
popq %rsi
movq %r14, %rdi
movq -0x30(%rbp), %rdx
callq 0x610a
movq %r14, -0x58(%rbp)
pushq $0x28
popq %rdi
callq 0x10f0
movq %rax, %rdi
movq 0x90(%rbx), %rax
movl (%rax), %r8d
movq %rdi, -0x30(%rbp)
movq %r13, %rsi
movq %r14, %rdx
movl -0x38(%rbp), %ecx
callq 0x6216
movq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
pushq $0x28
popq %rdi
callq 0x10f0
movq %rax, %r13
movq 0x90(%rbx), %rax
movl (%rax), %r8d
movq %r13, %rdi
movq %r14, %rsi
movq -0x48(%rbp), %rdx
movl -0x34(%rbp), %ecx
callq 0x6216
leaq -0x60(%rbp), %r14
movq %r13, (%r14)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq -0x30(%rbp), %rcx
movq %rax, 0x10(%rcx)
movsd 0x10(%r12), %xmm0
movsd %xmm0, 0x10(%r13)
leaq -0x50(%rbp), %rsi
movq -0x40(%rbp), %r12
movq %r12, %rdi
callq 0x2248
movq %r12, %rdi
movq %r14, %rsi
callq 0x2248
leaq 0x18(%rbx), %rdi
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
movq 0x88(%rbx), %rdx
movslq (%rdx), %rdx
subq %rdx, %rcx
leaq (%rax,%rcx,8), %rsi
leaq -0x58(%rbp), %rdx
callq 0x5f04
cmpl $0xf, %r15d
setb %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x28
popq %rsi
movq %r13, %rdi
jmp 0x5c16
movq %rax, %rbx
pushq $0x28
popq %rsi
movq -0x30(%rbp), %rdi
jmp 0x5c16
movq %rax, %rbx
pushq $0x48
popq %rsi
movq %r14, %rdi
callq 0x1100
movq %rbx, %rdi
callq 0x1180
nop