aboutsummaryrefslogtreecommitdiff
path: root/engines/xeen/dialogs_create_char.cpp
blob: aeadc360f2132953e21205f92ec21eb9fbb52380 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "xeen/dialogs_create_char.h"
#include "xeen/dialogs_input.h"
#include "xeen/xeen.h"

namespace Xeen {

void CreateCharacterDialog::show(XeenEngine *vm) {
	CreateCharacterDialog *dlg = new CreateCharacterDialog(vm);
	dlg->execute();
	delete dlg;
}

CreateCharacterDialog::CreateCharacterDialog(XeenEngine *vm) : ButtonContainer(vm) {
	_dicePos[0] = Common::Point(20, 17);
	_dicePos[1] = Common::Point(112, 35);
	_dicePos[2] = Common::Point(61, 50);
	_diceFrame[0] = 0;
	_diceFrame[1] = 2;
	_diceFrame[2] = 4;
	_diceInc[0] = Common::Point(10, -10);
	_diceInc[1] = Common::Point(-10, -10);
	_diceInc[2] = Common::Point(-10, 10);

	_dice.load("dice.vga");
	_diceSize = _dice.getFrameSize(0);

	loadButtons();
}

void CreateCharacterDialog::execute() {
	EventsManager &events = *_vm->_events;
	Party &party = *_vm->_party;
	Screen &screen = *_vm->_screen;
	Windows &windows = *_vm->_windows;
	Window &w = windows[0];
	Common::Array<int> freeCharList;
	int classId;
	int selectedClass = 0;
	bool hasFadedIn = false;
	bool restartFlag = true;
	uint attribs[TOTAL_ATTRIBUTES];
	bool allowedClasses[TOTAL_CLASSES];
	Race race = HUMAN;
	Sex sex = MALE;
	Common::String msg, details;
	int charIndex = 0;

	Mode oldMode = _vm->_mode;
	_vm->_mode = MODE_4;

	// Load the background
	screen.loadBackground("create.raw");
	events.setCursor(0);

	while (!_vm->shouldExit()) {
		classId = -1;

		if (restartFlag) {
			// Build up list of roster slot indexes that are free
			freeCharList.clear();
			for (uint idx = 0; idx < XEEN_TOTAL_CHARACTERS; ++idx) {
				if (party._roster[idx]._name.empty())
					freeCharList.push_back(idx);
			}
			charIndex = 0;
			//bool flag9 = true;

			if (freeCharList.size() == XEEN_TOTAL_CHARACTERS)
				break;

			// Get and race and sex for the given character
			race = (Race)((freeCharList[charIndex] / 4) % 5);
			sex = (Sex)(freeCharList[charIndex] & 1);

			// Randomly determine attributes, and which classes they allow
			throwDice(attribs, allowedClasses);

			// Get the display of the rolled character details
			selectedClass = newCharDetails(attribs, allowedClasses,
				race, sex, classId, selectedClass, details);
			msg = Common::String::format(Res.CREATE_CHAR_DETAILS,
				details.c_str());

			// Draw the icons and the currently selected headshot
			drawIcons();
			party._roster[freeCharList[charIndex]]._faceSprites->draw(
				w, 0, Common::Point(27, 102));

			// Render all on-screen text
			w.writeString(msg);
			w.update();

			// Draw the arrow for the selected class, if applicable
			if (selectedClass != -1)
				printSelectionArrow(selectedClass);

			// Draw the dice
			drawDice();
			if (!hasFadedIn) {
				screen.fadeIn();
				hasFadedIn = true;
			}

			restartFlag = false;
		}

		// Animate the dice until a user action occurs
		_buttonValue = 0;
		while (!_vm->shouldExit() && !_buttonValue)
			drawDice();

		// Handling for different actions
		switch (_buttonValue) {
		case Common::KEYCODE_UP:
			if (charIndex == 0)
				continue;

			race = (Race)((freeCharList[charIndex] / 4) % 5);
			sex = (Sex)(freeCharList[charIndex] & 1);
			break;

		case Common::KEYCODE_DOWN:
			if (++charIndex == (int)freeCharList.size()) {
				--charIndex;
				continue;
			} else {
				race = (Race)((freeCharList[charIndex] / 4) % 5);
				sex = (Sex)(freeCharList[charIndex] & 1);
			}
			break;

		case Common::KEYCODE_PAGEUP:
			for (int tempClass = selectedClass - 1; tempClass >= 0; --tempClass) {
				if (allowedClasses[tempClass]) {
					selectedClass = tempClass;
					break;
				}
			}

			printSelectionArrow(selectedClass);
			continue;

		case Common::KEYCODE_PAGEDOWN:
			break;

		case Common::KEYCODE_m:
		case Common::KEYCODE_i:
		case Common::KEYCODE_p:
		case Common::KEYCODE_e:
		case Common::KEYCODE_s:
		case Common::KEYCODE_a:
		case Common::KEYCODE_l: {
			Attribute srcAttrib, destAttrib;
			if (_buttonValue == Common::KEYCODE_m)
				srcAttrib = MIGHT;
			else if (_buttonValue == Common::KEYCODE_i)
				srcAttrib = INTELLECT;
			else if (_buttonValue == Common::KEYCODE_p)
				srcAttrib = PERSONALITY;
			else if (_buttonValue == Common::KEYCODE_e)
				srcAttrib = ENDURANCE;
			else if (_buttonValue == Common::KEYCODE_s)
				srcAttrib = SPEED;
			else if (_buttonValue == Common::KEYCODE_a)
				srcAttrib = ACCURACY;
			else
				srcAttrib = LUCK;

			_vm->_mode = MODE_86;
			_icons.draw(w, srcAttrib * 2 + 11, Common::Point(
				_buttons[srcAttrib + 5]._bounds.left, _buttons[srcAttrib + 5]._bounds.top));
			w.update();

			int destAttribVal = exchangeAttribute(srcAttrib + 1);
			if (destAttribVal) {
				destAttrib = (Attribute)(destAttribVal - 1);
				_icons.draw(w, destAttrib * 2 + 11, Common::Point(
					_buttons[destAttrib + 10]._bounds.left,
					_buttons[destAttrib + 10]._bounds.top));
				w.update();

				SWAP(attribs[srcAttrib], attribs[destAttrib]);
				checkClass(attribs, allowedClasses);
				classId = -1;
				selectedClass = newCharDetails(attribs, allowedClasses,
					race, sex, classId, selectedClass, msg);
			} else {
				_icons.draw(w, srcAttrib * 2 + 10, Common::Point(
					_buttons[srcAttrib + 5]._bounds.left,
					_buttons[srcAttrib + 5]._bounds.top));
				w.update();
				_vm->_mode = MODE_SLEEPING;
				continue;
			}
			break;
		}

		case 1000:
		case 1001:
		case 1002:
		case 1003:
		case 1004:
		case 1005:
		case 1006:
		case 1007:
		case 1008:
		case 1009:
			if (allowedClasses[_buttonValue - 1000]) {
				selectedClass = classId = _buttonValue - 1000;
			}
			break;

		case Common::KEYCODE_c: {
			_vm->_mode = MODE_FF;
			bool result = saveCharacter(party._roster[freeCharList[charIndex]],
				classId, race, sex, attribs);
			_vm->_mode = MODE_4;

			if (result)
				restartFlag = true;
			continue;
		}

		case Common::KEYCODE_RETURN:
			classId = selectedClass;
			break;

		case Common::KEYCODE_SPACE:
		case Common::KEYCODE_r:
			// Re-roll the attributes
			throwDice(attribs, allowedClasses);
			classId = -1;
			break;

		default:
			// For all other keypresses, skip the code below the switch
			// statement, and go to wait for the next key
			continue;
		}

		if (_buttonValue != Common::KEYCODE_PAGEDOWN) {
			selectedClass = newCharDetails(attribs, allowedClasses,
				race, sex, classId, selectedClass, msg);

			drawIcons2();
			party._roster[freeCharList[charIndex]]._faceSprites->draw(w, 0,
				Common::Point(27, 102));

			w.writeString(msg);
			w.update();

			if (selectedClass != -1) {
				printSelectionArrow(selectedClass);
				continue;
			}
		}

		// Move to next available class, or if the code block above resulted in
		// selectedClass being -1, move to select the first available class
		for (int tempClass = selectedClass + 1; tempClass <= CLASS_RANGER; ++tempClass) {
			if (allowedClasses[tempClass]) {
				selectedClass = tempClass;
				break;
			}
		}

		printSelectionArrow(selectedClass);
	} while (!_vm->shouldExit() && _buttonValue != Common::KEYCODE_ESCAPE);

	_vm->_mode = oldMode;
}

void CreateCharacterDialog::loadButtons() {
	_icons.load("create.icn");

	// Add buttons
	addButton(Common::Rect(132, 98, 156, 118), Common::KEYCODE_r, &_icons);
	addButton(Common::Rect(132, 128, 156, 148), Common::KEYCODE_c, &_icons);
	addButton(Common::Rect(132, 158, 156, 178), Common::KEYCODE_ESCAPE, &_icons);
	addButton(Common::Rect(86, 98, 110, 118), Common::KEYCODE_UP, &_icons);
	addButton(Common::Rect(86, 120, 110, 140), Common::KEYCODE_DOWN, &_icons);
	addButton(Common::Rect(168, 19, 192, 39), Common::KEYCODE_n, nullptr);
	addButton(Common::Rect(168, 43, 192, 63), Common::KEYCODE_i, nullptr);
	addButton(Common::Rect(168, 67, 192, 87), Common::KEYCODE_p, nullptr);
	addButton(Common::Rect(168, 91, 192, 111), Common::KEYCODE_e, nullptr);
	addButton(Common::Rect(168, 115, 192, 135), Common::KEYCODE_s, nullptr);
	addButton(Common::Rect(168, 139, 192, 159), Common::KEYCODE_a, nullptr);
	addButton(Common::Rect(168, 163, 192, 183), Common::KEYCODE_l, nullptr);
	addButton(Common::Rect(227, 19, 239, 29), 1000, nullptr);
	addButton(Common::Rect(227, 30, 239, 40), 1001, nullptr);
	addButton(Common::Rect(227, 41, 239, 51), 1002, nullptr);
	addButton(Common::Rect(227, 52, 239, 62), 1003, nullptr);
	addButton(Common::Rect(227, 63, 239, 73), 1004, nullptr);
	addButton(Common::Rect(227, 74, 239, 84), 1005, nullptr);
	addButton(Common::Rect(227, 85, 239, 95), 1006, nullptr);
	addButton(Common::Rect(227, 96, 239, 106), 1007, nullptr);
	addButton(Common::Rect(227, 107, 239, 117), 1008, nullptr);
	addButton(Common::Rect(227, 118, 239, 128), 1009, nullptr);
}

void CreateCharacterDialog::drawIcons() {
	// Draw the screen
	_icons.draw(0, 10, Common::Point(168, 19));
	_icons.draw(0, 12, Common::Point(168, 43));
	_icons.draw(0, 14, Common::Point(168, 67));
	_icons.draw(0, 16, Common::Point(168, 91));
	_icons.draw(0, 18, Common::Point(168, 115));
	_icons.draw(0, 20, Common::Point(168, 139));
	_icons.draw(0, 22, Common::Point(168, 163));
	for (int idx = 0; idx < 9; ++idx)
		_icons.draw(0, 24 + idx * 2, Common::Point(227, 19 + 11 * idx));

	for (int idx = 0; idx < 7; ++idx)
		_icons.draw(0, 50 + idx, Common::Point(195, 31 + 24 * idx));

	_icons.draw(0, 57, Common::Point(62, 148));
	_icons.draw(0, 58, Common::Point(62, 158));
	_icons.draw(0, 59, Common::Point(62, 168));
	_icons.draw(0, 61, Common::Point(220, 19));
	_icons.draw(0, 64, Common::Point(220, 155));
	_icons.draw(0, 65, Common::Point(220, 170));

	_icons.draw(0, 0, Common::Point(132, 98));
	_icons.draw(0, 2, Common::Point(132, 128));
	_icons.draw(0, 4, Common::Point(132, 158));
	_icons.draw(0, 6, Common::Point(86, 98));
	_icons.draw(0, 8, Common::Point(86, 120));
}

void CreateCharacterDialog::drawIcons2() {
	for (int idx = 0; idx < 7; ++idx)
		_icons.draw(0, 10 + idx * 2, Common::Point(168, 19 + idx * 24));
	for (int idx = 0; idx < 10; ++idx)
		_icons.draw(0, 24 + idx * 2, Common::Point(227, 19 + idx * 11));
	for (int idx = 0; idx < 8; ++idx)
		_icons.draw(0, 50 + idx, Common::Point(195, 31 + idx * 24));

	_icons.draw(0, 57, Common::Point(62, 148));
	_icons.draw(0, 58, Common::Point(62, 158));
	_icons.draw(0, 59, Common::Point(62, 168));
	_icons.draw(0, 61, Common::Point(220, 19));
	_icons.draw(0, 64, Common::Point(220, 155));
	_icons.draw(0, 65, Common::Point(220, 170));

	_icons.draw(0, 0, Common::Point(132, 98));
	_icons.draw(0, 2, Common::Point(132, 128));
	_icons.draw(0, 4, Common::Point(132, 158));
	_icons.draw(0, 6, Common::Point(86, 98));
	_icons.draw(0, 8, Common::Point(86, 120));
}

void CreateCharacterDialog::throwDice(uint attribs[TOTAL_ATTRIBUTES], bool allowedClasses[TOTAL_CLASSES]) {
	bool repeat = true;
	do {
		// Default all the attributes to zero
		Common::fill(&attribs[0], &attribs[TOTAL_ATTRIBUTES], 0);

		// Assign random amounts to each attribute
		for (int idx1 = 0; idx1 < 3; ++idx1) {
			for (int idx2 = 0; idx2 < TOTAL_ATTRIBUTES; ++idx2) {
				attribs[idx1] += _vm->getRandomNumber(10, 79) / 10;
			}
		}

		// Check which classes are allowed based on the rolled attributes
		checkClass(attribs, allowedClasses);

		// Only exit if the attributes allow for at least one class
		for (int idx = 0; idx < TOTAL_CLASSES; ++idx) {
			if (allowedClasses[idx])
				repeat = false;
		}
	} while (repeat);
}

void CreateCharacterDialog::checkClass(const uint attribs[TOTAL_ATTRIBUTES], bool allowedClasses[TOTAL_CLASSES]) {
	allowedClasses[CLASS_KNIGHT] = attribs[MIGHT] >= 15;
	allowedClasses[CLASS_PALADIN] = attribs[MIGHT] >= 13
		&& attribs[PERSONALITY] >= 13 && attribs[ENDURANCE] >= 13;
	allowedClasses[CLASS_ARCHER] = attribs[INTELLECT] >= 13 && attribs[ACCURACY] >= 13;
	allowedClasses[CLASS_CLERIC] = attribs[PERSONALITY] >= 13;
	allowedClasses[CLASS_SORCERER] = attribs[INTELLECT] >= 13;
	allowedClasses[CLASS_ROBBER] = attribs[LUCK] >= 13;
	allowedClasses[CLASS_NINJA] = attribs[SPEED] >= 13 && attribs[ACCURACY] >= 13;
	allowedClasses[CLASS_BARBARIAN] = attribs[ENDURANCE] >= 15;
	allowedClasses[CLASS_DRUID] = attribs[INTELLECT] >= 15 && attribs[PERSONALITY] >= 15;
	allowedClasses[CLASS_RANGER] = attribs[INTELLECT] >= 12 && attribs[PERSONALITY] >= 12
		&& attribs[ENDURANCE] >= 12 && attribs[SPEED] >= 12;
}

int CreateCharacterDialog::newCharDetails(const uint attribs[TOTAL_ATTRIBUTES],
		bool allowedClasses[TOTAL_CLASSES], Race race, Sex sex, int classId,
		int selectedClass, Common::String &msg) {
	int foundClass = -1;
	Common::String skillStr, classStr, raceSkillStr;

	// If a selected class is provided, set the default skill for that class
	if (classId != -1 && Res.NEW_CHAR_SKILLS[classId] != -1) {
		const char *skillP = Res.SKILL_NAMES[Res.NEW_CHAR_SKILLS[classId]];
		skillStr = Common::String(skillP, skillP + Res.NEW_CHAR_SKILLS_LEN[classId]);
	}

	// If a class is provided, set the class name
	if (classId != -1) {
		classStr = Common::String::format("\t062\v168%s", Res.CLASS_NAMES[classId]);
	}

	// Set up default skill for the race, if any
	if (Res.NEW_CHAR_RACE_SKILLS[race] != -1) {
		raceSkillStr = Res.SKILL_NAMES[Res.NEW_CHAR_RACE_SKILLS[race]];
	}

	// Set up color to use for each skill string to be displayed, based
	// on whether each class is allowed or not for the given attributes
	int classColors[TOTAL_CLASSES];
	Common::fill(&classColors[0], &classColors[TOTAL_CLASSES], 0);
	for (int classNum = CLASS_KNIGHT; classNum <= CLASS_RANGER; ++classNum) {
		if (allowedClasses[classNum]) {
			if (classId == -1 && (foundClass == -1 || foundClass < classNum))
				foundClass = classNum;
			classColors[classNum] = 4;
		}
	}

	// Return stats details and character class
	msg = Common::String::format(Res.NEW_CHAR_STATS, Res.RACE_NAMES[race], Res.SEX_NAMES[sex],
		attribs[MIGHT], attribs[INTELLECT], attribs[PERSONALITY],
		attribs[ENDURANCE], attribs[SPEED], attribs[ACCURACY], attribs[LUCK],
		classColors[CLASS_KNIGHT], classColors[CLASS_PALADIN],
		classColors[CLASS_ARCHER], classColors[CLASS_CLERIC],
		classColors[CLASS_SORCERER], classColors[CLASS_ROBBER],
		classColors[CLASS_NINJA], classColors[CLASS_BARBARIAN],
		classColors[CLASS_DRUID], classColors[CLASS_RANGER],
		skillStr.c_str(), raceSkillStr.c_str(), classStr.c_str()
	);
	return classId == -1 ? foundClass : selectedClass;
}

void CreateCharacterDialog::printSelectionArrow(int selectedClass) {
	Windows &windows = *_vm->_windows;
	Window &w = windows[0];

	_icons.draw(0, 61, Common::Point(220, 19));
	_icons.draw(0, 63, Common::Point(220, selectedClass * 11 + 21));
	w.update();
}

void CreateCharacterDialog::drawDice() {
	EventsManager &events = *_vm->_events;
	Windows &windows = *_vm->_windows;
	Window &w = windows[32];

	// Draw the dice area background
	events.updateGameCounter();
	_dice.draw(w, 7, Common::Point(12, 11));

	// Iterate through each of the three dice
	for (int diceNum = 0; diceNum < 3; ++diceNum) {
		_diceFrame[diceNum] = (_diceFrame[diceNum] + 1) % 7;
		_dicePos[diceNum] += _diceInc[diceNum];

		if (_dicePos[diceNum].x < 13) {
			_dicePos[diceNum].x = 13;
			_diceInc[diceNum].x *= -1;
		} else if (_dicePos[diceNum].x >= (163 - _diceSize.x)) {
			_dicePos[diceNum].x = 163 - _diceSize.x;
			_diceInc[diceNum].x *= -1;
		}

		if (_dicePos[diceNum].y < 12) {
			_dicePos[diceNum].y = 12;
			_diceInc[diceNum].y *= -1;
		} else if (_dicePos[diceNum].y >= (93 - _diceSize.y)) {
			_dicePos[diceNum].y = 93 - _diceSize.y;
			_diceInc[diceNum].y *= -1;
		}

		_dice.draw(w, _diceFrame[diceNum], _dicePos[diceNum]);
	}

	// Wait for a single frame, checking for any events
	w.update();
	events.wait(1);
	checkEvents(_vm);
}

int CreateCharacterDialog::exchangeAttribute(int srcAttr) {
	EventsManager &events = *_vm->_events;
	Windows &windows = *_vm->_windows;
	SpriteResource icons;
	icons.load("create2.icn");

	saveButtons();
	addButton(Common::Rect(118, 58, 142, 78), Common::KEYCODE_ESCAPE, &_icons);
	addButton(Common::Rect(168, 19, 192, 39), Common::KEYCODE_m);
	addButton(Common::Rect(168, 43, 192, 63), Common::KEYCODE_i);
	addButton(Common::Rect(168, 67, 192, 87), Common::KEYCODE_p);
	addButton(Common::Rect(168, 91, 192, 111), Common::KEYCODE_e);
	addButton(Common::Rect(168, 115, 192, 135), Common::KEYCODE_s);
	addButton(Common::Rect(168, 139, 192, 159), Common::KEYCODE_a);
	addButton(Common::Rect(168, 163, 192, 183), Common::KEYCODE_l);

	Window &w = windows[26];
	w.open();
	w.writeString(Common::String::format(Res.EXCHANGE_ATTR_WITH, Res.STAT_NAMES[srcAttr - 1]));
	icons.draw(w, 0, Common::Point(118, 58));
	w.update();

	int result = 0;
	bool breakFlag = false;
	while (!_vm->shouldExit() && !breakFlag) {
		// Wait for an action
		do {
			events.pollEventsAndWait();
			checkEvents(_vm);
		} while (!_vm->shouldExit() && !_buttonValue);

		Attribute destAttr;
		switch (_buttonValue) {
		case Common::KEYCODE_m:
			destAttr = MIGHT;
			break;
		case Common::KEYCODE_i:
			destAttr = INTELLECT;
			break;
		case Common::KEYCODE_p:
			destAttr = PERSONALITY;
			break;
		case Common::KEYCODE_e:
			destAttr = ENDURANCE;
			break;
		case Common::KEYCODE_s:
			destAttr = SPEED;
			break;
		case Common::KEYCODE_a:
			destAttr = ACCURACY;
			break;
		case Common::KEYCODE_l:
			destAttr = LUCK;
			break;
		case Common::KEYCODE_ESCAPE:
			result = 0;
			breakFlag = true;
			continue;
		default:
			continue;
		}

		if ((srcAttr - 1) != destAttr) {
			result = destAttr + 1;
			break;
		}
	}

	w.close();
	_buttonValue = 0;
	return result;
}

bool CreateCharacterDialog::saveCharacter(Character &c, int classId,
		Race race, Sex sex, uint attribs[TOTAL_ATTRIBUTES]) {
	if (classId == -1) {
		ErrorScroll::show(_vm, Res.SELECT_CLASS_BEFORE_SAVING);
		return false;
	}

	Map &map = *_vm->_map;
	Party &party = *_vm->_party;
	Windows &windows = *_vm->_windows;
	Window &w = windows[6];
	Common::String name;
	int result;
	bool isDarkCc = _vm->_files->_isDarkCc;

	saveButtons();
	w.writeString(Res.NAME_FOR_NEW_CHARACTER);

	result = Input::show(_vm, &w, name, 10, 200);
	w.close();
	restoreButtons();
	if (!result)
		return false;

	// Save new character details
	c.clear();
	c._name = name;
	c._savedMazeId = party._priorMazeId;
	c._xeenSide = map._loadDarkSide;
	c._sex = sex;
	c._race = race;
	c._class = (CharacterClass)classId;
	c._level._permanent = isDarkCc ? 5 : 1;

	c._might._permanent = attribs[MIGHT];
	c._intellect._permanent = attribs[INTELLECT];
	c._personality._permanent = attribs[PERSONALITY];
	c._endurance._permanent = attribs[ENDURANCE];
	c._speed._permanent = attribs[SPEED];
	c._accuracy._permanent = attribs[ACCURACY];
	c._luck._permanent = attribs[LUCK];

	c._magicResistence._permanent = Res.RACE_MAGIC_RESISTENCES[race];
	c._fireResistence._permanent = Res.RACE_FIRE_RESISTENCES[race];
	c._electricityResistence._permanent = Res.RACE_ELECTRIC_RESISTENCES[race];
	c._coldResistence._permanent = Res.RACE_COLD_RESISTENCES[race];
	c._energyResistence._permanent = Res.RACE_ENERGY_RESISTENCES[race];
	c._poisonResistence._permanent = Res.RACE_POISON_RESISTENCES[race];

	c._birthYear = party._year - 18;
	c._birthDay = party._day;
	c._hasSpells = false;
	c._currentSpell = -1;

	// Set up any default spells for the character's class
	for (int idx = 0; idx < 4; ++idx) {
		if (Res.NEW_CHARACTER_SPELLS[c._class][idx] != -1) {
			c._hasSpells = true;
			c._currentSpell = Res.NEW_CHARACTER_SPELLS[c._class][idx];
			c._spells[c._currentSpell] = true;
		}
	}

	int classSkill = Res.NEW_CHAR_SKILLS[c._class];
	if (classSkill != -1)
		c._skills[classSkill] = 1;

	int raceSkill = Res.NEW_CHAR_RACE_SKILLS[c._race];
	if (raceSkill != -1)
		c._skills[raceSkill] = 1;

	c._currentHp = c.getMaxHP();
	c._currentSp = c.getMaxSP();
	return true;
}

} // End of namespace Xeen