#############################################################################
# Configuration rules and options for SimCity
#
# This script both defines what the various game options
# do, and sets defaults for them based on the video
# card, sound card, CPU, and other factors.
#
# It also sets some properties determining what the
# hardware will support.
#


#############################################################################
# Constants
#

set Off 0
set On  1

set Min    0
set Low    1
set Medium 2
set High   3
set Ultra  4

set ModernFail -1
set ProbablyFail -2
set DefinitelyFail -3

# Shader paths -- must match with Material/config.mtl
seti shaderPathMin         0 # Not well supported
seti shaderPathLow         1
seti shaderPathRecommended 2
seti shaderPathMax         3

#############################################################################
# Set up
#

setVariables

setb isMac   false
setb isWin7  false
setb isXP    false
setb isVista false

if (match("${platform}", "OSX*"))
   # Running on the Mac wrapper. Set mac-specific options here
   output "Running on OSX"
   setb isMac true
   boolProp RenderTargetCorrection false

   if( match("${graphicsCard}", "*HD Graphics 3000*") )
        if( match("${OSVersion}", "10.7.*") )
            alert "Your Intel HD 3000 video card driver version provided by Apple for OS X Lion will cause graphical errors and corruption with SimCity.  Please upgrade to OS X Mountain Lion or a newer version of OS X to prevent these errors." 2005 -warning
        elseif( match("${OSVersion}", "10.8.0") )
            alert "Please update your Mac OS X to version 10.8.3 or newer.  Graphical errors and corruption will occur without this update." 2006 -error
        endif
   endif
else

    setb isWin7 (match("${OSVersion}", "6.1.*"))

    if ($isWin7 and $os64Bit and (not $has64BitExceptionFix)) 
       alert "To get proper crash handling it is recommended you install this hotfix:\n    http://support.microsoft.com/default.aspx/kb/976038\n\nIt is available from:\n    \\maxislocalbuild\Builds\Install\n\n" 2004 -warning
    endif

    setb isVista false
    if (match("${OSName}", "*Windows Vista*"))
        setb isVista true
    endif

    setb isXP false
    if (match("${OSName}", "*Windows XP*"))
        setb isXP true
    endif

    if ($isVista and $vistaKB940105Required) 
       alert "The game may run out of address space with some configurations\nand video cards, resulting in stability problems.\n\nIt is recommended that you install the latest Windows Vista Service Pack\nand check for updated video drivers.\n\nFor details see the Microsoft Knowledge Base article\n\n    http://support.microsoft.com/default.aspx/kb/940105\n\n" 2003 -warning
    endif

endif



#############################################################################
# Initial defaults.
#
# These may be overridden below according to CPU or card.

setOption OptionShadows             $High
setOption OptionTextureDetail       $Medium
setOption OptionEffects             $High
setOption OptionLighting            $High
setOption OptionGameQuality         $High
setOption OptionPeopleQuality       $High
setOption OptionSignQuality         $High
setOption OptionCityImpostorQuality $High
setOption OptionAudioPerformance    $Medium
setOption OptionGeometryDetail      $High
setOption OptionAnimationDetail     $High
setOption OptionFXAA				$Off
setOption OptionPictureFilter       $Off
setOption OptionNoWindowBorders     $Off

setOption OptionPhotoRes            $Medium
setOption OptionVideoRes            $High

#############################################################################
# Identify graphics adapter
#


# set some config variables based on a card table and vendor specific rules
# sets isCardFound, cardVendor, and cardName
setb isCardFound false
set cardVendor Unknown
set cardName Unknown
include "VideoCards.txt"

# Fallback on the card name text supplied by the card itself.
# Assumes that at least the cardVendor has been matched, since
# vendor name is not contained in some vendor card names.
# Note that specific vendors are overridden to a default card.
#  For AMD    -> Radeon 9800 Pro
#  For NVIDIA -> NVIDIA GeForceFX 5200 SE

if (not $isCardFound)
   set cardName "${graphicsCard}"
endif


#############################################################################
#
# Testing Code
#

# For testing
setb doTestCPU 0
setb doTestGPU 0

if ($doTestCPU)
    seti pentium4 0
    seti cpuCount 1

    seti cpuSpeed 2000
    set cpuBrand "AMD Athlon64"
endif

if ($doTestGPU)
    # test some 2.0/3.0 cards
    #set cardVendor "AMD"
    #set cardName "FirePro W5000"

    set cardVendor "Intel"
    set cardName "Intel HD Graphics 4000"

    #set cardVendor "NVIDIA"
    #set cardName "Quadro FX 5800"

    #seti testShaderModel 3
    #seti maxVertexProgramVersionHW ($testShaderModel*256)
    #seti maxPixelProgramVersion    ($testShaderModel*256)

    # have to set this, since some overrides fallback on graphicsCard string
    set graphicsCard "$cardName"
endif

#############################################################################
#
# Option Levels
#

