1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-22 00:53:50 +00:00

Fixed OpAddItem, OpRemoveItem

This commit is contained in:
scrawl 2013-02-17 19:44:00 +01:00
parent e1ca0a15ae
commit 52d0f0b750

View file

@ -21,6 +21,7 @@
#include "../mwworld/containerstore.hpp" #include "../mwworld/containerstore.hpp"
#include "../mwworld/actionequip.hpp" #include "../mwworld/actionequip.hpp"
#include "../mwworld/inventorystore.hpp" #include "../mwworld/inventorystore.hpp"
#include "../mwworld/player.hpp"
#include "interpretercontext.hpp" #include "interpretercontext.hpp"
#include "ref.hpp" #include "ref.hpp"
@ -47,6 +48,10 @@ namespace MWScript
if (count<0) if (count<0)
throw std::runtime_error ("second argument for AddItem must be non-negative"); throw std::runtime_error ("second argument for AddItem must be non-negative");
// no-op
if (count == 0)
return;
MWWorld::ManualRef ref (MWBase::Environment::get().getWorld()->getStore(), item); MWWorld::ManualRef ref (MWBase::Environment::get().getWorld()->getStore(), item);
ref.getPtr().getRefData().setCount (count); ref.getPtr().getRefData().setCount (count);
@ -61,27 +66,26 @@ namespace MWScript
MWWorld::Class::get (ptr).getContainerStore (ptr).add (ref.getPtr()); MWWorld::Class::get (ptr).getContainerStore (ptr).add (ref.getPtr());
// Spawn a messagebox (only for items added to player's inventory)
if (ptr == MWBase::Environment::get().getWorld ()->getPlayer ().getPlayer())
{
// The two GMST entries below expand to strings informing the player of what, and how many of it has been added to their inventory // The two GMST entries below expand to strings informing the player of what, and how many of it has been added to their inventory
std::string msgBox; std::string msgBox;
std::string itemName = MWWorld::Class::get(ref.getPtr()).getName(ref.getPtr()); std::string itemName = MWWorld::Class::get(ref.getPtr()).getName(ref.getPtr());
if (count == 1) if (count == 1)
{ {
msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage60}"); msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage60}");
std::stringstream temp; msgBox = boost::str(boost::format(msgBox) % itemName);
temp << boost::format(msgBox) % itemName;
msgBox = temp.str();
} }
else else
{ {
msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage61}"); msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage61}");
std::stringstream temp; msgBox = boost::str(boost::format(msgBox) % count % itemName);
temp << boost::format(msgBox) % (count) % itemName;
msgBox = temp.str();
} }
if(count > 0)
MWBase::Environment::get().getWindowManager()->messageBox(msgBox, std::vector<std::string>()); MWBase::Environment::get().getWindowManager()->messageBox(msgBox, std::vector<std::string>());
} }
}
}; };
template<class R> template<class R>
@ -126,9 +130,15 @@ namespace MWScript
if (count<0) if (count<0)
throw std::runtime_error ("second argument for RemoveItem must be non-negative"); throw std::runtime_error ("second argument for RemoveItem must be non-negative");
// no-op
if (count == 0)
return;
MWWorld::ContainerStore& store = MWWorld::Class::get (ptr).getContainerStore (ptr); MWWorld::ContainerStore& store = MWWorld::Class::get (ptr).getContainerStore (ptr);
std::string itemName = ""; std::string itemName = "";
// originalCount holds the total number of items to remove, count holds the remaining number of items to remove
Interpreter::Type_Integer originalCount = count; Interpreter::Type_Integer originalCount = count;
for (MWWorld::ContainerStoreIterator iter (store.begin()); iter!=store.end() && count; for (MWWorld::ContainerStoreIterator iter (store.begin()); iter!=store.end() && count;
@ -151,28 +161,26 @@ namespace MWScript
} }
} }
// Spawn a messagebox (only for items added to player's inventory)
if (ptr == MWBase::Environment::get().getWorld ()->getPlayer ().getPlayer())
{
// The two GMST entries below expand to strings informing the player of what, and how many of it has been removed from their inventory // The two GMST entries below expand to strings informing the player of what, and how many of it has been removed from their inventory
std::string msgBox; std::string msgBox;
if(originalCount - count > 1) int numRemoved = (originalCount - count);
if(numRemoved > 1)
{ {
msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage63}"); msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage63}");
std::stringstream temp; msgBox = boost::str (boost::format(msgBox) % numRemoved % itemName);
temp << boost::format(msgBox) % (originalCount - count) % itemName;
msgBox = temp.str();
} }
else else
{ {
msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage62}"); msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage62}");
std::stringstream temp; msgBox = boost::str (boost::format(msgBox) % itemName);
temp << boost::format(msgBox) % itemName;
msgBox = temp.str();
} }
if(originalCount - count > 0) if (numRemoved > 0)
MWBase::Environment::get().getWindowManager()->messageBox(msgBox, std::vector<std::string>()); MWBase::Environment::get().getWindowManager()->messageBox(msgBox, std::vector<std::string>());
}
// To be fully compatible with original Morrowind, we would need to check if
// count is >= 0 here and throw an exception. But let's be tollerant instead.
} }
}; };