// File: FreeSlave[P]vhlt/hlvis/zones.cpp
// Function: MakeZones()
Zones* MakeZones(void)
{
    UINT32 x;
    UINT32 func_vis_count = 0;

    ParseEntities();

    // Note: we arent looping through entities because we only care if it has a winding/bounding box

    // First count the number of func_vis's
    for (x=0; x<g_nummodels; x++)
    {
        entity_t*       ent = EntityForModel(x);

        if (!strcasecmp(ValueForKey(ent, "classname"), "func_vis"))
        {
            UINT32 value = atoi(ValueForKey(ent, "node"));
            if (value)
            {
                func_vis_count++;
            }
            else
            {
                Error("func_vis with no \"node\" id\n");
            }
        }
    }

    if (!func_vis_count)
    {
        return NULL;
    }

    Zones* zones = new Zones(func_vis_count);

    for (x=0; x<g_nummodels; x++)
    {
        dmodel_t*       mod = g_dmodels + x;
        entity_t*       ent = EntityForModel(x);

        if (!strcasecmp(ValueForKey(ent, "classname"), "func_vis"))
        {
            UINT32 func_vis_id = atoi(ValueForKey(ent, "node"));

            {
                epair_t* keyvalue;
    
                for (keyvalue = ent->epairs; keyvalue; keyvalue = keyvalue->next)
                {
                    UINT32 other_id = atoi(keyvalue->key);
                    if (other_id)
                    {
                        zones->flag(func_vis_id, other_id);
                    }
                }
            }
    
            {
                UINT32          j;
                BoundingBox     bounds;
                dface_t*        f = g_dfaces + mod->firstface;
            
                for (j = 0; j < mod->numfaces; j++, f++)
                {
                    Winding*        w = WindingFromFace(f);
                    UINT32          k;

                    for (k = 0; k < w->m_NumPoints; k++)
                    {
                        bounds.add(w->m_Points[k]);
                    }
                    delete w;
                }

                zones->set(func_vis_id, bounds);

                Log("Adding zone %u : mins(%4.3f %4.3f %4.3f) maxs(%4.3f %4.3f %4.3f)\n", func_vis_id, 
                    bounds.m_Mins[0],bounds.m_Mins[1],bounds.m_Mins[2],
                    bounds.m_Maxs[0],bounds.m_Maxs[1],bounds.m_Maxs[2]);
            }
        }
    }

    return zones;
}

