// File: /workspace/github_c_makefile_star2_O1/vlenhart[P]q3bsp/main.cpp
// Function: main
int  main(int argc,char **argv)
{

  VFormatOptions option;
  char *options=NULL;
  char *fileArg = NULL;
  
  int argi=1;	// the current argument 

  if (argc>=2 && argv[argi][0]== '-') { // we have options 
	  options = argv[argi];
	  argi++;
  }	

  if ( argi >= argc )
  {
    printf("Usage: q3bsp [-options] <name>.BSP\n");
    printf("Where <name> is the name of a valid Quake3 BSP file.\n\n");
    printf("This utility will convert a Quake3 BSP into a valid\n");
    printf("polygon mesh and output the results into two seperate VRML 1.0\n");
    printf("files.  The first VRML file contains all the U/V mapping and\n");
    printf("texture mapping information for channel #1, and the second\n");
    printf("VRML file will contain all of the U/V mapping and texture names\n");
    printf("for the second U/V channel, which contains all lightmap\n");
    printf("information.  You can then directly import these files into any\n");
    printf("number of 3d editing tools, including 3d Studio Max\n");

    printf("This tool also extracts the lightmap data and saves it out as a\n");
    printf("series of .BMP files.\n\n");
    printf("OpenSourced by John W. Ratcliff on December 5, 2000\n");
    printf("Merry Christmas!\n\n");
    printf("Contact Id Software about using Quake 3 data files in and\n");
    printf("Quake 3 editing tools for commercial software development\n");
    printf("projects.\n");
    printf("Extended Options: \n");
    printf("-1		VRML 1 output\n");
    printf("-2		VRML 2 output 2 files \n");
    printf("-2me	VRML 2 output with MultiTexture extension nodes & effects\n");
    exit(1);
  }

  fileArg = argv[argi];
  if (options) {
	  
	  if (strchr(options,'2'))
			option.vrml2 = true;
	  else if (strchr(options,'1'))
			option.vrml2 = false;
	  
	  if (strchr(options,'m'))
			option.useMultiTexturing = true;

	  if (strchr(options,'M'))
			option.useMat = true;
	  
	  if (strchr(options,'l'))
			option.useLighting = true;
	  
	  if (strchr(options,'b'))
			option.useBsp = true;

	  if (strchr(options,'e'))
			option.useEffects = true;

	  if (strchr(options,'v'))
			option.verbose = true;

	  if (strchr(options,'n'))
			option.noTextureCoordinates = true;

  }	
  
  Quake3BSP q( SGET(fileArg), SGET("a") );

  VertexMesh *mesh = q.GetVertexMesh();
  

  if ( mesh )
  {
    String str = fileArg;
	
	char * del = strrchr(fileArg,'\\');
	if (del) str = (del+1); // use file name part only
	
    String name1 = str + "1";
    String name2 = str + "2";


	if (option.vrml2) { // VRML 2 style 

		if (!option.useMultiTexturing) {
		    // save VRML file using channel #1
			// save VRML file using channel #2
			name1+=".wrl";

			FILE *fph = fopen(name1.c_str(),"wb");
			fprintf(fph,"#VRML V2.0 utf8 generated by QBSP from %s\n",fileArg);

			printf("Saving U/V channel #1 to VRML2 file %s\n",name1.c_str());
			option.tex1= true;
			mesh->SaveVRML2(fph,option);
			
			q.SaveEntitiesVRML2(fph,option);
			
			fclose(fph);


			printf("Saving U/V channel #2 to VRML2 file %s.wrl\n",name2.c_str());
			name2+=".wrl";
			fph = fopen(name2.c_str(),"wb");
			fprintf(fph,"#VRML V2.0 utf8 generated by QBSP from %s\n",fileArg);
			option.tex1=false;
			option.matDefined=false;
			option.useMat = false;
			mesh->SaveVRML2(fph,option);
			fclose(fph);

		} else {
		    name1 = str;
			printf("Saving MultiTexture extended VRML file %s.wrl\n",name1.c_str());

		    String oname = name1+".wrl";
			FILE *fph = fopen(oname.c_str(),"wb");
			fprintf(fph,"#VRML V2.0 utf8 generated by Q3BSP 1.1 from %s\n",fileArg);
			fprintf(fph,"WorldInfo { title \"%s\" }\n\n",fileArg);
			
			fprintf(fph,"NavigationInfo { headlight TRUE avatarSize [0.25 1.3333 2 ]} \n");

			// write some PROTO's for effects 
			if (option.useEffects)
			{
				Fload effects("q3effects.wrl");
				if (!effects.GetData()) {
					printf("***********q3effects.wrl NOT FOUND\n");
				}
				else {
					fprintf(fph,"%s\n",(char *)effects.GetData());
					option.matDefined = true; // assume MAT is defined in this file
				}
			}
  

			option.tex1= true;
			if (option.useBsp) 
				q.SaveNodesBsp(fph,option);
			else mesh->SaveVRML2(fph,option);
			
			q.SaveEntitiesVRML2(fph,option);

			fclose(fph);
		}

	} else {	// VRML 1 style 
		printf("Saving U/V channel #1 to file %s.wrl\n",name1.c_str());
		mesh->SaveVRML(name1,true);
		printf("Saving U/V channel #2 to file %s.wrl\n",name2.c_str());
		mesh->SaveVRML(name2,false);
	}
  }
  else
  {
    printf("Failed to crunch %s\n", fileArg);
	return -1;
  }

  return 0;
  
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x378, %rsp            # imm = 0x378
leaq -0x1a0(%rbp), %rax
xorl %r14d, %r14d
movl %r14d, (%rax)
movq %r14, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %r14, 0x20(%rax)
leaq -0x170(%rbp), %rax
movl %r14d, (%rax)
movq %r14, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movb $0x0, 0x78(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rax)
movl $0x1, 0x30(%rax)
movabsq $0x100000100000008, %rcx # imm = 0x100000100000008
movq %rcx, 0x38(%rax)
movb $0x1, %cl
movb %cl, 0x40(%rax)
leaq 0xc85b(%rip), %rdx      # 0x126b2
movq %rdx, 0x48(%rax)
leaq 0xc853(%rip), %rdx      # 0x126b5
movq %rdx, 0x50(%rax)
leaq 0xc84e(%rip), %rdx      # 0x126bb
movq %rdx, 0x58(%rax)
movb %cl, 0x3e(%rax)
leaq 0xc84f(%rip), %rcx      # 0x126ca
movq %rcx, 0x60(%rax)
leaq 0xc870(%rip), %rcx      # 0x126f6
movq %rcx, 0x68(%rax)
movq %rcx, 0x70(%rax)
movl $0x1, %eax
cmpl $0x2, %edi
jl 0x5ead
movq 0x8(%rsi), %rcx
xorl %r14d, %r14d
xorl %eax, %eax
cmpb $0x2d, (%rcx)
sete %al
cmoveq %rcx, %r14
incl %eax
cmpl %edi, %eax
jge 0x647e
movl %eax, %eax
movq (%rsi,%rax,8), %rbx
testq %r14, %r14
je 0x5fa1
movq %r14, %rdi
movl $0x32, %esi
callq 0x2100
movb $0x1, %cl
testq %rax, %rax
jne 0x5eec
movq %r14, %rdi
movl $0x31, %esi
callq 0x2100
testq %rax, %rax
je 0x5ef2
xorl %ecx, %ecx
movb %cl, -0x140(%rbp)
movq %r14, %rdi
movl $0x6d, %esi
callq 0x2100
testq %rax, %rax
je 0x5f0b
movb $0x1, -0x13e(%rbp)
movq %r14, %rdi
movl $0x4d, %esi
callq 0x2100
testq %rax, %rax
je 0x5f24
movb $0x1, -0x132(%rbp)
movq %r14, %rdi
movl $0x6c, %esi
callq 0x2100
testq %rax, %rax
je 0x5f3d
movb $0x1, -0x133(%rbp)
movq %r14, %rdi
movl $0x62, %esi
callq 0x2100
testq %rax, %rax
je 0x5f56
movb $0x1, -0x13d(%rbp)
movq %r14, %rdi
movl $0x65, %esi
callq 0x2100
testq %rax, %rax
je 0x5f6f
movb $0x1, -0x131(%rbp)
movq %r14, %rdi
movl $0x76, %esi
callq 0x2100
testq %rax, %rax
je 0x5f88
movb $0x1, -0x13f(%rbp)
movq %r14, %rdi
movl $0x6e, %esi
callq 0x2100
testq %rax, %rax
je 0x5fa1
movb $0x1, -0x12f(%rbp)
callq 0x697e
movq %rax, %rdi
movq %rbx, %rsi
callq 0x6a12
movq %rax, -0xf0(%rbp)
callq 0x697e
leaq 0xc9f6(%rip), %rsi      # 0x129ba
movq %rax, %rdi
callq 0x6a12
leaq -0xa8(%rbp), %rdx
movq %rax, (%rdx)
leaq -0x3a0(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x865c
movq -0x280(%rbp), %r14
testq %r14, %r14
je 0x6202
leaq -0xa8(%rbp), %rdi
leaq -0xf0(%rbp), %rdx
movq %rbx, %rsi
callq 0x69c6
movq %rbx, %rdi
movl $0x5c, %esi
callq 0x2260
testq %rax, %rax
je 0x604e
movq %rax, %r15
incq %r15
leaq -0xa8(%rbp), %r12
movq 0x8(%r12), %r13
movq %r15, %rdi
callq 0x2120
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rdx
movq %r15, %rcx
movq %rax, %r8
callq 0x2360
leaq -0x38(%rbp), %r12
movq %r12, -0x10(%r12)
movq -0xa8(%rbp), %rsi
movq -0xa0(%rbp), %rdx
addq %rsi, %rdx
leaq -0x48(%rbp), %rdi
callq 0x6ca8
leaq 0xc1a5(%rip), %rsi      # 0x1221d
leaq -0x48(%rbp), %rdi
callq 0x2400
leaq -0x78(%rbp), %r13
movq %r13, -0x10(%r13)
movq -0xa8(%rbp), %rsi
movq -0xa0(%rbp), %rdx
addq %rsi, %rdx
leaq -0x88(%rbp), %rdi
callq 0x6ca8
leaq 0xc350(%rip), %rsi      # 0x123fd
leaq -0x88(%rbp), %rdi
callq 0x2400
cmpb $0x1, -0x140(%rbp)
jne 0x621d
cmpb $0x0, -0x13e(%rbp)
je 0x626c
leaq -0x48(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x2140
movq -0x48(%rbp), %rsi
leaq 0xc390(%rip), %rdi      # 0x1247e
xorl %eax, %eax
callq 0x2050
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
addq %rsi, %rdx
leaq -0x68(%rbp), %rdi
callq 0x6ca8
leaq 0xc428(%rip), %rsi      # 0x12540
leaq -0x68(%rbp), %rdi
callq 0x2400
movq -0x68(%rbp), %rdi
leaq 0xc198(%rip), %rsi      # 0x122c4
callq 0x22a0
movq %rax, %r15
leaq 0xc372(%rip), %rsi      # 0x124ad
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2300
leaq 0xc38e(%rip), %rsi      # 0x124dd
movq %r15, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2300
leaq 0xc395(%rip), %rdi      # 0x124f8
movl $0x3e, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x2410
cmpb $0x1, -0x131(%rbp)
jne 0x6387
leaq -0xb8(%rbp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0xc3a3(%rip), %rsi      # 0x12537
leaq 0xc3a9(%rip), %rdx      # 0x12544
leaq -0xc8(%rbp), %rdi
callq 0x6d42
leaq -0xf0(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x7b70
movq -0xc8(%rbp), %rdi
cmpq %rbx, %rdi
je 0x61d5
movq -0xb8(%rbp), %rsi
incq %rsi
callq 0x2230
movq -0xe8(%rbp), %rdx
testq %rdx, %rdx
je 0x636f
leaq 0xdae0(%rip), %rsi      # 0x13ccc
movq %r15, %rdi
xorl %eax, %eax
callq 0x2300
movb $0x1, -0xf8(%rbp)
jmp 0x637b
leaq 0xc388(%rip), %rdi      # 0x12591
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2050
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x6446
leaq -0x48(%rbp), %rbx
movq (%rbx), %rsi
leaq 0xc31a(%rip), %rdi      # 0x12545
xorl %eax, %eax
callq 0x2050
movq %r14, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0xfc02
leaq -0x88(%rbp), %rbx
movq (%rbx), %rsi
leaq 0xc318(%rip), %rdi      # 0x1256b
xorl %eax, %eax
callq 0x2050
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xfc02
jmp 0x63f8
leaq 0xc2cd(%rip), %rsi      # 0x12540
leaq -0x48(%rbp), %rdi
callq 0x2400
movq -0x48(%rbp), %rdi
leaq 0xc03d(%rip), %rsi      # 0x122c4
callq 0x22a0
movq %rax, %r15
leaq 0xc169(%rip), %rsi      # 0x123ff
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2300
movq -0x48(%rbp), %rsi
leaq 0xc17c(%rip), %rdi      # 0x1242a
xorl %eax, %eax
callq 0x2050
leaq -0x1a8(%rbp), %rdx
movb $0x1, 0x6c(%rdx)
movq %r14, %rdi
movq %r15, %rsi
callq 0xf054
leaq -0x3a0(%rbp), %rdi
leaq -0x1a8(%rbp), %rdx
movq %r15, %rsi
callq 0xa4dc
movq %r15, %rdi
callq 0x2210
leaq -0x88(%rbp), %r15
movq (%r15), %rsi
leaq 0xc158(%rip), %rdi      # 0x12452
xorl %eax, %eax
callq 0x2050
leaq 0xc238(%rip), %rsi      # 0x12540
movq %r15, %rdi
callq 0x2400
movq -0x88(%rbp), %rdi
leaq 0xbfa6(%rip), %rsi      # 0x122c4
callq 0x22a0
movq %rax, %r15
leaq 0xc0d2(%rip), %rsi      # 0x123ff
xorl %r12d, %r12d
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2300
leaq -0x1a8(%rbp), %rdx
movb %r12b, 0x6c(%rdx)
movb %r12b, 0xb0(%rdx)
movb %r12b, 0x76(%rdx)
movq %r14, %rdi
movq %r15, %rsi
callq 0xf054
movq %r15, %rdi
callq 0x2210
leaq -0x38(%rbp), %r12
jmp 0x63f8
leaq 0xc3b3(%rip), %rdi      # 0x12729
callq 0x2350
leaq -0xf0(%rbp), %rdi
callq 0x7c62
movb $0x1, -0x13c(%rbp)
cmpb $0x1, -0x13d(%rbp)
jne 0x63af
leaq -0x3a0(%rbp), %rdi
leaq -0x1a8(%rbp), %rdx
movq %r15, %rsi
callq 0xa83a
jmp 0x63c1
leaq -0x1a8(%rbp), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0xf054
leaq -0x3a0(%rbp), %rdi
leaq -0x1a8(%rbp), %rdx
movq %r15, %rsi
callq 0xa4dc
movq %r15, %rdi
callq 0x2210
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x63f8
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2230
movq -0x88(%rbp), %rdi
cmpq %r13, %rdi
je 0x6410
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x2230
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
je 0x6425
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2230
leaq -0x98(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6444
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x2230
xorl %ebx, %ebx
leaq -0x3a0(%rbp), %rdi
callq 0x95a0
leaq -0x178(%rbp), %rdi
callq 0x6c4a
leaq -0x1a8(%rbp), %rdi
callq 0x6c4a
movl %ebx, %eax
addq $0x378, %rsp            # imm = 0x378
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc2c7(%rip), %rdi      # 0x1274c
callq 0x2350
leaq 0xc2de(%rip), %rdi      # 0x1276f
callq 0x2350
leaq 0xc308(%rip), %rdi      # 0x127a5
callq 0x2350
leaq 0xc330(%rip), %rdi      # 0x127d9
callq 0x2350
leaq 0xc363(%rip), %rdi      # 0x12818
callq 0x2350
leaq 0xc394(%rip), %rdi      # 0x12855
callq 0x2350
leaq 0xc3c3(%rip), %rdi      # 0x12890
callq 0x2350
leaq 0xc3f7(%rip), %rdi      # 0x128d0
callq 0x2350
leaq 0xc423(%rip), %rdi      # 0x12908
callq 0x2350
leaq 0xc457(%rip), %rdi      # 0x12948
callq 0x2350
leaq 0xc47f(%rip), %rdi      # 0x1297c
callq 0x2350
leaq 0xc4b3(%rip), %rdi      # 0x129bc
callq 0x2350
leaq 0xc4be(%rip), %rdi      # 0x129d3
callq 0x2350
leaq 0xc4e6(%rip), %rdi      # 0x12a07
callq 0x2350
leaq 0xc4ec(%rip), %rdi      # 0x12a19
callq 0x2350
leaq 0xc51a(%rip), %rdi      # 0x12a53
callq 0x2350
leaq 0xc548(%rip), %rdi      # 0x12a8d
callq 0x2350
leaq 0xc546(%rip), %rdi      # 0x12a97
callq 0x2350
leaq 0xc54d(%rip), %rdi      # 0x12aaa
callq 0x2350
leaq 0xc553(%rip), %rdi      # 0x12abc
callq 0x2350
leaq 0xc562(%rip), %rdi      # 0x12ad7
callq 0x2350
movl $0x1, %edi
callq 0x22c0
movq %rax, %r14
movq -0xc8(%rbp), %rdi
cmpq %rbx, %rdi
je 0x65ad
movq -0xb8(%rbp), %rsi
incq %rsi
callq 0x2230
jmp 0x65ad
jmp 0x65aa
jmp 0x65aa
jmp 0x65eb
movq %rax, %r14
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x65ee
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2230
jmp 0x65ee
jmp 0x65e1
jmp 0x65eb
jmp 0x65eb
movq %rax, %r14
jmp 0x6606
movq %rax, %r14
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
jne 0x6613
jmp 0x661f
movq %rax, %r14
jmp 0x661f
movq %rax, %r14
jmp 0x663e
movq %rax, %r14
movq -0x88(%rbp), %rdi
cmpq %r13, %rdi
je 0x6606
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x2230
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x661f
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2230
leaq -0x98(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x663e
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x2230
leaq -0x3a0(%rbp), %rdi
callq 0x95a0
jmp 0x6651
jmp 0x664e
movq %rax, %r14
leaq -0x178(%rbp), %rdi
callq 0x6c4a
leaq -0x1a8(%rbp), %rdi
callq 0x6c4a
movq %r14, %rdi
callq 0x23c0