// File: FreeSlave[P]vhlt/hlrad/lightmap.cpp
// Function: AddPatchLights(int)
void AddPatchLights (int facenum)
{
	dface_t *f;
#ifndef HLRAD_LOCALTRIANGULATION
	lerpTriangulation_t *trian;
#endif
	facelightlist_t *item;
	dface_t *f_other;
	facelight_t *fl_other;
	int k;
	int i;
	sample_t *samp;

	f = &g_dfaces[facenum];

	if (g_texinfo[f->texinfo].flags & TEX_SPECIAL)
	{
		return;
	}

#ifndef HLRAD_LOCALTRIANGULATION
	trian = CreateTriangulation (facenum);
#endif
	
	for (item = g_dependentfacelights[facenum]; item != NULL; item = item->next)
	{
		f_other = &g_dfaces[item->facenum];
		fl_other = &facelight[item->facenum];
		for (k = 0; k < MAXLIGHTMAPS && f_other->styles[k] != 255; k++)
		{
			for (i = 0; i < fl_other->numsamples; i++)
			{
				samp = &fl_other->samples[k][i];
				if (samp->surface != facenum)
				{ // the sample is not in this surface
					continue;
				}

				{
					vec3_t v;
		#ifdef ZHLT_XASH
					vec3_t v_direction;
		#endif

#ifdef HLRAD_LOCALTRIANGULATION
					int style = f_other->styles[k];
					InterpolateSampleLight (samp->pos, samp->surface, 1, &style, &v
		#ifdef ZHLT_XASH
											, &v_direction
		#endif
											);
#else
					SampleTriangulation (trian, samp->pos, v, 
		#ifdef ZHLT_XASH
										v_direction, 
		#endif
										f_other->styles[k]); //LRC
#endif

	#ifdef HLRAD_STYLE_CORING
					VectorAdd (samp->light, v, v);
		#ifdef ZHLT_XASH
					VectorAdd (samp->light_direction, v_direction, v_direction);
		#endif
					if (VectorMaximum (v) >= g_corings[f_other->styles[k]])
					{
						VectorCopy (v, samp->light);
		#ifdef ZHLT_XASH
						VectorCopy (v_direction, samp->light_direction);
		#endif
					}
					else
					{
		#ifdef HLRAD_AUTOCORING
						if (VectorMaximum (v) > g_maxdiscardedlight + NORMAL_EPSILON)
						{
							ThreadLock ();
							if (VectorMaximum (v) > g_maxdiscardedlight + NORMAL_EPSILON)
							{
								g_maxdiscardedlight = VectorMaximum (v);
								VectorCopy (samp->pos, g_maxdiscardedpos);
							}
							ThreadUnlock ();
						}
		#endif
					}
	#else
					VectorAdd (samp->light, v, samp->light);
	#endif
				}
			} // loop samples
		}
	}

#ifndef HLRAD_LOCALTRIANGULATION
	FreeTriangulation (trian);
#endif
}

