##########################################################################
#	Settings.py 	Blender 2.66a
#
#	Created by Florian Voelker (iFlowProduction)
#	Games, Downloads and co. at http://iflowproduction.de
#
#	Released under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0).
#	http://creativecommons.org/licenses/by-nc-sa/3.0/
#	
#	Feel free to change it for your own application(s), but leave this 
#	Header and inform others about the license.
#	If you use this code, please include this information header and don't change the license.
#
#	If you have any questions, please contact me.
#
#	Version 1.0
##########################################################################

from bge import logic
from bge import events

import sqlite3 as SQL
import sys, GameLogic, Rasterizer, os, subprocess

# get the path to the blend directory
mainDir = GameLogic.expandPath("//")

#Activate/Deactivate the Debug and Error-visibility (1 = True/ 0 = False)
Debug = 0
Errors = 1

def name():
	scene = logic.getCurrentScene()
	
	for object in scene.objects:
		print(object.name)

## Load UserID from File
def ReadLastUserFile(cont):
	obj = cont.owner

	try:
		f = open(logic.expandPath('//Database/LastUser.cfg'), 'r')
		obj["UserID"] = int(f.readline()[:-1]) #load a int
		f.close()
		if Debug:
			print("Loading UserID Successful!")
	except:
		if Errors:
			print("Loading from File Failed!")

