fix(locale): resolve Chinese monster and NPC names via Locale.mob_names and early-bind name resolver

This commit is contained in:
shen
2026-09-19 19:19:55 -07:00
parent 7cbda7b3dd
commit 1637b500e5
4 changed files with 143 additions and 22 deletions
+53 -19
View File
@@ -119,6 +119,7 @@ var net_world: Node # NetWorld
var hud: Node # 可空
var target_ui: Node # 40250 uitarget.py TargetBoard (可空)
var audio: Node # 可空
var locale: RefCounted = null # 语言包 Locale 实例(查 mob_names.txt / 翻译)
var _last_level := -1
var _last_exp := -1
var _last_dead_mob_pos := Vector3.INF
@@ -663,29 +664,54 @@ func _entity_kind(e: Dictionary) -> int:
return explicit_kind
return 0
var _locale_ref: RefCounted = null
func _get_locale() -> RefCounted:
if locale != null:
return locale
var ar_class = load("res://asset_root.gd")
var root_path: String = ar_class.path() if ar_class and ar_class.has_method("path") else ""
if root_path != "":
var loc_script = load("res://locale.gd")
if loc_script:
var loc: RefCounted = loc_script.new()
var cur_lang: String = loc_script.get_default_lang() if loc_script.has_method("get_default_lang") else "zh"
loc.setup(root_path, cur_lang)
locale = loc
return locale
return null
# 实体显示名:玩家 / NPC 走 GC_CHAR_ADD_INFO 的 name;怪没有名字包 -> 查 mob_proto / Locale。
# 实体显示名:
# 1. 玩家实体(PC):使用其自定义角色名(含变身中玩家、主玩家)
# 2. 怪物 / NPC / 陨石 / 建筑等(或 race >= 1 的非PC实体):优先查本地化 mob_names (locale),其次 mob_proto
# 3. 兜底回退:实体字典自带名称
func _entity_name(e: Dictionary) -> String:
var nm := String(e.get("name", ""))
if nm != "":
return nm
var race := int(e.get("race", 0))
var kind := _entity_kind(e)
var is_main := bool(e.get("is_main", false)) or (client and client.has_method("get_main_vid") and int(e.get("vid", 0)) == int(client.get_main_vid()))
var is_poly := EntityRules._is_poly(e)
# 1. 玩家实体(PC):使用其自定义角色名(含变身中玩家、主玩家)
if is_main or is_poly or kind == EntityRules.KIND_PC or (race < 8 and not EntityRules._is_enemy(e)):
var nm := String(e.get("name", ""))
if nm != "":
return nm
# 2. 怪物 / NPC / 陨石 / 建筑等:优先查本地化词表 (mob_names.txt)
if race >= 1:
if _locale_ref == null and ResourceLoader.exists("res://locale.gd"):
var LocaleScript = load("res://locale.gd")
if LocaleScript:
_locale_ref = LocaleScript.new()
_locale_ref.setup(AssetRoot.path() if ResourceLoader.exists("res://asset_root.gd") else "")
if _locale_ref and _locale_ref.has_method("mob_name"):
var zh_name: String = _locale_ref.mob_name(race)
if not zh_name.begins_with("怪物 #") and not zh_name.is_empty():
return zh_name
if race >= 1 and proto and proto.has_method("mob"):
var m: Dictionary = proto.mob(race)
if not m.is_empty():
return String(m.get("locale_name", m.get("name", "")))
return ""
var loc := _get_locale()
if loc and loc.has_method("has_mob_name") and loc.has_mob_name(race):
return loc.mob_name(race)
if proto and proto.has_method("mob"):
var m: Dictionary = proto.mob(race)
if not m.is_empty():
var ln := String(m.get("locale_name", ""))
if ln != "":
return ln
var pn := String(m.get("name", ""))
if pn != "":
return pn
# 3. 兜底回退:实体字典自带名称
return String(e.get("name", ""))
# --- movement ----------------------------------------------------------------
@@ -1094,6 +1120,14 @@ func _on_target_info(vid: int, hp_pct: int) -> void:
if target_ui and target_ui.has_method("open"):
var lvl := int(e.get("level", -1))
var grade := int(e.get("grade", -1))
var mob_race := int(e.get("race", 0))
if not is_pc and mob_race >= 1 and proto and proto.has_method("mob"):
var m: Dictionary = proto.mob(mob_race)
if not m.is_empty():
if lvl <= 0:
lvl = int(m.get("level", -1))
if grade < 0:
grade = int(m.get("rank", -1))
target_ui.open(vid, nm if nm != "" else "目标", lvl, grade, is_pc)
target_ui.set_hp(hp_clamped)
TargetBoard.Action.NONE: