extends Spatial

var playback = {
	"init": {
		"slave": [
			["travel", "parameters/main/playback", "SmartObject"],
			["travel", "parameters/main/SmartObject/playback", "bed"],
			["travel", "parameters/main/SmartObject/bed/playback", "bed_facing_up"],
			["set", "parameters/free_grabbed/current", 0]
		]
	},
	"engage": {
		"master": [
			["travel", "parameters/main/playback", "SmartObject"],
			["travel", "parameters/main/SmartObject/playback", "bed"],
			["travel", "parameters/main/SmartObject/bed/playback", "bed_start_m"],
			["set", "parameters/free_grabbed/current", 0]
		],
		"slave": [
			["travel", "parameters/main/playback", "SmartObject"],
			["travel", "parameters/main/SmartObject/playback", "bed"],
			["travel", "parameters/main/SmartObject/bed/playback", "bed_start"],
			["set", "parameters/free_grabbed/current", 0]
		]
	},
	"torture": {
		"master": [
			["travel", "parameters/main/playback", "SmartObject"],
			["travel", "parameters/main/SmartObject/playback", "bed"],
			["travel", "parameters/main/SmartObject/bed/playback", "beating_m"],
			["set", "parameters/free_grabbed/current", 0]
		],
		"slave": [
			["travel", "parameters/main/playback", "SmartObject"],
			["travel", "parameters/main/SmartObject/playback", "bed"],
			["travel", "parameters/main/SmartObject/bed/playback", "beating"],
			["set", "parameters/free_grabbed/current", 0]
		]
	},
	"force": {
		"master": [
			["travel", "parameters/main/playback", "SmartObject"],
			["travel", "parameters/main/SmartObject/playback", "bed"],
			["travel", "parameters/main/SmartObject/bed/playback", "forcing_m"],
			["set", "parameters/free_grabbed/current", 0]
		],
		"slave": [
			["travel", "parameters/main/playback", "SmartObject"],
			["travel", "parameters/main/SmartObject/playback", "bed"],
			["travel", "parameters/main/SmartObject/bed/playback", "forcing"],
			["set", "parameters/free_grabbed/current", 0]
		]
	},
	"force_s1": {
		"master": [
			["travel", "parameters/main/playback", "SmartObject"],
			["travel", "parameters/main/SmartObject/playback", "bed"],
			["travel", "parameters/main/SmartObject/bed/playback", "forcing_s1_m"],
			["set", "parameters/free_grabbed/current", 0]
		],
		"slave": [
			["travel", "parameters/main/playback", "SmartObject"],
			["travel", "parameters/main/SmartObject/playback", "bed"],
			["travel", "parameters/main/SmartObject/bed/playback", "forcing_s1"],
			["set", "parameters/free_grabbed/current", 0]
		]
	}
}
var state = "init"
var active_state = ""
var bodies = []
func entered(body):
	if body.is_in_group("characters"):
			if body.get_meta("grabbing"):
				bodies.push_back(body)
				global.smart_object.push_back(body)
				print("entered:", body.name)
			elif !body.get_meta("grabbed"):
				bodies.push_back(body)
func exited(body):
	if body in bodies:
		print("exited:", body.name)
		bodies.erase(body)
		global.smart_object.erase(body)
var move_queue = []
var captured = []
func capture_slave(ch, orig_owner):
	ch.add_collision_exception_with(orig_owner)
	ch.set_meta("smart_object", true)
	ch.set_meta("orig_owner", get_path_to(orig_owner))
	for place in get_children():
		if place.name.begins_with("place"):
			if !place.has_meta("busy"):
				move_queue.push_back([ch, place])
				place.set_meta("busy", ch)
				print("place: ", place.name)
				break
		captured.push_back(get_path_to(ch))
func free_slave(ch):
	var orig_owner = ch.get_meta("orig_owner")
	ch.remove_collision_exception_with(get_node(orig_owner))
	ch.set_meta("smart_object", false)
	ch.remove_meta("orig_owner")
var activate_delay = 0.0
func activate():
	if activate_delay > 0.0:
		return
	if state in ["init", "engage", "torture"]:
		activate_state()
		activate_delay += 0.5
