Skip to content

Commit 7361417

Browse files
authored
Cache Animation submodel by model id (#7331)
1 parent 9958531 commit 7361417

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

code/model/animation/modelanimation.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,8 @@ namespace animation {
420420
}
421421

422422
void ModelAnimationSubmodel::reset(polymodel_instance* pmi) {
423-
if(!m_submodel)
423+
auto cache_it = m_submodel.find(pmi->model_num);
424+
if (cache_it == m_submodel.end())
424425
findSubmodel(pmi);
425426

426427
auto dataIt = m_initialData.find({ pmi->id });
@@ -515,8 +516,9 @@ namespace animation {
515516
polymodel* pm = model_get(pmi->model_num);
516517

517518
//Do we have a submodel number already cached?
518-
if (m_submodel)
519-
submodelNumber = *m_submodel;
519+
auto cache_it = m_submodel.find(pm->id);
520+
if (cache_it != m_submodel.end())
521+
submodelNumber = cache_it->second;
520522
//We seem to have a submodel name
521523
else {
522524
for (int i = 0; i < pm->n_models; i++) {
@@ -526,7 +528,7 @@ namespace animation {
526528
}
527529
}
528530

529-
m_submodel = submodelNumber;
531+
m_submodel.emplace(pm->id, submodelNumber);
530532
}
531533

532534
//If the model does not exist, return null. The system is expected to just silently tolerate this,
@@ -559,11 +561,12 @@ namespace animation {
559561
polymodel* pm = model_get(pmi->model_num);
560562

561563
//Do we have a submodel number already cached?
562-
if (m_submodel)
563-
submodelNumber = *m_submodel;
564+
auto cache_it = m_submodel.find(pm->id);
565+
if (cache_it != m_submodel.end())
566+
submodelNumber = cache_it->second;
564567
//Do we know if we were told to find the barrel submodel or not? This implies we have a subsystem name, not a submodel name
565568
else {
566-
ship_info* sip = nullptr;
569+
const ship_info* sip = nullptr;
567570
if (pmi->objnum >= 0) {
568571
sip = &Ship_info[Ships[Objects[pmi->objnum].instance].ship_info_index];
569572
}
@@ -579,7 +582,7 @@ namespace animation {
579582

580583
for (int i = 0; i < sip->n_subsystems; i++) {
581584
if (!subsystem_stricmp(sip->subsystems[i].subobj_name, m_name.c_str())) {
582-
if ((bool)m_findBarrel) {
585+
if (m_findBarrel) {
583586
//Check if the barrel subobj is a dedicated existing subobj or just the base turret.
584587
if (sip->subsystems[i].turret_gun_sobj != sip->subsystems[i].subobj_num)
585588
submodelNumber = sip->subsystems[i].turret_gun_sobj;
@@ -592,7 +595,7 @@ namespace animation {
592595
}
593596
}
594597

595-
m_submodel = submodelNumber;
598+
m_submodel.emplace(pm->id, submodelNumber);
596599
}
597600

598601
//If the model does not exist, return null. The system is expected to just silently tolerate this,
@@ -682,7 +685,7 @@ namespace animation {
682685

683686
for (const auto& submodel : other.m_submodels) {
684687
auto newSubmodel = std::shared_ptr<ModelAnimationSubmodel>(submodel->copy());
685-
newSubmodel->m_submodel = std::nullopt;
688+
newSubmodel->m_submodel = {};
686689
m_submodels.push_back(newSubmodel);
687690
}
688691

code/model/animation/modelanimation.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ namespace animation {
127127
class ModelAnimationSubmodel {
128128
protected:
129129
SCP_string m_name;
130-
std::optional<int> m_submodel;
130+
//This maps from model id to submodel ID, as if an animation is reused on different models (such as with techroom models), IDs might differ per model
131+
SCP_unordered_map<int, int> m_submodel;
131132
bool is_turret = false;
132133

133134
private:

0 commit comments

Comments
 (0)