2012-10-17 18:03:02 +02:00
# include "trainingwindow.hpp"
# include <boost/lexical_cast.hpp>
# include "../mwbase/windowmanager.hpp"
# include "../mwbase/environment.hpp"
# include "../mwbase/world.hpp"
2012-11-05 19:53:55 +01:00
# include "../mwbase/mechanicsmanager.hpp"
2014-08-04 02:13:44 +02:00
# include "../mwbase/dialoguemanager.hpp"
2012-10-17 18:03:02 +02:00
2013-05-11 18:38:27 +02:00
# include "../mwworld/class.hpp"
2013-11-21 04:27:53 +01:00
# include "../mwworld/containerstore.hpp"
2012-10-17 18:03:02 +02:00
# include "../mwmechanics/npcstats.hpp"
# include "tooltips.hpp"
2014-06-09 03:40:14 +02:00
namespace
{
// Sorts a container descending by skill value. If skill value is equal, sorts ascending by skill ID.
// pair <skill ID, skill value>
bool sortSkills ( const std : : pair < int , int > & left , const std : : pair < int , int > & right )
{
if ( left = = right )
return false ;
if ( left . second > right . second )
return true ;
else if ( left . second < right . second )
return false ;
return left . first < right . first ;
}
}
2012-10-17 18:03:02 +02:00
namespace MWGui
{
2013-04-10 14:46:21 -04:00
TrainingWindow : : TrainingWindow ( )
: WindowBase ( " openmw_trainingwindow.layout " )
2012-10-17 18:03:02 +02:00
, mFadeTimeRemaining ( 0 )
{
getWidget ( mTrainingOptions , " TrainingOptions " ) ;
getWidget ( mCancelButton , " CancelButton " ) ;
getWidget ( mPlayerGold , " PlayerGold " ) ;
mCancelButton - > eventMouseButtonClick + = MyGUI : : newDelegate ( this , & TrainingWindow : : onCancelButtonClicked ) ;
}
void TrainingWindow : : open ( )
{
center ( ) ;
}
2014-05-26 23:13:37 -04:00
void TrainingWindow : : exit ( )
{
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > removeGuiMode ( GM_Training ) ;
}
2012-10-17 18:03:02 +02:00
void TrainingWindow : : startTraining ( MWWorld : : Ptr actor )
{
mPtr = actor ;
2014-01-08 23:37:46 +01:00
MWWorld : : Ptr player = MWBase : : Environment : : get ( ) . getWorld ( ) - > getPlayerPtr ( ) ;
int playerGold = player . getClass ( ) . getContainerStore ( player ) . count ( MWWorld : : ContainerStore : : sGoldId ) ;
mPlayerGold - > setCaptionWithReplacing ( " #{sGold}: " + boost : : lexical_cast < std : : string > ( playerGold ) ) ;
2012-10-17 18:03:02 +02:00
2014-05-22 20:37:22 +02:00
MWMechanics : : NpcStats & npcStats = actor . getClass ( ) . getNpcStats ( actor ) ;
2012-10-17 18:03:02 +02:00
// NPC can train you in his best 3 skills
2014-06-09 03:40:14 +02:00
std : : vector < std : : pair < int , int > > skills ;
2012-10-17 18:03:02 +02:00
for ( int i = 0 ; i < ESM : : Skill : : Length ; + + i )
{
int value = npcStats . getSkill ( i ) . getBase ( ) ;
2014-06-09 03:40:14 +02:00
skills . push_back ( std : : make_pair ( i , value ) ) ;
2012-10-17 18:03:02 +02:00
}
2014-06-09 03:40:14 +02:00
std : : sort ( skills . begin ( ) , skills . end ( ) , sortSkills ) ;
2012-10-17 18:03:02 +02:00
MyGUI : : EnumeratorWidgetPtr widgets = mTrainingOptions - > getEnumerator ( ) ;
MyGUI : : Gui : : getInstance ( ) . destroyWidgets ( widgets ) ;
2014-05-22 20:37:22 +02:00
MWMechanics : : NpcStats & pcStats = player . getClass ( ) . getNpcStats ( player ) ;
2012-10-17 18:03:02 +02:00
2012-11-06 13:43:48 +04:00
const MWWorld : : Store < ESM : : GameSetting > & gmst =
MWBase : : Environment : : get ( ) . getWorld ( ) - > getStore ( ) . get < ESM : : GameSetting > ( ) ;
2012-10-17 18:03:02 +02:00
for ( int i = 0 ; i < 3 ; + + i )
{
2012-11-09 14:42:09 +01:00
int price = MWBase : : Environment : : get ( ) . getMechanicsManager ( ) - > getBarterOffer
2014-06-09 03:40:14 +02:00
( mPtr , pcStats . getSkill ( skills [ i ] . first ) . getBase ( ) * gmst . find ( " iTrainingMod " ) - > getInt ( ) , true ) ;
2012-10-17 18:03:02 +02:00
2014-10-17 18:58:20 +02:00
MyGUI : : Button * button = mTrainingOptions - > createWidget < MyGUI : : Button > ( price < = playerGold ? " SandTextButton " : " SandTextButtonDisabled " , // can't use setEnabled since that removes tooltip
2012-10-17 18:03:02 +02:00
MyGUI : : IntCoord ( 5 , 5 + i * 18 , mTrainingOptions - > getWidth ( ) - 10 , 18 ) , MyGUI : : Align : : Default ) ;
2014-06-09 03:40:14 +02:00
button - > setUserData ( skills [ i ] . first ) ;
2012-10-17 18:03:02 +02:00
button - > eventMouseButtonClick + = MyGUI : : newDelegate ( this , & TrainingWindow : : onTrainingSelected ) ;
2014-06-09 03:40:14 +02:00
button - > setCaptionWithReplacing ( " #{ " + ESM : : Skill : : sSkillNameIds [ skills [ i ] . first ] + " } - " + boost : : lexical_cast < std : : string > ( price ) ) ;
2012-10-17 18:03:02 +02:00
button - > setSize ( button - > getTextSize ( ) . width + 12 , button - > getSize ( ) . height ) ;
2014-06-09 03:40:14 +02:00
ToolTips : : createSkillToolTip ( button , skills [ i ] . first ) ;
2012-10-17 18:03:02 +02:00
}
center ( ) ;
}
void TrainingWindow : : onReferenceUnavailable ( )
{
2013-04-10 00:32:05 -04:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > removeGuiMode ( GM_Training ) ;
2012-10-17 18:03:02 +02:00
}
void TrainingWindow : : onCancelButtonClicked ( MyGUI : : Widget * sender )
{
2014-05-26 23:13:37 -04:00
exit ( ) ;
2012-10-17 18:03:02 +02:00
}
void TrainingWindow : : onTrainingSelected ( MyGUI : : Widget * sender )
{
int skillId = * sender - > getUserData < int > ( ) ;
2014-01-08 18:39:44 +01:00
MWWorld : : Ptr player = MWBase : : Environment : : get ( ) . getWorld ( ) - > getPlayerPtr ( ) ;
2014-05-22 20:37:22 +02:00
MWMechanics : : NpcStats & pcStats = player . getClass ( ) . getNpcStats ( player ) ;
2012-10-17 18:03:02 +02:00
2012-11-06 11:29:18 +04:00
const MWWorld : : ESMStore & store =
MWBase : : Environment : : get ( ) . getWorld ( ) - > getStore ( ) ;
int price = pcStats . getSkill ( skillId ) . getBase ( ) * store . get < ESM : : GameSetting > ( ) . find ( " iTrainingMod " ) - > getInt ( ) ;
2012-11-09 14:42:09 +01:00
price = MWBase : : Environment : : get ( ) . getMechanicsManager ( ) - > getBarterOffer ( mPtr , price , true ) ;
2012-10-17 18:03:02 +02:00
2014-10-17 18:58:20 +02:00
if ( price > player . getClass ( ) . getContainerStore ( player ) . count ( MWWorld : : ContainerStore : : sGoldId ) )
return ;
2014-05-22 20:37:22 +02:00
MWMechanics : : NpcStats & npcStats = mPtr . getClass ( ) . getNpcStats ( mPtr ) ;
2012-10-17 18:48:29 +02:00
if ( npcStats . getSkill ( skillId ) . getBase ( ) < = pcStats . getSkill ( skillId ) . getBase ( ) )
{
2013-04-10 00:32:05 -04:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > messageBox ( " #{sServiceTrainingWords} " ) ;
2012-10-17 18:48:29 +02:00
return ;
}
2014-01-10 20:53:53 +01:00
// You can not train a skill above its governing attribute
const ESM : : Skill * skill = MWBase : : Environment : : get ( ) . getWorld ( ) - > getStore ( ) . get < ESM : : Skill > ( ) . find ( skillId ) ;
if ( pcStats . getSkill ( skillId ) . getBase ( ) > = pcStats . getAttribute ( skill - > mData . mAttribute ) . getBase ( ) )
{
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > messageBox ( " #{sNotifyMessage17} " ) ;
return ;
}
2012-10-17 18:03:02 +02:00
// increase skill
MWWorld : : LiveCellRef < ESM : : NPC > * playerRef = player . get < ESM : : NPC > ( ) ;
2012-11-06 11:29:18 +04:00
const ESM : : Class * class_ =
store . get < ESM : : Class > ( ) . find ( playerRef - > mBase - > mClass ) ;
2012-10-17 18:03:02 +02:00
pcStats . increaseSkill ( skillId , * class_ , true ) ;
// remove gold
2014-01-08 23:37:46 +01:00
player . getClass ( ) . getContainerStore ( player ) . remove ( MWWorld : : ContainerStore : : sGoldId , price , player ) ;
2012-10-17 18:03:02 +02:00
2014-07-28 00:55:57 +02:00
// add gold to NPC trading gold pool
npcStats . setGoldPool ( npcStats . getGoldPool ( ) + price ) ;
2012-10-17 18:03:02 +02:00
// go back to game mode
2013-04-10 00:32:05 -04:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > removeGuiMode ( GM_Training ) ;
2014-08-04 02:13:44 +02:00
MWBase : : Environment : : get ( ) . getDialogueManager ( ) - > goodbyeSelected ( ) ;
2012-10-17 18:03:02 +02:00
// advance time
MWBase : : Environment : : get ( ) . getWorld ( ) - > advanceTime ( 2 ) ;
2014-01-14 02:20:13 +01:00
MWBase : : Environment : : get ( ) . getMechanicsManager ( ) - > rest ( false ) ;
MWBase : : Environment : : get ( ) . getMechanicsManager ( ) - > rest ( false ) ;
2012-10-17 18:03:02 +02:00
2014-08-01 16:25:41 +02:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > fadeScreenOut ( 0.25 ) ;
2012-10-17 18:03:02 +02:00
mFadeTimeRemaining = 0.5 ;
}
void TrainingWindow : : onFrame ( float dt )
{
if ( mFadeTimeRemaining < = 0 )
return ;
mFadeTimeRemaining - = dt ;
if ( mFadeTimeRemaining < = 0 )
2014-08-01 16:25:41 +02:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > fadeScreenIn ( 0.25 ) ;
2012-10-17 18:03:02 +02:00
}
}