extends AIScriptModule


# Declare member variables here. Examples:
# var a = 2
# var b = "text"


# Called when the node enters the scene tree for the first time.
var root
var skel
var hair_skel

var garments = ["female-panties1", "mystress-dress", "mystress-necklace"]
var garments_head = []
var basedir = "res://scenes/clothes/"
var material = preload("res://scenes/clothes/mystress_clothes.material")
#var tween: Tween
#enum {
#	IDLE,
#	WALKING
#	PRAYING,
#	PRAYING_STARTLED,
#	ACTION
#}
#var state = PRAYING
func init(tick):
	root = get_character(tick)
	assert(root.has_meta("skeleton"))
	root.add_to_group("mystress")
#	tween = Tween.new()
#	tween.playback_process_mode = Tween.TWEEN_PROCESS_PHYSICS
#	tween.process_priority = 30
#	add_child(tween)
#	call_deferred("setup_markers")
	characters.call_deferred("setup_garments", root, garments, garments_head, material)
#func setup_markers():
#	var cmdq = []
#	if root.has_meta("cmdqueue"):
#		cmdq = root.get_meta("cmdqueue")
#	cmdq.push_back(["hide_marker", "head/marker_talk"])
#	cmdq.push_back(["hide_marker", "head/marker_hips_action"])
#	cmdq.push_back(["register_marker", self, "hips/marker_dagger_sacrifice",
#		{
#			"method": "dagger_sacrifice"
#		}
#	])
#	cmdq.push_back(["hide_marker", "hips/marker_dagger_sacrifice"])
#	cmdq.push_back(["anim_state", "pray"])
#	root.set_meta("cmdqueue", cmdq)
#	state = PRAYING
#var state_changed = 0.0
func set_orientation(v: Transform):
	root.set_meta("orientation", v)
func set_basis_dir(dir: Vector3):
	var xdir: Vector3 = dir
	xdir.y = 0
	var d0 = xdir.cross(Vector3.UP)
	var d1 = Vector3.UP
	var d2 = xdir
	var basis = Basis(d0, d1, -d2)
	var orientation = root.get_meta("orientation")
	orientation.basis = basis
	root.set_meta("orientation", orientation)
var state = 0
var sw_delay = 0.0
var oldx = Vector3()
func get_dest(pt: Vector3, exclusions: Array) -> Vector3:
	var ret = pt
	var o = root.global_transform.origin
	var offt = Vector3.UP * 0.3
	var space: PhysicsDirectSpaceState = root.get_world().get_direct_space_state()
	var r = space.intersect_ray(o + offt, pt + offt, exclusions)
	if !r.empty():
		ret = r.position - offt
	return ret
func update_physics(tick, delta):
	var cmdq = []
	if !skel:
		skel = root.get_meta("skeleton")
	var cam = root.get_viewport().get_camera()
	if !cam:
		return
	if !cam.has_meta("player"):
		return
	var space: PhysicsDirectSpaceState = root.get_world().get_direct_space_state()
	var o = root.global_transform.origin
	var p = cam.get_meta("player").global_transform
	var orientation = Transform(root.global_transform.basis, Vector3())
	var dir = orientation.xform(Vector3(0, 0, -1))
	var dp = dir.dot(p.origin - o)
	if oldx.length() == 0:
		oldx = p.origin
	var v = (p.origin - oldx) / delta
	v.y = 0
	var d = p.origin.distance_squared_to(root.global_transform.origin)
	if root.has_meta("cmdqueue"):
		cmdq = root.get_meta("cmdqueue")
	sw_delay -= delta
	match state:
		0:
			if d > 30.0:
				state = 30
			if d > 4.5 * 4.5 && v.length() == 0 && sw_delay < 0.0:
				state = 10
			elif d < 2.5 * 2.5 && v.length() > 0 && dp > 0:
				state = 20
			elif d < 1.0 * 1.0 && v.length() > 0:
				var here = (o - p.origin).normalized()
				if v.dot(here) > 0:
					state = 22
		10:
			if !root.has_meta("cmd_walk"):
				state = 11
		11:
			var dest = get_dest(p.origin, [root, cam.get_meta("player")])
			cmdq.push_back(["walkto", dest, 2.5, 15])
			state = 12
		12:
			if !root.has_meta("cmd_walk"):
				state = 0
		20:
			if !root.has_meta("cmd_walk"):
				state = 21
		21:
			var pt = (root.global_transform.origin - p.origin).normalized() * 5 + root.global_transform.origin
			var ddir = characters.locnav_probe(root, 2.5, (pt - o).normalized(), 0.2)
			var dpt = ddir + o
			var dest = get_dest(dpt, [root, cam.get_meta("player")])
			print("from: ", o, " to: ", dest, " ddir: ", ddir)
			cmdq.push_back(["walkto", dest, 1.5, 15])
			if dest != pt:
				sw_delay = 1.5
			else:
				sw_delay = 5.0
			state = 12
		22:
			if dp > 0 && dp < 0.9999:
				var tf = root.global_transform
				tf = tf.looking_at(p.origin, Vector3.UP)
				orientation.basis = orientation.basis.slerp(tf.basis, 0.3)
				root.set_meta("orientation", orientation)
			elif dp > 0:
				orchestration.grab(cam.get_meta("player"), root)
				state = 23
		23:
			if d > 5.0 * 5.0:
				state = 0
		30:
			if d < 20.0:
				state = 0
	root.set_meta("cmdqueue", cmdq)
	oldx = p.origin
	return ERR_BUSY
