mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-01-16 08:49:55 +00:00
359 lines
No EOL
13 KiB
C++
359 lines
No EOL
13 KiB
C++
|
|
#include "refidcollection.hpp"
|
|
|
|
#include <stdexcept>
|
|
|
|
#include "refidadapter.hpp"
|
|
#include "refidadapterimp.hpp"
|
|
|
|
CSMWorld::RefIdColumn::RefIdColumn (const std::string& title, Display displayType, int flag,
|
|
bool editable, bool userEditable)
|
|
: ColumnBase (title, displayType, flag), mEditable (editable), mUserEditable (userEditable)
|
|
{}
|
|
|
|
bool CSMWorld::RefIdColumn::isEditable() const
|
|
{
|
|
return mEditable;
|
|
}
|
|
|
|
bool CSMWorld::RefIdColumn::isUserEditable() const
|
|
{
|
|
return mUserEditable;
|
|
}
|
|
|
|
|
|
const CSMWorld::RefIdAdapter& CSMWorld::RefIdCollection::findAdaptor (UniversalId::Type type) const
|
|
{
|
|
std::map<UniversalId::Type, RefIdAdapter *>::const_iterator iter = mAdapters.find (type);
|
|
|
|
if (iter==mAdapters.end())
|
|
throw std::logic_error ("unsupported type in RefIdCollection");
|
|
|
|
return *iter->second;
|
|
}
|
|
|
|
CSMWorld::RefIdCollection::RefIdCollection()
|
|
{
|
|
BaseColumns baseColumns;
|
|
|
|
mColumns.push_back (RefIdColumn ("ID", ColumnBase::Display_String,
|
|
ColumnBase::Flag_Table | ColumnBase::Flag_Dialogue, false, false));
|
|
baseColumns.mId = &mColumns.back();
|
|
mColumns.push_back (RefIdColumn ("*", ColumnBase::Display_Integer,
|
|
ColumnBase::Flag_Table | ColumnBase::Flag_Dialogue, false, false));
|
|
baseColumns.mModified = &mColumns.back();
|
|
mColumns.push_back (RefIdColumn ("Type", ColumnBase::Display_Integer,
|
|
ColumnBase::Flag_Table | ColumnBase::Flag_Dialogue, false, false));
|
|
baseColumns.mType = &mColumns.back();
|
|
|
|
ModelColumns modelColumns (baseColumns);
|
|
|
|
mColumns.push_back (RefIdColumn ("Model", ColumnBase::Display_String));
|
|
modelColumns.mModel = &mColumns.back();
|
|
|
|
NameColumns nameColumns (modelColumns);
|
|
|
|
mColumns.push_back (RefIdColumn ("Name", ColumnBase::Display_String));
|
|
nameColumns.mName = &mColumns.back();
|
|
mColumns.push_back (RefIdColumn ("Script", ColumnBase::Display_String));
|
|
nameColumns.mScript = &mColumns.back();
|
|
|
|
InventoryColumns inventoryColumns (nameColumns);
|
|
|
|
mColumns.push_back (RefIdColumn ("Icon", ColumnBase::Display_String));
|
|
inventoryColumns.mIcon = &mColumns.back();
|
|
mColumns.push_back (RefIdColumn ("Weight", ColumnBase::Display_Float));
|
|
inventoryColumns.mWeight = &mColumns.back();
|
|
mColumns.push_back (RefIdColumn ("Value", ColumnBase::Display_Integer));
|
|
inventoryColumns.mValue = &mColumns.back();
|
|
|
|
EnchantableColumns enchantableColumns (inventoryColumns);
|
|
|
|
mColumns.push_back (RefIdColumn ("Enchantment", ColumnBase::Display_String));
|
|
enchantableColumns.mEnchantment = &mColumns.back();
|
|
mColumns.push_back (RefIdColumn ("Enchantment Points", ColumnBase::Display_Integer));
|
|
enchantableColumns.mEnchantmentPoints = &mColumns.back();
|
|
|
|
ToolColumns toolsColumns (inventoryColumns);
|
|
|
|
mColumns.push_back (RefIdColumn ("Quality", ColumnBase::Display_Float));
|
|
toolsColumns.mQuality = &mColumns.back();
|
|
mColumns.push_back (RefIdColumn ("Uses", ColumnBase::Display_Integer));
|
|
toolsColumns.mUses = &mColumns.back();
|
|
|
|
ActorColumns actorsColumns (nameColumns);
|
|
|
|
mColumns.push_back (RefIdColumn ("AI", ColumnBase::Display_Boolean));
|
|
actorsColumns.mHasAi = &mColumns.back();
|
|
mColumns.push_back (RefIdColumn ("AI Hello", ColumnBase::Display_Integer));
|
|
actorsColumns.mHello = &mColumns.back();
|
|
mColumns.push_back (RefIdColumn ("AI Flee", ColumnBase::Display_Integer));
|
|
actorsColumns.mFlee = &mColumns.back();
|
|
mColumns.push_back (RefIdColumn ("AI Fight", ColumnBase::Display_Integer));
|
|
actorsColumns.mFight = &mColumns.back();
|
|
mColumns.push_back (RefIdColumn ("AI Alarm", ColumnBase::Display_Integer));
|
|
actorsColumns.mAlarm = &mColumns.back();
|
|
|
|
static const struct
|
|
{
|
|
const char *mName;
|
|
unsigned int mFlag;
|
|
} sServiceTable[] =
|
|
{
|
|
{ "Buys Weapons", ESM::NPC::Weapon},
|
|
{ "Buys Armor", ESM::NPC::Armor},
|
|
{ "Buys Clothing", ESM::NPC::Clothing},
|
|
{ "Buys Books", ESM::NPC::Books},
|
|
{ "Buys Ingredients", ESM::NPC::Ingredients},
|
|
{ "Buys Lockpicks", ESM::NPC::Picks},
|
|
{ "Buys Probes", ESM::NPC::Probes},
|
|
{ "Buys Lights", ESM::NPC::Lights},
|
|
{ "Buys Apparati", ESM::NPC::Apparatus},
|
|
{ "Buys Repair Items", ESM::NPC::RepairItem},
|
|
{ "Buys Misc Items", ESM::NPC::Misc},
|
|
{ "Buys Potions", ESM::NPC::Potions},
|
|
{ "Buys Magic Items", ESM::NPC::MagicItems},
|
|
{ "Sells Spells", ESM::NPC::Spells},
|
|
{ "Trainer", ESM::NPC::Training},
|
|
{ "Spellmaking", ESM::NPC::Spellmaking},
|
|
{ "Enchanting Service", ESM::NPC::Enchanting},
|
|
{ "Repair Serivce", ESM::NPC::Repair},
|
|
{ 0, 0 }
|
|
};
|
|
|
|
for (int i=0; sServiceTable[i].mName; ++i)
|
|
{
|
|
mColumns.push_back (RefIdColumn (sServiceTable[i].mName, ColumnBase::Display_Boolean));
|
|
actorsColumns.mServices.insert (std::make_pair (&mColumns.back(), sServiceTable[i].mFlag));
|
|
}
|
|
|
|
mColumns.push_back (RefIdColumn ("Auto Calc", ColumnBase::Display_Boolean));
|
|
const RefIdColumn *autoCalc = &mColumns.back();
|
|
|
|
mColumns.push_back (RefIdColumn ("Apparatus Type", ColumnBase::Display_ApparatusType));
|
|
const RefIdColumn *apparatusType = &mColumns.back();
|
|
|
|
mColumns.push_back (RefIdColumn ("Armor Type", ColumnBase::Display_ArmorType));
|
|
const RefIdColumn *armorType = &mColumns.back();
|
|
|
|
mColumns.push_back (RefIdColumn ("Health", ColumnBase::Display_Integer));
|
|
const RefIdColumn *health = &mColumns.back();
|
|
|
|
mColumns.push_back (RefIdColumn ("Armor Value", ColumnBase::Display_Integer));
|
|
const RefIdColumn *armor = &mColumns.back();
|
|
|
|
mColumns.push_back (RefIdColumn ("Scroll", ColumnBase::Display_Boolean));
|
|
const RefIdColumn *scroll = &mColumns.back();
|
|
|
|
mColumns.push_back (RefIdColumn ("Attribute", ColumnBase::Display_Attribute));
|
|
const RefIdColumn *attribute = &mColumns.back();
|
|
|
|
mAdapters.insert (std::make_pair (UniversalId::Type_Activator,
|
|
new NameRefIdAdapter<ESM::Activator> (UniversalId::Type_Activator, nameColumns)));
|
|
mAdapters.insert (std::make_pair (UniversalId::Type_Potion,
|
|
new PotionRefIdAdapter (inventoryColumns, autoCalc)));
|
|
mAdapters.insert (std::make_pair (UniversalId::Type_Apparatus,
|
|
new ApparatusRefIdAdapter (inventoryColumns, apparatusType, toolsColumns.mQuality)));
|
|
mAdapters.insert (std::make_pair (UniversalId::Type_Armor,
|
|
new ArmorRefIdAdapter (enchantableColumns, armorType, health, armor)));
|
|
mAdapters.insert (std::make_pair (UniversalId::Type_Book,
|
|
new BookRefIdAdapter (enchantableColumns, scroll, attribute)));
|
|
mAdapters.insert (std::make_pair (UniversalId::Type_Clothing,
|
|
new EnchantableRefIdAdapter<ESM::Clothing> (UniversalId::Type_Clothing, enchantableColumns)));
|
|
mAdapters.insert (std::make_pair (UniversalId::Type_Container,
|
|
new NameRefIdAdapter<ESM::Container> (UniversalId::Type_Container, nameColumns)));
|
|
mAdapters.insert (std::make_pair (UniversalId::Type_Creature,
|
|
new ActorRefIdAdapter<ESM::Creature> (UniversalId::Type_Creature, actorsColumns)));
|
|
mAdapters.insert (std::make_pair (UniversalId::Type_Door,
|
|
new NameRefIdAdapter<ESM::Door> (UniversalId::Type_Door, nameColumns)));
|
|
mAdapters.insert (std::make_pair (UniversalId::Type_Ingredient,
|
|
new InventoryRefIdAdapter<ESM::Ingredient> (UniversalId::Type_Ingredient, inventoryColumns)));
|
|
mAdapters.insert (std::make_pair (UniversalId::Type_CreatureLevelledList,
|
|
new BaseRefIdAdapter<ESM::CreatureLevList> (
|
|
UniversalId::Type_CreatureLevelledList, baseColumns)));
|
|
mAdapters.insert (std::make_pair (UniversalId::Type_ItemLevelledList,
|
|
new BaseRefIdAdapter<ESM::ItemLevList> (UniversalId::Type_ItemLevelledList, baseColumns)));
|
|
mAdapters.insert (std::make_pair (UniversalId::Type_Light,
|
|
new InventoryRefIdAdapter<ESM::Light> (UniversalId::Type_Light, inventoryColumns)));
|
|
mAdapters.insert (std::make_pair (UniversalId::Type_Lockpick,
|
|
new ToolRefIdAdapter<ESM::Lockpick> (UniversalId::Type_Lockpick, toolsColumns)));
|
|
mAdapters.insert (std::make_pair (UniversalId::Type_Miscellaneous,
|
|
new InventoryRefIdAdapter<ESM::Miscellaneous> (UniversalId::Type_Miscellaneous,
|
|
inventoryColumns)));
|
|
mAdapters.insert (std::make_pair (UniversalId::Type_Npc,
|
|
new ActorRefIdAdapter<ESM::NPC> (UniversalId::Type_Npc, actorsColumns)));
|
|
mAdapters.insert (std::make_pair (UniversalId::Type_Probe,
|
|
new ToolRefIdAdapter<ESM::Probe> (UniversalId::Type_Probe, toolsColumns)));
|
|
mAdapters.insert (std::make_pair (UniversalId::Type_Repair,
|
|
new ToolRefIdAdapter<ESM::Repair> (UniversalId::Type_Repair, toolsColumns)));
|
|
mAdapters.insert (std::make_pair (UniversalId::Type_Static,
|
|
new ModelRefIdAdapter<ESM::Static> (UniversalId::Type_Static, modelColumns)));
|
|
mAdapters.insert (std::make_pair (UniversalId::Type_Weapon,
|
|
new EnchantableRefIdAdapter<ESM::Weapon> (UniversalId::Type_Weapon, enchantableColumns)));
|
|
}
|
|
|
|
CSMWorld::RefIdCollection::~RefIdCollection()
|
|
{
|
|
for (std::map<UniversalId::Type, RefIdAdapter *>::iterator iter (mAdapters.begin());
|
|
iter!=mAdapters.end(); ++iter)
|
|
delete iter->second;
|
|
}
|
|
|
|
int CSMWorld::RefIdCollection::getSize() const
|
|
{
|
|
return mData.getSize();
|
|
}
|
|
|
|
std::string CSMWorld::RefIdCollection::getId (int index) const
|
|
{
|
|
return getData (index, 0).toString().toUtf8().constData();
|
|
}
|
|
|
|
int CSMWorld::RefIdCollection::getIndex (const std::string& id) const
|
|
{
|
|
int index = searchId (id);
|
|
|
|
if (index==-1)
|
|
throw std::runtime_error ("invalid ID: " + id);
|
|
|
|
return index;
|
|
}
|
|
|
|
int CSMWorld::RefIdCollection::getColumns() const
|
|
{
|
|
return mColumns.size();
|
|
}
|
|
|
|
const CSMWorld::ColumnBase& CSMWorld::RefIdCollection::getColumn (int column) const
|
|
{
|
|
return mColumns.at (column);
|
|
}
|
|
|
|
QVariant CSMWorld::RefIdCollection::getData (int index, int column) const
|
|
{
|
|
RefIdData::LocalIndex localIndex = mData.globalToLocalIndex (index);
|
|
|
|
const RefIdAdapter& adaptor = findAdaptor (localIndex.second);
|
|
|
|
return adaptor.getData (&mColumns.at (column), mData, localIndex.first);
|
|
}
|
|
|
|
void CSMWorld::RefIdCollection::setData (int index, int column, const QVariant& data)
|
|
{
|
|
RefIdData::LocalIndex localIndex = mData.globalToLocalIndex (index);
|
|
|
|
const RefIdAdapter& adaptor = findAdaptor (localIndex.second);
|
|
|
|
adaptor.setData (&mColumns.at (column), mData, localIndex.first, data);
|
|
}
|
|
|
|
void CSMWorld::RefIdCollection::removeRows (int index, int count)
|
|
{
|
|
mData.erase (index, count);
|
|
}
|
|
|
|
void CSMWorld::RefIdCollection::appendBlankRecord (const std::string& id, UniversalId::Type type)
|
|
{
|
|
mData.appendRecord (type, id);
|
|
}
|
|
|
|
int CSMWorld::RefIdCollection::searchId (const std::string& id) const
|
|
{
|
|
RefIdData::LocalIndex localIndex = mData.searchId (id);
|
|
|
|
if (localIndex.first==-1)
|
|
return -1;
|
|
|
|
return mData.localToGlobalIndex (localIndex);
|
|
}
|
|
|
|
void CSMWorld::RefIdCollection::replace (int index, const RecordBase& record)
|
|
{
|
|
mData.getRecord (mData.globalToLocalIndex (index)).assign (record);
|
|
}
|
|
|
|
void CSMWorld::RefIdCollection::appendRecord (const RecordBase& record,
|
|
UniversalId::Type type)
|
|
{
|
|
std::string id = findAdaptor (type).getId (record);
|
|
|
|
int index = mData.getAppendIndex (type);
|
|
|
|
mData.appendRecord (type, id);
|
|
|
|
mData.getRecord (mData.globalToLocalIndex (index)).assign (record);
|
|
}
|
|
|
|
const CSMWorld::RecordBase& CSMWorld::RefIdCollection::getRecord (const std::string& id) const
|
|
{
|
|
return mData.getRecord (mData.searchId (id));
|
|
}
|
|
|
|
const CSMWorld::RecordBase& CSMWorld::RefIdCollection::getRecord (int index) const
|
|
{
|
|
return mData.getRecord (mData.globalToLocalIndex (index));
|
|
}
|
|
|
|
void CSMWorld::RefIdCollection::load (ESM::ESMReader& reader, bool base, UniversalId::Type type)
|
|
{
|
|
std::string id = reader.getHNOString ("NAME");
|
|
|
|
int index = searchId (id);
|
|
|
|
if (reader.isNextSub ("DELE"))
|
|
{
|
|
reader.skipRecord();
|
|
|
|
if (index==-1)
|
|
{
|
|
// deleting a record that does not exist
|
|
|
|
// ignore it for now
|
|
|
|
/// \todo report the problem to the user
|
|
}
|
|
else if (base)
|
|
{
|
|
mData.erase (index, 1);
|
|
}
|
|
else
|
|
{
|
|
mData.getRecord (mData.globalToLocalIndex (index)).mState = RecordBase::State_Deleted;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (index==-1)
|
|
{
|
|
// new record
|
|
int index = mData.getAppendIndex (type);
|
|
mData.appendRecord (type, id);
|
|
|
|
RefIdData::LocalIndex localIndex = mData.globalToLocalIndex (index);
|
|
|
|
mData.load (localIndex, reader, base);
|
|
|
|
mData.getRecord (localIndex).mState =
|
|
base ? RecordBase::State_BaseOnly : RecordBase::State_ModifiedOnly;
|
|
}
|
|
else
|
|
{
|
|
// old record
|
|
RefIdData::LocalIndex localIndex = mData.globalToLocalIndex (index);
|
|
|
|
if (!base)
|
|
if (mData.getRecord (localIndex).mState==RecordBase::State_Erased)
|
|
throw std::logic_error ("attempt to access a deleted record");
|
|
|
|
mData.load (localIndex, reader, base);
|
|
|
|
if (!base)
|
|
mData.getRecord (localIndex).mState = RecordBase::State_Modified;
|
|
}
|
|
}
|
|
}
|
|
|
|
int CSMWorld::RefIdCollection::getAppendIndex (UniversalId::Type type) const
|
|
{
|
|
return mData.getAppendIndex (type);
|
|
} |