;;; Sierra Script 1.0 - (do not remove this comment)
(script# 1)
(include sci.sh)
(include game.sh)
(use Main)
(use Game)
(use Obj)
(use Sound)
(use Print)

(public
	rm131 0
)

(define CELL_VIEW 0)
(define CELL_LOOP 0)
(enum
	LOCKED
	AVAILABLE
	GREEN
)
(define PUZZLE_LEFT 120)
(define PUZZLE_TOP 48)

(local
	[image 100] = [
		1 1 1 1 1 1 1 1 1 1
		1 1 1 1 1 1 1 1 1 1
		1 1 1 1 1 1 1 1 1 1
		1 1 1 1 1 1 1 1 1 1
		1 1 1 1 1 1 1 1 1 1
		1 1 1 1 1 1 1 1 1 1
		1 1 1 1 1 1 1 1 1 1
		1 1 1 1 1 1 1 1 1 1
		1 1 1 1 1 1 1 1 1 1
		1 1 1 1 1 1 1 1 1 1
	]
)

(procedure (DrawGrid &tmp col row i x y c w h)
	(= x PUZZLE_LEFT)
	(= y PUZZLE_TOP)
	(= i 0)
	(= row 0)
	(= col 0)
	(= w (CelWide CELL_VIEW CELL_LOOP 0))
	(= h (CelHigh CELL_VIEW CELL_LOOP 0))
	(while (< row 10)
		(= col 0)
		(while (< col 10)
			(= c [image i])
			(DrawCel CELL_VIEW CELL_LOOP c x y -1)
			(= x (+ x w))
			(++ i)
			(++ col)
		)
		(= y (+ y h))
		(= x PUZZLE_LEFT)
		(++ row)
	)
)

(procedure (SetCell x y from to &tmp i)
	(if (>= x 10) (return))
	(if (< x 0) (return))	
	(if (>= y 10) (return))
	(if (< y 0) (return))
	(= i (+ (* y 10) x))
	(if (== [image i] from)
		(= [image i] to)
	)
)

(instance rm131 of Rm
	(properties
		picture 0
	)
	
	(method (init)
		(super init:)
		(gOldMH addToFront: self)
		(DrawPic picture dpOPEN_CENTEREDGE)
		(music init: play:)
		(Animate true)
		(gRoom setScript: puzzleScript)
		(SL enable:)
	)

	(method (dispose)
		(gOldMH delete: self)
		(super dispose: &rest)
	)
	
	(method (doVerb)
	)
)

(instance music of Sound
	(properties
		flags 1
		number 1
		loop -1
	)
)

(instance puzzleScript of Script
	(method (changeState newState)
		(switch (= state newState)
			(0
				(gGame setCursor: 999 1)
				(= register 0)
				(DrawGrid)
			)
			(1
				(DrawGrid)
			)
		)
	)
	
	(method (handleEvent pEvent &tmp x y i j)
		(if (== (pEvent type?) evMOUSERELEASE)
			(pEvent claimed: 1)
			(= x (- (pEvent x?) PUZZLE_LEFT))
			(= y (- (pEvent y?) PUZZLE_TOP))
			(= x (/ x (CelWide CELL_VIEW CELL_LOOP 0)))
			(= y (/ y (CelHigh CELL_VIEW CELL_LOOP 0)))
			(if (or (< x 0)
					(< y 0)
					(> x 10)
					(> y 10))
				(pEvent claimed: 0)
				(return)
			)
			(= i (+ (* y 10) x))
			(switch [image i]
				(LOCKED
					;(= [image i] 1)
					(self changeState: 1)
				)
				(AVAILABLE
					(= j 0)
					(while (< j 100)
						(if (== [image j] AVAILABLE)
							(= [image j] LOCKED)
						)
						(++ j)
					)
					(= [image i] GREEN)
					(SetCell (- x 3) y LOCKED AVAILABLE)
					(SetCell (+ x 3) y LOCKED AVAILABLE)
					(SetCell x (- y 3) LOCKED AVAILABLE)
					(SetCell x (+ y 3) LOCKED AVAILABLE)
					(SetCell (- x 2) (- y 2) LOCKED AVAILABLE)
					(SetCell (+ x 2) (- y 2) LOCKED AVAILABLE)
					(SetCell (- x 2) (+ y 2) LOCKED AVAILABLE)
					(SetCell (+ x 2) (+ y 2) LOCKED AVAILABLE)
					(++ register)
					(= gScore register)
					(SL doit:)
					(cond
						((> j 1)
							(self changeState: 1)
						)
						((== j 0)
							(self changeState: 2)
						)
						((== register 100)
							(self changeState: 3)
						)
					)
				)
				(GREEN
					;(= [image i] 1)
					(self changeState: 1)
				)
			)
		)
	)
)