'########################################################################################################################
' JewelX - n Jewels Clone
'########################################################################################################################
'Idee: 2012.08.20 - 12:20:36
'Autor: Martin Wiemann - Admin@mln.ath.cx
'Lizenz: Tu was du nicht lassen kannst, solange du hiermit nicht mehr geld verdienst, als ich!
'########################################################################################################################
'NOTE: "XTGUI" mechanism copyright by Martin Wiemann
'NOTE: patent pending for "XTGUI" mechanism!
'NOTE: (contact us for comercial usage licence! Info@deltalabs.de)
'########################################################################################################################



'########################################################################################################################
#Include Once "vbcompat.bi"



'########################################################################################################################
Dim Shared G_FieldW				as UInteger = 12
Dim Shared G_FieldH				as UInteger = 12
Dim Shared G_ImgWH				as UInteger = 25
Dim Shared G_StatOffset			as UInteger = 50
Dim Shared G_Field()			as Integer
Dim Shared G_FieldNoShow()		as Integer
Dim Shared G_FieldBG()			as Integer
Dim Shared G_FieldOffsetX()		as Integer
Dim Shared G_FieldOffsetY()		as Integer
Dim Shared G_IMGJewelD(5)		as Any Ptr
Dim Shared G_IMGPuzzleD(10)		as Any Ptr
Dim Shared G_IMGRock			as Any Ptr
Dim Shared G_IMGEarth			as Any Ptr
Dim Shared G_IMGBackground		as Any Ptr
Dim Shared G_IMGBackgroundOK	as Any Ptr
Dim Shared G_CurX				as UByte
Dim Shared G_CurY				as UByte
Dim Shared G_CurHide			as UByte
Dim Shared G_SelX				as UByte
Dim Shared G_SelY				as UByte
Dim Shared G_GameMode			as UByte
Dim Shared G_GameLevel			as UByte
Dim Shared G_GameStartTime		as Double
Dim Shared G_GamePoints			as UInteger
Dim Shared G_GameStones			as UInteger



'########################################################################################################################
' \/ see notes! \/
'########################################################################################################################
Dim Shared XTGUI_IMGBG as Any Ptr
Dim Shared XTGUI_IMGBU as Any Ptr
Dim Shared XTGUI_W as UInteger
Dim Shared XTGUI_H as UInteger

'------------------------------------------------------------------------------------------------------------------------
Sub XTGUI_Init(V_Width as UInteger, V_Height as UInteger)
If XTGUI_IMGBG <> 0 Then Exit Sub
XTGUI_W = V_Width
XTGUI_H = V_Height
XTGUI_IMGBG = ImageCreate(XTGUI_W, XTGUI_H, 32)
XTGUI_IMGBU = ImageCreate(XTGUI_W, XTGUI_H, 32)
Line XTGUI_IMGBG, (0, 0)-(XTGUI_W - 1, XTGUI_H - 1), &H00000000, BF
Line XTGUI_IMGBU, (0, 0)-(XTGUI_W - 1, XTGUI_H - 1), &H00000000, BF
End Sub

'------------------------------------------------------------------------------------------------------------------------
Sub XTGUI_Term()
If XTGUI_IMGBG <> 0 Then ImageDestroy(XTGUI_IMGBG): XTGUI_IMGBG = 0
If XTGUI_IMGBU <> 0 Then ImageDestroy(XTGUI_IMGBU): XTGUI_IMGBU = 0
End Sub

'------------------------------------------------------------------------------------------------------------------------
Sub XTGUI_Cls()
Line XTGUI_IMGBG, (0, 0)-(XTGUI_W - 1, XTGUI_H - 1), &H00000000, BF
Line XTGUI_IMGBU, (0, 0)-(XTGUI_W - 1, XTGUI_H - 1), &H00000000, BF
End Sub

'------------------------------------------------------------------------------------------------------------------------
Function XTGUI_GetDrawBuffer() as Any Ptr
Return XTGUI_IMGBG
End Function

