extends RefCounted
## Radar P0 - leak fixes + faction rules (docs/research/LORDNINE_RADAR.md section 3.7 / phase P0, Boss decisions
## 2026-10-18 / 2026-10-19). SERVER rules only; the radar itself (P1+) builds on these helpers.
##  - hidden players (Hiding / Cloaking): never spawned / snapshotted / told about to other clients (net_server.gd
##    _visibility / _route). Party members still see them - except a party member of the ENEMY faction while both wear
##    the faction outfit (Boss 2026-10-18 #2: different factions are enemies even inside one party).
##  - disguise (faction outfit): player_look shows the alias only - no guild, Base Lv rounded down to tens (the client
##    PvP guess only needs "30+"), no creator look (local_server.gd player_look via disguise_look).
##  - faction PvP: suited thief vs suited merchant / guardian near a ★2+ caravan may fight even in the same party /
##    guild (pvp.gd field_ok checks job PvP before allies); the same side never (caravan.gd job_pvp_ok).
##  - support: heal / buff / party aura / AoE buff / revive from `a` never lands on `b` when both wear the outfit of
##    opposing factions (Boss 2026-10-19 #5) - skills2.gd party / ally_target / sanctuary, skills_v2.gd _party_heal,
##    cc.gd heal_allies.
##  - treasure: remote clients get a silver / gold chest's position only within TREASURE_REVEAL_R (treasure.gd
##    state_event); copper (one fixed spot in the open) stays map-wide.

## = NetProto.INTEREST (the radius a client sees anyway): no chest position from farther away.
const TREASURE_REVEAL_R := 40.0
const SUPPORT_MSG := "ช่วยผู้เล่นต่างฝ่ายไม่ได้ (ฮีล / บัฟ / ชุบชีวิต) ขณะที่ทั้งคู่สวมชุดสังกัด"
const GUILD_INVITE_MSG := "เชิญเข้ากิลด์ไม่ได้ระหว่างสวมชุดสังกัด (ถอดชุดในเมืองก่อน)"


static func _C():
	return load("res://scripts/server/caravan.gd")


static func _body_ok(p) -> bool:
	return p != null and is_instance_valid(p) and p.get("ch") != null


## Both wear faction outfits and stand on opposite sides (Desert Thief vs Merchant / Route Guardian).
static func faction_enemies(a, b) -> bool:
	if not _body_ok(a) or not _body_ok(b) or a == b:
		return false
	var C = _C()
	if not C.suited(a) or not C.suited(b):
		return false
	return (C.job_of(a.ch) == "thief") != (C.job_of(b.ch) == "thief")


## May a heal / buff / aura / revive from player `a` land on player `b`?
static func support_ok(a, b) -> bool:
	return a == b or not faction_enemies(a, b)


## `list` without the players `a` may not support (keeps the order).
static func support_filter(a, list: Array) -> Array:
	var out: Array = []
	for p in list:
		if support_ok(a, p):
			out.append(p)
	return out


## Is player `o` invisible to player `viewer` (o hidden, not a friendly party member)?
static func hidden_from(server, viewer, o) -> bool:
	if not _body_ok(o) or not _body_ok(viewer) or o == viewer:
		return false
	if not o.has_method("is_hidden") or not o.is_hidden():
		return false
	if server != null and server.has_method("party_of"):
		var pt = server.party_of(o.id)
		if pt != null and pt == server.party_of(viewer.id) and not faction_enemies(viewer, o):
			return false
	return true


## player_look of a disguised player: no guild, Base Lv in tens, no creator look (the alias is set by the caller).
static func disguise_look(pe, lk: Dictionary) -> Dictionary:
	if not _body_ok(pe) or not _C().suited(pe):
		return lk
	lk["guild"] = ""
	lk["lv"] = int(lk.get("lv", 0)) / 10 * 10
	lk["app"] = {}
	return lk


## The online player called `who`: the real character name first, else a suited player wearing that alias.
static func player_by_name(server, who: String):
	var p = server.player_named(who)
	if p != null:
		return p
	var C = _C()
	for o in server.players.values():
		if C.suited(o) and C.shown_name(o) == who:
			return o
	return null


## Whisper target at the start of `rest` ("<name> <text>"): [player, the name as typed, the text] or [] - aliases have a
## space in them ("โจรทะเลทราย #4821"), so suited players' aliases are tried as a prefix first.
static func whisper_target(server, rest: String) -> Array:
	var C = _C()
	for o in server.players.values():
		if C.suited(o):
			var al: String = C.shown_name(o)
			if al != "" and rest.begins_with(al + " "):
				return [o, al, rest.substr(al.length() + 1).strip_edges()]
	var sp := rest.find(" ")
	if sp <= 0:
		return []
	var to = server.player_named(rest.left(sp))
	if to == null:
		return []
	return [to, rest.left(sp), rest.substr(sp + 1).strip_edges()]
