// File: 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:
subq $0x68, %rsp
movb %sil, %al
movq %rdi, 0x58(%rsp)
andb $0x1, %al
movb %al, 0x57(%rsp)
movl %edx, 0x50(%rsp)
leaq 0x187b2(%rip), %rdi     # 0x3a06f
movb $0x0, %al
callq 0xba90
leaq 0x20ce157(%rip), %rax   # 0x20efa22
testb $0x1, (%rax)
je 0x218ed
leaq 0x187b0(%rip), %rdi     # 0x3a087
movb $0x0, %al
callq 0xb060
movq 0x58(%rsp), %rax
movq %rax, 0x60(%rsp)
jmp 0x21e07
cmpl $0x2, 0x50(%rsp)
jne 0x2190f
leaq 0x20ce12f(%rip), %rax   # 0x20efa2a
testb $0x1, (%rax)
je 0x2190f
movq 0x58(%rsp), %rax
movq %rax, 0x60(%rsp)
jmp 0x21e07
movb $0x0, 0x47(%rsp)
movl $0x1, 0x48(%rsp)
movl 0x48(%rsp), %eax
leaq 0x201bacd(%rip), %rcx   # 0x203d3f4
cmpl (%rcx), %eax
jge 0x21ad1
movslq 0x48(%rsp), %rax
leaq 0x201bac5(%rip), %rdi   # 0x203d400
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq 0x20(%rsp), %rdx
leaq 0x14ce4(%rip), %rsi     # 0x36632
callq 0x8ba0
movslq 0x48(%rsp), %rax
leaq 0x201baa1(%rip), %rdi   # 0x203d400
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq 0x14bd8(%rip), %rsi     # 0x36545
callq 0x7cd0
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
callq 0x21610
testb $0x1, %al
jne 0x21abf
movslq 0x48(%rsp), %rax
leaq 0x201ba6b(%rip), %rdi   # 0x203d400
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq 0x14c8f(%rip), %rsi     # 0x36632
callq 0x7cd0
cmpb $0x0, (%rax)
je 0x21abd
movsd 0x1778f(%rip), %xmm0    # 0x39148
addsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x18(%rsp), %rdi
leaq 0x186bf(%rip), %rsi     # 0x3a090
callq 0x4400
cmpl $0x0, %eax
jne 0x21a9b
movl $0xfffffff0, 0x14(%rsp) # imm = 0xFFFFFFF0
cmpl $0x10, 0x14(%rsp)
jg 0x21a97
movl $0xfffffff0, 0x10(%rsp) # imm = 0xFFFFFFF0
cmpl $0x10, 0x10(%rsp)
jg 0x21a85
cvtsi2sdl 0x14(%rsp), %xmm0
addsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
cvtsi2sdl 0x10(%rsp), %xmm0
addsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movl 0x48(%rsp), %edi
leaq 0x20(%rsp), %rsi
movq 0x58(%rsp), %rdx
callq 0x21e20
testb $0x1, %al
jne 0x21a42
jmp 0x21a49
movb $0x1, 0x47(%rsp)
jmp 0x21a99
cvtsi2sdl 0x14(%rsp), %xmm1
movsd 0x20(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
cvtsi2sdl 0x10(%rsp), %xmm1
movsd 0x28(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
movl 0x10(%rsp), %eax
addl $0x10, %eax
movl %eax, 0x10(%rsp)
jmp 0x219fa
jmp 0x21a87
movl 0x14(%rsp), %eax
addl $0x10, %eax
movl %eax, 0x14(%rsp)
jmp 0x219e7
jmp 0x21a99
jmp 0x21abb
movl 0x48(%rsp), %edi
leaq 0x20(%rsp), %rsi
movq 0x58(%rsp), %rdx
callq 0x21e20
testb $0x1, %al
jne 0x21ab4
jmp 0x21ab9
movb $0x1, 0x47(%rsp)
jmp 0x21abb
jmp 0x21abd
jmp 0x21abf
jmp 0x21ac1
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x2191c
testb $0x1, 0x47(%rsp)
jne 0x21af9
movl 0x50(%rsp), %esi
leaq 0x185bf(%rip), %rdi     # 0x3a0a2
movb $0x0, %al
callq 0xb980
movq 0x58(%rsp), %rax
movq %rax, 0x60(%rsp)
jmp 0x21e07
leaq 0x20c8e30(%rip), %rax   # 0x20ea930
cmpq $0x0, 0xb0(%rax)
jne 0x21b2b
movl 0x50(%rsp), %esi
leaq 0x185ce(%rip), %rdi     # 0x3a0e3
movb $0x0, %al
callq 0xb980
movq 0x58(%rsp), %rax
movq %rax, 0x60(%rsp)
jmp 0x21e07
leaq 0x20c8dfe(%rip), %rax   # 0x20ea930
movq 0xb0(%rax), %rax
leaq 0x20c8df0(%rip), %rcx   # 0x20ea930
cmpq %rcx, 0x50(%rax)
sete %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x4c(%rsp)
movl $0x0, 0x20c8d9c(%rip)   # 0x20ea8f8
movl 0x20c8d9a(%rip), %eax   # 0x20ea8fc
addl $0x1, %eax
movl %eax, 0x20c8d91(%rip)   # 0x20ea8fc
movq $0x0, 0x20c8d8a(%rip) # 0x20ea900
testb $0x1, 0x57(%rsp)
je 0x21bf1
leaq 0x20c9e9c(%rip), %rdi   # 0x20eba20
leaq 0x1975f(%rip), %rsi     # 0x3b2ea
callq 0x4360
movq %rax, 0x20c8d71(%rip)   # 0x20ea908
cmpq $0x0, 0x20c8d69(%rip)   # 0x20ea908
jne 0x21bb6
leaq 0x18587(%rip), %rdi     # 0x3a12f
leaq 0x20c9e71(%rip), %rsi   # 0x20eba20
movb $0x0, %al
callq 0xb510
leaq 0x20cae63(%rip), %rdi   # 0x20eca20
leaq 0x19726(%rip), %rsi     # 0x3b2ea
callq 0x4360
movq %rax, 0x20c8d40(%rip)   # 0x20ea910
cmpq $0x0, 0x20c8d38(%rip)   # 0x20ea910
jne 0x21bef
leaq 0x1856a(%rip), %rdi     # 0x3a14b
leaq 0x20cae38(%rip), %rsi   # 0x20eca20
movb $0x0, %al
callq 0xb510
jmp 0x21bf1
leaq 0x20c8d38(%rip), %rax   # 0x20ea930
movq 0xb0(%rax), %rax
movslq 0x4c(%rsp), %rcx
movq 0x48(%rax,%rcx,8), %rdi
xorl %esi, %esi
callq 0x21e80
andb $0x1, %al
movb %al, 0x46(%rsp)
testb $0x1, 0x57(%rsp)
je 0x21c35
movq 0x20c8ce4(%rip), %rdi   # 0x20ea908
callq 0x42e0
movq 0x20c8ce0(%rip), %rdi   # 0x20ea910
callq 0x42e0
testb $0x1, 0x46(%rsp)
je 0x21d11
movslq 0x20c8cd1(%rip), %rax   # 0x20ea918
leaq 0x201b7b2(%rip), %rdi   # 0x203d400
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq 0x20(%rsp), %rdx
leaq 0x149d1(%rip), %rsi     # 0x36632
callq 0x8ba0
movl 0x50(%rsp), %eax
movl %eax, 0xc(%rsp)
movslq 0x20c8ca3(%rip), %rax   # 0x20ea918
leaq 0x201b784(%rip), %rdi   # 0x203d400
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq 0x148bb(%rip), %rsi     # 0x36545
callq 0x7cd0
movl 0xc(%rsp), %esi
movq %rax, %rdx
movsd 0x20(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
leaq 0x184b7(%rip), %rdi     # 0x3a166
movb $0x3, %al
callq 0xb980
leaq 0x184e1(%rip), %rdi     # 0x3a19e
movb $0x0, %al
callq 0xb840
leaq 0x20cdd5e(%rip), %rax   # 0x20efa29
testb $0x1, (%rax)
jne 0x21cde
leaq 0x186f3(%rip), %rdi     # 0x3a3ca
movb $0x0, %al
callq 0xb060
leaq 0x20cdd43(%rip), %rax   # 0x20efa28
testb $0x1, (%rax)
je 0x21cf8
leaq 0x186f4(%rip), %rdi     # 0x3a3e5
movb $0x0, %al
callq 0xb510
leaq 0x20cdd2a(%rip), %rax   # 0x20efa29
movb $0x1, (%rax)
movq 0x58(%rsp), %rax
movq %rax, 0x60(%rsp)
jmp 0x21e07
testb $0x1, 0x57(%rsp)
je 0x21d37
testb $0x1, 0x46(%rsp)
jne 0x21d37
leaq 0x20cacfa(%rip), %rdi   # 0x20eca20
callq 0x4150
leaq 0x20c9cee(%rip), %rdi   # 0x20eba20
callq 0x4150
movl 0x20c8bbf(%rip), %eax   # 0x20ea8fc
addl $0x1, %eax
movl %eax, 0x20c8bb6(%rip)   # 0x20ea8fc
leaq 0x20c8be3(%rip), %rax   # 0x20ea930
movq 0xb0(%rax), %rax
movslq 0x4c(%rsp), %rcx
movq 0x48(%rax,%rcx,8), %rdi
movl $0x1, %esi
callq 0x21e80
movl $0x0, 0x20c8baa(%rip)   # 0x20ea91c
movl $0x0, 0x20c8ba4(%rip)   # 0x20ea920
movl $0x0, 0x20c8b9e(%rip)   # 0x20ea924
movq 0x58(%rsp), %rdi
callq 0x21ff0
movq %rax, 0x58(%rsp)
movl 0x20c8b5d(%rip), %esi   # 0x20ea8f8
leaq 0x18654(%rip), %rdi     # 0x3a3f6
movb $0x0, %al
callq 0xba90
movl 0x20c8b71(%rip), %esi   # 0x20ea920
leaq 0x1864e(%rip), %rdi     # 0x3a404
movb $0x0, %al
callq 0xba90
movl 0x20c8b61(%rip), %esi   # 0x20ea924
leaq 0x1864b(%rip), %rdi     # 0x3a415
movb $0x0, %al
callq 0xba90
movl 0x20c8b45(%rip), %esi   # 0x20ea91c
leaq 0x18647(%rip), %rdi     # 0x3a425
movb $0x0, %al
callq 0xba90
cmpl $0x0, 0x50(%rsp)
jne 0x21dfd
testb $0x1, 0x57(%rsp)
je 0x21dfd
movq 0x58(%rsp), %rdi
callq 0x22f90
movq 0x58(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rax
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)