'------------------------------------------------------------------------------------------------------------------------
Sub XTGUI_AddText(V_Left as Integer, V_Top as Integer, V_Text as String, V_TextColor as UInteger = &H00FFFFFF)
Draw String XTGUI_IMGBG, (V_Left + 5, V_Top + 5), V_Text, V_TextColor
End Sub

'------------------------------------------------------------------------------------------------------------------------
Sub XTGUI_AddButton(V_Left as Integer, V_Top as Integer, V_Width as UInteger, V_Height as UInteger, V_Text as String, V_Callback as Any Ptr, V_BackColor as UInteger = &H007070CC, V_TextColor as UInteger = &H00FFFFFF, V_BorderColor as UInteger = &H000000FF)
Line XTGUI_IMGBG, (V_Left, V_Top)-(V_Left + V_Width, V_Top + V_Height), V_BackColor, BF
Line XTGUI_IMGBG, (V_Left, V_Top)-(V_Left + V_Width, V_Top + V_Height), V_BorderColor, B
Draw String XTGUI_IMGBG, (V_Left + 5, V_Top + 5), V_Text, V_TextColor
Line XTGUI_IMGBU, (V_Left, V_Top)-(V_Left + V_Width, V_Top + V_Height), Cast(UInteger, V_Callback), BF
End Sub

'------------------------------------------------------------------------------------------------------------------------
Sub XTGUI_Draw(V_OffsetX as Integer, V_OffsetY as Integer)
Put (V_OffsetX, V_OffsetY), XTGUI_IMGBG, PSET
End Sub

'------------------------------------------------------------------------------------------------------------------------
Function XTGUI_CheckIO(V_OffsetX as Integer, V_OffsetY as Integer) as Integer
Static XTGUI_TX as Integer
Static XTGUI_TY as Integer
Static XTGUI_TZ as Integer
Static XTGUI_TB as Integer
Dim TR as Integer
Dim TX as Integer
Dim TY as Integer
Dim TZ as Integer
Dim TB as Integer
Dim RV as Integer = 0
TR = GetMouse(TX, TY, TZ, TB)
If TR = 0 Then
	If XTGUI_TB <> TB Then
		If TB = 1 Then
			If ((TX - V_OffsetX) >= 0) and ((TX - V_OffsetX) < XTGUI_W) Then
				If ((TY - V_OffsetY) >= 0) and ((TY - V_OffsetY) < XTGUI_H) Then
					Dim TSub as Sub (ByRef R_Return as Integer = 0)
					TSub = Cast(Any Ptr, Cast(UInteger Ptr, XTGUI_IMGBU + 32)[(TY - V_OffsetY) * XTGUI_W + (TX - V_OffsetX)])
					If TSub <> 0 Then TSub(RV)
				End If
			End If
		End If
	End If
	XTGUI_TX = TX
	XTGUI_TY = TY
	XTGUI_TZ = TZ
	XTGUI_TB = TB
End If
Return RV
End Function
'########################################################################################################################
' /\ see notes! /\
'########################################################################################################################



'########################################################################################################################
Sub Jewels_Exit(ByRef R_Return as Integer = 0)
R_Return = 1
End Sub

'------------------------------------------------------------------------------------------------------------------------
Sub Jewels_New(ByRef R_Return as Integer = 0)
R_Return = 2
End Sub

'------------------------------------------------------------------------------------------------------------------------
Sub Jewels_NewFill(ByRef R_Return as Integer = 0)
R_Return = 3
End Sub

'------------------------------------------------------------------------------------------------------------------------
Sub Jewels_NewRock(ByRef R_Return as Integer = 0)
R_Return = 4
End Sub

'------------------------------------------------------------------------------------------------------------------------
Sub Jewels_NewPuzzle(ByRef R_Return as Integer = 0)
R_Return = 5
End Sub

'------------------------------------------------------------------------------------------------------------------------
Sub Jewels_NewPuzzleRock(ByRef R_Return as Integer = 0)
R_Return = 6
End Sub

'------------------------------------------------------------------------------------------------------------------------
Sub Jewels_Abort(ByRef R_Return as Integer = 0)
R_Return = 10
End Sub

'------------------------------------------------------------------------------------------------------------------------
Sub Jewels_Resume(ByRef R_Return as Integer = 0)
R_Return = 11
End Sub