// Assembly:
subq $0x98, %rsp
movl %edi, 0x94(%rsp)
movslq 0x94(%rsp), %rcx
leaq 0x1ba5223(%rip), %rax   # 0x1be6410
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rax
movswq 0xa(%rax), %rcx
leaq 0x11a51f0(%rip), %rax   # 0x11e6400
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x24(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x41227
jmp 0x416f6
movslq 0x94(%rsp), %rcx
leaq 0x51e677a(%rip), %rax   # 0x52279b0
movq (%rax,%rcx,8), %rax
movq %rax, 0x80(%rsp)
cmpq $0x0, 0x80(%rsp)
je 0x416f6
movq 0x80(%rsp), %rax
movslq (%rax), %rcx
leaq 0x1ba51ad(%rip), %rax   # 0x1be6410
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x78(%rsp)
movq 0x80(%rsp), %rax
movslq (%rax), %rcx
leaq 0x4f6674f(%rip), %rax   # 0x4fa79d0
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x70(%rsp)
movl $0x0, 0x6c(%rsp)
xorl %eax, %eax
cmpl $0x4, 0x6c(%rsp)
movb %al, 0x4f(%rsp)
jge 0x412bd
movq 0x78(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movzbl 0xc(%rax,%rcx), %eax
cmpl $0xff, %eax
setne %al
movb %al, 0x4f(%rsp)
movb 0x4f(%rsp), %al
testb $0x1, %al
jne 0x412ca
jmp 0x416db
movl $0x0, 0x68(%rsp)
movl 0x68(%rsp), %eax
movq 0x70(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x416c9
movq 0x70(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movq 0x8(%rax,%rcx,8), %rax
movslq 0x68(%rsp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rax
movl 0x18(%rax), %eax
cmpl 0x94(%rsp), %eax
je 0x41319
jmp 0x416b9
movq 0x78(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movzbl 0xc(%rax,%rcx), %eax
movl %eax, 0x50(%rsp)
movq 0x60(%rsp), %rdi
movq 0x60(%rsp), %rax
movl 0x18(%rax), %esi
movl $0x1, %edx
leaq 0x50(%rsp), %rcx
leaq 0x54(%rsp), %r8
callq 0x132f0
movq 0x60(%rsp), %rax
movss 0xc(%rax), %xmm0
addss 0x54(%rsp), %xmm0
movss %xmm0, 0x54(%rsp)
movq 0x60(%rsp), %rax
movss 0x10(%rax), %xmm0
addss 0x58(%rsp), %xmm0
movss %xmm0, 0x58(%rsp)
movq 0x60(%rsp), %rax
movss 0x14(%rax), %xmm0
addss 0x5c(%rsp), %xmm0
movss %xmm0, 0x5c(%rsp)
movss 0x54(%rsp), %xmm0
movss %xmm0, 0x48(%rsp)
movss 0x58(%rsp), %xmm0
ucomiss 0x5c(%rsp), %xmm0
jbe 0x413b6
movss 0x58(%rsp), %xmm0
movss %xmm0, 0x44(%rsp)
jmp 0x413c2
movss 0x5c(%rsp), %xmm0
movss %xmm0, 0x44(%rsp)
movss 0x48(%rsp), %xmm0
movss 0x44(%rsp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x413e1
movss 0x54(%rsp), %xmm0
movss %xmm0, 0x40(%rsp)
jmp 0x41414
movss 0x58(%rsp), %xmm0
ucomiss 0x5c(%rsp), %xmm0
jbe 0x413fc
movss 0x58(%rsp), %xmm0
movss %xmm0, 0x3c(%rsp)
jmp 0x41408
movss 0x5c(%rsp), %xmm0
movss %xmm0, 0x3c(%rsp)
movss 0x3c(%rsp), %xmm0
movss %xmm0, 0x40(%rsp)
movss 0x40(%rsp), %xmm0
movq 0x78(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movzbl 0xc(%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x54ee64e(%rip), %rax   # 0x552fa80
ucomiss (%rax,%rcx,4), %xmm0
jb 0x4146d
movss 0x54(%rsp), %xmm0
movq 0x60(%rsp), %rax
movss %xmm0, 0xc(%rax)
movss 0x58(%rsp), %xmm0
movq 0x60(%rsp), %rax
movss %xmm0, 0x10(%rax)
movss 0x5c(%rsp), %xmm0
movq 0x60(%rsp), %rax
movss %xmm0, 0x14(%rax)
jmp 0x416b7
movss 0x54(%rsp), %xmm0
movss %xmm0, 0x38(%rsp)
movss 0x58(%rsp), %xmm0
ucomiss 0x5c(%rsp), %xmm0
jbe 0x41494
movss 0x58(%rsp), %xmm0
movss %xmm0, 0x34(%rsp)
jmp 0x414a0
movss 0x5c(%rsp), %xmm0
movss %xmm0, 0x34(%rsp)
movss 0x38(%rsp), %xmm0
movss 0x34(%rsp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x414bf
movss 0x54(%rsp), %xmm0
movss %xmm0, 0x30(%rsp)
jmp 0x414f2
movss 0x58(%rsp), %xmm0
ucomiss 0x5c(%rsp), %xmm0
jbe 0x414da
movss 0x58(%rsp), %xmm0
movss %xmm0, 0x2c(%rsp)
jmp 0x414e6
movss 0x5c(%rsp), %xmm0
movss %xmm0, 0x2c(%rsp)
movss 0x2c(%rsp), %xmm0
movss %xmm0, 0x30(%rsp)
movss 0x30(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x552e66d(%rip), %rax   # 0x556fb70
movss (%rax), %xmm1
cvtss2sd %xmm1, %xmm1
movsd 0x28b55(%rip), %xmm2    # 0x6a068
addsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x416b5
callq 0xd220
movss 0x54(%rsp), %xmm0
movss %xmm0, 0x28(%rsp)
movss 0x58(%rsp), %xmm0
ucomiss 0x5c(%rsp), %xmm0
jbe 0x4154d
movss 0x58(%rsp), %xmm0
movss %xmm0, 0x24(%rsp)
jmp 0x41559
movss 0x5c(%rsp), %xmm0
movss %xmm0, 0x24(%rsp)
movss 0x28(%rsp), %xmm0
movss 0x24(%rsp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x41578
movss 0x54(%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
jmp 0x415ab
movss 0x58(%rsp), %xmm0
ucomiss 0x5c(%rsp), %xmm0
jbe 0x41593
movss 0x58(%rsp), %xmm0
movss %xmm0, 0x1c(%rsp)
jmp 0x4159f
movss 0x5c(%rsp), %xmm0
movss %xmm0, 0x1c(%rsp)
movss 0x1c(%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
movss 0x20(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x552e5b4(%rip), %rax   # 0x556fb70
movss (%rax), %xmm1
cvtss2sd %xmm1, %xmm1
movsd 0x28a9c(%rip), %xmm2    # 0x6a068
addsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x416b0
movss 0x54(%rsp), %xmm0
movss %xmm0, 0x18(%rsp)
movss 0x58(%rsp), %xmm0
ucomiss 0x5c(%rsp), %xmm0
jbe 0x41601
movss 0x58(%rsp), %xmm0
movss %xmm0, 0x14(%rsp)
jmp 0x4160d
movss 0x5c(%rsp), %xmm0
movss %xmm0, 0x14(%rsp)
movss 0x18(%rsp), %xmm0
movss 0x14(%rsp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x4162c
movss 0x54(%rsp), %xmm0
movss %xmm0, 0x10(%rsp)
jmp 0x4165f
movss 0x58(%rsp), %xmm0
ucomiss 0x5c(%rsp), %xmm0
jbe 0x41647
movss 0x58(%rsp), %xmm0
movss %xmm0, 0xc(%rsp)
jmp 0x41653
movss 0x5c(%rsp), %xmm0
movss %xmm0, 0xc(%rsp)
movss 0xc(%rsp), %xmm0
movss %xmm0, 0x10(%rsp)
movss 0x10(%rsp), %xmm0
leaq 0x552e504(%rip), %rax   # 0x556fb70
movss %xmm0, (%rax)
movq 0x60(%rsp), %rax
movss (%rax), %xmm0
leaq 0x552e4f4(%rip), %rax   # 0x556fb74
movss %xmm0, (%rax)
movq 0x60(%rsp), %rax
movss 0x4(%rax), %xmm0
leaq 0x552e4df(%rip), %rax   # 0x556fb74
movss %xmm0, 0x4(%rax)
movq 0x60(%rsp), %rax
movss 0x8(%rax), %xmm0
leaq 0x552e4c9(%rip), %rax   # 0x556fb74
movss %xmm0, 0x8(%rax)
callq 0xd240
jmp 0x416b7
jmp 0x416b9
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0x412d2
jmp 0x416cb
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x41295
jmp 0x416dd
movq 0x80(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x80(%rsp)
jmp 0x41242
addq $0x98, %rsp
retq
nop