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.
21 # GUIMG.py - scripts to control mage spells windows from the GUIMG winpack
23 ###################################################
26 import GUICommonWindows
27 from GUIDefines
import *
28 from ie_stats
import *
29 from GUICommon
import CloseOtherWindow
, HasTOB
30 from GUICommon
import GameWindow
31 from GUICommonWindows
import *
34 MageSpellInfoWindow
= None
36 MageSpellUnmemorizeWindow
= None
39 OldPortraitWindow
= None
40 OldOptionsWindow
= None
49 def OpenMageWindow ():
50 global MageWindow
, OptionsWindow
, PortraitWindow
51 global OldPortraitWindow
, OldOptionsWindow
53 if CloseOtherWindow (OpenMageWindow
):
57 OptionsWindow
.Unload ()
59 PortraitWindow
.Unload ()
62 GemRB
.SetVar ("OtherWindow", -1)
63 GameWindow
.SetVisible(WINDOW_VISIBLE
)
65 GUICommonWindows
.PortraitWindow
= OldPortraitWindow
66 OldPortraitWindow
= None
67 GUICommonWindows
.OptionsWindow
= OldOptionsWindow
68 OldOptionsWindow
= None
69 SetSelectionChangeHandler (None)
73 GameWindow
.SetVisible(WINDOW_INVISIBLE
)
75 GemRB
.LoadWindowPack ("GUIMG", 640, 480)
77 #saving the original portrait window
78 OldOptionsWindow
= GUICommonWindows
.OptionsWindow
79 OptionsWindow
= GemRB
.LoadWindowObject (0)
80 MarkMenuButton (OptionsWindow
)
81 SetupMenuWindowControls (OptionsWindow
, 0, "OpenMageWindow")
82 OptionsWindow
.SetFrame ()
83 OldPortraitWindow
= GUICommonWindows
.PortraitWindow
85 PortraitWindow
= OpenPortraitWindow (0)
87 SetSelectionChangeHandler (SetupMageWindow
)
90 def SetupMageWindow ():
94 pc
= GemRB
.GameGetSelectedPCSingle ()
96 if (ClassSkillsTable
.GetValue (GemRB
.GetPlayerStat (pc
, IE_CLASS
), 8)==2):
104 MageWindow
= Window
= GemRB
.LoadWindowObject (8)
106 MageWindow
= Window
= GemRB
.LoadWindowObject (2)
107 GemRB
.SetVar ("OtherWindow", MageWindow
.ID
)
109 Button
= Window
.GetControl (1)
110 Button
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "MagePrevLevelPress")
112 Button
= Window
.GetControl (2)
113 Button
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "MageNextLevelPress")
116 Button
= Window
.GetControl (55)
117 Button
.SetState (IE_GUI_BUTTON_LOCKED
)
121 Button
= Window
.GetControl (56 + i
)
122 Button
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "RefreshMageLevel")
123 Button
.SetFlags (IE_GUI_BUTTON_RADIOBUTTON
, OP_OR
)
126 Button
= Window
.GetControl (56 + i
)
127 Button
.SetVarAssoc ("MageSpellLevel", i
)
129 # Setup memorized spells buttons
132 Button
= Window
.GetControl (3 + i
)
133 Button
.SetBorder (0,0,0,0,0,0,0,0,64,0,1)
134 Button
.SetSprites ("SPELFRAM",0,0,0,0,0)
135 Button
.SetFlags (IE_GUI_BUTTON_PICTURE
, OP_OR
)
136 Button
.SetState (IE_GUI_BUTTON_LOCKED
)
138 # Setup book spells buttons
140 Button
= Window
.GetControl (27 + i
)
141 Button
.SetFlags (IE_GUI_BUTTON_NO_IMAGE
, OP_OR
)
142 Button
.SetState (IE_GUI_BUTTON_LOCKED
)
145 OptionsWindow
.SetVisible (WINDOW_VISIBLE
)
146 #bringing the window front
147 Window
.SetVisible (WINDOW_FRONT
)
148 PortraitWindow
.SetVisible (WINDOW_VISIBLE
)
151 def UpdateMageWindow ():
152 global MageMemorizedSpellList
, MageKnownSpellList
154 MageMemorizedSpellList
= []
155 MageKnownSpellList
= []
158 pc
= GemRB
.GameGetSelectedPCSingle ()
159 type = IE_SPELL_TYPE_WIZARD
160 level
= MageSpellLevel
161 max_mem_cnt
= GemRB
.GetMemorizableSpellsCount (pc
, type, level
, 1)
163 Label
= Window
.GetControl (0x10000032)
164 GemRB
.SetToken("SPELLLEVEL", str(level
+1) )
165 Label
.SetText (10345)
167 Name
= GemRB
.GetPlayerName (pc
, 0)
168 Label
= Window
.GetControl (0x10000035)
172 mem_cnt
= GemRB
.GetMemorizedSpellsCount (pc
, type, level
)
174 Button
= Window
.GetControl (3 + i
)
176 ms
= GemRB
.GetMemorizedSpell (pc
, type, level
, i
)
177 Button
.SetSpellIcon (ms
['SpellResRef'], 0)
178 Button
.SetFlags (IE_GUI_BUTTON_NO_IMAGE
, OP_NAND
)
179 Button
.SetFlags (IE_GUI_BUTTON_PICTURE
, OP_OR
)
181 Button
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "OpenMageSpellUnmemorizeWindow")
183 Button
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "OnMageUnmemorizeSpell")
184 Button
.SetEvent (IE_GUI_BUTTON_ON_RIGHT_PRESS
, "OpenMageSpellInfoWindow")
185 spell
= GemRB
.GetSpell (ms
['SpellResRef'])
186 Button
.SetTooltip (spell
['SpellName'])
187 MageMemorizedSpellList
.append (ms
['SpellResRef'])
188 Button
.SetVarAssoc ("SpellButton", i
)
189 Button
.EnableBorder (0, ms
['Flags'] == 0)
192 Button
.SetFlags (IE_GUI_BUTTON_NORMAL
, OP_SET
)
194 Button
.SetFlags (IE_GUI_BUTTON_NO_IMAGE
, OP_SET
)
195 Button
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "")
196 Button
.SetEvent (IE_GUI_BUTTON_ON_RIGHT_PRESS
, "")
197 Button
.SetTooltip ('')
198 Button
.EnableBorder (0, 0)
200 label
= Window
.GetControl (0x10000040)
201 label
.SetText (GemRB
.GetString(61256) + " " + str(max_mem_cnt
)) #spells can cast
203 known_cnt
= GemRB
.GetKnownSpellsCount (pc
, type, level
)
205 Button
= Window
.GetControl (27 + i
)
207 ks
= GemRB
.GetKnownSpell (pc
, type, level
, i
)
208 Button
.SetSpellIcon (ks
['SpellResRef'], 0)
209 Button
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "OnMageMemorizeSpell")
210 Button
.SetEvent (IE_GUI_BUTTON_ON_RIGHT_PRESS
, "OpenMageSpellInfoWindow")
211 spell
= GemRB
.GetSpell (ks
['SpellResRef'])
212 Button
.SetTooltip (spell
['SpellName'])
213 MageKnownSpellList
.append (ks
['SpellResRef'])
214 Button
.SetVarAssoc ("SpellButton", 100 + i
)
217 Button
.SetFlags (IE_GUI_BUTTON_PICTURE
, OP_NAND
)
218 Button
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "")
219 Button
.SetEvent (IE_GUI_BUTTON_ON_RIGHT_PRESS
, "")
220 Button
.SetTooltip ('')
221 Button
.EnableBorder (0, 0)
223 if (ClassSkillsTable
.GetValue (GemRB
.GetPlayerStat (pc
, IE_CLASS
), 2)=="*"):
224 Window
.SetVisible (WINDOW_GRAYED
)
226 Window
.SetVisible (WINDOW_VISIBLE
)
229 def MagePrevLevelPress ():
230 global MageSpellLevel
232 if MageSpellLevel
> 0:
233 MageSpellLevel
= MageSpellLevel
- 1
237 def MageNextLevelPress ():
238 global MageSpellLevel
240 if MageSpellLevel
< 8:
241 MageSpellLevel
= MageSpellLevel
+ 1
245 def RefreshMageLevel ():
246 global MageSpellLevel
248 MageSpellLevel
= GemRB
.GetVar ("MageSpellLevel")
252 def OpenMageSpellInfoWindow ():
253 global MageSpellInfoWindow
255 if MageSpellInfoWindow
!= None:
256 if MageSpellInfoWindow
:
257 MageSpellInfoWindow
.Unload ()
258 MageSpellInfoWindow
= None
261 MageSpellInfoWindow
= Window
= GemRB
.LoadWindowObject (3)
264 Button
= Window
.GetControl (5)
265 Button
.SetText (15416)
266 Button
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "OpenMageSpellInfoWindow")
269 index
= GemRB
.GetVar ("SpellButton")
271 Button
= Window
.GetControl (6)
273 Button
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "")
274 Button
.SetFlags (IE_GUI_BUTTON_NO_IMAGE
, OP_SET
)
276 Button
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "OpenMageSpellRemoveWindow")
277 Button
.SetText (63668)
279 ResRef
= MageMemorizedSpellList
[index
]
281 ResRef
= MageKnownSpellList
[index
- 100]
283 spell
= GemRB
.GetSpell (ResRef
)
285 Label
= Window
.GetControl (0x0fffffff)
286 Label
.SetText (spell
['SpellName'])
288 Button
= Window
.GetControl (2)
289 Button
.SetSpellIcon (ResRef
, 1)
291 Text
= Window
.GetControl (3)
292 Text
.SetText (spell
['SpellDesc'])
294 Window
.ShowModal (MODAL_SHADOW_GRAY
)
297 def OnMageMemorizeSpell ():
298 pc
= GemRB
.GameGetSelectedPCSingle ()
299 level
= MageSpellLevel
300 type = IE_SPELL_TYPE_WIZARD
302 index
= GemRB
.GetVar ("SpellButton") - 100
304 if GemRB
.MemorizeSpell (pc
, type, level
, index
):
308 def CloseMageSpellUnmemorizeWindow ():
309 global MageSpellUnmemorizeWindow
311 if MageSpellUnmemorizeWindow
:
312 MageSpellUnmemorizeWindow
.Unload ()
313 MageSpellUnmemorizeWindow
= None
316 def OpenMageSpellRemoveWindow ():
317 global MageSpellUnmemorizeWindow
319 MageSpellUnmemorizeWindow
= Window
= GemRB
.LoadWindowObject (101)
321 # "Are you sure you want to ....?"
322 TextArea
= Window
.GetControl (3)
323 TextArea
.SetText (63745)
326 Button
= Window
.GetControl (0)
327 Button
.SetText (17507)
328 Button
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "OnMageRemoveSpell")
329 Button
.SetFlags (IE_GUI_BUTTON_DEFAULT
, OP_OR
)
332 Button
= Window
.GetControl (1)
333 Button
.SetText (13727)
334 Button
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "CloseMageSpellUnmemorizeWindow")
335 Button
.SetFlags (IE_GUI_BUTTON_CANCEL
, OP_OR
)
337 Window
.ShowModal (MODAL_SHADOW_GRAY
)
340 def OpenMageSpellUnmemorizeWindow ():
341 global MageSpellUnmemorizeWindow
343 MageSpellUnmemorizeWindow
= Window
= GemRB
.LoadWindowObject (101)
345 # "Are you sure you want to ....?"
346 TextArea
= Window
.GetControl (3)
347 TextArea
.SetText (11824)
350 Button
= Window
.GetControl (0)
351 Button
.SetText (17507)
352 Button
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "OnMageUnmemorizeSpell")
353 Button
.SetFlags (IE_GUI_BUTTON_DEFAULT
, OP_OR
)
356 Button
= Window
.GetControl (1)
357 Button
.SetText (13727)
358 Button
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "CloseMageSpellUnmemorizeWindow")
359 Button
.SetFlags (IE_GUI_BUTTON_CANCEL
, OP_OR
)
361 Window
.ShowModal (MODAL_SHADOW_GRAY
)
364 def OnMageUnmemorizeSpell ():
365 if MageSpellUnmemorizeWindow
:
366 CloseMageSpellUnmemorizeWindow()
368 pc
= GemRB
.GameGetSelectedPCSingle ()
369 level
= MageSpellLevel
370 type = IE_SPELL_TYPE_WIZARD
372 index
= GemRB
.GetVar ("SpellButton")
374 if GemRB
.UnmemorizeSpell (pc
, type, level
, index
):
378 def OnMageRemoveSpell ():
379 CloseMageSpellUnmemorizeWindow()
380 OpenMageSpellInfoWindow()
382 pc
= GemRB
.GameGetSelectedPCSingle ()
383 level
= MageSpellLevel
384 type = IE_SPELL_TYPE_WIZARD
386 index
= GemRB
.GetVar ("SpellButton")-100
388 #remove spell from book
389 GemRB
.RemoveSpell (pc
, type, level
, index
)
393 def LoadCondition ():
394 global ContCond
, ContTarg
396 Table
= GemRB
.LoadTableObject ("contcond")
397 CondCount
= Table
.GetRowCount ()
398 ContCond
= [0] * CondCount
400 for i
in range (CondCount
):
401 #get the condition's name and description
402 tuple = (Table
.GetValue (i
, 0),Table
.GetValue (i
, 1) )
405 Table
= GemRB
.LoadTableObject ("conttarg")
406 TargCount
= Table
.GetRowCount ()
407 ContTarg
= [0] * TargCount
409 for i
in range (TargCount
):
410 #get the condition's name and description
411 tuple = (Table
.GetValue (i
, 0),Table
.GetValue (i
, 1) )
415 def OpenSequencerWindow ():
417 global ContingencyTextArea
, TypeButton
, OkButton
418 global Level
, MaxLevel
, Target
, Count
420 global ContCond
, ContTarg
421 global Spell1
, Spell2
, Spell3
, Source
430 #the target player (who receives the contingency or sequencer)
431 pc
= GemRB
.GetVar("P0")
433 MaxLevel
= GemRB
.GetVar("P1")
434 #target 0 - any, 1 - caster only, 2 - sequencer
436 p2
= GemRB
.GetVar("P2")
437 Source
= GemRB
.GetSpellCastOn(pc
)
439 print "Source: ", Source
445 GemRB
.LoadWindowPack ("GUIMG", 640, 480)
447 #saving the original portrait window
448 OtherWindow
= Window
= GemRB
.LoadWindowObject (6)
450 Title
= Window
.GetControl (0x0fffffff)
452 ContingencyTextArea
= Window
.GetControl (25)
456 Title
.SetText (55374)
457 ContingencyTextArea
.SetText (60420)
459 Title
.SetText (55375)
460 ContingencyTextArea
.SetText (55372)
463 Title
.SetText (11941)
465 Title
.SetText (55376)
466 ContingencyTextArea
.SetText (55373)
468 CondSelect
= Window
.GetControl (4)
469 CondLabel
= Window
.GetControl (0x10000000)
470 TargSelect
= Window
.GetControl (6)
471 TargLabel
= Window
.GetControl (0x10000001)
472 TypeButton
= Window
.GetControl (8)
474 #no cleric spells available
475 if (ClassSkillsTable
.GetValue (GemRB
.GetPlayerStat (pc
, IE_CLASS
), 0)=="*" and
476 ClassSkillsTable
.GetValue (GemRB
.GetPlayerStat (pc
, IE_CLASS
), 1)=="*"):
477 TypeButton
.SetState (IE_GUI_BUTTON_DISABLED
)
480 CondSelect
.SetPos (-1,-1)
481 CondLabel
.SetPos (-1,-1)
482 TargSelect
.SetPos (-1,-1)
483 TargLabel
.SetPos (-1,-1)
484 sb
= Window
.GetControl (5)
486 sb
= Window
.GetControl (7)
489 CondSelect
.SetFlags (IE_GUI_TEXTAREA_SELECTABLE
, OP_SET
)
490 CondSelect
.SetEvent (IE_GUI_TEXTAREA_ON_CHANGE
, "ContingencyHelpCondition")
491 CondSelect
.SetVarAssoc ("ContCond", 0)
492 for elem
in ContCond
:
493 CondSelect
.Append (elem
[0], -1)
495 TargSelect
.SetFlags (IE_GUI_TEXTAREA_SELECTABLE
, OP_SET
)
496 TargSelect
.SetEvent (IE_GUI_TEXTAREA_ON_CHANGE
, "ContingencyHelpTarget")
497 TargSelect
.SetVarAssoc ("ContTarg", 0)
498 for elem
in ContTarg
:
499 TargSelect
.Append (elem
[0], -1)
500 #check if target is only self
502 TargSelect
.SetVarAssoc ("ContTarg", 1)
505 GemRB
.SetVar ("SpellType", 0)
506 TypeButton
.SetVarAssoc ("SpellType", 1)
507 TypeButton
.SetFlags (IE_GUI_BUTTON_CHECKBOX
, OP_OR
)
508 TypeButton
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "ContTypePressed")
510 Button
= Window
.GetControl (9)
511 Button
.SetEvent(IE_GUI_BUTTON_ON_PRESS
, "LevelIncrease")
512 Button
= Window
.GetControl (10)
513 Button
.SetEvent(IE_GUI_BUTTON_ON_PRESS
, "LevelDecrease")
515 OkButton
= Window
.GetControl (27)
516 OkButton
.SetText (11973)
517 OkButton
.SetFlags (IE_GUI_BUTTON_DEFAULT
, OP_OR
)
518 OkButton
.SetState (IE_GUI_BUTTON_DISABLED
)
520 CancelButton
= Window
.GetControl (29)
521 CancelButton
.SetText (13727)
522 CancelButton
.SetFlags (IE_GUI_BUTTON_CANCEL
, OP_OR
)
524 OkButton
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "ContingencyOk")
525 CancelButton
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "ContingencyCancel")
527 Window
.ShowModal (MODAL_SHADOW_GRAY
)
530 def UpdateSpellList ():
534 Type
= IE_SPELL_TYPE_PRIEST
536 Type
= IE_SPELL_TYPE_WIZARD
538 Label
= Window
.GetControl (0x1000001d)
539 Label
.SetText (GemRB
.GetString(12137)+str(Level
) )
541 BuildSpellList(pc
, Type
, Level
-1)
543 names
= SpellList
.keys()
548 for i
in range(11,20):
549 Button
= Window
.GetControl (i
)
550 Button
.SetFont ("NUMBER")
551 Button
.SetFlags (IE_FONT_ALIGN_RIGHT|IE_FONT_ALIGN_BOTTOM
,OP_OR
)
553 Button
.SetSpellIcon (names
[j
], 1)
554 Button
.SetText( str(SpellList
[names
[j
]]) )
555 Button
.SetVarAssoc("Index", j
)
556 Button
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "ContingencyHelpSpell")
557 Button
.SetState (IE_GUI_BUTTON_ENABLED
)
559 Button
.SetSpellIcon("")
561 Button
.SetState (IE_GUI_BUTTON_DISABLED
)
564 Button
= Window
.GetControl (22)
565 Button
.SetSpellIcon(Spell1
, 1)
566 Button
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "DeleteSpell1")
567 Button
= Window
.GetControl (23)
568 Button
.SetSpellIcon(Spell2
, 1)
569 Button
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "DeleteSpell2")
570 Button
= Window
.GetControl (24)
571 Button
.SetSpellIcon(Spell3
, 1)
572 Button
.SetEvent (IE_GUI_BUTTON_ON_PRESS
, "DeleteSpell3")
575 OkButton
.SetState (IE_GUI_BUTTON_DISABLED
)
577 OkButton
.SetState (IE_GUI_BUTTON_ENABLED
)
580 def ContTypePressed ():
583 Label
= OtherWindow
.GetControl (0x10000002)
584 SpellType
= GemRB
.GetVar ("SpellType")
587 Label
.SetText (54352)
588 TypeButton
.SetText (18039)
590 Label
.SetText (21836)
591 TypeButton
.SetText (7204)
595 def ContingencyOk ():
600 GemRB
.ApplyEffect (pc
, "Sequencer:Store", 0, 0, Spell1
, Spell2
, Spell3
, Source
)
602 GemRB
.ApplyEffect (pc
, "CastSpellOnCondition", Target
, Condition
, Spell1
, Spell2
, Spell3
, Source
)
604 GemRB
.LearnSpell (pc
, Source
+"d", LS_MEMO
)
608 def ContingencyCancel ():
611 GemRB
.SetPlayerStat (pc
, IE_IDENTIFYMODE
, 0)
615 def ContingencyHelpSpell ():
616 global Spell1
, Spell2
, Spell3
618 names
= SpellList
.keys()
620 i
= GemRB
.GetVar("Index")
623 if Spell1
=="" and Count
>0:
625 elif Spell2
=="" and Count
>1:
627 elif Spell3
=="" and Count
>2:
630 spl
= GemRB
.GetSpell(spell
)
631 ContingencyTextArea
.SetText (spl
["SpellDesc"])
636 global Spell1
, Spell2
, Spell3
641 ContingencyTextArea
.SetText("")
646 global Spell2
, Spell3
650 ContingencyTextArea
.SetText("")
658 ContingencyTextArea
.SetText("")
662 def ContingencyHelpCondition ():
663 i
= GemRB
.GetVar ("ContCond")
664 ContingencyTextArea
.SetText (ContCond
[i
][1])
667 def ContingencyHelpTarget ():
668 i
= GemRB
.GetVar ("ContTarg")
669 ContingencyTextArea
.SetText (ContTarg
[i
][1])
672 def LoadExclusions():
675 ExclusionTable
= GemRB
.LoadTableObject ("contingx")
676 Columns
= ExclusionTable
.GetColumnCount ()
677 Rows
= ExclusionTable
.GetRowCount ()
679 for i
in range (Columns
):
680 Exclusions
.append ([])
681 for j
in range (Rows
):
682 spell
= ExclusionTable
.GetValue (j
,i
,0)
685 Exclusions
[i
].append (spell
.lower())
689 #TODO: build a correct list for sorcerers too!
690 def BuildSpellList (pc
, type, level
):
697 dummy
= [Spell1
,Spell2
,Spell3
]
698 mem_cnt
= GemRB
.GetMemorizedSpellsCount (pc
, type, level
)
700 for i
in range(mem_cnt
):
701 ms
= GemRB
.GetMemorizedSpell (pc
, type, level
, i
)
703 spell
= ms
["SpellResRef"]
704 if spell
in Exclusions
[level
]:
709 if not (spell
in SpellList
):
712 SpellList
[spell
] = SpellList
[spell
]+1
731 ###################################################
732 # End of file GUIMG.py