'#####################################################################################################################
Function TimeFormat(V_Value as UInteger) as String
Dim XR as UInteger = V_Value
Dim XH as UInteger = XR \ 3600:		XR = XR mod 3600
Dim XM as UInteger = XR \ 60:		XR = XR mod 60
Return Format(XH, "00") & ":" & Format(XM, "00") & ":" & Format(XR, "00")
End Function



'########################################################################################################################
Sub DoDraw()
Dim TImg as Any Ptr
ScreenLock()
CLS()
Dim X as Integer
For Y as Integer = 0 to G_FieldH - 1
	For X = 0 to G_FieldW - 1
		Select Case G_FieldBG(Y + 1, X + 1)
			Case 0: Put (X * G_ImgWH, Y * G_ImgWH), G_IMGBackground, PSET
			Case 1: Put (X * G_ImgWH, Y * G_ImgWH), G_IMGBackgroundOK, PSET
		End Select
	Next
Next
For Y as Integer = 0 to G_FieldH - 1
	For X = 0 to G_FieldW - 1
		Select Case G_Field(Y + 1, X + 1)
			Case 0
			Case 1 to 5 'steine
				If G_FieldNoShow(Y + 1, X + 1) = 0 Then
					Put (X * G_ImgWH + G_FieldOffsetX(Y + 1, X + 1), Y * G_ImgWH + G_FieldOffsetY(Y + 1, X + 1)), G_IMGJewelD(G_Field(Y + 1, X + 1)), ALPHA
				End If
				
			Case 6 to 10 'rock-steine
				If G_FieldNoShow(Y + 1, X + 1) = 0 Then
					Put (X * G_ImgWH, Y * G_ImgWH), G_IMGJewelD(G_Field(Y + 1, X + 1) - 5), ALPHA
				End If
				Put (X * G_ImgWH, Y * G_ImgWH), G_IMGRock, ALPHA
				
			Case 11 to 20 'puzzel
				Put (X * G_ImgWH + G_FieldOffsetX(Y + 1, X + 1), Y * G_ImgWH + G_FieldOffsetY(Y + 1, X + 1)), G_IMGPuzzleD(G_Field(Y + 1, X + 1) - 10), ALPHA
				
			Case 21 'unnutzbar
				Put (X * G_ImgWH, Y * G_ImgWH), G_IMGEarth, PSET
				
		End Select
	Next
Next
Dim TCol as UInteger = &H006666FF
If G_SelX <> 0 Then TCol = &H00FFFF00
If G_CurHide = 0 Then
	Line ((G_CurX - 1) * G_ImgWH, (G_CurY - 1) * G_ImgWH)-(G_CurX * G_ImgWH, G_CurY * G_ImgWH), TCol, B
	Line ((G_CurX - 1) * G_ImgWH - 1, (G_CurY - 1) * G_ImgWH - 1)-(G_CurX * G_ImgWH + 1, G_CurY * G_ImgWH + 1), TCol, B
End If

X = G_FieldH * G_ImgWH
Line (0, X)-(G_FieldW * G_ImgWH - 1, X + G_StatOffset - 1), &H00444444, BF
Line (0, X)-(G_FieldW * G_ImgWH - 1, X + G_StatOffset - 1), &H00888888, B
Draw String (3, X + 3), "Points:  " & Space(8 - IIf(Len(Str(G_GamePoints)) > 8, 8, Len(Str(G_GamePoints)))) & Str(G_GamePoints)
Draw String (3, X + 15), "Runtime: " & TimeFormat(Fix(Timer() - G_GameStartTime))
Draw String (3, X + 27), "Stones:  " & Space(8 - IIf(Len(Str(G_GameStones)) > 8, 8, Len(Str(G_GameStones)))) & Str(G_GameStones)
Select Case G_GameMode
	Case 2 to 5: Draw String (3, X + 39), "Level:   " & Space(8 - IIf(Len(Str(G_GameLevel)) > 8, 8, Len(Str(G_GameLevel)))) & Str(G_GameLevel)
End Select
ScreenUnLock()
End Sub