seti cpuLevelHigh          3
seti cpuLevelMedium        2
seti cpuLevelLow           1

seti gpuLevelHigh          3
seti gpuLevelMedium        2
seti gpuLevelLow           1
seti gpuLevelMin           0

#############################################################################
#
# CPU Assessment
#

seti adjustedCPU $cpuSpeed

setb cpuAMD false
if (match("${cpuBrand}", "*AMD*"))
    setb cpuAMD true
endif

seti cpuCutoffLow 2000
seti cpuCutoffMed 2400

if ($pentium4 > 0)
    # intel p4 hyperthread or multiprocessor
    if ($hyperthreading > 0)
        # only count real cpus
        seti cpuCount (floor(${cpuCount} / 2))
    endif

    if ($cpuCount > 1)
        seti adjustedCPU (round($adjustedCPU * 1.3334))
    endif

    seti cpuCutoffLow 2400
    seti cpuCutoffMed 2800

elseif ($cpuCount == 1)
    if ($cpuAMD)
        # amd single core (xp, 64)
        seti cpuCutoffLow 1800
        seti cpuCutoffMed 2000
    else
        # pentium m, celeron, etc
        seti cpuCutoffLow 2000
        seti cpuCutoffMed 2200
    endif
else
    if ($cpuAMD)
        # amd multicore (athlon 64x2)
        seti cpuCutoffLow 1600
        seti cpuCutoffMed 1900
    else
        # intel multicore (core duo, core2 duo)
        seti cpuCutoffLow 1800
        seti cpuCutoffMed 2100
    endif
endif

# rate the cpu
if ($adjustedCPU < $cpuCutoffLow)
    seti cpuLevel $cpuLevelLow
elseif ($adjustedCPU < $cpuCutoffMed)
    seti cpuLevel $cpuLevelMedium
else
    seti cpuLevel $cpuLevelHigh
endif


#############################################################################
#
# GPU Assessment
#

# track specific vendors
setb isNvidia           false
#setb isNvidiaIntegrated false

setb isAMD              false
#setb isAMDIntegrated    false

setb isIntel            false
setb isIntelIntegrated  false

setb isS3               false
setb isVendorUnknown    false

setb isLimitedVideoMemory false

# convert to major version
if (not $isMac)
    seti vsVersion (floor($maxVertexProgramVersionHW / 256))
    seti psVersion (floor($maxPixelProgramVersion / 256))
else
    seti vsVersion (floor($maxVertexProgramVersionHW / 1))
    seti psVersion (floor($maxPixelProgramVersion / 1))
endif

# if not in database, assume any new card is "high"
seti gpuLevel $gpuLevelHigh

if (match("${cardVendor}", "AMD") or match("${cardVendor}", "ATI"))
    setb isAMD true
elseif (match("${cardVendor}", "NVIDIA"))
    setb isNvidia true
elseif (match("${cardVendor}", "INTEL"))
    setb isIntel true
elseif (match("${cardVendor}", "S3"))
    setb isS3 true
else
    setb isVendorUnknown true
endif

# old driver build detection/warning
setb oldDriver false                # Triggers a warning the first time the game is run
setb isOldDriverBuildExit false     # Set this if we know the game won't function with the current drivers

setb isMultiGpu false
if ($gpuCount > 1)
    setb isMultiGpu true
endif

seti oldDriverBuild 0   # Spurious setting necessary because variables are still expanded in skipped clauses =(

if (not match("${platform}", "OSX*"))
if (not $doTestGPU)
    # test each vendor against specific known driver builds
    if ($isNvidia)

        seti oldDriverBuild 13069

        if ($isMultiGpu)
            seti oldDriverBuild 130697
            #setb isOldDriverBuildExit true
        endif

        if ($driverBuild < $oldDriverBuild)
            setb oldDriver true
        endif
        
    elseif ($isIntel)
        seti oldDriverBuild 102875  # this numbering scheme is not continuous with the old drivers for series 4 cards

        if ($driverBuild < $oldDriverBuild)
            setb oldDriver true
        endif

    elseif ($isAMD)
        if (not $isXP)
            seti oldDriverBuild   101140  # Cat 13.1    TODO: update

            if ($isMultiGpu)
                seti oldDriverBuild 101140 # Cat 13.1    TODO: update
            endif
        else
            seti oldDriverBuild 101140  # Cat 13.1     TODO: update

            if ($isMultiGpu)
                seti oldDriverBuild 101140  # Cat 13.1   TODO: update
                #setb isOldDriverBuildExit true
            endif
        endif

        if ($driverBuild < $oldDriverBuild)
            setb oldDriver true
        endif
    endif

    # this alert is only displayed the first time the game is launched, or when the preferences are deleted
    if ($oldDriver)
       if ($isOldDriverBuildExit)
           if ($isMultiGpu)
               alert "An older video driver is detected.\n\nTo ensure correct visuals and gameplay with multiple gpus,\ninstalling the latest video driver is required,\nor multiple gpu mode must be disabled.\nThe game will not run.\nPlease see the README for details.\n\n" 1012
           else
               alert "An older video driver is detected.\n\nTo ensure correct visuals and gameplay,\ninstalling the latest video driver is required.\nThe game will not run.  Please see the README for details.\n\n" 1011
           endif
       else
          alert "An older video driver is detected.\n\nTo ensure correct visuals and gameplay,\ninstalling the latest video driver is recommended.\n\n" 1010 -info
       endif
    endif
