Added PC repair feature
parent
78f3f19f62
commit
a2ca679beb
@ -0,0 +1,157 @@
|
||||
#include "repair.hpp"
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
#include "../mwbase/world.hpp"
|
||||
#include "../mwbase/environment.hpp"
|
||||
#include "../mwbase/windowmanager.hpp"
|
||||
|
||||
#include "../mwworld/player.hpp"
|
||||
#include "../mwworld/containerstore.hpp"
|
||||
#include "../mwworld/class.hpp"
|
||||
|
||||
#include "widgets.hpp"
|
||||
|
||||
namespace MWGui
|
||||
{
|
||||
|
||||
Repair::Repair(MWBase::WindowManager &parWindowManager)
|
||||
: WindowBase("openmw_repair.layout", parWindowManager)
|
||||
{
|
||||
getWidget(mRepairBox, "RepairBox");
|
||||
getWidget(mRepairView, "RepairView");
|
||||
getWidget(mToolBox, "ToolBox");
|
||||
getWidget(mToolIcon, "ToolIcon");
|
||||
getWidget(mUsesLabel, "UsesLabel");
|
||||
getWidget(mQualityLabel, "QualityLabel");
|
||||
getWidget(mCancelButton, "CancelButton");
|
||||
|
||||
mCancelButton->eventMouseButtonClick += MyGUI::newDelegate(this, &Repair::onCancel);
|
||||
}
|
||||
|
||||
void Repair::open()
|
||||
{
|
||||
center();
|
||||
}
|
||||
|
||||
void Repair::startRepairItem(const MWWorld::Ptr &item)
|
||||
{
|
||||
mRepair.setTool(item);
|
||||
|
||||
std::string path = std::string("icons\\");
|
||||
path += MWWorld::Class::get(item).getInventoryIcon(item);
|
||||
int pos = path.rfind(".");
|
||||
path.erase(pos);
|
||||
path.append(".dds");
|
||||
mToolIcon->setImageTexture (path);
|
||||
mToolIcon->setUserString("ToolTipType", "ItemPtr");
|
||||
mToolIcon->setUserData(item);
|
||||
|
||||
updateRepairView();
|
||||
}
|
||||
|
||||
void Repair::updateRepairView()
|
||||
{
|
||||
MWWorld::LiveCellRef<ESM::Repair> *ref =
|
||||
mRepair.getTool().get<ESM::Repair>();
|
||||
|
||||
int uses = (mRepair.getTool().getCellRef().mCharge != -1) ? mRepair.getTool().getCellRef().mCharge : ref->mBase->mData.mUses;
|
||||
|
||||
float quality = ref->mBase->mData.mQuality;
|
||||
|
||||
std::stringstream qualityStr;
|
||||
qualityStr << std::setprecision(3) << quality;
|
||||
|
||||
mUsesLabel->setCaptionWithReplacing("#{sUses} " + boost::lexical_cast<std::string>(uses));
|
||||
mQualityLabel->setCaptionWithReplacing("#{sQuality} " + qualityStr.str());
|
||||
|
||||
bool toolBoxVisible = (mRepair.getTool().getRefData().getCount() != 0);
|
||||
mToolBox->setVisible(toolBoxVisible);
|
||||
|
||||
bool toolBoxWasVisible = (mRepairBox->getPosition().top != mToolBox->getPosition().top);
|
||||
|
||||
if (toolBoxVisible && !toolBoxWasVisible)
|
||||
{
|
||||
// shrink
|
||||
mRepairBox->setPosition(mRepairBox->getPosition() + MyGUI::IntPoint(0,mToolBox->getSize().height));
|
||||
mRepairBox->setSize(mRepairBox->getSize() - MyGUI::IntSize(0,mToolBox->getSize().height));
|
||||
}
|
||||
else if (!toolBoxVisible && toolBoxWasVisible)
|
||||
{
|
||||
// expand
|
||||
mRepairBox->setPosition(MyGUI::IntPoint (mRepairBox->getPosition().left, mToolBox->getPosition().top));
|
||||
mRepairBox->setSize(mRepairBox->getSize() + MyGUI::IntSize(0,mToolBox->getSize().height));
|
||||
}
|
||||
|
||||
while (mRepairView->getChildCount())
|
||||
MyGUI::Gui::getInstance().destroyWidget(mRepairView->getChildAt(0));
|
||||
|
||||
int currentY = 0;
|
||||
|
||||
MWWorld::Ptr player = MWBase::Environment::get().getWorld()->getPlayer().getPlayer();
|
||||
MWWorld::ContainerStore& store = MWWorld::Class::get(player).getContainerStore(player);
|
||||
int categories = MWWorld::ContainerStore::Type_Weapon | MWWorld::ContainerStore::Type_Armor;
|
||||
for (MWWorld::ContainerStoreIterator iter (store.begin(categories));
|
||||
iter!=store.end(); ++iter)
|
||||
{
|
||||
if (MWWorld::Class::get(*iter).hasItemHealth(*iter))
|
||||
{
|
||||
int maxDurability = MWWorld::Class::get(*iter).getItemMaxHealth(*iter);
|
||||
int durability = (iter->getCellRef().mCharge == -1) ? maxDurability : iter->getCellRef().mCharge;
|
||||
if (maxDurability == durability)
|
||||
continue;
|
||||
|
||||
MyGUI::TextBox* text = mRepairView->createWidget<MyGUI::TextBox> (
|
||||
"SandText", MyGUI::IntCoord(8, currentY, mRepairView->getWidth()-8, 18), MyGUI::Align::Default);
|
||||
text->setCaption(MWWorld::Class::get(*iter).getName(*iter));
|
||||
text->setNeedMouseFocus(false);
|
||||
currentY += 19;
|
||||
|
||||
MyGUI::ImageBox* icon = mRepairView->createWidget<MyGUI::ImageBox> (
|
||||
"ImageBox", MyGUI::IntCoord(16, currentY, 32, 32), MyGUI::Align::Default);
|
||||
std::string path = std::string("icons\\");
|
||||
path += MWWorld::Class::get(*iter).getInventoryIcon(*iter);
|
||||
int pos = path.rfind(".");
|
||||
path.erase(pos);
|
||||
path.append(".dds");
|
||||
icon->setImageTexture (path);
|
||||
icon->setUserString("ToolTipType", "ItemPtr");
|
||||
icon->setUserData(*iter);
|
||||
icon->eventMouseButtonClick += MyGUI::newDelegate(this, &Repair::onRepairItem);
|
||||
icon->eventMouseWheel += MyGUI::newDelegate(this, &Repair::onMouseWheel);
|
||||
|
||||
Widgets::MWDynamicStatPtr chargeWidget = mRepairView->createWidget<Widgets::MWDynamicStat>
|
||||
("MW_ChargeBar", MyGUI::IntCoord(72, currentY+2, 199, 20), MyGUI::Align::Default);
|
||||
chargeWidget->setValue(durability, maxDurability);
|
||||
chargeWidget->setNeedMouseFocus(false);
|
||||
|
||||
currentY += 32 + 4;
|
||||
}
|
||||
}
|
||||
mRepairView->setCanvasSize (MyGUI::IntSize(mRepairView->getWidth(), std::max(mRepairView->getHeight(), currentY)));
|
||||
}
|
||||
|
||||
void Repair::onCancel(MyGUI::Widget *sender)
|
||||
{
|
||||
mWindowManager.removeGuiMode(GM_Repair);
|
||||
}
|
||||
|
||||
void Repair::onRepairItem(MyGUI::Widget *sender)
|
||||
{
|
||||
if (!mRepair.getTool().getRefData().getCount())
|
||||
return;
|
||||
|
||||
mRepair.repair(*sender->getUserData<MWWorld::Ptr>());
|
||||
|
||||
updateRepairView();
|
||||
}
|
||||
|
||||
void Repair::onMouseWheel(MyGUI::Widget* _sender, int _rel)
|
||||
{
|
||||
if (mRepairView->getViewOffset().top + _rel*0.3 > 0)
|
||||
mRepairView->setViewOffset(MyGUI::IntPoint(0, 0));
|
||||
else
|
||||
mRepairView->setViewOffset(MyGUI::IntPoint(0, mRepairView->getViewOffset().top + _rel*0.3));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
#ifndef OPENMW_MWGUI_REPAIR_H
|
||||
#define OPENMW_MWGUI_REPAIR_H
|
||||
|
||||
#include "window_base.hpp"
|
||||
|
||||
#include "../mwworld/ptr.hpp"
|
||||
#include "../mwmechanics/repair.hpp"
|
||||
|
||||
namespace MWGui
|
||||
{
|
||||
|
||||
class Repair : public WindowBase
|
||||
{
|
||||
public:
|
||||
Repair(MWBase::WindowManager &parWindowManager);
|
||||
|
||||
virtual void open();
|
||||
|
||||
void startRepairItem (const MWWorld::Ptr& item);
|
||||
|
||||
protected:
|
||||
MyGUI::Widget* mRepairBox;
|
||||
MyGUI::ScrollView* mRepairView;
|
||||
|
||||
MyGUI::Widget* mToolBox;
|
||||
|
||||
MyGUI::ImageBox* mToolIcon;
|
||||
|
||||
MyGUI::TextBox* mUsesLabel;
|
||||
MyGUI::TextBox* mQualityLabel;
|
||||
|
||||
MyGUI::Button* mCancelButton;
|
||||
|
||||
MWMechanics::Repair mRepair;
|
||||
|
||||
void updateRepairView();
|
||||
|
||||
void onRepairItem (MyGUI::Widget* sender);
|
||||
void onCancel (MyGUI::Widget* sender);
|
||||
void onMouseWheel(MyGUI::Widget* _sender, int _rel);
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,110 @@
|
||||
#include "repair.hpp"
|
||||
|
||||
#include <boost/format.hpp>
|
||||
|
||||
#include "../mwbase/world.hpp"
|
||||
#include "../mwbase/environment.hpp"
|
||||
#include "../mwbase/mechanicsmanager.hpp"
|
||||
#include "../mwbase/windowmanager.hpp"
|
||||
#include "../mwbase/soundmanager.hpp"
|
||||
|
||||
#include "../mwworld/player.hpp"
|
||||
#include "../mwworld/containerstore.hpp"
|
||||
#include "../mwworld/class.hpp"
|
||||
|
||||
#include "../mwmechanics/creaturestats.hpp"
|
||||
#include "../mwmechanics/npcstats.hpp"
|
||||
|
||||
namespace MWMechanics
|
||||
{
|
||||
|
||||
void Repair::repair(const MWWorld::Ptr &itemToRepair)
|
||||
{
|
||||
MWWorld::Ptr player = MWBase::Environment::get().getWorld()->getPlayer().getPlayer();
|
||||
MWWorld::LiveCellRef<ESM::Repair> *ref =
|
||||
mTool.get<ESM::Repair>();
|
||||
|
||||
// reduce number of uses left
|
||||
int uses = (mTool.getCellRef().mCharge != -1) ? mTool.getCellRef().mCharge : ref->mBase->mData.mUses;
|
||||
mTool.getCellRef().mCharge = uses-1;
|
||||
|
||||
// unstack tool if required
|
||||
if (mTool.getRefData().getCount() > 1 && uses == ref->mBase->mData.mUses)
|
||||
{
|
||||
MWWorld::ContainerStore& store = MWWorld::Class::get(player).getContainerStore(player);
|
||||
MWWorld::ContainerStoreIterator it = store.add(mTool);
|
||||
it->getRefData().setCount(mTool.getRefData().getCount()-1);
|
||||
it->getCellRef().mCharge = -1;
|
||||
|
||||
mTool.getRefData().setCount(1);
|
||||
}
|
||||
|
||||
MWMechanics::CreatureStats& stats = MWWorld::Class::get(player).getCreatureStats(player);
|
||||
MWMechanics::NpcStats& npcStats = MWWorld::Class::get(player).getNpcStats(player);
|
||||
|
||||
float fatigueTerm = stats.getFatigueTerm();
|
||||
int pcStrength = stats.getAttribute(ESM::Attribute::Strength).getModified();
|
||||
int pcLuck = stats.getAttribute(ESM::Attribute::Luck).getModified();
|
||||
int armorerSkill = npcStats.getSkill(ESM::Skill::Armorer).getModified();
|
||||
|
||||
float fRepairAmountMult = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>()
|
||||
.find("fRepairAmountMult")->getFloat();
|
||||
|
||||
float toolQuality = ref->mBase->mData.mQuality;
|
||||
|
||||
float x = (0.1 * pcStrength + 0.1 * pcLuck + armorerSkill) * fatigueTerm;
|
||||
|
||||
int roll = static_cast<float> (std::rand()) / RAND_MAX * 100;
|
||||
if (roll <= x)
|
||||
{
|
||||
int y = fRepairAmountMult * toolQuality * roll;
|
||||
y = std::max(1, y);
|
||||
|
||||
// repair by 'y' points
|
||||
itemToRepair.getCellRef().mCharge += y;
|
||||
itemToRepair.getCellRef().mCharge = std::min(itemToRepair.getCellRef().mCharge,
|
||||
MWWorld::Class::get(itemToRepair).getItemMaxHealth(itemToRepair));
|
||||
|
||||
// set the OnPCRepair variable on the item's script
|
||||
std::string script = MWWorld::Class::get(itemToRepair).getScript(itemToRepair);
|
||||
if(script != "")
|
||||
itemToRepair.getRefData().getLocals().setVarByInt(script, "onpcrepair", 1);
|
||||
|
||||
// increase skill
|
||||
MWWorld::Class::get(player).skillUsageSucceeded(player, ESM::Skill::Armorer, 0);
|
||||
|
||||
MWBase::Environment::get().getSoundManager()->playSound("Repair",1,1);
|
||||
MWBase::Environment::get().getWindowManager()->messageBox("#{sRepairSuccess}");
|
||||
}
|
||||
else
|
||||
{
|
||||
MWBase::Environment::get().getSoundManager()->playSound("Repair Fail",1,1);
|
||||
MWBase::Environment::get().getWindowManager()->messageBox("#{sRepairFailed}");
|
||||
}
|
||||
|
||||
// tool used up?
|
||||
if (mTool.getCellRef().mCharge == 0)
|
||||
{
|
||||
mTool.getRefData().setCount(0);
|
||||
|
||||
std::string message = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>()
|
||||
.find("sNotifyMessage51")->getString();
|
||||
|
||||
MWBase::Environment::get().getWindowManager()->messageBox((boost::format(message) % MWWorld::Class::get(mTool).getName(mTool)).str());
|
||||
|
||||
// try to find a new tool of the same ID
|
||||
MWWorld::Ptr player = MWBase::Environment::get().getWorld()->getPlayer().getPlayer();
|
||||
MWWorld::ContainerStore& store = MWWorld::Class::get(player).getContainerStore(player);
|
||||
for (MWWorld::ContainerStoreIterator iter (store.begin());
|
||||
iter!=store.end(); ++iter)
|
||||
{
|
||||
if (Misc::StringUtils::ciEqual(iter->getCellRef().mRefID, mTool.getCellRef().mRefID))
|
||||
{
|
||||
mTool = *iter;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
#ifndef OPENMW_MWMECHANICS_REPAIR_H
|
||||
#define OPENMW_MWMECHANICS_REPAIR_H
|
||||
|
||||
#include "../mwworld/ptr.hpp"
|
||||
|
||||
namespace MWMechanics
|
||||
{
|
||||
|
||||
class Repair
|
||||
{
|
||||
public:
|
||||
void setTool (const MWWorld::Ptr& tool) { mTool = tool; }
|
||||
MWWorld::Ptr getTool() { return mTool; }
|
||||
|
||||
void repair (const MWWorld::Ptr& itemToRepair);
|
||||
|
||||
private:
|
||||
MWWorld::Ptr mTool;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,18 @@
|
||||
#include "actionrepair.hpp"
|
||||
|
||||
#include "../mwbase/environment.hpp"
|
||||
#include "../mwbase/windowmanager.hpp"
|
||||
|
||||
namespace MWWorld
|
||||
{
|
||||
ActionRepair::ActionRepair(const Ptr &item)
|
||||
: Action(false, item)
|
||||
{
|
||||
}
|
||||
|
||||
void ActionRepair::executeImp (const Ptr& actor)
|
||||
{
|
||||
MWBase::Environment::get().getWindowManager()->pushGuiMode(MWGui::GM_Repair);
|
||||
MWBase::Environment::get().getWindowManager()->startRepairItem(getTarget());
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
#ifndef GAME_MWWORLD_ACTIONREPAIR_H
|
||||
#define GAME_MWWORLD_ACTIONREPAIR_H
|
||||
|
||||
#include "action.hpp"
|
||||
|
||||
namespace MWWorld
|
||||
{
|
||||
class ActionRepair : public Action
|
||||
{
|
||||
virtual void executeImp (const Ptr& actor);
|
||||
|
||||
public:
|
||||
ActionRepair(const MWWorld::Ptr& item);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<MyGUI type="Layout">
|
||||
<Widget type="Window" skin="MW_Dialog" layer="Windows" position="0 0 329 253" name="_Main">
|
||||
|
||||
<Widget type="Widget" skin="" position="4 4 321 42" name="ToolBox">
|
||||
<Widget type="ImageBox" skin="ImageBox" position="5 6 32 32" name="ToolIcon"/>
|
||||
|
||||
<Widget type="AutoSizedTextBox" skin="SandText" position="55 13 300 18" name="UsesLabel">
|
||||
<Property key="Caption" value="#{sUses}"/>
|
||||
</Widget>
|
||||
|
||||
<Widget type="TextBox" skin="SandText" position="55 13 250 18" name="QualityLabel">
|
||||
<Property key="Caption" value="#{sQuality}"/>
|
||||
<Property key="TextAlign" value="Right"/>
|
||||
</Widget>
|
||||
|
||||
</Widget>
|
||||
|
||||
<Widget type="Widget" skin="MW_Box" position="6 46 309 160" align="Left Stretch" name="RepairBox">
|
||||
<Widget type="ScrollView" skin="MW_ScrollView" position="4 4 301 152" align="Left Top Stretch" name="RepairView">
|
||||
<Property key="CanvasAlign" value="Left"/>
|
||||
</Widget>
|
||||
</Widget>
|
||||
|
||||
<Widget type="AutoSizedButton" skin="MW_Button" position="239 214 75 24" name="CancelButton">
|
||||
<Property key="ExpandDirection" value="Left"/>
|
||||
<Property key="Caption" value="#{sCancel}"/>
|
||||
</Widget>
|
||||
|
||||
</Widget>
|
||||
|
||||
</MyGUI>
|
Loading…
Reference in New Issue