
; #FUNCTION# ====================================================================================================================
; Name ..........: waitMainScreen
; Description ...: Waits 5 minutes for the pixel of mainscreen to be located, checks for obstacles every 2 seconds.  After five minutes, will try to restart bluestacks.
; Syntax ........: waitMainScreen()
; Parameters ....:
; Return values .: None
; Author ........:
; Modified ......: KnowJack (July/Aug 2015), TheMaster (2015)
; Remarks .......: This file is part of MyBot, previously known as ClashGameBot. Copyright 2015
;                  MyBot is distributed under the terms of the GNU GPL
; Related .......:
; Link ..........: https://github.com/MyBotRun/MyBot/wiki
; Example .......: No
; ===============================================================================================================================

Func waitMainScreen() ;Waits for main screen to popup

    Local $iCount
	SetLog("Waiting for Main Screen")
	$iCount = 0
	For $i = 0 To 105 ;105*2000 = 3.5 Minutes
		If $debugsetlog = 1 Then Setlog("ChkObstl Loop = " & $i & "ExitLoop = " & $iCount, $COLOR_PURPLE) ; Debug stuck loop
		$iCount += 1
		If WinExists($Title) = False Then
			OpenAndroid(True)
			Return
	    EndIf
		getBSPos() ; Update $HWnd and Android Window Positions
		_CaptureRegion()
		If _CheckPixel($aIsMain, $bNoCapturepixel) = False Then ;Checks for Main Screen
			If _Sleep($iDelaywaitMainScreen1) Then Return
			If checkObstacles() Then $i = 0 ;See if there is anything in the way of mainscreen
		Else
			If $debugsetlog = 1 Then Setlog("Screen cleared, WaitMainScreen exit", $COLOR_PURPLE)
			Return
		EndIf
		If ($i > 105) Or ($iCount > 120) Then ExitLoop  ; If CheckObstacles forces reset, limit total time to 4 minutes
	Next

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	CloseCoC(True) ; Close then Open CoC
	If _CheckPixel($aIsMain, True) Then Return ; If its main screen return
	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	; If mainscreen is not found, then fix it
	$iCount = 0
	While 1
		SetLog("Unable to load CoC, attempt to fix it...", $COLOR_RED)
		If $debugsetlog = 1 Then Setlog("Restart Loop = " & $iCount, $COLOR_PURPLE) ; Debug stuck loop data
		CloseAndroid() 	 ; BS must die!
		If _Sleep(1000) Then Return
		OpenAndroid(True) ; Open BS and restart CoC
		If @extended Then
			SetError(1, 1, -1)
			Return
		EndIf
		If _CheckPixel($aIsMain, $bCapturepixel) = True Then ExitLoop
		CheckObstacles()  ; Check for random error windows and close them
		$iCount += 1
		If $iCount > 2 Then ; If we can't restart BS after 2 tries, exit the loop
			SetLog("Stuck trying to Restart " & $Android & "...", $COLOR_RED)
			SetError(1, 0, 0)
			Return
		EndIf
		If _CheckPixel($aIsMain, $bCapturepixel) = True Then ExitLoop
	WEnd

EndFunc   ;==>waitMainScreen
