// File: /workspace/github_c_makefile_star2_O1/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 0x2f707
movb $0x0, (%r15,%rax)
movb $0x1, %bpl
movsbl %bpl, %esi
movq %r15, %rdi
callq 0x1ff50
movl %eax, %ecx
shll $0x10, %ecx
cmpl $0xffff0000, %ecx       # imm = 0xFFFF0000
jne 0x2f734
movl $0x7, 0x7406e(%rip)     # 0xa3798
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x2f8b5
movswl %ax, %ebp
leaq 0x7405a(%rip), %rsi     # 0xa3798
movl %ebp, %edi
movl $0x4, %edx
callq 0x201c2
movl 0x74048(%rip), %eax     # 0xa3798
cmpl $0x7, %eax
je 0x2f77b
cmpl $0x8, %eax
jne 0x2f8a9
movq %rbx, 0x10(%rsp)
movl $0x4000, 0xc(%rsp)      # imm = 0x4000
movl $0x1000, %r15d          # imm = 0x1000
movl $0x4000, 0x8(%rsp)      # imm = 0x4000
jmp 0x2f796
movq %rbx, 0x10(%rsp)
movl $0x1000, 0x8(%rsp)      # imm = 0x1000
movl $0x2000, 0xc(%rsp)      # imm = 0x2000
movl $0x400, %r15d           # imm = 0x400
movq 0x50(%rsp), %rbx
callq 0x2565f
leaq 0x37ab39(%rip), %rdi    # 0x3aa2e0
movl $0x80, %esi
xorl %edx, %edx
callq 0x4abc9
leaq 0x37ad26(%rip), %rdi    # 0x3aa4e0
movl $0x200, %esi            # imm = 0x200
xorl %edx, %edx
callq 0x4abc9
leaq 0x37b513(%rip), %rdi    # 0x3aace0
movl $0x200, %esi            # imm = 0x200
xorl %edx, %edx
callq 0x4abc9
movl %ebp, %edi
movq %r14, %rsi
movl $0x4, %edx
callq 0x201c2
movl %ebp, %edi
movq 0x10(%rsp), %rsi
movl $0x4, %edx
callq 0x201c2
movl %ebp, %edi
movq %r13, %rsi
movl $0x4, %edx
callq 0x201c2
movl %ebp, %edi
movq %r12, %rsi
movl $0x2, %edx
callq 0x201c2
movl %ebp, %edi
movq %rbx, %rsi
movl $0x2, %edx
callq 0x201c2
leaq 0x39f733(%rip), %rsi    # 0x3cef60
movl %ebp, %edi
movl $0x2, %edx
callq 0x201c2
movswl 0x39f720(%rip), %eax    # 0x3cef60
cmpl %eax, %r15d
jl 0x2f8a9
shll $0x3, %eax
leal (%rax,%rax,4), %edx
leaq 0x21320e(%rip), %rsi    # 0x242a60
movl %ebp, %edi
callq 0x201c2
leaq 0x3e6408(%rip), %rsi    # 0x415c68
movl %ebp, %edi
movl $0x2, %edx
callq 0x201c2
movswl 0x3e63f5(%rip), %edx    # 0x415c68
cmpl %edx, 0xc(%rsp)
jl 0x2f8a9
shll $0x5, %edx
leaq 0x1881cd(%rip), %rsi    # 0x1b7a50
movl %ebp, %edi
callq 0x201c2
leaq 0x6(%rsp), %r12
movl %ebp, %edi
movq %r12, %rsi
movl $0x2, %edx
callq 0x201c2
movswl (%r12), %eax
cmpl %eax, 0x8(%rsp)
jge 0x2f8c6
movl %ebp, %edi
callq 0x20671
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
imull $0x2c, %eax, %edx
leaq 0x24c3e0(%rip), %rsi    # 0x27bcb0
movl %ebp, %edi
callq 0x201c2
cmpw $0x0, 0x6(%rsp)
movq 0x10(%rsp), %rbx
jle 0x2f92c
leaq 0x24c3d1(%rip), %r15    # 0x27bcbc
xorl %r13d, %r13d
movl (%r15), %eax
movl %eax, %ecx
notl %ecx
testb $0x30, %cl
jne 0x2f903
andl $0xffcf, %eax           # imm = 0xFFCF
movw %ax, (%r15)
movswl 0xc(%r15), %r12d
movswl 0xe(%r15), %edi
callq 0x35a4f
movl %r12d, %edi
callq 0x35acd
incq %r13
movswq 0x6(%rsp), %rax
addq $0x2c, %r15
cmpq %rax, %r13
jl 0x2f8ee
movl (%r14), %edi
movl (%rbx), %esi
movq 0x50(%rsp), %rdx
callq 0x25f34
movl %ebp, %edi
callq 0x20671
leaq 0x2fc367(%rip), %rdi    # 0x32bcb0
xorl %ebx, %ebx
movl $0x30c00, %edx          # imm = 0x30C00
xorl %esi, %esi
callq 0x62f0
movl $0x0, 0x3e630b(%rip)    # 0x415c6c
jmp 0x2f8b5