TickHook: Fix crash when TickHook isn't set.
[gemrb.git] / gemrb / GUIScripts / bg1 / CharGenGui.py
blobddbbdbcadc9eb8c46d2fd8586c0d19d32360669f
1 import GemRB
2 from GUIDefines import *
3 from ie_stats import *
4 import CharGenCommon
5 import GUICommon
6 import CommonTables
7 import LUCommon
8 import LUProfsSelection
10 def Imprt():
11 GemRB.SetToken("NextScript","CharGen")
12 GemRB.SetNextScript("ImportFile") #import
13 return
15 def setPlayer():
16 MyChar = GemRB.GetVar ("Slot")
17 GemRB.CreatePlayer ("charbase", MyChar | 0x8000 )
18 return False
20 def unsetPlayer():
21 MyChar = GemRB.GetVar ("Slot")
22 GemRB.CreatePlayer ("", MyChar | 0x8000 )
23 return False
25 def unsetGender():
26 #print "unset Gender"
27 MyChar = GemRB.GetVar ("Slot")
28 GemRB.SetPlayerStat (MyChar, IE_SEX, 0)
30 def unsetPortrait():
31 #print "unset Portrait"
32 GemRB.SetToken("SmallPortrait","")
33 GemRB.SetToken("LargePortrait","")
35 def getGender(area):
36 MyChar = GemRB.GetVar ("Slot")
37 area.SetText(12135)
38 area.Append (": ")
39 if GemRB.GetPlayerStat(MyChar,IE_SEX) == 1:
40 return area.Append(1050)
41 else:
42 return area.Append(1051)
44 #race
45 def unsetRace():
46 MyChar = GemRB.GetVar ("Slot")
47 GemRB.SetPlayerStat (MyChar, IE_RACE, 0)
49 def getRace(area):
50 MyChar = GemRB.GetVar ("Slot")
51 RaceID = GemRB.GetPlayerStat (MyChar, IE_RACE)
52 RaceIndex = CommonTables.Races.FindValue(3,RaceID)
53 RaceCap = CommonTables.Races.GetValue(RaceIndex,2)
54 area.Append(1048,-1) # new line
55 area.Append(": ")
56 area.Append(RaceCap)
58 #class
59 def unsetClass():
60 MyChar = GemRB.GetVar ("Slot")
61 GemRB.SetPlayerStat (MyChar, IE_CLASS, 0)
62 GemRB.SetPlayerStat (MyChar, IE_KIT, 0)
64 def getClass(area):
65 MyChar = GemRB.GetVar ("Slot")
66 ClassTitle = GUICommon.GetActorClassTitle(MyChar)
68 area.Append(12136, -1)
69 area.Append(": ")
70 area.Append(ClassTitle)
72 def guardSpecialist():
73 return GemRB.GetVar("Specialist") == 1
75 def guardMultiClass():
76 return GemRB.GetVar("Multi Class") == 1
79 #Alignment
80 def unsetAlignment():
81 MyChar = GemRB.GetVar ("Slot")
82 GemRB.SetPlayerStat (MyChar, IE_ALIGNMENT,0)
84 def getAlignment(area):
85 AlignmentTable = GemRB.LoadTable("aligns")
87 MyChar = GemRB.GetVar ("Slot")
88 AllignID = GemRB.GetPlayerStat (MyChar, IE_ALIGNMENT)
90 area.Append(1049, -1)
91 area.Append(": ")
92 AllignIndex = AlignmentTable.FindValue(3,AllignID)
93 AllignCap = AlignmentTable.GetValue(AllignIndex,2)
94 area.Append(AllignCap)
95 area.Append("\n")
97 #Abilties
98 def unsetAbilities():
99 MyChar = GemRB.GetVar ("Slot")
100 AbilityTable = GemRB.LoadTable ("ability")
101 AbilityCount = AbilityTable.GetRowCount ()
103 # set all our abilites to zero
104 GemRB.SetPlayerStat (MyChar, IE_STREXTRA, 0)
105 for i in range(AbilityCount):
106 StatID = AbilityTable.GetValue (i, 3)
107 GemRB.SetPlayerStat (MyChar, StatID, 0)
109 def getAbilities(area):
110 MyChar = GemRB.GetVar ("Slot")
111 AbilityTable = GemRB.LoadTable ("ability")
112 AbilityCount = AbilityTable.GetRowCount ()
113 for i in range(AbilityCount):
114 v = AbilityTable.GetValue(i,2)
115 id = AbilityTable.GetValue(i,3)
116 area.Append(v, -1)
117 area.Append(": "+str(GemRB.GetPlayerStat(MyChar,id)))
118 area.Append("\n")
119 area.Append("\n")
121 #Skill
122 def unsetHateRace():
123 MyChar = GemRB.GetVar ("Slot")
124 GemRB.SetPlayerStat(MyChar, IE_HATEDRACE, 0 )
126 def guardHateRace():
127 MyChar = GemRB.GetVar ("Slot")
128 Class = GemRB.GetPlayerStat(MyChar,IE_CLASS)
129 ClassName = CommonTables.ClassSkills.GetRowName(Class)
130 TableName = CommonTables.ClassSkills.GetValue(ClassName, "HATERACE")
131 return TableName != "*"
133 def getHatedRace(TextAreaControl):
134 MyChar = GemRB.GetVar ("Slot")
135 Race = GemRB.GetPlayerStat(MyChar, IE_HATEDRACE)
136 if Race:
137 HateRaceTable = GemRB.LoadTable ("HATERACE")
138 Row = HateRaceTable.FindValue (1, Race)
139 info = GemRB.GetString (HateRaceTable.GetValue(Row, 0))
140 if info != "":
141 #TextAreaControl.Append("\n")
142 info = ": " + info + "\n"
143 TextAreaControl.Append(15982)
144 TextAreaControl.Append(info)
146 def unsetMageSpells():
147 print("unsetMageSpells")
148 MyChar = GemRB.GetVar ("Slot")
150 GUICommon.RemoveKnownSpells (MyChar, IE_SPELL_TYPE_WIZARD, 1, 5, 1)
152 def guardMageSpells():
153 MyChar = GemRB.GetVar ("Slot")
154 Class = GemRB.GetPlayerStat(MyChar,IE_CLASS)
155 TableName = CommonTables.ClassSkills.GetValue(Class, 2)
156 return TableName != "*"
158 def getMageSpells(TextAreaControl):
159 MyChar = GemRB.GetVar ("Slot")
160 # arcane spells
161 info = ""
162 for level in range(0, 9):
163 for j in range(0, GemRB.GetKnownSpellsCount (MyChar, IE_SPELL_TYPE_WIZARD, level) ):
164 Spell = GemRB.GetKnownSpell (MyChar, IE_SPELL_TYPE_WIZARD, level, j)
165 Spell = GemRB.GetSpell (Spell['SpellResRef'], 1)['SpellName']
166 info += GemRB.GetString (Spell) + "\n"
167 if info != "":
168 info = "\n" + info + "\n"
169 TextAreaControl.Append (11027)
170 TextAreaControl.Append (info)
172 def guardSkills():
173 SkillTable = GemRB.LoadTable("skills")
174 RowCount = SkillTable.GetRowCount()-2
176 MyChar = GemRB.GetVar ("Slot")
177 Kit = GemRB.GetPlayerStat (MyChar, IE_KIT)
179 if Kit != 0:
180 KitName = CommonTables.KitList.GetValue(Kit, 0) #rowname is just a number
181 PointsLeft = SkillTable.GetValue("FIRST_LEVEL",KitName)
182 if PointsLeft < 0:
183 Kit = 0
185 if Kit == 0:
186 ClassID = GemRB.GetPlayerStat (MyChar, IE_CLASS)
187 ClassIndex = CommonTables.Classes.FindValue(5,ClassID)
188 KitName = CommonTables.Classes.GetRowName(ClassIndex)
190 for i in range(RowCount):
191 SkillName = SkillTable.GetRowName(i+2)
192 if SkillTable.GetValue(SkillName, KitName)==1:
193 return True
195 return False
197 def unsetSkill():
198 import LUSkillsSelection
199 MyChar = GemRB.GetVar ("Slot")
200 LUSkillsSelection.SkillsNullify ()
201 LUSkillsSelection.SkillsSave (MyChar)
203 def getSkills(TextAreaControl):
204 MyChar = GemRB.GetVar ("Slot")
205 # thieving and other skills
206 info = ""
207 SkillTable = GemRB.LoadTable ("skills")
208 ClassID = GemRB.GetPlayerStat (MyChar, IE_CLASS)
209 Class = CommonTables.Classes.FindValue (5, ClassID)
210 ClassName = CommonTables.Classes.GetRowName(Class)
211 RangerSkills = CommonTables.ClassSkills.GetValue (ClassName, "RANGERSKILL")
212 BardSkills = CommonTables.ClassSkills.GetValue (ClassName, "BARDSKILL")
213 KitName = GUICommon.GetKitIndex (MyChar)
214 if KitName == 0:
215 KitName = ClassName
216 else:
217 KitName = CommonTables.KitList.GetValue (KitName, 0)
219 if SkillTable.GetValue ("RATE", KitName) != -1 or BardSkills != "*" or RangerSkills != "*":
220 for skill in range(SkillTable.GetRowCount () - 2):
221 name = GemRB.GetString (SkillTable.GetValue (skill+2, 1))
222 id = SkillTable.GetValue (skill+2, 2)
223 available = SkillTable.GetValue (SkillTable.GetRowName (skill+2), KitName)
224 value = GemRB.GetPlayerStat(MyChar,id)
225 if value >= 0 and available != -1:
226 info += name + ": " + str(value) + "\n"
228 if info != "":
229 info = "\n" + info + "\n"
230 TextAreaControl.Append("\n")
231 TextAreaControl.Append (8442)
232 TextAreaControl.Append (info)
234 def unsetProfi():
235 MyChar = GemRB.GetVar ("Slot")
236 LUProfsSelection.ProfsNullify ()
237 LUProfsSelection.ProfsSave(MyChar, LUProfsSelection.LUPROFS_TYPE_CHARGEN)
239 def getProfi(TextAreaControl):
240 MyChar = GemRB.GetVar ("Slot")
241 # weapon proficiencies
242 TextAreaControl.Append ("\n")
243 TextAreaControl.Append (9466)
244 TextAreaControl.Append ("\n")
245 TmpTable=GemRB.LoadTable ("weapprof")
246 ProfCount = TmpTable.GetRowCount ()
247 #bg2 weapprof.2da contains the bg1 proficiencies too, skipping those
248 for i in range(ProfCount):
249 # 4294967296 overflows to -1 on some arches, so we use a smaller invalid strref
250 id = TmpTable.GetValue (i, 0)+IE_PROFICIENCYBASTARDSWORD
251 Weapon = GemRB.GetString (TmpTable.GetValue (i, 1))
252 Value = GemRB.GetPlayerStat (MyChar,id)
253 if Value:
254 pluses = " "
255 for plus in range(0, Value):
256 pluses += "+"
257 TextAreaControl.Append (Weapon + pluses + "\n")
259 #Appearance
260 def unsetColors():
261 MyChar = GemRB.GetVar ("Slot")
262 GUICommon.SetColorStat (MyChar, IE_HAIR_COLOR, 0 )
263 GUICommon.SetColorStat (MyChar, IE_SKIN_COLOR, 0 )
264 GUICommon.SetColorStat (MyChar, IE_MAJOR_COLOR, 0 )
265 GUICommon.SetColorStat (MyChar, IE_MINOR_COLOR, 0 )
267 def unsetSounds():
268 MyChar = GemRB.GetVar ("Slot")
269 GemRB.SetPlayerSound(MyChar,"")
272 #name
273 def unsetName():
274 MyChar = GemRB.GetVar ("Slot")
275 GemRB.SetPlayerName (MyChar, "", 0)
277 def getName(TextAreaControl):
278 MyChar = GemRB.GetVar ("Slot")
279 name = GemRB.GetPlayerName(MyChar)
280 if(name != ""):
281 TextAreaControl.Append(name + "\n")
283 #divine spells
284 def setDivineSpells():
285 MyChar = GemRB.GetVar ("Slot")
287 ClassID = GemRB.GetPlayerStat (MyChar, IE_CLASS)
288 Class = CommonTables.Classes.FindValue (5, ClassID)
289 ClassName = CommonTables.Classes.GetRowName(Class)
291 DruidTable = CommonTables.ClassSkills.GetValue (ClassName, "DRUIDSPELL")
292 ClericTable = CommonTables.ClassSkills.GetValue (ClassName, "CLERICSPELL")
294 print("CGG setDivineSpells: CP1",ClassID,Class,ClassName,DruidTable,ClericTable)
296 AllignID = GemRB.GetPlayerStat (MyChar, IE_ALIGNMENT)
298 if ClericTable != "*":
299 learnDivine(MyChar,0x4000,ClericTable,AllignID)
300 if DruidTable != "*":
301 learnDivine(MyChar,0x8000,DruidTable,AllignID)
303 return False
305 def learnDivine(MyChar,ClassFlag,TableName,AllignID):
306 #print ("CGG setDivineSpells: CP2",MyChar,ClassFlag,TableName,AllignID )
307 GUICommon.SetupSpellLevels(MyChar, TableName, IE_SPELL_TYPE_PRIEST, 1)
308 Learnable = GUICommon.GetLearnablePriestSpells( ClassFlag, AllignID, 1)
309 for i in range(len(Learnable) ):
310 #print ("CGG setDivineSpells: CP3",Learnable[i])
311 if -1 == GUICommon.HasSpell(MyChar,IE_SPELL_TYPE_PRIEST,1,Learnable[i]):
312 #print ("CGG setDivineSpells: CP4",Learnable[i])
313 GemRB.LearnSpell (MyChar, Learnable[i], 0)
315 def unsetDivineSpells():
316 print("unsetDivineSpells")
317 MyChar = GemRB.GetVar ("Slot")
318 GUICommon.RemoveKnownSpells (MyChar, IE_SPELL_TYPE_PRIEST, 1, 1, 1)
320 def getDivineSpells(TextAreaControl):
321 MyChar = GemRB.GetVar ("Slot")
322 # divine spells
323 info = ""
324 for level in range(0, 7):
325 for j in range(0, GemRB.GetKnownSpellsCount (MyChar, IE_SPELL_TYPE_PRIEST, level) ):
326 Spell = GemRB.GetKnownSpell (MyChar, IE_SPELL_TYPE_PRIEST, level, j)
327 Spell = GemRB.GetSpell (Spell['SpellResRef'], 1)['SpellName']
328 info += GemRB.GetString (Spell) + "\n"
329 if info != "":
330 info = "\n" + info + "\n"
331 TextAreaControl.Append (11028)
332 TextAreaControl.Append (info)
334 #finish
335 def setAccept():
336 #set my character up
337 MyChar = GemRB.GetVar ("Slot")
339 ClassID = GemRB.GetPlayerStat (MyChar, IE_CLASS)
340 Class = CommonTables.Classes.FindValue (5, ClassID)
341 ClassName = CommonTables.Classes.GetRowName(Class)
343 #reputation
344 AllignID = GemRB.GetPlayerStat (MyChar, IE_ALIGNMENT)
346 TmpTable=GemRB.LoadTable ("repstart")
347 AlignmentTable = GemRB.LoadTable ("aligns")
348 t = TmpTable.GetValue (AllignID,0) * 10
349 GemRB.SetPlayerStat (MyChar, IE_REPUTATION, t)
351 #lore, thac0, hp, and saves
352 GemRB.SetPlayerStat (MyChar, IE_MAXHITPOINTS, 0)
353 GemRB.SetPlayerStat (MyChar, IE_HITPOINTS, 0)
354 LUCommon.SetupSavingThrows (MyChar)
355 LUCommon.SetupThaco (MyChar)
356 LUCommon.SetupLore (MyChar)
357 LUCommon.SetupHP (MyChar)
359 #slot 1 is the protagonist
360 if MyChar == 1:
361 GemRB.GameSetReputation( t )
363 #gold
364 TmpTable=GemRB.LoadTable ("strtgold")
365 t = GemRB.Roll (TmpTable.GetValue (ClassName,"ROLLS"),TmpTable.GetValue(ClassName,"SIDES"), TmpTable.GetValue (ClassName,"MODIFIER") )
366 GemRB.SetPlayerStat (MyChar, IE_GOLD, t*TmpTable.GetValue (ClassName,"MULTIPLIER") )
368 #set the base number of attacks; effects will add the proficiency bonus
369 GemRB.SetPlayerStat (MyChar, IE_NUMBEROFATTACKS, 2)
371 #colors
372 GUICommon.SetColorStat (MyChar, IE_METAL_COLOR, 0x1B )
373 GUICommon.SetColorStat (MyChar, IE_LEATHER_COLOR, 0x16 )
374 GUICommon.SetColorStat (MyChar, IE_ARMOR_COLOR, 0x17 )
376 #does all the rest
377 LargePortrait = GemRB.GetToken ("LargePortrait")
378 SmallPortrait = GemRB.GetToken ("SmallPortrait")
379 GemRB.FillPlayerInfo (MyChar, LargePortrait, SmallPortrait)
380 #10 is a weapon slot (see slottype.2da row 10)
381 GemRB.CreateItem (MyChar, "staf01", 10, 1, 0, 0)
382 GemRB.SetEquippedQuickSlot (MyChar, 0)
383 #LETS PLAY!!
384 playmode = GemRB.GetVar ("PlayMode")
386 GUICommon.CloseOtherWindow(None)
388 if playmode >=0:
389 CharGenCommon.close()
390 if GemRB.GetVar("GUIEnhancements"):
391 GemRB.SaveCharacter ( GemRB.GetVar ("Slot"), "gembak" )
392 GemRB.EnterGame()
393 else:
394 #show the export window
395 GemRB.SetToken("NextScript","CharGen")
396 GemRB.SetNextScript ("ExportFile")