'########################################################################################################################
Function CheckGoal() as Integer
Dim X as Integer
Dim Y as Integer
Select Case G_GameMode
	Case 1: Return -1
	Case 2, 3
		For Y = 1 to G_FieldH
			For X = 1 to G_FieldW
				If G_Field(Y, X) <> 21 Then If G_FieldBG(Y, X) = 0 Then Return -1
			Next
		Next
		
	Case 4, 5
		Dim Z as Integer
		Dim X1 as Integer
		Dim Y1 as Integer
		For Y = 1 to G_FieldH
			For X = 1 to G_FieldW
				Select Case G_Field(Y, X)
					Case 11, 14, 17
						X1 = X
						Y1 = Y
						Z = G_Field(Y, X)
						Do
							Select Case Z
								Case 11 to 13
									X1 += 1
									If X1 > G_FieldW Then Return -1
									Select Case G_Field(Y, X1)
										Case 12
										Case 13: Exit Do
										Case Else: Return -1
									End Select
									
								Case 14 to 16
									Y1 += 1
									If Y1 > G_FieldH Then Return -1
									Select Case G_Field(Y1, X)
										Case 15
										Case 16: Exit Do
										Case Else: Return -1
									End Select
									
								Case Else: Exit Do
							End Select
							If (X1 = X) and (Y1 = Y) Then Exit Do
						Loop
				End Select
			Next
		Next
		
End Select
Return 1
End Function