endif
# OSX check
endif


if ($isNvidia)
    # rules don't apply to Quadro numbering, if not renamed in db assume medium
    if (not match("${cardName}", "*Quadro*"))
        # default to medium
        seti gpuLevel $gpuLevelMedium
        
        # classify failure
        if (match("${cardName}", "*5??0*"))
            seti gpuLevel $ProbablyFail
        elseif (match("${cardName}", "*6??0*"))
            seti gpuLevel $ProbablyFail
            
        #7xxx series
        elseif (match("${cardName}", "*7??0*"))
            if (match("${cardName}", "*79?0*"))
                seti gpuLevel $gpuLevelMin
            elseif (match("${cardName}", "*78?0*"))
                seti gpuLevel $gpuLevelMin
            else
                seti gpuLevel $ProbablyFail
            endif
        
        #8xxx series
        elseif (match("${cardName}", "*8??0*"))
            if (match("${cardName}", "*81?0*"))
                seti gpuLevel $ProbablyFail
            elseif (match("${cardName}", "*82?0*"))
                seti gpuLevel $ProbablyFail
            elseif (match("${cardName}", "*83?0*"))
                seti gpuLevel $ProbablyFail
            elseif (match("${cardName}", "*84?0*"))
                seti gpuLevel $ModernFail
            elseif (match("${cardName}", "*85?0*"))
                seti gpuLevel $ModernFail
            elseif (match("${cardName}", "*86?0*"))
                seti gpuLevel $gpuLevelMin
            elseif (match("${cardName}", "*88?0*"))
                seti gpuLevel $gpuLevelLow
            endif
            
        #9xxx series
        elseif (match("${cardName}", "*9??0*"))
            if (match("${cardName}", "*91?0*"))
                seti gpuLevel $ProbablyFail
            elseif (match("${cardName}", "*92?0*"))
                seti gpuLevel $ProbablyFail
            elseif (match("${cardName}", "*93?0*"))
                seti gpuLevel $ProbablyFail
            elseif (match("${cardName}", "*94?0*"))
                seti gpuLevel $ModernFail
            elseif (match("${cardName}", "*95?0*"))
                seti gpuLevel $ModernFail
            elseif (match("${cardName}", "*96?0*"))
                seti gpuLevel $gpuLevelMin
            elseif (match("${cardName}", "*98?0*"))
                seti gpuLevel $gpuLevelLow
            endif
        
        #1xx series - strange outlier, rebranded 9xxx cards
        elseif (match("${cardName}", "*1?0*"))
            if (match("${cardName}", "*100*"))
                seti gpuLevel $ProbablyFail
            elseif (match("${cardName}", "*120*"))
                seti gpuLevel $ProbablyFail
            elseif (match("${cardName}", "*130*"))
                seti gpuLevel $ProbablyFail
            elseif (match("${cardName}", "*140*"))
                seti gpuLevel $gpuLevelLow
            elseif (match("${cardName}", "*150*"))
                seti gpuLevel $gpuLevelLow
            endif

        #2xx series
        elseif (match("${cardName}", "*2?0*") or match("${cardName}", "*2?5*"))
            if (match("${cardName}", "*205*"))
                seti gpuLevel $ProbablyFail
            elseif (match("${cardName}", "*210*"))
                seti gpuLevel $ModernFail
            elseif (match("${cardName}", "*220*"))
                seti gpuLevel $ModernFail
            elseif (match("${cardName}", "*230*"))
                seti gpuLevel $ModernFail
            elseif (match("${cardName}", "*240*"))
                if (match("${cardName}", "*GTS*"))
                    seti gpuLevel $gpuLevelLow
                else
                    seti gpuLevel $gpuLevelMin
                endif
            elseif (match("${cardName}", "*250*"))
                seti gpuLevel $gpuLevelLow
            elseif (match("${cardName}", "*260*"))
                seti gpuLevel $gpuLevelMedium
            elseif (match("${cardName}", "*27?*"))
                seti gpuLevel $gpuLevelMedium
            elseif (match("${cardName}", "*28?*"))
                seti gpuLevel $gpuLevelMedium
            elseif (match("${cardName}", "*29?*"))
                seti gpuLevel $gpuLevelHigh
            endif
        
        #3xx series - rebrand budget parts
        elseif (match("${cardName}", "*3?0*") or match("${cardName}", "*3?5*"))
            if (match("${cardName}", "*31?*"))
                seti gpuLevel $ModernFail
            elseif (match("${cardName}", "*32?*"))
                seti gpuLevel $gpuLevelMin
            elseif (match("${cardName}", "*33?*"))
                seti gpuLevel $gpuLevelMin
            elseif (match("${cardName}", "*34?*"))
                seti gpuLevel $gpuLevelMin
            endif
            
            
        #4xx series!    
        elseif (match("${cardName}", "*4?0*") or match("${cardName}", "*4?5*"))
            if (match("${cardName}", "*405*"))
                seti gpuLevel $ModernFail
            elseif (match("${cardName}", "*420*"))
                seti gpuLevel $ModernFail
            elseif (match("${cardName}", "*430*"))
                seti gpuLevel $gpuLevelMin
            elseif (match("${cardName}", "*440*"))
                seti gpuLevel $gpuLevelLow
            elseif (match("${cardName}", "*450*"))
                seti gpuLevel $gpuLevelMedium
            elseif (match("${cardName}", "*46?*"))
                seti gpuLevel $gpuLevelMedium
            elseif (match("${cardName}", "*470*"))
                seti gpuLevel $gpuLevelMedium
            elseif (match("${cardName}", "*480*"))
                seti gpuLevel $gpuLevelHigh
            endif
            
        #5xx series!    
        elseif (match("${cardName}", "*5?0*") or match("${cardName}", "*5?5*"))
            if (match("${cardName}", "*510*"))
                seti gpuLevel $ModernFail
            elseif (match("${cardName}", "*520*"))
                seti gpuLevel $ModernFail
            elseif (match("${cardName}", "*525*"))
                seti gpuLevel $gpuLevelLow
            elseif (match("${cardName}", "*530*"))
                seti gpuLevel $gpuLevelMin
            elseif (match("${cardName}", "*540*"))  #this is actually only for the 540M
                seti gpuLevel $gpuLevelLow
            elseif (match("${cardName}", "*545*"))
                seti gpuLevel $gpuLevelLow
            elseif (match("${cardName}", "*55?*"))
                seti gpuLevel $gpuLevelMedium
            elseif (match("${cardName}", "*560*"))
                seti gpuLevel $gpuLevelMedium
            elseif (match("${cardName}", "*570*"))
                seti gpuLevel $gpuLevelMedium
            elseif (match("${cardName}", "*580*"))
                seti gpuLevel $gpuLevelHigh
            elseif (match("${cardName}", "*590*"))
                seti gpuLevel $gpuLevelHigh
            endif
            
        elseif (match("${cardName}", "*6?0*") or match("${cardName}", "*6?5*"))
            if (match("${cardName}", "*605*"))
                seti gpuLevel $ModernFail
            elseif (match("${cardName}", "*610*"))
                seti gpuLevel $ModernFail
            elseif (match("${cardName}", "*620*"))
                seti gpuLevel $ModernFail
            elseif (match("${cardName}", "*630M*"))
                seti gpuLevel $gpuLevelLow # will bump down to min
            elseif (match("${cardName}", "*630*"))
                seti gpuLevel $gpuLevelMin
            elseif (match("${cardName}", "*64?*"))
                seti gpuLevel $gpuLevelLow
            elseif (match("${cardName}", "*650*"))
                seti gpuLevel $gpuLevelMedium
            elseif (match("${cardName}", "*660*"))
                seti gpuLevel $gpuLevelHigh
            elseif (match("${cardName}", "*670*"))
                seti gpuLevel $gpuLevelHigh
            elseif (match("${cardName}", "*675*"))
                seti gpuLevel $gpuLevelHigh
            elseif (match("${cardName}", "*680*"))
                seti gpuLevel $gpuLevelHigh
            elseif (match("${cardName}", "*690*"))
                seti gpuLevel $gpuLevelHigh
            endif
        elseif (match("${cardName}", "* 7?0*"))
            seti gpuLevel $gpuLevelHigh
        endif
        
        
        #if it's a mobile card, kick it down a notch
        if ((match("${cardName}", "*Go*") or match("${cardName}", "*M*")) and ($gpuLevel >= $gpuLevelMin) )
            seti gpuLevel ($gpuLevel-1)
        endif
    else
        seti gpuLevel $ProbablyFail
        
        if ( match("${cardName}", "*FX*") )
            if (match("${cardName}", "*3500*"))
                seti gpuLevel $gpuLevelLow
            elseif (match("${cardName}", "*3700*"))
                seti gpuLevel $gpuLevelLow
            elseif (match("${cardName}", "*3800*"))
                seti gpuLevel $gpuLevelLow
            elseif (match("${cardName}", "*4600*"))
                seti gpuLevel $gpuLevelLow
            elseif (match("${cardName}", "*4800*"))
                seti gpuLevel $gpuLevelLow
            elseif (match("${cardName}", "*5500*"))
                seti gpuLevel $gpuLevelMin
            elseif (match("${cardName}", "*5600*"))
                seti gpuLevel $gpuLevelMedium
            elseif (match("${cardName}", "*5800*"))
                seti gpuLevel $gpuLevelHigh
            else
                seti gpuLevel $ProbablyFail
            endif
        else
            if (match("${cardName}", "*2000*"))
                seti gpuLevel $gpuLevelLow
            elseif (match("${cardName}", "*4000*"))
                seti gpuLevel $gpuLevelMedium
            elseif (match("${cardName}", "*5000K*"))
                seti gpuLevel $gpuLevelHigh
            elseif (match("${cardName}", "*5000*"))
                seti gpuLevel $gpuLevelMedium
            elseif (match("${cardName}", "*6000*"))
                seti gpuLevel $gpuLevelHigh
            endif
        endif
        
        #if it's a mobile card, kick it down a notch
        if ((match("${cardName}", "*Go*") or match("${cardName}", "*M*")) and ($gpuLevel >= $gpuLevelMin) )
            seti gpuLevel ($gpuLevel-1)
        endif
    endif
