// File: FreeSlave[P]vhlt/hlrad/qrad.cpp
// Function: MakePatches()
static void     MakePatches()
{
    int             i;
    int             j;
    unsigned int    k;
    dface_t*        f;
    int             fn;
    Winding*        w;
    dmodel_t*       mod;
    vec3_t          origin;
    entity_t*       ent;
    const char*     s;
    vec3_t          light_origin;
    vec3_t          model_center;
    bool            b_light_origin;
    bool            b_model_center;
    eModelLightmodes lightmode;

#ifdef ZHLT_TEXLIGHT
    int				style; //LRC
#endif

#ifndef HLRAD_OPAQUE_NODE
#ifdef HLRAD_HULLU
    vec3_t		d_transparency;
    bool		b_transparency;
#endif
#endif

    Log("%i faces\n", g_numfaces);

    Log("Create Patches : ");
#ifdef HLRAD_MORE_PATCHES
	g_patches = (patch_t *)AllocBlock (MAX_PATCHES * sizeof (patch_t));
#endif

    for (i = 0; i < g_nummodels; i++)
    {
        b_light_origin = false;
        b_model_center = false;
        lightmode = eModelLightmodeNull;

#ifndef HLRAD_OPAQUE_NODE
#ifdef HLRAD_OPACITY // AJM
        float         l_opacity = 0.0f; // decimal percentage 
#endif
#endif

        mod = g_dmodels + i;
        ent = EntityForModel(i);
        VectorCopy(vec3_origin, origin);

        if (*(s = ValueForKey(ent, "zhlt_lightflags")))
        {
            lightmode = (eModelLightmodes)atoi(s);
        }

        // models with origin brushes need to be offset into their in-use position
        if (*(s = ValueForKey(ent, "origin")))
        {
            double          v1, v2, v3;

            if (sscanf(s, "%lf %lf %lf", &v1, &v2, &v3) == 3)
            {
                origin[0] = v1;
                origin[1] = v2;
                origin[2] = v3;
            }

        }

        // Allow models to be lit in an alternate location (pt1)
        if (*(s = ValueForKey(ent, "light_origin")))
        {
            entity_t*       e = FindTargetEntity(s);

            if (e)
            {
                if (*(s = ValueForKey(e, "origin")))
                {
                    double          v1, v2, v3;

                    if (sscanf(s, "%lf %lf %lf", &v1, &v2, &v3) == 3)
                    {
                        light_origin[0] = v1;
                        light_origin[1] = v2;
                        light_origin[2] = v3;

                        b_light_origin = true;
                    }
                }
            }
        }

        // Allow models to be lit in an alternate location (pt2)
        if (*(s = ValueForKey(ent, "model_center")))
        {
            double          v1, v2, v3;

            if (sscanf(s, "%lf %lf %lf", &v1, &v2, &v3) == 3)
            {
                model_center[0] = v1;
                model_center[1] = v2;
                model_center[2] = v3;

                b_model_center = true;
            }
        }

#ifndef HLRAD_OPAQUE_NODE
#ifdef HLRAD_HULLU
	// Check for colored transparency/custom shadows
        VectorFill(d_transparency, 1.0);
        b_transparency = false;
        
        if (*(s = ValueForKey(ent, "zhlt_customshadow")))
        {
        	double r1 = 1.0, g1 = 1.0, b1 = 1.0, tmp = 1.0;
        	if (sscanf(s, "%lf %lf %lf", &r1, &g1, &b1) == 3) //RGB version
        	{
        		if(r1<0.0) r1 = 0.0;
        		if(g1<0.0) g1 = 0.0;
        		if(b1<0.0) b1 = 0.0;
        		
        		d_transparency[0] = r1;
        		d_transparency[1] = g1;
        		d_transparency[2] = b1;
        		b_transparency = true;
        	}
        	else if (sscanf(s, "%lf", &tmp) == 1) //Greyscale version
        	{
        		if(tmp<0.0) tmp = 0.0;
        		
        		VectorFill(d_transparency, tmp);
        		b_transparency = true;
        	}
        }
#endif
#endif
        // Allow models to be lit in an alternate location (pt3)
        if (b_light_origin && b_model_center)
        {
            VectorSubtract(light_origin, model_center, origin);
        }

#ifdef ZHLT_TEXLIGHT        
		//LRC:
		if (*(s = ValueForKey(ent, "style")))
		{
			style = atoi(s);
			if (style < 0)
				style = -style;
		}
		else
		{
			style = 0;
		}
        //LRC (ends)
	#ifdef HLRAD_STYLE_CORING
		style = (unsigned char)style;
		if (style >= ALLSTYLES)
		{
			Error ("invalid light style: style (%d) >= ALLSTYLES (%d)", style, ALLSTYLES);
		}
	#endif
#endif
#ifndef HLRAD_OPAQUE_NODE
#ifdef HLRAD_OPAQUE_STYLE
		int opaquestyle = -1;
		for (j = 0; j < g_numentities; j++)
		{
			entity_t *lightent = &g_entities[j];
			if (!strcmp (ValueForKey (lightent, "classname"), "light_shadow")
				&& *ValueForKey (lightent, "target")
				&& !strcmp (ValueForKey (lightent, "target"), ValueForKey (ent, "targetname")))
			{
				opaquestyle = IntForKey (lightent, "style");
	#ifdef ZHLT_TEXLIGHT
				if (opaquestyle < 0)
					opaquestyle = -opaquestyle;
	#endif
	#ifdef HLRAD_STYLE_CORING
				opaquestyle = (unsigned char)opaquestyle;
				if (opaquestyle >= ALLSTYLES)
				{
					Error ("invalid light style: style (%d) >= ALLSTYLES (%d)", opaquestyle, ALLSTYLES);
				}
	#endif
				break;
			}
		}
#endif
#endif
#ifdef HLRAD_BOUNCE_STYLE
		int bouncestyle = -1;
		{
			int j;
			for (j = 0; j < g_numentities; j++)
			{
				entity_t *lightent = &g_entities[j];
				if (!strcmp (ValueForKey (lightent, "classname"), "light_bounce")
					&& *ValueForKey (lightent, "target")
					&& !strcmp (ValueForKey (lightent, "target"), ValueForKey (ent, "targetname")))
				{
					bouncestyle = IntForKey (lightent, "style");
	#ifdef ZHLT_TEXLIGHT
					if (bouncestyle < 0)
						bouncestyle = -bouncestyle;
	#endif
	#ifdef HLRAD_STYLE_CORING
					bouncestyle = (unsigned char)bouncestyle;
					if (bouncestyle >= ALLSTYLES)
					{
						Error ("invalid light style: style (%d) >= ALLSTYLES (%d)", bouncestyle, ALLSTYLES);
					}
	#endif
					break;
				}
			}
		}
#endif

        for (j = 0; j < mod->numfaces; j++)
        {
            fn = mod->firstface + j;
            g_face_entity[fn] = ent;
            VectorCopy(origin, g_face_offset[fn]);
#ifdef HLRAD_CUSTOMTEXLIGHT
			g_face_texlights[fn] = FindTexlightEntity (fn);
#endif
            g_face_lightmode[fn] = lightmode;
            f = g_dfaces + fn;
            w = new Winding(*f);
            for (k = 0; k < w->m_NumPoints; k++)
            {
                VectorAdd(w->m_Points[k], origin, w->m_Points[k]);
            }
#ifndef HLRAD_OPAQUE_NODE
            if (g_allow_opaques)
            {
                if (lightmode & eModelLightmodeOpaque)
                {
					AddFaceToOpaqueList(fn, w
#ifdef HLRAD_HULLU
						, d_transparency, b_transparency
#endif
#ifdef HLRAD_OPAQUE_GROUP
						, mod
#endif
#ifdef HLRAD_OPAQUE_STYLE
						, opaquestyle
#endif
						); 
                }
            }
#endif
#ifdef ZHLT_TEXLIGHT
            MakePatchForFace(fn, w, style
#ifdef HLRAD_BOUNCE_STYLE
				, bouncestyle
#endif
				); //LRC
#else
            MakePatchForFace(fn, w
#ifdef HLRAD_BOUNCE_STYLE
				, bouncestyle
#endif
				);
#endif
        }
    }

    Log("%i base patches\n", g_num_patches);
#ifndef HLRAD_OPAQUE_NODE
#ifdef HLRAD_OPAQUE_GROUP
	Log("%i opaque models\n", g_opaque_group_count);
#endif
    Log("%i opaque faces\n", g_opaque_face_count);
#endif
    Log("%i square feet [%.2f square inches]\n", (int)(totalarea / 144), totalarea);
}

