1# -*-python-*-
2# GemRB - Infinity Engine Emulator
3# Copyright (C) 2003-2005 The GemRB Project
4#
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.
9#
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.
14#
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.
18#
19
20###################################################
21
22import GemRB
23import GUICommon
24import CommonTables
25import MessageWindow
26import LUCommon
27
28from GameCheck import MAX_PARTY_SIZE
29from GUIDefines import *
30from CharGenEnd import GiveEquipment
31from ie_stats import *
32
33RemoveColor = {'r' : 0xf5, 'g' : 0xf5, 'b' : 0x96, 'a' : 255}
34
35def EnterGame():
36	GemRB.GameSetPartySize(MAX_PARTY_SIZE)
37	GemRB.GameSetProtagonistMode(1)
38
39	MessageWindow.OnLoad()
40
41def RemoveYoshimo( idx):
42	GemRB.DisplayString(72046, RemoveColor)
43	#WARNING:multiple strings are executed in reverse order
44	GemRB.ExecuteString('ApplySpellRES("destself",myself)', idx)
45	GemRB.ExecuteString('GivePartyAllEquipment()', idx)
46	return
47
48def RemoveImoen( idx):
49	GemRB.DisplayString(72047, RemoveColor)
50	GemRB.ExecuteString('ApplySpellRES("destself",myself)', idx)
51	GemRB.ExecuteString('GivePartyAllEquipment()', idx)
52	return
53
54def FixEdwin( idx):
55	GemRB.ApplySpell(idx, "SPIN661")
56	return
57
58def FixAnomen( idx):
59	#lawful neutral
60	if (GemRB.GetPlayerStat(idx, IE_ALIGNMENT) == 0x12):
61		GemRB.ApplySpell(idx, "SPIN678")
62	return
63
64#do all the stuff not done yet
65def FixProtagonist( idx):
66	ClassName = GUICommon.GetClassRowName (idx)
67	KitIndex = GUICommon.GetKitIndex (idx)
68
69	# fresh tob game? Grant all the equipment if we're not importing a pc (they get a poorer bag, but only if low-level enough)
70	if not (GemRB.GetVar ("oldgame") or GemRB.GetVar ("ImportedChar")):
71		GiveEquipment (idx, ClassName, KitIndex)
72		FixInnates (idx)
73		return
74
75	# prepare to adjust the XP to the minimum if it is lower than what new characters start with
76	# but don't let dualclassed characters get more xp than the rest
77	XP = GemRB.GetPlayerStat (idx, IE_XP)
78	XP2 = CommonTables.ClassSkills.GetValue (ClassName, "STARTXP2", GTV_INT)
79	Dual = GUICommon.IsDualClassed (idx, True)
80	if Dual:
81		OldLevel = GemRB.GetPlayerStat (idx, IE_LEVEL)
82		if GUICommon.IsDualSwap (idx):
83			OldLevel = GemRB.GetPlayerStat (idx, IE_LEVEL2)
84
85		OldClassIndex = Dual[1]
86		if Dual[0] == 1:
87			# was not a class before
88			KittedClass = CommonTables.KitList.GetValue (Dual[1], 7)
89			OldClassIndex = CommonTables.Classes.FindValue ("ID", KittedClass)
90		ClassName = GUICommon.GetClassRowName (OldClassIndex, "index")
91		OldXP = LUCommon.GetNextLevelExp (OldLevel, ClassName)
92		XP += OldXP
93
94	# only give a few items for transitions from soa or if we're upgrading an imported character
95	# give the Amulet of Seldarine to the pc's first empty inventory slot
96	invslot = GemRB.GetSlots (idx, -1, -1)
97	GemRB.CreateItem (idx, "AMUL27", invslot[0], 1, 0, 0)
98	GemRB.ChangeItemFlag (idx, invslot[0], IE_INV_ITEM_IDENTIFIED, OP_OR)
99
100	# adjust the XP and set a hardcoded difficulty variable to the difference
101	if XP <= XP2:
102		GemRB.SetPlayerStat (idx, IE_XP, XP2)
103		GemRB.SetGlobal ("XPGIVEN", "GLOBAL", XP2 - XP)
104		# extra bag of goodies to go along with all that learning
105		# bag19b-bag19e are unused and identical
106		if GemRB.GetVar ("ImportedChar"):
107			GemRB.CreateItem (idx, "BAG19A", invslot[1], 1, 0, 0)
108		else:
109			GemRB.CreateItem (idx, "BAG19", invslot[1], 1, 0, 0)
110		GemRB.ChangeItemFlag (idx, invslot[1], IE_INV_ITEM_IDENTIFIED, OP_OR)
111
112	FixFamiliar (idx)
113	FixInnates (idx)
114	return
115
116# replace the familiar with the improved version
117# NOTE: fx_familiar_marker destroys the old one and creates a new one (as proper actors)
118def FixFamiliar(pc):
119	# if the critter is outside, summoned, the effect will upgrade it (fx_familiar_marker runs on it)
120	# if the critter is packed in your inventory, it will be upgraded as soon as it gets released
121	# after picking it up again, also the inventory item will be new
122	pass
123
124# replace bhaal powers with the improved versions
125# or add the new ones, since soa transitioners lost them in hell
126# abstart.2da was not updated for tob
127def FixInnates(pc):
128	import Spellbook
129	from ie_spells import LS_MEMO
130	# adds the spell: SPIN822 (slayer change) if needed
131	if Spellbook.HasSpell (pc, IE_SPELL_TYPE_INNATE, 1, "SPIN822") == -1:
132		GemRB.LearnSpell (pc, "SPIN822", LS_MEMO)
133
134	# apply starting (alignment dictated) abilities (just to be replaced later)
135	# pc, table, new level, level diff, alignment
136	AlignmentAbbrev = CommonTables.Aligns.FindValue ("VALUE", GemRB.GetPlayerStat (pc, IE_ALIGNMENT))
137	GUICommon.AddClassAbilities (pc, "abstart", 6,6, AlignmentAbbrev)
138
139	# some old/new pairs are the same, so we can skip them
140	# all the innates are doubled
141	old = [ "SPIN101", "SPIN102", "SPIN104", "SPIN105" ]
142	new = [ "SPIN200", "SPIN201", "SPIN202", "SPIN203" ]
143	for i in range(len(old)):
144		if GemRB.RemoveSpell (pc, old[i]):
145			Spellbook.LearnSpell (pc, new[i], IE_SPELL_TYPE_INNATE, 3, 2, LS_MEMO)
146
147#upgrade savegame to next version
148def GameExpansion():
149
150	version = GemRB.GameGetExpansion()
151	if version<3:
152		GemRB.GameSetReputation(100)
153
154	if not GameCheck.HasTOB():
155		return
156
157	# bgt reuses the tutorial for its soa mode (playmode==0 is bg1)
158	bgtSOA = False
159	if GemRB.GetVar ("PlayMode") == 1 and (GameCheck.HasBGT() or GameCheck.HasTutu()):
160		bgtSOA = True
161
162	# old singleplayer soa or bgt soa/tutorial hybrid
163	if version < 5 and (GemRB.GetVar ("PlayMode") == 0 or bgtSOA) and GemRB.GetVar ("oldgame"):
164		#upgrade SoA to ToB/SoA
165		if GemRB.GameSetExpansion(4):
166			GemRB.AddNewArea("xnewarea")
167		return
168
169	if not GemRB.GameSetExpansion(5):
170		return
171
172	#upgrade to ToB only
173	GemRB.SetMasterScript("BALDUR25","WORLDM25")
174	GemRB.SetGlobal("INTOB","GLOBAL",1)
175	GemRB.SetGlobal("HADELLESIMEDREAM1","GLOBAL", 1)
176	GemRB.SetGlobal("HADELLESIMEDREAM2","GLOBAL", 1)
177	GemRB.SetGlobal("HADIMOENDREAM1","GLOBAL", 1)
178	GemRB.SetGlobal("HADSLAYERDREAM","GLOBAL", 1)
179	GemRB.SetGlobal("HADJONDREAM1","GLOBAL", 1)
180	GemRB.SetGlobal("HADJONDREAM2","GLOBAL", 1)
181	idx = GemRB.GetPartySize()
182	PDialogTable = GemRB.LoadTable ("pdialog")
183
184	while idx:
185		name = GemRB.GetPlayerName(idx, 2) #scripting name
186		# change the override script to the new one
187		if name != "none":
188			newScript = PDialogTable.GetValue (name.upper(), "25OVERRIDE_SCRIPT_FILE")
189			newDialog = PDialogTable.GetValue (name.upper(), "25JOIN_DIALOG_FILE")
190			GemRB.SetPlayerScript (idx, newScript, 0) # 0 is SCR_OVERRIDE, the override script slot
191			GemRB.SetPlayerDialog (idx, newDialog)
192
193			if name == "yoshimo":
194				RemoveYoshimo(idx)
195			elif name == "imoen":
196				RemoveImoen(idx)
197			elif name == "edwin":
198				FixEdwin(idx)
199			elif name == "anomen":
200				FixAnomen(idx)
201		else:
202			FixProtagonist(idx)
203			GemRB.GameSelectPC (idx, True, SELECT_REPLACE)
204		idx=idx-1
205	return
206