endif

if ($isAMD)
    # rules don't apply to FireGL/FireMV numbering, if not renamed in db assume medium
    if (not match("${cardName}", "*Fire*")) # FireGL/MV

        if (match("${cardName}", "*HD*"))
            if (match("${cardName}", "*2??0*"))
                if (match("${cardName}", "*23?0*"))
                    seti gpuLevel $ProbablyFail
                elseif (match("${cardName}", "*24?0*"))
                    seti gpuLevel $ProbablyFail
                elseif (match("${cardName}", "*26?0*"))
                    seti gpuLevel $gpuLevelMin
                elseif (match("${cardName}", "*29?0*"))
                    seti gpuLevel $gpuLevelLow
                else
                    seti gpuLevel $gpuLevelLow
                endif
            elseif (match("${cardName}", "*3??0*"))
                if (match("${cardName}", "*34?0*"))
                    seti gpuLevel $ProbablyFail
                elseif (match("${cardName}", "*36?0*"))
                    seti gpuLevel $gpuLevelMin
                elseif (match("${cardName}", "*3850*"))
                    seti gpuLevel $gpuLevelLow
                elseif (match("${cardName}", "*38?0*"))
                    seti gpuLevel $gpuLevelMedium
                else
                    seti gpuLevel $ProbablyFail
                endif
            elseif (match("${cardName}", "*4??0*"))
                if (match("${cardName}", "*42?0*"))
                    seti gpuLevel $ProbablyFail
                elseif (match("${cardName}", "*43?0*"))
                    seti gpuLevel $ProbablyFail
                elseif (match("${cardName}", "*45?0*"))
                    seti gpuLevel $ModernFail
                elseif (match("${cardName}", "*46?0*"))
                    seti gpuLevel $gpuLevelMin
                elseif (match("${cardName}", "*4730*"))
                    seti gpuLevel $gpuLevelMin
                elseif (match("${cardName}", "*47?0*"))
                    seti gpuLevel $gpuLevelLow
                elseif (match("${cardName}", "*4870*") or match("${cardName}", "*4890*"))
                    seti gpuLevel $gpuLevelMedium
                elseif (match("${cardName}", "*48?0*"))
                    seti gpuLevel $gpuLevelMedium
                else
                    seti gpuLevel $gpuLevelLow
                endif

            elseif (match("${cardName}", "*5??0*"))
                if (match("${cardName}", "*54?0*"))
                    seti gpuLevel $ModernFail
                elseif (match("${cardName}", "*55?0*"))
                    seti gpuLevel $gpuLevelMin
                elseif (match("${cardName}", "*56?0*"))
                    seti gpuLevel $gpuLevelLow
                elseif (match("${cardName}", "*57?0*"))
                    seti gpuLevel $gpuLevelMedium
                elseif (match("${cardName}", "*58?0*"))
                    seti gpuLevel $gpuLevelMedium
                elseif (match("${cardName}", "*59?0*"))
                    seti gpuLevel $gpuLevelHigh
                else
                    seti gpuLevel $gpuLevelLow
                endif
                
            elseif (match("${cardName}", "*6??0*"))
                if (match("${cardName}", "*62?0*") or match("${cardName}", "*6310*") or match("${cardName}", "*6320*"))
                    #integrated parts
                    seti gpuLevel $ModernFail
                elseif (match("${cardName}", "*6???D*"))
                    #integrated
                    seti gpuLevel $gpuLevelLow
                elseif (match("${cardName}", "*6???G*"))
                    #integrated mobile
                    seti gpuLevel $gpuLevelLow
                elseif (match("${cardName}", "*63?0*"))
                    seti gpuLevel $ModernFail
                elseif (match("${cardName}", "*64?0*"))
                    seti gpuLevel $ProbablyFail
                elseif (match("${cardName}", "*65?0*"))
                    seti gpuLevel $gpuLevelLow
                #GPU 6600 and 6700 strings are both in cardName string.  If you have a 6700 gpu
                #you will have the settings of a 6600.
                elseif (match("${cardName}", "*66?0*"))
                    seti gpuLevel $gpuLevelLow
                elseif (match("${cardName}", "*67?0*"))
                    seti gpuLevel $gpuLevelLow
                elseif (match("${cardName}", "*68?0*"))
                    seti gpuLevel $gpuLevelHigh
                elseif (match("${cardName}", "*69?0*"))
                    seti gpuLevel $gpuLevelHigh
                else
                    seti gpuLevel $gpuLevelMedium
                endif
                
            elseif (match("${cardName}", "*7??0*"))
                if (match("${cardName}", "*7???D*"))
                    #integrated
                    seti gpuLevel $gpuLevelMin
                elseif (match("${cardName}", "*73?0*"))
                    seti gpuLevel $ModernFail
                elseif (match("${cardName}", "*74?0*"))
                    seti gpuLevel $gpuLevelMin
                elseif (match("${cardName}", "*75?0*"))
                    seti gpuLevel $gpuLevelLow
                elseif (match("${cardName}", "*76?0*"))
                    seti gpuLevel $gpuLevelMedium
                elseif (match("${cardName}", "*77?0*"))
                    seti gpuLevel $gpuLevelMedium
                elseif (match("${cardName}", "*78?0*"))
                    seti gpuLevel $gpuLevelHigh
                elseif (match("${cardName}", "*79?0*"))
                    seti gpuLevel $gpuLevelHigh
                else
                    seti gpuLevel $gpuLevelMedium
                endif
                
            elseif (match("${cardName}", "*8??0*"))
                if (match("${cardName}", "*83?0*"))
                    seti gpuLevel $ModernFail
                elseif (match("${cardName}", "*84?0*"))
                    seti gpuLevel $gpuLevelLow
                elseif (match("${cardName}", "*85?0*"))
                    seti gpuLevel $gpuLevelLow
                elseif (match("${cardName}", "*86?0*"))
                    seti gpuLevel $gpuLevelMedium
                elseif (match("${cardName}", "*87?0*"))
                    seti gpuLevel $gpuLevelHigh
                elseif (match("${cardName}", "*88?0*"))
                    seti gpuLevel $gpuLevelHigh
                elseif (match("${cardName}", "*99?0*"))
                    seti gpuLevel $gpuLevelHigh
                else
                    seti gpuLevel $gpuLevelMedium
                endif
            endif
        
            #if it's a mobile card, kick it down a notch
            if ((match("${cardName}", "*Mobility*") or match("${cardName}", "*M*")) and ($gpuLevel >= $gpuLevelMin))
                seti gpuLevel ($gpuLevel-1)
            endif
        else
            #non "HD" is all pre-min
            seti gpuLevel $ProbablyFail
        endif
    else
        seti gpuLevel $ProbablyFail
        
        if (match("${cardName}", "*V4800*"))
            seti gpuLevel $gpuLevelLow
        elseif (match("${cardName}", "*V4900*"))
            seti gpuLevel $gpuLevelLow
        elseif (match("${cardName}", "*V5700*"))
            seti gpuLevel $gpuLevelMin
        elseif (match("${cardName}", "*V5800*"))
            seti gpuLevel $gpuLevelLow
        elseif (match("${cardName}", "*V5900*"))
            seti gpuLevel $gpuLevelLow
        elseif (match("${cardName}", "*V7800*"))
            seti gpuLevel $gpuLevelMedium
        elseif (match("${cardName}", "*V7900*"))
            seti gpuLevel $gpuLevelMedium
        elseif (match("${cardName}", "*V87?0*"))
            seti gpuLevel $gpuLevelLow
        elseif (match("${cardName}", "*V8800*"))
            seti gpuLevel $gpuLevelMedium
        elseif (match("${cardName}", "*V9800*"))
            seti gpuLevel $gpuLevelMedium
        elseif (match("${cardName}", "*W600*"))
            seti gpuLevel $gpuLevelLow
        elseif (match("${cardName}", "*W5000*"))
            seti gpuLevel $gpuLevelLow
        elseif (match("${cardName}", "*S7000*"))
            seti gpuLevel $gpuLevelMedium
        elseif (match("${cardName}", "*W7000*"))
            seti gpuLevel $gpuLevelMedium
        elseif (match("${cardName}", "*W8000*"))
            seti gpuLevel $gpuLevelHigh
        elseif (match("${cardName}", "*S9000*"))
            seti gpuLevel $gpuLevelHigh
        elseif (match("${cardName}", "*W9000*"))
            seti gpuLevel $gpuLevelHigh
        elseif (match("${cardName}", "*S10000*"))
            seti gpuLevel $gpuLevelHigh
        elseif (match("${cardName}", "*RG5000*"))
            seti gpuLevel $gpuLevelHigh
        endif
        
        #if it's a mobile card, kick it down a notch
        if ((match("${cardName}", "*Mobility*") or match("${cardName}", "*M*")) and ($gpuLevel >= $gpuLevelMin))
            seti gpuLevel ($gpuLevel-1)
        endif
    endif
