2018-07-28 17:23:43 +00:00
|
|
|
#include "actoradapter.hpp"
|
|
|
|
|
2018-08-24 02:40:43 +00:00
|
|
|
#include <components/debug/debuglog.hpp>
|
2018-07-28 17:23:43 +00:00
|
|
|
#include <components/esm/loadarmo.hpp>
|
|
|
|
#include <components/esm/loadclot.hpp>
|
|
|
|
#include <components/esm/loadnpc.hpp>
|
|
|
|
#include <components/esm/loadrace.hpp>
|
|
|
|
#include <components/esm/mappings.hpp>
|
|
|
|
|
|
|
|
#include "data.hpp"
|
|
|
|
|
|
|
|
namespace CSMWorld
|
|
|
|
{
|
|
|
|
ActorAdapter::ActorAdapter(CSMWorld::Data& data)
|
|
|
|
: mReferenceables(data.getReferenceables())
|
|
|
|
, mRaces(data.getRaces())
|
|
|
|
, mBodyParts(data.getBodyParts())
|
|
|
|
{
|
|
|
|
connect(data.getTableModel(UniversalId::Type_Referenceable), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
|
|
|
|
this, SLOT(handleReferenceableChanged(const QModelIndex&, const QModelIndex&)));
|
|
|
|
connect(data.getTableModel(UniversalId::Type_Race), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
|
|
|
|
this, SLOT(handleRaceChanged(const QModelIndex&, const QModelIndex&)));
|
|
|
|
connect(data.getTableModel(UniversalId::Type_BodyPart), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
|
|
|
|
this, SLOT(handleBodyPartChanged(const QModelIndex&, const QModelIndex&)));
|
|
|
|
}
|
|
|
|
|
2018-08-24 06:44:02 +00:00
|
|
|
const ActorAdapter::ActorPartMap* ActorAdapter::getActorParts(const std::string& refId, bool create)
|
2018-07-28 17:23:43 +00:00
|
|
|
{
|
2018-08-24 06:44:02 +00:00
|
|
|
auto it = mCachedActors.find(refId);
|
|
|
|
if (it != mCachedActors.end())
|
2018-07-28 17:23:43 +00:00
|
|
|
{
|
2018-08-24 06:44:02 +00:00
|
|
|
return &it->second.parts;
|
2018-07-28 17:23:43 +00:00
|
|
|
}
|
2018-08-24 06:44:02 +00:00
|
|
|
else if (create)
|
2018-07-28 17:23:43 +00:00
|
|
|
{
|
|
|
|
updateActor(refId);
|
2018-08-24 06:44:02 +00:00
|
|
|
return getActorParts(refId, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nullptr;
|
2018-07-28 17:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActorAdapter::handleReferenceableChanged(const QModelIndex& topLeft, const QModelIndex& botRight)
|
|
|
|
{
|
2018-08-24 02:40:43 +00:00
|
|
|
// Setup
|
|
|
|
const int TypeColumn = mReferenceables.findColumnIndex(CSMWorld::Columns::ColumnId_RecordType);
|
|
|
|
int rowStart = getHighestIndex(topLeft).row();
|
|
|
|
int rowEnd = getHighestIndex(botRight).row();
|
|
|
|
|
|
|
|
// Handle each record
|
|
|
|
for (int row = rowStart; row <= rowEnd; ++row)
|
|
|
|
{
|
|
|
|
int type = mReferenceables.getData(row, TypeColumn).toInt();
|
|
|
|
if (type == CSMWorld::UniversalId::Type_Creature || type == CSMWorld::UniversalId::Type_Npc)
|
|
|
|
{
|
|
|
|
// Update the cached npc or creature
|
|
|
|
std::string refId = mReferenceables.getId(row);
|
2018-08-24 06:44:02 +00:00
|
|
|
if (mCachedActors.find(refId) != mCachedActors.end())
|
2018-08-24 02:40:43 +00:00
|
|
|
updateActor(refId);
|
|
|
|
}
|
2018-08-24 06:44:02 +00:00
|
|
|
else if (type == CSMWorld::UniversalId::Type_Armor || type == CSMWorld::UniversalId::Type_Clothing)
|
2018-08-24 02:40:43 +00:00
|
|
|
{
|
2018-08-24 06:44:02 +00:00
|
|
|
std::string refId = mReferenceables.getId(row);
|
|
|
|
updateActorsWithDependency(refId);
|
2018-08-24 02:40:43 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-28 17:23:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActorAdapter::handleRaceChanged(const QModelIndex& topLeft, const QModelIndex& botRight)
|
|
|
|
{
|
2018-08-24 02:40:43 +00:00
|
|
|
int rowStart = getHighestIndex(topLeft).row();
|
|
|
|
int rowEnd = getHighestIndex(botRight).row();
|
|
|
|
for (int row = rowStart; row <= rowEnd; ++row)
|
|
|
|
{
|
|
|
|
std::string raceId = mRaces.getId(row);
|
2018-08-24 06:44:02 +00:00
|
|
|
updateActorsWithDependency(raceId);
|
2018-08-24 02:40:43 +00:00
|
|
|
}
|
2018-07-28 17:23:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActorAdapter::handleBodyPartChanged(const QModelIndex& topLeft, const QModelIndex& botRight)
|
|
|
|
{
|
2018-08-24 06:44:02 +00:00
|
|
|
int rowStart = getHighestIndex(topLeft).row();
|
|
|
|
int rowEnd = getHighestIndex(botRight).row();
|
|
|
|
for (int row = rowStart; row <= rowEnd; ++row)
|
|
|
|
{
|
|
|
|
// Manually update race specified by part
|
|
|
|
auto& record = mBodyParts.getRecord(row);
|
|
|
|
if (!record.isDeleted())
|
|
|
|
{
|
|
|
|
updateRace(record.get().mRace);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update entries with a tracked dependency
|
|
|
|
std::string partId = mBodyParts.getId(row);
|
|
|
|
updateRacesWithDependency(partId);
|
|
|
|
updateActorsWithDependency(partId);
|
|
|
|
}
|
2018-08-24 02:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex ActorAdapter::getHighestIndex(QModelIndex index) const
|
|
|
|
{
|
|
|
|
while (index.parent().isValid())
|
|
|
|
index = index.parent();
|
|
|
|
return index;
|
2018-07-28 17:23:43 +00:00
|
|
|
}
|
|
|
|
|
2018-08-24 06:44:02 +00:00
|
|
|
bool ActorAdapter::is1stPersonPart(const std::string& name) const
|
2018-07-28 17:23:43 +00:00
|
|
|
{
|
2018-08-24 06:44:02 +00:00
|
|
|
return name.size() >= 4 && name.find(".1st", name.size() - 4) != std::string::npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActorAdapter::RaceData& ActorAdapter::getRaceData(const std::string& raceId)
|
|
|
|
{
|
|
|
|
auto it = mCachedRaces.find(raceId);
|
|
|
|
if (it != mCachedRaces.end())
|
2018-07-28 17:23:43 +00:00
|
|
|
{
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Create and find result
|
2018-08-24 06:44:02 +00:00
|
|
|
updateRace(raceId);
|
|
|
|
return mCachedRaces.find(raceId)->second;
|
2018-07-28 17:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 06:44:02 +00:00
|
|
|
void ActorAdapter::updateRace(const std::string& raceId)
|
2018-07-28 17:23:43 +00:00
|
|
|
{
|
2018-08-24 06:44:02 +00:00
|
|
|
// Retrieve or create cache entry
|
|
|
|
auto raceDataIt = mCachedRaces.find(raceId);
|
|
|
|
if (raceDataIt == mCachedRaces.end())
|
|
|
|
{
|
|
|
|
auto result = mCachedRaces.emplace(raceId, RaceData());
|
|
|
|
raceDataIt = result.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& raceData = raceDataIt->second;
|
|
|
|
raceData.femaleParts.clear();
|
|
|
|
raceData.maleParts.clear();
|
|
|
|
raceData.dependencies.clear();
|
2018-07-28 17:23:43 +00:00
|
|
|
|
2018-08-24 06:44:02 +00:00
|
|
|
// Construct entry
|
2018-07-28 17:23:43 +00:00
|
|
|
for (int i = 0; i < mBodyParts.getSize(); ++i)
|
|
|
|
{
|
|
|
|
auto& record = mBodyParts.getRecord(i);
|
2018-08-24 06:44:02 +00:00
|
|
|
if (!record.isDeleted() && record.get().mRace == raceId)
|
2018-07-28 17:23:43 +00:00
|
|
|
{
|
|
|
|
auto& part = record.get();
|
2018-08-24 06:44:02 +00:00
|
|
|
|
|
|
|
// Part could affect race data
|
|
|
|
raceData.dependencies.emplace(part.mId, true);
|
|
|
|
|
|
|
|
// Add base types
|
|
|
|
if (part.mData.mType == ESM::BodyPart::MT_Skin && !is1stPersonPart(part.mId))
|
|
|
|
{
|
|
|
|
auto type = (ESM::BodyPart::MeshPart) part.mData.mPart;
|
|
|
|
// Note: Prefer the first part encountered for duplicates. emplace() does not overwrite
|
|
|
|
if (part.mData.mFlags & ESM::BodyPart::BPF_Female)
|
|
|
|
raceData.femaleParts.emplace(type, part.mId);
|
|
|
|
else
|
|
|
|
raceData.maleParts.emplace(type, part.mId);
|
|
|
|
}
|
2018-07-28 17:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 06:44:02 +00:00
|
|
|
updateActorsWithDependency(raceId);
|
2018-07-28 17:23:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActorAdapter::updateActor(const std::string& refId)
|
|
|
|
{
|
|
|
|
int index = mReferenceables.searchId(refId);
|
|
|
|
if (index != -1)
|
|
|
|
{
|
|
|
|
int typeColumn = mReferenceables.findColumnIndex(CSMWorld::Columns::ColumnId_RecordType);
|
|
|
|
int recordType = mReferenceables.getData(index, typeColumn).toInt();
|
|
|
|
if (recordType == CSMWorld::UniversalId::Type_Creature)
|
|
|
|
updateCreature(refId);
|
|
|
|
else if (recordType == CSMWorld::UniversalId::Type_Npc)
|
|
|
|
updateNpc(refId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActorAdapter::updateNpc(const std::string& refId)
|
|
|
|
{
|
|
|
|
auto& record = mReferenceables.getRecord(refId);
|
2018-08-24 06:44:02 +00:00
|
|
|
|
|
|
|
// Retrieve record if possible
|
2018-07-28 17:23:43 +00:00
|
|
|
if (record.isDeleted())
|
|
|
|
{
|
2018-08-24 06:44:02 +00:00
|
|
|
mCachedActors.erase(refId);
|
|
|
|
emit actorChanged(refId);
|
2018-07-28 17:23:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto& npc = dynamic_cast<const Record<ESM::NPC>&>(record).get();
|
|
|
|
|
2018-08-24 06:44:02 +00:00
|
|
|
// Create holder for cached data
|
|
|
|
auto actorIt = mCachedActors.find(refId);
|
|
|
|
if (actorIt == mCachedActors.end())
|
|
|
|
{
|
|
|
|
auto result = mCachedActors.emplace(refId, ActorData());
|
|
|
|
actorIt = result.first;
|
|
|
|
}
|
|
|
|
auto& actorData = actorIt->second;
|
|
|
|
|
|
|
|
// Reset old data
|
|
|
|
actorData.parts.clear();
|
|
|
|
actorData.dependencies.clear();
|
2018-07-28 17:23:43 +00:00
|
|
|
|
|
|
|
// Look at the npc's inventory first
|
|
|
|
for (auto& item : npc.mInventory.mList)
|
|
|
|
{
|
|
|
|
if (item.mCount > 0)
|
|
|
|
{
|
|
|
|
std::string itemId = item.mItem.toString();
|
2018-08-24 06:44:02 +00:00
|
|
|
// Handle armor and clothing
|
2018-07-28 17:23:43 +00:00
|
|
|
int index = mReferenceables.searchId(itemId);
|
|
|
|
if (index != -1 && !mReferenceables.getRecord(index).isDeleted())
|
|
|
|
{
|
|
|
|
auto& itemRecord = mReferenceables.getRecord(index);
|
|
|
|
|
|
|
|
int typeColumn = mReferenceables.findColumnIndex(CSMWorld::Columns::ColumnId_RecordType);
|
|
|
|
int recordType = mReferenceables.getData(index, typeColumn).toInt();
|
|
|
|
if (recordType == CSMWorld::UniversalId::Type_Armor)
|
|
|
|
{
|
2018-08-24 06:44:02 +00:00
|
|
|
// Changes here could affect the actor
|
|
|
|
actorData.dependencies.emplace(itemId, true);
|
|
|
|
|
|
|
|
// Add any parts if there is room
|
2018-07-28 17:23:43 +00:00
|
|
|
auto& armor = dynamic_cast<const Record<ESM::Armor>&>(itemRecord).get();
|
|
|
|
for (auto& part : armor.mParts.mParts)
|
|
|
|
{
|
|
|
|
std::string bodyPartId;
|
|
|
|
if (!npc.isMale())
|
|
|
|
bodyPartId = part.mFemale;
|
|
|
|
if (bodyPartId.empty())
|
|
|
|
bodyPartId = part.mMale;
|
|
|
|
|
|
|
|
if (!bodyPartId.empty())
|
2018-08-24 06:44:02 +00:00
|
|
|
{
|
|
|
|
actorData.parts.emplace(static_cast<ESM::PartReferenceType>(part.mPart), bodyPartId);
|
|
|
|
actorData.dependencies.emplace(bodyPartId, true);
|
|
|
|
}
|
2018-07-28 17:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (recordType == CSMWorld::UniversalId::Type_Clothing)
|
|
|
|
{
|
2018-08-24 06:44:02 +00:00
|
|
|
// Changes here could affect the actor
|
|
|
|
actorData.dependencies.emplace(itemId, true);
|
|
|
|
|
|
|
|
// Add any parts if there is room
|
2018-07-28 17:23:43 +00:00
|
|
|
auto& clothing = dynamic_cast<const Record<ESM::Clothing>&>(itemRecord).get();
|
|
|
|
for (auto& part : clothing.mParts.mParts)
|
|
|
|
{
|
|
|
|
std::string bodyPartId;
|
|
|
|
if (!npc.isMale())
|
|
|
|
bodyPartId = part.mFemale;
|
|
|
|
if (bodyPartId.empty())
|
|
|
|
bodyPartId = part.mMale;
|
|
|
|
|
|
|
|
if (!bodyPartId.empty())
|
2018-08-24 06:44:02 +00:00
|
|
|
{
|
|
|
|
actorData.parts.emplace(static_cast<ESM::PartReferenceType>(part.mPart), bodyPartId);
|
|
|
|
actorData.dependencies.emplace(bodyPartId, true);
|
|
|
|
}
|
2018-07-28 17:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 06:44:02 +00:00
|
|
|
// Lookup cached race parts
|
|
|
|
auto& raceData = getRaceData(npc.mRace);
|
|
|
|
|
|
|
|
// Changes to race could affect the actor
|
|
|
|
actorData.dependencies.emplace(npc.mRace, true);
|
|
|
|
|
|
|
|
// Fill in the rest with race specific body parts
|
2018-07-28 17:23:43 +00:00
|
|
|
for (int i = 0; i < ESM::PRT_Count; ++i)
|
|
|
|
{
|
|
|
|
auto type = static_cast<ESM::PartReferenceType>(i);
|
2018-08-24 06:44:02 +00:00
|
|
|
if (actorData.parts.find(type) == actorData.parts.end())
|
2018-07-28 17:23:43 +00:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case ESM::PRT_Head:
|
2018-08-24 06:44:02 +00:00
|
|
|
actorData.parts.emplace(type, npc.mHead);
|
|
|
|
actorData.dependencies.emplace(npc.mHead, true);
|
2018-07-28 17:23:43 +00:00
|
|
|
break;
|
|
|
|
case ESM::PRT_Hair:
|
2018-08-24 06:44:02 +00:00
|
|
|
actorData.parts.emplace(type, npc.mHair);
|
|
|
|
actorData.dependencies.emplace(npc.mHair, true);
|
2018-07-28 17:23:43 +00:00
|
|
|
break;
|
|
|
|
case ESM::PRT_Skirt:
|
|
|
|
case ESM::PRT_Shield:
|
|
|
|
case ESM::PRT_RPauldron:
|
|
|
|
case ESM::PRT_LPauldron:
|
|
|
|
case ESM::PRT_Weapon:
|
|
|
|
// No body part associated
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
std::string bodyPartId;
|
|
|
|
// Check female map if applicable
|
|
|
|
if (!npc.isMale())
|
|
|
|
{
|
2018-08-24 06:44:02 +00:00
|
|
|
auto partIt = raceData.femaleParts.find(ESM::getMeshPart(type));
|
|
|
|
if (partIt != raceData.femaleParts.end())
|
2018-07-28 17:23:43 +00:00
|
|
|
bodyPartId = partIt->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check male map next
|
|
|
|
if (bodyPartId.empty() || npc.isMale())
|
|
|
|
{
|
2018-08-24 06:44:02 +00:00
|
|
|
auto partIt = raceData.maleParts.find(ESM::getMeshPart(type));
|
|
|
|
if (partIt != raceData.maleParts.end())
|
2018-07-28 17:23:43 +00:00
|
|
|
bodyPartId = partIt->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add to map
|
|
|
|
if (!bodyPartId.empty())
|
|
|
|
{
|
2018-08-24 06:44:02 +00:00
|
|
|
actorData.parts.emplace(type, bodyPartId);
|
|
|
|
actorData.dependencies.emplace(bodyPartId, true);
|
2018-07-28 17:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 06:44:02 +00:00
|
|
|
// Signal change to actor
|
2018-08-24 02:40:43 +00:00
|
|
|
emit actorChanged(refId);
|
2018-07-28 17:23:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActorAdapter::updateCreature(const std::string& refId)
|
|
|
|
{
|
2018-08-24 06:44:02 +00:00
|
|
|
// Signal change to actor
|
2018-08-24 02:40:43 +00:00
|
|
|
emit actorChanged(refId);
|
|
|
|
}
|
|
|
|
|
2018-08-24 06:44:02 +00:00
|
|
|
void ActorAdapter::updateActorsWithDependency(const std::string& id)
|
2018-08-24 02:40:43 +00:00
|
|
|
{
|
2018-08-24 06:44:02 +00:00
|
|
|
for (auto it : mCachedActors)
|
2018-08-24 02:40:43 +00:00
|
|
|
{
|
2018-08-24 06:44:02 +00:00
|
|
|
auto& deps = it.second.dependencies;
|
|
|
|
if (deps.find(id) != deps.end())
|
|
|
|
updateActor(it.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActorAdapter::updateRacesWithDependency(const std::string& id)
|
|
|
|
{
|
|
|
|
for (auto it : mCachedRaces)
|
|
|
|
{
|
|
|
|
auto& deps = it.second.dependencies;
|
|
|
|
if (deps.find(id) != deps.end())
|
|
|
|
updateRace(it.first);
|
2018-08-24 02:40:43 +00:00
|
|
|
}
|
2018-07-28 17:23:43 +00:00
|
|
|
}
|
|
|
|
}
|