// File: /workspace/github_c_makefile_star2_O1/jonof[P]jftekwar/src/bstub.c
// Function: ExtInit
int ExtInit(void)
{
	int i, rv = 0;
	unsigned char remapbuf[256];

	buildputs("Tekwar BUILD V1.2\n\n");

#if defined(DATADIR)
    {
        const char *datadir = DATADIR;
        if (datadir && datadir[0]) {
            addsearchpath(datadir);
        }
    }
#endif

     {
          char *supportdir = Bgetsupportdir(1);
          char *appdir = Bgetappdir();
          char dirpath[BMAX_PATH+1];

          // the OSX app bundle, or on Windows the directory where the EXE was launched
          if (appdir) {
               addsearchpath(appdir);
               free(appdir);
          }

          // the global support files directory
          if (supportdir) {
               Bsnprintf(dirpath, sizeof(dirpath), "%s/JFTekWar", supportdir);
               addsearchpath(dirpath);
               free(supportdir);
          }
     }

     // default behaviour is to write to the user profile directory, but
     // creating a 'user_profiles_disabled' file in the current working
     // directory where the game was launched makes the installation
     // "portable" by writing into the working directory
     if (access("user_profiles_disabled", F_OK) == 0) {
          char cwd[BMAX_PATH+1];
          if (getcwd(cwd, sizeof(cwd))) {
               addsearchpath(cwd);
          }
     } else {
          char *supportdir;
          char dirpath[BMAX_PATH+1];
          int asperr;

          if ((supportdir = Bgetsupportdir(0))) {
#if defined(_WIN32) || defined(__APPLE__)
               const char *confdir = "JFTekWar";
#else
               const char *confdir = ".jftekwar";
#endif
               Bsnprintf(dirpath, sizeof(dirpath), "%s/%s", supportdir, confdir);
               asperr = addsearchpath(dirpath);
               if (asperr == -2) {
                    if (Bmkdir(dirpath, S_IRWXU) == 0) {
                         asperr = addsearchpath(dirpath);
                    } else {
                         asperr = -1;
                    }
               }
               if (asperr == 0) {
                    chdir(dirpath);
               }
               free(supportdir);
          }
     }

	initgroupfile("stuff.dat");
	bpp = 8;
	if (loadsetup("build.cfg") < 0) buildputs("Configuration file not found, using defaults.\n"), rv = 1;
	memcpy((void *)buildkeys,(void *)keys,sizeof(buildkeys));   //Trick to make build use setup.dat keys
	if (option[4] > 0) option[4] = 0;
	if (initengine()) {
		wm_msgbox("Build Engine Initialisation Error",
				"There was a problem initialising the Build engine: %s", engineerrstr);
		return -1;
	}

		//You can load your own palette lookup tables here if you just
		//copy the right code!
	for(i=0;i<256;i++)
		remapbuf[i] = ((i+32)&255);  //remap colors for screwy palette sectors
	makepalookup(16,remapbuf,0,0,0,1);

	kensplayerheight = 34;
	zmode = 0;
	defaultspritecstat = 0;

    parallaxtype=2;
    parallaxyoffs=112;
    pskyoff[0]=0;   
    pskyoff[1]=1;   
    pskyoff[2]=2; 
    pskyoff[3]=3;   
    pskybits=2;     // 4 tiles
    memset(&recXT, 0, sizeof(struct spriteextension));
    recordXT=-1;

    ExtDeleteSprite = DeleteSprite;

	return rv;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x410, %rsp            # imm = 0x410
leaq 0x68ff3(%rip), %rdi     # 0x72cba
callq 0x32200
leaq 0x68ffb(%rip), %rdi     # 0x72cce
callq 0x1f6ff
movl $0x1, %edi
callq 0x21bbd
movq %rax, %rbx
callq 0x21b31
testq %rax, %rax
je 0x9d02
movq %rax, %r14
movq %rax, %rdi
callq 0x1f6ff
movq %r14, %rdi
callq 0x61c0
testq %rbx, %rbx
je 0x9d33
leaq 0x68fe0(%rip), %rdx     # 0x72cee
movq %rsp, %r14
movl $0x401, %esi            # imm = 0x401
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x64f0
movq %r14, %rdi
callq 0x1f6ff
movq %rbx, %rdi
callq 0x61c0
leaq 0x68fc0(%rip), %rdi     # 0x72cfa
xorl %esi, %esi
callq 0x6160
testl %eax, %eax
je 0x9dbe
xorl %edi, %edi
callq 0x21bbd
testq %rax, %rax
je 0x9dd8
movq %rax, %rbx
leaq 0x68fbc(%rip), %rdx     # 0x72d1b
leaq 0x68fab(%rip), %r8      # 0x72d11
movq %rsp, %r14
movl $0x401, %esi            # imm = 0x401
movq %r14, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x64f0
movq %r14, %rdi
callq 0x1f6ff
cmpl $-0x2, %eax
jne 0x9da8
movq %rsp, %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x6290
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
jne 0x9da8
movq %rsp, %rdi
callq 0x1f6ff
testl %eax, %eax
jne 0x9db4
movq %rsp, %rdi
callq 0x6ca0
movq %rbx, %rdi
callq 0x61c0
jmp 0x9dd8
movq %rsp, %rdi
movl $0x401, %esi            # imm = 0x401
callq 0x63c0
testq %rax, %rax
je 0x9dd8
movq %rsp, %rdi
callq 0x1f6ff
leaq 0x68f42(%rip), %rdi     # 0x72d21
callq 0x1fa6b
leaq 0x1732b9(%rip), %rax    # 0x17d0a4
movl $0x8, (%rax)
leaq 0x68f33(%rip), %rdi     # 0x72d2b
callq 0x1d9e0
xorl %ebx, %ebx
testl %eax, %eax
jns 0x9e14
leaq 0x68f2b(%rip), %rdi     # 0x72d35
callq 0x32200
movl $0x1, %ebx
leaq 0x99165(%rip), %rdi     # 0xa2f80
leaq 0x8889e(%rip), %rsi     # 0x926c0
movl $0xa, %ecx
rep  movsq (%rsi), %es:(%rdi)
cmpb $0x0, 0x88883(%rip)     # 0x926b4
je 0x9e3a
movb $0x0, 0x8887a(%rip)     # 0x926b4
callq 0x24dd7
testl %eax, %eax
je 0x9e6c
leaq 0x1adbd6(%rip), %rax    # 0x1b7a20
movq (%rax), %rdx
leaq 0x68f10(%rip), %rdi     # 0x72d64
leaq 0x68f2b(%rip), %rsi     # 0x72d86
xorl %eax, %eax
callq 0x6b95c
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x9f28
xorl %eax, %eax
leal 0x20(%rax), %ecx
movb %cl, (%rsp,%rax)
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x9e6e
movq %rsp, %rsi
xorl %ebp, %ebp
movl $0x10, %edi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0x3ae5b
leaq 0x99152(%rip), %rax     # 0xa2ff4
movl $0x22, (%rax)
leaq 0x122a71(%rip), %rax    # 0x12c920
movl $0x0, (%rax)
leaq 0x122a68(%rip), %rax    # 0x12c924
movw %bp, (%rax)
leaq 0x352bfc(%rip), %rax    # 0x35cac2
movb $0x2, (%rax)
leaq 0x352bf4(%rip), %rax    # 0x35cac4
movl $0x70, (%rax)
leaq 0x3529e3(%rip), %rax    # 0x35c8c0
movabsq $0x3000200010000, %rcx  # imm = 0x3000200010000
movq %rcx, (%rax)
leaq 0x352bcf(%rip), %rax    # 0x35cac0
movw $0x2, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x9a8e0(%rip)    # 0xa47e0
movups %xmm0, 0x9a8e9(%rip)    # 0xa47f0
movw %bp, 0x9a8f2(%rip)      # 0xa4800
movw $0xffff, 0x88801(%rip)  # imm = 0xFFFF
leaq 0x18(%rip), %rax        # 0x9f36
leaq 0x13267b(%rip), %rcx    # 0x13c5a0
movq %rax, (%rcx)
movl %ebx, %eax
addq $0x410, %rsp            # imm = 0x410
popq %rbx
popq %r14
popq %rbp
retq