TickHook: Fix crash when TickHook isn't set.
[gemrb.git] / gemrb / GUIScripts / bg1 / GUICG4.py
blob7ec64a161818736c4560e5143183360dcdcfa138
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 from GUIDefines import *
22 from ie_stats import *
23 import CharGenCommon
24 import GUICommon
25 import CommonTables
27 AbilityWindow = 0
28 TextAreaControl = 0
29 DoneButton = 0
30 AbilityTable = 0
31 Abclasrq = 0
32 Abclsmod = 0
33 Abclasrq = 0
34 Abracerq = 0
35 PointsLeft = 0
36 Minimum = 0
37 Maximum = 0
38 Add = 0
39 KitIndex = 0
40 HasStrExtra = 0
41 MyChar = 0
43 def CalcLimits(Abidx):
44 global Minimum, Maximum, Add
46 Race = CommonTables.Races.FindValue (3, GemRB.GetPlayerStat (MyChar, IE_RACE) )
47 RaceName = CommonTables.Races.GetRowName(Race)
49 Minimum = 3
50 Maximum = 18
52 tmp = Abclasrq.GetValue(KitIndex, Abidx)
53 if tmp!=0 and tmp>Minimum:
54 Minimum = tmp
56 Race = Abracerq.GetRowIndex(RaceName)
57 tmp = Abracerq.GetValue(Race, Abidx*2)
58 if tmp!=0 and tmp>Minimum:
59 Minimum = tmp
61 tmp = Abracerq.GetValue(Race, Abidx*2+1)
62 if tmp!=0 and tmp>Maximum:
63 Maximum = tmp
65 Race = Abracead.GetRowIndex(RaceName)
66 Add = Abracead.GetValue(Race, Abidx)# + Abclsmod.GetValue(KitIndex, Abidx)
67 Maximum = Maximum + Add
68 Minimum = Minimum + Add
69 if Minimum<1:
70 Minimum=1
71 if Maximum>25:
72 Maximum=25
74 return
76 def RollPress():
77 global Minimum, Maximum, Add, HasStrExtra, PointsLeft
79 GemRB.SetVar("Ability",0)
80 GemRB.SetVar("Ability -1",0)
81 PointsLeft = 0
82 SumLabel = AbilityWindow.GetControl(0x10000002)
83 SumLabel.SetText("0")
84 SumLabel.SetUseRGB(1)
86 if HasStrExtra:
87 e = GemRB.Roll(1,100,0)
88 else:
89 e = 0
90 GemRB.SetVar("StrExtra", e)
91 for i in range(6):
92 dice = 3
93 size = 5
94 CalcLimits(i)
95 v = GemRB.Roll(dice, size, Add+3)
96 if v<Minimum:
97 v = Minimum
98 if v>Maximum:
99 v = Maximum
100 GemRB.SetVar("Ability "+str(i), v )
101 Label = AbilityWindow.GetControl(0x10000003+i)
102 if i==0 and v==18 and HasStrExtra:
103 Label.SetText("18/"+str(e) )
104 else:
105 Label.SetText(str(v) )
106 Label.SetUseRGB(1)
107 return
109 def OnLoad():
110 global AbilityWindow, TextAreaControl, DoneButton
111 global PointsLeft, HasStrExtra
112 global AbilityTable, Abclasrq, Abclsmod, Abracerq, Abracead
113 global KitIndex, Minimum, Maximum, MyChar
115 if GUICommon.CloseOtherWindow (OnLoad):
116 if(AbilityWindow):
117 AbilityWindow.Unload()
118 AbilityWindow = None
119 return
121 Abracead = GemRB.LoadTable("ABRACEAD")
122 #Abclsmod = GemRB.LoadTable("ABCLSMOD")
123 Abclasrq = GemRB.LoadTable("ABCLASRQ")
124 Abracerq = GemRB.LoadTable("ABRACERQ")
126 MyChar = GemRB.GetVar ("Slot")
127 Kit = GUICommon.GetKitIndex (MyChar)
128 Class = GemRB.GetPlayerStat (MyChar, IE_CLASS)
129 Class = CommonTables.Classes.FindValue (5, Class)
130 if Kit == 0:
131 KitName = CommonTables.Classes.GetRowName(Class)
132 else:
133 #rowname is just a number, first value row what we need here
134 KitName = CommonTables.KitList.GetValue(Kit, 0)
136 #if the class uses the warrior table for saves, then it may have the extra strength
137 if CommonTables.Classes.GetValue(Class, 3)=="SAVEWAR":
138 HasStrExtra=1
139 else:
140 HasStrExtra=0
142 KitIndex = Abclasrq.GetRowIndex(KitName)
144 GemRB.LoadWindowPack("GUICG", 640, 480)
145 AbilityTable = GemRB.LoadTable("ability")
146 AbilityWindow = GemRB.LoadWindow(4)
148 RerollButton = AbilityWindow.GetControl(2)
149 RerollButton.SetText(11982)
150 StoreButton = AbilityWindow.GetControl(37)
151 StoreButton.SetText(17373)
152 RecallButton = AbilityWindow.GetControl(38)
153 RecallButton.SetText(17374)
155 BackButton = AbilityWindow.GetControl(36)
156 BackButton.SetText(15416)
157 BackButton.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
158 DoneButton = AbilityWindow.GetControl(0)
159 DoneButton.SetText(11973)
160 DoneButton.SetFlags(IE_GUI_BUTTON_DEFAULT,OP_OR)
161 DoneButton.SetState(IE_GUI_BUTTON_ENABLED)
163 RollPress()
164 StorePress()
165 for i in range(6):
166 Button = AbilityWindow.GetControl(i+30)
167 Button.SetEvent(IE_GUI_BUTTON_ON_PRESS, JustPress)
168 Button.SetVarAssoc("Ability", i)
170 Button = AbilityWindow.GetControl(i*2+16)
171 Button.SetEvent(IE_GUI_BUTTON_ON_PRESS, LeftPress)
172 Button.SetVarAssoc("Ability", i )
174 Button = AbilityWindow.GetControl(i*2+17)
175 Button.SetEvent(IE_GUI_BUTTON_ON_PRESS, RightPress)
176 Button.SetVarAssoc("Ability", i )
178 TextAreaControl = AbilityWindow.GetControl(29)
179 TextAreaControl.SetText(17247)
181 StoreButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, StorePress)
182 RecallButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, RecallPress)
183 RerollButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, RollPress)
184 DoneButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, NextPress)
185 BackButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, BackPress)
186 AbilityWindow.ShowModal(MODAL_SHADOW_NONE)
187 GemRB.SetRepeatClickFlags(GEM_RK_DISABLE, OP_NAND)
188 return
190 def RightPress():
191 global PointsLeft
193 Abidx = GemRB.GetVar("Ability")
194 Ability = GemRB.GetVar("Ability "+str(Abidx) )
195 CalcLimits(Abidx)
196 GemRB.SetToken("MINIMUM",str(Minimum) )
197 GemRB.SetToken("MAXIMUM",str(Maximum) )
198 TextAreaControl.SetText(AbilityTable.GetValue(Abidx, 1) )
199 if Ability<=Minimum:
200 return
201 GemRB.SetVar("Ability "+str(Abidx), Ability-1)
202 PointsLeft = PointsLeft + 1
203 GemRB.SetVar("Ability -1",PointsLeft)
204 SumLabel = AbilityWindow.GetControl(0x10000002)
205 SumLabel.SetText(str(PointsLeft) )
206 Label = AbilityWindow.GetControl(0x10000003+Abidx)
207 StrExtra = GemRB.GetVar("StrExtra")
208 if Abidx==0 and Ability==19 and StrExtra:
209 Label.SetText("18/"+str(StrExtra) )
210 else:
211 Label.SetText(str(Ability-1) )
212 return
214 def JustPress():
215 Abidx = GemRB.GetVar("Ability")
216 Ability = GemRB.GetVar("Ability "+str(Abidx) )
217 CalcLimits(Abidx)
218 GemRB.SetToken("MINIMUM",str(Minimum) )
219 GemRB.SetToken("MAXIMUM",str(Maximum) )
220 TextAreaControl.SetText(AbilityTable.GetValue(Abidx, 1) )
221 return
223 def LeftPress():
224 global PointsLeft
226 Abidx = GemRB.GetVar("Ability")
227 Ability = GemRB.GetVar("Ability "+str(Abidx) )
228 CalcLimits(Abidx)
229 GemRB.SetToken("MINIMUM",str(Minimum) )
230 GemRB.SetToken("MAXIMUM",str(Maximum) )
231 TextAreaControl.SetText(AbilityTable.GetValue(Abidx, 1) )
232 if PointsLeft == 0:
233 return
234 if Ability>=Maximum: #should be more elaborate
235 return
236 GemRB.SetVar("Ability "+str(Abidx), Ability+1)
237 PointsLeft = PointsLeft - 1
238 GemRB.SetVar("Ability -1",PointsLeft)
239 SumLabel = AbilityWindow.GetControl(0x10000002)
240 SumLabel.SetText(str(PointsLeft) )
241 Label = AbilityWindow.GetControl(0x10000003+Abidx)
242 StrExtra = GemRB.GetVar("StrExtra")
243 if Abidx==0 and Ability==17 and HasStrExtra==1:
244 Label.SetText("18/%02d"%(StrExtra) )
245 else:
246 Label.SetText(str(Ability+1) )
247 return
249 def EmptyPress():
250 TextAreaControl = AbilityWindow.GetControl(29)
251 TextAreaControl.SetText(17247)
252 return
254 def StorePress():
255 GemRB.SetVar("StoredStrExtra",GemRB.GetVar("StrExtra") )
256 for i in range(-1,6):
257 GemRB.SetVar("Stored "+str(i),GemRB.GetVar("Ability "+str(i) ) )
258 return
260 def RecallPress():
261 global PointsLeft
263 e=GemRB.GetVar("StoredStrExtra")
264 GemRB.SetVar("StrExtra",e)
265 for i in range(-1,6):
266 v = GemRB.GetVar("Stored "+str(i) )
267 GemRB.SetVar("Ability "+str(i), v)
268 Label = AbilityWindow.GetControl(0x10000003+i)
269 if i==0 and v==18 and HasStrExtra==1:
270 Label.SetText("18/"+str(e) )
271 else:
272 Label.SetText(str(v) )
274 PointsLeft = GemRB.GetVar("Ability -1")
275 return
277 def BackPress():
278 GemRB.SetRepeatClickFlags(GEM_RK_DISABLE, OP_OR)
279 CharGenCommon.back()
281 def NextPress():
282 GemRB.SetRepeatClickFlags(GEM_RK_DISABLE, OP_OR)
284 AbilityTable = GemRB.LoadTable ("ability")
285 AbilityCount = AbilityTable.GetRowCount ()
287 # print our diagnostic as we loop (so as not to duplicate)
288 for i in range (AbilityCount):
289 StatID = AbilityTable.GetValue (i, 3)
290 StatName = AbilityTable.GetRowName (i)
291 StatValue = GemRB.GetVar ("Ability "+str(i))
292 GemRB.SetPlayerStat (MyChar, StatID, StatValue)
293 print "\t",StatName,":\t", StatValue
295 GemRB.SetPlayerStat (MyChar, IE_STREXTRA, GemRB.GetVar ("StrExtra"))
296 print "\tSTREXTRA:\t",GemRB.GetVar ("StrExtra")
298 GemRB.SetRepeatClickFlags(GEM_RK_DISABLE, OP_OR)
300 CharGenCommon.next()