#	print("state =", state)
#	var px = p.origin
#	px.y = root.global_transform.origin.y
#	var pdir = px - root.global_transform.origin
#	var fdir = -root.global_transform.basis[2]
#	fdir.y = 0
#	pdir.y = 0
#	pdir = pdir.normalized()
#	fdir = fdir.normalized()
#	var cmdq = []
#	if root.has_meta("cmdqueue"):
#		cmdq = root.get_meta("cmdqueue")
#	state_changed -= delta
#	state_changed = clamp(state_changed, 0.0, 10.0)
#	match state:
#		PRAYING:
#			if root.has_meta("req_state") && root.get_meta("req_state") in ["TALK", "ACTION"]:
#				state = ACTION
#				cmdq.push_back(["anim_state", "locomotion"])
#				root.set_meta("cmdqueue", cmdq)
#			elif d < 2.0 && state_changed <= 0.0:
#				if pdir.angle_to(fdir) > PI / 8.0:
#					tween.interpolate_method(self, "set_basis_dir", fdir, pdir, 0.3 + randf() * 0.6, Tween.TRANS_CUBIC)
#					tween.start()
#					state_changed = 0.5
#				else:
#					cmdq.push_back(["anim_state", "pray-startled"])
##					cmdq.push_back(["anim_state", "locomotion"])
##					cmdq.push_back(["anim_param", "state/locomotion/loc/blend_position", Vector2(0.5, 0)])
#					root.set_meta("cmdqueue", cmdq)
#					state = PRAYING_STARTLED
#					state_changed = 3.0
#		PRAYING_STARTLED:
#			if root.get_meta("req_state") in ["TALK", "ACTION"]:
#				state = ACTION
#				cmdq.push_back(["anim_state", "locomotion"])
#				root.set_meta("cmdqueue", cmdq)
#			elif d > 4.0 && state_changed <= 0.0:
#				cmdq.push_back(["anim_state", "pray"])
#				root.set_meta("cmdqueue", cmdq)
#				state = PRAYING
#				state_changed = 4.0
#			else:
#				var f = root.global_transform.origin + Vector3.UP * 0.5
#				var t = f + root.global_transform.basis[2] * 0.22
#				var result = space.intersect_ray(f, t, [root])
#				if result.has("collider"):
#					cmdq.push_back(["anim_state", "pray"])
#					root.set_meta("cmdqueue", cmdq)
#					state = PRAYING
#					state_changed = 2.0
#		ACTION:
#			if !root.has_meta("req_state"):
#				state = PRAYING
#
##			elif d <= 4.0 || state_changed > 0.0:
##				tween.interpolate_method(self, "set_orientation", root.get_meta("orientation"), root.get_meta("orientation").looking_at(px, Vector3(0, 1, 0)), 0.3 + randf() * 0.6, Tween.TRANS_CUBIC)
##				tween.start()
##	var sacrifice_marker = skel.get_node("hips/marker_dagger_sacrifice")
##	if sacrifice_marker.visible && !inventory.items.s_dagger:
##		sacrifice_marker.hide()
##	elif !sacrifice_marker.visible && inventory.items.s_dagger:
##		sacrifice_marker.show()
#
##func dagger_sacrifice(data):
##	print("dagger sacrifice")
##	var cmdq = []
##	var other = group_manager.player
##	if other.has_meta("cmdqueue"):
##		cmdq = other.get_meta("cmdqueue")
##	cmdq.push_back(["anim_param", "blade_right/blend_amount", 1.0])
##	cmdq.push_back(["equip", "wrist_r/weapon_right", "s_dagger"])
##	other.set_meta("cmdqueue", cmdq)
##	group_manager.submit_player_npc_event_arot(root, "sacrificed-a", root, "sacrificed", root)
func update(tick, delta):
	return ERR_BUSY