endif

if ($isIntel)
    if (match("${cardName}", "*915*") or match("${cardName}", "*945*"))
        seti gpuLevel $ProbablyFail
        setb isIntelIntegrated true
        boolProp IsIntelIntegratedGPU true
    elseif (match("${cardName}", "*HD Graphics*") or match("${cardName}", "*Iris*"))
        seti gpuLevel $gpuLevelMin
        setb isIntelIntegrated true
        boolProp IsIntelIntegratedGPU true
        if (match("${cardName}", "*5???*"))
            seti gpuLevel $gpuLevelLow
        endif
    endif

    setb isLimitedVideoMemory true
endif

if ($isS3)
    seti gpuLevel $ProbablyFail
    setb isLimitedVideoMemory true
endif

# drop gpu level if less than 256MB of video memory, below minspec
if (not $isMac)
    if ($textureMemory < 250)
        seti gpuLevel $ProbablyFail
    endif
endif

############################################################################
# Handle bad video cards
#

if ($gpuLevel == $ModernFail)
    alert "While your graphics card is newer than our minimum specified graphics card, it might actually be pretty slow.  We've set the game to low settings, but you may still encounter less than optimal performance.~br~Please see the README for details." 2004 -info
    seti gpuLevel $gpuLevelMin
elseif ($gpuLevel == $ProbablyFail)
    alert "Sorry, your graphics card is below our min spec.~br~The game will attempt to run but may fail or perform poorly.~br~Please see the README for details." 2002 -info
    seti gpuLevel $gpuLevelMin
