From 78457a82345ebd04dada271c0e6004e0d40f9fbe Mon Sep 17 00:00:00 2001 From: cc9cii Date: Fri, 26 Jun 2015 13:50:09 +1000 Subject: [PATCH] Simplify npc data update and fix data copy when autocal flag changed. --- apps/opencs/model/world/data.cpp | 75 ++++++++------------------------ 1 file changed, 17 insertions(+), 58 deletions(-) diff --git a/apps/opencs/model/world/data.cpp b/apps/opencs/model/world/data.cpp index 3cd501b4fc..3cb5d3cbb3 100644 --- a/apps/opencs/model/world/data.cpp +++ b/apps/opencs/model/world/data.cpp @@ -1373,70 +1373,33 @@ void CSMWorld::Data::npcDataChanged (const QModelIndex& topLeft, const QModelInd static_cast(getTableModel(CSMWorld::UniversalId::Type_Referenceable)); int autoCalcColumn = objectModel->findColumnIndex(CSMWorld::Columns::ColumnId_AutoCalc); - int miscColumn = objectModel->findColumnIndex(CSMWorld::Columns::ColumnId_NpcMisc); - // first check for level - bool levelChanged = false; - if (topLeft.parent().isValid() && bottomRight.parent().isValid()) + // check for autocalc + if (topLeft.parent().isValid() || bottomRight.parent().isValid() + || topLeft.column() > autoCalcColumn || autoCalcColumn > bottomRight.column()) { - if (topLeft.parent().column() <= miscColumn && miscColumn <= bottomRight.parent().column()) - { - for (int col = topLeft.column(); col <= bottomRight.column(); ++col) - { - int role = objectModel->nestedHeaderData(topLeft.parent().column(), - col, Qt::Horizontal, CSMWorld::ColumnBase::Role_ColumnId).toInt(); - if (role == CSMWorld::Columns::ColumnId_NpcLevel) - { - levelChanged = true; - break; - } - } - } + return; } - // next check for autocalc - bool autoCalcChanged = false; - if (!topLeft.parent().isValid() && !bottomRight.parent().isValid()) - { - if ((topLeft.column() <= autoCalcColumn && autoCalcColumn <= bottomRight.column()) - || (topLeft.column() <= miscColumn && miscColumn <= bottomRight.column())) - { - autoCalcChanged = true; - } - } - - if (!levelChanged && !autoCalcChanged) - return; - - int row = 0; - int end = 0; - if (topLeft.parent().isValid()) - row = topLeft.parent().row(); - else - row = topLeft.row(); - - if (bottomRight.parent().isValid()) - end = bottomRight.parent().row(); - else - end = bottomRight.row(); - - for (; row <= end; ++row) + int row = topLeft.row(); + for (; row <= bottomRight.row(); ++row) { Record record = static_cast&>(mReferenceables.getRecord(row)); ESM::NPC &npc = record.get(); - // If going from autocalc to non-autocalc, save the autocalc values - if (autoCalcChanged) + if (npc.mNpdtType != ESM::NPC::NPC_WITH_AUTOCALCULATED_STATS) { - if (npc.mNpdtType != ESM::NPC::NPC_WITH_AUTOCALCULATED_STATS) - saveAutoCalcValues(npc); // update attributes and skills - else - npc.mNpdt12.mLevel = npc.mNpdt52.mLevel; // for NPC's loaded as non-autocalc - - record.setModified(npc); - mReferenceables.replace(row, record); + // first pretend autocalc to force recalculation + npc.mNpdtType = ESM::NPC::NPC_WITH_AUTOCALCULATED_STATS; + saveAutoCalcValues(npc); // update attributes and skills + npc.mNpdtType = ESM::NPC::NPC_DEFAULT; } + else + npc.mNpdt12.mLevel = npc.mNpdt52.mLevel; // for NPC's loaded as non-autocalc + + record.setModified(npc); + mReferenceables.replace(row, record); } } @@ -1469,11 +1432,7 @@ void CSMWorld::Data::gmstDataChanged (const QModelIndex& topLeft, const QModelIn // FIXME: how to undo? void CSMWorld::Data::saveAutoCalcValues(ESM::NPC& npc) { - CSMWorld::NpcStats * cachedStats = getCachedNpcData (npc.mId); - if (!cachedStats) - return; // silently fail - - CSMWorld::NpcStats* stats = npcAutoCalculate(npc); + CSMWorld::NpcStats *stats = npcAutoCalculate(npc); // update npc npc.mNpdt52.mLevel = npc.mNpdt12.mLevel;