'########################################################################################################################
Function CheckField() as Integer
Dim X as Integer
Dim Y as Integer
Dim Z as Integer
Dim TID as Integer
Dim C as Integer = 1
Dim SZ as Integer = 1
Dim TField(0 to G_FieldH, 0 to G_FieldW) as Integer
Dim RV as Integer = 0
Dim DRV as Integer
Dim SZX as Integer
Dim BC as UInteger
Do
	BC = 0
	DRV = 0
	For Y = 1 to G_FieldH
		For X = 1 to G_FieldW
			TField(Y, X) = 0
		Next
	Next
	For Y = 1 to G_FieldH
		TID = IIf(G_Field(Y, 1) <= 5, G_Field(Y, 1), IIf(G_Field(Y, 1) <= 10, G_Field(Y, 1) - 5, G_Field(Y, 1)))
		SZ = 1
		C = 1
		For X = 2 to G_FieldW
			If (IIf(G_Field(Y, X) <= 5, G_Field(Y, X), IIf(G_Field(Y, X) <= 10, G_Field(Y, X) - 5, G_Field(Y, X))) <> TID) or (X = G_FieldW) Then
				If X = G_FieldW Then If IIf(G_Field(Y, X) <= 5, G_Field(Y, X), IIf(G_Field(Y, X) <= 10, G_Field(Y, X) - 5, G_Field(Y, X))) = TID Then C += 1
				If C >= 3 Then
					Select Case TID
						Case 1 to 5, 6 to 10
							SZX = SZ + C - 1
							If SZX > G_FieldW Then SZX = G_FieldW
							For Z = SZ to SZX
								TField(Y, Z) = 1
							Next
							G_GamePoints += C
							If C = 4 Then G_GamePoints += 1
							If C = 5 Then G_GamePoints += 1
							If C = 6 Then G_GamePoints += 2
							If C = 7 Then G_GamePoints += 3
							If C > 7 Then G_GamePoints += C
							BC += C
					End Select
				End If
				TID = IIf(G_Field(Y, X) <= 5, G_Field(Y, X), IIf(G_Field(Y, X) <= 10, G_Field(Y, X) - 5, G_Field(Y, X)))
				SZ = X
				C = 1
			Else: C += 1
			End If
		Next
	Next
	For X = 1 to G_FieldW
		TID = IIf(G_Field(1, X) <= 5, G_Field(1, X), IIf(G_Field(1, X) <= 10, G_Field(1, X) - 5, G_Field(1, X)))
		SZ = 1
		C = 1
		For Y = 2 to G_FieldH
			If (IIf(G_Field(Y, X) <= 5, G_Field(Y, X), IIf(G_Field(Y, X) <= 10, G_Field(Y, X) - 5, G_Field(Y, X))) <> TID) or (Y = G_FieldH) Then
				If Y = G_FieldH Then If IIf(G_Field(Y, X) <= 5, G_Field(Y, X), IIf(G_Field(Y, X) <= 10, G_Field(Y, X) - 5, G_Field(Y, X))) = TID Then C += 1
				If C >= 3 Then
					Select Case TID
						Case 1 to 5, 6 to 10
							SZX = SZ + C - 1
							If SZX > G_FieldH Then SZX = G_FieldH
							For Z = SZ to SZX
								TField(Z, X) = 1
							Next
							G_GamePoints += C
							If C = 4 Then G_GamePoints += 1
							If C = 5 Then G_GamePoints += 2
							If C = 6 Then G_GamePoints += 3
							If C = 7 Then G_GamePoints += 4
							If C > 7 Then G_GamePoints += C
							BC += C
					End Select
				End If
				TID = IIf(G_Field(Y, X) <= 5, G_Field(Y, X), IIf(G_Field(Y, X) <= 10, G_Field(Y, X) - 5, G_Field(Y, X)))
				SZ = Y
				C = 1
			Else: C += 1
			End If
		Next
	Next
	If BC = 6 Then G_GamePoints += 1
	If BC = 7 Then G_GamePoints += 2
	If BC = 8 Then G_GamePoints += 3
	If BC = 9 Then G_GamePoints += 4
	If BC = 10 Then G_GamePoints += 5
	C = 0
	For Z = 1 to 4
		For Y = 1 to G_FieldH
			For X = 1 to G_FieldW
				If TField(Y, X) = 1 Then
					If G_FieldNoShow(Y, X) = 0 Then
						G_FieldNoShow(Y, X) = 1
					Else: G_FieldNoShow(Y, X) = 0
					End If
					C = 1
				End If
			Next
		Next
		If C = 1 Then
			DoDraw()
			Sleep 150, 1
			C = 0
		End If
	Next
	
	For Y = 1 to G_FieldH
		For X = 1 to G_FieldW
			If TField(Y, X) = 1 Then
				Select Case G_Field(Y, X)
					Case 1 to 5
						G_Field(Y, X) = 0
						Select Case G_GameMode
							Case 2, 3
								G_FieldBG(Y, X) = 1
								G_GamePoints += 10
								
						End Select
						RV = 1
						DRV = 1
						
					Case 6 to 10
						G_GamePoints += 10
						G_Field(Y, X) -= 5
						RV = 1
						DRV = 1
						
				End Select
			End If
		Next
	Next
	
	Do
		C = 0
		For X = 1 to G_FieldW
			For Y = G_FieldH to 1 Step -1
				If G_Field(Y, X) = 0 Then
					If Y = 1 Then
						G_Field(0, X) = Int((Rnd * 5) + 1)
						G_GamePoints += 1
						G_FieldOffsetY(0, X) = -G_ImgWH
						G_GameStones += 1
					End If
					Select Case G_Field(Y - 1, X)
						Case 1 to 5, 11 to 20
							G_Field(Y, X) = G_Field(Y - 1, X)
							G_Field(Y - 1, X) = 0
							G_FieldOffsetY(Y, X) = -G_ImgWH
							C = 1
					End Select
				End If
			Next
		Next
		If C = 0 Then Exit Do
		For Z = 1 to G_ImgWH
			For X = 1 to G_FieldW
				For Y = G_FieldH to 0 Step -1
					If G_FieldOffsetY(Y, X) < 0 Then G_FieldOffsetY(Y, X) += 1
				Next
			Next
			DoDraw()
			Sleep 10, 1
		Next
	Loop
	
	For X = 1 to G_FieldW
		If G_Field(1, X) = 0 Then G_Field(1, X) = Int((Rnd * 5) + 1)
	Next
	If DRV = 0 Then Exit Do
Loop
Return RV
End Function



