extends MeshInstance

var signature
var u0
var xoffset
var yoffset
var minheight
var maxheight
var heightscale
var xverticesnumber
var yverticesnumber
var stretch
var u1
var u2
var u3
var u4
var version
var u6
var u7
var u8
var tex

var edges
var file = File.new()

var vertices = []
var triangles = []

func _on_MeshInstance_ready():
	readgti("res://default.gti")
	var st = SurfaceTool.new()
	st.begin(Mesh.PRIMITIVE_TRIANGLES)
	for v in triangles:
		st.add_color(Color(v[4] / 255.0, v[5] / 255.0, v[6] / 255.0))
		st.add_vertex(Vector3(v[0], v[2], v[1]))
		
	var m = st.commit()
	mesh = m

func fill():
	# fill read values
	# print("fill")
	var p = 0
	while p < xverticesnumber * yverticesnumber:
		var b = file.get_8()
		if b >= 0x80:
			while b != 0x00:
				if p >= xverticesnumber * yverticesnumber:
					return
				b = (b + 1) & 0xFF
				p+=1
		else:
			while b != 0xff:
				if p >= xverticesnumber * yverticesnumber:
					return
				if version != 7:
					vertices[p][2] = file.get_float() * heightscale # height
					vertices[p][3] = file.get_8() # triangulation
					vertices[p][4] = file.get_8() # R
					vertices[p][5] = file.get_8() # G
					vertices[p][6] = file.get_8() # B
				else:
					vertices[p][2] = file.get_float() * heightscale # height
					vertices[p][3] = file.get_8() # triangulation
					vertices[p][4] = file.get_8() # R
					vertices[p][5] = file.get_8() # G
					vertices[p][6] = file.get_8() # B
					var unk = file.get_8() # unk
				# print("["+ str(vertices[p][0]) + ", "+ str(vertices[p][1])+", "+ str(vertices[p][2])+", "+ str(vertices[p][3])+", "+ str(vertices[p][4])+", "+ str(vertices[p][5])+", "+ str(vertices[p][6])+", "+str(b)+"]")
				p+=1
				b = (b - 1) & 0xFF

func readgti(filename):
	file.open(filename, File.READ)
	if not file.is_open():
		get_tree().quit()
		return
	signature = file.get_32()
	u0 = file.get_32()
	xoffset = file.get_float()
	yoffset = file.get_float()
	minheight = file.get_float()
	maxheight = file.get_float()
	xverticesnumber = file.get_32()
	yverticesnumber = file.get_32()
	stretch = file.get_float()
	stretch = 40.0
	heightscale = stretch/40.0 # normal stretch
	u1 = file.get_float()
	u2 = file.get_float()
	u3 = file.get_float()
	u4 = file.get_float()
	version = file.get_16()
	u6 = file.get_16()
	u7 = file.get_float()
	u8 = file.get_float()
	tex = file.get_buffer(32).get_string_from_ascii()
	
	vertices.resize(xverticesnumber * yverticesnumber)
	
	# initialize empty terrain
	# print("init")
	for y in range(yverticesnumber):
		for x in range(xverticesnumber):
			vertices[y * xverticesnumber + x] = []
			vertices[y * xverticesnumber + x].append(xoffset + x * stretch) # 0: x
			vertices[y * xverticesnumber + x].append(yoffset + y * stretch) # 1: y
			vertices[y * xverticesnumber + x].append(minheight) # 2: height
			vertices[y * xverticesnumber + x].append(0) # 3: triangulation value
			vertices[y * xverticesnumber + x].append(0) # 4: R
			vertices[y * xverticesnumber + x].append(0) # 5: G
			vertices[y * xverticesnumber + x].append(0) # 6: B
			
	
	fill()
	file.close()
	
	# sort triangles
	# print("sort")
	var i = 0
	# triangles.resize(2*len(vertices))
	for y in range(yverticesnumber-1):
		for x in range(xverticesnumber-1):
			var b = (vertices[y * xverticesnumber + x][3] & 7) % 128
			if b == 4 or b == 5 or b == 7:
				triangles.append(vertices[(y+1) * xverticesnumber + x])
				triangles.append(vertices[y * xverticesnumber + x])
				triangles.append(vertices[(y+1) * xverticesnumber + (x+1)])
			if b == 5 or b == 3:
				triangles.append(vertices[y * xverticesnumber + (x + 1)])
				triangles.append(vertices[y * xverticesnumber + x])
				triangles.append(vertices[(y + 1) * xverticesnumber + (x + 1)])
	for y in range (yverticesnumber-1):
		for x in range(xverticesnumber-1):
			var b = (vertices[y * xverticesnumber + x][3] & 7) % 128
			if b == 1 or b == 6:
				triangles.append(vertices[(y + 1) * xverticesnumber + x])
				triangles.append(vertices[y * xverticesnumber + x])
				triangles.append(vertices[y * xverticesnumber + (x + 1)])
			if b == 2 or b == 6:
				triangles.append(vertices[y * xverticesnumber + (x + 1)])
				triangles.append(vertices[(y + 1) * xverticesnumber + x])
				triangles.append(vertices[(y + 1) * xverticesnumber + (x + 1)])
