-- Author   "Drigien"
-- Version: "0.2 alpha"
-- About:   "This script imports the Lithtech ASCII format into a 3ds mesh"

fn ReadMatrix3 mat3 input =
(
	r1 = [ 0, 0, 0 ]
	r2 = [ 0, 0, 0 ]
	r3 = [ 0, 0, 0 ]
	r4 = [ 0, 0, 0 ]
	
	r1.x = readValue input
	r2.x = readValue input
	r3.x = readValue input
	r4.x = readValue input
	skipToNextLine input
	
	skipToString input "("
	r1.z = readValue input
	r2.z = readValue input
	r3.z = readValue input
	r4.z = readValue input
	skipToNextLine input

	skipToString input  "("	
	r1.y = readValue input
	r2.y = readValue input
	r3.y = readValue input
	r4.y = readValue input

	mat3.row1 = r1
	mat3.row2 = r2
	mat3.row3 = r3
	mat3.row4 = r4
	
)

fn ReadBones input parnt node_matrix_array node_parent_array =
(	
	skipToString input "(transform"
 	skipToString input "\""
	
 	/* Read Bone Name */
 	bone_name = readDelimitedString input "\""
 	print bone_name
	
 	/* Skip to Matrix */
 	skipToNextLine input
 	skipToNextLine input
 	skipToNextLine input
 	new_node = box name:"node"
 	TMatrix = new_node.transform

 	str = filePos input
 	skipToString input "("
 	end = filePos input
 	
 	ReadMatrix3 TMatrix input
 	new_node.transform = TMatrix
	delete new_node
 	append node_matrix_array ( TMatrix /*new_node*/ )
 	append node_parent_array ( parnt )
	parnt = node_parent_array.Count
 	print TMatrix
 	
 	skipToNextLine input
 	skipToNextLine input
 	skipToNextLine input
 	skipToNextLine input
 	child = readLine input
	--break()
 	if findString child "(children" != undefined then
 	(
 		Tpos = filePos input
 		skipToNextLine input
 		trans = readLine input
		--break()
 		while findString trans "(transform" != undefined do
 		(
			parnt = parnt
			parnet = parnt
 			seek input Tpos
 			ReadBones input parnet node_matrix_array node_parent_array
 			Tpos = filePos input
 			trans = readLine input
			--break()
 		)
 		skipToNextLine input
 		skipToNextLine input
 	)
)	

fn ReadFirstBone input parnt node_matrix_array node_parent_array =
(	
 	skipToString input "(transform"
  	skipToString input "\""
	
  	/* Read Bone Name */
  	bone_name = readDelimitedString input "\""
  	print bone_name
	
  	/* Skip to Matrix */
  	skipToNextLine input
  	skipToNextLine input
  	skipToNextLine input
  	new_node = box name:"node"
  	TMatrix = new_node.transform

  	str = filePos input
  	skipToString input "("
  	end = filePos input
 	
  	ReadMatrix3 TMatrix input
  	new_node.transform = TMatrix
 	delete new_node
  	append node_matrix_array ( TMatrix /*new_node*/ )
  	append node_parent_array ( parnt )
  	print TMatrix
 	
  	skipToNextLine input
  	skipToNextLine input
  	skipToNextLine input
  	skipToNextLine input
  	child = readLine input
 	--break()
  	if findString child "(children" != undefined then
  	(
  		Tpos = filePos input
  		skipToNextLine input
  		trans = readLine input
 		--break()
 		while findString trans "(transform" != undefined do
  		(
   			seek input Tpos
 			ReadBones input parnt node_matrix_array node_parent_array
 	 		Tpos = filePos input
 	 		trans = readLine input
	 		--break()
 	 	)
 	  	skipToNextLine input
 		skipToNextLine input
  	) 
	
  	else
 		parnt = parnt-1	
)

