// File: FreeSlave[P]vhlt/hlvis/vis.cpp
// Function: CalcVis()
static void     CalcVis()
{
    unsigned        i;
	char visdatafile[_MAX_PATH];

#ifdef ZHLT_DEFAULTEXTENSION_FIX
	safe_snprintf(visdatafile, _MAX_PATH, "%s.vdt", g_Mapname);
#else
	strcpy(visdatafile, g_Mapname);
	DefaultExtension(visdatafile, ".vdt");
#endif

	// Remove this file
	unlink(visdatafile);

/*    if(g_postcompile)
	{
		if(!g_maxdistance)
		{
			Error("Must use -maxdistance parameter with -postcompile");
		}

		// Decompress everything so we can edit it
		DecompressAll();
		
		NamedRunThreadsOn(g_portalleafs, g_estimate, PostMaxDistVis);

		// Recompress it
		CompressAll();
	}
	else
	{*/
//		InitVisBlock();
//		SetupVisBlockLeafs();

		NamedRunThreadsOn(g_numportals * 2, g_estimate, BasePortalVis);

//		if(g_numvisblockers)
//			NamedRunThreadsOn(g_numvisblockers, g_estimate, BlockVis);

		// First do a normal VIS, save to file, then redo MaxDistVis

		CalcPortalVis();

		//
		// assemble the leaf vis lists by oring and compressing the portal lists
		//
		for (i = 0; i < g_portalleafs; i++)
		{
	        LeafFlow(i);
	    }

		Log("average leafs visible: %i\n", totalvis / g_portalleafs);

		if(g_maxdistance)
		{
			totalvis = 0;
			
			Log("saving visdata to %s...\n", visdatafile);
			SaveVisData(visdatafile);

			// We need to reset the uncompressed variable and portal visbits
			free(g_uncompressed);
			g_uncompressed = (byte*)calloc(g_portalleafs, g_bitbytes);

			vismap_p = g_dvisdata;

			// We don't need to run BasePortalVis again			
			NamedRunThreadsOn(g_portalleafs, g_estimate, MaxDistVis);

			// No need to run this - MaxDistVis now writes directly to visbits after the initial VIS
			//CalcPortalVis();
		
			for (i = 0; i < g_portalleafs; i++)
			{
			    LeafFlow(i);
			}

#ifndef HLVIS_MAXDIST_NEW
			// FIX: Used to reset p->status to stat_none; now it justs frees p->visbits
			ResetPortalStatus();
#endif

			Log("average maxdistance leafs visible: %i\n", totalvis / g_portalleafs);
		}
//	}
}

// Assembly:
subq $0x1008, %rsp           # imm = 0x1008
movq %rsp, %rdi
movl $0x1000, %esi           # imm = 0x1000
leaq 0x6b4e(%rip), %rdx      # 0x1d1d4
leaq 0xabf3(%rip), %rcx      # 0x21280
movb $0x0, %al
callq 0x82e0
movq %rsp, %rdi
callq 0x3100
leaq 0x6b38(%rip), %rdi      # 0x1d1db
callq 0xa0f0
movq %rax, %rsi
leaq 0x44c9(%rip), %rdi      # 0x1ab7b
movb $0x0, %al
callq 0x9ed0
movl 0x208f0b1(%rip), %edi   # 0x20a5770
shll %edi
movb 0xbbc9(%rip), %al       # 0x22290
leaq -0x53fe(%rip), %rdx     # 0x112d0
andb $0x1, %al
movzbl %al, %esi
callq 0xc0a0
callq 0x174a0
movl $0x0, 0x1004(%rsp)
movl 0x1004(%rsp), %eax
cmpl 0x208f07f(%rip), %eax   # 0x20a5774
jae 0x16716
movl 0x1004(%rsp), %edi
callq 0x17560
movl 0x1004(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1004(%rsp)
jmp 0x166e8
movl 0x20df0d4(%rip), %eax   # 0x20f57f0
xorl %edx, %edx
divl 0x208f050(%rip)         # 0x20a5774
movl %eax, %esi
leaq 0x6abd(%rip), %rdi      # 0x1d1ea
movb $0x0, %al
callq 0x9ed0
cmpl $0x0, 0x208f079(%rip)   # 0x20a57b4
je 0x1682b
movl $0x0, 0x20df0a5(%rip)   # 0x20f57f0
movq %rsp, %rsi
leaq 0x6ab0(%rip), %rdi      # 0x1d205
movb $0x0, %al
callq 0x9ed0
movq %rsp, %rdi
callq 0x15610
movq 0x208f035(%rip), %rdi   # 0x20a57a0
callq 0x32b0
movl 0x208effe(%rip), %eax   # 0x20a5774
movl %eax, %edi
movl 0x208f02a(%rip), %eax   # 0x20a57a8
movl %eax, %esi
callq 0x3160
movq %rax, 0x208f014(%rip)   # 0x20a57a0
leaq 0x13b3d(%rip), %rax     # 0x2a2d0
movq %rax, 0x20df036(%rip)   # 0x20f57d0
leaq 0x6a7d(%rip), %rdi      # 0x1d21e
callq 0xa0f0
movq %rax, %rsi
leaq 0x43cb(%rip), %rdi      # 0x1ab7b
movb $0x0, %al
callq 0x9ed0
movl 0x208efb7(%rip), %edi   # 0x20a5774
movb 0xbacd(%rip), %al       # 0x22290
leaq -0x2faa(%rip), %rdx     # 0x13820
andb $0x1, %al
movzbl %al, %esi
callq 0xc0a0
movl $0x0, 0x1004(%rsp)
movl 0x1004(%rsp), %eax
cmpl 0x208ef88(%rip), %eax   # 0x20a5774
jae 0x1680d
movl 0x1004(%rsp), %edi
callq 0x17560
movl 0x1004(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1004(%rsp)
jmp 0x167df
movl 0x20defdd(%rip), %eax   # 0x20f57f0
xorl %edx, %edx
divl 0x208ef59(%rip)         # 0x20a5774
movl %eax, %esi
leaq 0x6a06(%rip), %rdi      # 0x1d22a
movb $0x0, %al
callq 0x9ed0
addq $0x1008, %rsp           # imm = 0x1008
retq
nopw %cs:(%rax,%rax)