// File: /workspace/github_c_makefile_star2_O2/S01den[P]Lost_CavernRL/pnj.c
// Function: setPnj
void setPnj(Map map[LONGUEUR][LARGEUR], Pnj pnj[], int *nbrPnjOnFloor)
{
	*nbrPnjOnFloor = rand()%NBR_PNJ_MAX+1;
	char *nameClerc[] = {"Sister Maria", "Brother Mikael","Mother Cynthia", "Father Lyris"};
	char *nameWorker[] = {"Swann","Nicolas","Thomas","Erica"};
	char *nameErmit[] = {"Michel","Wallace","Francis","Jannice"};
	char *nameScientific[] = {"Dr Jane","Dr Cooper","Dr Jones","Dr Marlene"}; 
	
	int i = 0;

	for(i = 0; i < *nbrPnjOnFloor; i++) // let's spawn the NPC
	{
		pnj[i].x = rand()%LONGUEUR;
		pnj[i].y = rand()%LARGEUR;
		pnj[i].questDone = 0;

		if(pnj[i].x == 0)
			pnj[i].x++;
		if(pnj[i].x == LONGUEUR)
			pnj[i].x--;
		if(pnj[i].y == 0)
			pnj[i].y++;
		if(pnj[i].y == LARGEUR)
			pnj[i].y--;

		if(map[pnj[i].x][pnj[i].y].content != FLOOR)
		{
			if(LARGEUR-pnj[i].y > pnj[i].y)
			{
				while(map[pnj[i].x][pnj[i].y].content != FLOOR && pnj[i].y < LARGEUR)
				{
					pnj[i].y++;
				}
			}
			else
			{
				while(map[pnj[i].x][pnj[i].y].content != FLOOR && pnj[i].y > 0)
				{
					pnj[i].y--;
				}
			}
		}

		int rollType = rand()%4;
		pnj[i].type = rollType;

		switch(pnj[i].type)
		{
			case 0: // cleric
				pnj[i].name = nameClerc[rand()%4];
				pnj[i].quest = rand()%3;
				break;
			case 1: // worker
				pnj[i].name = nameWorker[rand()%4];
				pnj[i].quest = rand()%3+3;
				break;
			case 2: // ermit
				pnj[i].name = nameErmit[rand()%4];
				pnj[i].quest = rand()%3+6;
				break;
			case 3: // scientific
				pnj[i].name = nameScientific[rand()%4];
				pnj[i].quest = rand()%2+9;
				break;
		}
			map[pnj[i].x][pnj[i].y].content = PNJ;
			map[pnj[i].x][pnj[i].y].typePnj = pnj[i].type;

	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
callq 0x11d0
pushq $0x4
popq %rcx
cltd
idivl %ecx
incl %edx
movl %edx, (%rbx)
leaq -0x1c(%r15), %rax
movq %rax, 0x10(%rsp)
leaq 0x1c(%r15), %rax
movq %rax, 0x8(%rsp)
xorl %ebp, %ebp
movslq %edx, %rax
cmpq %rax, %rbp
jge 0xb312
callq 0x11d0
cltd
pushq $0x27
popq %rcx
idivl %ecx
movq %rbp, %r12
shlq $0x5, %r12
leaq (%r14,%r12), %r13
movl %edx, 0x14(%r14,%r12)
callq 0x11d0
cltd
movl $0x82, %ecx
idivl %ecx
movl %edx, 0x18(%r14,%r12)
andl $0x0, 0x10(%r14,%r12)
movl 0x14(%r14,%r12), %eax
pushq $0x1
popq %rcx
testl %eax, %eax
je 0xb168
cmpl $0x27, %eax
jne 0xb16e
pushq $0x26
popq %rcx
movl %ecx, 0x14(%r13)
movl %ecx, %eax
testl %edx, %edx
jne 0xb17d
movl $0x1, 0x18(%r13)
pushq $0x1
popq %rdx
movslq %eax, %rcx
movslq %edx, %rax
imulq $0xe38, %rcx, %rdi      # imm = 0xE38
leaq (%r15,%rdi), %rsi
imulq $0x1c, %rax, %rcx
movl (%rcx,%rsi), %esi
cmpl $0x1, %esi
jne 0xb1f2
callq 0x11d0
cltd
pushq $0x4
popq %rcx
idivl %ecx
movl %edx, 0x8(%r13)
cmpl $0x3, %edx
ja 0xb2e3
leaq 0x41b7(%rip), %rcx      # 0xf370
movslq (%rcx,%rdx,4), %rax
addq %rcx, %rax
jmpq *%rax
callq 0x11d0
cltd
pushq $0x4
popq %rcx
idivl %ecx
movslq %edx, %rax
leaq 0x41c9(%rip), %rcx      # 0xf3a0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq %rax, (%r13)
callq 0x11d0
cltd
pushq $0x3
popq %rcx
idivl %ecx
jmp 0xb2df
movl $0x82, %r8d
subl %edx, %r8d
cmpl %edx, %r8d
jle 0xb226
addq 0x8(%rsp), %rcx
addq %rdi, %rcx
incl %edx
cmpq $0x81, %rax
jg 0xb19a
cmpl $0x1, %esi
je 0xb19a
incq %rax
movl %edx, 0x18(%r13)
movl (%rcx), %esi
addq $0x1c, %rcx
jmp 0xb208
addq 0x10(%rsp), %rcx
addq %rdi, %rcx
decl %edx
testq %rax, %rax
jle 0xb19a
cmpl $0x1, %esi
je 0xb19a
decq %rax
movl %edx, 0x18(%r13)
movl (%rcx), %esi
addq $-0x1c, %rcx
jmp 0xb22e
callq 0x11d0
cltd
pushq $0x4
popq %rcx
idivl %ecx
movslq %edx, %rax
leaq 0x415a(%rip), %rcx      # 0xf3c0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq %rax, (%r13)
callq 0x11d0
cltd
pushq $0x3
popq %rcx
idivl %ecx
addl $0x6, %edx
jmp 0xb2df
callq 0x11d0
cltd
pushq $0x4
popq %rcx
idivl %ecx
movslq %edx, %rax
leaq 0x413a(%rip), %rcx      # 0xf3d0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq %rax, (%r13)
callq 0x11d0
cltd
pushq $0x2
popq %rcx
idivl %ecx
addl $0x9, %edx
jmp 0xb2df
callq 0x11d0
cltd
pushq $0x4
popq %rcx
idivl %ecx
movslq %edx, %rax
leaq 0x40ea(%rip), %rcx      # 0xf3b0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq %rax, (%r13)
callq 0x11d0
cltd
pushq $0x3
popq %rcx
idivl %ecx
addl $0x3, %edx
movl %edx, 0xc(%r13)
movslq 0x14(%r13), %rax
movslq 0x18(%r13), %rcx
imulq $0xe38, %rax, %rax      # imm = 0xE38
addq %r15, %rax
imulq $0x1c, %rcx, %rcx
movl $0xb, (%rcx,%rax)
movl 0x8(%r13), %edx
movl %edx, 0x10(%rcx,%rax)
incq %rbp
movl (%rbx), %edx
jmp 0xb115
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq