TickHook: Fix crash when TickHook isn't set.
[gemrb.git] / gemrb / GUIScripts / bg2 / GUICG4.py
blobd20776f94d5df52c4f8616ef458bb4a76f57d8f3
1 # GemRB - Infinity Engine Emulator
2 # Copyright (C) 2003 The GemRB Project
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.
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.
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.
19 #character generation, ability (GUICG4)
20 import GemRB
21 import GUICommon
22 import CommonTables
23 from ie_stats import *
24 from GUIDefines import *
26 AbilityWindow = 0
27 TextAreaControl = 0
28 DoneButton = 0
29 AbilityTable = 0
30 Abclasrq = 0
31 Abclsmod = 0
32 Abclasrq = 0
33 Abracerq = 0
34 PointsLeft = 0
35 Minimum = 0
36 Maximum = 0
37 Add = 0
38 KitIndex = 0
39 HasStrExtra = 0
40 MyChar = 0
42 def CalcLimits(Abidx):
43 global Minimum, Maximum, Add
45 Race = CommonTables.Races.FindValue (3, GemRB.GetPlayerStat (MyChar, IE_RACE) )
46 RaceName = CommonTables.Races.GetRowName(Race)
48 Minimum = 3
49 Maximum = 18
51 tmp = Abclasrq.GetValue(KitIndex, Abidx)
52 if tmp!=0 and tmp>Minimum:
53 Minimum = tmp
55 Race = Abracerq.GetRowIndex(RaceName)
56 tmp = Abracerq.GetValue(Race, Abidx*2)
57 if tmp!=0 and tmp>Minimum:
58 Minimum = tmp
60 tmp = Abracerq.GetValue(Race, Abidx*2+1)
61 if tmp!=0 and tmp>Maximum:
62 Maximum = tmp
64 Race = Abracead.GetRowIndex(RaceName)
65 Add = Abracead.GetValue(Race, Abidx) + Abclsmod.GetValue(KitIndex, Abidx)
66 Maximum = Maximum + Add
67 Minimum = Minimum + Add
68 if Minimum<1:
69 Minimum=1
70 if Maximum>25:
71 Maximum=25
73 return
75 def RollPress():
76 global Minimum, Maximum, Add, HasStrExtra, PointsLeft
78 GemRB.SetVar("Ability",0)
79 GemRB.SetVar("Ability -1",0)
80 PointsLeft = 0
81 SumLabel = AbilityWindow.GetControl(0x10000002)
82 SumLabel.SetText("0")
83 SumLabel.SetUseRGB(1)
85 if HasStrExtra:
86 e = GemRB.Roll(1,100,0)
87 else:
88 e = 0
89 GemRB.SetVar("StrExtra", e)
90 for i in range(6):
91 dice = 3
92 size = 5
93 CalcLimits(i)
94 v = GemRB.Roll(dice, size, Add+3)
95 if v<Minimum:
96 v = Minimum
97 if v>Maximum:
98 v = Maximum
99 GemRB.SetVar("Ability "+str(i), v )
100 Label = AbilityWindow.GetControl(0x10000003+i)
101 if i==0 and v==18 and HasStrExtra:
102 Label.SetText("18/"+str(e) )
103 else:
104 Label.SetText(str(v) )
105 Label.SetUseRGB(1)
106 return
108 def OnLoad():
109 global AbilityWindow, TextAreaControl, DoneButton
110 global PointsLeft, HasStrExtra
111 global AbilityTable, Abclasrq, Abclsmod, Abracerq, Abracead
112 global KitIndex, Minimum, Maximum, MyChar
114 Abracead = GemRB.LoadTable("ABRACEAD")
115 Abclsmod = GemRB.LoadTable("ABCLSMOD")
116 Abclasrq = GemRB.LoadTable("ABCLASRQ")
117 Abracerq = GemRB.LoadTable("ABRACERQ")
119 MyChar = GemRB.GetVar ("Slot")
120 Kit = GUICommon.GetKitIndex (MyChar)
121 Class = GemRB.GetPlayerStat (MyChar, IE_CLASS)
122 Class = CommonTables.Classes.FindValue (5, Class)
123 if Kit == 0:
124 KitName = CommonTables.Classes.GetRowName(Class)
125 else:
126 #rowname is just a number, first value row what we need here
127 KitName = CommonTables.KitList.GetValue(Kit, 0)
129 #if the class uses the warrior table for saves, then it may have the extra strength
130 if CommonTables.Classes.GetValue(Class, 3)=="SAVEWAR":
131 HasStrExtra=1
132 else:
133 HasStrExtra=0
135 KitIndex = Abclasrq.GetRowIndex(KitName)
137 GemRB.LoadWindowPack("GUICG", 640, 480)
138 AbilityTable = GemRB.LoadTable("ability")
139 AbilityWindow = GemRB.LoadWindow(4)
141 RerollButton = AbilityWindow.GetControl(2)
142 RerollButton.SetText(11982)
143 StoreButton = AbilityWindow.GetControl(37)
144 StoreButton.SetText(17373)
145 RecallButton = AbilityWindow.GetControl(38)
146 RecallButton.SetText(17374)
148 BackButton = AbilityWindow.GetControl(36)
149 BackButton.SetText(15416)
150 BackButton.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
151 DoneButton = AbilityWindow.GetControl(0)
152 DoneButton.SetText(11973)
153 DoneButton.SetFlags(IE_GUI_BUTTON_DEFAULT,OP_OR)
154 DoneButton.SetState(IE_GUI_BUTTON_ENABLED)
156 RollPress()
157 StorePress()
158 for i in range(6):
159 Label = AbilityWindow.GetControl(i+0x10000009)
160 Label.SetEvent(IE_GUI_LABEL_ON_PRESS, eval("OverPress"+str(i)))
161 Button = AbilityWindow.GetControl(i+30)
162 Button.SetEvent(IE_GUI_BUTTON_ON_PRESS, JustPress)
163 Button.SetEvent(IE_GUI_MOUSE_LEAVE_BUTTON, EmptyPress)
164 Button.SetVarAssoc("Ability", i)
166 Button = AbilityWindow.GetControl(i*2+16)
167 Button.SetEvent(IE_GUI_BUTTON_ON_PRESS, LeftPress)
168 Button.SetVarAssoc("Ability", i )
170 Button = AbilityWindow.GetControl(i*2+17)
171 Button.SetEvent(IE_GUI_BUTTON_ON_PRESS, RightPress)
172 Button.SetVarAssoc("Ability", i )
174 TextAreaControl = AbilityWindow.GetControl(29)
175 TextAreaControl.SetText(17247)
177 StoreButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, StorePress)
178 RecallButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, RecallPress)
179 RerollButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, RollPress)
180 DoneButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, NextPress)
181 BackButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, BackPress)
182 AbilityWindow.SetVisible(WINDOW_VISIBLE)
183 GemRB.SetRepeatClickFlags(GEM_RK_DISABLE, OP_NAND)
184 return
186 def RightPress():
187 global PointsLeft
189 Abidx = GemRB.GetVar("Ability")
190 Ability = GemRB.GetVar("Ability "+str(Abidx) )
191 CalcLimits(Abidx)
192 GemRB.SetToken("MINIMUM",str(Minimum) )
193 GemRB.SetToken("MAXIMUM",str(Maximum) )
194 TextAreaControl.SetText(AbilityTable.GetValue(Abidx, 1) )
195 if Ability<=Minimum:
196 return
197 GemRB.SetVar("Ability "+str(Abidx), Ability-1)
198 PointsLeft = PointsLeft + 1
199 GemRB.SetVar("Ability -1",PointsLeft)
200 SumLabel = AbilityWindow.GetControl(0x10000002)
201 SumLabel.SetText(str(PointsLeft) )
202 Label = AbilityWindow.GetControl(0x10000003+Abidx)
203 StrExtra = GemRB.GetVar("StrExtra")
204 if Abidx==0 and Ability==19 and StrExtra:
205 Label.SetText("18/"+str(StrExtra) )
206 else:
207 Label.SetText(str(Ability-1) )
208 return
210 def JustPress():
211 Abidx = GemRB.GetVar("Ability")
212 Ability = GemRB.GetVar("Ability "+str(Abidx) )
213 CalcLimits(Abidx)
214 GemRB.SetToken("MINIMUM",str(Minimum) )
215 GemRB.SetToken("MAXIMUM",str(Maximum) )
216 TextAreaControl.SetText(AbilityTable.GetValue(Abidx, 1) )
217 return
219 def LeftPress():
220 global PointsLeft
222 Abidx = GemRB.GetVar("Ability")
223 Ability = GemRB.GetVar("Ability "+str(Abidx) )
224 CalcLimits(Abidx)
225 GemRB.SetToken("MINIMUM",str(Minimum) )
226 GemRB.SetToken("MAXIMUM",str(Maximum) )
227 TextAreaControl.SetText(AbilityTable.GetValue(Abidx, 1) )
228 if PointsLeft == 0:
229 return
230 if Ability>=Maximum: #should be more elaborate
231 return
232 GemRB.SetVar("Ability "+str(Abidx), Ability+1)
233 PointsLeft = PointsLeft - 1
234 GemRB.SetVar("Ability -1",PointsLeft)
235 SumLabel = AbilityWindow.GetControl(0x10000002)
236 SumLabel.SetText(str(PointsLeft) )
237 Label = AbilityWindow.GetControl(0x10000003+Abidx)
238 StrExtra = GemRB.GetVar("StrExtra")
239 if Abidx==0 and Ability==17 and HasStrExtra==1:
240 Label.SetText("18/%02d"%(StrExtra) )
241 else:
242 Label.SetText(str(Ability+1) )
243 return
245 def EmptyPress():
246 TextAreaControl = AbilityWindow.GetControl(29)
247 TextAreaControl.SetText(17247)
248 return
250 def StorePress():
251 GemRB.SetVar("StoredStrExtra",GemRB.GetVar("StrExtra") )
252 for i in range(-1,6):
253 GemRB.SetVar("Stored "+str(i),GemRB.GetVar("Ability "+str(i) ) )
254 return
256 def RecallPress():
257 global PointsLeft
259 e=GemRB.GetVar("StoredStrExtra")
260 GemRB.SetVar("StrExtra",e)
261 for i in range(-1,6):
262 v = GemRB.GetVar("Stored "+str(i) )
263 GemRB.SetVar("Ability "+str(i), v)
264 Label = AbilityWindow.GetControl(0x10000003+i)
265 if i==0 and v==18 and HasStrExtra==1:
266 Label.SetText("18/"+str(e) )
267 else:
268 Label.SetText(str(v) )
270 PointsLeft = GemRB.GetVar("Ability -1")
271 return
273 def BackPress():
274 if AbilityWindow:
275 AbilityWindow.Unload()
276 GemRB.SetNextScript("CharGen5")
277 GemRB.SetVar("StrExtra",0)
278 for i in range(-1,6):
279 GemRB.SetVar("Ability "+str(i),0) #scrapping the abilities
280 GemRB.SetRepeatClickFlags(GEM_RK_DISABLE, OP_OR)
281 return
283 def NextPress():
284 if AbilityWindow:
285 AbilityWindow.Unload()
286 AbilityTable = GemRB.LoadTable ("ability")
287 AbilityCount = AbilityTable.GetRowCount ()
289 # print our diagnostic as we loop (so as not to duplicate)
290 for i in range (AbilityCount):
291 StatID = AbilityTable.GetValue (i, 3)
292 StatName = AbilityTable.GetRowName (i)
293 StatValue = GemRB.GetVar ("Ability "+str(i))
294 GemRB.SetPlayerStat (MyChar, StatID, StatValue)
295 print "\t",StatName,":\t", StatValue
297 GemRB.SetPlayerStat (MyChar, IE_STREXTRA, GemRB.GetVar ("StrExtra"))
298 print "\tSTREXTRA:\t",GemRB.GetVar ("StrExtra")
300 GemRB.SetRepeatClickFlags(GEM_RK_DISABLE, OP_OR)
301 GemRB.SetNextScript("CharGen6")
302 return
304 def OverPress0():
305 Ability = GemRB.GetVar("Ability 0")
306 CalcLimits(0)
307 GemRB.SetToken("MINIMUM",str(Minimum) )
308 GemRB.SetToken("MAXIMUM",str(Maximum) )
309 TextAreaControl.SetText(AbilityTable.GetValue(0, 1) )
310 return
312 def OverPress1():
313 Ability = GemRB.GetVar("Ability 1")
314 CalcLimits(1)
315 GemRB.SetToken("MINIMUM",str(Minimum) )
316 GemRB.SetToken("MAXIMUM",str(Maximum) )
317 TextAreaControl.SetText(AbilityTable.GetValue(1, 1) )
318 return
320 def OverPress2():
321 Ability = GemRB.GetVar("Ability 2")
322 CalcLimits(2)
323 GemRB.SetToken("MINIMUM",str(Minimum) )
324 GemRB.SetToken("MAXIMUM",str(Maximum) )
325 TextAreaControl.SetText(AbilityTable.GetValue(2, 1) )
326 return
328 def OverPress3():
329 Ability = GemRB.GetVar("Ability 3")
330 CalcLimits(3)
331 GemRB.SetToken("MINIMUM",str(Minimum) )
332 GemRB.SetToken("MAXIMUM",str(Maximum) )
333 TextAreaControl.SetText(AbilityTable.GetValue(3, 1) )
334 return
336 def OverPress4():
337 Ability = GemRB.GetVar("Ability 4")
338 CalcLimits(4)
339 GemRB.SetToken("MINIMUM",str(Minimum) )
340 GemRB.SetToken("MAXIMUM",str(Maximum) )
341 TextAreaControl.SetText(AbilityTable.GetValue(4, 1) )
342 return
344 def OverPress5():
345 Ability = GemRB.GetVar("Ability 5")
346 CalcLimits(5)
347 GemRB.SetToken("MINIMUM",str(Minimum) )
348 GemRB.SetToken("MAXIMUM",str(Maximum) )
349 TextAreaControl.SetText(AbilityTable.GetValue(5, 1) )
350 return