;==================================================================
;Project Title: CRYSTALIUM
;Author:        Vladislav Borzunov aka ObelardO
;Email:         Obelardos@gmail.com 
;Version:       0.9 Alpha
;Date:          22.02.13
;Notes:         Console code-file
;==================================================================

Global quotes$ = Chr(34)

; Console command execution
Function ConsoleExecute(command$)

	If Left(command,1)=";" command=""

	If Not Command = ""
		command = Replace(command, "'", quotes)
		While Not Instr(Command,"/") = 0
			command = Right(command,Len(command) - Instr(command,"/"))
			ConsoleExecute(StrCut(command, 1, "/"))
		Wend
	End If

	Select StrCut(Command,1)
		Case "start"
			If Not WINDOW_LOADED 
				Loggin("[INFO] Game starting...")
				xAppTitle WINDOW_TITLE
				xAppWindowFrame WINDOW_BORDER
				xSetAntiAliasType RENDER_ANTIALIAS; xGetMaxAntiAlias()
				xGraphics3D WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_DEPTH, WINDOW_MODE, RENDER_VSYNC
				WINDOW_WIDTH = xGraphicsWidth() :WINDOW_HEIGHT = xGraphicsHeight()
				xHidePointer()
				xAntiAlias True
				WINDOW_LOADED = 1
				Loggin("[INFO] Game started.")
			Else
				Loggin("[ERROR] Game is already running.")
			End If

		Case "stop"
			Loggin("[INFO] Game stoped.")
			End
			
		Case "config"
			FileName$ = StrCut(command,2,quotes)
			Config$ = OpenFile(FileName)
			Loggin("[INFO] Config load "+StrQuote(FileName))
			If Config
				While Not Eof(Config)
					ConfigLine$ = ReadLine(Config)
					;If Left(ConfigLine,3) = "set" ConsoleExecute(ConfigLine)
					If StrCut(ConfigLine,1) = "set" And StrCut(ConfigLine,1) <> "" ConsoleExecute(ConfigLine)
				Wend
				CloseFile(Config)
				Loggin("[INFO] Config "+StrQuote(FileName)+" completed.")
			Else
				Loggin("[ERROR] Config "+StrQuote(FileName)+" not found.")
			End If

		Case "set"
			 variable$ = Replace(Command, "set ", "")
			Parameter$ = Lower(StrCut(StrCut(variable, 1), 2, "_"))
			  Section$ = Lower(StrCut(StrCut(variable, 1), 1, "_"))
			 StrValue$ = StrCut(variable, 2, quotes)
			  IntValue = StrToInt(StrValue)
			Loggin("[INFO] Set "+StrQuote(Section+"_"+Parameter)+" to "+StrQuote(StrValue))
			Select Section
				Case "r" ; Render
					Select Parameter
						Case "windowmode":     WINDOW_MODE      = IntValue
						Case "windowborder":   WINDOW_BORDER    = IntValue
						Case "windowwidth":    WINDOW_WIDTH     = IntValue
						Case "windowheight":   WINDOW_HEIGHT    = IntValue
						Case "colordepth":     WINDOW_DEPTH     = IntValue
						Case "vsync":          RENDER_VSYNC     = IntValue
						Case "antialias":      RENDER_ANTIALIAS = IntValue
						Case "windowtitle":    WINDOW_TITLE     = StrValue
						Default Loggin("[ERROR] Parametr "+StrQuote(Parameter)+" does not exist!")
					End Select
				Case "sy" ; System
					Select Parameter
						Case "logfile":        LOGGING_FILE     = StrValue
						Case "logmode":        LOGGING_MODE     = IntValue
						Case "debugmode":      DEBUG_MODE       = IntValue
						Case "language":       GAME_LANGUAGE    = StrValue
						Case "debugmap":       MAP_DEBUG        = IntValue
						Default Loggin("[ERROR] Parametr "+StrQuote(Parameter)+" does not exist!")
					End Select
				Case "g" ; Gameplay
					Select Parameter
						Case "lightpower":     LIGHT_POWER 	    = IntValue
						Case "zooming"   :PLAYER_CAMERA_ZOOMING = IntValue
						Default Loggin("[ERROR] Parametr "+StrQuote(Parameter)+" does not exist!")
					End Select
				Default Loggin("[ERROR] Section "+StrQuote(Section)+" does not exist!")
			End Select

		Case "logstart"
			If Not LOGGING_MODE
				LOGGING_MODE  = 1
				LOGGING_STEAM = WriteFile(LOGGING_FILE)
				Loggin("[INFO] logging start in "+StrQuote(LOGGING_FILE))
			Else
				Loggin("[ERROR] logging is already running.")
			End If

		Case "logstop"
			If LOGGING_MODE
				Loggin("[INFO] logging stoped.")
			      OGGING_MODE = 0
				CloseFile(LOGGING_STEAM)
			Else
				Loggin("[ERROR] logging is not running.")
			End If
				
		Case "logfile"
			If Not LOGGING_MODE
				LOGGING_FILE = StrCut(Command,2,quotes)
				Loggin("[INFO] logging file change to "+StrQuote(LOGGING_FILE))
			Else
				Loggin("[ERROR] logging is already running.")
			End If
		Case "level"
			If WINDOW_LOADED MapLoad(StrCut(Command,2)) Else MAP_NAME = StrCut(Command,2)
		Case "restart"
			If WINDOW_LOADED MapLoad(MAP_NAME)
		Default
			Loggin("[ERROR] Command "+StrQuote(Command)+" does not exist!")
	End Select
	
End Function

; transfer command line to console
Function CheckCommandLine()
	Commands$ = CommandLine()
	If Not Commands = ""
		;commands = Replace(commands, "'", quotes)
		ConsoleExecute(commands)
;		While Not Instr(Commands,"/") = 0
;			commands = Right(commands,Len(commands) - Instr(commands,"/"))
;			ConsoleExecute(StrCut(commands, 1, "/"))
;		Wend
	End If
End Function

; logging and debug
Function Loggin(InputString$)
	Time$ = CurrentTime()
	If LOGGING_MODE WriteLine(LOGGING_STEAM, Time+ ": " + InputString$)
	DebugLog Time + ": " + InputString$
End Function 

; extraction words from string
Function StrCut$(InputString$, WordNum, Seperators$ = " ")

        FoundWord  = False
        WordsFound = 0

        For CharLoop = 1 To Len(InputString$)
        	ThisChar$ = Mid$(InputString$, CharLoop, 1)
            If Instr(Seperators$, ThisChar$, 1)
            	If FoundWord
                  	WordsFound = WordsFound + 1
                        If WordsFound = WordNum
                        	Return Word$
                        Else
					Word$ = ""
					FoundWord = False
                        End If
                  End If                                                
            Else
            	FoundWord = True
                  Word$ = Word$ + ThisChar$                     
            End If
        Next    

        If (WordsFound+1) = WordNum Return Word$ Else Return ""
End Function

; Enclosing the string in quotes
Function StrQuote$(InputString$)
	Return Quotes+InputString+Quotes
End Function

; Determines string is numeric
Function StrToInt(InputString$)
	For CharLoop = 1 To Len(InputString$)
		ThisChar$ = Mid$(InputString$, CharLoop, 1)
		IntDetected = False
		For IntChar = 48 To 57
			If Asc(ThisChar) = IntChar IntDetected = True
		Next
		If IntDetected = False Return False
	Next
	Return Int(InputString)
End Function 