;**************************************************************************
;* rimport.sc   Quest for Glory 3 -- Restore character stats from QG2	  *
;*					Created by Randy MacNeill	September 30, 1990	  			  *
;*																								  *
;**************************************************************************

(script# 601)

(public QG3import	0)

;** FLAGS ****************************
(enum 600		; temp flag setting for token
	fEOFToken
;	fBabaFrog	; flag brought from QG1	and passing to QG3
)
;*************************************

;; Bits in svMiscEquip
(define	FINESWORD_BIT 	$0001) ; fine sword
(define	FLAMESWORD_BIT	$0002) ; flaming sword
(define	COMPASS_BIT		$0003) ; compass
(define	PIN_BIT			$0004) ; spahire pin
(define	LAMP_BIT			$0008) ; brass lamp
(define	TOKEN_BIT		$0010) ; EOF token
(define	GLASSES_BIT		$0020) ; X-Ray Glasses

; things transferred from "Quest for Glory 1"
(define	BABA_BIT			$0030) ; Flag set from QG1
(define	SWORD_BIT		$0040) ; Sword
(define	CHAIN_BIT		$0080) ;
(define	PICK_BIT			$0100) ;
(define	TOOL_BIT			$0200) ;

(define	EXTRA_DATA	18)	; Data items other than stats and name
(define	CHECK_DATA	10)	; Data items that are in check sums

(define	QG2_NUM_ATTRIBS 30) ;nums_attrib equalled 30, 9-28-90

(local
;; local data for restoring hero stats from previous game
								 ;;;;;;;;;;;;;;;;;;start;;;;;;;;;;;;;;;;;;
	statsKey = $53			 ;;;;;;;;order dependent variables;;;;;;;;
	svCharType				 ;;;;;;;;order dependent variables;;;;;;;;
;**	svExperience			 ;;;;;;;;order dependent variables;;;;;;;;
	svHighDinar				 ;;;;;;;;order dependent variables;;;;;;;;
	svLowDinar				 ;;;;;;;;order dependent variables;;;;;;;;
	svScore					 ;;;;;;;;order dependent variables;;;;;;;;
	svMiscEquip				 ;;;;;;;;order dependent variables;;;;;;;;
	[codedStats QG2_NUM_ATTRIBS] ;;;;;;;;order dependent variables;;;;;;;;
	svDaggers				 ;;;;;;;;order dependent variables;;;;;;;;
	svHealingPill				 ;;;;;;;;order dependent variables;;;;;;;;
	svManaPill					 ;;;;;;;;order dependent variables;;;;;;;;
	svStaminaPill				 ;;;;;;;;order dependent variables;;;;;;;;
	svPoisonCurePill		 ;;;;;;;;order dependent variables;;;;;;;;	
	bogus0   = $a0			 ;;;;;;;;order dependent variables;;;;;;;;
	bogus1   = $3e			 ;;;;;;;;order dependent variables;;;;;;;;
	checkSum1				 ;;;;;;;;order dependent variables;;;;;;;;
	checkSum2				 ;;;;;;;;order dependent variables;;;;;;;;
	bogus2   = $2f			 ;;;;;;;;order dependent variables;;;;;;;;
	bogus3   = $90			 ;;;;;;;;order dependent variables;;;;;;;;
	bogus4   = $19			 ;;;;;;;;order dependent variables;;;;;;;;
	bogus5   = $a3			 ;;;;;;;;order dependent variables;;;;;;;;
	checkSumKey = $da		 ;;;;;;;;order dependent variables;;;;;;;;
								 ;;;;;;;;;;;;;;;;;;;end;;;;;;;;;;;;;;;;;;;
	check1
	check2
	[YNSTR 5]
	validFile
	[heroFileName 44]
	[bigStr 400]
	[str 40]
	statMap =
	[
		STR		INT		AGIL		VIT		LUCK			WEAPON	PARRY
		DODGE		STEALTH	PICK		THROW		CLIMB			MAGIC		COMM
		HONOR		EXPER		HEALTH	STAMINA	MANA			OPEN		DETMAGIC
		TRIGGER	DAZZLE	ZAP		CALM		FLAMEDART	FETCH		FORCEBOLT
		LEVITATE	REVERSAL
	]
)

(enum				;states of importHero Script
	askRestore
	getInfoFileName
	getInfoFileName2
	restoreFile
	readComplete
	importDone
	tryAgain
	badAnswer
)

(procedure
	makeZero
	restoreHero
	convWord
	convByte
)

(procedure (makeZero &tmp whichSkill)
	(for	((= whichSkill 0))
			(< whichSkill QG2_NUM_ATTRIBS)
			((++ whichSkill))
		(= [egoStats whichSkill] 0)
	)
	(= heroType 0)
	(= [invNum iDinar] 0)
	(= score 0)
	(= [invNum iDagger] 0)
	(= [invNum iHealingPill] 0)
	(= [invNum iManaPill] 0)
	(= [invNum iStaminaPill] 0)
	(StrCpy @userName {xxxxxxxxxxy})
	(for	((= whichSkill 0))
			(< whichSkill (+ QG2_NUM_ATTRIBS EXTRA_DATA))
			((++ whichSkill))
		(= [statsKey (+ whichSkill 1)] 0)
	)
	(return)
)

(procedure (restoreHero &tmp whichSkill)
	(if (not (heroinfo open: fRead))
		(Print (Format @bigStr "Could not find Hero file -- %s."
						(heroinfo name?)))
		(return FALSE)
	)

	(heroinfo readString: @userName 52)
	(heroinfo readString: @bigStr	90)

	(for	((= whichSkill 0))
			(< whichSkill (+ QG2_NUM_ATTRIBS EXTRA_DATA))
			((++ whichSkill))

		(= [statsKey (+ whichSkill 1)] (convWord [bigStr whichSkill]))

	)

	(for	((= whichSkill (+ QG2_NUM_ATTRIBS EXTRA_DATA)))
			(< 0 whichSkill)
			((-- whichSkill))

		(^= [statsKey whichSkill] (& [statsKey (- whichSkill 1)] 255))

	)

	(= check1 checkSumKey)
	(for	((= whichSkill 0))
			(< whichSkill (+ QG2_NUM_ATTRIBS CHECK_DATA))
			((+= whichSkill 2))
		(= [statsKey (+ whichSkill 1)] (& [statsKey (+ whichSkill 1)] 255))
		(+= check1 [statsKey (+ whichSkill 1)])
	)

	(= check2 0)
	(for	((= whichSkill 1))
			(< whichSkill (+ QG2_NUM_ATTRIBS CHECK_DATA))
			((+= whichSkill 2))
		(= [statsKey (+ whichSkill 1)] (& [statsKey (+ whichSkill 1)] 255))
		(+= check2 [statsKey (+ whichSkill 1)])
	)

	(&= check1 255)
	(&= check2 255)

	(if (or (!= check1 checkSum1) (!= check2 checkSum2))

		(Print "I'm terribly sorry.__That doesn't seem to be a proper
				\"Quest for Glory II\" character save file."
		)
		(return FALSE)
	)

	(for	((= whichSkill 0))
			(< whichSkill QG2_NUM_ATTRIBS)
			((++ whichSkill))
		(= [egoStats [statMap whichSkill]] [codedStats whichSkill])
		(if (and
				(not (< COMM [statMap whichSkill] OPEN))
				(not (<= 0 [codedStats whichSkill] 200))
			)
			(Print "I'm terribly sorry.__That doesn't seem to be a proper
					\"Quest for Glory II\" character save file."
			)
			(return FALSE)
		)
	)

	(= heroType svCharType)


	(= [invNum iDinar]			(+ (* svHighDinar 100) svLowDinar))
;**	(= [egoStats EXPER]			(* svExperience 100))
	(= [invNum iDagger]			svDaggers)  
	(= [invNum iHealingPill]	svHealingPill)  
	(= [invNum iManaPill]		svManaPill)     
	(= [invNum iStaminaPill]	svStaminaPill)  
	(= [invNum iPoisonCure]		svPoisonCurePill)


	(if (& svMiscEquip FINESWORD_BIT)
		(ego get: iFineSword 1)
	)
	(if (& svMiscEquip FLAMESWORD_BIT)
		(ego get: iSoulforge 1)
	)
	(if (& svMiscEquip COMPASS_BIT)
		(ego get: iCompass 1)
	)
	(if (& svMiscEquip PIN_BIT)
		(ego get: iSapphPin 1)
	)
	(if (& svMiscEquip LAMP_BIT)
		(ego get: iBrassLamp 1)
	)
	(if (& svMiscEquip TOKEN_BIT)
		(Bset fEOFToken)
	)
	(if (& svMiscEquip GLASSES_BIT)
		(ego get: iGlasses 1)
	)
	(if (& svMiscEquip BABA_BIT)
		(Bset fBabaFrog)
	)
	(if (& svMiscEquip SWORD_BIT)
		(ego get: iSword 1)
	)
	(if (& svMiscEquip CHAIN_BIT)
		(ego get: iChainMail 1)
	)
	(if (& svMiscEquip PICK_BIT)
		(ego get: iLockPick 1)
	)
	(if (& svMiscEquip TOOL_BIT)
		(ego get: iThiefKit 1)
	)

	(return TRUE)
)

(procedure (convWord ascii)
	(return (+ (convByte (>> ascii 8)) (* (convByte (& ascii 255)) 16)))
)

(procedure (convByte ascii)
	(cond
		((== ascii 32)
			(return 0)
		)
		((<= 48 ascii 57)
			(return (- ascii 48))
		)
		(else
			(return (- ascii 87))
		)
	)
)

(instance QG3import of Room
	(properties
		picture	pBlue
		horizon	0
		style		IRISOUT
	)
	
	(method (dispose)
		(StatusLine code: dftStatusCode)
		(Bset fInMainGame)
		(super dispose:)
	)

	(method (init)
		(Bclr fInMainGame)
		(StatusLine
			code: endStatus,
			enable:)
		(super init: &rest)
		(cSound stop:)

		; don't let'm control anything!
		(HandsOff)
		(self setScript: importHero)
	)
)


(instance heroinfo of File
	(properties
		name	{glory2.sav})
)

(instance importHero of Script
	(method (changeState newState &tmp whichSkill oldGold)
		(switch (= state newState)
			(askRestore
				(Format @heroFileName "a:glory2.sav")

				(Print "Please insert the disk on which you saved your
						winning Hero from \"Quest for Glory II:  Trial by Fire\"
						into the disk drive so that your Hero
						can make the journey to Tucumcari, New Mexico.")
				(self cue:)
			)

			(getInfoFileName
				(= cycles 2)
			)

			(getInfoFileName2
				(if (GetHeroFileName @heroFileName)
					(heroinfo name: @heroFileName)
					(= cycles 2)
				else
					(self changeState: importDone)
				)
			)

			(restoreFile
				(makeZero)
				; Try to restore the character file
				(if (= validFile (restoreHero))
					(= cycles 2)
				else
					(self changeState: tryAgain)
				)
			)

			(readComplete
				(Print "Character successfully imported.")
				(= cycles 1)
			)

			(importDone
				(HandsOn)
				(curRoom newRoom: rMoneychanger)
			)


			(tryAgain
				(Format @YNSTR "n")
				(if (GetInput @YNSTR 2
							{If you wish to try importing your character again,
								type \"y\",	then ENTER.__Otherwise type \"n\",
								then ENTER.}
						)
					(if (StrCmp @YNSTR {y})
						(self changeState: importDone)
					else
						(= bogus0 $a0)
						(= bogus1 $3e)
						(= bogus2 $2f)	
						(= bogus3 $90)	
						(= bogus4 $19)	
						(= bogus5 $a3)	
						(self changeState: getInfoFileName)
					)
				else
					(= cycles 2)
				)
			)

			(badAnswer
				(Print "Please answer \"y\" or \"n\".")
				(self  changeState: tryAgain)
			)
		)
	)
)

(instance endStatus of Code
	(method (doit strg)
		(Format strg "___Wow!__You're Really A Hero!__[score %d of 500]" score)
	)
)

