1# GemRB - Infinity Engine Emulator
2# Copyright (C) 2003 The GemRB Project
3#
4# This program is free software; you can redistribute it and/or
5# modify it under the terms of the GNU General Public License
6# as published by the Free Software Foundation; either version 2
7# of the License, or (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17#
18#
19#character generation, ability (GUICG4)
20import GemRB
21import GUICommon
22import CommonTables
23from ie_stats import *
24from GUIDefines import *
25from ie_restype import RES_2DA
26
27import CharGenCommon
28
29AbilityWindow = 0
30TextAreaControl = 0
31DoneButton = 0
32AbilityTable = 0
33Abclasrq = 0
34Abclsmod = 0
35Abclasrq = 0
36Abracerq = 0
37PointsLeft = 0
38Minimum = 0
39Maximum = 0
40Add = 0
41KitIndex = 0
42HasStrExtra = 0
43MyChar = 0
44
45def CalcLimits(Abidx):
46	global Minimum, Maximum, Add
47
48	Race = CommonTables.Races.FindValue (3, GemRB.GetPlayerStat (MyChar, IE_RACE) )
49	RaceName = CommonTables.Races.GetRowName(Race)
50
51	Minimum = 3
52	Maximum = 18
53
54	Race = Abracerq.GetRowIndex(RaceName)
55	tmp = Abracerq.GetValue(Race, Abidx*2)
56	if tmp != 0:
57		Minimum = tmp
58
59	tmp = Abracerq.GetValue(Race, Abidx*2+1)
60	if tmp != 0:
61		Maximum = tmp
62
63
64	Race = Abracead.GetRowIndex(RaceName)
65	Add = Abracead.GetValue(Race, Abidx)
66	Minimum += Add
67	Maximum += Add
68
69	if Abclsmod:
70		tmp = Abclsmod.GetValue(KitIndex, Abidx)
71		Maximum += tmp
72		Add += tmp
73
74	tmp = Abclasrq.GetValue(KitIndex, Abidx)
75	if tmp != 0 and tmp > Minimum:
76		Minimum = tmp
77
78	if Minimum < 1:
79		Minimum = 1
80	if Maximum > 25:
81		Maximum = 25
82
83	return
84
85def RollPress():
86	global Minimum, Maximum, Add, HasStrExtra, PointsLeft
87	global AllPoints18
88
89	GemRB.SetVar("Ability",0)
90	GemRB.SetVar("Ability -1",0)
91	PointsLeft = 0
92	SumLabel = AbilityWindow.GetControl(0x10000002)
93	SumLabel.SetText("0")
94
95	if HasStrExtra:
96		if AllPoints18:
97			e = 100
98		else:
99			e = GemRB.Roll(1,100,0)
100	else:
101		e = 0
102	GemRB.SetVar("StrExtra", e)
103
104	Total = 0
105	while (Total < 75):
106		Total = 0
107		for i in range(6):
108			dice = 3
109			size = 5
110			CalcLimits(i)
111			v = GemRB.Roll(dice, size, Add+3)
112			if v<Minimum:
113				v = Minimum
114			if v>Maximum:
115				v = Maximum
116			if AllPoints18:
117				v = 18
118			GemRB.SetVar("Ability "+str(i), v )
119			Total += v
120
121			Label = AbilityWindow.GetControl(0x10000003+i)
122			if i==0 and v==18 and HasStrExtra:
123				Label.SetText("18/"+str(e) )
124			else:
125				Label.SetText(str(v) )
126
127	# add a counter to the title
128	SumLabel = AbilityWindow.GetControl (0x10000000)
129	SumLabel.SetText(GemRB.GetString(11976) + ": " + str(Total))
130	AllPoints18 = 0
131	return
132
133def GiveAll18():
134	global AllPoints18
135
136	AllPoints18 = 1
137	RollPress()
138
139def OnLoad():
140	global AbilityWindow, TextAreaControl, DoneButton
141	global PointsLeft, HasStrExtra
142	global AbilityTable, Abclasrq, Abclsmod, Abracerq, Abracead
143	global KitIndex, Minimum, Maximum, MyChar
144	global AllPoints18
145
146	AllPoints18 = 0
147
148	Abracead = GemRB.LoadTable("ABRACEAD")
149	if GemRB.HasResource ("ABCLSMOD", RES_2DA):
150		Abclsmod = GemRB.LoadTable ("ABCLSMOD")
151	Abclasrq = GemRB.LoadTable("ABCLASRQ")
152	Abracerq = GemRB.LoadTable("ABRACERQ")
153
154	MyChar = GemRB.GetVar ("Slot")
155	Kit = GUICommon.GetKitIndex (MyChar)
156	ClassName = GUICommon.GetClassRowName (MyChar)
157	if Kit == 0:
158		KitName = ClassName
159	else:
160		#rowname is just a number, first value row what we need here
161		KitName = CommonTables.KitList.GetValue(Kit, 0)
162
163	#if the class uses the warrior table for saves, then it may have the extra strength
164	HasStrExtra = CommonTables.Classes.GetValue(ClassName, "STREXTRA", GTV_INT)
165
166	KitIndex = Abclasrq.GetRowIndex(KitName)
167
168	AbilityTable = GemRB.LoadTable("ability")
169	AbilityWindow = GemRB.LoadWindow(4, "GUICG")
170	CharGenCommon.PositionCharGenWin(AbilityWindow)
171
172	Button = AbilityWindow.CreateButton (2000, 0, 0, 0, 0)
173	Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, GiveAll18)
174	Button.SetHotKey ("8", 3, True) # TODO: make defines for CTRL and SHIFT modifiers
175
176	RerollButton = AbilityWindow.GetControl(2)
177	RerollButton.SetText(11982)
178	StoreButton = AbilityWindow.GetControl(37)
179	StoreButton.SetText(17373)
180	RecallButton = AbilityWindow.GetControl(38)
181	RecallButton.SetText(17374)
182
183	BackButton = AbilityWindow.GetControl(36)
184	BackButton.SetText(15416)
185	BackButton.MakeEscape()
186	DoneButton = AbilityWindow.GetControl(0)
187	DoneButton.SetText(11973)
188	DoneButton.MakeDefault()
189	DoneButton.SetState(IE_GUI_BUTTON_ENABLED)
190
191	RollPress()
192	StorePress()
193	for i in range(6):
194		Button = AbilityWindow.GetControl(i+30)
195		Button.SetEvent(IE_GUI_BUTTON_ON_PRESS, JustPress)
196		Button.SetEvent(IE_GUI_MOUSE_LEAVE_BUTTON, EmptyPress)
197		Button.SetVarAssoc("Ability", i)
198		# delete the labels and use the buttons instead
199		AbilityWindow.DeleteControl (i+0x10000009)
200		Button.SetText (AbilityTable.GetValue (i, 0))
201		Button.SetFlags (IE_GUI_BUTTON_ALIGN_RIGHT, OP_OR)
202		Button.SetState (IE_GUI_BUTTON_LOCKED)
203
204		Button = AbilityWindow.GetControl(i*2+16)
205		Button.SetEvent(IE_GUI_BUTTON_ON_PRESS, LeftPress)
206		Button.SetVarAssoc("Ability", i )
207		Button.SetActionInterval (200)
208
209		Button = AbilityWindow.GetControl(i*2+17)
210		Button.SetEvent(IE_GUI_BUTTON_ON_PRESS, RightPress)
211		Button.SetVarAssoc("Ability", i )
212		Button.SetActionInterval (200)
213
214	TextAreaControl = AbilityWindow.GetControl(29)
215	TextAreaControl.SetText(17247)
216
217	StoreButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, StorePress)
218	RecallButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, RecallPress)
219	RerollButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, RollPress)
220	DoneButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, NextPress)
221	BackButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, BackPress)
222	AbilityWindow.Focus()
223	return
224
225def RightPress(btn, Abidx):
226	global PointsLeft
227
228	Ability = GemRB.GetVar("Ability "+str(Abidx) )
229	CalcLimits(Abidx)
230	GemRB.SetToken("MINIMUM",str(Minimum) )
231	GemRB.SetToken("MAXIMUM",str(Maximum) )
232	TextAreaControl.SetText(AbilityTable.GetValue(Abidx, 1) )
233	if Ability<=Minimum:
234		return
235	GemRB.SetVar("Ability "+str(Abidx), Ability-1)
236	PointsLeft = PointsLeft + 1
237	GemRB.SetVar("Ability -1",PointsLeft)
238	SumLabel = AbilityWindow.GetControl(0x10000002)
239	SumLabel.SetText(str(PointsLeft) )
240	Label = AbilityWindow.GetControl(0x10000003+Abidx)
241	StrExtra = GemRB.GetVar("StrExtra")
242	if Abidx==0 and Ability==19 and StrExtra:
243		Label.SetText("18/"+str(StrExtra) )
244	else:
245		Label.SetText(str(Ability-1) )
246	return
247
248def JustPress(btn, Abidx):
249	CalcLimits(Abidx)
250	GemRB.SetToken("MINIMUM",str(Minimum) )
251	GemRB.SetToken("MAXIMUM",str(Maximum) )
252	TextAreaControl.SetText(AbilityTable.GetValue(Abidx, 1) )
253	return
254
255def LeftPress(btn, Abidx):
256	global PointsLeft
257
258	Ability = GemRB.GetVar("Ability "+str(Abidx) )
259	CalcLimits(Abidx)
260	GemRB.SetToken("MINIMUM",str(Minimum) )
261	GemRB.SetToken("MAXIMUM",str(Maximum) )
262	TextAreaControl.SetText(AbilityTable.GetValue(Abidx, 1) )
263	if PointsLeft == 0:
264		return
265	if Ability>=Maximum:  #should be more elaborate
266		return
267	GemRB.SetVar("Ability "+str(Abidx), Ability+1)
268	PointsLeft = PointsLeft - 1
269	GemRB.SetVar("Ability -1",PointsLeft)
270	SumLabel = AbilityWindow.GetControl(0x10000002)
271	SumLabel.SetText(str(PointsLeft) )
272	Label = AbilityWindow.GetControl(0x10000003+Abidx)
273	StrExtra = GemRB.GetVar("StrExtra")
274	if Abidx==0 and Ability==17 and HasStrExtra==1:
275		Label.SetText("18/%02d"%(StrExtra) )
276	else:
277		Label.SetText(str(Ability+1) )
278	return
279
280def EmptyPress():
281	TextAreaControl = AbilityWindow.GetControl(29)
282	TextAreaControl.SetText(17247)
283	return
284
285def StorePress():
286	GemRB.SetVar("StoredStrExtra",GemRB.GetVar("StrExtra") )
287	for i in range(-1,6):
288		GemRB.SetVar("Stored "+str(i),GemRB.GetVar("Ability "+str(i) ) )
289	return
290
291def RecallPress():
292	global PointsLeft
293
294	e=GemRB.GetVar("StoredStrExtra")
295	GemRB.SetVar("StrExtra",e)
296	Total = 0
297	for i in range(-1,6):
298		v = GemRB.GetVar("Stored "+str(i) )
299		Total += v
300		GemRB.SetVar("Ability "+str(i), v)
301		Label = AbilityWindow.GetControl(0x10000003+i)
302		if i==0 and v==18 and HasStrExtra==1:
303			Label.SetText("18/"+str(e) )
304		else:
305			Label.SetText(str(v) )
306
307	PointsLeft = GemRB.GetVar("Ability -1")
308
309	# add a counter to the title
310	SumLabel = AbilityWindow.GetControl (0x10000000)
311	SumLabel.SetText(GemRB.GetString(11976) + ": " + str(Total))
312	return
313
314def BackPress():
315	if AbilityWindow:
316		AbilityWindow.Unload()
317	GemRB.SetNextScript("CharGen5")
318	GemRB.SetVar("StrExtra",0)
319	for i in range(-1,6):
320		GemRB.SetVar("Ability "+str(i),0)  #scrapping the abilities
321	return
322
323def NextPress():
324	if AbilityWindow:
325		AbilityWindow.Unload()
326	AbilityTable = GemRB.LoadTable ("ability")
327	AbilityCount = AbilityTable.GetRowCount ()
328
329	for i in range (AbilityCount):
330		StatID = AbilityTable.GetValue (i, 3)
331		StatValue = GemRB.GetVar ("Ability "+str(i))
332		GemRB.SetPlayerStat (MyChar, StatID, StatValue)
333
334	GemRB.SetPlayerStat (MyChar, IE_STREXTRA, GemRB.GetVar ("StrExtra"))
335
336	GemRB.SetNextScript("CharGen6")
337	return
338