func activate_state():
#	if active_state == state:
#		return
	print("ACTIVATE ", state)
	match(state):
		"init":
			for k in bodies:
				var ch = grabbing.get_grabbed(k)
				if !ch:
					continue
				var s = playback[state]["slave"]
				print("ungrab")
				print("SMART: ", s)
				grabbing.ungrab_character(k, s)
				capture_slave(ch, k)
				state = "engage"
		"engage":
			for k in bodies:
				if k.get_meta("smart_object"):
					continue
				for place in get_children():
					if place.name.begins_with("place"):
						if place.has_meta("busy"):
							print("has npc")
							if !place.has_meta("master"):
								print("has no master")
								place.set_meta("master", k)
								k.set_meta("smart_object", true)
								print(k.name)
								place.get_meta("busy").smart_obj(playback[state]["slave"])
								place.get_meta("master").smart_obj(playback[state]["master"])
								state = "torture"
		"torture":
			for place in get_children():
				if place.name.begins_with("place"):
					if place.has_meta("busy"):
						if place.has_meta("master"):
							place.get_meta("busy").smart_obj(playback[state]["slave"])
							place.get_meta("master").smart_obj(playback[state]["master"])
		"force":
			for place in get_children():
				if place.name.begins_with("place"):
					if place.has_meta("busy"):
						if place.has_meta("master"):
							place.get_meta("busy").smart_obj(playback[state]["slave"])
							place.get_meta("master").smart_obj(playback[state]["master"])
		"force_s1":
			for place in get_children():
				if place.name.begins_with("place"):
					if place.has_meta("busy"):
						if place.has_meta("master"):
							place.get_meta("busy").smart_obj(playback[state]["slave"])
							place.get_meta("master").smart_obj(playback[state]["master"])
		"leave":
			for place in get_children():
				if place.has_meta("busy") && place.has_meta("master"):
					var m = place.get_meta("master")
					m.set_meta("smart_object", false)
					place.remove_meta("master")
					m.do_stop()
					var s = place.get_meta("busy")
					free_slave(s)
					place.remove_meta("busy")
					s.do_stop()
					var sub = rpg.get_submission(s)
					sub += 100.0
					s.set_meta("submission", sub)
					rpg.update_xp(m, 300.0)
	active_state = state
func _ready():
	var e = $Area.connect("body_entered", self, "entered")
	assert(e == OK)
	e = $Area.connect("body_exited", self, "exited")
	assert(e == OK)
var act = false
func _process(_delta):
	if Input.is_action_just_pressed("grab"):
		act = true
func _physics_process(delta):
	if act:
		activate()
		act = false
	while move_queue.size() > 0:
		var item = move_queue.pop_front()
		item[0].global_transform = item[1].global_transform
	for place in get_children():
		if place.has_meta("busy"):
			place.get_meta("busy").global_transform = place.global_transform
			place.get_meta("busy").orientation.basis = place.global_transform.basis
		if place.has_meta("master"):
			place.get_meta("master").global_transform = place.global_transform
			place.get_meta("master").orientation.basis = place.global_transform.basis
	activate_delay -= delta
	if active_state == "torture":
		for place in get_children():
			if place.has_meta("busy") && place.has_meta("master"):
				var strength = rpg.get_strength(place.get_meta("master"))
				rpg.damage_stamina(place.get_meta("busy"), strength * 0.1 * delta)
				print(rpg.get_stamina(place.get_meta("busy")))
				if rpg.get_stamina(place.get_meta("busy")) <= 10.0:
					state = "force"
					activate_state()
				rpg.damage_stamina(place.get_meta("master"), 5.0 * delta)
	if active_state == "force":
		for place in get_children():
			if place.has_meta("busy") && place.has_meta("master"):
				var strength = rpg.get_strength(place.get_meta("master"))
				rpg.damage_stamina(place.get_meta("busy"), strength * 0.01 * delta)
				print(rpg.get_stamina(place.get_meta("busy")))
				if rpg.get_stamina(place.get_meta("busy")) <= 10.0:
					state = "force_s1"
					activate_state()
				rpg.damage_stamina(place.get_meta("master"), 1.0 * delta)
	if active_state == "force_s1":
		for place in get_children():
			if place.has_meta("busy") && place.has_meta("master"):
				var strength = rpg.get_strength(place.get_meta("master"))
				rpg.damage_stamina(place.get_meta("busy"), strength * 0.01 * delta)
				print(rpg.get_stamina(place.get_meta("busy")))
				rpg.damage_stamina(place.get_meta("master"), 1.0 * delta)
				if rpg.get_stamina(place.get_meta("busy")) <= 0.0:
					state = "leave"
					activate_state()
	if active_state == "leave":
		state = "init"
		active_state = ""
		
