// This component changes the northernmost "General Store" in Southwest Baldur's Gate into a Book Shop - Torvael's Tomes.
// This merchant sells very expensive tomes that permanently alter the user and can also buy the stat tomes.
// This merchant also starts several book-related fetch quests the provide gold and an expanded seleciton of books to purchase.

// Modify map note
COPY_EXISTING "BG1100.ARE" override
	SAY 0x8c58 "Torvael's Tomes"
	// SAY 0x8d28 "General Store B"
	// SAY 0x8d5c "General Store C"

// Compile shopkeep dialog
COMPILE "%MOD_FOLDER%/Components/010-BookMerchant/ZUShop01.d"

// Add merchant character ZUShop01.CRE
COPY "%MOD_FOLDER%/Components/010-BookMerchant/ZUShop01.CRE" override
	SAY NAME1 Torvael
	SAY NAME2 Torvael

// Add the book shop shop file
COPY "%MOD_FOLDER%/Components/010-BookMerchant/ZUShop01.STO" override
	SAY NAME2 @101

// Override BG1116.ARE with the book merchant's store
COPY "%MOD_FOLDER%/Components/010-BookMerchant/BG1116.ARE" override

// Load journal entries for related quests
ADD_JOURNAL @800 @801 @802 @803 @804 @805 @806 @807 @808 @809 @810 @811 USING "%MOD_FOLDER%/Components/010-BookMerchant/english.tra"

// Load tome icons
COPY "%MOD_FOLDER%/Components/010-BookMerchant/icon" override

// Add XP reduction and general effects given through tomes
COPY "%MOD_FOLDER%/Components/010-BookMerchant/spell" override

// Append two checks against character experience so tomes that reduce XP don't underflow
LAF ADD_SPLPROT_ENTRY
	INT_VAR
		stat = "44"
	STR_VAR
		value = "-1"
		relation = "4"
	RET
		index
END
OUTER_SET XpGreaterEqualThanRow = index
LAF ADD_SPLPROT_ENTRY
	INT_VAR
		stat      = "44"
	STR_VAR
		value     = "-1"
		relation  = "2"
	RET
		index
END
OUTER_SET XpLessThanRow = index

// Append an icon for a custom effect given by a tome
LAF ADD_PORTRAIT_ICON
	INT_VAR
		string = RESOLVE_STR_REF(@1138)
	STR_VAR
		bam_file = SPCL238D
	RET
		icon
END

// Add text to effects and spells
COPY_EXISTING "ZUBRZKS.SPL" override
	SAY 0x9e @1138
	WRITE_LONG 0x102 icon

// ---- START Copy Spells ----
// Copy Reflected Image to make an innate ability version
COPY_EXISTING "SPWI120.SPL" "override/ZUSPL01.SPL"
	// Set 'Spell type' to 'Innate'
	WRITE_SHORT 0x1c 4
	// Remove all 'Exclusion flags'
	WRITE_LONG 0x1e 0
	// Set 'Ability Location' to 'Ability' for each spell ability
	GET_OFFSET_ARRAY effect_array SPL_V10_HEADERS
	PHP_EACH effect_array AS int => effect_off BEGIN
		WRITE_SHORT effect_off + 0x2 4
	END

// Copy Spook to make an innate ability version
COPY_EXISTING "SPWI125.SPL" "override/ZUSPL02.SPL"
	// Set 'Spell type' to 'Innate'
	WRITE_SHORT 0x1c 4
	// Remove all 'Exclusion flags'
	WRITE_LONG 0x1e 0
	// Set 'Ability Location' to 'Ability' for each spell ability
	GET_OFFSET_ARRAY effect_array SPL_V10_HEADERS
	PHP_EACH effect_array AS int => effect_off BEGIN
		WRITE_SHORT effect_off + 0x2 4
	END

