#Takes a ms3d ascii vertex list and puts it into a table meant for use with ActorMultiVertex. -Kid
import sys
import os

def main():
	gotfile = 0
	if len(sys.argv) > 1:
		if sys.argv[1] == '-h':
			print("Translates ms3d ascii triangle list into SM5 AMV table for SetVertices().")
			print("USAGE: {} <INFILE>".format(sys.argv[0]))
			sys.exit()
		else:
			gotfile = 1
			filein = sys.argv[1]

	if not gotfile:
		#Grab the filename from the user.
		filein = raw_input("input file: ")

	#Does the file exist? If not, repeat until we have something valid.
	while not os.path.isfile(filein):
		print("File {} does not exist. Try again.".format(filein))
		filein = raw_input("Input file: ")

	#open something for output.
	outputf = raw_input("Output file (Overwrites given file, default: \"output-tri.lua\"): ")
	if not outputf:
		outputf = "output-tri.lua"
	fileout = open(outputf,'w')

	arrayname = raw_input("input vertex array name (default: \"vertex\"): ")
	if not arrayname:
		arrayname = "vertex"

	#We're sure we have something that exists. Begin the task.
	with open(filein) as fp:
		fileout.write("--Made with ms3dascii-tri2amvtable.py! -Kid\n")
		fileout.write("return {\n")#What I intend to do is have a function that returns a table for SetVertices.
		for line in fp:
			argument = line.split()
			#print("Flag: {}, vert1: {}, vert2: {}, vert3: {}, norm: {}, smoothing group: {}".format(argument[0],argument[1],argument[2],argument[3],argument[4],argument[7]))
			#note: vertex indices are incremented by 1 to work with a lua table made by ms3dascii-vert2amvtable.py

			#print("Flag: {}, vert1: {}, vert2: {}, vert3: {}, norm: {}, smoothing group: {}".format(argument[0],int(argument[1])+1,int(argument[2])+1,int(argument[3])+1,argument[4],argument[7]))


			fileout.write("    {}[{}],{}[{}],{}[{}],--norm: {}, smoothing group: {}\n".format(arrayname,int(argument[1])+1,arrayname,int(argument[2])+1,arrayname,int(argument[3])+1,argument[4],argument[7]))
		fp.close()#Close the file. It's common courtesy.
		fileout.write("}")#Close off the output.

	print("Results output to \"{}\"".format(outputf))
	fileout.close()#One last close.

if __name__ == '__main__':
	main()
