// File: FreeSlave[P]vhlt/hlrad/qradutil.cpp
// Function: CalcSinglePosition(positionmap_t*, int, int)
static void CalcSinglePosition (positionmap_t *map, int is, int it)
{
	position_t *p;
	vec_t smin, smax, tmin, tmax;
	dplane_t clipplanes[4];
	const vec3_t v_s = {1, 0, 0};
	const vec3_t v_t = {0, 1, 0};
	Winding *zone;

	p = &map->grid[is + map->w * it];
	smin = map->start[0] + is * map->step[0];
	smax = map->start[0] + (is + 1) * map->step[0];
	tmin = map->start[1] + it * map->step[1];
	tmax = map->start[1] + (it + 1) * map->step[1];

	VectorScale (v_s,  1, clipplanes[0].normal); clipplanes[0].dist =  smin;
	VectorScale (v_s, -1, clipplanes[1].normal); clipplanes[1].dist = -smax;
	VectorScale (v_t,  1, clipplanes[2].normal); clipplanes[2].dist =  tmin;
	VectorScale (v_t, -1, clipplanes[3].normal); clipplanes[3].dist = -tmax;

#ifdef HLRAD_AVOIDWALLBLEED
	p->nudged = true; // it's nudged unless it can get its position directly from its s,t
#endif
	zone = new Winding (*map->texwinding);
	for (int x = 0; x < 4 && zone->m_NumPoints > 0; x++)
	{
		zone->Clip (clipplanes[x], false);
	}
	if (zone->m_NumPoints == 0)
	{
		p->valid = false;
	}
	else
	{
		vec3_t original_st;
		vec3_t test_st;

		original_st[0] = map->start[0] + (is + 0.5) * map->step[0];
		original_st[1] = map->start[1] + (it + 0.5) * map->step[1];
		original_st[2] = 0.0;

		p->valid = false;

		if (!p->valid)
		{
			VectorCopy (original_st, test_st);
			snap_to_winding (*zone, map->texplane, test_st);

			if (IsPositionValid (map, test_st, p->pos))
			{
				p->valid = true;
#ifdef HLRAD_AVOIDWALLBLEED
				p->nudged = false;
#endif
				p->best_s = test_st[0];
				p->best_t = test_st[1];
			}
		}
		
		if (!p->valid)
		{
			zone->getCenter (test_st);
			if (IsPositionValid (map, test_st, p->pos))
			{
				p->valid = true;
				p->best_s = test_st[0];
				p->best_t = test_st[1];
			}
		}

		if (!p->valid
#ifdef HLRAD_FASTMODE
			&& !g_fastmode
#endif
			)
		{
			const int numnudges = 12;
			vec3_t nudgelist[numnudges] = {{0.1, 0, 0}, {-0.1, 0, 0}, {0, 0.1, 0}, {0, -0.1, 0},
										   {0.3, 0, 0}, {-0.3, 0, 0}, {0, 0.3, 0}, {0, -0.3, 0},
										   {0.3, 0.3, 0}, {-0.3, 0.3, 0}, {-0.3, -0.3, 0}, {0.3, -0.3, 0}};

			for (int i = 0; i < numnudges; i++)
			{
				VectorMultiply (nudgelist[i], map->step, test_st);
				VectorAdd (test_st, original_st, test_st);
				snap_to_winding (*zone, map->texplane, test_st);

				if (IsPositionValid (map, test_st, p->pos))
				{
					p->valid = true;
					p->best_s = test_st[0];
					p->best_t = test_st[1];
					break;
				}
			}
		}
	}
	delete zone;
}

