Files
sor/fix4.py
T
2026-07-02 13:09:05 -04:00

27 lines
5.3 KiB
Python

import io, sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
path = 'C:/GameDev/SOR/Client/origin-client/scenes/game/components/units/unit_mesh_builder.gd'
src = open(path, "r", encoding="utf-8").read()
pairs = [
('var _active_tweens: Array[Tween] = [] # shader tweens tracked for kill-on-rebuild\n',
'var _active_tweens: Array[Tween] = [] # shader tweens tracked for kill-on-rebuild\n\nconst _MAX_POOL_PER_TYPE := 6 # overflow beyond this is freed immediately\nvar _unit_pool: Dictionary = {} # card_type (String) -> Array[StaticBody3D]\nvar _pool_root: Node3D = null\n'),
('# ── Incremental rebuild ───────────────────────────────────────────────────────\n\n## Incremental diff:',
'# ── Unit pool ───────────────────────────────────────────────────────────────────\n\nfunc _ready() -> void:\n\t_pool_root = Node3D.new()\n\t_pool_root.name = "UnitPool"\n\t_pool_root.visible = false\n\tadd_child(_pool_root)\n\n\n## Retire a unit body to the pool instead of freeing it.\n## MeshInstance3D children are freed immediately (releases mesh + material refs).\n## If the per-type pool is full the body is freed immediately to cap memory.\nfunc _pool_unit(body: StaticBody3D, card_type: String) -> void:\n\tif not is_instance_valid(body):\n\t\treturn\n\tfor child: Node in body.get_children():\n\t\tif child is MeshInstance3D:\n\t\t\tchild.free()\n\tbody.visible = false\n\tbody.reparent(_pool_root, false)\n\tif not _unit_pool.has(card_type):\n\t\t_unit_pool[card_type] = []\n\tvar pool: Array = _unit_pool[card_type]\n\tif pool.size() >= _MAX_POOL_PER_TYPE:\n\t\tbody.free()\n\telse:\n\t\tpool.append(body)\n\n\n## Pop a body from the per-type pool and fully reset it for reuse.\n## Returns null if the pool is empty (caller should allocate fresh).\nfunc _acquire_from_pool(card_type: String) -> StaticBody3D:\n\tif not _unit_pool.has(card_type):\n\t\treturn null\n\tvar pool: Array = _unit_pool[card_type]\n\tif pool.is_empty():\n\t\treturn null\n\tvar body: StaticBody3D = pool.pop_back()\n\tif not is_instance_valid(body):\n\t\treturn null\n\tbody.position = Vector3.ZERO\n\tbody.rotation = Vector3.ZERO\n\tbody.scale = Vector3.ONE\n\tbody.collision_layer = 1\n\tbody.collision_mask = 1\n\tfor group: String in body.get_groups():\n\t\tbody.remove_from_group(group)\n\tfor key: String in body.get_meta_list():\n\t\tif key not in ["unit_id", "owner_id", "card_type", "team_color", "above_tile"]:\n\t\t\tbody.remove_meta(key)\n\tbody.reparent(units_root, false)\n\tbody.visible = true\n\treturn body\n\n\n# ── Incremental rebuild ───────────────────────────────────────────────────────\n\n## Incremental diff:'),
('\t\t\telse:\n\t\t\t\tbody.queue_free()\n',
'\t\t\telse:\n\t\t\t\t_pool_unit(body, card_type)\n'),
('\tvar body: StaticBody3D = StaticBody3D.new()\n\tbody.set_meta("unit_id", iid)\n\tbody.set_meta("owner_id", owner_id)\n\tbody.set_meta("card_type", card_type)\n\tbody.set_meta("team_color", team_color)\n\tif not owner_id.is_empty():\n\t\tunit_owner_ids[iid] = owner_id\n\n\tvar shape: CollisionShape3D = CollisionShape3D.new()\n\tvar mi: MeshInstance3D = MeshInstance3D.new()\n',
'\tvar _pool_hit := false\n\tvar body: StaticBody3D = _acquire_from_pool(card_type)\n\tvar shape: CollisionShape3D\n\tif body == null:\n\t\tbody = StaticBody3D.new()\n\t\tshape = CollisionShape3D.new()\n\telse:\n\t\t_pool_hit = true\n\t\tfor child in body.get_children():\n\t\t\tif child is CollisionShape3D:\n\t\t\t\tshape = child\n\t\t\t\tbreak\n\tbody.set_meta("unit_id", iid)\n\tbody.set_meta("owner_id", owner_id)\n\tbody.set_meta("card_type", card_type)\n\tbody.set_meta("team_color", team_color)\n\tif not owner_id.is_empty():\n\t\tunit_owner_ids[iid] = owner_id\n\tif OS.is_debug_build():\n\t\tvar pool_sizes := {}\n\t\tfor ct: String in _unit_pool:\n\t\t\tpool_sizes[ct] = _unit_pool[ct].size()\n\t\tprint("UnitMeshBuilder: add_unit iid=%s pool_hit=%s pools=%s" % [iid, _pool_hit, pool_sizes])\n\n\tvar mi: MeshInstance3D = MeshInstance3D.new()\n'),
('\tbody.add_child(shape)\n\tbody.add_child(mi)\n\tbody.set_meta("above_tile", above_tile)\n\tbody.position = wp + Vector3(0, tile_base + tile_h + above_tile, 0)\n\tunits_root.add_child(body)\n',
'\tif not _pool_hit:\n\t\tbody.add_child(shape)\n\tbody.add_child(mi)\n\tbody.set_meta("above_tile", above_tile)\n\tbody.position = wp + Vector3(0, tile_base + tile_h + above_tile, 0)\n\tif not _pool_hit:\n\t\tunits_root.add_child(body)\n'),
('func _exit_tree() -> void:\n\tfor t: Tween in _active_tweens:\n\t\tif is_instance_valid(t):\n\t\t\tt.kill()\n\t_active_tweens.clear()\n',
'func _exit_tree() -> void:\n\tfor t: Tween in _active_tweens:\n\t\tif is_instance_valid(t):\n\t\t\tt.kill()\n\t_active_tweens.clear()\n\tif is_instance_valid(_pool_root):\n\t\t_pool_root.free()\n\t_unit_pool.clear()\n'),
]
for i, (old, new) in enumerate(pairs, 1):
assert old in src, f"anchor {i} not found"
src = src.replace(old, new, 1)
print(f"step {i} done")
open(path, "w", encoding="utf-8").write(src)
print("All done, line count:", src.count(chr(10)))