[Client] Update LocalPlayer to C++11

pull/249/merge
Koncord 8 years ago
parent 6222c20e82
commit 58a6a8c3bc

@ -196,14 +196,8 @@ void LocalPlayer::updateStatsDynamic(bool forceUpdate)
// Update stats when they become 0 or they have changed enough // Update stats when they become 0 or they have changed enough
bool shouldUpdateHealth = oldHealth != health && (health.getCurrent() == 0 || abs(oldHealth.getCurrent() - health.getCurrent()) > 3); bool shouldUpdateHealth = oldHealth != health && (health.getCurrent() == 0 || abs(oldHealth.getCurrent() - health.getCurrent()) > 3);
bool shouldUpdateMagicka = false; bool shouldUpdateMagicka = oldMagicka != magicka && (magicka.getCurrent() == 0 || abs(oldMagicka.getCurrent() - magicka.getCurrent()) > 10);
bool shouldUpdateFatigue = false; bool shouldUpdateFatigue = oldFatigue != fatigue && (fatigue.getCurrent() == 0 || abs(oldFatigue.getCurrent() - fatigue.getCurrent()) > 10);
if (!shouldUpdateHealth)
shouldUpdateMagicka = oldMagicka != magicka && (magicka.getCurrent() == 0 || abs(oldMagicka.getCurrent() - magicka.getCurrent()) > 10);
if (!shouldUpdateMagicka)
shouldUpdateFatigue = oldFatigue != fatigue && (fatigue.getCurrent() == 0 || abs(oldFatigue.getCurrent() - fatigue.getCurrent()) > 10);
if (forceUpdate || shouldUpdateHealth || shouldUpdateMagicka || shouldUpdateFatigue) if (forceUpdate || shouldUpdateHealth || shouldUpdateMagicka || shouldUpdateFatigue)
{ {
@ -262,17 +256,14 @@ void LocalPlayer::updateSkills(bool forceUpdate)
// If we only have skill progress, remember it for future packets, // If we only have skill progress, remember it for future packets,
// but don't send a packet just because of this // but don't send a packet just because of this
else if (ptrNpcStats.getSkill(i).getProgress() != npcStats.mSkills[i].mProgress) else if (ptrNpcStats.getSkill(i).getProgress() != npcStats.mSkills[i].mProgress)
{
ptrNpcStats.getSkill(i).writeState(npcStats.mSkills[i]); ptrNpcStats.getSkill(i).writeState(npcStats.mSkills[i]);
} }
}
for (int i = 0; i < 8; i++) for (int i = 0; i < 8; i++)
{ {
if (ptrNpcStats.getSkillIncrease(i) != npcStats.mSkillIncrease[i]) { if (ptrNpcStats.getSkillIncrease(i) != npcStats.mSkillIncrease[i])
npcStats.mSkillIncrease[i] = ptrNpcStats.getSkillIncrease(i); npcStats.mSkillIncrease[i] = ptrNpcStats.getSkillIncrease(i);
} }
}
if (skillsChanged || forceUpdate) if (skillsChanged || forceUpdate)
{ {
@ -329,7 +320,6 @@ void LocalPlayer::updatePosition(bool forceUpdate)
if (forceUpdate || posIsChanging || posWasChanged) if (forceUpdate || posIsChanging || posWasChanged)
{ {
oldRot[0] = position.rot[0]; oldRot[0] = position.rot[0];
oldRot[1] = position.rot[2]; oldRot[1] = position.rot[2];
@ -365,7 +355,8 @@ void LocalPlayer::updateCell(bool forceUpdate)
{ {
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Sending ID_PLAYER_CELL_CHANGE about LocalPlayer to server"); LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Sending ID_PLAYER_CELL_CHANGE about LocalPlayer to server");
LOG_APPEND(Log::LOG_INFO, "- Moved from %s to %s", cell.getDescription().c_str(), ptrCell->getDescription().c_str()); LOG_APPEND(Log::LOG_INFO, "- Moved from %s to %s", cell.getDescription().c_str(),
ptrCell->getDescription().c_str());
if (!Misc::StringUtils::ciEqual(cell.mRegion, ptrCell->mRegion)) if (!Misc::StringUtils::ciEqual(cell.mRegion, ptrCell->mRegion))
{ {
@ -397,15 +388,8 @@ void LocalPlayer::updateCell(bool forceUpdate)
void LocalPlayer::updateChar() void LocalPlayer::updateChar()
{ {
MWBase::Environment::get().getMechanicsManager()->setPlayerRace( MWBase::Environment::get().getMechanicsManager()->setPlayerRace(npc.mRace, npc.isMale(), npc.mHead, npc.mHair);
npc.mRace,
npc.isMale(),
npc.mHead,
npc.mHair
);
MWBase::Environment::get().getMechanicsManager()->setPlayerBirthsign(birthsign); MWBase::Environment::get().getMechanicsManager()->setPlayerBirthsign(birthsign);
MWBase::Environment::get().getWindowManager()->getInventoryWindow()->rebuildAvatar(); MWBase::Environment::get().getWindowManager()->getInventoryWindow()->rebuildAvatar();
} }
@ -466,21 +450,26 @@ void LocalPlayer::updateInventory(bool forceUpdate)
MWWorld::InventoryStore &ptrInventory = ptrPlayer.getClass().getInventoryStore(ptrPlayer); MWWorld::InventoryStore &ptrInventory = ptrPlayer.getClass().getInventoryStore(ptrPlayer);
mwmp::Item item; mwmp::Item item;
auto setItem = [](Item &item, const MWWorld::Ptr &iter) {
item.refId = iter.getCellRef().getRefId();
if (item.refId.find("$dynamic") != string::npos) // skip generated items (self enchanted for e.g.)
return true;
item.count = iter.getRefData().getCount();
item.charge = iter.getCellRef().getCharge();
return false;
};
if (!invChanged) if (!invChanged)
{ {
for (vector<Item>::iterator iter = inventoryChanges.items.begin(); iter != inventoryChanges.items.end(); ++iter) for(const auto &itemOld : inventoryChanges.items)
{ {
MWWorld::ContainerStoreIterator result(ptrInventory.begin()); auto result = ptrInventory.begin();
for (; result != ptrInventory.end(); ++result) for (; result != ptrInventory.end(); ++result)
{ {
item.refId = result->getCellRef().getRefId(); if(setItem(item, *result))
if (item.refId.find("$dynamic") != string::npos) // skip generated items (self enchanted for e.g.)
continue; continue;
item.count = result->getRefData().getCount(); if (item == itemOld)
item.charge = result->getCellRef().getCharge();
if (item == (*iter))
break; break;
} }
if (result == ptrInventory.end()) if (result == ptrInventory.end())
@ -493,24 +482,14 @@ void LocalPlayer::updateInventory(bool forceUpdate)
if (!invChanged) if (!invChanged)
{ {
for (MWWorld::ContainerStoreIterator iter(ptrInventory.begin()); iter != ptrInventory.end(); ++iter) for(const auto &iter : ptrInventory)
{ {
item.refId = iter->getCellRef().getRefId(); if(setItem(item, iter))
if (item.refId.find("$dynamic") != string::npos) // skip generated items (self enchanted for e.g.)
continue; continue;
item.count = iter->getRefData().getCount(); auto items = inventoryChanges.items;
item.charge = iter->getCellRef().getCharge();
vector<Item>::iterator result = inventoryChanges.items.begin();
for (; result != inventoryChanges.items.end(); result++) if (find(items.begin(), items.end(), item) == items.end())
{
if ((*result) == item)
break;
}
if (result == inventoryChanges.items.end())
{ {
invChanged = true; invChanged = true;
break; break;
@ -643,9 +622,8 @@ void LocalPlayer::addItems()
MWWorld::Ptr ptrPlayer = getPlayerPtr(); MWWorld::Ptr ptrPlayer = getPlayerPtr();
MWWorld::ContainerStore &ptrStore = ptrPlayer.getClass().getContainerStore(ptrPlayer); MWWorld::ContainerStore &ptrStore = ptrPlayer.getClass().getContainerStore(ptrPlayer);
for (unsigned int i = 0; i < inventoryChanges.count; i++) for(const auto &item : inventoryChanges.items)
{ {
mwmp::Item item = inventoryChanges.items.at(i);
MWWorld::Ptr itemPtr = *ptrStore.add(item.refId, item.count, ptrPlayer); MWWorld::Ptr itemPtr = *ptrStore.add(item.refId, item.count, ptrPlayer);
if (item.charge != -1) if (item.charge != -1)
itemPtr.getCellRef().setCharge(item.charge); itemPtr.getCellRef().setCharge(item.charge);
@ -657,50 +635,44 @@ void LocalPlayer::addSpells()
MWWorld::Ptr ptrPlayer = getPlayerPtr(); MWWorld::Ptr ptrPlayer = getPlayerPtr();
MWMechanics::Spells &ptrSpells = ptrPlayer.getClass().getCreatureStats(ptrPlayer).getSpells(); MWMechanics::Spells &ptrSpells = ptrPlayer.getClass().getCreatureStats(ptrPlayer).getSpells();
for (vector<ESM::Spell>::const_iterator spell = spellbookChanges.spells.begin(); spell != spellbookChanges.spells.end(); spell++) for(const auto &spell : spellbookChanges.spells)
ptrSpells.add(spell->mId); ptrSpells.add(spell.mId);
} }
void LocalPlayer::addJournalItems() void LocalPlayer::addJournalItems()
{ {
for (unsigned int i = 0; i < journalChanges.count; i++) for(const auto &journalItem : journalChanges.journalItems)
{ {
mwmp::JournalItem journalItem = journalChanges.journalItems.at(i);
if (journalItem.type == JournalItem::ENTRY) if (journalItem.type == JournalItem::ENTRY)
{ {
MWWorld::Ptr ptrFound = MWBase::Environment::get().getWorld()->searchPtr(journalItem.actorRefId, false); MWWorld::Ptr ptrFound = MWBase::Environment::get().getWorld()->searchPtr(journalItem.actorRefId, false);
if (!ptrFound) if (!ptrFound)
{
ptrFound = getPlayerPtr(); ptrFound = getPlayerPtr();
}
MWBase::Environment::get().getJournal()->addEntry(journalItem.quest, journalItem.index, ptrFound); MWBase::Environment::get().getJournal()->addEntry(journalItem.quest, journalItem.index, ptrFound);
} }
else else
{
MWBase::Environment::get().getJournal()->setJournalIndex(journalItem.quest, journalItem.index); MWBase::Environment::get().getJournal()->setJournalIndex(journalItem.quest, journalItem.index);
} }
} }
}
void LocalPlayer::addTopics() void LocalPlayer::addTopics()
{ {
for (unsigned int i = 0; i < topicChanges.count; i++) auto &env = MWBase::Environment::get();
for(const auto &topic : topicChanges.topics)
{ {
mwmp::Topic topic = topicChanges.topics.at(i);
std::string topicId = topic.topicId; std::string topicId = topic.topicId;
// If we're using a translated version of Morrowind, translate this topic from English into our language // If we're using a translated version of Morrowind, translate this topic from English into our language
if (MWBase::Environment::get().getWindowManager()->getTranslationDataStorage().hasTranslation()) if (env.getWindowManager()->getTranslationDataStorage().hasTranslation())
topicId = MWBase::Environment::get().getWindowManager()->getTranslationDataStorage().getLocalizedTopicId(topicId); topicId = env.getWindowManager()->getTranslationDataStorage().getLocalizedTopicId(topicId);
MWBase::Environment::get().getDialogueManager()->addTopic(topicId); env.getDialogueManager()->addTopic(topicId);
if (MWBase::Environment::get().getWindowManager()->containsMode(MWGui::GM_Dialogue)) if (env.getWindowManager()->containsMode(MWGui::GM_Dialogue))
MWBase::Environment::get().getDialogueManager()->updateTopics(); env.getDialogueManager()->updateTopics();
} }
} }
@ -709,24 +681,20 @@ void LocalPlayer::removeItems()
MWWorld::Ptr ptrPlayer = getPlayerPtr(); MWWorld::Ptr ptrPlayer = getPlayerPtr();
MWWorld::ContainerStore &ptrStore = ptrPlayer.getClass().getContainerStore(ptrPlayer); MWWorld::ContainerStore &ptrStore = ptrPlayer.getClass().getContainerStore(ptrPlayer);
for (unsigned int i = 0; i < inventoryChanges.count; i++) for(const auto &item : inventoryChanges.items)
{
mwmp::Item item = inventoryChanges.items.at(i);
ptrStore.remove(item.refId, item.count, ptrPlayer); ptrStore.remove(item.refId, item.count, ptrPlayer);
} }
}
void LocalPlayer::removeSpells() void LocalPlayer::removeSpells()
{ {
MWWorld::Ptr ptrPlayer = getPlayerPtr(); MWWorld::Ptr ptrPlayer = getPlayerPtr();
MWMechanics::Spells &ptrSpells = ptrPlayer.getClass().getCreatureStats(ptrPlayer).getSpells(); MWMechanics::Spells &ptrSpells = ptrPlayer.getClass().getCreatureStats(ptrPlayer).getSpells();
for (vector<ESM::Spell>::const_iterator spell = spellbookChanges.spells.begin(); spell != spellbookChanges.spells.end(); spell++)
{
ptrSpells.remove(spell->mId);
MWBase::WindowManager *wm = MWBase::Environment::get().getWindowManager(); MWBase::WindowManager *wm = MWBase::Environment::get().getWindowManager();
if (spell->mId == wm->getSelectedSpell()) for(const auto &spell : spellbookChanges.spells)
{
ptrSpells.remove(spell.mId);
if (spell.mId == wm->getSelectedSpell())
wm->unsetSelectedSpell(); wm->unsetSelectedSpell();
} }
} }
@ -778,9 +746,7 @@ void LocalPlayer::setSkills()
} }
for (int i = 0; i < 8; ++i) for (int i = 0; i < 8; ++i)
{
ptrNpcStats->setSkillIncrease(i, npcStats.mSkillIncrease[i]); ptrNpcStats->setSkillIncrease(i, npcStats.mSkillIncrease[i]);
}
ptrNpcStats->setLevelProgress(npcStats.mLevelProgress); ptrNpcStats->setLevelProgress(npcStats.mLevelProgress);
} }
@ -811,9 +777,7 @@ void LocalPlayer::setPosition()
// If we're ignoring this position packet because of an invalid cell change, // If we're ignoring this position packet because of an invalid cell change,
// don't make the next one get ignored as well // don't make the next one get ignored as well
if (ignorePosPacket) if (ignorePosPacket)
{
ignorePosPacket = false; ignorePosPacket = false;
}
else else
{ {
world->getPlayer().setTeleported(true); world->getPlayer().setTeleported(true);
@ -906,33 +870,27 @@ void LocalPlayer::setEquipment()
for (int slot = 0; slot < MWWorld::InventoryStore::Slots; slot++) for (int slot = 0; slot < MWWorld::InventoryStore::Slots; slot++)
{ {
mwmp::Item *currentItem = &equipedItems[slot]; mwmp::Item &currentItem = equipedItems[slot];
if (!currentItem->refId.empty()) if (!currentItem.refId.empty())
{ {
MWWorld::ContainerStoreIterator it = ptrInventory.begin(); auto it = find_if(ptrInventory.begin(), ptrInventory.end(), [&currentItem](const MWWorld::Ptr &a) {
for (; it != ptrInventory.end(); ++it) // find item in inventory return Misc::StringUtils::ciEqual(a.getCellRef().getRefId(), currentItem.refId);
});
if (it == ptrInventory.end()) // if not exists add item
{ {
if (::Misc::StringUtils::ciEqual(it->getCellRef().getRefId(), currentItem->refId)) auto equipped = equipedItems[slot];
break; auto addIter = ptrInventory.ContainerStore::add(equipped.refId.c_str(), equipped.count, ptrPlayer);
ptrInventory.equip(slot, addIter, ptrPlayer);
} }
if (it == ptrInventory.end()) // if not exists add item
ptrInventory.equip(
slot,
ptrInventory.ContainerStore::add(
equipedItems[slot].refId.c_str(),
equipedItems[slot].count,
ptrPlayer),
ptrPlayer);
else else
ptrInventory.equip(slot, it, ptrPlayer); ptrInventory.equip(slot, it, ptrPlayer);
} }
else else
{
ptrInventory.unequipSlot(slot, ptrPlayer); ptrInventory.unequipSlot(slot, ptrPlayer);
} }
} }
}
void LocalPlayer::setInventory() void LocalPlayer::setInventory()
{ {
@ -983,10 +941,8 @@ void LocalPlayer::setFactions()
MWWorld::Ptr player = getPlayerPtr(); MWWorld::Ptr player = getPlayerPtr();
MWMechanics::NpcStats &ptrNpcStats = player.getClass().getNpcStats(player); MWMechanics::NpcStats &ptrNpcStats = player.getClass().getNpcStats(player);
for (unsigned int i = 0; i < factionChanges.count; i++) for(const auto &faction : factionChanges.factions)
{ {
mwmp::Faction faction = factionChanges.factions.at(i);
// If the player isn't in this faction, make them join it // If the player isn't in this faction, make them join it
if (!ptrNpcStats.isInFaction(faction.factionId)) if (!ptrNpcStats.isInFaction(faction.factionId))
ptrNpcStats.joinFaction(faction.factionId); ptrNpcStats.joinFaction(faction.factionId);
@ -1015,14 +971,8 @@ void LocalPlayer::setFactions()
void LocalPlayer::setKills() void LocalPlayer::setKills()
{ {
for (unsigned int i = 0; i < killChanges.count; i++) for(const auto &kill : killChanges.kills)
{ MWBase::Environment::get().getMechanicsManager()->setDeaths(kill.refId, kill.number);
mwmp::Kill kill = killChanges.kills.at(i);
std::string refId = kill.refId;
int number = kill.number;
MWBase::Environment::get().getMechanicsManager()->setDeaths(refId, number);
}
} }
void LocalPlayer::setBooks() void LocalPlayer::setBooks()
@ -1030,13 +980,8 @@ void LocalPlayer::setBooks()
MWWorld::Ptr player = getPlayerPtr(); MWWorld::Ptr player = getPlayerPtr();
MWMechanics::NpcStats &ptrNpcStats = player.getClass().getNpcStats(player); MWMechanics::NpcStats &ptrNpcStats = player.getClass().getNpcStats(player);
for (unsigned int i = 0; i < bookChanges.count; i++) for(const auto &book : bookChanges.books)
{ ptrNpcStats.flagAsUsed(book.bookId);
mwmp::Book book = bookChanges.books.at(i);
std::string bookId = book.bookId;
ptrNpcStats.flagAsUsed(bookId);
}
} }
void LocalPlayer::sendClass() void LocalPlayer::sendClass()
@ -1067,14 +1012,14 @@ void LocalPlayer::sendInventory()
inventoryChanges.items.clear(); inventoryChanges.items.clear();
for (MWWorld::ContainerStoreIterator iter(ptrInventory.begin()); iter != ptrInventory.end(); ++iter) for(const auto &iter : ptrInventory)
{ {
item.refId = iter->getCellRef().getRefId(); item.refId = iter.getCellRef().getRefId();
if (item.refId.find("$dynamic") != string::npos) // skip generated items (self enchanted for e.g.) if (item.refId.find("$dynamic") != string::npos) // skip generated items (self enchanted for e.g.)
continue; continue;
item.count = iter->getRefData().getCount(); item.count = iter.getRefData().getCount();
item.charge = iter->getCellRef().getCharge(); item.charge = iter.getCellRef().getCharge();
inventoryChanges.items.push_back(item); inventoryChanges.items.push_back(item);
} }
@ -1093,14 +1038,10 @@ void LocalPlayer::sendSpellbook()
spellbookChanges.spells.clear(); spellbookChanges.spells.clear();
// Send spells in spellbook, while ignoring abilities, powers, etc. // Send spells in spellbook, while ignoring abilities, powers, etc.
for (MWMechanics::Spells::TIterator iter = ptrSpells.begin(); iter != ptrSpells.end(); ++iter) for(const auto &spell : ptrSpells)
{ {
const ESM::Spell *spell = iter->first; if (spell.first->mData.mType == ESM::Spell::ST_Spell)
spellbookChanges.spells.push_back(*spell.first);
if (spell->mData.mType == ESM::Spell::ST_Spell)
{
spellbookChanges.spells.push_back(*spell);
}
} }
spellbookChanges.action = SpellbookChanges::SET; spellbookChanges.action = SpellbookChanges::SET;
@ -1275,9 +1216,7 @@ void LocalPlayer::storeCellState(ESM::Cell cell, int stateType)
// If there's already a cell state recorded for this particular cell, // If there's already a cell state recorded for this particular cell,
// remove it // remove it
if (cell.getDescription() == (*iter).cell.getDescription()) if (cell.getDescription() == (*iter).cell.getDescription())
{
iter = cellStateChanges.cellStates.erase(iter); iter = cellStateChanges.cellStates.erase(iter);
}
else else
++iter; ++iter;
} }

@ -327,9 +327,9 @@ bool Main::isValidPacketScript(std::string script)
"ouch_wraithguard" "ouch_wraithguard"
}; };
for (int i = 0; i < validPacketScriptsCount; i++) for(const auto &validPacketScript : validPacketScripts)
{ {
if (Misc::StringUtils::ciEqual(script, validPacketScripts[i])) if (Misc::StringUtils::ciEqual(script, validPacketScript))
return true; return true;
} }

Loading…
Cancel
Save