// Assembly:
subq $0x198, %rsp            # imm = 0x198
movq %rdi, 0x190(%rsp)
movl %esi, 0x18c(%rsp)
movl %edx, 0x188(%rsp)
movl $0x0, 0x118(%rsp)
movq $0x3f800000, 0x110(%rsp) # imm = 0x3F800000
movabsq $0x3f80000000000000, %rax # imm = 0x3F80000000000000
movq %rax, 0x100(%rsp)
movl $0x0, 0x108(%rsp)
movq 0x190(%rsp), %rdx
movq 0x108(%rdx), %rax
movl 0x18c(%rsp), %ecx
movl 0x100(%rdx), %edx
movl 0x188(%rsp), %esi
imull %esi, %edx
addl %edx, %ecx
movslq %ecx, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rax
movq %rax, 0x180(%rsp)
movq 0x190(%rsp), %rax
cvtsi2ssl 0x18c(%rsp), %xmm0
movss 0xe8(%rax), %xmm1
movss 0xf4(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x17c(%rsp)
movq 0x190(%rsp), %rax
movl 0x18c(%rsp), %ecx
incl %ecx
cvtsi2ss %ecx, %xmm0
movss 0xe8(%rax), %xmm1
movss 0xf4(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x178(%rsp)
movq 0x190(%rsp), %rax
cvtsi2ssl 0x188(%rsp), %xmm0
movss 0xec(%rax), %xmm1
movss 0xf8(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x174(%rsp)
movq 0x190(%rsp), %rax
movl 0x188(%rsp), %ecx
incl %ecx
cvtsi2ss %ecx, %xmm0
movss 0xec(%rax), %xmm1
movss 0xf8(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x170(%rsp)
movss 0x110(%rsp), %xmm0
movss %xmm0, 0x120(%rsp)
movss 0x114(%rsp), %xmm0
movss %xmm0, 0x124(%rsp)
movss 0x118(%rsp), %xmm0
movss %xmm0, 0x128(%rsp)
movss 0x17c(%rsp), %xmm0
movss %xmm0, 0x12c(%rsp)
movss 0x110(%rsp), %xmm0
movaps 0x11fec(%rip), %xmm1    # 0x718a0
movaps %xmm1, 0x10(%rsp)
pxor %xmm1, %xmm0
movss %xmm0, 0x134(%rsp)
movss 0x114(%rsp), %xmm0
pxor %xmm1, %xmm0
movss %xmm0, 0x138(%rsp)
movss 0x118(%rsp), %xmm0
pxor %xmm1, %xmm0
movss %xmm0, 0x13c(%rsp)
movss 0x178(%rsp), %xmm0
pxor %xmm1, %xmm0
movss %xmm0, 0x140(%rsp)
movss 0x100(%rsp), %xmm0
movss %xmm0, 0x148(%rsp)
movss 0x104(%rsp), %xmm0
movss %xmm0, 0x14c(%rsp)
movss 0x108(%rsp), %xmm0
movss %xmm0, 0x150(%rsp)
movss 0x174(%rsp), %xmm0
movss %xmm0, 0x154(%rsp)
movss 0x100(%rsp), %xmm0
pxor %xmm1, %xmm0
movss %xmm0, 0x15c(%rsp)
movss 0x104(%rsp), %xmm0
pxor %xmm1, %xmm0
movss %xmm0, 0x160(%rsp)
movss 0x108(%rsp), %xmm0
pxor %xmm1, %xmm0
movss %xmm0, 0x164(%rsp)
movss 0x170(%rsp), %xmm0
pxor %xmm1, %xmm0
movss %xmm0, 0x168(%rsp)
movq 0x180(%rsp), %rax
movb $0x1, 0x1(%rax)
movl $0x20, %edi
callq 0x4320
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x20(%rsp)
movq 0x190(%rsp), %rax
movq 0xc0(%rax), %rsi
callq 0xea00
jmp 0x5f9df
movq 0x20(%rsp), %rax
movq %rax, 0xf8(%rsp)
movl $0x0, 0xe8(%rsp)
xorl %eax, %eax
cmpl $0x4, 0xe8(%rsp)
movb %al, 0xf(%rsp)
jge 0x5fa1a
movq 0xf8(%rsp), %rax
cmpl $0x0, 0x8(%rax)
seta %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x5fa24
jmp 0x5fa8d
movq 0xf8(%rsp), %rdi
movslq 0xe8(%rsp), %rax
leaq 0x120(%rsp), %rsi
imulq $0x14, %rax, %rax
addq %rax, %rsi
xorl %edx, %edx
movss 0xe143(%rip), %xmm0     # 0x6db90
callq 0x10450
movl 0xe8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe8(%rsp)
jmp 0x5f9f7
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
movl $0x20, %esi
callq 0x4330
jmp 0x5fef8
movq 0xf8(%rsp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x5faab
movq 0x180(%rsp), %rax
movb $0x0, (%rax)
jmp 0x5fed4
movq 0x190(%rsp), %rax
movss 0xe8(%rax), %xmm0
cvtss2sd %xmm0, %xmm1
cvtsi2sdl 0x18c(%rsp), %xmm0
movsd 0xda20(%rip), %xmm2     # 0x6d4f0
addsd %xmm2, %xmm0
movq 0x190(%rsp), %rax
movss 0xf4(%rax), %xmm2
cvtss2sd %xmm2, %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xdc(%rsp)
movq 0x190(%rsp), %rax
movss 0xec(%rax), %xmm0
cvtss2sd %xmm0, %xmm1
cvtsi2sdl 0x188(%rsp), %xmm0
movsd 0xd9ce(%rip), %xmm2     # 0x6d4f0
addsd %xmm2, %xmm0
movq 0x190(%rsp), %rax
movss 0xf8(%rax), %xmm2
cvtss2sd %xmm2, %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xe0(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0xe4(%rsp)
movq 0x180(%rsp), %rax
movb $0x0, (%rax)
movq 0x180(%rsp), %rax
testb $0x1, (%rax)
jne 0x5fc54
movss 0xdc(%rsp), %xmm0
movss %xmm0, 0xd0(%rsp)
movss 0xe0(%rsp), %xmm0
movss %xmm0, 0xd4(%rsp)
movss 0xe4(%rsp), %xmm0
movss %xmm0, 0xd8(%rsp)
movq 0xf8(%rsp), %rdi
movq 0x190(%rsp), %rsi
addq $0xc8, %rsi
leaq 0xd0(%rsp), %rdx
callq 0x4a4a0
movq 0x190(%rsp), %rdi
movq 0x180(%rsp), %rdx
addq $0xc, %rdx
leaq 0xd0(%rsp), %rsi
movl $0x1, %r8d
movl $0x2, %r9d
movss 0xdfab(%rip), %xmm0     # 0x6dbac
movl %r8d, %ecx
callq 0x61090
testb $0x1, %al
jne 0x5fc0f
jmp 0x5fc52
movq 0x180(%rsp), %rax
movb $0x1, (%rax)
movq 0x180(%rsp), %rax
movb $0x0, 0x1(%rax)
movss 0xd0(%rsp), %xmm0
movq 0x180(%rsp), %rax
movss %xmm0, 0x4(%rax)
movss 0xd4(%rsp), %xmm0
movq 0x180(%rsp), %rax
movss %xmm0, 0x8(%rax)
jmp 0x5fc54
movq 0x180(%rsp), %rax
testb $0x1, (%rax)
jne 0x5fcf1
movq 0xf8(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0xe060
movq 0x190(%rsp), %rdi
movq 0x180(%rsp), %rdx
addq $0xc, %rdx
leaq 0xd0(%rsp), %rsi
movl $0x1, %r8d
movl $0x2, %r9d
movss 0xdf02(%rip), %xmm0     # 0x6dbac
movl %r8d, %ecx
callq 0x61090
testb $0x1, %al
jne 0x5fcb8
jmp 0x5fcef
movq 0x180(%rsp), %rax
movb $0x1, (%rax)
movss 0xd0(%rsp), %xmm0
movq 0x180(%rsp), %rax
movss %xmm0, 0x4(%rax)
movss 0xd4(%rsp), %xmm0
movq 0x180(%rsp), %rax
movss %xmm0, 0x8(%rax)
jmp 0x5fcf1
movq 0x180(%rsp), %rax
testb $0x1, (%rax)
jne 0x5fed2
leaq 0x524dce7(%rip), %rax   # 0x52ad9f0
testb $0x1, (%rax)
jne 0x5fed2
movl $0xc, 0xcc(%rsp)
leaq 0x30(%rsp), %rdi
leaq 0x11cd7(%rip), %rsi     # 0x71a00
movl $0x90, %edx
callq 0x4280
movl $0x0, 0x2c(%rsp)
cmpl $0xc, 0x2c(%rsp)
jge 0x5fed0
movslq 0x2c(%rsp), %rcx
leaq 0x30(%rsp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
movq 0x190(%rsp), %rax
mulss 0xf4(%rax), %xmm0
movss %xmm0, 0xd0(%rsp)
movslq 0x2c(%rsp), %rcx
leaq 0x30(%rsp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movq 0x190(%rsp), %rax
mulss 0xf8(%rax), %xmm0
movss %xmm0, 0xd4(%rsp)
movslq 0x2c(%rsp), %rcx
leaq 0x30(%rsp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movq 0x190(%rsp), %rax
mulss 0xfc(%rax), %xmm0
movss %xmm0, 0xd8(%rsp)
movss 0xd0(%rsp), %xmm0
addss 0xdc(%rsp), %xmm0
movss %xmm0, 0xd0(%rsp)
movss 0xd4(%rsp), %xmm0
addss 0xe0(%rsp), %xmm0
movss %xmm0, 0xd4(%rsp)
movss 0xd8(%rsp), %xmm0
addss 0xe4(%rsp), %xmm0
movss %xmm0, 0xd8(%rsp)
movq 0xf8(%rsp), %rdi
movq 0x190(%rsp), %rsi
addq $0xc8, %rsi
leaq 0xd0(%rsp), %rdx
callq 0x4a4a0
movq 0x190(%rsp), %rdi
movq 0x180(%rsp), %rdx
addq $0xc, %rdx
leaq 0xd0(%rsp), %rsi
movl $0x1, %r8d
movl $0x2, %r9d
movss 0xdd35(%rip), %xmm0     # 0x6dbac
movl %r8d, %ecx
callq 0x61090
testb $0x1, %al
jne 0x5fe85
jmp 0x5febe
movq 0x180(%rsp), %rax
movb $0x1, (%rax)
movss 0xd0(%rsp), %xmm0
movq 0x180(%rsp), %rax
movss %xmm0, 0x4(%rax)
movss 0xd4(%rsp), %xmm0
movq 0x180(%rsp), %rax
movss %xmm0, 0x8(%rax)
jmp 0x5fed0
jmp 0x5fec0
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x5fd3b
jmp 0x5fed2
jmp 0x5fed4
movq 0xf8(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x5fef0
movq (%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
addq $0x198, %rsp            # imm = 0x198
retq
movq 0xf0(%rsp), %rdi
callq 0x45c0
nopw %cs:(%rax,%rax)