## Save UserID to GlobalDict
def SaveIDgd(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	obj = cont.owner
	
	logic.globalDict["UserID"] = objList["Startup_Mngr_3.5"]["UserID"]
	
	cont.activate(cont.actuators[0])
	
	if Debug:
		print("saved!!!!!!")

## Save UserID to File		
def SaveID(cont):
	obj = cont.owner
	
	try:
		First_Run = obj['FirstRun']
	except:
		if Debug:
			print("Can't find obj['FirstRun']")
			
	#Save Settings to File
	try:
		f = open(logic.expandPath('//Database/LastUser.cfg'), 'w')
		f.write(str(obj["UserID"]) + '\n')
		f.close()
		if Debug:
			print("Save to File Successful!")
	except:
		if Errors:
			print("Save to File Failed!")
	
	if Debug:
		print('UserID = ' + str(cont.owner["UserID"]) + ' saved!')
	
	#Try, because used with different objects
	try:
		# get actuator attached to the controller named switchScene
		act_Menu = cont.actuators["setMenu"]
		act_Settings = cont.actuators["setSettings"]
		
		if First_Run:
			cont.activate(act_Settings)
		else:	
			cont.activate(act_Menu)
	except:
		if Debug:
			print("Can't find variable 'First_Run'")

## Load UserID from GlobalDict			
def Loadgd(cont):
	obj = cont.owner
	
	logic.loadGlobalDict()
		
	obj["UserID"] = logic.globalDict["UserID"]
	
	if Debug:
		print("loaded!!!!")
		
	obj.state = 1

## Load User(-settings) from Database (and store variable in properties)
def Load(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	obj = cont.owner
	
	#--------------------------------------------------------------------
	#Get the properties for Settings
	ReadLastUserFile(cont)
	UserID = obj['UserID']							#The User - ID

	#--------------------------------------------------------------------	
	#Connect to the Database
	con = SQL.connect(mainDir + "Database/User.db")
	
	cur = con.cursor()
	#Load the Database-row with the choosen "Auto-ID"
	cur.execute("SELECT * FROM User WHERE UserID=:Id", {"Id": UserID})
	con.commit()

	data = cur.fetchone()

	#--------------------------------------------------------------------
	#Define and save the loaded Database-row-Data
	UserName = data[1]
	Money = data[2]
	Driver = data[3]
	Level = data[4]
	List_Nr	= data[5]
	Points = data[6]
	Skills = data[7]
	SSAO = data[8]
	HUD = data[9]
	Smoke = data[10]
	Motionblur = data[11]
	LOD = data[12]
	Minimap = data[13]
	Playtime = data[14]
	Real_Mode = data[15]
	Cheats = data[16]
	First_Run = data[17]
	Available = data[18]
	Language = data[19]
	Sounds = data[20]
	Music = data[21]
	Notifications = data[22]
	Light = data[23]
	DOF = data[24]
	AF = data[25]
	Water = data[26]
	Updates = data[27]
	a_3D = data[28]
	Shadows = data[29]
	AimCross = data[30]
	
	#Close the Database-Connection
	if con:
		con.close()
	
	if Debug:
		print("User " + UserName + " Settings loaded")
		
	#--------------------------------------------------------------------
	# Store the Data into properties
	obj['Cur_Money'] = Money
	obj['OP_SSAO'] = SSAO
	obj['OP_HUD'] = HUD
	obj['OP_SMOKE'] = Smoke
	obj['OP_MotionBlur'] = Motionblur
	obj['OP_LOD'] = LOD
	obj['OP_Minimap'] = Minimap
	obj['OP_RealMode'] = Real_Mode
	obj['OP_Cheats'] = Cheats
	obj['OP_Sounds'] = Sounds
	obj['OP_Music'] = Music
	obj['OP_Notifications'] = Notifications
	obj['OP_Language'] = Language
	obj['OP_Light'] = Light
	obj['OP_DOF'] = DOF
	obj['OP_AF'] = AF
	obj['OP_Water'] = Water
	obj['OP_Updates'] = Updates
	obj['OP_a_3D'] = a_3D
	obj['OP_Shadows'] = Shadows
	obj['OP_AimCross'] = AimCross

## Save User(-settings) to Database	
def Save(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	obj = cont.owner		

	#--------------------------------------------------------------------
	#Get the properties for Settings
	UserID = obj['UserID']							#The User - ID
	
	Prop_SSAO = obj['OP_SSAO']						#The SSAO - ID (Bool)
	Prop_HUD = obj['OP_HUD']						#The HUD - ID (Bool)
	Prop_SMOKE = obj['OP_SMOKE']					#The SMOKE - ID (Bool)
	Prop_MotionBlur = obj['OP_MotionBlur']			#The MotionBlur - ID (Bool)
	Prop_LOD = obj['OP_LOD']						#The LOD - ID (Bool)
	Prop_Minimap = obj['OP_Minimap']				#The Minimap - ID (Bool)
	Prop_RealMode = obj['OP_RealMode']				#The RealMode - ID (Bool)
	Prop_Cheats = obj['OP_Cheats']					#The Cheats - ID (Bool)
	Prop_Sounds = obj['OP_Sounds']					#The Sounds - ID (Bool)
	Prop_Music = obj['OP_Music']					#The Muic - ID (Bool)
	Prop_Notifications = obj['OP_Notifications']	#The Notifications - ID (Bool)
	Prop_Language = obj['OP_Language']				#The Language - ID
	Prop_Light = obj['OP_Light']					#The Light - ID (Bool)
	Prop_DOF = obj['OP_DOF']						#The DOF - ID (Bool)
	Prop_AF = obj['OP_AF']							#The Anisotropic - ID
	Prop_Water = obj['OP_Water']					#The Water-Quality - ID
	Prop_Updates = obj['OP_Updates']				#The Check for Updates - ID (Bool)
	Prop_a_3D = obj['OP_a_3D']						#The Analgyph 3D - ID (Bool)
	Prop_Shadows = obj['OP_Shadows']				#The Shadows-Active - ID (Bool)
	Prop_AimCross = obj['OP_AimCross']				#The AimCross-Active - ID (Bool)
	
#--------------------------------------------------------------------
	#Connect to the Database
	con = SQL.connect(mainDir + "Database/User.db")
	
	cur = con.cursor()
	#Load the Database-row with the choosen "Auto-ID"
	#update <tabellen-name> set <spalten-name> = <wert> [ , <spalten-name> = <wert> [ , < ... > ] ] [ where <bedingungen> ] ;
	cur.execute("UPDATE User SET SSAO =?, HUD =?, Smoke =?, Motionblur =?, LOD =?, Minimap =?, Cheats =?, First_Run ='0', Language =?, Sounds =?, Music =?, Notifications =?, Light =?, DOF =?, AF =?, Water =?, Updates =?, a_3D =?, Shadows =?, AimCross =? WHERE UserID=?", (Prop_SSAO, Prop_HUD, Prop_SMOKE, Prop_MotionBlur, Prop_LOD, Prop_Minimap, Prop_Cheats, Prop_Language, Prop_Sounds, Prop_Music, Prop_Notifications, Prop_Light, Prop_DOF, Prop_AF, Prop_Water, Prop_Updates, Prop_a_3D, Prop_Shadows, Prop_AimCross, UserID))
	
	con.commit()

	data = cur.fetchone()
	
	#Close the Database-Connection
	if con:
		con.close()
	
	if Debug:
		print("User Settings for UserID " + str(UserID) + " saved")
		
## Load the maximum number of User-datasets
def LoadUserNumber(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	obj = cont.owner
	
	#--------------------------------------------------------------------
	#Get the User-Count and set it to zero
	UserNumber = obj['UserNumber'] = 0
	
	#--------------------------------------------------------------------
	#Connect to the Database
	con = SQL.connect(mainDir + "Database/User.db")
	
	cur = con.cursor()
	cur.execute("SELECT * FROM User")
	
	rows = cur.fetchall()
	
	#--------------------------------------------------------------------
	#Read out each row from Database
	for data in rows:
		#Increase the UserNumber to get the maximum User-Count at the end
		obj['UserNumber'] = obj['UserNumber'] + 1
	
	#Close the Database-Connection
	if con:
		con.close()

## Check, if the selected/ choosen UserID is possible and correct it if necessary		
def IDcheck(cont):
	obj = cont.owner
	
	#Get the properties for User-Selection
	UserNumber = obj['UserNumber']	#The maximum number of users
	UserID = obj['UserID']			#The current User-ID
	
	#--------------------------------------------------------------------
	#Check if the selected UserID is possible
	if UserID > UserNumber:
		obj['UserID'] = 1
	elif UserID < 1:
		obj['UserID'] = UserNumber
	obj.state = 2

## Load the User-Name and replace 
def LoadName(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	obj = cont.owner
	
	#--------------------------------------------------------------------
	#Get the properties for Settings
	UserID = obj['UserID']			#The current User-ID
	Name = obj['Name']				#The Text-Object
	
	#--------------------------------------------------------------------	
	#Connect to the Database
	con = SQL.connect(mainDir + "Database/User.db")
	
	cur = con.cursor()
	#Load the Database-row with the choosen "Auto-ID"
	cur.execute("SELECT * FROM User WHERE UserID=:Id", {"Id": UserID})
	con.commit()

	data = cur.fetchone()

	#--------------------------------------------------------------------
	#Define and save the loaded Database-row-Data
	UserName = data[1]
	First_Run = data[17]
	
	#Close the Database-Connection
	if con:
		con.close()
	
	#Display the User-Name
	objList[Name]['Text'] = UserName
	
	#Store if User runs the game his first time
	obj['FirstRun'] = First_Run
	
	if Debug:
		print("User-Name " + UserName + " loaded")
	
	obj.state = 1

## Load the Language-ID from selected UserID
def LoadLang(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	obj = cont.owner
	
	#--------------------------------------------------------------------
	#Get the properties for Settings
	ReadLastUserFile(cont)
	UserID = obj['UserID']							#The User - ID

	if Debug:
		print("LoadLang " + str(UserID))
	
	#--------------------------------------------------------------------	
	#Connect to the Database
	con = SQL.connect(mainDir + "Database/User.db")
	
	cur = con.cursor()
	#Load the Database-row with the choosen "Auto-ID"
	cur.execute("SELECT * FROM User WHERE UserID=:Id", {"Id": UserID})
	con.commit()

	data = cur.fetchone()

	#--------------------------------------------------------------------
	#Define and save the loaded Database-row-Data
	UserName = data[1]
	Language = data[19]
	
	#Close the Database-Connection
	if con:
		con.close()
	
	obj['LangID'] = Language
	
	if Debug:
		print("Language: " + str(Language) + " loaded")
	
	obj.state = 2

## replace the Buttons in Settings-menue for every state	
def icons(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	obj = cont.owner
	
	#--------------------------------------------------------------------
	#Get the Properties
	OP_SSAO = obj['OP_SSAO']
	OP_HUD = obj['OP_HUD']
	OP_SMOKE = obj['OP_SMOKE']
	OP_MotionBlur = obj['OP_MotionBlur']
	OP_LOD = obj['OP_LOD']
	OP_Minimap = obj['OP_Minimap']
	OP_RealMode = obj['OP_RealMode']
	OP_Cheats = obj['OP_Cheats']
	OP_Sounds = obj['OP_Sounds']
	OP_Music = obj['OP_Music']
	OP_Notifications = obj['OP_Notifications']
	OP_Language = obj['OP_Language']
	OP_Light = obj['OP_Light']
	OP_DOF = obj['OP_DOF']
	OP_AF = obj['OP_AF']
	OP_Water = obj['OP_Water']
	OP_Updates = obj['OP_Updates']
	OP_a_3D = obj['OP_a_3D']
	OP_Shadows = obj['OP_Shadows']
	
	#--------------------------------------------------------------------
	#Define the objects, which should be replaced
	OBJ_SSAO = objList["Plane_OP_SSAO"]
	OBJ_HUD = objList["Plane_OP_HUD"]
	OBJ_SMOKE = objList["Plane_OP_SMOKE"]
	OBJ_MotionBlur = objList["Plane_OP_MotionBlur"]
	OBJ_LOD = objList["Plane_OP_LOD"]
	OBJ_Minimap = objList["Plane_OP_Minimap"]
	OBJ_RealMode = objList["Plane_OP_RealMode"]
	OBJ_Cheats = objList["Plane_OP_Cheats"]
	OBJ_Sounds = objList["Plane_OP_Sounds"]
	OBJ_Music = objList["Plane_OP_Music"]
	OBJ_Notifications = objList["Plane_OP_Notifications"]
	OBJ_Light = objList["Plane_OP_Lights"]
	OBJ_DOF = objList["Plane_OP_DOF"]
	#OBJ_AF = objList["Plane_OP_AF"]
	#OBJ_Water = objList["Plane_OP_Water"]
	OBJ_Updates = objList["Plane_OP_Updates"]
	OBJ_a_3D = objList["Plane_OP_a_3D"]
	OBJ_Shadows = objList["Plane_OP_Shadows"]
	

	#--------------------------------------------------------------------
	#Replace the Objects
	OBJ_Active = "Cube_Active"
	OBJ_InActive = "Cube_InActive"
	
	if OP_SSAO:
		OBJ_SSAO.replaceMesh(OBJ_Active)
	else:
		OBJ_SSAO.replaceMesh(OBJ_InActive)
	
	if OP_HUD:
		OBJ_HUD.replaceMesh(OBJ_Active)
	else:
		OBJ_HUD.replaceMesh(OBJ_InActive)
		
	if OP_SMOKE:
		OBJ_SMOKE.replaceMesh(OBJ_Active)
	else:
		OBJ_SMOKE.replaceMesh(OBJ_InActive)
		
	if OP_MotionBlur:
		OBJ_MotionBlur.replaceMesh(OBJ_Active)
	else:
		OBJ_MotionBlur.replaceMesh(OBJ_InActive)

	if OP_LOD:
		OBJ_LOD.replaceMesh(OBJ_Active)
	else:
		OBJ_LOD.replaceMesh(OBJ_InActive)
		
	if OP_Minimap:
		OBJ_Minimap.replaceMesh(OBJ_Active)
	else:
		OBJ_Minimap.replaceMesh(OBJ_InActive)

	if OP_RealMode:
		OBJ_RealMode.replaceMesh(OBJ_Active)
	else:
		OBJ_RealMode.replaceMesh(OBJ_InActive)
		
	if OP_Cheats:
		OBJ_Cheats.replaceMesh(OBJ_Active)
	else:
		OBJ_Cheats.replaceMesh(OBJ_InActive)
		
	if OP_Sounds:
		OBJ_Sounds.replaceMesh(OBJ_Active)
	else:
		OBJ_Sounds.replaceMesh(OBJ_InActive)
		
	if OP_Music:
		OBJ_Music.replaceMesh(OBJ_Active)
	else:
		OBJ_Music.replaceMesh(OBJ_InActive)
		
	if OP_Notifications:
		OBJ_Notifications.replaceMesh(OBJ_Active)
	else:
		OBJ_Notifications.replaceMesh(OBJ_InActive)
		
	if OP_Light:
		OBJ_Light.replaceMesh(OBJ_Active)
	else:
		OBJ_Light.replaceMesh(OBJ_InActive)
		
	if OP_DOF:
		OBJ_DOF.replaceMesh(OBJ_Active)
	else:
		OBJ_DOF.replaceMesh(OBJ_InActive)
		
	#if OP_AF:
	#	OBJ_AF.replaceMesh(OBJ_Active)
	#else:
	#	OBJ_AF.replaceMesh(OBJ_InActive)

	#if OP_Water:
	#	OBJ_Water.replaceMesh(OBJ_Active)
	#else:
	#	OBJ_Water.replaceMesh(OBJ_InActive)

	if OP_Updates:
		OBJ_Updates.replaceMesh(OBJ_Active)
	else:
		OBJ_Updates.replaceMesh(OBJ_InActive)
		
	if OP_a_3D:
		OBJ_a_3D.replaceMesh(OBJ_Active)
	else:
		OBJ_a_3D.replaceMesh(OBJ_InActive)
	
	if OP_Shadows:
		OBJ_Shadows.replaceMesh(OBJ_Active)
	else:
		OBJ_Shadows.replaceMesh(OBJ_InActive)

## Add a new User with Name from Text-Property and current Usernumber +1		
def addUser(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	obj = cont.owner
	
	#Load the current maximum-UserNumber
	LoadUserNumber(cont)
	
	#--------------------------------------------------------------------
	#Get the User-Count
	UserNumber = obj['UserNumber']
	UserNameNew = objList['Start_NewUser_Text']['Text']
	
	UserIDNew = UserNumber + 1
	#--------------------------------------------------------------------	
	#Connect to the Database
	con = SQL.connect(mainDir + "Database/User.db")
	
	cur = con.cursor()
	#Load the Database-row with the choosen "Auto-ID"
	cur.execute("INSERT INTO User (UserID, Username) VALUES (?, ?)", (UserIDNew, UserNameNew))
	con.commit()

	data = cur.fetchone()
	
	#Close the Database-Connection
	if con:
		con.close()
	
	#Cheat a UserID for saving
	obj["UserID"] = UserIDNew
	
	#Save the User-ID	
	SaveID(cont)	

## Change the name of User
def changeUserName(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	obj = cont.owner
	
	#--------------------------------------------------------------------
	#Get the new User-Name
	UserID = obj['UserID']
	UserNameNew = objList['Start_changeUser_Text']['Text']
	
	#--------------------------------------------------------------------	
	#Connect to the Database
	con = SQL.connect(mainDir + "Database/User.db")
	
	cur = con.cursor()
	#Load the Database-row with the choosen "User-ID"
	cur.execute("UPDATE User SET Username =:Name WHERE UserID =:ID", {"Name":UserNameNew, "ID":UserID})
	con.commit()

	data = cur.fetchone()
	
	#Close the Database-Connection
	if con:
		con.close()

## prevent double event caused by sensors		
def onePositive(cont):
  for trigger in cont.sensors:
    if trigger.positive:
      return True

def playError(cont):
		#Play "Error"		
		act = cont.actuators["Error"]
		act.startSound()
	  
## Buy a car depends on Users-Money amount
def Buy(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	obj = cont.owner
	
	if onePositive(cont):
		return
	
	bought = obj['bought']
	Available = obj['Available']
	
	if Available == 0:
		playError(cont)
		return
		
	if bought:
		if Debug:
			print("Car bought already!")
			
		playError(cont)
		
		return
		
	else:
		#--------------------------------------------------------------------
		UserID = obj['UserID']
		AutoID = obj['AutoID']
		
		#--------------------------------------------------------------------
		##Get the current money liquidity:
		#Connect to the Database User/User
		con = SQL.connect(mainDir + "Database/User.db")
		
		cur = con.cursor()
		#Load the Database-row with the choosen "User-ID"
		cur.execute("SELECT * FROM User WHERE UserID=:Uid", {"Uid":UserID})
		con.commit()

		data = cur.fetchone()
		
		#Save the Money to variable
		Money = data[2]
		
		#Close the Database-Connection
		if con:
			con.close()
		
		#--------------------------------------------------------------------
		#Do some math before buying.
		
		Price_Orig = obj['Price_Orig']		#Price of car, which was loaded and saved before
		
		if Money >= Price_Orig:
			Money_New = Money - Price_Orig
			if Debug:
				print("Car price: "+ str(Price_Orig))
		else:
			if Debug:
				print("Car is to expensive! Can't buy it! Need more Money...")
			
			playError(cont)
			return
		
		#--------------------------------------------------------------------
		##The real buying
		#Connect to the Database User/BoughtCars
		con = SQL.connect(mainDir + "Database/User.db")
		
		cur = con.cursor()
		#Load the Database-row with the choosen "User-ID" and "AutoID" and set bought to True
		cur.execute("INSERT INTO BoughtCars (UserID, AutoID, Bought) VALUES(?, ?, 1)", (UserID, AutoID))
		con.commit()
		
		if Debug:
			print("Car "+str(AutoID)+" bought!")
		
		#Close the Database-Connection
		if con:
			con.close()
		
		#--------------------------------------------------------------------
		##Save the new User-Money amount:	
		#Connect to the Database User/BoughtCars
		con = SQL.connect(mainDir + "Database/User.db")
		
		cur = con.cursor()
		#Load the Database-row with the choosen "User-ID" and "AutoID" and set bought to True
		cur.execute("UPDATE User SET Money = ? WHERE UserID =?", (Money_New, UserID))
		con.commit()
		
		if Debug:
			print("New saved ammount: "+ str(Money_New))
		
		#Close the Database-Connection
		if con:
			con.close()
		
		#Play "cha-ching"		
		act = cont.actuators["cha-ching"]
		act.startSound()
		
		obj.state = 4		#Switch to state 4 for reloading the Car

## Sell a car depends on car-price	
def Sell(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	obj = cont.owner
	
	ratio = 0.6
	
	if onePositive(cont):
		return
	
	bought = obj['bought']
	
	if bought:
	
		#--------------------------------------------------------------------
		UserID = obj['UserID']
		AutoID = obj['AutoID']
		
		#--------------------------------------------------------------------
		##Get the current money liquidity:
		#Connect to the Database User/User
		con = SQL.connect(mainDir + "Database/User.db")
		
		cur = con.cursor()
		#Load the Database-row with the choosen "User-ID"
		cur.execute("SELECT * FROM User WHERE UserID=:Uid", {"Uid":UserID})
		con.commit()

		data = cur.fetchone()
		
		#Save the Money to variable
		Money = data[2]
		
		#Close the Database-Connection
		if con:
			con.close()
		
		#--------------------------------------------------------------------
		#Do some math before selling.
		
		Price_Orig = obj['Price_Orig']		#Price of car, which was bought, loaded and saved before
		
		Price_Sell = Price_Orig * ratio
		
		Money_New = Money + round(Price_Sell)
		
		if Debug:
			print("New added ammount: "+ str(Price_Sell))
		
		#--------------------------------------------------------------------
		##The real selling
		#Connect to the Database User/BoughtCars
		con = SQL.connect(mainDir + "Database/User.db")
		
		cur = con.cursor()
		#Load the Database-row with the choosen "User-ID" and "AutoID" and set bought to True
		cur.execute("INSERT INTO BoughtCars (UserID, AutoID, Bought) VALUES(?, ?, 0)", (UserID, AutoID))
		con.commit()
		
		if Debug:
			print("Car "+str(AutoID)+" selled!")
		
		#Close the Database-Connection
		if con:
			con.close()
		
		#--------------------------------------------------------------------
		##Save the new User-Money amount:	
		#Connect to the Database User/BoughtCars
		con = SQL.connect(mainDir + "Database/User.db")
		
		cur = con.cursor()
		#Load the Database-row with the choosen "User-ID" and "AutoID" and set bought to True
		cur.execute("UPDATE User SET Money = ? WHERE UserID =?", (Money_New, UserID))
		con.commit()
		
		if Debug:
			print("New saved ammount: "+ str(Money_New))
		
		#Close the Database-Connection
		if con:
			con.close()
		
		#Play "cha-ching"		
		act = cont.actuators["cha-ching"]
		act.startSound()
		
		obj.state = 4		#Switch to state 4 for reloading the Car	
		
	else:
	
		if Debug:
			print("Car isn't bought! Can't Sell it!")
		
		#Play "Error"	
		act = cont.actuators["Error"]
		act.startSound()
		
		return
	
def MoneyCheat(cont):
	
	obj = cont.owner
	
	#--------------------------------------------------------------------
	UserID = obj['UserID']
	Money_New = 1000000000	#The new Money-Ammount
	
	#--------------------------------------------------------------------
	##Save the new User-Money amount:	
	#Connect to the Database User/BoughtCars
	con = SQL.connect(mainDir + "Database/User.db")
	
	cur = con.cursor()
	#Load the Database-row with the choosen "User-ID" and "AutoID" and set bought to True
	cur.execute("UPDATE User SET Money = ? WHERE UserID =?", (Money_New, UserID))
	con.commit()
	
	if Debug:
		print("New saved ammount: "+ str(Money_New))
	
	#Close the Database-Connection
	if con:
		con.close()
	
	obj["Cheat"] = ""	

def AF_Check(cont):
	obj = cont.owner
	
	OP_AF = obj['OP_AF']
	
	if OP_AF > 5:
		obj['OP_AF'] = 1
	elif OP_AF <= 0:
		obj['OP_AF'] = 5

def AF_Nr_Set(cont):
	obj = cont.owner
	
	level = obj['level']
	
	if level == 1:
		obj['Text'] = "Off"
		
	elif level == 2:
		obj['Text'] = 2
		
	elif level == 3:
		obj['Text'] = 4
		
	elif level == 4:
		obj['Text'] = 8
		
	elif level == 5:
		obj['Text'] = 16
	
	else:
		obj['Text'] = "Error"
		
def setAnisotropic(cont):
	obj = cont.owner
	
	OP_AF = obj['OP_AF']
	
	if OP_AF == 1:
		level = 1
		
	elif OP_AF == 2:
		level = 2
		
	elif OP_AF == 3:
		level = 4
		
	elif OP_AF == 4:
		level = 8
		
	elif OP_AF == 5:
		level = 16
	
	else:
		level = 1
	
	#Set the anisotropic filtering level for textures.
    #Parameters: level (integer (must be one of 1, 2, 4, 8, 16)) – The new anisotropic filtering level to use
	Rasterizer.setAnisotropicFiltering(level)

def Water_Check(cont):
	obj = cont.owner
	
	OP_Wat = obj['OP_Water']
	
	if OP_Wat > 1:
		obj['OP_Water'] = 0
	elif OP_Wat < 0:
		obj['OP_Water'] = 1

def Water_Nr_Set(cont):
	obj = cont.owner
	
	level = obj['level']
	
	if level == 0:
		obj['Text'] = "Off"
		
	elif level == 1:
		obj['Text'] = "LQ"
		
	elif level == 2:
		obj['Text'] = "HQ"
		
	else:
		obj['Text'] = "Error"
	
def setShadows(cont):
	obj = cont.owner
	OP_Shadows = obj['OP_Shadows']
	
	Rasterizer.setGLSLMaterialSetting("shadows", OP_Shadows)
	
def setMinimapTyp(cont):
	obj = cont.owner
	OP_MinimapTyp = obj['OP_MinimapTyp']
	
	if OP_MinimapTyp:
		if Debug:
			print("Minimap = 2D")
		cam.perspective = False
	else:
		if Debug:
			print("Minimap = 3D")
		cam.perspective = True