1 /***************************************************************************
2   traindialog.cpp  - The train skills dialog
3 -------------------
4     begin                : 9/9/2005
5     copyright            : (C) 2005 by Gabor Torok
6     email                : cctorok@yahoo.com
7 ***************************************************************************/
8 
9 /***************************************************************************
10 *                                                                         *
11 *   This program is free software; you can redistribute it and/or modify  *
12 *   it under the terms of the GNU General Public License as published by  *
13 *   the Free Software Foundation; either version 2 of the License, or     *
14 *   (at your option) any later version.                                   *
15 *                                                                         *
16 ***************************************************************************/
17 
18 #include "common/constants.h"
19 #include "traindialog.h"
20 #include "scourge.h"
21 #include "shapepalette.h"
22 #include "creature.h"
23 #include "rpg/rpglib.h"
24 #include "gui/window.h"
25 #include "gui/button.h"
26 #include "gui/textfield.h"
27 #include "gui/scrollinglabel.h"
28 #include "gui/scrollinglist.h"
29 
30 using namespace std;
31 
32 
TrainDialog(Scourge * scourge)33 TrainDialog::TrainDialog( Scourge *scourge ) {
34 	this->scourge = scourge;
35 	this->creature = NULL;
36 	int w = 400;
37 	int h = 400;
38 	win =
39 	  scourge->createWindow( 50, 50,
40 	                         w, h,
41 	                         Constants::getMessage( Constants::TRAIN_DIALOG_TITLE ) );
42 	creatureLabel = win->createLabel( 10, 15, "" );
43 	errorLabel = win->createLabel( 10, 45, "" );
44 	errorLabel->setSpecialColor();
45 	errorLabel2 = win->createLabel( 10, 60, "" );
46 	errorLabel2->setSpecialColor();
47 
48 	list = new ScrollingList( 10, 75, w - 30, 120, scourge->getShapePalette()->getHighlightTexture() );
49 	win->addWidget( list );
50 
51 	description = new ScrollingLabel( 10, 205, w - 30, 120, "" );
52 	win->addWidget( description );
53 
54 	h = 20;
55 	int y = win->getHeight() - h - 30;
56 	closeButton = win->createButton( w - 80, y, w - 10, y + h, _( "Close" ) );
57 	applyButton = win->createButton( w - 160, y, w - 90, y + h, _( "Train!" ) );
58 	win->setEscapeHandler( closeButton );
59 
60 	win->registerEventHandler( this );
61 }
62 
~TrainDialog()63 TrainDialog::~TrainDialog() {
64 	delete win;
65 }
66 
setCreature(Creature * creature)67 void TrainDialog::setCreature( Creature *creature ) {
68 	this->creature = creature;
69 	updateUI();
70 	win->setVisible( true );
71 }
72 
updateUI()73 void TrainDialog::updateUI() {
74 	enum { S_SIZE = 255 };
75 	char s[ S_SIZE ];
76 
77 	// level-based mark-up
78 	Creature *player = scourge->getParty()->getPlayer();
79 	int base = 150;
80 	int price = base + static_cast<int>( Util::getRandomSum( static_cast<float>( base / 2 ), creature->getNpcInfo()->level ) );
81 	// 25% variance based on leadership skill.
82 	float skill = static_cast<float>( player->getSkill( Skill::LEADERSHIP ) );
83 	int percentage = static_cast<int>( static_cast<float>( price ) * ( 100.0f - skill ) / 100.0f * MAX_DISCOUNT );
84 	cost = price + percentage;
85 
86 	snprintf( s, S_SIZE, "%s (%s %d) %s: %d",
87 	          _( creature->getName() ),
88 	          _( "level" ),
89 	          creature->getNpcInfo()->level,
90 	          _( "Cost" ),
91 	          cost );
92 	creatureLabel->setText( s );
93 
94 	// is the trainer high enough level?
95 	list->setLines( 0, text );
96 	description->setText( "" );
97 
98 	// does this trainer teach your profession?
99 	Character *rc = player->getCharacter();
100 	while ( rc->getParent() ) {
101 		rc = rc->getParent();
102 	}
103 	int index = Characters::getRootIndexByName( rc->getName() );
104 	if ( creature->getNpcInfo()->getSubtype()->find( index ) ==
105 	        creature->getNpcInfo()->getSubtype()->end() ) {
106 		errorLabel->setColor( 1, 0, 0 );
107 		errorLabel2->setColor( 1, 0, 0 );
108 		snprintf( s, S_SIZE, _( "%s, I cannot teach you. " ), player->getName() );
109 		errorLabel->setText( s );
110 		snprintf( s, S_SIZE, _( "You must seek out one who can train a %s." ), rc->getDisplayName() );
111 		errorLabel2->setText( s );
112 	} else if ( creature->getNpcInfo()->level < player->getCharacter()->getMinLevelReq() ) {
113 		errorLabel->setColor( 0, 1, 1 );
114 		errorLabel2->setColor( 0, 1, 1 );
115 		snprintf( s, S_SIZE, _( "%s, I can teach you no more. " ), player->getName() );
116 		errorLabel->setText( s );
117 		errorLabel2->setText( _( "You must seek a higher level trainer." ) );
118 	} else if ( player->getCharacter()->getChildCount() == 0 ) {
119 		errorLabel->setColor( 1, 0, 1 );
120 		errorLabel2->setColor( 1, 0, 1 );
121 		snprintf( s, S_SIZE, _( "%s, I can teach you no more. " ), player->getName() );
122 		errorLabel->setText( s );
123 		errorLabel2->setText( _( "You must learn by yourself from now on." ) );
124 	} else if ( player->getCharacter()->getChild( 0 )->getMinLevelReq() >
125 	            player->getLevel() ) {
126 		errorLabel->setColor( 1, 1, 0 );
127 		errorLabel2->setColor( 1, 1, 0 );
128 		snprintf( s, S_SIZE, _( "%s, you are not yet ready." ), player->getName() );
129 		errorLabel->setText( s );
130 		snprintf( s, S_SIZE, _( "Come back when you've reached level %d." ),
131 		          player->getCharacter()->getChild( 0 )->getMinLevelReq() );
132 		errorLabel2->setText( s );
133 	} else {
134 		errorLabel->setColor( 0, 1, 0 );
135 		errorLabel2->setColor( 0, 1, 0 );
136 		snprintf( s, S_SIZE, _( "%s, you are ready to learn." ), player->getName() );
137 		errorLabel->setText( s );
138 		errorLabel2->setText( _( "Select your next profession from the list below." ) );
139 
140 		for ( int i = 0; i < player->getCharacter()->getChildCount(); i++ ) {
141 			text[i] = player->getCharacter()->getChild( i )->getDisplayName();
142 			text[i] += " (";
143 			text[i] += _( "min level" );
144 			char str[20];
145 			snprintf( str, 20, " %d)", player->getCharacter()->getChild( i )->getMinLevelReq() );
146 			text[i] += str;
147 		}
148 		list->setLines( player->getCharacter()->getChildCount(), text );
149 		description->setText( player->getCharacter()->getChildCount() > 0 ?
150 		                      player->getCharacter()->getChild( 0 )->getDescription() :
151 		                      "" );
152 	}
153 }
154 
handleEvent(Widget * widget,SDL_Event * event)155 bool TrainDialog::handleEvent( Widget *widget, SDL_Event *event ) {
156 	if ( widget == closeButton || widget == win->closeButton ) {
157 		win->setVisible( false );
158 	} else if ( widget == list ) {
159 		int n = list->getSelectedLine();
160 		if ( n >= 0 ) {
161 			description->setText( scourge->getParty()->getPlayer()->getCharacter()->getChild( n )->getDescription() );
162 		}
163 	} else if ( widget == applyButton ) {
164 		int n = list->getSelectedLine();
165 		if ( n >= 0 ) {
166 			train( scourge->getParty()->getPlayer()->getCharacter()->getChild( n ) );
167 		}
168 	}
169 	return false;
170 }
171 
train(Character * newProfession)172 void TrainDialog::train( Character *newProfession ) {
173 	Creature *player = scourge->getParty()->getPlayer();
174 	if ( player->getMoney() < cost ) {
175 		scourge->showMessageDialog( _( "You cannot afford the training!" ) );
176 		return;
177 	}
178 
179 	player->setMoney( player->getMoney() - cost );
180 	player->changeProfession( newProfession );
181 
182 	updateUI();
183 
184 	char tmp[120];
185 	snprintf( tmp, 120, _( "Congratulation %1$s, you are now a %2$s." ),
186 	          player->getName(),
187 	          player->getCharacter()->getDisplayName() );
188 	scourge->showMessageDialog( tmp );
189 }
190 
191