fixed the rest of the functions that needed globalid capabilities
[gemrb.git] / gemrb / GUIScripts / bg2 / LUHLASelection.py
blob3070693d775a1c1a1489a103cbbf7ec0f402aa92
1 # -*-python-*-
2 # GemRB - Infinity Engine Emulator
3 # Copyright (C) 2003-2004 The GemRB Project
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.
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.
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.
20 import GemRB
21 from math import ceil
22 from GUIDefines import *
23 from ie_stats import *
24 import GUICommon
25 import CommonTables
27 # HLA selection
28 HLAWindow = 0 # << HLA selection window
29 HLAAbilities = [] # << all learnable HLA abilities
30 HLANewAbilities = [] # << selected HLA abilites
31 HLADoneButton = 0 # << done button
32 HLATextArea = 0 # << HLA ability description area
33 HLACount = 0 # << number of HLA selections left
34 pc = 0 # << the pc
35 NumClasses = 0 # << number of classes
36 Classes = [] # << classes (ids)
37 Level = [] # << levels for each class
38 EnhanceGUI = 0 # << toggle for scrollbar and 25th hla slot
40 def OpenHLAWindow (actor, numclasses, classes, levels):
41 """Opens the HLA selection window."""
43 global HLAWindow, HLADoneButton, HLATextArea, HLACount, NumClasses, pc, Classes, Level
44 global EnhanceGUI
46 #enhance GUI?
47 if (GemRB.GetVar("GUIEnhancements")):
48 EnhanceGUI = 1
50 # save our variables
51 pc = actor
52 NumClasses = numclasses
53 Classes = classes
54 Level = levels
55 HLACount = GemRB.GetVar ("HLACount")
57 # we use the same window as sorcerer spell selection
58 HLAWindow = GemRB.LoadWindow (8)
60 # get all our HLAs (stored in HLAAbilities)
61 GetHLAs ()
63 # change the title to ABILITIES
64 TitleLabel = HLAWindow.GetControl (0x10000017)
65 TitleLabel.SetText (63818)
67 # create the done button
68 HLADoneButton = HLAWindow.GetControl (28)
69 HLADoneButton.SetState(IE_GUI_BUTTON_DISABLED)
70 HLADoneButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, HLADonePress)
71 HLADoneButton.SetText(11973)
72 HLADoneButton.SetFlags(IE_GUI_BUTTON_DEFAULT, OP_OR)
74 # setup our text area
75 HLATextArea = HLAWindow.GetControl(26)
77 print "Number of HLAs:",len (HLAAbilities)
79 # create a scrollbar if need-be
80 if ( len (HLAAbilities) >= 25 ) and EnhanceGUI:
81 # setup extra 25th HLA slot:
82 HLAWindow.CreateButton (24, 231, 345, 42, 42)
83 if ( len (HLAAbilities) > 25):
84 # setup our scroll index
85 GemRB.SetVar("HLATopIndex", 0)
86 # setup scrollbar
87 HLAWindow.CreateScrollBar (1000, 290,142, 16,252)
88 ScrollBar = HLAWindow.GetControl (1000)
89 ScrollBar.SetSprites ("GUISCRCW", 0, 0,1,2,3,5,4)
90 ScrollBar.SetEvent (IE_GUI_SCROLLBAR_ON_CHANGE, HLAShowAbilities)
91 #with enhanced GUI we have 5 rows of 5 abilities (the last one is 'the extra slot')
92 ScrollBar.SetVarAssoc ("HLATopIndex", int ( ceil ( ( len (HLAAbilities)-25 ) / 5.0 ) ) + 1 )
93 ScrollBar.SetDefaultScrollBar ()
95 # draw our HLAs and show the window
96 HLAShowAbilities ()
97 HLAWindow.ShowModal (MODAL_SHADOW_GRAY)
99 return
101 def HLADonePress ():
102 """Saves the new HLAs and closes the HLA selection window."""
104 # save all of our HLAs
105 for i in range (len (HLANewAbilities)):
106 # see if we're going to learn this ability
107 if HLANewAbilities[i] == 0:
108 continue
110 # figure out the ability type
111 HLARef = HLAAbilities[i][0]
112 HLAType = HLARef[5:7]
113 if HLAType == "PR":
114 HLAType = IE_SPELL_TYPE_PRIEST
115 HLALevel = int(HLARef[7])-1
116 elif HLAType == "WI":
117 HLAType = IE_SPELL_TYPE_WIZARD
118 HLALevel = int(HLARef[7])-1
119 else:
120 HLAType = IE_SPELL_TYPE_INNATE
121 HLALevel = 0
123 # do we need to apply or learn it?
124 if HLARef[:2] == "AP":
125 GemRB.ApplySpell(pc, HLARef[3:])
126 elif HLARef[:2] == "GA":
127 # make sure it isn't already learned
128 SpellIndex = GUICommon.HasSpell (pc, HLAType, HLALevel, HLARef[3:])
129 if SpellIndex < 0: # gotta learn it
130 GemRB.LearnSpell (pc, HLARef[3:], 8)
131 else: # memorize it again
132 GemRB.MemorizeSpell (pc, HLAType, HLALevel, SpellIndex)
134 #save the number of this HLA memorized
135 #TODO: check param2 (0 seems to work ok)
136 GemRB.ApplyEffect(pc, "HLA", HLAAbilities[i][2], 0, HLARef[3:])
138 # close the window
139 if HLAWindow:
140 HLAWindow.Unload ()
142 # so redraw skills knows we're done
143 GemRB.SetVar ("HLACount", 0)
145 return
147 def HLAShowAbilities ():
148 """Updates the HLA selections window.
150 Called whenever an HLA is pressed."""
152 j = ( GemRB.GetVar("HLATopIndex") + 1 ) * 5 - 5
154 # we have a grid of 24 abilites
155 for i in range (24+EnhanceGUI):
156 # ensure we can learn this many abilites
157 if len (HLAAbilities) < 25 and i == 24: #break if we don't need extra 25th button
158 break
159 SpellButton = HLAWindow.GetControl (i)
160 if i + j >= len (HLAAbilities):
161 SpellButton.SetState (IE_GUI_BUTTON_DISABLED)
162 SpellButton.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_SET)
163 continue
164 else:
165 SpellButton.SetState(IE_GUI_BUTTON_ENABLED)
166 SpellButton.SetFlags(IE_GUI_BUTTON_NO_IMAGE, OP_NAND)
168 # fill in the button with the spell data
169 HLARef = HLAAbilities[i+j][0][3:]
170 if not HLARef:
171 continue
172 Spell = GemRB.GetSpell (HLARef)
173 SpellButton.SetTooltip(Spell['SpellName'])
174 SpellButton.SetSpellIcon(HLARef, 1)
175 SpellButton.SetVarAssoc("ButtonPressed", i)
176 SpellButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, HLASelectPress)
177 SpellButton.SetSprites("GUIBTBUT", 0,0,1,2,3)
178 SpellButton.SetFlags(IE_GUI_BUTTON_PICTURE, OP_OR)
180 # don't allow the selection of an un-learnable ability
181 if HLAAbilities[i+j][1] == 0:
182 SpellButton.SetState(IE_GUI_BUTTON_LOCKED)
183 # shade red
184 SpellButton.SetBorder (0, 0,0, 0,0, 200,0,0,100, 1,1)
185 else:
186 SpellButton.SetState (IE_GUI_BUTTON_ENABLED)
187 # unset any borders on this button or an un-learnable from last level
188 # will still shade red even though it is clickable
189 SpellButton.SetBorder (0, 0,0, 0,0, 0,0,0,0, 0,0)
191 # show which spells are selected
192 HLAShowSelectedAbilities ()
194 GemRB.SetToken("number", str(HLACount))
195 HLATextArea.SetText(63817)
197 # show the points left
198 PointsLeftLabel = HLAWindow.GetControl (0x10000018)
199 PointsLeftLabel.SetText (str (HLACount))
201 return
203 def HLASelectPress ():
204 """Toggles the HLA and displays a description string."""
206 global HLACount, HLAAbilities, HLANewAbilities
208 # get our variables
209 j = ( GemRB.GetVar("HLATopIndex") + 1 ) * 5 - 5
210 i = GemRB.GetVar ("ButtonPressed") + j
212 # get the spell that's been pushed
213 Spell = GemRB.GetSpell (HLAAbilities[i][0][3:])
214 HLATextArea.SetText (Spell["SpellDesc"])
216 # make sure we can learn the spell
217 if HLAAbilities[i][1]:
218 if HLANewAbilities[i]: # already picked -- unselecting
219 # make we aren't the pre-req to another spell that is selected
220 for j in range (len (HLAAbilities)):
221 if (HLAAbilities[j][3] == HLAAbilities[i][0]) and (HLANewAbilities[j]):
222 HLAShowSelectedAbilities () # so our pre-req is still highlighted
223 return
225 HLACount += 1
226 HLANewAbilities[i] = 0
227 HLAAbilities[i][2] -= 1 # internal counter
228 HLADoneButton.SetState (IE_GUI_BUTTON_DISABLED)
229 else: # selecting
230 # we don't have any picks left
231 if HLACount == 0:
232 HLAMarkButton (i, 0)
233 return
235 # select the spell and change the done state if need be
236 HLACount -= 1
237 HLANewAbilities[i] = 1
238 HLAAbilities[i][2] += 1 # increment internal counter
239 if HLACount == 0:
240 HLADoneButton.SetState (IE_GUI_BUTTON_ENABLED)
242 # recheck internal exclusions and prereqs
243 HLARecheckPrereqs (i)
245 # show selected spells
246 HLAShowAbilities ()
247 HLAShowSelectedAbilities ()
248 HLATextArea.SetText (Spell["SpellDesc"])
250 # show the points left
251 PointsLeftLabel = HLAWindow.GetControl (0x10000018)
252 PointsLeftLabel.SetText (str (HLACount))
253 return
255 def HLAShowSelectedAbilities ():
256 """Marks all of the selected abilities."""
258 j = ( GemRB.GetVar("HLATopIndex") + 1 ) * 5 - 5
260 # mark all of the abilities picked thus far
261 for i in range (24+EnhanceGUI):
262 if i + j >= len (HLANewAbilities): # make sure we don't call unavailable indexes
263 break
264 if HLANewAbilities[i+j]:
265 HLAMarkButton (i+j, 1)
266 else:
267 HLAMarkButton (i+j, 0)
269 return
271 def HLAMarkButton (i, select):
272 """Enables, disables, or highlights the given button.
274 If select is true, the button is highlighted."""
276 j = ( GemRB.GetVar("HLATopIndex") + 1 ) * 5 - 5
278 if select:
279 type = IE_GUI_BUTTON_SELECTED
280 else:
281 if HLAAbilities[i][1]:
282 type = IE_GUI_BUTTON_ENABLED
283 else: # can't learn
284 type = IE_GUI_BUTTON_LOCKED
286 # we have to use the index on the actual grid
287 SpellButton = HLAWindow.GetControl(i-j)
288 SpellButton.SetState(type)
289 return
291 def GetHLAs ():
292 """Updates HLAAbilites with all the choosable class HLAs.
294 HLAAbilities[x][0] is the given HLAs spell reference.
295 HLAAbilities[x][1] is true if the HLAs prerequisites have been met."""
297 global HLAAbilities, HLANewAbilities, HLACount
299 # get some needed values
300 Kit = GUICommon.GetKitIndex (pc)
301 IsDual = GUICommon.IsDualClassed (pc, 0)
302 IsDual = IsDual[0] > 0
303 MaxHLACount = 0
305 # reset the abilities
306 HLAAbilities = []
307 HLANewAbilities = []
309 # the HLA table lookup table
310 HLAAbbrTable = GemRB.LoadTable ("luabbr")
312 # get all the HLAs for each class
313 for i in range (NumClasses):
314 ClassIndex = CommonTables.Classes.FindValue (5, Classes[i])
315 ClassName = CommonTables.Classes.GetRowName (ClassIndex)
316 CurrentLevel = Level[i]
318 if Kit != 0 and NumClasses == 1 and not IsDual: # kitted single-class
319 KitName = CommonTables.KitList.GetValue (Kit, 0)
320 HLAClassTable = "lu" + HLAAbbrTable.GetValue (KitName, "ABBREV")
321 ClassName = KitName
322 else: # everyone else
323 HLAClassTable = "lu" + HLAAbbrTable.GetValue (ClassName, "ABBREV")
325 # actually load the table
326 HLAClassTable = GemRB.LoadTable (HLAClassTable)
327 print "HLA Class/Kit:",ClassName
329 # save all our HLAs from this class
330 for j in range (HLAClassTable.GetRowCount ()):
331 HLARef = HLAClassTable.GetValue (j, 0, 0)
332 print "\tHLA",j,":",HLARef
334 # make sure we have an ability here
335 if HLARef == "*":
336 print "\t\tEnd of HLAs"
337 break
339 # [ref to hla, memorizable?, num memorized, pre-req ref, excluded ref]
340 SaveArray = [\
341 HLARef,\
343 GemRB.CountEffects (pc, "HLA", -1, -1, HLARef[3:]),\
344 HLAClassTable.GetValue (j, 6, 0),\
345 HLAClassTable.GetValue (j, 7, 0)]
347 # make sure we fall within the min and max paramaters
348 if HLAClassTable.GetValue (j, 3) > CurrentLevel or HLAClassTable.GetValue (j, 4) < CurrentLevel:
349 print "\t\tNot within parameters"
350 HLAAbilities.append(SaveArray)
351 continue
353 # see if we're alignment restricted (we never get them)
354 HLAAlign = HLAClassTable.GetValue (j, 8, 0)
355 if HLAAlign == "ALL_EVIL" and GemRB.GetPlayerStat (pc, IE_ALIGNMENT) < 6:
356 # don't even save this one because we can never get it
357 print "\t\tNeeds ALL_EVIL"
358 continue
359 elif HLAAlign == "ALL_GOOD" and GemRB.GetPlayerStat (pc, IE_ALIGNMENT) > 2:
360 # ditto
361 print "\t\tNeeds ALL_GOOD"
362 continue
364 # make sure we haven't already surpassed the number of time memorizable
365 HLANumAllowed = HLAClassTable.GetValue (j, 5)
366 print "\t\tHLA count:",SaveArray[2]
367 if SaveArray[2] >= HLANumAllowed:
368 print "\t\tOnly allowed to learn",HLANumAllowed,"times"
369 HLAAbilities.append(SaveArray)
370 continue
372 # make sure we haven't learned an HLA that excludes this one
373 HLAMemorized = GemRB.CountEffects (pc, "HLA", -1, -1, SaveArray[4][3:])
374 print "\t\tHLAExcluded count:",HLAMemorized
375 if (SaveArray[4] != "*") and (HLAMemorized > 0):
376 print "\t\tExcluded by:",SaveArray[4]
377 HLAAbilities.append(SaveArray)
378 continue
380 # we meet the prereqs so we can learn the HLA
381 HLAMemorized = GemRB.CountEffects (pc, "HLA", -1, -1, SaveArray[3][3:])
382 print "\t\tHLAPre count:",HLAMemorized
383 if (SaveArray[3] == "*") or (HLAMemorized > 0):
384 print "\t\tWe can learn it!"
385 MaxHLACount += 1
386 SaveArray[1] = 1
387 HLAAbilities.append (SaveArray)
388 continue
390 # we didn't meet prereqs :(
391 print "\t\tNeed pre-req:",SaveArray[3]
392 HLAAbilities.append (SaveArray)
394 # create an array to store our abilities as they are selected
395 HLANewAbilities = [0]*len (HLAAbilities)
397 #make sure we don't get stuck with HLAs we can't apply
398 if MaxHLACount < HLACount:
399 HLACount = MaxHLACount
400 GemRB.SetVar ("HLACount", HLACount)
402 return
404 def HLARecheckPrereqs (index):
405 """Rechecks the HLA prequisites on the index on the fly."""
407 # the numer of times memorized
408 Ref = HLAAbilities[index][0]
409 Memorized = HLAAbilities[index][2]
411 # check for new exclusions and pre-reqs
412 for i in range (len (HLAAbilities)):
413 # we don't need to check the index
414 # this also fixes the assassination bug (it is excluded by itself)
415 if i == index:
416 continue
417 # check for exclusions first
418 if HLAAbilities[i][4] == Ref:
419 if Memorized > 0: # can't learn it
420 HLAAbilities[i][1] = 0
421 else: # can, if it meets pre-reqs
422 if HLAAbilities[i][3] != "*": # check prereqs
423 for j in range (len (HLAAblities)): # search for the prereq ref
424 if (HLAAbilities[j][0] == HLAAbilities[i][3]) and (HLAAbilities[j][2] > 0): # can learn
425 HLAAbilities[i][1] = 1
426 break
427 else: # no prereqs
428 HLAAbilities[i][1] = 1
430 # check for prereqs
431 if HLAAbilities[i][3] == Ref:
432 if Memorized > 0: # can learn if not excluded
433 if HLAAbilities[i][4] != "*": # check for exclusions
434 for j in range (len (HLAAbilities)): # search for the exclusion ref
435 if (HLAAbilities[j][0] == HLAAbilities[i][4]) and (HLAAbilities[j][2] <= 0): # can learn
436 HLAAbilities[i][1] = 1
437 break
438 else: # no exlusions
439 HLAAbilities[i][1] = 1
440 else: # prereqs not met
441 HLAAbilities[i][1] = 0
443 return