/*macroScript ImportLTA category:"LTA"*/
/*(*/

	undo off 
	(
	output = false /*Toggles Mesh Info Output*/
	
	node_name_array = #()
	node_matrix_array = #()
	node_parent_array = #()
	bone_array = #()
	
	name_array = #()
	mesh_array = #()
	in_name = getOpenFileName caption:"Import Lta Model" initialDir:"$scripts" types:"LithTech Model(*.lta)|*.lta|All|*.*|"
	
	if in_name != undefined then
	(
		in_file = openFile in_name		
		if in_file != undefined then
		(
				/* Read in Node Names */
				skipToString in_file "set-node-flags"
				check = " "
				brace = ")"
				while check != brace do
				(
					/*("Scene Root" 0 )*/
					skipToString in_file "(\""
					node_name = readDelimitedString in_file "\""
					append node_name_array ( node_name )
					
					skipToNextLine in_file
					line =  readChars in_file 4
					check = readChar in_file				
				)
				for i=1 to node_name_array.count do
				(
					print node_name_array[i]
				)		
				
				/* Setting up recursive call */
				skipToString in_file "(hierarchy"
				skipToNextLine in_file
				skipToNextLine in_file
				skipToNextLine in_file
				parnt = 1	
				ReadFirstBone in_file parnt node_matrix_array node_parent_array
				/**/
	
				print "======= END OF ARRAY ======="
				seek in_file 0
				
		
				count = 1
				skip = skipToString in_file "mesh \""
				while skip != undefined do
				(
				vert_array =   #()
				face_array =   #()
				normal_array = #()
				uv_array =     #()
				
				add_mesh = true
				mesh_name = readDelimitedString in_file "\""
				
				/* CHECK IF ITS JUST AN LOD */
				c = mesh_name.count - 2				
				s = substring mesh_name 1 c
				for i=1 to name_array.count do
					if s == name_array[i] then
						add_mesh = false
						
				if add_mesh == true then
				(
				if output == true then
					print mesh_name
					
				append name_array ( mesh_name )
				
				/* READ IN VERTICIES */
				skipToNextLine in_file
				line = readLine in_file
				line = readLine in_file	
				
				line =  readChars in_file 6
				check = " "/*readChar in_file*/
				brace = ")"
				while check != brace do
				(
					check = readChar in_file
					x = readValue in_file
					y = readValue in_file 
					z = readValue in_file
					/*format "% % % \n" x z y*/	
					append vert_array ( [x, z, y] )					
					
					skipToNextLine in_file								
					line =  readChars in_file 5
					check = readChar in_file
				)
				if output==true then
					format "Number of Vertices %\n" vert_array.count
				--End Verts
				
				
				/* READ IN NORMALS */
				skipToString in_file "normals"
				skipToNextLine in_file
				skipToNextLine in_file
				
				line =  readChars in_file 5
				check = readChar in_file
				
				line =  readChars in_file 6
				check = " "/*readChar in_file*/
				brace = ")"
				while check != brace do
				(
				 	check = readChar in_file
				 	x = readValue in_file
				 	y = readValue in_file 
				 	z = readValue in_file					
				 	/*format "% % % \n" x z y*/	
				 	append normal_array ( [x, z, y] )					
					
				 	skipToNextLine in_file								
				 	line =  readChars in_file 5
				 	check = readChar in_file
				)
				--End Normals
				
			    /* READ IN UVS */
				skipToString in_file "uvs"
				skipToNextLine in_file
				skipToNextLine in_file
				
				line =  readChars in_file 5
				check = readChar in_file
				
				line =  readChars in_file 6
				check = " "/*readChar in_file*/
				brace = ")"
				while check != brace do
				(
				 	check = readChar in_file
				 	x = readValue in_file
				 	y = readValue in_file 
				 	z = 0.0
					
					y = 1-y
				
				 	--format "% % % \n" x z y
				 	append uv_array ( [x, y, z] )					
					
				 	skipToNextLine in_file								
				 	line =  readChars in_file 5
				 	check = readChar in_file
				)
				--End UVs

				
				/* READ IN FACES */
				skipToString in_file "tri-fs"
				skipToNextLine in_file	
				
				line =  readChars in_file 5
				check = readChar in_file
				pos = filePos in_file
				check = readValue in_file
				
				brace = ")"
				high = "9"
				low  = "0"
				check = readChar in_file
				while check >= low and check <=high do
				(
					seek in_file pos
					x = readValue in_file					
					y = readValue in_file					
					z = readValue in_file
					/*format "% % % \n" x y z*/
					append face_array ( [x+1, y+1, z+1] )
					
					pos = filePos in_file - 1
					check = readChar in_file
				)
				if output==true then
					format "Number of Faces: % \n" face_array.count
				
				/* STORE IN NEW MESH */
				new_mesh = mesh vertices:vert_array faces:face_array
				new_mesh.name = mesh_name
				
				for v = 1 to new_mesh.numVerts do
					setNormal new_mesh v normal_array[v]
					
				/* FLIP ALL NORMALS */
				for f=1 to new_mesh.numFaces do --for each face in the mesh
				(
					verts=getface new_mesh f -- get its 3 vertices as a point3
					local tmp=verts.x -- swap the first and third vertices
					verts.x=verts.z -- which flips the normal
					verts.z=tmp -- (right-hand rule), and 
					setface new_mesh f verts -- store the new vertex order for the face
				)					
				
				/* STORE UVS IN MESH */
				theChannel = 1
				meshop.setMapSupport new_mesh theChannel true
				meshop.setNumMapVerts new_mesh theChannel uv_array.count
				for f=1 to face_array.count do
				(	
					x = f*3-2
					y = f*3-1
					z = f*3-0
					
					face = getFace new_mesh f
					setTVert new_mesh face.z uv_array[x]
					setTVert new_mesh face.y uv_array[y]
					setTVert new_mesh face.x uv_array[z]
				)
				--End Store UVs
				update new_mesh
				
				/* ADD TO ARRAY OF MESHES*/
				append mesh_array ( new_mesh )
				update mesh_array[ count ]
				count += 1
				)
				
				/* CHECK TO SEE IF THERES MORE MESHES */
				skip = skipToString in_file "mesh \""
				)
				
				/* Read in Node Matrixs */
				seek in_file 0
				skipToString in_file "children"
				check = " "
				brace = ")"
				
				--format "Name Array: % \n" node_name_array.count
				--format "Parent Array: % \n" node_parent_array.count
				--format "Matrix Array: % \n" node_matrix_array.count
				for i=1 to node_name_array.count do
				(
					parent = node_parent_array[i]
					--format "Parent: % \n" parent
					
					TMatrix = node_matrix_array[i]
					--print TMatrix
					
					b0 = 
					if i>1 then
					(
						zAxis = [ 0, 0, 0 ]
						--BoneSys.createBone node_matrix_array[parent].row4 node_matrix_array[i].row4 zAxis
						in bone_array[parent] b1 = 	bone pos:TMatrix.row4 name:node_name_array[i]
						b1.transform = TMatrix
						b1.SetBoneEnable true 0
						b1.BoneAutoAlign = true
						b1.BoneFreezeLength = true
						b1.ShowLinks = true
						b1.ShowLinksOnly = true
						append bone_array ( b1 )
						--format "Bone: %(%) Parent: %(%) \n" b1.name i bone_array[parent].name parent

					)					
					else
					(
						zAxis = [ 0, 0, 0 ]
						--BoneSys.createBone node_matrix_array[i].row4 node_matrix_array[i].row4 zAxis
						b0 = bone pos:TMatrix.row4
						b0.transform = TMatrix
						b0.SetBoneEnable true 0
						b0.BoneAutoAlign = true
						b0.BoneFreezeLength = true
						b0.ShowLinks = true
						b0.ShowLinksOnly = true
						append bone_array ( b0 )
						--format "Bone: %(%) Parent: %(%) \n" b0.name i bone_array[parent].name parent
					)
				)
				for i=1 to node_parent_array.count do
				(
					format "Node: % \t % Parent: % \n" i node_name_array[i]  node_parent_array[i]
				)
			close in_file
		)
	)
	)
/*)*/
	