// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/engine.c
// Function: loadboard
int loadboard(char *filename, char fromwhere, int *daposx, int *daposy, int *daposz,
			 short *daang, short *dacursectnum)
{
	short fil, i, numsprites;
	short maxsectors, maxwalls, maxsprites;

	i = strlen(filename)-1;
	if ((unsigned char)filename[i] == 255) { filename[i] = 0; fromwhere = 1; }	// JBF 20040119: "compatibility"
	if ((fil = kopen4load(filename,fromwhere)) == -1)
		{ mapversion = 7L; return(-1); }

	kread(fil,&mapversion,4); mapversion = B_LITTLE32(mapversion);
	if (mapversion == 7)
	{
		maxsectors = MAXSECTORSV7;
		maxwalls = MAXWALLSV7;
		maxsprites = MAXSPRITESV7;
	}
	else if (mapversion == 8)
	{
		maxsectors = MAXSECTORSV8;
		maxwalls = MAXWALLSV8;
		maxsprites = MAXSPRITESV8;
	}
	else
	{
		kclose(fil);
		return(-2);
	}

	/*
	// Enable this for doing map checksum tests
	clearbufbyte(&wall,   sizeof(wall),   0);
	clearbufbyte(&sector, sizeof(sector), 0);
	clearbufbyte(&sprite, sizeof(sprite), 0);
	*/

	initspritelists();

	clearbuf(&show2dsector[0],(int)((MAXSECTORS+3)>>5),0L);
	clearbuf(&show2dsprite[0],(int)((MAXSPRITES+3)>>5),0L);
	clearbuf(&show2dwall[0],(int)((MAXWALLS+3)>>5),0L);

	kread(fil,daposx,4); *daposx = B_LITTLE32(*daposx);
	kread(fil,daposy,4); *daposy = B_LITTLE32(*daposy);
	kread(fil,daposz,4); *daposz = B_LITTLE32(*daposz);
	kread(fil,daang,2);  *daang  = B_LITTLE16(*daang);
	kread(fil,dacursectnum,2); *dacursectnum = B_LITTLE16(*dacursectnum);

	kread(fil,&numsectors,2); numsectors = B_LITTLE16(numsectors);
	if (numsectors > maxsectors) { kclose(fil); return(-2); }
	kread(fil,&sector[0],sizeof(sectortype)*numsectors);
	for (i=numsectors-1; i>=0; i--) {
		sector[i].wallptr       = B_LITTLE16(sector[i].wallptr);
		sector[i].wallnum       = B_LITTLE16(sector[i].wallnum);
		sector[i].ceilingz      = B_LITTLE32(sector[i].ceilingz);
		sector[i].floorz        = B_LITTLE32(sector[i].floorz);
		sector[i].ceilingstat   = B_LITTLE16(sector[i].ceilingstat);
		sector[i].floorstat     = B_LITTLE16(sector[i].floorstat);
		sector[i].ceilingpicnum = B_LITTLE16(sector[i].ceilingpicnum);
		sector[i].ceilingheinum = B_LITTLE16(sector[i].ceilingheinum);
		sector[i].floorpicnum   = B_LITTLE16(sector[i].floorpicnum);
		sector[i].floorheinum   = B_LITTLE16(sector[i].floorheinum);
		sector[i].lotag         = B_LITTLE16(sector[i].lotag);
		sector[i].hitag         = B_LITTLE16(sector[i].hitag);
		sector[i].extra         = B_LITTLE16(sector[i].extra);
	}

	kread(fil,&numwalls,2); numwalls = B_LITTLE16(numwalls);
	if (numwalls > maxwalls) { kclose(fil); return(-2); }
	kread(fil,&wall[0],sizeof(walltype)*numwalls);
	for (i=numwalls-1; i>=0; i--) {
		wall[i].x          = B_LITTLE32(wall[i].x);
		wall[i].y          = B_LITTLE32(wall[i].y);
		wall[i].point2     = B_LITTLE16(wall[i].point2);
		wall[i].nextwall   = B_LITTLE16(wall[i].nextwall);
		wall[i].nextsector = B_LITTLE16(wall[i].nextsector);
		wall[i].cstat      = B_LITTLE16(wall[i].cstat);
		wall[i].picnum     = B_LITTLE16(wall[i].picnum);
		wall[i].overpicnum = B_LITTLE16(wall[i].overpicnum);
		wall[i].lotag      = B_LITTLE16(wall[i].lotag);
		wall[i].hitag      = B_LITTLE16(wall[i].hitag);
		wall[i].extra      = B_LITTLE16(wall[i].extra);
	}

	kread(fil,&numsprites,2); numsprites = B_LITTLE16(numsprites);
	if (numsprites > maxsprites) { kclose(fil); return(-2); }
	kread(fil,&sprite[0],sizeof(spritetype)*numsprites);
	for (i=numsprites-1; i>=0; i--) {
		sprite[i].x       = B_LITTLE32(sprite[i].x);
		sprite[i].y       = B_LITTLE32(sprite[i].y);
		sprite[i].z       = B_LITTLE32(sprite[i].z);
		sprite[i].cstat   = B_LITTLE16(sprite[i].cstat);
		sprite[i].picnum  = B_LITTLE16(sprite[i].picnum);
		sprite[i].sectnum = B_LITTLE16(sprite[i].sectnum);
		sprite[i].statnum = B_LITTLE16(sprite[i].statnum);
		sprite[i].ang     = B_LITTLE16(sprite[i].ang);
		sprite[i].owner   = B_LITTLE16(sprite[i].owner);
		sprite[i].xvel    = B_LITTLE16(sprite[i].xvel);
		sprite[i].yvel    = B_LITTLE16(sprite[i].yvel);
		sprite[i].zvel    = B_LITTLE16(sprite[i].zvel);
		sprite[i].lotag   = B_LITTLE16(sprite[i].lotag);
		sprite[i].hitag   = B_LITTLE16(sprite[i].hitag);
		sprite[i].extra   = B_LITTLE16(sprite[i].extra);
	}

	for(i=0;i<numsprites;i++) {
		if ((sprite[i].cstat & 48) == 48) sprite[i].cstat &= ~48;
		insertsprite(sprite[i].sectnum,sprite[i].statnum);
	}

		//Must be after loading sectors, etc!
	updatesector(*daposx,*daposy,dacursectnum);

	kclose(fil);

#if USE_POLYMOST && USE_OPENGL
	memset(spriteext, 0, sizeof(spriteext));
#endif
	guniqhudid = 0;

	return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r9, %r12
movq %r8, %r13
movq %rcx, %rbx
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %r15
callq 0x6420
decl %eax
movswq %ax, %rax
cmpb $-0x1, (%r15,%rax)
jne 0x2d1c7
movb $0x0, (%r15,%rax)
movb $0x1, %bpl
movsbl %bpl, %esi
movq %r15, %rdi
callq 0x1dc36
movl %eax, %ecx
shll $0x10, %ecx
cmpl $0xffff0000, %ecx       # imm = 0xFFFF0000
jne 0x2d1f1
movl $0x7, 0x705ae(%rip)     # 0x9d798
pushq $-0x1
jmp 0x2d35e
movswl %ax, %ebp
leaq 0x7059d(%rip), %rsi     # 0x9d798
pushq $0x4
popq %rdx
movl %ebp, %edi
callq 0x1de3f
movl 0x7058d(%rip), %eax     # 0x9d798
cmpl $0x7, %eax
je 0x2d235
cmpl $0x8, %eax
jne 0x2d355
movq %rbx, 0x10(%rsp)
movl $0x4000, 0xc(%rsp)      # imm = 0x4000
movl $0x1000, %ebx           # imm = 0x1000
movl $0x4000, 0x8(%rsp)      # imm = 0x4000
jmp 0x2d24f
movq %rbx, 0x10(%rsp)
movl $0x1000, 0x8(%rsp)      # imm = 0x1000
movl $0x2000, 0xc(%rsp)      # imm = 0x2000
movl $0x400, %ebx            # imm = 0x400
callq 0x22da8
leaq 0x377085(%rip), %rdi    # 0x3a42e0
movl $0x80, %esi
xorl %edx, %edx
callq 0x444af
leaq 0x377272(%rip), %rdi    # 0x3a44e0
movl $0x200, %esi            # imm = 0x200
xorl %edx, %edx
callq 0x444af
leaq 0x377a5f(%rip), %rdi    # 0x3a4ce0
movl $0x200, %esi            # imm = 0x200
xorl %edx, %edx
callq 0x444af
pushq $0x4
popq %r15
movl %ebp, %edi
movq %r14, %rsi
movl %r15d, %edx
callq 0x1de3f
movl %ebp, %edi
movq 0x10(%rsp), %rsi
movl %r15d, %edx
callq 0x1de3f
movl %ebp, %edi
movq %r13, %rsi
movl %r15d, %edx
callq 0x1de3f
pushq $0x2
popq %r15
movl %ebp, %edi
movq %r12, %rsi
movq 0x50(%rsp), %r12
movl %r15d, %edx
callq 0x1de3f
movl %ebp, %edi
movq %r12, %rsi
movl %r15d, %edx
callq 0x1de3f
leaq 0x39bc7c(%rip), %rsi    # 0x3c8f60
movl %ebp, %edi
movl %r15d, %edx
callq 0x1de3f
movswl 0x39bc6b(%rip), %eax    # 0x3c8f60
cmpl %eax, %ebx
jl 0x2d355
imull $0x28, %eax, %edx
leaq 0x20f75d(%rip), %rsi    # 0x23ca60
movl %ebp, %edi
callq 0x1de3f
leaq 0x3e2957(%rip), %rsi    # 0x40fc68
pushq $0x2
popq %rdx
movl %ebp, %edi
callq 0x1de3f
movswl 0x3e2946(%rip), %edx    # 0x40fc68
cmpl %edx, 0xc(%rsp)
jl 0x2d355
shll $0x5, %edx
leaq 0x18471e(%rip), %rsi    # 0x1b1a50
movl %ebp, %edi
callq 0x1de3f
leaq 0x6(%rsp), %r15
pushq $0x2
popq %rdx
movl %ebp, %edi
movq %r15, %rsi
callq 0x1de3f
movswl (%r15), %eax
cmpl %eax, 0x8(%rsp)
jge 0x2d370
movl %ebp, %edi
callq 0x1e2db
pushq $-0x2
popq %rbx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
imull $0x2c, %eax, %edx
leaq 0x248936(%rip), %rsi    # 0x275cb0
movl %ebp, %edi
callq 0x1de3f
leaq 0x248934(%rip), %rbx    # 0x275cbc
xorl %r15d, %r15d
movswq 0x6(%rsp), %rax
cmpq %rax, %r15
jge 0x2d3bf
movl (%rbx), %eax
movl %eax, %ecx
notl %ecx
testb $0x30, %cl
jne 0x2d3a9
andl $0xffcf, %eax           # imm = 0xFFCF
movw %ax, (%rbx)
movswl 0xc(%rbx), %edi
movswl 0xe(%rbx), %esi
callq 0x2d3f9
incq %r15
addq $0x2c, %rbx
jmp 0x2d38b
movl (%r14), %edi
movq 0x10(%rsp), %rax
movl (%rax), %esi
movq %r12, %rdx
callq 0x23663
movl %ebp, %edi
callq 0x1e2db
leaq 0x2f88d1(%rip), %rdi    # 0x325cb0
xorl %ebx, %ebx
movl $0x30c00, %edx          # imm = 0x30C00
xorl %esi, %esi
callq 0x62f0
andl $0x0, 0x3e2878(%rip)    # 0x40fc6c
jmp 0x2d35f