// Copy Glitterdust to make an innate ability version
COPY_EXISTING "SPWI224.SPL" "override/ZUSPL03.SPL"
	// Set 'Spell type' to 'Innate'
	WRITE_SHORT 0x1c 4
	// Remove all 'Exclusion flags'
	WRITE_LONG 0x1e 0
	// Set 'Ability Location' to 'Ability' for each spell ability
	GET_OFFSET_ARRAY effect_array SPL_V10_HEADERS
	PHP_EACH effect_array AS int => effect_off BEGIN
		WRITE_SHORT effect_off + 0x2 4
	END

// Copy Luck to make an innate ability version
COPY_EXISTING "SPWI209.SPL" "override/ZUSPL04.SPL"
	// Set 'Spell type' to 'Innate'
	WRITE_SHORT 0x1c 4
	// Remove all 'Exclusion flags'
	WRITE_LONG 0x1e 0
	// Set 'Ability Location' to 'Ability' for each spell ability
	GET_OFFSET_ARRAY effect_array SPL_V10_HEADERS
	PHP_EACH effect_array AS int => effect_off BEGIN
		WRITE_SHORT effect_off + 0x2 4
	END

// Copy Bless to make an innate ability version
COPY_EXISTING "SPPR101.SPL" "override/ZUSPL05.SPL"
	// Set 'Spell type' to 'Innate'
	WRITE_SHORT 0x1c 4
	// Remove all 'Exclusion flags'
	WRITE_LONG 0x1e 0
	// Set 'Ability Location' to 'Ability' for each spell ability
	GET_OFFSET_ARRAY effect_array SPL_V10_HEADERS
	PHP_EACH effect_array AS int => effect_off BEGIN
		WRITE_SHORT effect_off + 0x2 4
	END

// Copy Bless to make an innate ability version
COPY_EXISTING "SPPR201.SPL" "override/ZUSPL06.SPL"
	// Set 'Spell type' to 'Innate'
	WRITE_SHORT 0x1c 4
	// Remove all 'Exclusion flags'
	WRITE_LONG 0x1e 0
	// Set 'Ability Location' to 'Ability' for each spell ability
	GET_OFFSET_ARRAY effect_array SPL_V10_HEADERS
	PHP_EACH effect_array AS int => effect_off BEGIN
		WRITE_SHORT effect_off + 0x2 4
	END
// ---- END Copy Spells ----


// ---- START Secret / Quest Tomes ----
// Secrets of Demonic Imposition
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook00.ITM" override
	SAY NAME1 @995
	SAY UNIDENTIFIED_DESC @996
	SAY NAME2 @997
	SAY DESC @998
	SAY 0xae @999
// ---- END Secret / Quest Tomes ----


// ---- START Ability Tomes ----
// Codex Arcana - Reflected Image
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook01.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1002
	SAY DESC @1003
	SAY 0xae @1004
	SAY 0x16e @1005
	WRITE_LONG 0xe2 XpLessThanRow
	WRITE_LONG 0x1a2 XpGreaterEqualThanRow

// Codex Arcana - Spook
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook02.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1006
	SAY DESC @1007
	SAY 0xae @1008
	SAY 0x16e @1009
	WRITE_LONG 0xe2 XpLessThanRow
	WRITE_LONG 0x1a2 XpGreaterEqualThanRow

// Codex Arcana - Glitterdust
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook03.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1010
	SAY DESC @1011
	SAY 0xae @1012
	SAY 0x16e @1013
	WRITE_LONG 0xe2 XpLessThanRow
	WRITE_LONG 0x1a2 XpGreaterEqualThanRow

// Codex Arcana - Luck
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook04.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1014
	SAY DESC @1015
	SAY 0xae @1016
	SAY 0x16e @1017
	WRITE_LONG 0xe2 XpLessThanRow
	WRITE_LONG 0x1a2 XpGreaterEqualThanRow

// Codex Divinae - Bless
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook05.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1018
	SAY DESC @1019
	SAY 0xae @1020
	SAY 0x16e @1021
	WRITE_LONG 0xe2 XpLessThanRow
	WRITE_LONG 0x1a2 XpGreaterEqualThanRow