'########################################################################################################################
Function DoMove(V_Direction as UByte) as Integer
If G_SelX <> 0 Then
	Dim TID1 as Integer = G_Field(G_CurY, G_CurX)
	G_SelX = G_CurX
	G_SelY = G_CurY
	Select Case V_Direction
		Case 1
			If G_CurX <= 1 Then Return 0
			G_SelX = G_CurX - 1
			
		Case 2
			If G_CurX >= G_FieldW Then Return 0
			G_SelX = G_CurX + 1
			
		Case 3
			If G_CurY <= 1 Then Return 0
			G_SelY = G_CurY - 1
			
		Case 4
			If G_CurY >= G_FieldH Then Return 0
			G_SelY = G_CurY + 1
			
	End Select
	Select Case G_Field(G_SelY, G_SelX)
		Case 0, 1 to 5, 11 to 20
		Case Else: Return 0
	End Select
	Dim TID2 as Integer = G_Field(G_SelY, G_SelX)
	G_Field(G_CurY, G_CurX) = TID2
	G_Field(G_SelY, G_SelX) = TID1
	If (CheckField() <> 1) and (TID2 <> 0) Then
		G_Field(G_CurY, G_CurX) = TID1
		G_Field(G_SelY, G_SelX) = TID2
		G_SelX = 0
		G_SelY = 0
	Else
		G_SelX = 0
		G_SelY = 0
	End If
	CheckField()
Else
	Dim C as UInteger
	Do
		C += 1
		Select Case V_Direction
			Case 1: If G_CurX = 1 Then G_CurX = G_FieldW Else G_CurX -= 1
			Case 2: If G_CurX = G_FieldW Then G_CurX = 1 Else G_CurX += 1
			Case 3: If G_CurY = 1 Then G_CurY = G_FieldH Else G_CurY -= 1
			Case 4: If G_CurY = G_FieldH Then G_CurY = 1 Else G_CurY += 1
		End Select
		Select Case V_Direction
			Case 1, 2: If C = G_FieldW Then Exit Do
			Case 3, 4: If C = G_FieldH Then Exit Do
		End Select
		If G_Field(G_CurY, G_CurX) <> 21 Then Exit Do
	Loop
End If
Return CheckGoal()
End Function



'########################################################################################################################
Sub Main()
Randomize(Timer())
ScreenRes G_FieldW * G_ImgWH, G_FieldH * G_ImgWH + G_StatOffset, 32
WindowTitle "JewelX"

Dim X as UInteger
For X = 1 to 5
	G_IMGJewelD(X) = ImageCreate(G_ImgWH, G_ImgWH, 32)
	BLoad "j" & Str(X) & ".bmp", G_IMGJewelD(X)
Next
For X = 1 to 10
	G_IMGPuzzleD(X) = ImageCreate(G_ImgWH, G_ImgWH, 32)
	BLoad "p" & Str(X) & ".bmp", G_IMGPuzzleD(X)
Next
G_IMGRock = ImageCreate(G_ImgWH, G_ImgWH, 32)
BLoad "rock.bmp", G_IMGRock
G_IMGEarth = ImageCreate(G_ImgWH, G_ImgWH, 32)
BLoad "earth.bmp", G_IMGEarth
G_IMGBackground = ImageCreate(G_ImgWH, G_ImgWH, 32)
BLoad "background.bmp", G_IMGBackground
G_IMGBackgroundOK = ImageCreate(G_ImgWH, G_ImgWH, 32)
BLoad "backgroundok.bmp", G_IMGBackgroundOK

Redim Preserve G_Field(0 to G_FieldH, 0 to G_FieldW) as Integer
Redim Preserve G_FieldBG(0 to G_FieldH, 0 to G_FieldW) as Integer
Redim Preserve G_FieldNoShow(0 to G_FieldH, 0 to G_FieldW) as Integer
Redim Preserve G_FieldOffsetX(0 to G_FieldH, 0 to G_FieldW) as Integer
Redim Preserve G_FieldOffsetY(0 to G_FieldH, 0 to G_FieldW) as Integer

