Keep SetWeapon/AttachPart helper attachments across rebinds

Helper attachments made outside the part slots were attached to
PART_MAIN only and dropped whenever the parts were rebound (LOD level
change, shape change, slot update). They are now recorded on the actor,
attached to the drawn instance and re-attached after the slots. A slot
taking the same bone drops the helper (last writer wins, as before);
SetRace/SetModel clear them. LODs missing a helper's bone stay on
PART_MAIN.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-12 18:18:14 +09:00
co-authored by Claude Opus 5.5
parent 552f6c7df4
commit c556795ecf
3 changed files with 48 additions and 3 deletions
+24 -3
View File
@@ -82,6 +82,17 @@ void ActorInstance::RebindParts() {
if (m_hasSashScale) target->SetAttachmentScale(slot.bone, m_sashScale[0], m_sashScale[1], m_sashScale[2]);
}
}
for (const auto& extra : m_extraAttachments) {
if (extra.bone.empty()) target->AttachPart(extra.model);
else target->AttachModel(extra.bone, extra.model);
}
}
void ActorInstance::DropExtraAttachment(const std::string& bone) {
if (bone.empty()) return;
m_extraAttachments.erase(std::remove_if(m_extraAttachments.begin(), m_extraAttachments.end(),
[&](const SExtraAttachment& e) { return e.bone == bone; }),
m_extraAttachments.end());
}
Gr2ModelInstance* ActorInstance::DrawModel() const {
@@ -105,6 +116,8 @@ bool ActorInstance::LodHasAttachBones(const Gr2ModelInstance& lod) const {
const auto& slot = m_parts[part];
if (slot.model && !slot.bone.empty() && !HasBone(lod, slot.bone)) return false;
}
for (const auto& extra : m_extraAttachments)
if (!extra.bone.empty() && !HasBone(lod, extra.bone)) return false;
static const std::string kSaddle = "saddle";
return !HasBone(*m_model, kSaddle) || HasBone(lod, kSaddle);
}
@@ -161,6 +174,7 @@ bool ActorInstance::SetRace(uint32_t race) {
// memset(m_adwPartItemID) + CGraphicThingInstance::Clear(): every part model is dropped.
std::fill(std::begin(m_partItemID), std::end(m_partItemID), 0u);
for (auto& slot : m_parts) slot = SPartSlot{};
m_extraAttachments.clear();
m_model.reset();
m_modelPath.clear();
m_shapeFile.clear();
@@ -197,7 +211,6 @@ void ActorInstance::SetShape(uint32_t shape, float specular) {
const RaceData::Shape* pkShape = raceData->FindShape(shape);
if (pkShape) {
const std::string& file = pkShape->model.empty() ? raceData->GetBaseModelFileName() : pkShape->model;
// The _lod_01.._03 LOD things are not ported: LOD 0 always draws.
auto model = LoadPartModel(file);
m_mainSkins.clear();
for (const auto& skin : pkShape->skins) m_mainSkins.emplace_back(skin.srcFileName, skin.dstFileName);
@@ -242,6 +255,7 @@ void ActorInstance::AttachWeaponPart(uint32_t part, const SPartItem& item) {
m_parts[part].model = LoadPartModel(item.model);
m_parts[part].file = item.model;
m_parts[part].bone = bone;
DropExtraAttachment(bone);
// USE_WEAPON_SPECULAR sphere map and CWeaponTrace are not ported.
}
@@ -271,6 +285,7 @@ void ActorInstance::AttachSash(const SPartItem* item, float specular) {
m_parts[RaceData::PART_SASH].model = LoadPartModel(item->model);
m_parts[RaceData::PART_SASH].file = item->model;
m_parts[RaceData::PART_SASH].bone = "Bip01 Spine2";
DropExtraAttachment(m_parts[RaceData::PART_SASH].bone);
}
RebindParts();
}
@@ -321,6 +336,7 @@ bool ActorInstance::SetModel(const std::string& modelPath) {
if (!m_model) return false;
m_modelPath = modelPath;
m_mainSkins.clear();
m_extraAttachments.clear(); // a fresh instance carries none of the old attachments
ResetLodModel();
return true;
}
@@ -337,7 +353,10 @@ bool ActorInstance::SetWeapon(const std::string& weaponPath, const std::string&
}
}
if (!weaponModel) return false;
m_model->AttachModel(boneName, std::move(weaponModel));
std::shared_ptr<Gr2ModelInstance> weapon(std::move(weaponModel));
DropExtraAttachment(boneName);
m_extraAttachments.push_back({boneName, weapon});
DrawModel()->AttachModel(boneName, std::move(weapon));
return true;
}
@@ -347,7 +366,9 @@ bool ActorInstance::AttachPart(const std::string& partPath) {
if (!std::filesystem::exists(resolved)) return false;
auto partModel = Gr2ModelInstance::Load(resolved);
if (!partModel) return false;
m_model->AttachPart(std::move(partModel));
std::shared_ptr<Gr2ModelInstance> part(std::move(partModel));
m_extraAttachments.push_back({std::string(), part});
DrawModel()->AttachPart(std::move(part));
return true;
}