// File: /workspace/github_cpp_makefile_star2_O1/FreeSlave[P]vhlt/hlbsp/outside.cpp
// Function: FillOutside(node_s*, bool, unsigned int)
node_t*         FillOutside(node_t* node, const bool leakfile, const unsigned hullnum)
{
    int             s;
    int             i;
    bool            inside;
    bool            ret;
    vec3_t          origin;
    const char*     cl;

    Verbose("----- FillOutside ----\n");

    if (g_nofill)
    {
        Log("skipped\n");
        return node;
    }
#ifdef HLBSP_REMOVEHULL2
	if (hullnum == 2 && g_nohull2)
		return node;
#endif

    //
    // place markers for all entities so
    // we know if we leak inside
    //
    inside = false;
    for (i = 1; i < g_numentities; i++)
    {
        GetVectorForKey(&g_entities[i], "origin", origin);
        cl = ValueForKey(&g_entities[i], "classname");
        if (!isClassnameAllowableOutside(cl))
        {
            /*if (!VectorCompare(origin, vec3_origin))
			*/ if (*ValueForKey(&g_entities[i], "origin")) //--vluzacn
            {
                origin[2] += 1;                            // so objects on floor are ok

                // nudge playerstart around if needed so clipping hulls allways
                // have a vlaid point
                if (!strcmp(cl, "info_player_start"))
                {
                    int             x, y;

                    for (x = -16; x <= 16; x += 16)
                    {
                        for (y = -16; y <= 16; y += 16)
                        {
                            origin[0] += x;
                            origin[1] += y;
                            if (PlaceOccupant(i, origin, node))
                            {
                                inside = true;
                                goto gotit;
                            }
                            origin[0] -= x;
                            origin[1] -= y;
                        }
                    }
                  gotit:;
                }
                else
                {
                    if (PlaceOccupant(i, origin, node))
                        inside = true;
                }
            }
        }
    }

    if (!inside)
    {
        Warning("No entities exist in hull %i, no filling performed for this hull", hullnum);
        return node;
    }

	if(!g_outside_node.portals)
	{
		Warning("No outside node portal found in hull %i, no filling performed for this hull",hullnum);
		return node;
	}

    s = !(g_outside_node.portals->nodes[1] == &g_outside_node);

    // first check to see if an occupied leaf is hit
    outleafs = 0;
    valid++;

    prevleaknode = NULL;

    if (leakfile)
    {
        pointfile = fopen(g_pointfilename, "w");
        if (!pointfile)
        {
            Error("Couldn't open pointfile %s\n", g_pointfilename);
        }

        linefile = fopen(g_linefilename, "w");
        if (!linefile)
        {
            Error("Couldn't open linefile %s\n", g_linefilename);
        }
    }

    ret = RecursiveFillOutside(g_outside_node.portals->nodes[s], false);

    if (leakfile)
    {
        fclose(pointfile);
        fclose(linefile);
    }

    if (ret)
    {
        GetVectorForKey(&g_entities[hit_occupied], "origin", origin);


        {
            Warning("=== LEAK in hull %i ===\nEntity %s @ (%4.0f,%4.0f,%4.0f)",
                 hullnum, ValueForKey(&g_entities[hit_occupied], "classname"), origin[0], origin[1], origin[2]);
            PrintOnce(
                "\n  A LEAK is a hole in the map, where the inside of it is exposed to the\n"
                "(unwanted) outside region.  The entity listed in the error is just a helpful\n"
                "indication of where the beginning of the leak pointfile starts, so the\n"
                "beginning of the line can be quickly found and traced to until reaching the\n"
                "outside. Unless this entity is accidentally on the outside of the map, it\n"
                "probably should not be deleted.  Some complex rotating objects entities need\n"
                "their origins outside the map.  To deal with these, just enclose the origin\n"
                "brush with a solid world brush\n");
        }

        if (!g_bLeaked)
        {
            // First leak spits this out
            Log("Leak pointfile generated\n\n");
        }

        if (g_bLeakOnly)
        {
            Error("Stopped by leak.");
        }

        g_bLeaked = true;
            
        return node;
    }
#ifdef HLBSP_DELETELEAKFILE
	if (leakfile && !ret)
	{
		unlink(g_linefilename);
		unlink(g_pointfilename);
	}
#endif

    // now go back and fill things in
    valid++;
    RecursiveFillOutside(g_outside_node.portals->nodes[s], true);

    // remove faces and nodes from filled in leafs  
    c_falsenodes = 0;
    c_free_faces = 0;
    c_keep_faces = 0;
    node = ClearOutFaces_r(node);

    Verbose("%5i outleafs\n", outleafs);
    Verbose("%5i freed faces\n", c_free_faces);
    Verbose("%5i keep faces\n", c_keep_faces);
    Verbose("%5i falsenodes\n", c_falsenodes);

    // save portal file for vis tracing
    if ((hullnum == 0) && leakfile)
    {
        WritePortalfile(node);
    }

    return node;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %edx, %r13d
movl %esi, %ebp
movq %rdi, %rbx
leaq 0xed79(%rip), %rdi      # 0x21ed7
xorl %eax, %eax
callq 0xa3d7
leaq 0x20c18a6(%rip), %rax   # 0x20d4a12
cmpb $0x1, (%rax)
jne 0x13184
leaq 0xed77(%rip), %rdi      # 0x21eef
xorl %eax, %eax
callq 0x9c96
jmp 0x134d3
cmpl $0x2, %r13d
jne 0x1319a
leaq 0x20c1889(%rip), %rax   # 0x20d4a1a
cmpb $0x0, (%rax)
jne 0x134d3
leaq 0x200f253(%rip), %rax   # 0x20223f4
cmpl $0x2, (%rax)
jl 0x134c2
movl %ebp, 0x4(%rsp)
movl %r13d, 0xc(%rsp)
movl $0x1, %r13d
movl $0x0, 0x8(%rsp)
leaq 0xb51e(%rip), %r15      # 0x1e6e6
leaq 0x10(%rsp), %r12
leaq (,%r13,4), %rbp
addq %r13, %rbp
shlq $0x3, %rbp
addq 0x17df5(%rip), %rbp     # 0x2afd8
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x80ad
movq %rbp, %rdi
leaq 0xb3fe(%rip), %rsi      # 0x1e5f9
callq 0x74c9
movq %rax, %r14
movq %rax, %rdi
callq 0x12f68
testb %al, %al
jne 0x13267
movq %rbp, %rdi
movq %r15, %rsi
callq 0x74c9
cmpb $0x0, (%rax)
je 0x13267
movsd 0x20(%rsp), %xmm0
addsd 0xdeab(%rip), %xmm0     # 0x210d8
movsd %xmm0, 0x20(%rsp)
movq %r14, %rdi
leaq 0xecbb(%rip), %rsi      # 0x21ef8
callq 0x4420
testl %eax, %eax
je 0x13282
movq %rbx, %rdi
movq %r12, %rsi
callq 0x139e2
cmpl $-0x2, 0xa0(%rax)
je 0x13267
movl %r13d, 0xc0(%rax)
movb $0x1, %al
movl %eax, 0x8(%rsp)
incq %r13
leaq 0x200f183(%rip), %rax   # 0x20223f4
movslq (%rax), %rax
cmpq %rax, %r13
jl 0x131cd
jmp 0x1330f
movl $0xfffffff0, %r14d      # imm = 0xFFFFFFF0
movapd 0x10(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
movapd %xmm0, 0x30(%rsp)
movl $0xffffffe0, %ebp       # imm = 0xFFFFFFE0
leal 0x10(%rbp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movapd 0x30(%rsp), %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
movapd %xmm2, 0x50(%rsp)
addpd %xmm2, %xmm1
movapd %xmm1, 0x40(%rsp)
movapd %xmm1, 0x10(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x139e2
cmpl $-0x2, 0xa0(%rax)
jne 0x1325a
movapd 0x40(%rsp), %xmm0
subpd 0x50(%rsp), %xmm0
movapd %xmm0, 0x10(%rsp)
addl $0x10, %ebp
movapd %xmm0, %xmm1
jle 0x132a1
leal 0x10(%r14), %eax
testl %r14d, %r14d
movl %eax, %r14d
jle 0x1328e
jmp 0x13267
testb $0x1, 0x8(%rsp)
movl 0xc(%rsp), %r13d
movl 0x4(%rsp), %ecx
je 0x134c2
leaq 0x20bc5f6(%rip), %r14   # 0x20cf920
movq 0xb0(%r14), %rax
testq %rax, %rax
je 0x134e5
movq 0x50(%rax), %r15
incl 0x20bc5a8(%rip)         # 0x20cf8ec
movl $0x0, 0x20bc59a(%rip)   # 0x20cf8e8
movq $0x0, 0x20bc597(%rip) # 0x20cf8f0
testb %cl, %cl
je 0x133c5
leaq 0x20bd6ac(%rip), %rdi   # 0x20d0a10
leaq 0xfe1f(%rip), %rsi      # 0x2318a
callq 0x4370
movq %rax, 0x20bc581(%rip)   # 0x20cf8f8
testq %rax, %rax
jne 0x13391
leaq 0xec14(%rip), %rdi      # 0x21f97
leaq 0x20bd686(%rip), %rsi   # 0x20d0a10
xorl %eax, %eax
callq 0x9f84
leaq 0x20be678(%rip), %rdi   # 0x20d1a10
leaq 0xfdeb(%rip), %rsi      # 0x2318a
callq 0x4370
movq %rax, 0x20bc555(%rip)   # 0x20cf900
testq %rax, %rax
jne 0x133c5
leaq 0xebfc(%rip), %rdi      # 0x21fb3
leaq 0x20be652(%rip), %rsi   # 0x20d1a10
xorl %eax, %eax
callq 0x9f84
xorl %r12d, %r12d
cmpq %r14, %r15
setne %r12b
movq 0xb0(%r14), %rax
movq 0x48(%rax,%r12,8), %rdi
xorl %esi, %esi
callq 0x135b6
movl %eax, %ebp
movl 0x4(%rsp), %r15d
testb %r15b, %r15b
je 0x13406
movq 0x20bc503(%rip), %rdi   # 0x20cf8f8
callq 0x42e0
movq 0x20bc4ff(%rip), %rdi   # 0x20cf900
callq 0x42e0
testb %bpl, %bpl
je 0x134ee
movslq 0x20bc4f2(%rip), %rax   # 0x20cf908
leaq (%rax,%rax,4), %rax
leaq 0x200efdf(%rip), %r15   # 0x2022400
leaq (%r15,%rax,8), %rdi
leaq 0xb2ba(%rip), %rsi      # 0x1e6e6
leaq 0x10(%rsp), %r14
movq %r14, %rdx
callq 0x80ad
movslq 0x20bc4c8(%rip), %rax   # 0x20cf908
leaq (%rax,%rax,4), %rax
leaq (%r15,%rax,8), %rdi
leaq 0xb1aa(%rip), %rsi      # 0x1e5f9
callq 0x74c9
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
movsd 0x10(%r14), %xmm2
leaq 0xeb62(%rip), %rdi      # 0x21fce
movl %r13d, %esi
movq %rax, %rdx
movb $0x3, %al
callq 0xa2f6
leaq 0xeb86(%rip), %rdi      # 0x22006
xorl %eax, %eax
callq 0xa1f4
leaq 0x20c158b(%rip), %r14   # 0x20d4a19
cmpb $0x0, (%r14)
jne 0x134a2
leaq 0xed97(%rip), %rdi      # 0x22232
xorl %eax, %eax
callq 0x9c96
leaq 0x20c156f(%rip), %rax   # 0x20d4a18
cmpb $0x1, (%rax)
jne 0x134bc
leaq 0xed98(%rip), %rdi      # 0x2224d
xorl %eax, %eax
callq 0x9f84
movb $0x1, (%r14)
jmp 0x134d3
leaq 0xea41(%rip), %rdi      # 0x21f0a
movl %r13d, %esi
xorl %eax, %eax
callq 0xa2f6
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xea5f(%rip), %rdi      # 0x21f4b
jmp 0x134c9
testb %r15b, %r15b
je 0x1350b
leaq 0x20be516(%rip), %rdi   # 0x20d1a10
callq 0x4150
leaq 0x20bd50a(%rip), %rdi   # 0x20d0a10
callq 0x4150
incl 0x20bc3db(%rip)         # 0x20cf8ec
movq 0xb0(%r14), %rax
movq 0x48(%rax,%r12,8), %rdi
movl $0x1, %esi
callq 0x135b6
xorl %eax, %eax
movl %eax, 0x20bc3dd(%rip)   # 0x20cf90c
movl %eax, 0x20bc3db(%rip)   # 0x20cf910
movl %eax, 0x20bc3d9(%rip)   # 0x20cf914
movq %rbx, %rdi
callq 0x13691
movq %rax, %rbx
movl 0x20bc39c(%rip), %esi   # 0x20cf8e8
leaq 0xed0b(%rip), %rdi      # 0x2225e
xorl %eax, %eax
callq 0xa3d7
movl 0x20bc3b0(%rip), %esi   # 0x20cf910
leaq 0xed05(%rip), %rdi      # 0x2226c
xorl %eax, %eax
callq 0xa3d7
movl 0x20bc3a0(%rip), %esi   # 0x20cf914
leaq 0xed02(%rip), %rdi      # 0x2227d
xorl %eax, %eax
callq 0xa3d7
movl 0x20bc384(%rip), %esi   # 0x20cf90c
leaq 0xecfe(%rip), %rdi      # 0x2228d
xorl %eax, %eax
callq 0xa3d7
testl %r13d, %r13d
setne %al
xorb $0x1, %r15b
orb %al, %r15b
jne 0x134d3
movq %rbx, %rdi
callq 0x1403f
jmp 0x134d3