elseif ($gpuLevel == $DefinitelyFail)
    alert "Sorry, your graphics card is below our min spec.~br~The game will not run.~br~Please see the README for details." 2001
    seti gpuLevel $gpuLevelMin
endif

############################################################################
# Set base shader path
#

seti shaderPathToSet $shaderPathMax

if (not $isMac)
    if ($psVersion >= 3)
       seti shaderPathToSet $shaderPathMax
    else
       alert "Sorry, your graphics card is below our min spec.~br~The game will not run.~br~Please see the README for details.\n\n" 2001
    endif
else
    if ($psVersion >= 1)
        seti shaderPathToSet $shaderPathMax
    endif
endif

# NOTE: do not override this for special cases, unless the card will
# actually crash or has bugs. Instead setOption OptionLighting to
# reduce the path by default to what we recommend.
intProp shaderPath $shaderPathToSet

#############################################################################
# Instancing support

if ($instancing)
    trace "=== Instancing can be enabled"
    boolProp effectsInstancing true

    # rendering artifacts using instancing on hw tnl based intel cards
    #  dropping to swvp does not help
    if ($isIntel)
        boolProp effectsInstancing true
    endif
endif

# Report classification

trace "==="
trace "=== GPU level ${gpuLevel}"
trace "=== Card ${cardName}, Vendor ${cardVendor}, Driver ${driverBuild}, ${instancing}"
trace "==="
trace "=== CPU level ${cpuLevel}"
trace "=== CPU ${cpuCount}, Speed ${adjustedCPU}, HT ${hyperthreading}, P4 ${pentium4}"
trace "=== GPU Texture Memory ${textureMemory}"