// Assembly:
subq $0xf8, %rsp
leaq 0x1b91f26(%rip), %rax   # 0x1be6404
movl (%rax), %esi
leaq 0x1d163(%rip), %rdi     # 0x7164a
movb $0x0, %al
callq 0xb0d0
leaq 0x1d15f(%rip), %rdi     # 0x71654
movb $0x0, %al
callq 0xb0d0
movl $0x137fec80, %edi       # imm = 0x137FEC80
callq 0x47a0
movq %rax, 0x53994f3(%rip)   # 0x53eda00
movl $0x0, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
leaq 0x29f4a(%rip), %rcx     # 0x7e470
cmpl (%rcx), %eax
jge 0x54ca2
movb $0x0, 0x8f(%rsp)
movb $0x0, 0x8e(%rsp)
movl $0x0, 0x88(%rsp)
movslq 0xf4(%rsp), %rcx
leaq 0x29f28(%rip), %rax     # 0x7e480
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, 0xc8(%rsp)
movl 0xf4(%rsp), %edi
callq 0x8d50
movq %rax, 0xb0(%rsp)
leaq 0x178ce(%rip), %rax     # 0x6be50
movss (%rax), %xmm0
movss %xmm0, 0xbc(%rsp)
leaq 0x178ba(%rip), %rax     # 0x6be50
movss 0x4(%rax), %xmm0
movss %xmm0, 0xc0(%rsp)
leaq 0x178a5(%rip), %rax     # 0x6be50
movss 0x8(%rax), %xmm0
movss %xmm0, 0xc4(%rsp)
movq 0xb0(%rsp), %rdi
leaq 0x1d000(%rip), %rsi     # 0x715c8
callq 0x7d50
movq %rax, 0xa8(%rsp)
cmpb $0x0, (%rax)
je 0x545ee
movq 0xa8(%rsp), %rdi
callq 0x4520
movl %eax, 0x88(%rsp)
movq 0xb0(%rsp), %rdi
leaq 0x1cfb7(%rip), %rsi     # 0x715b4
callq 0x7d50
movq %rax, 0xa8(%rsp)
cmpb $0x0, (%rax)
je 0x54674
movq 0xa8(%rsp), %rdi
leaq 0x199a2(%rip), %rsi     # 0x6dfc0
leaq 0x78(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0x68(%rsp), %r8
movb $0x0, %al
callq 0x4300
cmpl $0x3, %eax
jne 0x54672
movsd 0x78(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xbc(%rsp)
movsd 0x70(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xc0(%rsp)
movsd 0x68(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xc4(%rsp)
jmp 0x54674
movq 0xb0(%rsp), %rdi
leaq 0x1cf2b(%rip), %rsi     # 0x715ae
callq 0x7d50
movq %rax, 0xa8(%rsp)
cmpb $0x0, (%rax)
je 0x54746
movq 0xa8(%rsp), %rdi
callq 0x8b70
movq %rax, 0x60(%rsp)
cmpq $0x0, 0x60(%rsp)
je 0x54744
movq 0x60(%rsp), %rdi
leaq 0x1cef1(%rip), %rsi     # 0x715b4
callq 0x7d50
movq %rax, 0xa8(%rsp)
cmpb $0x0, (%rax)
je 0x54742
movq 0xa8(%rsp), %rdi
leaq 0x198dc(%rip), %rsi     # 0x6dfc0
leaq 0x58(%rsp), %rdx
leaq 0x50(%rsp), %rcx
leaq 0x48(%rsp), %r8
movb $0x0, %al
callq 0x4300
cmpl $0x3, %eax
jne 0x54740
movsd 0x58(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x9c(%rsp)
movsd 0x50(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xa0(%rsp)
movsd 0x48(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xa4(%rsp)
movb $0x1, 0x8f(%rsp)
jmp 0x54742
jmp 0x54744
jmp 0x54746
movq 0xb0(%rsp), %rdi
leaq 0x1ce66(%rip), %rsi     # 0x715bb
callq 0x7d50
movq %rax, 0xa8(%rsp)
cmpb $0x0, (%rax)
je 0x547d4
movq 0xa8(%rsp), %rdi
leaq 0x1984a(%rip), %rsi     # 0x6dfc0
leaq 0x40(%rsp), %rdx
leaq 0x38(%rsp), %rcx
leaq 0x30(%rsp), %r8
movb $0x0, %al
callq 0x4300
cmpl $0x3, %eax
jne 0x547d2
movsd 0x40(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x90(%rsp)
movsd 0x38(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x94(%rsp)
movsd 0x30(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x98(%rsp)
movb $0x1, 0x8e(%rsp)
jmp 0x547d4
testb $0x1, 0x8f(%rsp)
je 0x54839
testb $0x1, 0x8e(%rsp)
je 0x54839
movss 0x9c(%rsp), %xmm0
subss 0x90(%rsp), %xmm0
movss %xmm0, 0xbc(%rsp)
movss 0xa0(%rsp), %xmm0
subss 0x94(%rsp), %xmm0
movss %xmm0, 0xc0(%rsp)
movss 0xa4(%rsp), %xmm0
subss 0x98(%rsp), %xmm0
movss %xmm0, 0xc4(%rsp)
movq 0xb0(%rsp), %rdi
leaq 0x19725(%rip), %rsi     # 0x6df6d
callq 0x7d50
movq %rax, 0xa8(%rsp)
cmpb $0x0, (%rax)
je 0x5488a
movq 0xa8(%rsp), %rdi
callq 0x4520
movl %eax, 0x84(%rsp)
cmpl $0x0, 0x84(%rsp)
jge 0x54888
xorl %eax, %eax
subl 0x84(%rsp), %eax
movl %eax, 0x84(%rsp)
jmp 0x54895
movl $0x0, 0x84(%rsp)
movl 0x84(%rsp), %eax
movzbl %al, %eax
movl %eax, 0x84(%rsp)
cmpl $0x40, 0x84(%rsp)
jl 0x548ca
movl 0x84(%rsp), %esi
leaq 0x196c6(%rip), %rdi     # 0x6df84
movl $0x40, %edx
movb $0x0, %al
callq 0xb580
movl $0xffffffff, 0x2c(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
leaq 0x201fb4f(%rip), %rcx   # 0x2074434
cmpl (%rcx), %eax
jge 0x549e7
movslq 0x28(%rsp), %rcx
leaq 0x201fb47(%rip), %rax   # 0x2074440
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x15c46(%rip), %rsi     # 0x6a557
callq 0x7d50
movq %rax, %rdi
leaq 0x15cd4(%rip), %rsi     # 0x6a5f4
callq 0x4480
cmpl $0x0, %eax
jne 0x549d5
movq 0x20(%rsp), %rdi
leaq 0x19701(%rip), %rsi     # 0x6e03b
callq 0x7d50
cmpb $0x0, (%rax)
je 0x549d5
movq 0x20(%rsp), %rdi
leaq 0x196e7(%rip), %rsi     # 0x6e03b
callq 0x7d50
movq %rax, 0x8(%rsp)
movq 0xb0(%rsp), %rdi
leaq 0x15d4e(%rip), %rsi     # 0x6a6bb
callq 0x7d50
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x4480
cmpl $0x0, %eax
jne 0x549d5
movq 0x20(%rsp), %rdi
leaq 0x195dd(%rip), %rsi     # 0x6df6d
callq 0x7f70
movl %eax, 0x2c(%rsp)
cmpl $0x0, 0x2c(%rsp)
jge 0x549aa
xorl %eax, %eax
subl 0x2c(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movzbl %al, %eax
movl %eax, 0x2c(%rsp)
cmpl $0x40, 0x2c(%rsp)
jl 0x549d3
movl 0x2c(%rsp), %esi
leaq 0x195bd(%rip), %rdi     # 0x6df84
movl $0x40, %edx
movb $0x0, %al
callq 0xb580
jmp 0x549e7
jmp 0x549d7
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x548da
movl $0x0, 0xf0(%rsp)
movl 0xf0(%rsp), %eax
movq 0xc8(%rsp), %rcx
cmpl 0x3c(%rcx), %eax
jge 0x54c8a
movq 0xc8(%rsp), %rax
movl 0x38(%rax), %eax
movl 0xf0(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0xdc(%rsp)
movq 0xb0(%rsp), %rdx
movslq 0xdc(%rsp), %rcx
leaq 0x52d8fc4(%rip), %rax   # 0x532da00
movq %rdx, (%rax,%rcx,8)
movss 0xbc(%rsp), %xmm0
movslq 0xdc(%rsp), %rax
leaq (%rax,%rax,2), %rcx
leaq 0x5418fb4(%rip), %rax   # 0x546da10
movss %xmm0, (%rax,%rcx,4)
movss 0xc0(%rsp), %xmm0
movslq 0xdc(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
movss %xmm0, 0x4(%rax,%rcx,4)
movss 0xc4(%rsp), %xmm0
movslq 0xdc(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
movss %xmm0, 0x8(%rax,%rcx,4)
movl 0xdc(%rsp), %edi
callq 0x556e0
movq %rax, %rdx
movslq 0xdc(%rsp), %rcx
leaq 0x5398f5b(%rip), %rax   # 0x53eda10
movq %rdx, (%rax,%rcx,8)
movl 0x88(%rsp), %edx
movslq 0xdc(%rsp), %rcx
leaq 0x5358f31(%rip), %rax   # 0x53ada00
movl %edx, (%rax,%rcx,4)
movslq 0xdc(%rsp), %rax
leaq (%rax,%rax,4), %rcx
leaq 0x1b9192b(%rip), %rax   # 0x1be6410
leaq (%rax,%rcx,4), %rax
movq %rax, 0xe0(%rsp)
movl $0x20, %edi
callq 0x4320
movq %rax, %rdi
movq %rdi, %rax
movq %rax, (%rsp)
movq 0xe0(%rsp), %rsi
movss 0x1907b(%rip), %xmm0    # 0x6db90
callq 0xf130
jmp 0x54b1c
movq (%rsp), %rax
movq %rax, 0xd0(%rsp)
movl $0x0, 0xec(%rsp)
movl 0xec(%rsp), %eax
movq 0xd0(%rsp), %rcx
cmpl 0x8(%rcx), %eax
jae 0x54c55
movq 0xd0(%rsp), %rax
movq 0x10(%rax), %rax
movl 0xec(%rsp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
addss 0xbc(%rsp), %xmm0
movq 0xd0(%rsp), %rax
movq 0x10(%rax), %rax
movl 0xec(%rsp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, (%rax)
movq 0xd0(%rsp), %rax
movq 0x10(%rax), %rax
movl 0xec(%rsp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
addss 0xc0(%rsp), %xmm0
movq 0xd0(%rsp), %rax
movq 0x10(%rax), %rax
movl 0xec(%rsp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x4(%rax)
movq 0xd0(%rsp), %rax
movq 0x10(%rax), %rax
movl 0xec(%rsp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
addss 0xc4(%rsp), %xmm0
movq 0xd0(%rsp), %rax
movq 0x10(%rax), %rax
movl 0xec(%rsp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x8(%rax)
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x54b33
movq (%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x18(%rsp)
movl %eax, 0x14(%rsp)
movl $0x20, %esi
callq 0x4330
jmp 0x54cf0
movl 0xdc(%rsp), %edi
movq 0xd0(%rsp), %rsi
movl 0x84(%rsp), %edx
movl 0x2c(%rsp), %ecx
callq 0x55b60
movl 0xf0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf0(%rsp)
jmp 0x549f2
jmp 0x54c8c
movl 0xf4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf4(%rsp)
jmp 0x54518
movl 0x5418d60(%rip), %esi   # 0x546da08
leaq 0x1c9b7(%rip), %rdi     # 0x71666
movb $0x0, %al
callq 0xb0d0
movss 0x551af12(%rip), %xmm0  # 0x556fbd0
movss 0x19f12(%rip), %xmm1    # 0x6ebd8
divss %xmm1, %xmm0
cvttss2si %xmm0, %esi
movss 0x551aefa(%rip), %xmm0  # 0x556fbd0
cvtss2sd %xmm0, %xmm0
leaq 0x1c996(%rip), %rdi     # 0x71677
movb $0x1, %al
callq 0xb0d0
addq $0xf8, %rsp
retq
movq 0x18(%rsp), %rdi
callq 0x45c0
nopw (%rax,%rax)