Dim TKey as String
Dim TKey1 as UByte
Dim TKey2 as UByte
Dim Y as Integer
Dim TGUIW as Integer = G_FieldW * G_ImgWH - 60
Dim RV as Integer
Dim TNoReset as UByte
XTGUI_Init(G_FieldW * G_ImgWH, G_FieldH * G_ImgWH + G_StatOffset)
G_GameLevel = 0
G_CurX = 1
G_CurY = 1
Do
	XTGUI_Cls()
	Line XTGUI_GetDrawBuffer(), (1, 1)-(G_FieldW * G_ImgWH - 2, G_FieldH * G_ImgWH + G_StatOffset - 2), &H0000FF00, B
	XTGUI_AddText(20, 10, "JewelsX - Version: keine Ahnung")
	XTGUI_AddButton(30, 50, TGUIW, 25, "START Regular", @Jewels_New)
	XTGUI_AddButton(30, 80, TGUIW, 25, "START Fill", @Jewels_NewFill)
	XTGUI_AddButton(30, 110, TGUIW, 25, "START Fill & Rocks", @Jewels_NewRock)
	XTGUI_AddButton(30, 140, TGUIW, 25, "START Puzzle", @Jewels_NewPuzzle)
	XTGUI_AddButton(30, 170, TGUIW, 25, "START Puzzle & Rocks", @Jewels_NewPuzzleRock)
	XTGUI_AddButton(30, 250, TGUIW, 25, "EXIT", @Jewels_Exit)
	XTGUI_AddText(50, G_FieldH * G_ImgWH + G_StatOffset - 30, "Coder: ThePuppetMaster")
	XTGUI_Draw(0, 0)
	Do
		Select Case G_GameMode
			Case 2
				If G_GameLevel < 7 Then
					RV = G_GameMode + 1
					TNoReset = 1
					G_GamePoints += (G_GameLevel * 100) * G_GameMode
				End If
				
			Case 4, 5
				If G_GameLevel < 7 Then
					RV = G_GameMode + 1
					TNoReset = 1
					G_GamePoints += (G_GameLevel * 100) * G_GameMode
				End If
				
			Case Else: RV = XTGUI_CheckIO(0, 0): G_GameLevel = 0
		End Select
		Select Case RV
			Case 0
			Case 1: Exit Sub
			Case 2 to 6
				G_GameMode = RV - 1
				For Y = 1 to G_FieldH
					For X = 1 to G_FieldW
						G_FieldBG(Y, X) = 0
						G_FieldNoShow(Y, X) = 0
						G_FieldOffsetX(Y, X) = 0
						G_FieldOffsetY(Y, X) = 0
						Select Case G_GameMode
							Case 1, 2, 4: G_Field(Y, X) = Int((Rnd * 5) + 1)
							Case 3, 5: G_Field(Y, X) = IIf(Int(Rnd * 2) = 0, Int(Rnd * 10), Int(Rnd * 4))
						End Select
					Next
				Next
				G_SelX = 0
				G_SelY = 0
				G_CurHide = 1
				If TNoReset = 0 Then
					TNoReset = 0
					G_CurX = 1
					G_CurY = 1
					G_GameStartTime = Timer()
					G_GamePoints = 0
					G_GameStones = 0
					G_GameLevel = 0
				End If
				Select Case G_GameMode
					Case 1 to 3: G_GameLevel += 1
					Case 4, 5: If G_GameLevel < 10 Then G_GameLevel += 1
				End Select
				CheckField()
				Select Case G_GameMode
					Case 2
						Select Case G_GameLevel
							Case 2
								G_Field(5, 1) = 21
								G_Field(6, 1) = 21
								G_Field(5, G_FieldW) = 21
								G_Field(6, G_FieldW) = 21
								
						End Select
					Case 4, 5
						Select Case G_GameLevel
							Case 1
								G_Field(4, 6) = 14
								G_Field(6, 6) = 16
								
							Case 2
								G_Field(3, 6) = 14
								G_Field(5, 6) = 15
								G_Field(7, 6) = 16
								
							Case 3
								G_Field(4, 6) = 11
								G_Field(5, 7) = 13
								
							Case 4
								G_Field(4, 6) = 11
								G_Field(5, 7) = 12
								G_Field(6, 8) = 13
								
							Case 5
								G_Field(2, 6) = 11
								G_Field(4, 8) = 12
								G_Field(5, 9) = 13
								
							Case 6
								G_Field(2, 6) = 14
								G_Field(4, 8) = 15
								G_Field(6, 10) = 16
								
							Case 7
								G_Field(2, 6) = 14
								G_Field(4, 8) = 15
								G_Field(6, 10) = 16
								G_Field(3, 2) = 11
								G_Field(5, 3) = 12
								G_Field(4, 4) = 12
								G_Field(7, 8) = 13
								
							Case 8
								G_Field(2, 6) = 14
								G_Field(4, 8) = 15
								G_Field(2, 12) = 15
								G_Field(6, 10) = 16
								G_Field(3, 2) = 11
								G_Field(5, 3) = 12
								G_Field(4, 4) = 12
								G_Field(7, 8) = 13
								
							Case 9
								G_Field(8, 10) = 14
								G_Field(2, 6) = 14
								G_Field(4, 8) = 15
								G_Field(2, 12) = 15
								G_Field(6, 10) = 16
								G_Field(1, 1) = 16
								G_Field(3, 2) = 11
								G_Field(5, 3) = 12
								G_Field(4, 4) = 12
								G_Field(7, 8) = 13
								
							Case 10
								G_Field(8, 10) = 14
								G_Field(2, 6) = 15
								G_Field(4, 8) = 15
								G_Field(2, 12) = 15
								G_Field(6, 10) = 15
								G_Field(1, 1) = 15
								G_Field(3, 2) = 15
								G_Field(5, 3) = 15
								G_Field(4, 4) = 15
								G_Field(7, 8) = 16
								
						End Select
				End Select
				CheckField()
				If CheckGoal() <> 1 Then
					CheckField()
					G_CurHide = 0
					Do Until InKey() = ""
					Loop
					Do
						TKey1 = 0
						TKey2 = 0
						TKey = InKey()
						If Len(TKey) > 0 Then TKey1 = TKey[0]
						If Len(TKey) > 1 Then TKey2 = TKey[1]
						Select Case TKey1
							Case 0
							Case 13
								If G_SelX = 0 Then
									Select Case G_Field(G_CurY, G_CurX)
										Case 1 to 5, 11 to 20
											G_SelX = G_CurX
											G_SelY = G_CurY
									End Select
								Else
									G_SelX = 0
									G_SelY = 0
								End If
								
							Case 27
								XTGUI_Cls()
								Line XTGUI_GetDrawBuffer(), (1, 1)-(G_FieldW * G_ImgWH - 2, G_FieldH * G_ImgWH + G_StatOffset - 2), &H00FF0000, B
								XTGUI_AddText(20, 10, "Abort game?")
								XTGUI_AddButton(30, 50, TGUIW, 25, "RESUME", @Jewels_Resume)
								XTGUI_AddButton(30, 250, TGUIW, 25, "ABORT", @Jewels_Abort)
								XTGUI_AddText(50, G_FieldH * G_ImgWH + G_StatOffset - 30, "Coder: ThePuppetMaster")
								XTGUI_Draw(0, 0)
								Do
									RV = XTGUI_CheckIO(0, 0)
									If RV <> 0 Then Exit Do
									Sleep 1, 1
								Loop
								If RV = 10 Then G_GameMode = 0: Exit Do
								
							Case Else
								G_CurHide = 1
								Select Case TKey2
									Case 75: If DoMove(1) = 1 Then Exit Do 'left
									Case 77: If DoMove(2) = 1 Then Exit Do 'right
									Case 72: If DoMove(3) = 1 Then Exit Do 'top
									Case 80: If DoMove(4) = 1 Then Exit Do 'bot
									Case 107: Exit Do
									'Case Else: Print Str(TKey1) & " " & Str(TKey2): Sleep 1000, 1
								End Select
								G_CurHide = 0
						End Select
						DoDraw()
						Sleep 1, 1
					Loop
				End If
				Exit Do
		End Select
		Sleep 1, 1
	Loop
	For Y = 1 to G_FieldH
		For X = 1 to G_FieldW
			G_FieldNoShow(Y, X) = 1
		Next
	Next
	DoDraw()
Loop
End Sub



'#################################################################################################################################################################
Main()
Screen 0
End 0