#############################################################################
# Set options based on cpu / gpu
#

setOption OptionEffects             $cpuLevel
setOption OptionAudioPerformance    $cpuLevel
setOption OptionGameQuality         $cpuLevel

setOption OptionPeopleQuality       $gpuLevel
setOption OptionSignQuality         $gpuLevel


if ($gpuLevel <= 1)
    setOption OptionGeometryDetail 1
else
    setOption OptionGeometryDetail $gpuLevel
endif

if ($gpuLevel <= 1)
    setOption OptionTextureDetail 1
else
    setOption OptionTextureDetail $gpuLevel
endif

if ($gpuLevel <= 1)
    setOption OptionAnimationDetail 1
else
    setOption OptionAnimationDetail $gpuLevel
endif

#############################################################################
# MRT
#

boolProp MRT 0
if ( $numSimultaneousRTs > 1 )
    boolProp MRT 1
endif

############################################################################/Users/snagy/dev/SimCity/Data/Tests/Perf/Near_Zoom_Looking_Up.txt
# Resolution
#
seti resolutionTarget (1024 * 768) # 4x3

if ($isMac )
    if ($gpuLevel > $gpuLevelHigh)
        seti resolutionTarget (3840 * 2400) # 16x10
    elseif ($gpuLevel > $gpuLevelMedium)
        seti resolutionTarget (1920 * 1200) # 16x10
    elseif ($gpuLevel >= $gpuLevelLow)
        seti resolutionTarget (1280 * 800) # 16x10
    endif

