1# -*-python-*-
2# GemRB - Infinity Engine Emulator
3# Copyright (C) 2003-2005 The GemRB Project
4#
5# This program is free software; you can redistribute it and/or
6# modify it under the terms of the GNU General Public License
7# as published by the Free Software Foundation; either version 2
8# of the License, or (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18#
19
20
21#Character Generation
22
23###################################################
24
25import GemRB
26from GUIDefines import *
27from ie_stats import *
28from ie_spells import LS_MEMO
29import GUICommon
30import Spellbook
31import CommonTables
32import LUSkillsSelection
33
34CharGenWindow = 0
35CharGenState = 0
36TextArea = 0
37PortraitButton = 0
38AcceptButton = 0
39
40GenderButton = 0
41GenderWindow = 0
42GenderTextArea = 0
43GenderDoneButton = 0
44
45Portrait = 0
46PortraitsTable = 0
47PortraitPortraitButton = 0
48
49RaceButton = 0
50RaceWindow = 0
51RaceTextArea = 0
52RaceDoneButton = 0
53
54ClassButton = 0
55ClassWindow = 0
56ClassTextArea = 0
57ClassDoneButton = 0
58
59ClassMultiWindow = 0
60ClassMultiTextArea = 0
61ClassMultiDoneButton = 0
62
63KitTable = 0
64KitWindow = 0
65KitTextArea = 0
66KitDoneButton = 0
67
68AlignmentButton = 0
69AlignmentWindow = 0
70AlignmentTextArea = 0
71AlignmentDoneButton = 0
72
73AbilitiesButton = 0
74AbilitiesWindow = 0
75AbilitiesTable = 0
76AbilitiesRaceAddTable = 0
77AbilitiesRaceReqTable = 0
78AbilitiesClassReqTable = 0
79AbilitiesMinimum = 0
80AbilitiesMaximum = 0
81AbilitiesModifier = 0
82AbilitiesTextArea = 0
83AbilitiesRecallButton = 0
84AbilitiesDoneButton = 0
85
86SkillsButton = 0
87SkillsWindow = 0
88SkillsTable = 0
89SkillsTextArea = 0
90SkillsDoneButton = 0
91SkillsPointsLeft = 0
92SkillsState = 0
93
94RacialEnemyButton = 0
95RacialEnemyWindow = 0
96RacialEnemyTable = 0
97RacialEnemyTextArea = 0
98RacialEnemyDoneButton = 0
99
100ProficienciesWindow = 0
101ProficienciesTable = 0
102ProfsMaxTable = 0
103ProficienciesTextArea = 0
104ProficienciesDoneButton = 0
105ProficienciesPointsLeft = 0
106
107MageSpellsWindow = 0
108MageSpellsTextArea = 0
109MageSpellsDoneButton = 0
110MageSpellsSelectPointsLeft = 0
111
112MageMemorizeWindow = 0
113MageMemorizeTextArea = 0
114MageMemorizeDoneButton = 0
115MageMemorizePointsLeft = 0
116
117PriestMemorizeWindow = 0
118PriestMemorizeTextArea = 0
119PriestMemorizeDoneButton = 0
120PriestMemorizePointsLeft = 0
121
122AppearanceButton = 0
123AppearanceWindow = 0
124AppearanceTable = 0
125AppearanceAvatarButton = 0
126AppearanceHairButton = 0
127AppearanceSkinButton = 0
128AppearanceMajorButton = 0
129AppearanceMinorButton = 0
130HairColor = 0
131SkinColor = 0
132MajorColor = 0
133MinorColor = 0
134
135CharSoundWindow = 0
136CharSoundTable = 0
137CharSoundStrings = 0
138
139BiographyButton = 0
140BiographyWindow = 0
141BiographyTextArea = 0
142
143NameButton = 0
144NameWindow = 0
145NameField = 0
146NameDoneButton = 0
147
148SoundIndex = 0
149VerbalConstants = None
150HasStrExtra = 0
151MyChar = 0
152ImportedChar = 0
153
154def OnLoad():
155	global CharGenWindow, CharGenState, TextArea, PortraitButton, AcceptButton
156	global GenderButton, RaceButton, ClassButton, AlignmentButton
157	global AbilitiesButton, SkillsButton, AppearanceButton, BiographyButton, NameButton
158	global KitTable, ProficienciesTable, RacialEnemyTable
159	global AbilitiesTable, SkillsTable, PortraitsTable
160	global MyChar, ImportedChar
161
162	KitTable = GemRB.LoadTable ("magesch")
163	ProficienciesTable = GemRB.LoadTable ("weapprof")
164	RacialEnemyTable = GemRB.LoadTable ("haterace")
165	AbilitiesTable = GemRB.LoadTable ("ability")
166	SkillsTable = GemRB.LoadTable ("skills")
167	PortraitsTable = GemRB.LoadTable ("pictures")
168	CharGenWindow = GemRB.LoadWindow (0, "GUICG")
169	CharGenState = 0
170	MyChar = GemRB.GetVar ("Slot")
171	ImportedChar = 0
172
173	GenderButton = CharGenWindow.GetControl (0)
174	GenderButton.SetState (IE_GUI_BUTTON_ENABLED)
175	GenderButton.MakeDefault()
176	GenderButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, GenderPress)
177	GenderButton.SetText (11956)
178
179	RaceButton = CharGenWindow.GetControl (1)
180	RaceButton.SetState (IE_GUI_BUTTON_DISABLED)
181	RaceButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, RacePress)
182	RaceButton.SetText (11957)
183
184	ClassButton = CharGenWindow.GetControl (2)
185	ClassButton.SetState (IE_GUI_BUTTON_DISABLED)
186	ClassButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, ClassPress)
187	ClassButton.SetText (11959)
188
189	AlignmentButton = CharGenWindow.GetControl (3)
190	AlignmentButton.SetState (IE_GUI_BUTTON_DISABLED)
191	AlignmentButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AlignmentPress)
192	AlignmentButton.SetText (11958)
193
194	AbilitiesButton = CharGenWindow.GetControl (4)
195	AbilitiesButton.SetState (IE_GUI_BUTTON_DISABLED)
196	AbilitiesButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AbilitiesPress)
197	AbilitiesButton.SetText (11960)
198
199	SkillsButton = CharGenWindow.GetControl (5)
200	SkillsButton.SetState (IE_GUI_BUTTON_DISABLED)
201	SkillsButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, SkillsPress)
202	SkillsButton.SetText (11983)
203
204	AppearanceButton = CharGenWindow.GetControl (6)
205	AppearanceButton.SetState (IE_GUI_BUTTON_DISABLED)
206	AppearanceButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AppearancePress)
207	AppearanceButton.SetText (11961)
208
209	BiographyButton = CharGenWindow.GetControl (16)
210	BiographyButton.SetState (IE_GUI_BUTTON_DISABLED)
211	BiographyButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, BiographyPress)
212	BiographyButton.SetText (18003)
213
214	NameButton = CharGenWindow.GetControl (7)
215	NameButton.SetState (IE_GUI_BUTTON_DISABLED)
216	NameButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, NamePress)
217	NameButton.SetText (11963)
218
219	BackButton = CharGenWindow.GetControl (11)
220	BackButton.SetState (IE_GUI_BUTTON_ENABLED)
221	BackButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, BackPress)
222	BackButton.MakeEscape()
223
224	PortraitButton = CharGenWindow.GetControl (12)
225	PortraitButton.SetFlags (IE_GUI_BUTTON_PICTURE|IE_GUI_BUTTON_NO_IMAGE, OP_SET)
226	PortraitButton.SetState (IE_GUI_BUTTON_LOCKED)
227
228	ImportButton = CharGenWindow.GetControl (13)
229	ImportButton.SetState (IE_GUI_BUTTON_ENABLED)
230	ImportButton.SetText (13955)
231	ImportButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, ImportPress)
232
233	CancelButton = CharGenWindow.GetControl (15)
234	CancelButton.SetState (IE_GUI_BUTTON_ENABLED)
235	CancelButton.SetText (13727)
236	CancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, CancelPress)
237
238	AcceptButton = CharGenWindow.GetControl (8)
239	AcceptButton.SetState (IE_GUI_BUTTON_DISABLED)
240	AcceptButton.SetText (11962)
241	AcceptButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AcceptPress)
242
243	TextArea = CharGenWindow.GetControl (9)
244	TextArea.SetText (16575)
245
246	return
247
248def BackPress():
249	global CharGenWindow, CharGenState, SkillsState
250	global GenderButton, RaceButton, ClassButton, AlignmentButton, AbilitiesButton, SkillsButton, AppearanceButton, BiographyButton, NameButton
251
252	if CharGenState > 0:
253		CharGenState = CharGenState - 1
254	else:
255		CancelPress()
256		return
257
258	if CharGenState > 6:
259		CharGenState = 6
260		GemRB.SetToken ("CHARNAME","")
261
262	if CharGenState == 0:
263		RaceButton.SetState (IE_GUI_BUTTON_DISABLED)
264		GenderButton.SetState (IE_GUI_BUTTON_ENABLED)
265		GenderButton.MakeDefault()
266	elif CharGenState == 1:
267		ClassButton.SetState (IE_GUI_BUTTON_DISABLED)
268		RaceButton.SetState (IE_GUI_BUTTON_ENABLED)
269		RaceButton.MakeDefault()
270	elif CharGenState == 2:
271		AlignmentButton.SetState (IE_GUI_BUTTON_DISABLED)
272		ClassButton.SetState (IE_GUI_BUTTON_ENABLED)
273		ClassButton.MakeDefault()
274	elif CharGenState == 3:
275		AbilitiesButton.SetState (IE_GUI_BUTTON_DISABLED)
276		AlignmentButton.SetState (IE_GUI_BUTTON_ENABLED)
277		AlignmentButton.MakeDefault()
278	elif CharGenState == 4:
279		SkillsButton.SetState (IE_GUI_BUTTON_DISABLED)
280		AbilitiesButton.SetState (IE_GUI_BUTTON_ENABLED)
281		AbilitiesButton.MakeDefault()
282	elif CharGenState == 5:
283		AppearanceButton.SetState (IE_GUI_BUTTON_DISABLED)
284		SkillsButton.SetState (IE_GUI_BUTTON_ENABLED)
285		SkillsButton.MakeDefault()
286		SkillsState = 0
287	elif CharGenState == 6:
288		NameButton.SetState (IE_GUI_BUTTON_DISABLED)
289		BiographyButton.SetState (IE_GUI_BUTTON_DISABLED)
290		AppearanceButton.SetState (IE_GUI_BUTTON_ENABLED)
291
292	AcceptButton.SetState (IE_GUI_BUTTON_DISABLED)
293	SetCharacterDescription()
294	return
295
296def CancelPress():
297	global CharGenWindow
298
299	if CharGenWindow:
300		CharGenWindow.Unload ()
301	GemRB.CreatePlayer ("", MyChar | 0x8000 )
302	GemRB.SetNextScript ("PartyFormation")
303	return
304
305def LearnSpells(MyChar):
306	Kit = GemRB.GetPlayerStat (MyChar, IE_KIT)
307	ClassName = GUICommon.GetClassRowName (MyChar)
308	t = GemRB.GetPlayerStat (MyChar, IE_ALIGNMENT)
309
310	# mage spells
311	TableName = CommonTables.ClassSkills.GetValue (ClassName, "MAGESPELL", GTV_STR)
312	if TableName != "*":
313		# setting up just the first spell level is enough, since the rest will be granted on level-up
314		Spellbook.SetupSpellLevels (MyChar, TableName, IE_SPELL_TYPE_WIZARD, 1)
315		Learnable = Spellbook.GetLearnableMageSpells (Kit, t, 1)
316		SpellBook = GemRB.GetVar ("MageSpellBook")
317		MemoBook = GemRB.GetVar ("MageMemorized")
318		j=1
319		for i in range (len(Learnable) ):
320			if SpellBook & j:
321				memorize = LS_MEMO if MemoBook & j else 0
322				GemRB.LearnSpell (MyChar, Learnable[i], memorize)
323			j=j<<1
324
325	#priest spells
326	TableName = CommonTables.ClassSkills.GetValue (ClassName, "CLERICSPELL", GTV_STR)
327	# druids and rangers have a column of their own
328	if TableName == "*":
329		TableName = CommonTables.ClassSkills.GetValue (ClassName, "DRUIDSPELL", GTV_STR)
330	if TableName != "*":
331		ClassFlag = GetClassFlag (TableName)
332		TableName = Spellbook.GetPriestSpellTable (TableName)
333
334		Spellbook.SetupSpellLevels (MyChar, TableName, IE_SPELL_TYPE_PRIEST, 1)
335		Learnable = Spellbook.GetLearnablePriestSpells (ClassFlag, t, 1)
336		PriestMemorized = GemRB.GetVar ("PriestMemorized")
337		j = 0
338		while PriestMemorized and PriestMemorized != 1<<j:
339			j = j + 1
340		for i in range (len(Learnable) ):
341			GemRB.LearnSpell (MyChar, Learnable[i], 0)
342		GemRB.MemorizeSpell (MyChar, IE_SPELL_TYPE_PRIEST, 0, j, 1)
343
344def AcceptPress():
345	# apply class/kit abilities
346	ClassName = GUICommon.GetClassRowName (MyChar)
347	GUICommon.ResolveClassAbilities (MyChar, ClassName)
348	t = GemRB.GetPlayerStat (MyChar, IE_ALIGNMENT)
349
350	TmpTable = GemRB.LoadTable ("repstart")
351	t = CommonTables.Aligns.FindValue (3, t)
352	t = TmpTable.GetValue (t, 0) * 10
353	GemRB.SetPlayerStat (MyChar, IE_REPUTATION, t)
354	# set the party rep if this in the main char
355	if MyChar == 1:
356		GemRB.GameSetReputation (t)
357
358	TmpTable = GemRB.LoadTable ("strtgold")
359	a = TmpTable.GetValue (ClassName, "ROLLS") #number of dice
360	b = TmpTable.GetValue (ClassName, "SIDES") #size
361	c = TmpTable.GetValue (ClassName, "MODIFIER") #adjustment
362	d = TmpTable.GetValue (ClassName, "MULTIPLIER") #external multiplier
363	e = TmpTable.GetValue (ClassName, "BONUS_PER_LEVEL") #level bonus rate (iwd only!)
364	t = GemRB.GetPlayerStat (MyChar, IE_LEVEL)
365	if t>1:
366		e=e*(t-1)
367	else:
368		e=0
369	t = GemRB.Roll (a,b,c)*d+e
370	GemRB.SetPlayerStat (MyChar, IE_GOLD, t)
371	GemRB.SetPlayerStat (MyChar, IE_EA, 2 )
372
373	GemRB.SetPlayerName (MyChar, GemRB.GetToken ("CHARNAME"), 0)
374	GemRB.SetToken ("CHARNAME","")
375	# don't reset imported char's xp back to start
376	if not ImportedChar:
377		GemRB.SetPlayerStat (MyChar, IE_XP, CommonTables.ClassSkills.GetValue (ClassName, "STARTXP"))
378
379	GUICommon.SetColorStat (MyChar, IE_SKIN_COLOR, GemRB.GetVar ("SkinColor") )
380	GUICommon.SetColorStat (MyChar, IE_HAIR_COLOR, GemRB.GetVar ("HairColor") )
381	GUICommon.SetColorStat (MyChar, IE_MAJOR_COLOR, GemRB.GetVar ("MajorColor") )
382	GUICommon.SetColorStat (MyChar, IE_MINOR_COLOR, GemRB.GetVar ("MinorColor") )
383	GUICommon.SetColorStat (MyChar, IE_METAL_COLOR, 0x1B )
384	GUICommon.SetColorStat (MyChar, IE_LEATHER_COLOR, 0x16 )
385	GUICommon.SetColorStat (MyChar, IE_ARMOR_COLOR, 0x17 )
386
387	#does all the rest
388	LargePortrait = GemRB.GetToken ("LargePortrait")
389	SmallPortrait = GemRB.GetToken ("SmallPortrait")
390	GemRB.FillPlayerInfo (MyChar, LargePortrait, SmallPortrait)
391
392	#10 is a weapon slot (see slottype.2da row 10)
393	if not ImportedChar:
394		GemRB.CreateItem (MyChar, "staf01", 10, 1, 0, 0)
395	GemRB.SetEquippedQuickSlot (MyChar, 0)
396
397	if CharGenWindow:
398		CharGenWindow.Unload ()
399	GemRB.SetNextScript ("PartyFormation")
400	return
401
402def SetCharacterDescription():
403	global CharGenWindow, TextArea, CharGenState, ClassFlag
404	global MyChar
405
406	TextArea.Clear()
407	if CharGenState > 7:
408		TextArea.Append (1047)
409		TextArea.Append (": ")
410		TextArea.Append (GemRB.GetToken ("CHARNAME"))
411		TextArea.Append ("\n")
412	if CharGenState > 0:
413		TextArea.Append (12135)
414		TextArea.Append (": ")
415		if GemRB.GetPlayerStat (MyChar, IE_SEX) == 1:
416			TextArea.Append (1050)
417		else:
418			TextArea.Append (1051)
419		TextArea.Append ("\n")
420	if CharGenState > 2:
421		ClassName = GUICommon.GetClassRowName (MyChar)
422		TextArea.Append (12136)
423		TextArea.Append (": ")
424		#this is only mage school in iwd
425		Kit = GemRB.GetPlayerStat (MyChar, IE_KIT)
426		KitIndex = KitTable.FindValue (3, Kit)
427		if KitIndex <= 0:
428			ClassTitle = CommonTables.Classes.GetValue (ClassName, "CAP_REF")
429		else:
430			ClassTitle = KitTable.GetValue (KitIndex, 2)
431		TextArea.Append (ClassTitle)
432		TextArea.Append ("\n")
433
434	if CharGenState > 1:
435		TextArea.Append (1048)
436		TextArea.Append (": ")
437		Race = GemRB.GetPlayerStat (MyChar, IE_RACE)
438		Race = CommonTables.Races.FindValue (3, Race)
439		TextArea.Append (CommonTables.Races.GetValue (Race, 2) )
440		TextArea.Append ("\n")
441	if CharGenState > 3:
442		TextArea.Append (1049)
443		TextArea.Append (": ")
444		Alignment = CommonTables.Aligns.FindValue (3, GemRB.GetPlayerStat(MyChar, IE_ALIGNMENT))
445		TextArea.Append (CommonTables.Aligns.GetValue (Alignment, 2))
446		TextArea.Append ("\n")
447	if CharGenState > 4:
448		strextra = GemRB.GetPlayerStat (MyChar, IE_STREXTRA)
449		TextArea.Append ("\n")
450		for i in range (6):
451			TextArea.Append (AbilitiesTable.GetValue (i, 2))
452			TextArea.Append (": " )
453			StatID = AbilitiesTable.GetValue (i, 3)
454			stat = GemRB.GetPlayerStat (MyChar, StatID)
455			if (i == 0) and HasStrExtra and (stat==18):
456				TextArea.Append (str(stat) + "/" + str(strextra) )
457			else:
458				TextArea.Append (str(stat) )
459			TextArea.Append ("\n")
460	if CharGenState > 5:
461		DruidSpell = CommonTables.ClassSkills.GetValue (ClassName, "DRUIDSPELL")
462		PriestSpell = CommonTables.ClassSkills.GetValue (ClassName, "CLERICSPELL")
463		MageSpell = CommonTables.ClassSkills.GetValue (ClassName, "MAGESPELL")
464		IsBard = CommonTables.ClassSkills.GetValue (ClassName, "BARDSKILL")
465		IsThief = CommonTables.ClassSkills.GetValue (ClassName, "THIEFSKILL")
466
467		if IsThief!="*":
468			TextArea.Append ("\n")
469			TextArea.Append (8442)
470			TextArea.Append ("\n")
471			for i in range (4):
472				TextArea.Append (SkillsTable.GetValue (i+2, 2))
473				StatID = SkillsTable.GetValue (i+2, 3)
474				TextArea.Append (": " )
475				TextArea.Append (str(GemRB.GetPlayerStat (MyChar, StatID)) )
476				TextArea.Append ("%\n")
477		elif DruidSpell!="*":
478			PositiveStats = []
479			for i in range (4):
480				StatID = SkillsTable.GetValue (i+2, 3)
481				Stat = GemRB.GetPlayerStat (MyChar, StatID)
482				if Stat>0:
483					PositiveStats.append ((i, Stat))
484			if PositiveStats:
485				TextArea.Append ("\n")
486				TextArea.Append (8442)
487				TextArea.Append ("\n")
488				for i, Stat in PositiveStats:
489					TextArea.Append (SkillsTable.GetValue (i+2, 2))
490					TextArea.Append (": " )
491					TextArea.Append (str(Stat) )
492					TextArea.Append ("%\n")
493
494			RacialEnemy = GemRB.GetVar ("RacialEnemyIndex") + GemRB.GetVar ("RacialEnemy") - 1
495			if RacialEnemy != -1:
496				TextArea.Append ("\n")
497				TextArea.Append (15982)
498				TextArea.Append (": " )
499				TextArea.Append (RacialEnemyTable.GetValue (RacialEnemy, 3))
500				TextArea.Append ("\n")
501		elif IsBard!="*":
502			TextArea.Append ("\n")
503			TextArea.Append (8442)
504			TextArea.Append ("\n")
505			for i in range (4):
506				StatID = SkillsTable.GetValue (i+2, 3)
507				Stat = GemRB.GetPlayerStat (MyChar, StatID)
508				if Stat>0:
509					TextArea.Append (SkillsTable.GetValue (i+2, 2))
510					TextArea.Append (": " )
511					TextArea.Append (str(Stat) )
512					TextArea.Append ("%\n")
513
514		if MageSpell != "*":
515			info = Spellbook.GetKnownSpellsDescription (MyChar, IE_SPELL_TYPE_WIZARD)
516			if info:
517				TextArea.Append ("\n" + GemRB.GetString (11027) + "\n" + info)
518
519		if PriestSpell == "*":
520			PriestSpell = DruidSpell
521		if PriestSpell != "*":
522			info = Spellbook.GetKnownSpellsDescription (MyChar, IE_SPELL_TYPE_PRIEST)
523			if info:
524				TextArea.Append ("\n" + GemRB.GetString (11028) + "\n" + info)
525
526		TextArea.Append ("\n")
527		TextArea.Append (9466)
528		TextArea.Append ("\n")
529		for i in range (15):
530			StatID = ProficienciesTable.GetValue (i, 0)
531			ProficiencyValue = GemRB.GetPlayerStat (MyChar, StatID )
532			if ProficiencyValue > 0:
533				TextArea.Append (ProficienciesTable.GetValue (i, 3))
534				TextArea.Append (" ")
535				j = 0
536				while j < ProficiencyValue:
537					TextArea.Append ("+")
538					j = j + 1
539				TextArea.Append ("\n")
540	return
541
542def GetClassFlag(TableName):
543	if TableName in ("MXSPLPRS", "MXSPLPAL"):
544		return 0x4000
545	elif TableName in ("MXSPLDRU", "MXSPLRAN"):
546		return 0x8000
547	else:
548		return 0
549
550# Gender Selection
551def GenderPress():
552	global CharGenWindow, GenderWindow, GenderDoneButton, GenderTextArea
553	global MyChar
554
555	GenderWindow = GemRB.LoadWindow (1)
556	GemRB.SetVar ("Gender", 0)
557	GemRB.CreatePlayer ("charbase", MyChar | 0x8000 )
558
559	MaleButton = GenderWindow.GetControl (2)
560	MaleButton.SetState (IE_GUI_BUTTON_ENABLED)
561	MaleButton.SetFlags (IE_GUI_BUTTON_RADIOBUTTON,OP_OR)
562	MaleButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, MalePress)
563
564	FemaleButton = GenderWindow.GetControl (3)
565	FemaleButton.SetState (IE_GUI_BUTTON_ENABLED)
566	FemaleButton.SetFlags (IE_GUI_BUTTON_RADIOBUTTON,OP_OR)
567	FemaleButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, FemalePress)
568
569	MaleButton.SetVarAssoc ("Gender", 1)
570	FemaleButton.SetVarAssoc ("Gender", 2)
571
572	GenderTextArea = GenderWindow.GetControl (5)
573	GenderTextArea.SetText (17236)
574
575	GenderDoneButton = GenderWindow.GetControl (0)
576	GenderDoneButton.SetState (IE_GUI_BUTTON_DISABLED)
577	GenderDoneButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, GenderDonePress)
578	GenderDoneButton.SetText (11973)
579	GenderDoneButton.MakeDefault()
580
581	GenderCancelButton = GenderWindow.GetControl (6)
582	GenderCancelButton.SetState (IE_GUI_BUTTON_ENABLED)
583	GenderCancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, GenderCancelPress)
584	GenderCancelButton.SetText (13727)
585	GenderCancelButton.MakeEscape()
586
587	GenderWindow.ShowModal(MODAL_SHADOW_NONE)
588	return
589
590def MalePress():
591	global GenderWindow, GenderDoneButton, GenderTextArea
592
593	GenderTextArea.SetText (13083)
594	GenderDoneButton.SetState (IE_GUI_BUTTON_ENABLED)
595	return
596
597def FemalePress():
598	global GenderWindow, GenderDoneButton, GenderTextArea
599
600	GenderTextArea.SetText (13084)
601	GenderDoneButton.SetState (IE_GUI_BUTTON_ENABLED)
602	return
603
604def GenderDonePress():
605	global CharGenWindow, GenderWindow
606	global MyChar
607
608	if GenderWindow:
609		GenderWindow.Unload ()
610	Gender = GemRB.GetVar ("Gender")
611	GemRB.SetPlayerStat (MyChar, IE_SEX, Gender)
612
613	PortraitSelect()
614	return
615
616def GenderCancelPress():
617	global CharGenWindow, GenderWindow
618	global MyChar
619
620	GemRB.SetVar ("Gender", 0)
621	GemRB.SetPlayerStat (MyChar, IE_SEX, 0)
622	if GenderWindow:
623		GenderWindow.Unload ()
624	return
625
626def PortraitSelect():
627	global CharGenWindow, PortraitWindow, Portrait, PortraitPortraitButton
628	global MyChar
629
630	PortraitWindow = GemRB.LoadWindow (11)
631
632	# this is not the correct one, but I don't know which is
633	Portrait = 0
634
635	PortraitPortraitButton = PortraitWindow.GetControl (1)
636	PortraitPortraitButton.SetState (IE_GUI_BUTTON_DISABLED)
637	PortraitPortraitButton.SetFlags (IE_GUI_BUTTON_PICTURE|IE_GUI_BUTTON_NO_IMAGE, OP_SET)
638
639	PortraitLeftButton = PortraitWindow.GetControl (2)
640	PortraitLeftButton.SetState (IE_GUI_BUTTON_ENABLED)
641	PortraitLeftButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, CGPortraitLeftPress)
642	PortraitLeftButton.SetFlags (IE_GUI_BUTTON_RADIOBUTTON, OP_OR)
643
644	PortraitRightButton = PortraitWindow.GetControl (3)
645	PortraitRightButton.SetState (IE_GUI_BUTTON_ENABLED)
646	PortraitRightButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, CGPortraitRightPress)
647	PortraitRightButton.SetFlags (IE_GUI_BUTTON_RADIOBUTTON, OP_OR)
648
649	PortraitCustomButton = PortraitWindow.GetControl (6)
650	PortraitCustomButton.SetState (IE_GUI_BUTTON_ENABLED)
651	PortraitCustomButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, PortraitCustomPress)
652	PortraitCustomButton.SetText (17545)
653
654	PortraitDoneButton = PortraitWindow.GetControl (0)
655	PortraitDoneButton.SetState (IE_GUI_BUTTON_ENABLED)
656	PortraitDoneButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, CGPortraitDonePress)
657	PortraitDoneButton.SetText (11973)
658	PortraitDoneButton.MakeDefault()
659
660	PortraitCancelButton = PortraitWindow.GetControl (5)
661	PortraitCancelButton.SetState (IE_GUI_BUTTON_ENABLED)
662	PortraitCancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, CGPortraitCancelPress)
663	PortraitCancelButton.SetText (13727)
664	PortraitCancelButton.MakeEscape()
665
666	while PortraitsTable.GetValue (Portrait, 0) != GemRB.GetPlayerStat (MyChar, IE_SEX):
667		Portrait = Portrait + 1
668	PortraitPortraitButton.SetPicture (PortraitsTable.GetRowName (Portrait) + "G")
669
670	PortraitWindow.ShowModal(MODAL_SHADOW_NONE)
671	return
672
673def CGPortraitLeftPress():
674	global PortraitWindow, Portrait, PortraitPortraitButton
675	global MyChar
676
677	while True:
678		Portrait = Portrait - 1
679		if Portrait < 0:
680			Portrait = PortraitsTable.GetRowCount () - 1
681		if PortraitsTable.GetValue (Portrait, 0) == GemRB.GetPlayerStat(MyChar, IE_SEX):
682			PortraitPortraitButton.SetPicture (PortraitsTable.GetRowName (Portrait) + "G")
683			return
684
685def CGPortraitRightPress():
686	global PortraitWindow, Portrait, PortraitPortraitButton
687	global MyChar
688
689	while True:
690		Portrait = Portrait + 1
691		if Portrait == PortraitsTable.GetRowCount():
692			Portrait = 0
693		if PortraitsTable.GetValue (Portrait, 0) == GemRB.GetPlayerStat(MyChar, IE_SEX):
694			PortraitPortraitButton.SetPicture (PortraitsTable.GetRowName (Portrait) + "G")
695			return
696
697def CustomDone():
698	global CharGenWindow, PortraitWindow
699	global PortraitButton, GenderButton, RaceButton
700	global CharGenState, Portrait
701
702	Window = CustomWindow
703
704	PortraitName = PortraitList2.QueryText ()
705	GemRB.SetToken ("SmallPortrait", PortraitName)
706	PortraitName = PortraitList1.QueryText ()
707	GemRB.SetToken ("LargePortrait", PortraitName)
708	if Window:
709		Window.Unload ()
710
711	if PortraitWindow:
712		PortraitWindow.Unload ()
713	PortraitButton.SetPicture(PortraitName)
714	GenderButton.SetState (IE_GUI_BUTTON_DISABLED)
715	RaceButton.SetState (IE_GUI_BUTTON_ENABLED)
716	RaceButton.MakeDefault()
717	CharGenState = 1
718	Portrait = -1
719	SetCharacterDescription()
720	return
721
722def CustomAbort():
723	if CustomWindow:
724		CustomWindow.Unload ()
725	return
726
727def CGLargeCustomPortrait():
728	Window = CustomWindow
729
730	Portrait = PortraitList1.QueryText ()
731	#small hack
732	if GemRB.GetVar ("Row1") == RowCount1:
733		return
734
735	Label = Window.GetControl (0x10000007)
736	Label.SetText (Portrait)
737
738	Button = Window.GetControl (6)
739	if Portrait=="":
740		Portrait = "NOPORTMD"
741		Button.SetState (IE_GUI_BUTTON_DISABLED)
742	elif PortraitList2.QueryText () != "":
743		Button.SetState (IE_GUI_BUTTON_ENABLED)
744
745	Button = Window.GetControl (0)
746	Button.SetPicture (Portrait, "NOPORTMD")
747	return
748
749def CGSmallCustomPortrait():
750	Window = CustomWindow
751
752	Portrait = PortraitList2.QueryText ()
753	#small hack
754	if GemRB.GetVar ("Row2") == RowCount2:
755		return
756
757	Label = Window.GetControl (0x10000008)
758	Label.SetText (Portrait)
759
760	Button = Window.GetControl (6)
761	if Portrait=="":
762		Portrait = "NOPORTSM"
763		Button.SetState (IE_GUI_BUTTON_DISABLED)
764	elif PortraitList1.QueryText () != "":
765		Button.SetState (IE_GUI_BUTTON_ENABLED)
766
767	Button = Window.GetControl (1)
768	Button.SetPicture (Portrait, "NOPORTSM")
769	return
770
771def PortraitCustomPress():
772	global PortraitList1, PortraitList2
773	global RowCount1, RowCount2
774	global CustomWindow
775
776	CustomWindow = Window = GemRB.LoadWindow (18)
777	PortraitList1 = Window.GetControl (2)
778	RowCount1 = len(PortraitList1.ListResources (CHR_PORTRAITS, 1))
779	PortraitList1.SetEvent (IE_GUI_TEXTAREA_ON_SELECT, CGLargeCustomPortrait)
780	PortraitList1.SetVarAssoc ("Row1",RowCount1)
781
782	PortraitList2 = Window.GetControl (4)
783	RowCount2 = len(PortraitList2.ListResources (CHR_PORTRAITS, 0))
784	PortraitList2.SetEvent (IE_GUI_TEXTAREA_ON_SELECT, CGSmallCustomPortrait)
785	PortraitList2.SetVarAssoc ("Row2",RowCount2)
786
787	Button = Window.GetControl (6)
788	Button.SetText (11973)
789	Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, CustomDone)
790	Button.SetState (IE_GUI_BUTTON_DISABLED)
791
792	Button = Window.GetControl (7)
793	Button.SetText (13727)
794	Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, CustomAbort)
795
796	Button = Window.GetControl (0)
797	PortraitName = PortraitsTable.GetRowName (Portrait)+"L"
798	Button.SetPicture (PortraitName, "NOPORTLG")
799	Button.SetState (IE_GUI_BUTTON_LOCKED)
800
801	Button = Window.GetControl (1)
802	PortraitName = PortraitsTable.GetRowName (Portrait)+"S"
803	Button.SetPicture (PortraitName, "NOPORTSM")
804	Button.SetState (IE_GUI_BUTTON_LOCKED)
805
806	Window.ShowModal (MODAL_SHADOW_NONE)
807	return
808
809def CGPortraitDonePress():
810	global CharGenWindow, PortraitWindow, PortraitButton, GenderButton, RaceButton
811	global CharGenState, Portrait
812
813	PortraitName = PortraitsTable.GetRowName (Portrait )
814	GemRB.SetToken ("SmallPortrait", PortraitName+"S")
815	GemRB.SetToken ("LargePortrait", PortraitName+"L")
816	PortraitButton.SetPicture(PortraitsTable.GetRowName (Portrait) + "L")
817	GenderButton.SetState (IE_GUI_BUTTON_DISABLED)
818	RaceButton.SetState (IE_GUI_BUTTON_ENABLED)
819	RaceButton.MakeDefault()
820	CharGenState = 1
821	SetCharacterDescription()
822	if PortraitWindow:
823		PortraitWindow.Unload ()
824	return
825
826def CGPortraitCancelPress():
827	global CharGenWindow, PortraitWindow
828
829	if PortraitWindow:
830		PortraitWindow.Unload ()
831	return
832
833# Race Selection
834
835def RacePress():
836	global CharGenWindow, RaceWindow, RaceDoneButton, RaceTextArea
837
838	RaceWindow = GemRB.LoadWindow (8)
839	GemRB.SetVar ("Race", 0)
840
841	for i in range (2, 8):
842		RaceSelectButton = RaceWindow.GetControl (i)
843		RaceSelectButton.SetFlags (IE_GUI_BUTTON_RADIOBUTTON, OP_OR)
844
845	for i in range (2, 8):
846		RaceSelectButton = RaceWindow.GetControl (i)
847		RaceSelectButton.SetState (IE_GUI_BUTTON_ENABLED)
848		RaceSelectButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, RaceSelectPress)
849		RaceSelectButton.SetText (CommonTables.Races.GetValue (i - 2, 0))
850		RaceSelectButton.SetVarAssoc ("Race", i - 1)
851
852	RaceTextArea = RaceWindow.GetControl (8)
853	RaceTextArea.SetText (17237)
854
855	RaceDoneButton = RaceWindow.GetControl (0)
856	RaceDoneButton.SetState (IE_GUI_BUTTON_DISABLED)
857	RaceDoneButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, RaceDonePress)
858	RaceDoneButton.SetText (11973)
859	RaceDoneButton.MakeDefault()
860
861	RaceCancelButton = RaceWindow.GetControl (10)
862	RaceCancelButton.SetState (IE_GUI_BUTTON_ENABLED)
863	RaceCancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, RaceCancelPress)
864	RaceCancelButton.SetText (13727)
865	RaceCancelButton.MakeEscape()
866
867	RaceWindow.ShowModal(MODAL_SHADOW_NONE)
868	return
869
870def RaceSelectPress():
871	global RaceWindow, RaceDoneButton, RaceTextArea
872
873	Race = GemRB.GetVar ("Race") - 1
874	RaceTextArea.SetText (CommonTables.Races.GetValue (Race, 1) )
875	RaceDoneButton.SetState (IE_GUI_BUTTON_ENABLED)
876	return
877
878def RaceDonePress():
879	global CharGenWindow, CharGenState, RaceWindow, RaceButton, ClassButton
880
881	if RaceWindow:
882		RaceWindow.Unload ()
883	RaceButton.SetState (IE_GUI_BUTTON_DISABLED)
884	ClassButton.SetState (IE_GUI_BUTTON_ENABLED)
885	ClassButton.MakeDefault()
886	CharGenState = 2
887
888	Race = GemRB.GetVar ("Race")-1
889	Race = CommonTables.Races.GetValue (Race, 3)
890	GemRB.SetPlayerStat (MyChar, IE_RACE, Race)
891	SetCharacterDescription()
892	return
893
894def RaceCancelPress():
895	global CharGenWindow, RaceWindow
896
897	if RaceWindow:
898		RaceWindow.Unload ()
899	return
900
901# Class Selection
902
903def ClassPress():
904	global CharGenWindow, ClassWindow, ClassTextArea, ClassDoneButton
905
906	ClassWindow = GemRB.LoadWindow (2)
907	ClassCount = CommonTables.Classes.GetRowCount ()
908	RaceRow = CommonTables.Races.FindValue (3, GemRB.GetPlayerStat (MyChar, IE_RACE) )
909	RaceName = CommonTables.Races.GetRowName (RaceRow)
910	GemRB.SetVar ("Class", 0)
911	GemRB.SetVar ("Class Kit", 0)
912	GemRB.SetVar ("MAGESCHOOL", 0)
913
914	for i in range (2, 10):
915		ClassSelectButton = ClassWindow.GetControl (i)
916		ClassSelectButton.SetFlags (IE_GUI_BUTTON_RADIOBUTTON, OP_SET)
917
918	HasMulti = 0
919	j = 2
920	for i in range (ClassCount):
921		ClassRowName = CommonTables.Classes.GetRowName (i)
922		Allowed = CommonTables.Classes.GetValue (ClassRowName, RaceName)
923		if CommonTables.Classes.GetValue (ClassRowName, "MULTI"):
924			if Allowed != 0:
925				HasMulti = 1
926		else:
927			ClassSelectButton = ClassWindow.GetControl (j)
928			j = j + 1
929			if Allowed > 0:
930				ClassSelectButton.SetState (IE_GUI_BUTTON_ENABLED)
931			else:
932				ClassSelectButton.SetState (IE_GUI_BUTTON_DISABLED)
933			ClassSelectButton.SetEvent (IE_GUI_BUTTON_ON_PRESS,  ClassSelectPress)
934			ClassSelectButton.SetText (CommonTables.Classes.GetValue (ClassRowName, "NAME_REF"))
935			ClassSelectButton.SetVarAssoc ("Class", i + 1)
936
937	ClassMultiButton = ClassWindow.GetControl (10)
938	if HasMulti == 0:
939		ClassMultiButton.SetState (IE_GUI_BUTTON_DISABLED)
940	else:
941		ClassMultiButton.SetState (IE_GUI_BUTTON_ENABLED)
942	ClassMultiButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, ClassMultiPress)
943	ClassMultiButton.SetText (11993)
944
945	KitButton = ClassWindow.GetControl (11)
946	#only the mage class has schools
947	Allowed = CommonTables.Classes.GetValue ("MAGE", RaceName)
948	if Allowed:
949		KitButton.SetState (IE_GUI_BUTTON_ENABLED)
950	else:
951		KitButton.SetState (IE_GUI_BUTTON_DISABLED)
952	KitButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, KitPress)
953	KitButton.SetText (11994)
954
955	ClassTextArea = ClassWindow.GetControl (13)
956	ClassTextArea.SetText (17242)
957
958	ClassDoneButton = ClassWindow.GetControl (0)
959	ClassDoneButton.SetState (IE_GUI_BUTTON_DISABLED)
960	ClassDoneButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, ClassDonePress)
961	ClassDoneButton.SetText (11973)
962	ClassDoneButton.MakeDefault()
963
964	ClassCancelButton = ClassWindow.GetControl (14)
965	ClassCancelButton.SetState (IE_GUI_BUTTON_ENABLED)
966	ClassCancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, ClassCancelPress)
967	ClassCancelButton.SetText (13727)
968	ClassCancelButton.MakeEscape()
969
970	ClassWindow.ShowModal(MODAL_SHADOW_NONE)
971	return
972
973def ClassSelectPress():
974	global ClassWindow, ClassTextArea, ClassDoneButton
975
976	ClassName = GUICommon.GetClassRowName (GemRB.GetVar ("Class")-1, "index")
977	ClassTextArea.SetText (CommonTables.Classes.GetValue (ClassName, "DESC_REF"))
978	ClassDoneButton.SetState (IE_GUI_BUTTON_ENABLED)
979	return
980
981def ClassMultiPress():
982	global ClassWindow, ClassMultiWindow, ClassMultiTextArea, ClassMultiDoneButton
983
984	ClassWindow.SetVisible(False)
985	ClassMultiWindow = GemRB.LoadWindow (10)
986	ClassCount = CommonTables.Classes.GetRowCount ()
987	RaceRow = CommonTables.Races.FindValue (3, GemRB.GetPlayerStat (MyChar, IE_RACE) )
988	RaceName = CommonTables.Races.GetRowName (RaceRow)
989
990	for i in range (2, 10):
991		ClassMultiSelectButton = ClassMultiWindow.GetControl (i)
992		ClassMultiSelectButton.SetFlags (IE_GUI_BUTTON_RADIOBUTTON, OP_SET)
993
994	j = 2
995	for i in range (ClassCount):
996		ClassName = CommonTables.Classes.GetRowName (i)
997		if CommonTables.Classes.GetValue (ClassName, "MULTI") > 0:
998			ClassMultiSelectButton = ClassMultiWindow.GetControl (j)
999			j = j + 1
1000			if CommonTables.Classes.GetValue (ClassName, RaceName) > 0:
1001				ClassMultiSelectButton.SetState (IE_GUI_BUTTON_ENABLED)
1002			else:
1003				ClassMultiSelectButton.SetState (IE_GUI_BUTTON_DISABLED)
1004			ClassMultiSelectButton.SetEvent (IE_GUI_BUTTON_ON_PRESS,  ClassMultiSelectPress)
1005			ClassMultiSelectButton.SetText (CommonTables.Classes.GetValue (ClassName, "NAME_REF"))
1006			ClassMultiSelectButton.SetVarAssoc ("Class", i + 1)
1007
1008	ClassMultiTextArea = ClassMultiWindow.GetControl (12)
1009	ClassMultiTextArea.SetText (17244)
1010
1011	ClassMultiDoneButton = ClassMultiWindow.GetControl (0)
1012	ClassMultiDoneButton.SetState (IE_GUI_BUTTON_DISABLED)
1013	ClassMultiDoneButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, ClassMultiDonePress)
1014	ClassMultiDoneButton.SetText (11973)
1015	ClassMultiDoneButton.MakeDefault()
1016
1017	ClassMultiCancelButton = ClassMultiWindow.GetControl (14)
1018	ClassMultiCancelButton.SetState (IE_GUI_BUTTON_ENABLED)
1019	ClassMultiCancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, ClassMultiCancelPress)
1020	ClassMultiCancelButton.SetText (13727)
1021	ClassMultiCancelButton.MakeEscape()
1022
1023	ClassMultiWindow.ShowModal(MODAL_SHADOW_NONE)
1024	return
1025
1026def ClassMultiSelectPress():
1027	global ClassMultiWindow, ClassMultiTextArea, ClassMultiDoneButton
1028
1029	ClassName = GUICommon.GetClassRowName (GemRB.GetVar ("Class")-1, "index")
1030	ClassMultiTextArea.SetText (CommonTables.Classes.GetValue (ClassName, "DESC_REF"))
1031	ClassMultiDoneButton.SetState (IE_GUI_BUTTON_ENABLED)
1032	return
1033
1034def ClassMultiDonePress():
1035	global ClassMultiWindow
1036
1037	if ClassMultiWindow:
1038		ClassMultiWindow.Unload ()
1039	ClassDonePress()
1040	return
1041
1042def ClassMultiCancelPress():
1043	global ClassWindow, ClassMultiWindow
1044
1045	if ClassMultiWindow:
1046		ClassMultiWindow.Unload ()
1047	ClassWindow.ShowModal(MODAL_SHADOW_NONE)
1048	return
1049
1050def KitPress():
1051	global ClassWindow, KitWindow, KitTextArea, KitDoneButton
1052
1053	ClassWindow.SetVisible(False)
1054	KitWindow = GemRB.LoadWindow (12)
1055
1056	#only mage class (1) has schools. It is the sixth button
1057	GemRB.SetVar ("Class", 6)
1058	GemRB.SetVar ("Class Kit",0)
1059	GemRB.SetVar ("MAGESCHOOL",0)
1060
1061	for i in range (8):
1062		Button = KitWindow.GetControl (i+2)
1063		Button.SetFlags (IE_GUI_BUTTON_RADIOBUTTON, OP_OR)
1064		Button.SetText (KitTable.GetValue (i+1, 0) )
1065		Button.SetVarAssoc ("MAGESCHOOL", i+1)
1066		Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, KitSelectPress)
1067
1068	KitTextArea = KitWindow.GetControl (11)
1069	KitTextArea.SetText (17245)
1070
1071	KitDoneButton = KitWindow.GetControl (0)
1072	KitDoneButton.SetState (IE_GUI_BUTTON_DISABLED)
1073	KitDoneButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, KitDonePress)
1074	KitDoneButton.SetText (11973)
1075	KitDoneButton.MakeDefault()
1076
1077	KitCancelButton = KitWindow.GetControl (12)
1078	KitCancelButton.SetState (IE_GUI_BUTTON_ENABLED)
1079	KitCancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, KitCancelPress)
1080	KitCancelButton.SetText (13727)
1081	KitCancelButton.MakeEscape()
1082
1083	KitWindow.ShowModal(MODAL_SHADOW_NONE)
1084	return
1085
1086def KitSelectPress():
1087	global KitWindow, KitTextArea
1088
1089	Kit = GemRB.GetVar ("MAGESCHOOL")
1090	KitTextArea.SetText (KitTable.GetValue (Kit, 1))
1091	KitDoneButton.SetState (IE_GUI_BUTTON_ENABLED)
1092	return
1093
1094def KitDonePress():
1095	global KitWindow
1096
1097	if KitWindow:
1098		KitWindow.Unload ()
1099	ClassDonePress()
1100	return
1101
1102def KitCancelPress():
1103	global ClassWindow, KitWindow
1104
1105	if KitWindow:
1106		KitWindow.Unload ()
1107	ClassWindow.ShowModal(MODAL_SHADOW_NONE)
1108	return
1109
1110def ClassDonePress():
1111	global CharGenWindow, CharGenState, ClassWindow, ClassButton, AlignmentButton
1112	global MyChar
1113
1114	if ClassWindow:
1115		ClassWindow.Unload ()
1116	ClassButton.SetState (IE_GUI_BUTTON_DISABLED)
1117	AlignmentButton.SetState (IE_GUI_BUTTON_ENABLED)
1118	AlignmentButton.MakeDefault()
1119
1120	ClassName = GUICommon.GetClassRowName (GemRB.GetVar ("Class")-1, "index")
1121	Class = CommonTables.Classes.GetValue (ClassName, "ID")
1122	GemRB.SetPlayerStat (MyChar, IE_CLASS, Class)
1123
1124	Kit = KitTable.GetValue (GemRB.GetVar ("MAGESCHOOL"), 3 )
1125	if (Kit == -1 ):
1126		Kit = 0x4000
1127
1128	GemRB.SetPlayerStat (MyChar, IE_KIT, Kit)
1129
1130	CharGenState = 3
1131	SetCharacterDescription()
1132	return
1133
1134def ClassCancelPress():
1135	global CharGenWindow, ClassWindow
1136
1137	if ClassWindow:
1138		ClassWindow.Unload ()
1139	return
1140
1141# Alignment Selection
1142
1143def AlignmentPress():
1144	global CharGenWindow, AlignmentWindow, AlignmentTextArea, AlignmentDoneButton
1145
1146	AlignmentWindow = GemRB.LoadWindow (3)
1147	ClassAlignmentTable = GemRB.LoadTable ("alignmnt")
1148	ClassName = GUICommon.GetClassRowName (MyChar)
1149	GemRB.SetVar ("Alignment", 0)
1150
1151	for i in range (9):
1152		AlignmentSelectButton = AlignmentWindow.GetControl (i + 2)
1153		AlignmentSelectButton.SetFlags (IE_GUI_BUTTON_RADIOBUTTON, OP_OR)
1154		if ClassAlignmentTable.GetValue (ClassName, CommonTables.Aligns.GetValue(i, 4)) == 0:
1155			AlignmentSelectButton.SetState (IE_GUI_BUTTON_DISABLED)
1156		else:
1157			AlignmentSelectButton.SetState (IE_GUI_BUTTON_ENABLED)
1158		AlignmentSelectButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AlignmentSelectPress)
1159		AlignmentSelectButton.SetText (CommonTables.Aligns.GetValue (i, 0))
1160		AlignmentSelectButton.SetVarAssoc ("Alignment", i + 1)
1161
1162	AlignmentTextArea = AlignmentWindow.GetControl (11)
1163	AlignmentTextArea.SetText (9602)
1164
1165	AlignmentDoneButton = AlignmentWindow.GetControl (0)
1166	AlignmentDoneButton.SetState (IE_GUI_BUTTON_DISABLED)
1167	AlignmentDoneButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AlignmentDonePress)
1168	AlignmentDoneButton.SetText (11973)
1169	AlignmentDoneButton.MakeDefault()
1170
1171	AlignmentCancelButton = AlignmentWindow.GetControl (13)
1172	AlignmentCancelButton.SetState (IE_GUI_BUTTON_ENABLED)
1173	AlignmentCancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AlignmentCancelPress)
1174	AlignmentCancelButton.SetText (13727)
1175	AlignmentCancelButton.MakeEscape()
1176
1177	AlignmentWindow.ShowModal(MODAL_SHADOW_NONE)
1178	return
1179
1180def AlignmentSelectPress():
1181	global AlignmentWindow, AlignmentTextArea, AlignmentDoneButton
1182
1183	Alignment = GemRB.GetVar ("Alignment") - 1
1184	AlignmentTextArea.SetText (CommonTables.Aligns.GetValue (Alignment, 1))
1185	AlignmentDoneButton.SetState (IE_GUI_BUTTON_ENABLED)
1186	return
1187
1188def AlignmentDonePress():
1189	global CharGenWindow, CharGenState, AlignmentWindow, AlignmentButton, AbilitiesButton
1190	global MyChar
1191
1192	if AlignmentWindow:
1193		AlignmentWindow.Unload ()
1194	AlignmentButton.SetState (IE_GUI_BUTTON_DISABLED)
1195	AbilitiesButton.SetState (IE_GUI_BUTTON_ENABLED)
1196	AbilitiesButton.MakeDefault()
1197
1198	Alignment = CommonTables.Aligns.GetValue (GemRB.GetVar ("Alignment")-1, 3)
1199	GemRB.SetPlayerStat (MyChar, IE_ALIGNMENT, Alignment )
1200
1201	CharGenState = 4
1202	SetCharacterDescription()
1203	return
1204
1205def AlignmentCancelPress():
1206	global CharGenWindow, AlignmentWindow
1207
1208	if AlignmentWindow:
1209		AlignmentWindow.Unload ()
1210	return
1211
1212# Abilities Selection
1213
1214def AbilitiesPress():
1215	global CharGenWindow, AbilitiesWindow
1216	global AbilitiesTextArea, AbilitiesRecallButton, AbilitiesDoneButton
1217	global AbilitiesRaceAddTable, AbilitiesRaceReqTable, AbilitiesClassReqTable
1218	global HasStrExtra
1219
1220	AbilitiesWindow = GemRB.LoadWindow (4)
1221	AbilitiesRaceAddTable = GemRB.LoadTable ("ABRACEAD")
1222	AbilitiesRaceReqTable = GemRB.LoadTable ("ABRACERQ")
1223	AbilitiesClassReqTable = GemRB.LoadTable ("ABCLASRQ")
1224
1225	ClassName = GUICommon.GetClassRowName (MyChar)
1226	HasStrExtra = CommonTables.Classes.GetValue (ClassName, "STREXTRA", GTV_INT)
1227
1228	for i in range (6):
1229		AbilitiesLabelButton = AbilitiesWindow.GetControl (30 + i)
1230		AbilitiesLabelButton.SetState (IE_GUI_BUTTON_ENABLED)
1231		AbilitiesLabelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AbilitiesLabelPress)
1232		AbilitiesLabelButton.SetVarAssoc ("AbilityIndex", i + 1)
1233
1234		AbilitiesPlusButton = AbilitiesWindow.GetControl (16 + i * 2)
1235		AbilitiesPlusButton.SetState (IE_GUI_BUTTON_ENABLED)
1236		AbilitiesPlusButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AbilitiesPlusPress)
1237		AbilitiesPlusButton.SetVarAssoc ("AbilityIndex", i + 1)
1238		AbilitiesPlusButton.SetActionInterval (200)
1239
1240		AbilitiesMinusButton = AbilitiesWindow.GetControl (17 + i * 2)
1241		AbilitiesMinusButton.SetState (IE_GUI_BUTTON_ENABLED)
1242		AbilitiesMinusButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AbilitiesMinusPress)
1243		AbilitiesMinusButton.SetVarAssoc ("AbilityIndex", i + 1)
1244		AbilitiesMinusButton.SetActionInterval (200)
1245
1246	AbilitiesStoreButton = AbilitiesWindow.GetControl (37)
1247	AbilitiesStoreButton.SetState (IE_GUI_BUTTON_ENABLED)
1248	AbilitiesStoreButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AbilitiesStorePress)
1249	AbilitiesStoreButton.SetText (17373)
1250
1251	AbilitiesRecallButton = AbilitiesWindow.GetControl (38)
1252	AbilitiesRecallButton.SetState (IE_GUI_BUTTON_DISABLED)
1253	AbilitiesRecallButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AbilitiesRecallPress)
1254	AbilitiesRecallButton.SetText (17374)
1255
1256	AbilitiesRerollButton = AbilitiesWindow.GetControl (2)
1257	AbilitiesRerollButton.SetState (IE_GUI_BUTTON_ENABLED)
1258	AbilitiesRerollButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AbilitiesRerollPress)
1259	AbilitiesRerollButton.SetText (11982)
1260
1261	AbilitiesTextArea = AbilitiesWindow.GetControl (29)
1262	AbilitiesTextArea.SetText (17247)
1263
1264	AbilitiesDoneButton = AbilitiesWindow.GetControl (0)
1265	AbilitiesDoneButton.SetState (IE_GUI_BUTTON_ENABLED)
1266	AbilitiesDoneButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AbilitiesDonePress)
1267	AbilitiesDoneButton.SetText (11973)
1268	AbilitiesDoneButton.MakeDefault()
1269
1270	AbilitiesCancelButton = AbilitiesWindow.GetControl (36)
1271	AbilitiesCancelButton.SetState (IE_GUI_BUTTON_ENABLED)
1272	AbilitiesCancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AbilitiesCancelPress)
1273	AbilitiesCancelButton.SetText (13727)
1274	AbilitiesCancelButton.MakeEscape()
1275
1276	AbilitiesRerollPress()
1277
1278	AbilitiesWindow.ShowModal(MODAL_SHADOW_NONE)
1279	return
1280
1281def AbilitiesCalcLimits(Index):
1282	global AbilitiesRaceReqTable, AbilitiesRaceAddTable, AbilitiesClassReqTable
1283	global AbilitiesMinimum, AbilitiesMaximum, AbilitiesModifier
1284
1285	Race = CommonTables.Races.FindValue (3, GemRB.GetPlayerStat (MyChar, IE_RACE))
1286	RaceName = CommonTables.Races.GetRowName (Race)
1287	Race = AbilitiesRaceReqTable.GetRowIndex (RaceName)
1288	AbilitiesMinimum = AbilitiesRaceReqTable.GetValue (Race, Index * 2)
1289	AbilitiesMaximum = AbilitiesRaceReqTable.GetValue (Race, Index * 2 + 1)
1290	AbilitiesModifier = AbilitiesRaceAddTable.GetValue (Race, Index)
1291
1292	ClassName = GUICommon.GetClassRowName (MyChar)
1293	ClassIndex = AbilitiesClassReqTable.GetRowIndex (ClassName)
1294	Min = AbilitiesClassReqTable.GetValue (ClassIndex, Index)
1295	if Min > 0 and AbilitiesMinimum < Min:
1296		AbilitiesMinimum = Min
1297
1298	AbilitiesMinimum = AbilitiesMinimum + AbilitiesModifier
1299	AbilitiesMaximum = AbilitiesMaximum + AbilitiesModifier
1300	return
1301
1302def AbilitiesLabelPress():
1303	global AbilitiesWindow, AbilitiesTextArea
1304
1305	AbilityIndex = GemRB.GetVar ("AbilityIndex") - 1
1306	AbilitiesCalcLimits(AbilityIndex)
1307	GemRB.SetToken ("MINIMUM", str(AbilitiesMinimum) )
1308	GemRB.SetToken ("MAXIMUM", str(AbilitiesMaximum) )
1309	AbilitiesTextArea.SetText (AbilitiesTable.GetValue (AbilityIndex, 1) )
1310	return
1311
1312def AbilitiesPlusPress():
1313	global AbilitiesWindow, AbilitiesTextArea
1314	global AbilitiesMinimum, AbilitiesMaximum
1315
1316	Abidx = GemRB.GetVar ("AbilityIndex") - 1
1317	AbilitiesCalcLimits(Abidx)
1318	GemRB.SetToken ("MINIMUM", str(AbilitiesMinimum) )
1319	GemRB.SetToken ("MAXIMUM", str(AbilitiesMaximum) )
1320	AbilitiesTextArea.SetText (AbilitiesTable.GetValue (Abidx, 1) )
1321	PointsLeft = GemRB.GetVar ("Ability0")
1322	Ability = GemRB.GetVar ("Ability" + str(Abidx + 1) )
1323	if PointsLeft > 0 and Ability < AbilitiesMaximum:
1324		PointsLeft = PointsLeft - 1
1325		GemRB.SetVar ("Ability0", PointsLeft)
1326		PointsLeftLabel = AbilitiesWindow.GetControl (0x10000002)
1327		PointsLeftLabel.SetText (str(PointsLeft) )
1328		Ability = Ability + 1
1329		GemRB.SetVar ("Ability" + str(Abidx + 1), Ability)
1330		Label = AbilitiesWindow.GetControl (0x10000003 + Abidx)
1331		StrExtra = GemRB.GetVar("StrExtra")
1332		if Abidx==0 and Ability==18 and HasStrExtra:
1333			Label.SetText("18/"+str(StrExtra) )
1334		else:
1335			Label.SetText(str(Ability) )
1336
1337		if PointsLeft == 0:
1338			AbilitiesDoneButton.SetState (IE_GUI_BUTTON_ENABLED)
1339	return
1340
1341def AbilitiesMinusPress():
1342	global AbilitiesWindow, AbilitiesTextArea
1343	global AbilitiesMinimum, AbilitiesMaximum
1344
1345	Abidx = GemRB.GetVar ("AbilityIndex") - 1
1346	AbilitiesCalcLimits(Abidx)
1347	GemRB.SetToken ("MINIMUM", str(AbilitiesMinimum) )
1348	GemRB.SetToken ("MAXIMUM", str(AbilitiesMaximum) )
1349	AbilitiesTextArea.SetText (AbilitiesTable.GetValue (Abidx, 1) )
1350	PointsLeft = GemRB.GetVar ("Ability0")
1351	Ability = GemRB.GetVar ("Ability" + str(Abidx + 1) )
1352	if Ability > AbilitiesMinimum:
1353		Ability = Ability - 1
1354		GemRB.SetVar ("Ability" + str(Abidx + 1), Ability)
1355		Label = AbilitiesWindow.GetControl (0x10000003 + Abidx)
1356		StrExtra = GemRB.GetVar("StrExtra")
1357		if Abidx==0 and Ability==18 and HasStrExtra:
1358			Label.SetText("18/"+str(StrExtra) )
1359		else:
1360			Label.SetText(str(Ability) )
1361
1362		PointsLeft = PointsLeft + 1
1363		GemRB.SetVar ("Ability0", PointsLeft)
1364		PointsLeftLabel = AbilitiesWindow.GetControl (0x10000002)
1365		PointsLeftLabel.SetText (str(PointsLeft) )
1366		AbilitiesDoneButton.SetState (IE_GUI_BUTTON_DISABLED)
1367	return
1368
1369def AbilitiesStorePress():
1370	global AbilitiesWindow, AbilitiesRecallButton
1371
1372	GemRB.SetVar("StoredStrExtra", GemRB.GetVar ("StrExtra") )
1373	for i in range (7):
1374		GemRB.SetVar ("Stored" + str(i), GemRB.GetVar ("Ability" + str(i)))
1375	AbilitiesRecallButton.SetState (IE_GUI_BUTTON_ENABLED)
1376	return
1377
1378def AbilitiesRecallPress():
1379	global AbilitiesWindow
1380
1381	e=GemRB.GetVar("StoredStrExtra")
1382	GemRB.SetVar("StrExtra",e)
1383	for i in range (7):
1384		v =  GemRB.GetVar ("Stored" + str(i))
1385		GemRB.SetVar ("Ability" + str(i), v)
1386		Label = AbilitiesWindow.GetControl (0x10000002 + i)
1387		if i==0 and v==18 and HasStrExtra==1:
1388			Label.SetText("18/"+str(e) )
1389		else:
1390			Label.SetText(str(v) )
1391
1392	PointsLeft = GemRB.GetVar("Ability0")
1393	if PointsLeft == 0:
1394		AbilitiesDoneButton.SetState(IE_GUI_BUTTON_ENABLED)
1395	else:
1396		AbilitiesDoneButton.SetState(IE_GUI_BUTTON_DISABLED)
1397	return
1398
1399def AbilitiesRerollPress():
1400	global AbilitiesWindow, AbilitiesMinimum, AbilitiesMaximum, AbilitiesModifier
1401
1402	GemRB.SetVar ("Ability0", 0)
1403	PointsLeftLabel = AbilitiesWindow.GetControl (0x10000002)
1404	PointsLeftLabel.SetText ("0")
1405	Dices = 3
1406	Sides = 5
1407
1408	#roll strextra even when the current stat is not 18
1409	if HasStrExtra:
1410		e = GemRB.Roll (1,100,0)
1411	else:
1412		e = 0
1413	GemRB.SetVar("StrExtra", e)
1414	for i in range (6):
1415		AbilitiesCalcLimits(i)
1416		Value = GemRB.Roll (Dices, Sides, AbilitiesModifier+3)
1417		if Value < AbilitiesMinimum:
1418			Value = AbilitiesMinimum
1419		if Value > AbilitiesMaximum:
1420			Value = AbilitiesMaximum
1421		GemRB.SetVar ("Ability" + str(i + 1), Value)
1422		Label = AbilitiesWindow.GetControl (0x10000003 + i)
1423
1424		if i==0 and HasStrExtra and Value==18:
1425			Label.SetText("18/"+str(e) )
1426		else:
1427			Label.SetText(str(Value) )
1428
1429	AbilitiesDoneButton.SetState(IE_GUI_BUTTON_ENABLED)
1430	return
1431
1432def AbilitiesDonePress():
1433	global CharGenWindow, CharGenState, AbilitiesWindow, AbilitiesButton, SkillsButton, SkillsState
1434
1435	if AbilitiesWindow:
1436		AbilitiesWindow.Unload ()
1437	AbilitiesButton.SetState (IE_GUI_BUTTON_DISABLED)
1438	SkillsButton.SetState (IE_GUI_BUTTON_ENABLED)
1439	SkillsButton.MakeDefault()
1440
1441	Str = GemRB.GetVar ("Ability1")
1442	GemRB.SetPlayerStat (MyChar, IE_STR, Str)
1443	if Str == 18:
1444		GemRB.SetPlayerStat (MyChar, IE_STREXTRA, GemRB.GetVar ("StrExtra"))
1445	else:
1446		GemRB.SetPlayerStat (MyChar, IE_STREXTRA, 0)
1447
1448	GemRB.SetPlayerStat (MyChar, IE_DEX, GemRB.GetVar ("Ability2"))
1449	GemRB.SetPlayerStat (MyChar, IE_CON, GemRB.GetVar ("Ability3"))
1450	GemRB.SetPlayerStat (MyChar, IE_INT, GemRB.GetVar ("Ability4"))
1451	GemRB.SetPlayerStat (MyChar, IE_WIS, GemRB.GetVar ("Ability5"))
1452	GemRB.SetPlayerStat (MyChar, IE_CHR, GemRB.GetVar ("Ability6"))
1453
1454	CharGenState = 5
1455	SkillsState = 0
1456	SetCharacterDescription()
1457	return
1458
1459def AbilitiesCancelPress():
1460	global CharGenWindow, AbilitiesWindow
1461
1462	if AbilitiesWindow:
1463		AbilitiesWindow.Unload ()
1464	return
1465
1466# Skills Selection
1467
1468def SkillsPress():
1469	global CharGenWindow, AppearanceButton
1470	global SkillsState, SkillsButton, CharGenState, ClassFlag
1471
1472	Level = 1
1473	SpellLevel = 1
1474	ClassName = GUICommon.GetClassRowName (MyChar)
1475	DruidSpell = CommonTables.ClassSkills.GetValue (ClassName, "DRUIDSPELL")
1476	PriestSpell = CommonTables.ClassSkills.GetValue (ClassName, "CLERICSPELL")
1477	MageSpell = CommonTables.ClassSkills.GetValue (ClassName, "MAGESPELL")
1478	IsBard = CommonTables.ClassSkills.GetValue (ClassName, "BARDSKILL")
1479	IsThief = CommonTables.ClassSkills.GetValue (ClassName, "THIEFSKILL")
1480
1481	if SkillsState == 0:
1482		LUSkillsSelection.SkillsNullify (MyChar)
1483		HateRace = CommonTables.ClassSkills.GetValue(ClassName, "HATERACE")
1484		if DruidSpell != "*" and HateRace != "*":
1485			Skill = GemRB.LoadTable("SKILLRNG").GetValue(str(Level), "STEALTH")
1486			GemRB.SetPlayerStat (MyChar, IE_STEALTH, Skill)
1487		elif IsBard != "*":
1488			Skill = GemRB.LoadTable(IsBard).GetValue(str(Level), "PICK_POCKETS")
1489			GemRB.SetPlayerStat (MyChar, IE_PICKPOCKET, Skill)
1490
1491		GemRB.SetVar ("HatedRace", 0)
1492		GemRB.SetVar ("RacialEnemy", 0)
1493		GemRB.SetVar ("RacialEnemyIndex", 0)
1494		if HateRace != "*":
1495			RacialEnemySelect()
1496		elif IsThief != "*":
1497			SkillsSelect()
1498		else:
1499			SkillsState = 1
1500
1501	if SkillsState == 1:
1502		ProficienciesSelect()
1503
1504	if SkillsState == 2:
1505		if MageSpell!="*":
1506			MageSpellsSelect(MageSpell, Level, SpellLevel)
1507		else:
1508			SkillsState = 3
1509
1510	if SkillsState == 3:
1511		if MageSpell!="*":
1512			MageSpellsMemorize(MageSpell, Level, SpellLevel)
1513		else:
1514			SkillsState = 4
1515
1516	if SkillsState == 4:
1517		if PriestSpell == "MXSPLPRS":
1518			ClassFlag = 0x4000
1519			PriestSpellsMemorize(PriestSpell, Level, SpellLevel)
1520		elif DruidSpell == "MXSPLDRU":
1521			# no separate spell progression by default
1522			DruidSpell = Spellbook.GetPriestSpellTable (DruidSpell)
1523			ClassFlag = 0x8000
1524			PriestSpellsMemorize(DruidSpell, Level, SpellLevel)
1525		else:
1526			SkillsState = 5
1527
1528	if SkillsState == 5:
1529		SkillsButton.SetState (IE_GUI_BUTTON_DISABLED)
1530		AppearanceButton.SetState (IE_GUI_BUTTON_ENABLED)
1531		AppearanceButton.MakeDefault()
1532
1533		Race = GemRB.GetVar ("HatedRace")
1534		GemRB.SetPlayerStat (MyChar, IE_HATEDRACE, Race)
1535
1536		ProfCount = ProficienciesTable.GetRowCount ()
1537		for i in range(ProfCount):
1538			StatID = ProficienciesTable.GetValue (i, 0)
1539			Value = GemRB.GetVar ("Proficiency"+str(i) )
1540			GemRB.SetPlayerStat (MyChar, StatID, Value )
1541
1542		CharGenState = 6
1543		SetCharacterDescription()
1544	return
1545
1546def SkillsSelect():
1547	global CharGenWindow, SkillsWindow, SkillsTextArea, SkillsDoneButton, SkillsPointsLeft
1548
1549	SkillsWindow = GemRB.LoadWindow (6)
1550
1551	Levels = [GemRB.GetPlayerStat (MyChar, IE_LEVEL),
1552		GemRB.GetPlayerStat (MyChar, IE_LEVEL2),
1553		GemRB.GetPlayerStat (MyChar, IE_LEVEL3)]
1554
1555	LUSkillsSelection.SetupSkillsWindow (MyChar,
1556		LUSkillsSelection.LUSKILLS_TYPE_CHARGEN, SkillsWindow, RedrawSkills, [0,0,0], Levels, 0, False)
1557
1558	SkillsPointsLeft = GemRB.GetVar ("SkillPointsLeft")
1559	if SkillsPointsLeft<=0:
1560		SkillsDonePress()
1561		return
1562
1563	SkillsDoneButton = SkillsWindow.GetControl (0)
1564	SkillsDoneButton.SetState (IE_GUI_BUTTON_DISABLED)
1565	SkillsDoneButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, SkillsDonePress)
1566	SkillsDoneButton.SetText (11973)
1567	SkillsDoneButton.MakeDefault()
1568
1569	SkillsCancelButton = SkillsWindow.GetControl (25)
1570	SkillsCancelButton.SetState (IE_GUI_BUTTON_ENABLED)
1571	SkillsCancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, SkillsCancelPress)
1572	SkillsCancelButton.SetText (13727)
1573	SkillsCancelButton.MakeEscape()
1574
1575	RedrawSkills()
1576	SkillsWindow.ShowModal(MODAL_SHADOW_NONE)
1577	return
1578
1579def RedrawSkills():
1580	PointsLeft = GemRB.GetVar ("SkillPointsLeft")
1581	if PointsLeft == 0:
1582		SkillsDoneButton.SetState(IE_GUI_BUTTON_ENABLED)
1583	else:
1584		SkillsDoneButton.SetState(IE_GUI_BUTTON_DISABLED)
1585	return
1586
1587def SkillsDonePress():
1588	global CharGenWindow, SkillsWindow, SkillsState
1589
1590	# save all the skills
1591	LUSkillsSelection.SkillsSave (MyChar)
1592
1593	if SkillsWindow:
1594		SkillsWindow.Unload ()
1595	SkillsState = 1
1596	SkillsPress()
1597	return
1598
1599def SkillsCancelPress():
1600	global CharGenWindow, SkillsWindow, SkillsState
1601
1602	if SkillsWindow:
1603		SkillsWindow.Unload ()
1604	SkillsState = 0
1605	return
1606
1607# Racial Enemy Selection
1608
1609def RacialEnemySelect():
1610	global CharGenWindow, RacialEnemyWindow, RacialEnemyTextArea, RacialEnemyDoneButton
1611
1612	RacialEnemyWindow = GemRB.LoadWindow (15)
1613	RacialEnemyCount = RacialEnemyTable.GetRowCount ()
1614
1615	for i in range (2, 8):
1616		RacialEnemySelectButton = RacialEnemyWindow.GetControl (i)
1617		RacialEnemySelectButton.SetFlags (IE_GUI_BUTTON_RADIOBUTTON, OP_OR)
1618
1619	for i in range (2, 8):
1620		RacialEnemySelectButton = RacialEnemyWindow.GetControl (i)
1621		RacialEnemySelectButton.SetState (IE_GUI_BUTTON_ENABLED)
1622		RacialEnemySelectButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, RacialEnemySelectPress)
1623		RacialEnemySelectButton.SetVarAssoc ("RacialEnemy", i - 1)
1624
1625	GemRB.SetVar ("RacialEnemyIndex", 0)
1626	GemRB.SetVar ("HatedRace", 0)
1627	RacialEnemyScrollBar = RacialEnemyWindow.GetControl (1)
1628	RacialEnemyScrollBar.SetVarAssoc ("RacialEnemyIndex", RacialEnemyCount - 6)
1629	RacialEnemyScrollBar.SetEvent (IE_GUI_SCROLLBAR_ON_CHANGE, DisplayRacialEnemies)
1630
1631	RacialEnemyTextArea = RacialEnemyWindow.GetControl (8)
1632	RacialEnemyTextArea.SetText (17256)
1633
1634	RacialEnemyDoneButton = RacialEnemyWindow.GetControl (11)
1635	RacialEnemyDoneButton.SetState (IE_GUI_BUTTON_DISABLED)
1636	RacialEnemyDoneButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, RacialEnemyDonePress)
1637	RacialEnemyDoneButton.SetText (11973)
1638	RacialEnemyDoneButton.MakeDefault()
1639
1640	RacialEnemyCancelButton = RacialEnemyWindow.GetControl (10)
1641	RacialEnemyCancelButton.SetState (IE_GUI_BUTTON_ENABLED)
1642	RacialEnemyCancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, RacialEnemyCancelPress)
1643	RacialEnemyCancelButton.SetText (13727)
1644	RacialEnemyCancelButton.MakeEscape()
1645
1646	DisplayRacialEnemies()
1647	RacialEnemyWindow.ShowModal(MODAL_SHADOW_NONE)
1648	return
1649
1650def DisplayRacialEnemies():
1651	global RacialEnemyWindow
1652
1653	RacialEnemyIndex = GemRB.GetVar ("RacialEnemyIndex")
1654	for i in range (2, 8):
1655		RacialEnemySelectButton = RacialEnemyWindow.GetControl (i)
1656		RacialEnemySelectButton.SetText (RacialEnemyTable.GetValue (RacialEnemyIndex + i - 2, 0))
1657	return
1658
1659def RacialEnemySelectPress():
1660	global RacialEnemyWindow, RacialEnemyDoneButton, RacialEnemyTextArea
1661
1662	RacialEnemy = GemRB.GetVar ("RacialEnemyIndex") + GemRB.GetVar ("RacialEnemy") - 1
1663	RacialEnemyTextArea.SetText (RacialEnemyTable.GetValue (RacialEnemy, 2) )
1664	GemRB.SetVar ("HatedRace", RacialEnemyTable.GetValue (RacialEnemy, 1) )
1665	RacialEnemyDoneButton.SetState (IE_GUI_BUTTON_ENABLED)
1666	return
1667
1668def RacialEnemyDonePress():
1669	global CharGenWindow, RacialEnemyWindow, SkillsState
1670
1671	if RacialEnemyWindow:
1672		RacialEnemyWindow.Unload ()
1673
1674	SkillsState = 1
1675	SkillsPress()
1676	return
1677
1678def RacialEnemyCancelPress():
1679	global CharGenWindow, RacialEnemyWindow, SkillsState
1680
1681	if RacialEnemyWindow:
1682		RacialEnemyWindow.Unload ()
1683	SkillsState = 0
1684	return
1685
1686
1687# Weapon Proficiencies Selection
1688
1689def ProficienciesSelect():
1690	global CharGenWindow, ProficienciesWindow, ProficienciesTextArea
1691	global ProficienciesPointsLeft, ProficienciesDoneButton, ProfsMaxTable
1692
1693	ProficienciesWindow = GemRB.LoadWindow (9)
1694	ProfsTable = GemRB.LoadTable ("profs")
1695	ProfsMaxTable = GemRB.LoadTable ("profsmax")
1696	ClassWeaponsTable = GemRB.LoadTable ("clasweap")
1697
1698	# remove all known spells and nullify the memorizable counts
1699	Spellbook.RemoveKnownSpells (MyChar, IE_SPELL_TYPE_WIZARD, 1,9, 1)
1700	Spellbook.RemoveKnownSpells (MyChar, IE_SPELL_TYPE_PRIEST, 1,7, 1)
1701	GemRB.SetVar ("MageMemorized", 0)
1702	GemRB.SetVar ("MageSpellBook", 0)
1703
1704	ClassName = GUICommon.GetClassRowName (MyChar)
1705	ProficienciesPointsLeft = ProfsTable.GetValue (ClassName, "FIRST_LEVEL")
1706	PointsLeftLabel = ProficienciesWindow.GetControl (0x10000009)
1707	PointsLeftLabel.SetText (str(ProficienciesPointsLeft))
1708
1709	for i in range (8):
1710		ProficienciesLabel = ProficienciesWindow.GetControl (69 + i)
1711		ProficienciesLabel.SetState (IE_GUI_BUTTON_ENABLED)
1712		ProficienciesLabel.SetEvent (IE_GUI_BUTTON_ON_PRESS, ProficienciesLabelPress)
1713		ProficienciesLabel.SetVarAssoc ("ProficienciesIndex", i + 1)
1714
1715		for j in range (5):
1716			ProficienciesMark = ProficienciesWindow.GetControl (27 + i * 5 + j)
1717			ProficienciesMark.SetSprites("GUIPFC", 0, 0, 0, 0, 0)
1718			ProficienciesMark.SetState (IE_GUI_BUTTON_DISABLED)
1719			ProficienciesMark.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_OR)
1720
1721		Allowed = ClassWeaponsTable.GetValue (ClassName, ProficienciesTable.GetRowName (i))
1722
1723		ProficienciesPlusButton = ProficienciesWindow.GetControl (11 + i * 2)
1724		if Allowed == 0:
1725			ProficienciesPlusButton.SetState (IE_GUI_BUTTON_DISABLED)
1726			ProficienciesPlusButton.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_OR)
1727		else:
1728			ProficienciesPlusButton.SetState (IE_GUI_BUTTON_ENABLED)
1729		ProficienciesPlusButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, ProficienciesPlusPress)
1730		ProficienciesPlusButton.SetVarAssoc ("ProficienciesIndex", i + 1)
1731		ProficienciesPlusButton.SetActionInterval (200)
1732
1733		ProficienciesMinusButton = ProficienciesWindow.GetControl (12 + i * 2)
1734		if Allowed == 0:
1735			ProficienciesMinusButton.SetState (IE_GUI_BUTTON_DISABLED)
1736			ProficienciesMinusButton.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_OR)
1737		else:
1738			ProficienciesMinusButton.SetState (IE_GUI_BUTTON_ENABLED)
1739		ProficienciesMinusButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, ProficienciesMinusPress)
1740		ProficienciesMinusButton.SetVarAssoc ("ProficienciesIndex", i + 1)
1741		ProficienciesMinusButton.SetActionInterval (200)
1742
1743	for i in range (7):
1744		ProficienciesLabel = ProficienciesWindow.GetControl (85 + i)
1745		ProficienciesLabel.SetState (IE_GUI_BUTTON_ENABLED)
1746		ProficienciesLabel.SetEvent (IE_GUI_BUTTON_ON_PRESS, ProficienciesLabelPress)
1747		ProficienciesLabel.SetVarAssoc ("ProficienciesIndex", i + 9)
1748
1749		for j in range (5):
1750			ProficienciesMark = ProficienciesWindow.GetControl (92 + i * 5 + j)
1751			ProficienciesMark.SetSprites("GUIPFC", 0, 0, 0, 0, 0)
1752			ProficienciesMark.SetState (IE_GUI_BUTTON_DISABLED)
1753			ProficienciesMark.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_OR)
1754
1755		Allowed = ClassWeaponsTable.GetValue (ClassName, ProficienciesTable.GetRowName (i + 8))
1756
1757		ProficienciesPlusButton = ProficienciesWindow.GetControl (127 + i * 2)
1758		if Allowed == 0:
1759			ProficienciesPlusButton.SetState (IE_GUI_BUTTON_DISABLED)
1760			ProficienciesPlusButton.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_OR)
1761		else:
1762			ProficienciesPlusButton.SetState (IE_GUI_BUTTON_ENABLED)
1763		ProficienciesPlusButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, ProficienciesPlusPress)
1764		ProficienciesPlusButton.SetVarAssoc ("ProficienciesIndex", i + 9)
1765		ProficienciesPlusButton.SetActionInterval (200)
1766
1767		ProficienciesMinusButton = ProficienciesWindow.GetControl (128 + i * 2)
1768		if Allowed == 0:
1769			ProficienciesMinusButton.SetState (IE_GUI_BUTTON_DISABLED)
1770			ProficienciesMinusButton.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_OR)
1771		else:
1772			ProficienciesMinusButton.SetState (IE_GUI_BUTTON_ENABLED)
1773		ProficienciesMinusButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, ProficienciesMinusPress)
1774		ProficienciesMinusButton.SetVarAssoc ("ProficienciesIndex", i + 9)
1775		ProficienciesMinusButton.SetActionInterval (200)
1776
1777	for i in range (15):
1778		GemRB.SetVar ("Proficiency" + str(i), 0)
1779
1780	GemRB.SetToken ("number", str(ProficienciesPointsLeft) )
1781	ProficienciesTextArea = ProficienciesWindow.GetControl (68)
1782	ProficienciesTextArea.SetText (9588)
1783
1784	ProficienciesDoneButton = ProficienciesWindow.GetControl (0)
1785	ProficienciesDoneButton.SetState (IE_GUI_BUTTON_DISABLED)
1786	ProficienciesDoneButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, ProficienciesDonePress)
1787	ProficienciesDoneButton.SetText (11973)
1788	ProficienciesDoneButton.MakeDefault()
1789
1790	ProficienciesCancelButton = ProficienciesWindow.GetControl (77)
1791	ProficienciesCancelButton.SetState (IE_GUI_BUTTON_ENABLED)
1792	ProficienciesCancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, ProficienciesCancelPress)
1793	ProficienciesCancelButton.SetText (13727)
1794	ProficienciesCancelButton.MakeEscape()
1795
1796	ProficienciesWindow.ShowModal(MODAL_SHADOW_NONE)
1797	return
1798
1799def ProficienciesLabelPress():
1800	global ProficienciesWindow, ProficienciesTextArea
1801
1802	ProficienciesIndex = GemRB.GetVar ("ProficienciesIndex") - 1
1803	ProficienciesTextArea.SetText (ProficienciesTable.GetValue (ProficienciesIndex, 2) )
1804	return
1805
1806def ProficienciesPlusPress():
1807	global ProficienciesWindow, ProficienciesTextArea
1808	global ProficienciesPointsLeft, ProfsMaxTable
1809
1810	ProficienciesIndex = GemRB.GetVar ("ProficienciesIndex") - 1
1811	ProficienciesValue = GemRB.GetVar ("Proficiency" + str(ProficienciesIndex) )
1812	ClassName = GUICommon.GetClassRowName (MyChar)
1813	if ProficienciesPointsLeft > 0 and ProficienciesValue < ProfsMaxTable.GetValue (ClassName, "FIRST_LEVEL"):
1814		ProficienciesPointsLeft = ProficienciesPointsLeft - 1
1815		PointsLeftLabel = ProficienciesWindow.GetControl (0x10000009)
1816		PointsLeftLabel.SetText (str(ProficienciesPointsLeft))
1817		if ProficienciesPointsLeft == 0:
1818			ProficienciesDoneButton.SetState (IE_GUI_BUTTON_ENABLED)
1819
1820		ProficienciesValue = ProficienciesValue + 1
1821		GemRB.SetVar ("Proficiency" + str(ProficienciesIndex), ProficienciesValue)
1822		if ProficienciesIndex < 8:
1823			ControlID = 26 + ProficienciesIndex * 5 + ProficienciesValue
1824		else:
1825			ControlID = 51 + ProficienciesIndex * 5 + ProficienciesValue
1826		ProficienciesMark = ProficienciesWindow.GetControl (ControlID)
1827		ProficienciesMark.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_NAND)
1828
1829	ProficienciesTextArea.SetText (ProficienciesTable.GetValue (ProficienciesIndex, 2) )
1830	return
1831
1832def ProficienciesMinusPress():
1833	global ProficienciesWindow, ProficienciesTextArea, ProficienciesPointsLeft
1834
1835	ProficienciesIndex = GemRB.GetVar ("ProficienciesIndex") - 1
1836	ProficienciesValue = GemRB.GetVar ("Proficiency" + str(ProficienciesIndex) )
1837	if ProficienciesValue > 0:
1838		ProficienciesValue = ProficienciesValue - 1
1839		GemRB.SetVar ("Proficiency" + str(ProficienciesIndex), ProficienciesValue)
1840		if ProficienciesIndex < 8:
1841			ControlID = 27 + ProficienciesIndex * 5 + ProficienciesValue
1842		else:
1843			ControlID = 52 + ProficienciesIndex * 5 + ProficienciesValue
1844		ProficienciesMark = ProficienciesWindow.GetControl (ControlID)
1845		ProficienciesMark.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_OR)
1846
1847		ProficienciesPointsLeft = ProficienciesPointsLeft + 1
1848		PointsLeftLabel = ProficienciesWindow.GetControl (0x10000009)
1849		PointsLeftLabel.SetText (str(ProficienciesPointsLeft))
1850		ProficienciesDoneButton.SetState (IE_GUI_BUTTON_DISABLED)
1851
1852	ProficienciesTextArea.SetText (ProficienciesTable.GetValue (ProficienciesIndex, 2) )
1853	return
1854
1855def ProficienciesDonePress():
1856	global CharGenWindow, ProficienciesWindow, SkillsState
1857
1858	if ProficienciesWindow:
1859		ProficienciesWindow.Unload ()
1860	SkillsState = 2
1861	SkillsPress()
1862	return
1863
1864def ProficienciesCancelPress():
1865	global CharGenWindow, ProficienciesWindow, SkillsState
1866
1867	if ProficienciesWindow:
1868		ProficienciesWindow.Unload ()
1869	SkillsState = 0
1870	return
1871
1872# Spells Selection
1873
1874def MageSpellsSelect(SpellTable, Level, SpellLevel):
1875	global CharGenWindow, MageSpellsWindow, MageSpellsTextArea, MageSpellsDoneButton, MageSpellsSelectPointsLeft, Learnable
1876
1877	MageSpellsWindow = GemRB.LoadWindow (7)
1878	#kit (school), alignment, level
1879	k = GemRB.GetPlayerStat (MyChar, IE_KIT)
1880	t = GemRB.GetPlayerStat (MyChar, IE_ALIGNMENT)
1881	Learnable = Spellbook.GetLearnableMageSpells(k, t, SpellLevel)
1882	GemRB.SetVar ("MageSpellBook", 0)
1883	GemRB.SetVar ("SpellMask", 0)
1884
1885	if len(Learnable) < 1 or GemRB.GetPlayerStat (MyChar, IE_CLASS) == 5: # no bards
1886		MageSpellsDonePress()
1887		return
1888
1889	if k & ~0x4000 > 0:
1890		MageSpellsSelectPointsLeft = 3
1891	else:
1892		MageSpellsSelectPointsLeft = 2
1893	PointsLeftLabel = MageSpellsWindow.GetControl (0x1000001b)
1894	PointsLeftLabel.SetText (str(MageSpellsSelectPointsLeft))
1895
1896	for i in range (24):
1897		SpellButton = MageSpellsWindow.GetControl (i + 2)
1898		SpellButton.SetFlags (IE_GUI_BUTTON_PICTURE|IE_GUI_BUTTON_CHECKBOX, OP_OR)
1899		if i < len(Learnable):
1900			Spell = GemRB.GetSpell (Learnable[i])
1901			SpellButton.SetSpellIcon(Learnable[i], 1)
1902			SpellButton.SetState (IE_GUI_BUTTON_ENABLED)
1903			SpellButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, MageSpellsSelectPress)
1904			SpellButton.SetVarAssoc ("SpellMask", 1 << i)
1905			SpellButton.SetTooltip(Spell["SpellName"])
1906		else:
1907			SpellButton.SetState (IE_GUI_BUTTON_DISABLED)
1908
1909	GemRB.SetToken ("number", str(MageSpellsSelectPointsLeft))
1910	MageSpellsTextArea = MageSpellsWindow.GetControl (27)
1911	MageSpellsTextArea.SetText (17250)
1912
1913	MageSpellsDoneButton = MageSpellsWindow.GetControl (0)
1914	MageSpellsDoneButton.SetState (IE_GUI_BUTTON_DISABLED)
1915	MageSpellsDoneButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, MageSpellsDonePress)
1916	MageSpellsDoneButton.SetText (11973)
1917	MageSpellsDoneButton.MakeDefault()
1918
1919	MageSpellsCancelButton = MageSpellsWindow.GetControl (29)
1920	MageSpellsCancelButton.SetState (IE_GUI_BUTTON_ENABLED)
1921	MageSpellsCancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, MageSpellsCancelPress)
1922	MageSpellsCancelButton.SetText (13727)
1923	MageSpellsCancelButton.MakeEscape()
1924
1925	MageSpellsWindow.ShowModal(MODAL_SHADOW_NONE)
1926	return
1927
1928def MageSpellsSelectPress():
1929	global MageSpellsWindow, MageSpellsTextArea, MageSpellsDoneButton, MageSpellsSelectPointsLeft, Learnable
1930
1931	MageSpellBook = GemRB.GetVar ("MageSpellBook")
1932	SpellMask = GemRB.GetVar ("SpellMask")
1933
1934	#getting the bit index
1935	Spell = abs(MageSpellBook - SpellMask)
1936	i = -1
1937	while (Spell > 0):
1938		i = i + 1
1939		Spell = Spell >> 1
1940
1941	Spell = GemRB.GetSpell (Learnable[i])
1942	MageSpellsTextArea.SetText (Spell["SpellDesc"])
1943
1944	if SpellMask < MageSpellBook:
1945		MageSpellsSelectPointsLeft = MageSpellsSelectPointsLeft + 1
1946	else:
1947		if MageSpellsSelectPointsLeft==0:
1948			SpellMask = MageSpellBook
1949			GemRB.SetVar ("SpellMask", SpellMask)
1950		else:
1951			MageSpellsSelectPointsLeft = MageSpellsSelectPointsLeft - 1
1952
1953	if MageSpellsSelectPointsLeft == 0:
1954		MageSpellsDoneButton.SetState (IE_GUI_BUTTON_ENABLED)
1955	else:
1956		MageSpellsDoneButton.SetState (IE_GUI_BUTTON_DISABLED)
1957
1958	for i in range (len(Learnable)):
1959		SpellButton = MageSpellsWindow.GetControl (i + 2)
1960		if ((1 << i) & SpellMask) == 0:
1961			SpellButton.SetState (IE_GUI_BUTTON_LOCKED)
1962
1963	PointsLeftLabel = MageSpellsWindow.GetControl (0x1000001b)
1964	PointsLeftLabel.SetText (str(MageSpellsSelectPointsLeft))
1965	GemRB.SetVar ("MageSpellBook", SpellMask)
1966	return
1967
1968def MageSpellsDonePress():
1969	global CharGenWindow, MageSpellsWindow, SkillsState
1970
1971	if MageSpellsWindow:
1972		MageSpellsWindow.Unload ()
1973	SkillsState = 3
1974	SkillsPress()
1975	return
1976
1977def MageSpellsCancelPress():
1978	global CharGenWindow, MageSpellsWindow, SkillsState
1979
1980	if MageSpellsWindow:
1981		MageSpellsWindow.Unload ()
1982	SkillsState = 0
1983	return
1984
1985
1986# Mage Spells Memorize
1987
1988def MageSpellsMemorize(SpellTable, Level, SpellLevel):
1989	global CharGenWindow, MageMemorizeWindow, MageMemorizeTextArea, MageMemorizeDoneButton, MageMemorizePointsLeft
1990
1991	MageMemorizeWindow = GemRB.LoadWindow (16)
1992	MaxSpellsMageTable = GemRB.LoadTable (SpellTable)
1993	MageSpellBook = GemRB.GetVar ("MageSpellBook")
1994	GemRB.SetVar ("MageMemorized", 0)
1995	GemRB.SetVar ("SpellMask", 0)
1996
1997	MageMemorizePointsLeft = MaxSpellsMageTable.GetValue (str(Level), str(SpellLevel) )
1998	if MageMemorizePointsLeft<1 or len(Learnable)<1:
1999		MageMemorizeDonePress()
2000		return
2001
2002	# one more spell for specialists
2003	k = GemRB.GetPlayerStat (MyChar, IE_KIT)
2004	if k & ~0x4000 > 0:
2005		MageMemorizePointsLeft = MageMemorizePointsLeft + 1
2006
2007	PointsLeftLabel = MageMemorizeWindow.GetControl (0x1000001b)
2008	PointsLeftLabel.SetText (str(MageMemorizePointsLeft))
2009
2010	j = 0
2011	for i in range (12):
2012		SpellButton = MageMemorizeWindow.GetControl (i + 2)
2013		SpellButton.SetFlags (IE_GUI_BUTTON_PICTURE|IE_GUI_BUTTON_CHECKBOX, OP_OR)
2014		while (j < len(Learnable)) and (((1 << j) & MageSpellBook) == 0):
2015			j = j + 1
2016		if j < len(Learnable):
2017			Spell = GemRB.GetSpell (Learnable[j])
2018			SpellButton.SetTooltip(Spell["SpellName"])
2019			SpellButton.SetSpellIcon(Learnable[j], 1)
2020			SpellButton.SetState (IE_GUI_BUTTON_ENABLED)
2021			SpellButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, MageMemorizeSelectPress)
2022			SpellButton.SetVarAssoc ("SpellMask", 1 << j)
2023			j = j + 1
2024		else:
2025			SpellButton.SetState (IE_GUI_BUTTON_DISABLED)
2026
2027	GemRB.SetToken ("number", str(MageMemorizePointsLeft))
2028	MageMemorizeTextArea = MageMemorizeWindow.GetControl (27)
2029	MageMemorizeTextArea.SetText (17253)
2030
2031	MageMemorizeDoneButton = MageMemorizeWindow.GetControl (0)
2032	MageMemorizeDoneButton.SetState (IE_GUI_BUTTON_DISABLED)
2033	MageMemorizeDoneButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, MageMemorizeDonePress)
2034	MageMemorizeDoneButton.SetText (11973)
2035	MageMemorizeDoneButton.MakeDefault()
2036
2037	MageMemorizeCancelButton = MageMemorizeWindow.GetControl (29)
2038	MageMemorizeCancelButton.SetState (IE_GUI_BUTTON_ENABLED)
2039	MageMemorizeCancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, MageMemorizeCancelPress)
2040	MageMemorizeCancelButton.SetText (13727)
2041	MageMemorizeCancelButton.MakeEscape()
2042
2043	MageMemorizeWindow.ShowModal(MODAL_SHADOW_NONE)
2044	return
2045
2046def MageMemorizeSelectPress():
2047	global MageMemorizeWindow, MageMemorizeTextArea, MageMemorizeDoneButton, MageMemorizePointsLeft, Learnable
2048
2049	MageSpellBook = GemRB.GetVar ("MageSpellBook")
2050	MageMemorized = GemRB.GetVar ("MageMemorized")
2051	SpellMask = GemRB.GetVar ("SpellMask")
2052
2053	Spell = abs(MageMemorized - SpellMask)
2054	i = -1
2055	while (Spell > 0):
2056		i = i + 1
2057		Spell = Spell >> 1
2058
2059	Spell = GemRB.GetSpell (Learnable[i])
2060	MageMemorizeTextArea.SetText (Spell["SpellDesc"])
2061
2062	if SpellMask < MageMemorized:
2063		MageMemorizePointsLeft = MageMemorizePointsLeft + 1
2064		j = 0
2065		for i in range (12):
2066			SpellButton = MageMemorizeWindow.GetControl (i + 2)
2067			while (j < len(Learnable) ) and (((1 << j) & MageSpellBook) == 0):
2068				j = j + 1
2069			if j < len(Learnable):
2070				SpellButton.SetState (IE_GUI_BUTTON_ENABLED)
2071				j = j + 1
2072		MageMemorizeDoneButton.SetState (IE_GUI_BUTTON_DISABLED)
2073	else:
2074		MageMemorizePointsLeft = MageMemorizePointsLeft - 1
2075		if MageMemorizePointsLeft == 0:
2076			j = 0
2077			for i in range (12):
2078				SpellButton = MageMemorizeWindow.GetControl (i + 2)
2079				while (j < len(Learnable) ) and (((1 << j) & MageSpellBook) == 0):
2080					j = j + 1
2081				if j < len(Learnable):
2082					if ((1 << j) & SpellMask) == 0:
2083						SpellButton.SetState (IE_GUI_BUTTON_DISABLED)
2084					j = j + 1
2085			MageMemorizeDoneButton.SetState (IE_GUI_BUTTON_ENABLED)
2086
2087	PointsLeftLabel = MageMemorizeWindow.GetControl (0x1000001b)
2088	PointsLeftLabel.SetText (str(MageMemorizePointsLeft))
2089	GemRB.SetVar ("MageMemorized", SpellMask)
2090	return
2091
2092def MageMemorizeDonePress():
2093	global CharGenWindow, MageMemorizeWindow, SkillsState, MyChar
2094
2095	if MageMemorizeWindow:
2096		MageMemorizeWindow.Unload ()
2097	LearnSpells (MyChar)
2098	SkillsState = 4
2099	SkillsPress()
2100	return
2101
2102def MageMemorizeCancelPress():
2103	global CharGenWindow, MageMemorizeWindow, SkillsState
2104
2105	if MageMemorizeWindow:
2106		MageMemorizeWindow.Unload ()
2107	SkillsState = 0
2108	return
2109
2110# Priest Spells Memorize
2111
2112def PriestSpellsMemorize(SpellTable, Level, SpellLevel):
2113	global CharGenWindow, PriestMemorizeWindow, Learnable, ClassFlag
2114	global PriestMemorizeTextArea, PriestMemorizeDoneButton, PriestMemorizePointsLeft
2115
2116	PriestMemorizeWindow = GemRB.LoadWindow (17)
2117	t = CommonTables.Aligns.GetValue (GemRB.GetVar ("Alignment")-1, 3)
2118	Learnable = Spellbook.GetLearnablePriestSpells( ClassFlag, t, SpellLevel)
2119
2120	MaxSpellsPriestTable = GemRB.LoadTable (SpellTable)
2121	GemRB.SetVar ("PriestMemorized", 0)
2122	GemRB.SetVar ("SpellMask", 0)
2123
2124	PriestMemorizePointsLeft = MaxSpellsPriestTable.GetValue (str(Level), str(SpellLevel) )
2125	if PriestMemorizePointsLeft<1 or len(Learnable)<1:
2126		PriestMemorizeDonePress()
2127		return
2128
2129	PointsLeftLabel = PriestMemorizeWindow.GetControl (0x1000001b)
2130	PointsLeftLabel.SetText (str(PriestMemorizePointsLeft))
2131
2132	for i in range (12):
2133		SpellButton = PriestMemorizeWindow.GetControl (i + 2)
2134		SpellButton.SetFlags (IE_GUI_BUTTON_PICTURE|IE_GUI_BUTTON_CHECKBOX, OP_OR)
2135		if i < len(Learnable):
2136			Spell = GemRB.GetSpell (Learnable[i])
2137			SpellButton.SetTooltip(Spell["SpellName"])
2138			SpellButton.SetSpellIcon(Learnable[i], 1)
2139			SpellButton.SetState (IE_GUI_BUTTON_ENABLED)
2140			SpellButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, PriestMemorizeSelectPress)
2141			SpellButton.SetVarAssoc ("SpellMask", 1 << i)
2142		else:
2143			SpellButton.SetState (IE_GUI_BUTTON_DISABLED)
2144
2145	GemRB.SetToken ("number", str(PriestMemorizePointsLeft))
2146	PriestMemorizeTextArea = PriestMemorizeWindow.GetControl (27)
2147	PriestMemorizeTextArea.SetText (17253)
2148
2149	PriestMemorizeDoneButton = PriestMemorizeWindow.GetControl (0)
2150	PriestMemorizeDoneButton.SetState (IE_GUI_BUTTON_DISABLED)
2151	PriestMemorizeDoneButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, PriestMemorizeDonePress)
2152	PriestMemorizeDoneButton.SetText (11973)
2153	PriestMemorizeDoneButton.MakeDefault()
2154
2155	PriestMemorizeCancelButton = PriestMemorizeWindow.GetControl (29)
2156	PriestMemorizeCancelButton.SetState (IE_GUI_BUTTON_ENABLED)
2157	PriestMemorizeCancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, PriestMemorizeCancelPress)
2158	PriestMemorizeCancelButton.SetText (13727)
2159	PriestMemorizeCancelButton.MakeEscape()
2160
2161	PriestMemorizeWindow.ShowModal(MODAL_SHADOW_NONE)
2162	return
2163
2164def PriestMemorizeSelectPress():
2165	global PriestMemorizeWindow, Learnable, PriestMemorizeTextArea, PriestMemorizeDoneButton, PriestMemorizePointsLeft
2166
2167	PriestMemorized = GemRB.GetVar ("PriestMemorized")
2168	SpellMask = GemRB.GetVar ("SpellMask")
2169	Spell = abs(PriestMemorized - SpellMask)
2170
2171	i = -1
2172	while (Spell > 0):
2173		i = i + 1
2174		Spell = Spell >> 1
2175
2176	Spell=GemRB.GetSpell (Learnable[i])
2177	PriestMemorizeTextArea.SetText (Spell["SpellDesc"])
2178
2179	if SpellMask < PriestMemorized:
2180		PriestMemorizePointsLeft = PriestMemorizePointsLeft + 1
2181		for i in range (len(Learnable)):
2182			SpellButton = PriestMemorizeWindow.GetControl (i + 2)
2183			if (((1 << i) & SpellMask) == 0):
2184				SpellButton.SetState (IE_GUI_BUTTON_ENABLED)
2185		PriestMemorizeDoneButton.SetState (IE_GUI_BUTTON_DISABLED)
2186	else:
2187		PriestMemorizePointsLeft = PriestMemorizePointsLeft - 1
2188		if PriestMemorizePointsLeft == 0:
2189			for i in range (len(Learnable)):
2190				SpellButton = PriestMemorizeWindow.GetControl (i + 2)
2191				if ((1 << i) & SpellMask) == 0:
2192					SpellButton.SetState (IE_GUI_BUTTON_DISABLED)
2193			PriestMemorizeDoneButton.SetState (IE_GUI_BUTTON_ENABLED)
2194
2195	PointsLeftLabel = PriestMemorizeWindow.GetControl (0x1000001b)
2196	PointsLeftLabel.SetText (str(PriestMemorizePointsLeft))
2197	GemRB.SetVar ("PriestMemorized", SpellMask)
2198	return
2199
2200def PriestMemorizeDonePress():
2201	global CharGenWindow, PriestMemorizeWindow, SkillsState, MyChar
2202
2203	if PriestMemorizeWindow:
2204		PriestMemorizeWindow.Unload ()
2205	LearnSpells (MyChar)
2206	SkillsState = 5
2207	SkillsPress()
2208	return
2209
2210def PriestMemorizeCancelPress():
2211	global CharGenWindow, PriestMemorizeWindow, SkillsState
2212
2213	if PriestMemorizeWindow:
2214		PriestMemorizeWindow.Unload ()
2215	SkillsState = 0
2216	return
2217
2218# Appearance Selection
2219
2220def AppearancePress():
2221	global CharGenWindow, AppearanceWindow, AppearanceTable
2222	global Portrait, AppearanceAvatarButton, PortraitName
2223	global AppearanceHairButton, AppearanceSkinButton
2224	global AppearanceMajorButton, AppearanceMinorButton
2225	global HairColor, SkinColor, MajorColor, MinorColor
2226
2227	AppearanceWindow = GemRB.LoadWindow (13)
2228	AppearanceTable = GemRB.LoadTable ("PORTCOLR")
2229
2230	if Portrait<0:
2231		PortraitIndex = 0
2232	else:
2233		PortraitName = PortraitsTable.GetRowName (Portrait)
2234		PortraitIndex = AppearanceTable.GetRowIndex (PortraitName + "L")
2235
2236	HairColor = AppearanceTable.GetValue (PortraitIndex, 1)
2237	GemRB.SetVar ("HairColor", HairColor)
2238	SkinColor = AppearanceTable.GetValue (PortraitIndex, 0)
2239	GemRB.SetVar ("SkinColor", SkinColor)
2240	MajorColor = AppearanceTable.GetValue (PortraitIndex, 2)
2241	GemRB.SetVar ("MajorColor", MajorColor)
2242	MinorColor = AppearanceTable.GetValue (PortraitIndex, 3)
2243	GemRB.SetVar ("MinorColor", MinorColor)
2244
2245	AppearanceAvatarButton = AppearanceWindow.GetControl (1)
2246	AppearanceAvatarButton.SetState (IE_GUI_BUTTON_LOCKED)
2247	AppearanceAvatarButton.SetFlags (IE_GUI_BUTTON_PICTURE|IE_GUI_BUTTON_ANIMATED, OP_OR)
2248	DrawAvatar()
2249
2250	AppearanceHairButton = AppearanceWindow.GetControl (2)
2251	AppearanceHairButton.SetFlags (IE_GUI_BUTTON_PICTURE, OP_OR)
2252	AppearanceHairButton.SetState (IE_GUI_BUTTON_ENABLED)
2253	AppearanceHairButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AppearanceHairPress)
2254	AppearanceHairButton.SetBAM ("COLGRAD", 0, 0, HairColor)
2255
2256	AppearanceSkinButton = AppearanceWindow.GetControl (3)
2257	AppearanceSkinButton.SetFlags (IE_GUI_BUTTON_PICTURE, OP_OR)
2258	AppearanceSkinButton.SetState (IE_GUI_BUTTON_ENABLED)
2259	AppearanceSkinButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AppearanceSkinPress)
2260	AppearanceSkinButton.SetBAM ("COLGRAD", 0, 0, SkinColor)
2261
2262	AppearanceMajorButton = AppearanceWindow.GetControl (4)
2263	AppearanceMajorButton.SetFlags (IE_GUI_BUTTON_PICTURE, OP_OR)
2264	AppearanceMajorButton.SetState (IE_GUI_BUTTON_ENABLED)
2265	AppearanceMajorButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AppearanceMajorPress)
2266	AppearanceMajorButton.SetBAM ("COLGRAD", 0, 0, MajorColor)
2267
2268	AppearanceMinorButton = AppearanceWindow.GetControl (5)
2269	AppearanceMinorButton.SetFlags (IE_GUI_BUTTON_PICTURE, OP_OR)
2270	AppearanceMinorButton.SetState (IE_GUI_BUTTON_ENABLED)
2271	AppearanceMinorButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AppearanceMinorPress)
2272	AppearanceMinorButton.SetBAM ("COLGRAD", 0, 0, MinorColor)
2273
2274	AppearanceDoneButton = AppearanceWindow.GetControl (0)
2275	AppearanceDoneButton.SetState (IE_GUI_BUTTON_ENABLED)
2276	AppearanceDoneButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AppearanceDonePress)
2277	AppearanceDoneButton.SetText (11973)
2278	AppearanceDoneButton.MakeDefault()
2279
2280	AppearanceCancelButton = AppearanceWindow.GetControl (13)
2281	AppearanceCancelButton.SetState (IE_GUI_BUTTON_ENABLED)
2282	AppearanceCancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AppearanceCancelPress)
2283	AppearanceCancelButton.SetText (13727)
2284	AppearanceCancelButton.MakeEscape()
2285
2286	AppearanceWindow.ShowModal(MODAL_SHADOW_NONE)
2287	return
2288
2289def DrawAvatar():
2290	global AppearanceAvatarButton
2291	global MyChar
2292
2293	AvatarID = 0x6000
2294	table = GemRB.LoadTable ("avprefr")
2295	lookup = CommonTables.Races.FindValue (3, GemRB.GetPlayerStat(MyChar, IE_RACE))
2296	lookup = CommonTables.Races.GetRowName (lookup)
2297	AvatarID = AvatarID+table.GetValue (lookup, "RACE")
2298	table = GemRB.LoadTable ("avprefc")
2299	lookup = GUICommon.GetClassRowName (MyChar)
2300	AvatarID = AvatarID + table.GetValue (lookup, "CLASS")
2301	table = GemRB.LoadTable ("avprefg")
2302	AvatarID = AvatarID + table.GetValue (GemRB.GetPlayerStat(MyChar,IE_SEX), GTV_STR)
2303
2304	AvatarRef = CommonTables.Pdolls.GetValue (hex(AvatarID), "LEVEL1")
2305	AppearanceAvatarButton.SetPLT(AvatarRef, 0, MinorColor, MajorColor, SkinColor, 0, 0, HairColor, 0)
2306
2307	return
2308
2309def AppearanceHairPress():
2310	GemRB.SetVar ("ColorType", 0)
2311	AppearanceColorChoice (GemRB.GetVar ("HairColor"))
2312	return
2313
2314def AppearanceSkinPress():
2315	GemRB.SetVar ("ColorType", 1)
2316	AppearanceColorChoice (GemRB.GetVar ("SkinColor"))
2317	return
2318
2319def AppearanceMajorPress():
2320	GemRB.SetVar ("ColorType", 2)
2321	AppearanceColorChoice (GemRB.GetVar ("MajorColor"))
2322	return
2323
2324def AppearanceMinorPress():
2325	GemRB.SetVar ("ColorType", 3)
2326	AppearanceColorChoice (GemRB.GetVar ("MinorColor"))
2327	return
2328
2329def AppearanceColorChoice (CurrentColor):
2330	global AppearanceWindow, AppearanceColorWindow
2331
2332	AppearanceWindow.SetVisible(False)
2333	AppearanceColorWindow = GemRB.LoadWindow (14)
2334	AppearanceColorTable = GemRB.LoadTable ("clowncol")
2335	ColorType = GemRB.GetVar ("ColorType")
2336	GemRB.SetVar ("SelectedColor", CurrentColor)
2337
2338	for i in range (34):
2339		ColorButton = AppearanceColorWindow.GetControl (i)
2340		ColorButton.SetState (IE_GUI_BUTTON_ENABLED)
2341		ColorButton.SetFlags (IE_GUI_BUTTON_PICTURE, OP_OR)
2342
2343	for i in range (34):
2344		Color = AppearanceColorTable.GetValue (ColorType, i)
2345		if Color != "*":
2346			ColorButton = AppearanceColorWindow.GetControl (i)
2347			ColorButton.SetBAM ("COLGRAD", 2, 0, Color)
2348			ColorButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AppearanceColorSelected)
2349			ColorButton.SetVarAssoc ("SelectedColor", Color)
2350
2351	AppearanceColorWindow.ShowModal(MODAL_SHADOW_NONE)
2352	return
2353
2354def AppearanceColorSelected():
2355	global HairColor, SkinColor, MajorColor, MinorColor
2356	global AppearanceWindow, AppearanceColorWindow
2357	global AppearanceHairButton, AppearanceSkinButton
2358	global AppearanceMajorButton, AppearanceMinorButton
2359
2360	if AppearanceColorWindow:
2361		AppearanceColorWindow.Unload ()
2362	ColorType = GemRB.GetVar ("ColorType")
2363	if ColorType == 0:
2364		HairColor = GemRB.GetVar ("SelectedColor")
2365		GemRB.SetVar ("HairColor", HairColor)
2366		AppearanceHairButton.SetBAM ("COLGRAD", 0, 0, HairColor)
2367	elif ColorType == 1:
2368		SkinColor = GemRB.GetVar ("SelectedColor")
2369		GemRB.SetVar ("SkinColor", SkinColor)
2370		AppearanceSkinButton.SetBAM ("COLGRAD", 0, 0, SkinColor)
2371	elif ColorType == 2:
2372		MajorColor = GemRB.GetVar ("SelectedColor")
2373		GemRB.SetVar ("MajorColor", MajorColor)
2374		AppearanceMajorButton.SetBAM ("COLGRAD", 0, 0, MajorColor)
2375	elif ColorType == 3:
2376		MinorColor = GemRB.GetVar ("SelectedColor")
2377		GemRB.SetVar ("MinorColor", MinorColor)
2378		AppearanceMinorButton.SetBAM ("COLGRAD", 0, 0, MinorColor)
2379	DrawAvatar()
2380	AppearanceWindow.ShowModal(MODAL_SHADOW_NONE)
2381	return
2382
2383def AppearanceDonePress():
2384	global CharGenWindow, AppearanceWindow
2385
2386	if AppearanceWindow:
2387		AppearanceWindow.Unload ()
2388	CharSoundSelect()
2389	return
2390
2391def AppearanceCancelPress():
2392	global CharGenWindow, AppearanceWindow
2393
2394	if AppearanceWindow:
2395		AppearanceWindow.Unload ()
2396	return
2397
2398def CharSoundSelect():
2399	global CharGenWindow, CharSoundWindow, CharSoundTable, CharSoundStrings
2400	global CharSoundVoiceList, VerbalConstants
2401
2402	CharSoundWindow = GemRB.LoadWindow (19)
2403	CharSoundTable = GemRB.LoadTable ("CHARSND")
2404	CharSoundStrings = GemRB.LoadTable ("CHARSTR")
2405
2406	VerbalConstants =  [CharSoundTable.GetRowName(i) for i in range(CharSoundTable.GetRowCount())]
2407	CharSoundVoiceList = CharSoundWindow.GetControl (45)
2408
2409	if GemRB.GetVar ("Gender") == 2:
2410		GemRB.SetVar ("Selected", 0) #first female sound
2411	else:
2412		GemRB.SetVar ("Selected", 15)
2413	CharSoundVoiceList.SetVarAssoc ("Selected", 0)
2414	CharSoundVoiceList.ListResources (CHR_SOUNDS)
2415
2416	CharSoundPlayButton = CharSoundWindow.GetControl (47)
2417	CharSoundPlayButton.SetState (IE_GUI_BUTTON_ENABLED)
2418	CharSoundPlayButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, CharSoundPlayPress)
2419	CharSoundPlayButton.SetText (17318)
2420
2421	CharSoundTextArea = CharSoundWindow.GetControl (50)
2422	CharSoundTextArea.SetText (11315)
2423
2424	CharSoundDoneButton = CharSoundWindow.GetControl (0)
2425	CharSoundDoneButton.SetState (IE_GUI_BUTTON_ENABLED)
2426	CharSoundDoneButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, CharSoundDonePress)
2427	CharSoundDoneButton.SetText (11973)
2428	CharSoundDoneButton.MakeDefault()
2429
2430	CharSoundCancelButton = CharSoundWindow.GetControl (10)
2431	CharSoundCancelButton.SetState (IE_GUI_BUTTON_ENABLED)
2432	CharSoundCancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, CharSoundCancelPress)
2433	CharSoundCancelButton.SetText (13727)
2434	CharSoundCancelButton.MakeEscape()
2435
2436	CharSoundWindow.ShowModal(MODAL_SHADOW_NONE)
2437	return
2438
2439def CharSoundPlayPress():
2440	global CharGenWindow, CharSoundWindow, CharSoundTable, CharSoundStrings
2441	global CharSoundVoiceList, SoundIndex, VerbalConstants
2442
2443	row = CharSoundVoiceList.QueryText ()
2444	GemRB.SetPlayerSound (MyChar, row)
2445
2446	#play sound as sound slot
2447	GemRB.VerbalConstant (MyChar, int(VerbalConstants[SoundIndex]))
2448
2449	SoundIndex += 1
2450	if SoundIndex >= len(VerbalConstants):
2451		SoundIndex = 0
2452	return
2453
2454def CharSoundDonePress():
2455	global CharGenWindow, CharSoundWindow, AppearanceButton, BiographyButton, NameButton, CharGenState
2456
2457	if CharSoundWindow:
2458		CharSoundWindow.Unload ()
2459	AppearanceButton.SetState (IE_GUI_BUTTON_DISABLED)
2460	BiographyButton.SetState (IE_GUI_BUTTON_ENABLED)
2461	NameButton.SetState (IE_GUI_BUTTON_ENABLED)
2462	NameButton.MakeDefault()
2463	CharGenState = 7
2464	SetCharacterDescription()
2465	return
2466
2467def CharSoundCancelPress():
2468	global CharGenWindow, CharSoundWindow
2469
2470	if CharSoundWindow:
2471		CharSoundWindow.Unload ()
2472	return
2473
2474# Biography Selection
2475
2476def BiographyPress():
2477	global CharGenWindow, BiographyWindow, BiographyTextArea
2478
2479	BiographyWindow = GemRB.LoadWindow (51)
2480	BiographyTextArea = BiographyWindow.ReplaceSubview(4, IE_GUI_TEXTAREA, "NORMAL")
2481	BiographyTextArea.SetFlags(IE_GUI_TEXTAREA_EDITABLE, OP_OR)
2482	BiographyTextArea.Focus()
2483	BiographyTextArea.SetColor (ColorWhitish, TA_COLOR_NORMAL)
2484
2485	BIO = GemRB.GetToken("Biography")
2486	if BIO:
2487		BiographyTextArea.SetText (BIO)
2488	else:
2489		BiographyTextArea.SetText (19423)
2490
2491	BiographyClearButton = BiographyWindow.GetControl (5)
2492	BiographyClearButton.SetState (IE_GUI_BUTTON_ENABLED)
2493	BiographyClearButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, lambda: BiographyClearPress (BiographyTextArea))
2494	BiographyClearButton.SetText (18622)
2495
2496	BiographyCancelButton = BiographyWindow.GetControl (2)
2497	BiographyCancelButton.SetState (IE_GUI_BUTTON_ENABLED)
2498	BiographyCancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, lambda: BiographyWindow.Close())
2499	BiographyCancelButton.SetText (13727)
2500	BiographyCancelButton.MakeEscape()
2501
2502	BiographyDoneButton = BiographyWindow.GetControl (1)
2503	BiographyDoneButton.SetState (IE_GUI_BUTTON_ENABLED)
2504	BiographyDoneButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, BiographyDonePress)
2505	BiographyDoneButton.SetText (11973)
2506
2507	BiographyWindow.ShowModal(MODAL_SHADOW_NONE)
2508	return
2509
2510def BiographyClearPress(TA):
2511	TA.Clear ()
2512	TA.Focus ()
2513
2514def BiographyDonePress():
2515	global CharGenWindow, BiographyWindow, BiographyTextArea
2516
2517	BIO = BiographyTextArea.QueryText ()
2518	GemRB.SetToken ("Biography", BIO) # just for any window reopens
2519	BioStrRefSlot = 63
2520	DefaultBIO = 19423
2521	if BIO == GemRB.GetString (DefaultBIO):
2522		GemRB.SetPlayerString (MyChar, BioStrRefSlot, DefaultBIO)
2523	else:
2524		# unlike tob, iwd has no marked placeholders (or strings) at 62015; but we have special magic in place ...
2525		# still, use the returned strref in case anything unexpected happened
2526		ref = GemRB.CreateString (62015+MyChar, BIO)
2527		GemRB.SetPlayerString (MyChar, BioStrRefSlot, ref)
2528
2529	if BiographyWindow:
2530		BiographyWindow.Unload ()
2531	return
2532
2533# Name Selection
2534
2535def NamePress():
2536	global CharGenWindow, NameWindow, NameDoneButton, NameField
2537
2538	NameWindow = GemRB.LoadWindow (5)
2539
2540	NameDoneButton = NameWindow.GetControl (0)
2541	NameDoneButton.SetState (IE_GUI_BUTTON_DISABLED)
2542	NameDoneButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, NameDonePress)
2543	NameDoneButton.SetText (11973)
2544	NameDoneButton.MakeDefault()
2545
2546	NameCancelButton = NameWindow.GetControl (3)
2547	NameCancelButton.SetState (IE_GUI_BUTTON_ENABLED)
2548	NameCancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, NameCancelPress)
2549	NameCancelButton.SetText (13727)
2550	NameCancelButton.MakeEscape()
2551
2552	NameField = NameWindow.GetControl (2)
2553	NameField.SetEvent (IE_GUI_EDIT_ON_CHANGE, NameEditChange)
2554	NameField.SetText (GemRB.GetToken ("CHARNAME") )
2555	NameField.Focus()
2556
2557	NameWindow.ShowModal(MODAL_SHADOW_NONE)
2558	NameEditChange()
2559	return
2560
2561def NameEditChange():
2562	global NameField
2563
2564	if NameField.QueryText () == "":
2565		NameDoneButton.SetState (IE_GUI_BUTTON_DISABLED)
2566	else:
2567		NameDoneButton.SetState (IE_GUI_BUTTON_ENABLED)
2568	return
2569
2570def NameDonePress():
2571	global CharGenWindow, CharGenState, NameWindow, NameField, AcceptButton
2572
2573	GemRB.SetToken ("CHARNAME", NameField.QueryText () )
2574	if NameWindow:
2575		NameWindow.Unload ()
2576	CharGenState = 8
2577	AcceptButton.SetState (IE_GUI_BUTTON_ENABLED)
2578	AcceptButton.MakeDefault()
2579	SetCharacterDescription()
2580	return
2581
2582def NameCancelPress():
2583	global CharGenWindow, NameWindow
2584
2585	GemRB.SetToken ("CHARNAME", "")
2586	if NameWindow:
2587		NameWindow.Unload ()
2588	return
2589
2590# Import Character
2591
2592def ImportPress():
2593	global CharGenWindow, ImportWindow
2594	global CharImportList
2595
2596	ImportWindow = GemRB.LoadWindow (20)
2597
2598	TextAreaControl = ImportWindow.GetControl(4)
2599	TextAreaControl.SetText(10963)
2600
2601	GemRB.SetVar ("Selected", 0)
2602	CharImportList = ImportWindow.GetControl(2)
2603	CharImportList.SetVarAssoc ("Selected",0)
2604	CharImportList.ListResources(CHR_EXPORTS)
2605
2606	ImportDoneButton = ImportWindow.GetControl (0)
2607	ImportDoneButton.SetState (IE_GUI_BUTTON_ENABLED)
2608	ImportDoneButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, ImportDonePress)
2609	ImportDoneButton.SetText (11973)
2610	ImportDoneButton.MakeDefault()
2611
2612	ImportCancelButton = ImportWindow.GetControl (1)
2613	ImportCancelButton.SetState (IE_GUI_BUTTON_ENABLED)
2614	ImportCancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, ImportCancelPress)
2615	ImportCancelButton.SetText (13727)
2616	ImportCancelButton.MakeEscape()
2617
2618	ImportWindow.ShowModal(MODAL_SHADOW_NONE)
2619	return
2620
2621def ImportDonePress():
2622	global CharGenWindow, ImportWindow, CharImportList
2623	global CharGenState, SkillsState, Portrait, ImportedChar, HasStrExtra
2624
2625	# Import the character from the chosen name
2626	GemRB.CreatePlayer (CharImportList.QueryText(True), MyChar|0x8000, 1)
2627
2628	GemRB.SetToken ("CHARNAME", GemRB.GetPlayerName (MyChar))
2629	GemRB.SetToken ("SmallPortrait", GemRB.GetPlayerPortrait (MyChar, 1)["ResRef"])
2630	Portrait = GemRB.GetPlayerPortrait (MyChar, 0)
2631	GemRB.SetToken ("LargePortrait", Portrait["ResRef"])
2632	PortraitButton.SetPicture (Portrait["Sprite"], "NOPORTLG")
2633	Portrait = -1
2634
2635	ClassName = GUICommon.GetClassRowName (MyChar)
2636	HasStrExtra = CommonTables.Classes.GetValue (ClassName, "STREXTRA", GTV_INT)
2637
2638	ImportedChar = 1
2639	CharGenState = 7
2640	SkillsState = 5
2641	SetCharacterDescription ()
2642	GenderButton.SetState (IE_GUI_BUTTON_DISABLED)
2643	RaceButton.SetState (IE_GUI_BUTTON_DISABLED)
2644	ClassButton.SetState (IE_GUI_BUTTON_DISABLED)
2645	AlignmentButton.SetState (IE_GUI_BUTTON_DISABLED)
2646	AbilitiesButton.SetState (IE_GUI_BUTTON_DISABLED)
2647	SkillsButton.SetState (IE_GUI_BUTTON_DISABLED)
2648	AppearanceButton.SetState (IE_GUI_BUTTON_ENABLED)
2649	BiographyButton.SetState (IE_GUI_BUTTON_DISABLED)
2650	NameButton.SetState (IE_GUI_BUTTON_DISABLED)
2651	NameButton.MakeDefault()
2652	if ImportWindow:
2653		ImportWindow.Unload ()
2654	return
2655
2656def ImportCancelPress():
2657	global CharGenWindow, ImportWindow
2658
2659	if ImportWindow:
2660		ImportWindow.Unload ()
2661	return
2662