From bc6cd6e5f2fce20d21c95095ba06b2727747a67a Mon Sep 17 00:00:00 2001 From: Kelsi Date: Wed, 11 Mar 2026 10:53:52 -0700 Subject: [PATCH] refactor: remove duplicate weapon key-bone fallback in attachWeapon() Consolidated identical key-bone lookup logic that appeared at lines 3076 and 3099. Both performed the same search for weapon attachment points (ID 1/2 for right/left hand). Removed duplication while preserving behavior and improving code clarity with better comments. --- src/rendering/character_renderer.cpp | 37 ++++++++++------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/src/rendering/character_renderer.cpp b/src/rendering/character_renderer.cpp index 17330fe5..7b3fbb52 100644 --- a/src/rendering/character_renderer.cpp +++ b/src/rendering/character_renderer.cpp @@ -3072,30 +3072,7 @@ bool CharacterRenderer::attachWeapon(uint32_t charInstanceId, uint32_t attachmen } } } - // Fallback to key-bone lookup for weapon hand attachment IDs. - if (!found && (attachmentId == 1 || attachmentId == 2)) { - int32_t targetKeyBone = (attachmentId == 1) ? 26 : 27; - for (size_t i = 0; i < charModel.bones.size(); i++) { - if (charModel.bones[i].keyBoneId == targetKeyBone) { - boneIndex = static_cast(i); - found = true; - break; - } - } - } - - // Fallback for head attachment (ID 11): try common head bone indices - // Some models may not have attachment 11 defined, but have bone 0 or 1 as head - if (!found && attachmentId == 11 && charModel.bones.size() > 0) { - // Try bone 0 first (common for head in many humanoid models) - boneIndex = 0; - found = true; - } - - // Validate bone index (bad attachment tables should not silently bind to origin) - if (found && boneIndex >= charModel.bones.size()) { - found = false; - } + // Fallback: key-bone lookup for weapon hand attachment IDs (ID 1 = right hand, ID 2 = left hand) if (!found && (attachmentId == 1 || attachmentId == 2)) { int32_t targetKeyBone = (attachmentId == 1) ? 26 : 27; for (size_t i = 0; i < charModel.bones.size(); i++) { @@ -3108,6 +3085,18 @@ bool CharacterRenderer::attachWeapon(uint32_t charInstanceId, uint32_t attachmen } } + // Fallback for head attachment (ID 11): use bone 0 if attachment not defined + // Some models may not have explicit attachment 11 but use bone 0 as head + if (!found && attachmentId == 11 && charModel.bones.size() > 0) { + boneIndex = 0; + found = true; + } + + // Validate bone index (bad attachment tables should not silently bind to origin) + if (found && boneIndex >= charModel.bones.size()) { + found = false; + } + if (!found) { core::Logger::getInstance().warning("attachWeapon: no bone found for attachment ", attachmentId); return false;