extends Node
## NPC look QA (NPC redesign 2026-10-03, not part of the game): stands a row of town NPC figures (npc.gd, the real
## view) beside the hero at the --shotat spot, so main.gd's --shot captures NPCs and the hero in the same light.
##   godot --path game -s res://scripts/tools/npc_lineup_boot.gd -- --fresh --shot=<png> --shotat=x,z --shotcam=yaw,pitch,dist
##        --npclineup=<set>      set: roles | jobs | ids:<npc id>,<npc id>,...
## The row runs along the camera's right axis, 2.3 m apart, 2.6 m in front of the hero, everyone facing the camera.

var main: Node = null
var _f := 0
var _done := false

const ROLES := [
	{"id": "qa_warp", "name": "Warper", "kind": "warp", "role": "warp", "name_th": "นักวาร์ป · ลูลู่"},
	{"id": "qa_save", "name": "Save", "kind": "save", "role": "save", "name_th": "บันทึกจุดเกิด · พิม"},
	{"id": "qa_heal", "name": "Healer", "kind": "heal", "role": "heal", "name_th": "พยาบาล · ลีนา"},
	{"id": "qa_tool", "name": "Tools", "kind": "shop", "role": "tool", "name_th": "ร้านยา/ของใช้ · มีนา"},
	{"id": "qa_weapon", "name": "Weapons", "kind": "shop", "role": "weapon", "name_th": "ร้านอาวุธ · ลุงบรูโน่"},
	{"id": "qa_armor", "name": "Armour", "kind": "shop", "role": "armor", "name_th": "ร้านเกราะ · เบอร์ธา"},
	{"id": "qa_refine", "name": "Refiner", "kind": "refine", "role": "refine", "name_th": "ช่างตีบวก · โดแรน"},
	{"id": "qa_bank", "name": "Storage", "kind": "talk", "role": "bank", "name_th": "ผู้ดูแลคลัง · ฮัมฟรีย์"},
	{"id": "qa_guide", "name": "Guide", "kind": "talk", "role": "guide", "name_th": "ไกด์นำทาง · โตโต้"},
	{"id": "qa_cook", "name": "Cook", "kind": "talk", "role": "cook", "name_th": "พ่อครัว · โพเมโล"},
	{"id": "qa_fish", "name": "Fishing", "kind": "talk", "role": "fish", "name_th": "ครูตกปลา · มาร์โล"},
	{"id": "qa_quest", "name": "Quest", "kind": "talk", "role": "quest", "name_th": "ภารกิจ · คาเอเดะ"},
]


func _process(_d: float) -> void:
	if _done or main == null:
		return
	var pl = main.get("player")
	if pl == null or not is_instance_valid(pl) or not (pl as Node3D).is_inside_tree():
		return
	_f += 1
	if _f < 8:
		return
	_done = true
	var spec := ""
	for a in OS.get_cmdline_user_args():
		if a.begins_with("--npclineup="):
			spec = a.substr(12)
	var defs: Array = []
	if spec == "roles":
		defs = ROLES
	elif spec == "roles1":
		defs = ROLES.slice(0, 6)
	elif spec == "roles2":
		defs = ROLES.slice(6)
	elif spec == "jobs":
		for n in GameData.npcs:
			if str(n.get("kind", "")) == "job":
				defs.append(n)
	elif spec.begins_with("ids:"):
		for i in spec.substr(4).split(",", false):
			for n in GameData.npcs:
				if str(n.get("id", "")) == i:
					defs.append(n)
	# main.gd applies --shotat / --shotcam later (30 frames into the shot): read them from the arguments
	var yaw := 0.0
	var at: Vector3 = (pl as Node3D).global_position
	for a in OS.get_cmdline_user_args():
		if a.begins_with("--shotcam="):
			yaw = deg_to_rad(float(a.substr(10).split(",")[0]))
		if a.begins_with("--shotat="):
			var xz := a.substr(9).split(",")
			at = Vector3(float(xz[0]), 0, float(xz[1]))
	var right := Vector3(cos(yaw), 0, -sin(yaw))
	var fwd := Vector3(-sin(yaw), 0, -cos(yaw))
	# nobody in the way
	for g in ["monsters", "npcs"]:
		for n in get_tree().get_nodes_in_group(g):
			if (n as Node3D).global_position.distance_to(at) < 16.0:
				(n as Node3D).visible = false
	var view = load("res://scripts/npc.gd")
	var cnt := defs.size()
	for i in cnt:
		var d: Dictionary = (defs[i] as Dictionary).duplicate(true)
		var p: Vector3 = at + fwd * 2.6 + right * (float(i) - (cnt - 1) * 0.5) * 2.3
		d["pos"] = [p.x, p.z]
		d["face"] = rad_to_deg(yaw)
		d.erase("map")
		var v = view.new()
		v.npc = d
		v.name = "QA_" + str(d.get("id", i))
		main.add_child(v)
	print("NPCLINEUP spawned ", cnt)