// Codex Divinae - Aid
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook06.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1022
	SAY DESC @1023
	SAY 0xae @1024
	SAY 0x16e @1025
	WRITE_LONG 0xe2 XpLessThanRow
	WRITE_LONG 0x1a2 XpGreaterEqualThanRow
// ---- END Ability Tomes ----


// ---- START Flat Bonus Tomes ----
// Tome of Speed
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook11.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1041
	SAY DESC @1042
	SAY 0x10e @1043

// Tome of the Fairy's Foe
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook12.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1044
	SAY DESC @1045
	SAY 0x10e @1046

// Tome of Worldly Knowledge
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook13.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1047
	SAY DESC @1048
	SAY 0x10e @1049

// Tome of Eagle's Sight
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook14.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1050
	SAY DESC @1051
	SAY 0x10e @1052

// Tome of Giant's Downfall
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook15.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1053
	SAY DESC @1054
	SAY 0x10e @1055

// Tome of Skilled Stabbing
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook16.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1056
	SAY DESC @1057
	SAY 0x10e @1058
// ---- END Flat Bonus Tomes ----


// ---- START Balanced Bonus Tomes ----
// High Stakes Hexbook
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook21.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1071
	SAY DESC @1072
	SAY 0xde @1073
	SAY 0x10e @1074
	SAY 0x13e @994

// The Beating Hide
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook22.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1075
	SAY DESC @1076
	SAY 0xde @1077
	SAY 0x10e @1078
	SAY 0x13e @1079

// Measured Blows Manual
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook23.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1080
	SAY DESC @1081
	SAY 0xde @1082
	SAY 0x10e @1083
	SAY 0x13e @992

// Longevity Ledger
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook24.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1084
	SAY DESC @1085
	SAY 0xde @1086
	SAY 0x10e @1087

// Chivalry's Burden
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook25.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1088
	SAY DESC @1089
	SAY 0xde @1090
	SAY 0x10e @1091
	SAY 0x13e @1092
	SAY 0x16e @1093

// The Piercing Shadow
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook26.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1094
	SAY DESC @1095
	SAY 0xde @1096
	SAY 0x10e @1097
	SAY 0x13e @1098
	SAY 0x16e @1099
	SAY 0x19e @1100
	SAY 0x1ce @1101

// Fumbleflux Folio
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook27.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1102
	SAY DESC @1103
	SAY 0xde @1104
	SAY 0x10e @1105
	SAY 0x13e @991
	SAY 0x16e @1106
	SAY 0x19e @1107
	SAY 0x1ce @1108
	SAY 0x1fe @1109

// Pyrophage Grimorie
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook28.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1110
	SAY DESC @1111
	SAY 0xde @993
	SAY 0x10e @1112
	SAY 0x13e @1113
	SAY 0x16e @1114
	SAY 0x19e @1115

// The Point
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook29.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1116
	SAY DESC @1117
	SAY 0xde @1118
	SAY 0x10e @1119
	SAY 0x13e @1120
	SAY 0x16e @1121
	SAY 0x19e @1122

// Canticle of Hollow Benedictions
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook30.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1123
	SAY DESC @1124
	SAY 0xde @1125
	SAY 0x10e @1126
	SAY 0x13e @1127
	SAY 0x16e @1128
	SAY 0x19e @1129

// Pyrovolt Tutor
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook31.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1130
	SAY DESC @1131
	SAY 0xde @1132
	SAY 0x10e @1133
// ---- END Balanced Bonus Tomes ----


// ---- START Extra Bonus Tomes ----
// CHA Reward - The Crimson Trance
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook35.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1134
	SAY DESC @1135
	SAY 0xde @1136
	SAY 0x10e @1137

// Quest Reward - The Wayward Step
COPY "%MOD_FOLDER%/Components/010-BookMerchant/items/ZUBook36.ITM" override
	SAY NAME1 @1000
	SAY UNIDENTIFIED_DESC @1001
	SAY NAME2 @1139
	SAY DESC @1140
	SAY 0xde @1141
	SAY 0x10e @1142
// ---- END Extra Bonus Tomes ----