// Assembly:
subq $0xa8, %rsp
movl $0x0, 0x98(%rsp)
callq 0x6da0
movl $0x0, 0x9c(%rsp)
movl 0x9c(%rsp), %eax
leaq 0xa370(%rip), %rcx      # 0x222b0
cmpl (%rcx), %eax
jae 0x17fec
movl 0x9c(%rsp), %edi
callq 0x7b50
movq %rax, 0x90(%rsp)
movq 0x90(%rsp), %rdi
leaq 0x15f9(%rip), %rsi      # 0x19564
callq 0x6b50
movq %rax, %rdi
leaq 0x53bd(%rip), %rsi      # 0x1d337
callq 0x3260
cmpl $0x0, %eax
jne 0x17fd4
movq 0x90(%rsp), %rdi
leaq 0x53ad(%rip), %rsi      # 0x1d340
callq 0x6b50
movq %rax, %rdi
callq 0x33c0
movl %eax, 0x8c(%rsp)
cmpl $0x0, 0x8c(%rsp)
je 0x17fc4
movl 0x98(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x98(%rsp)
jmp 0x17fd2
leaq 0x537a(%rip), %rdi      # 0x1d345
movb $0x0, %al
callq 0xa380
jmp 0x17fd4
jmp 0x17fd6
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0x17f32
cmpl $0x0, 0x98(%rsp)
jne 0x18007
movq $0x0, 0xa0(%rsp)
jmp 0x182d3
movl $0x28, %edi
callq 0x3240
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x10(%rsp)
movl 0x98(%rsp), %esi
callq 0x18490
jmp 0x1802a
movq 0x10(%rsp), %rax
movq %rax, 0x80(%rsp)
movl $0x0, 0x9c(%rsp)
movl 0x9c(%rsp), %eax
leaq 0xa260(%rip), %rcx      # 0x222b0
cmpl (%rcx), %eax
jae 0x182c3
movl 0x9c(%rsp), %eax
movl %eax, %ecx
leaq 0xa258(%rip), %rax      # 0x222c0
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, 0x68(%rsp)
movl 0x9c(%rsp), %edi
callq 0x7b50
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rdi
leaq 0x14d3(%rip), %rsi      # 0x19564
callq 0x6b50
movq %rax, %rdi
leaq 0x5297(%rip), %rsi      # 0x1d337
callq 0x3260
cmpl $0x0, %eax
jne 0x182ab
movq 0x60(%rsp), %rdi
leaq 0x5286(%rip), %rsi      # 0x1d340
callq 0x6b50
movq %rax, %rdi
callq 0x33c0
movl %eax, 0x5c(%rsp)
movq 0x60(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x50(%rsp)
cmpq $0x0, 0x50(%rsp)
je 0x18144
movq 0x50(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x33c0
movl %eax, 0x4c(%rsp)
cmpl $0x0, 0x4c(%rsp)
je 0x18133
movq 0x80(%rsp), %rdi
movl 0x5c(%rsp), %esi
movl 0x4c(%rsp), %edx
callq 0x186c0
jmp 0x18133
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x78(%rsp)
movl %eax, 0x74(%rsp)
movl $0x28, %esi
callq 0x3250
jmp 0x182e3
jmp 0x18135
movq 0x50(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x50(%rsp)
jmp 0x180d9
leaq 0x30(%rsp), %rdi
callq 0x108b0
movq 0x68(%rsp), %rax
movslq 0x38(%rax), %rcx
leaq 0x1b720f2(%rip), %rax   # 0x1b8a250
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x28(%rsp)
movl $0x0, 0x48(%rsp)
movl 0x48(%rsp), %eax
movq 0x68(%rsp), %rcx
cmpl 0x3c(%rcx), %eax
jae 0x1823b
movq 0x28(%rsp), %rdi
callq 0x182f0
movq %rax, 0x8(%rsp)
jmp 0x18195
movq 0x8(%rsp), %rax
movq %rax, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpl 0x8(%rcx), %eax
jae 0x18202
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x1c(%rsp), %ecx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,4), %rsi
leaq 0x30(%rsp), %rdi
callq 0x108c0
jmp 0x181d6
jmp 0x181d8
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x181a7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x78(%rsp)
movl %eax, 0x74(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x10a70
jmp 0x182e3
movq 0x20(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x1821b
movq (%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x1821d
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
movq 0x28(%rsp), %rax
addq $0x14, %rax
movq %rax, 0x28(%rsp)
jmp 0x18172
movq 0x80(%rsp), %rdi
movl 0x5c(%rsp), %esi
leaq 0x30(%rsp), %rdx
callq 0x17d70
movl 0x5c(%rsp), %esi
movss 0x30(%rsp), %xmm0
movss 0x34(%rsp), %xmm1
cvtss2sd %xmm0, %xmm0
cvtss2sd %xmm1, %xmm1
movss 0x38(%rsp), %xmm2
cvtss2sd %xmm2, %xmm2
movss 0x3c(%rsp), %xmm3
cvtss2sd %xmm3, %xmm3
movss 0x40(%rsp), %xmm4
cvtss2sd %xmm4, %xmm4
movss 0x44(%rsp), %xmm5
cvtss2sd %xmm5, %xmm5
leaq 0x50c9(%rip), %rdi      # 0x1d361
movb $0x6, %al
callq 0x9ed0
jmp 0x182a1
leaq 0x30(%rsp), %rdi
callq 0x10a70
jmp 0x182ad
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0x18042
movq 0x80(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rax
addq $0xa8, %rsp
retq
movq 0x78(%rsp), %rdi
callq 0x3410
nopl (%rax)