2013-03-22 13:13:10 +00:00
|
|
|
#include "merchantrepair.hpp"
|
|
|
|
|
2014-02-23 19:11:05 +00:00
|
|
|
#include <components/esm/loadgmst.hpp>
|
|
|
|
|
2015-01-10 01:50:43 +00:00
|
|
|
#include <MyGUI_Button.h>
|
|
|
|
#include <MyGUI_ScrollView.h>
|
|
|
|
#include <MyGUI_Gui.h>
|
|
|
|
|
2020-05-20 05:09:25 +00:00
|
|
|
/*
|
|
|
|
Start of tes3mp addition
|
|
|
|
|
|
|
|
Include additional headers for multiplayer purposes
|
|
|
|
*/
|
|
|
|
#include "../mwmp/Main.hpp"
|
|
|
|
#include "../mwmp/Networking.hpp"
|
|
|
|
#include "../mwmp/ObjectList.hpp"
|
|
|
|
/*
|
|
|
|
End of tes3mp addition
|
|
|
|
*/
|
|
|
|
|
2013-03-22 13:13:10 +00:00
|
|
|
#include "../mwbase/world.hpp"
|
|
|
|
#include "../mwbase/environment.hpp"
|
|
|
|
#include "../mwbase/mechanicsmanager.hpp"
|
|
|
|
#include "../mwbase/windowmanager.hpp"
|
|
|
|
|
2014-07-27 22:55:57 +00:00
|
|
|
#include "../mwmechanics/creaturestats.hpp"
|
2015-08-21 09:12:39 +00:00
|
|
|
#include "../mwmechanics/actorutil.hpp"
|
2014-07-27 22:55:57 +00:00
|
|
|
|
2013-05-11 16:38:27 +00:00
|
|
|
#include "../mwworld/class.hpp"
|
|
|
|
#include "../mwworld/containerstore.hpp"
|
2014-02-23 19:11:05 +00:00
|
|
|
#include "../mwworld/esmstore.hpp"
|
2013-03-22 13:13:10 +00:00
|
|
|
|
|
|
|
namespace MWGui
|
|
|
|
{
|
2013-04-10 18:46:21 +00:00
|
|
|
MerchantRepair::MerchantRepair()
|
|
|
|
: WindowBase("openmw_merchantrepair.layout")
|
2013-03-22 13:13:10 +00:00
|
|
|
{
|
|
|
|
getWidget(mList, "RepairView");
|
|
|
|
getWidget(mOkButton, "OkButton");
|
|
|
|
getWidget(mGoldLabel, "PlayerGold");
|
|
|
|
|
|
|
|
mOkButton->eventMouseButtonClick += MyGUI::newDelegate(this, &MerchantRepair::onOkButtonClick);
|
|
|
|
}
|
|
|
|
|
2017-09-22 19:26:41 +00:00
|
|
|
void MerchantRepair::setPtr(const MWWorld::Ptr &actor)
|
2013-03-22 13:13:10 +00:00
|
|
|
{
|
|
|
|
mActor = actor;
|
|
|
|
|
|
|
|
while (mList->getChildCount())
|
|
|
|
MyGUI::Gui::getInstance().destroyWidget(mList->getChildAt(0));
|
|
|
|
|
2018-06-18 09:43:39 +00:00
|
|
|
int lineHeight = MWBase::Environment::get().getWindowManager()->getFontHeight() + 2;
|
2013-03-22 13:13:10 +00:00
|
|
|
int currentY = 0;
|
|
|
|
|
2015-08-21 09:12:39 +00:00
|
|
|
MWWorld::Ptr player = MWMechanics::getPlayer();
|
2014-01-08 22:37:46 +00:00
|
|
|
int playerGold = player.getClass().getContainerStore(player).count(MWWorld::ContainerStore::sGoldId);
|
|
|
|
|
2017-03-13 01:42:43 +00:00
|
|
|
MWWorld::ContainerStore& store = player.getClass().getContainerStore(player);
|
2013-04-03 19:13:01 +00:00
|
|
|
int categories = MWWorld::ContainerStore::Type_Weapon | MWWorld::ContainerStore::Type_Armor;
|
2017-03-13 01:42:43 +00:00
|
|
|
for (MWWorld::ContainerStoreIterator iter (store.begin(categories)); iter!=store.end(); ++iter)
|
2013-03-22 13:13:10 +00:00
|
|
|
{
|
2014-05-22 18:37:22 +00:00
|
|
|
if (iter->getClass().hasItemHealth(*iter))
|
2013-03-22 13:13:10 +00:00
|
|
|
{
|
2014-05-22 18:37:22 +00:00
|
|
|
int maxDurability = iter->getClass().getItemMaxHealth(*iter);
|
2014-05-25 12:13:07 +00:00
|
|
|
int durability = iter->getClass().getItemHealth(*iter);
|
2018-10-24 15:51:34 +00:00
|
|
|
if (maxDurability == durability || maxDurability == 0)
|
2013-03-22 13:13:10 +00:00
|
|
|
continue;
|
|
|
|
|
2014-05-22 18:37:22 +00:00
|
|
|
int basePrice = iter->getClass().getValue(*iter);
|
2013-03-22 13:13:10 +00:00
|
|
|
float fRepairMult = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>()
|
2018-08-29 15:38:12 +00:00
|
|
|
.find("fRepairMult")->mValue.getFloat();
|
2013-03-22 13:13:10 +00:00
|
|
|
|
2015-03-08 00:07:29 +00:00
|
|
|
float p = static_cast<float>(std::max(1, basePrice));
|
|
|
|
float r = static_cast<float>(std::max(1, static_cast<int>(maxDurability / p)));
|
2013-03-22 13:13:10 +00:00
|
|
|
|
2015-03-08 00:07:29 +00:00
|
|
|
int x = static_cast<int>((maxDurability - durability) / r);
|
|
|
|
x = static_cast<int>(fRepairMult * x);
|
2013-03-22 13:13:10 +00:00
|
|
|
|
|
|
|
int price = MWBase::Environment::get().getMechanicsManager()->getBarterOffer(mActor, x, true);
|
|
|
|
|
2014-05-22 18:37:22 +00:00
|
|
|
std::string name = iter->getClass().getName(*iter)
|
2015-01-10 02:01:01 +00:00
|
|
|
+ " - " + MyGUI::utility::toString(price)
|
2013-03-22 13:13:10 +00:00
|
|
|
+ MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>()
|
2018-08-29 15:38:12 +00:00
|
|
|
.find("sgp")->mValue.getString();
|
2013-03-22 13:13:10 +00:00
|
|
|
|
|
|
|
MyGUI::Button* button =
|
2014-10-17 16:58:20 +00:00
|
|
|
mList->createWidget<MyGUI::Button>(price <= playerGold ? "SandTextButton" : "SandTextButtonDisabled", // can't use setEnabled since that removes tooltip
|
2013-03-22 13:13:10 +00:00
|
|
|
0,
|
|
|
|
currentY,
|
|
|
|
0,
|
2018-06-18 09:43:39 +00:00
|
|
|
lineHeight,
|
2013-03-22 13:13:10 +00:00
|
|
|
MyGUI::Align::Default
|
|
|
|
);
|
|
|
|
|
2018-06-18 09:43:39 +00:00
|
|
|
currentY += lineHeight;
|
2013-03-22 13:13:10 +00:00
|
|
|
|
2015-01-10 02:01:01 +00:00
|
|
|
button->setUserString("Price", MyGUI::utility::toString(price));
|
2017-03-13 01:47:52 +00:00
|
|
|
button->setUserData(MWWorld::Ptr(*iter));
|
2013-03-22 13:13:10 +00:00
|
|
|
button->setCaptionWithReplacing(name);
|
2018-06-18 09:43:39 +00:00
|
|
|
button->setSize(mList->getWidth(), lineHeight);
|
2013-03-22 13:13:10 +00:00
|
|
|
button->eventMouseWheel += MyGUI::newDelegate(this, &MerchantRepair::onMouseWheel);
|
|
|
|
button->setUserString("ToolTipType", "ItemPtr");
|
|
|
|
button->eventMouseButtonClick += MyGUI::newDelegate(this, &MerchantRepair::onRepairButtonClick);
|
|
|
|
}
|
|
|
|
}
|
2014-07-26 00:23:42 +00:00
|
|
|
// Canvas size must be expressed with VScroll disabled, otherwise MyGUI would expand the scroll area when the scrollbar is hidden
|
|
|
|
mList->setVisibleVScroll(false);
|
2013-03-22 13:13:10 +00:00
|
|
|
mList->setCanvasSize (MyGUI::IntSize(mList->getWidth(), std::max(mList->getHeight(), currentY)));
|
2014-07-26 00:23:42 +00:00
|
|
|
mList->setVisibleVScroll(true);
|
2013-03-22 13:13:10 +00:00
|
|
|
|
|
|
|
mGoldLabel->setCaptionWithReplacing("#{sGold}: "
|
2015-01-10 02:01:01 +00:00
|
|
|
+ MyGUI::utility::toString(playerGold));
|
2013-03-22 13:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MerchantRepair::onMouseWheel(MyGUI::Widget* _sender, int _rel)
|
|
|
|
{
|
2015-03-08 00:07:29 +00:00
|
|
|
if (mList->getViewOffset().top + _rel*0.3f > 0)
|
2013-03-22 13:13:10 +00:00
|
|
|
mList->setViewOffset(MyGUI::IntPoint(0, 0));
|
|
|
|
else
|
2015-03-08 00:07:29 +00:00
|
|
|
mList->setViewOffset(MyGUI::IntPoint(0, static_cast<int>(mList->getViewOffset().top + _rel*0.3f)));
|
2013-03-22 13:13:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 15:10:53 +00:00
|
|
|
void MerchantRepair::onOpen()
|
2013-03-22 13:13:10 +00:00
|
|
|
{
|
|
|
|
center();
|
2015-06-04 20:09:40 +00:00
|
|
|
// Reset scrollbars
|
|
|
|
mList->setViewOffset(MyGUI::IntPoint(0, 0));
|
2013-03-22 13:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MerchantRepair::onRepairButtonClick(MyGUI::Widget *sender)
|
|
|
|
{
|
2015-08-21 09:12:39 +00:00
|
|
|
MWWorld::Ptr player = MWMechanics::getPlayer();
|
2014-07-16 13:30:06 +00:00
|
|
|
|
2015-01-10 02:01:01 +00:00
|
|
|
int price = MyGUI::utility::parseInt(sender->getUserString("Price"));
|
2014-10-17 16:58:20 +00:00
|
|
|
if (price > player.getClass().getContainerStore(player).count(MWWorld::ContainerStore::sGoldId))
|
|
|
|
return;
|
|
|
|
|
2013-03-22 13:13:10 +00:00
|
|
|
// repair
|
|
|
|
MWWorld::Ptr item = *sender->getUserData<MWWorld::Ptr>();
|
2014-05-25 12:13:07 +00:00
|
|
|
item.getCellRef().setCharge(item.getClass().getItemMaxHealth(item));
|
2013-03-22 13:13:10 +00:00
|
|
|
|
2014-07-16 13:30:06 +00:00
|
|
|
player.getClass().getContainerStore(player).restack(item);
|
|
|
|
|
2017-07-10 11:48:00 +00:00
|
|
|
MWBase::Environment::get().getWindowManager()->playSound("Repair");
|
2013-11-21 03:27:53 +00:00
|
|
|
|
2014-01-08 22:37:46 +00:00
|
|
|
player.getClass().getContainerStore(player).remove(MWWorld::ContainerStore::sGoldId, price, player);
|
2013-03-22 13:13:10 +00:00
|
|
|
|
2014-07-27 22:55:57 +00:00
|
|
|
// add gold to NPC trading gold pool
|
|
|
|
MWMechanics::CreatureStats& actorStats = mActor.getClass().getCreatureStats(mActor);
|
2020-05-20 05:09:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Start of tes3mp change (major)
|
|
|
|
|
|
|
|
Don't unilaterally change the merchant's gold pool on our client and instead let the server do it
|
|
|
|
*/
|
|
|
|
//actorStats.setGoldPool(actorStats.getGoldPool() + price);
|
|
|
|
|
|
|
|
mwmp::ObjectList* objectList = mwmp::Main::get().getNetworking()->getObjectList();
|
|
|
|
objectList->reset();
|
|
|
|
objectList->packetOrigin = mwmp::CLIENT_GAMEPLAY;
|
|
|
|
objectList->addObjectMiscellaneous(mActor, actorStats.getGoldPool() + price, actorStats.getLastRestockTime().getHour(),
|
|
|
|
actorStats.getLastRestockTime().getDay());
|
|
|
|
objectList->sendObjectMiscellaneous();
|
|
|
|
/*
|
|
|
|
End of tes3mp change (major)
|
|
|
|
*/
|
2014-07-27 22:55:57 +00:00
|
|
|
|
2017-09-22 19:26:41 +00:00
|
|
|
setPtr(mActor);
|
2013-03-22 13:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MerchantRepair::onOkButtonClick(MyGUI::Widget *sender)
|
|
|
|
{
|
2017-09-23 10:18:39 +00:00
|
|
|
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_MerchantRepair);
|
2013-03-22 13:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|