1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-21 01:23:53 +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,26 +66,25 @@ namespace MWScript
MWWorld::Class::get (ptr).getContainerStore (ptr).add (ref.getPtr()); MWWorld::Class::get (ptr).getContainerStore (ptr).add (ref.getPtr());
// The two GMST entries below expand to strings informing the player of what, and how many of it has been added to their inventory // Spawn a messagebox (only for items added to player's inventory)
std::string msgBox; if (ptr == MWBase::Environment::get().getWorld ()->getPlayer ().getPlayer())
std::string itemName = MWWorld::Class::get(ref.getPtr()).getName(ref.getPtr());
if (count == 1)
{ {
msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage60}"); // 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::stringstream temp; std::string msgBox;
temp << boost::format(msgBox) % itemName; std::string itemName = MWWorld::Class::get(ref.getPtr()).getName(ref.getPtr());
msgBox = temp.str(); if (count == 1)
} {
else msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage60}");
{ msgBox = boost::str(boost::format(msgBox) % itemName);
msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage61}"); }
std::stringstream temp; else
temp << boost::format(msgBox) % (count) % itemName; {
msgBox = temp.str(); msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage61}");
} msgBox = boost::str(boost::format(msgBox) % count % itemName);
}
if(count > 0)
MWBase::Environment::get().getWindowManager()->messageBox(msgBox, std::vector<std::string>()); MWBase::Environment::get().getWindowManager()->messageBox(msgBox, std::vector<std::string>());
}
} }
}; };
@ -122,13 +126,19 @@ namespace MWScript
Interpreter::Type_Integer count = runtime[0].mInteger; Interpreter::Type_Integer count = runtime[0].mInteger;
runtime.pop(); runtime.pop();
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
} }
} }
// The two GMST entries below expand to strings informing the player of what, and how many of it has been removed from their inventory // Spawn a messagebox (only for items added to player's inventory)
std::string msgBox; if (ptr == MWBase::Environment::get().getWorld ()->getPlayer ().getPlayer())
if(originalCount - count > 1)
{ {
msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage63}"); // 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::stringstream temp; std::string msgBox;
temp << boost::format(msgBox) % (originalCount - count) % itemName; int numRemoved = (originalCount - count);
msgBox = temp.str(); if(numRemoved > 1)
} {
else msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage63}");
{ msgBox = boost::str (boost::format(msgBox) % numRemoved % itemName);
msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage62}"); }
std::stringstream temp; else
temp << boost::format(msgBox) % itemName; {
msgBox = temp.str(); msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage62}");
} msgBox = boost::str (boost::format(msgBox) % itemName);
}
if(originalCount - count > 0)
MWBase::Environment::get().getWindowManager()->messageBox(msgBox, std::vector<std::string>());
// To be fully compatible with original Morrowind, we would need to check if if (numRemoved > 0)
// count is >= 0 here and throw an exception. But let's be tollerant instead. MWBase::Environment::get().getWindowManager()->messageBox(msgBox, std::vector<std::string>());
}
} }
}; };