else
    if ($gpuLevel >= $gpuLevelHigh)
        seti resolutionTarget (3840 * 2160) # 16x9
    elseif ($gpuLevel >= $gpuLevelMedium)
        seti resolutionTarget (1920 * 1080) # 16x9
    elseif ($gpuLevel >= $gpuLevelLow)
        seti resolutionTarget (1280 * 720) # 16x9
    endif
endif

setResolution $resolutionTarget

############################################################################
# Shadow settings
#

#seti shadowOption $cpuLevel
#if ($gpuLevel < $cpuLevel)
    #seti shadowOption $gpuLevel
#endif

seti shadowOption $gpuLevel
    
trace "=== Shadow setting ${shadowOption}"
setOption OptionShadows $shadowOption


############################################################################
# Event Query
#

# Note: Vista and now Windows 7 exhibits hangs when using D3DQUERYTYPE_EVENT.
# Clearly this is not something they'll fix, so now we only enable on XP.

#trace "OS = ${OSName}"
if (not $isXP)
    trace "=== Win7/Vista/Mac: avoiding GPU flush"
    intProp NumFramesToBuffer 3    # avoid GPU lock to reduce buffering -- leave it to the driver
endif


############################################################################
# Lighting level controls shaders that run.  Can be set higher.

setOption OptionLighting $gpuLevel

if ($gpuLevel > $gpuLevelMedium)
	setOption OptionFXAA $On
endif

if ($cpuLevel == $cpuLevelLow)
    # this sets the minspec shader path
    setOption OptionLighting $Min
endif


############################################################################
# Drop CPU Settings By GPU
#

if ($gpuLevel == $gpuLevelMedium)
    # prevent high cpu level from pushing too many batches to medium gpu
    #  this helps medium gpu classification apply to more cards
    if ($cpuLevel == $cpuLevelHigh)
        setOption OptionEffects       $Medium
        setOption OptionGameQuality $Medium
    endif

elseif ($gpuLevel == $gpuLevelLow)
    setOption OptionEffects       $Low
    setOption OptionGameQuality   $Low

    # swvp needs more cpu processing
    if ($isIntelIntegrated)
       setOption OptionAudioPerformance $Low
    endif
endif

#######################################
# Misc
#
#Mac fails to parse the below freeVirtualMemory conditional, so we are turning this off for Mac 04/11/2013
if (not $isMac)
    if ($freeVirtualMemory <= 2500)
        intProp modelMeshLODOffsetOverride 1 -list @SimCityNonSim_ecoUnit!TemplateBuilding
        intProp rasterMipOffset 1
    
         #bump this up for 'low' otherwise they get knocked down two levels
        if ($gpuLevel >= $gpuLevelLow)
            setOption OptionTextureDetail $Medium
        endif
    else
        intProp modelMeshLODOffsetOverride 0 -list @SimCityNonSim_ecoUnit!TemplateBuilding
        intProp rasterMipOffset 0
    endif
endif

#######################################
#  Specific OSX settings
if ($isMac)
    if ($isNvidia)
        if (match("${cardName}", "*650*"))
            setOption OptionLighting $Medium
            setOption OptionTextureDetail $Medium
        endif
    endif
endif

#######################################
# Safe mode
#

# Set options to safe values. In safe mode, all options will
# be reset to their defaults, but the prefs won't be saved.

# Basically setting anything that might trigger bad hardware behaviour to low.
if ($safeMode)
    trace "=== SAFE MODE"

    alert "Running in safe mode" 8888 -info

    setOption OptionLighting      $Low
    setOption OptionShadows       $Low
    setOption OptionEffects       $Low
    setOption OptionTextureDetail $Medium
    setOption OptionAudioPerformance    $Low
    setOption OptionPhotoRes            $Low
    setOption OptionVideoRes            $Low
endif

# These are all of the alert dialogs localized if text is present (from AppConfig.txt)
#set showConfigAlerts 1
if (varExists(showConfigAlerts) and ($showConfigAlerts))
    trace "=== Test Alerts"

    alert "not localized" 1000 -info -always # 3e8
    alert "not localized" 1001 -info -always # 3e9
    alert "not localized" 1002 -info -always # 3ea
    alert "not localized" 1004 -info -always # 3ec
    alert "not localized" 1005 -info -always # 3ed

    alert "not localized" 1010 -info -always # 3f2
    alert "not localized" 1011 -info -always # 3f3
    alert "not localized" 1012 -info -always # 3f4

    alert "not localized" 2000 -info -always # 7d0
    alert "not localized" 2001 -info -always # 7d1
    alert "not localized" 2002 -info -always # 7d2
    alert "not localized" 2003 -info -always # 7d3

    alert "not localized" 2004 -info -always # 7d4
    alert "not localized" 2005 -info -always # 7d5
    alert "not localized" 2006 -info -always # 7d6
    alert "not localized" 2007 -info -always # 7d7

endif
