#############################################################################
#
# Logging settings
#

set logGroup           "ConfigurationManager"
set logLevelWarning    warning
set logLevelInfo       info
set logLevelDebug      debug

log $logGroup $logLevelInfo "Parsing Mac configuration script"


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

seti Off 0
seti On  1

seti Low    1
seti Medium 2
seti High   3

seti memoryLevelHigh    512
seti memoryLevelMedium  256
seti memoryLevelLow     128
seti memoryLevelUnknown 0

seti cpuLevelHigh        1800
seti cpuLevelMedium      1200
seti cpuLevelLow         600
seti cpuLevelUnsupported 0


#############################################################################
#
# Identify gfx device
#

# set some config variables based on a card table and vendor specific rules
# sets isCardFound, cardVendor, and cardName
include "Video Cards.sgr"

# 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 ATI    -> Radeon 9800 Pro
#  For NVIDIA -> NVIDIA 4800 TI SE
if (not $isCardFound)
   set cardName $cardNameText
endif



#############################################################################
#
# Determine if gfx device is supported, force software rendering if not
#

setb useSoftwareRasterizer false


if ($useSoftwareRasterizer)
   log $logGroup $logLevelWarning "Unsupported video card.  Forcing software rendering on this device"
else
   log $logGroup $logLevelInfo "Supported video card."
endif

if (not $useSoftwareRasterizer)
   # failed to obtain device texture memory size, force to 32MB
   if ($textureMemory = 0)
      log $logGroup $logLevelDebug "failed to obtain device texture memory size"
      seti textureMemory       32
      setb textureMemorySizeOK false
   endif
endif

if (not $useSoftwareRasterizer)
   log $logGroup $logLevelInfo "Hardware rendering is enabled"
endif



#############################################################################
#
# Apply gfx device specific workarounds
#
# Available device props:
#  forceSoftwareVP
#  forceSoftwareDevice
#  queriesOK
#  windowedModeFormatConversionOK
#  enumerateMultisampleLevels
#  skipValidateDevice
#  enableDriverMemoryManager
#  softwareDeviceLocalVideoMemorySize
#  deviceLocalVideoMemorySizeDefault
#  disableVSyncSupport
#  substituteFlipForDiscardSwapEffect
#  minTextureDimensionForBalance
#
# Additional props:
#  vs2LoopsFunctional

setb textureMemorySizeOK        true
setb supportsDirtyRect          true
setb supportsTurboRect          false
setb supportsSpecialEventCamera true
setb forceLowSettings           false
setb forceLowResolution         false

boolProp useRenderTextures               false
uintProp antialiasingSupport             0


if (not $useSoftwareRasterizer)

   # never trust the driver to manage its own memory
   boolProp enableDriverMemoryManager  false

   boolProp vs2LoopsFunctional         false
   boolProp presentWorkaround          false
   boolProp enumerateMultisampleLevels false
   
   boolProp causticsEnabled            false
   boolProp useShaders                 false
   boolProp forceSoftwareVP            false
   boolProp disableTextureProjection   true
   boolProp forceSoftwareDevice        false

else
   setb forceLowSettings   true
   setb forceLowResolution true
 
   # force sw, swvp, and no aa
   boolProp forceSoftwareDevice        true
   boolProp forceSoftwareVP            true
   boolProp enumerateMultisampleLevels false
   boolProp simpleTerrain              true
   boolProp causticsEnabled            false

   intProp softwareDeviceLocalVideoMemorySize ($textureMemory * 1024 * 1024)

endif

# since we don't do bumpmapping on less ps2.0-capable hardware, eliminate tangents
# from the vertex data
# also, the presence of per-vertex tangents in the vertex data stream causes
# "exploded" polygons on the Radeon 9000, even if the data is ignored by the shader
if ($maxPixelProgramVersionMajor < 2)
   boolProp skipTangentsInVertexData true
endif


#############################################################################
#
# Print system info
#

if ($deviceNumber = 0)
logSystemInfo "=== Application info ==="
logSystemInfo "Name:            ${appName}"
logSystemInfo "Version:         ${version}"
logSystemInfo "Build:           ${buildType}"

logSystemInfo "=== Machine info ==="
logSystemInfo "OS version:      ${osVersion}"
logSystemInfo "CPU:             ${cpuSpeed}Mhz, Name:${CPU}, FPU:${FPU}, MMX:${MMX}"
logSystemInfo "Memory:          ${memory}MB"
logSystemInfo "Free memory:     ${freeMemory}MB"
logSystemInfo "User:            ${userName}"
# logSystemInfo "Computer:        ${computerName}"

logSystemInfo "=== Sound device info ==="
logSystemInfo "Name:            ${soundCardName}"
logSystemInfo "Driver:          ${soundDriverName}"
endif

logSystemInfo "=== Graphics device info ==="
logSystemInfo "Number:          ${deviceNumber}"
logSystemInfo "Name (driver):   ${cardNameText}"
if ($isCardFound)
logSystemInfo "Name (database): ${cardName}"
else
logSystemInfo "Name (database): ${cardName} <<NOT FOUND IN DATABASE!>>"
endif
logSystemInfo "Vendor:          ${cardVendor}"
logSystemInfo "Chipset:         ${cardChipset}"
logSystemInfo "Driver:          ${driverName}, Version: ${driverVersion}"
logSystemInfo "Driver version:  ${driverBuild}"
logSystemInfo "Monitor:         ${monitorName}"
logSystemInfo "Monitor aspect:  ${monitorAspect}, ${monitorAspectString}"
logSystemInfo "Screen mode:     ${screenWidth}x${screenHeight}x${screenBPP}BPP,${screenRefresh}Hz"
if ($textureMemorySizeOK)
logSystemInfo "Texture memory:  ${textureMemory}MB"
else
logSystemInfo "Texture memory:  ${textureMemory}MB <<OVERRIDE>>"
endif
logSystemInfo "HW T&L:          Fixed function:${fixedFunctionHWTnL} Programmable:${maxVertexProgramVersionHWMajor}.${maxVertexProgramVersionHWMinor}"
logSystemInfo "Pixel program:   ${maxPixelProgramVersionMajor}.${maxPixelProgramVersionMinor}"
logSystemInfo "Texture stages:  ${textureStages}"
logSystemInfo "AppControlledAA: ${appControlledAA}"


#############################################################################
#
# UI Options

log $logGroup $logLevelDebug "Begin parsing option definitions"

option MaterialDetail
   setting $Low
      boolProp bumpMapping false
      intProp  imageDataSizeReductionOnLoad 2

   setting $Medium
      boolProp bumpMapping false
      intProp  imageDataSizeReductionOnLoad 1

   setting $High
      boolProp bumpMapping true
      intProp  imageDataSizeReductionOnLoad 0
end


option ObjectDetail
   setting $Medium
      boolProp reduceBoneWeights true
      boolProp useLODs           true
      intProp  lodOverride       35

   setting $High
      boolProp reduceBoneWeights false
      boolProp useLODs           false
      intProp  lodOverride       0
end

option ObjectHiding
   setting $Off
      intProp renderInsideVisibleObjects 0

   setting $On
      intProp renderInsideVisibleObjects 1
end

option Shadows
   setting $Low
      boolProp simShadows       false
      boolProp objectShadows    false
      boolProp guob             false
      boolProp heightMapShadows false

   setting $Medium
      boolProp simShadows       false
      boolProp objectShadows    false
      boolProp guob             true
      boolProp heightMapShadows true

   setting $High
      boolProp simShadows       true
      boolProp objectShadows    true
      boolProp guob             true
      boolProp heightMapShadows true
end


option OpaqueUI
    setting $Off
       boolProp renderOpaqueUI false

    setting $On
       boolProp renderOpaqueUI true
end


option Reflection
   setting $Off
      boolProp    reflectionWithExtraViewer  false
      boolProp    nhoodWaterReflection       false

   setting $On
      boolProp    reflectionWithExtraViewer  true
      boolProp    nhoodWaterReflection       true
end


option EffectsQuality
   setting $Low
      boolProp  useEffects            true
      floatProp particleDensity       1
      floatProp particleScale         1
      intProp   maxParticlesTarget    2000
      intProp   particleLODOffset     0    # subtracted from the zoom.
      intProp   effectPriorityLevel   1

   setting $Medium
      boolProp  useEffects            true
      floatProp particleDensity       1
      floatProp particleScale         1
      intProp   maxParticlesTarget    5000
      intProp   particleLODOffset     0    # subtracted from the zoom.
      intProp   effectPriorityLevel   2

   setting $High
      boolProp  useEffects            true
      floatProp particleDensity       1
      floatProp particleScale         1
      intProp   maxParticlesTarget    10000
      intProp   particleLODOffset     0    # subtracted from the zoom.
      intProp   effectPriorityLevel   3
end


option LightingQuality
   setting $Low
      boolProp lightingEnabled           true
      boolProp portalLighting            false
      boolProp floorAndWallNormalMapping false
      boolProp specHighlights            false

   setting $Medium
      boolProp lightingEnabled           true
      boolProp portalLighting            true
      boolProp floorAndWallNormalMapping false
      boolProp specHighlights            true

   setting $High
      boolProp lightingEnabled           true
      boolProp portalLighting            true
      boolProp floorAndWallNormalMapping true
      boolProp specHighlights            true
end


option SoundQuality
   setting $Low
      intProp AudioPerformance 0

   setting $Medium
      intProp AudioPerformance 1

   setting $High
      intProp AudioPerformance 2
end


#
# Options without ui access.
#
option DirtyRect
   setting $High
      # no dirty rects
      intProp dynamicRenderStrategy 0

   setting $Medium
      # 4 buffer mode
      intProp dynamicRenderStrategy 2

   setting $Low
      # 2 buffer mode
      intProp dynamicRenderStrategy 1
end


option FullscreenFadeEffect
   setting $Off
      boolProp enableSnapshot false

   setting $On
      boolProp enableSnapshot true
end


option Turbo
   setting $High
      boolProp useTurboRect true

   setting $Medium
      boolProp useTurboRect true

   setting $Low
      boolProp useTurboRect false

end


option SimulatorControls
   setting $High
      intProp maxNumOfVisitingSims   8

   setting $Medium
      intProp maxNumOfVisitingSims   4

   setting $Low
      intProp maxNumOfVisitingSims   2
end


option LightingOptimizations
   setting $High
      boolProp optimizedDiffusion        true
      boolProp incrementalLighting       true
      boolProp lerpLights                true
      boolProp useDirtyTiles             true
end


option AnimationSamplingLevel
   setting $Low
      # sample nearest animation frame
      boolProp animationFrameSampling true

   setting $Medium
      # sample nearest two animation frames and slerp
      boolProp animationFrameSampling false
end


option LivePIP
   setting $Off
      boolProp livePIP        false
      boolProp livePIPDefault false

   setting $On
      boolProp livePIPDefault true
	# do not override user's choice if the setting is ON
end


option SpecialEventCamera
   setting $Off
      boolProp chooseCameraSpecialEventEnabled false
      boolProp CameraSpecialEventEnabled       false

   setting $On
      boolProp chooseCameraSpecialEventEnabled true
	# do not override user's choice if the setting is ON

      
end


option ScreenModeResolution
   setting $Low
      uintProp maxResWidth      2048
      uintProp maxResHeight     2048
      uintProp defaultResWidth  800
      uintProp defaultResHeight 600

   setting $Medium
      uintProp maxResWidth      2048
      uintProp maxResHeight     2048
      uintProp defaultResWidth  1024
      uintProp defaultResHeight 768

   setting $High
      uintProp maxResWidth      2048
      uintProp maxResHeight     2048
      uintProp defaultResWidth  1024
      uintProp defaultResHeight 768
end

log $logGroup $logLevelDebug "Finished parsing option definitions"


#############################################################################
#
# Hardware Rendering
#

# This cannot be defined as a local variable, the if clause is still executed when
#   sw render path is taken and an exception generated that fails the parsing.  Be
#   careful with the use of local variables.

if (not $useSoftwareRasterizer)
   
   # set a base level of options based on the card rating
   if (($textureMemory >= 64) and ($memory >= $memoryLevelMedium) and ($cpuSpeed >= $cpuLevelMedium) and ($maxVertexProgramVersionHWMajor >= 1) and not $forceLowSettings)
      log $logGroup $logLevelInfo "Selecting Medium base level"
      
      setOption OpaqueUI              $Off
      setOption LivePIP               $On
      setOption Reflection            $Off
      setOption EffectsQuality        $Medium
      setOption LightingQuality       $Medium
      setOption Shadows               $Medium
      setOption MaterialDetail        $Medium
      setOption ObjectDetail          $Medium
      setOption ObjectHiding          $On

   else
      log $logGroup $logLevelInfo "Selecting Low base level"
      setb forceLowSettings true

      setOption OpaqueUI              $Off
      setOption LivePIP               $Off
      setOption Reflection            $Off
      setOption EffectsQuality        $Low
      setOption LightingQuality       $Low
      setOption Shadows               $Low
      setOption MaterialDetail        $Low
      setOption ObjectDetail          $Medium
      setOption ObjectHiding          $On

   endif

   setOption LightingOptimizations $High

   # adjust simulator based on cpu speed
   # adjust the sound quality based on cpu speed (this may be overridden below for low-end systems)
   # adjust animation sampling based on cpu speed

   if ($cpuSpeed >= $cpuLevelMedium and not $forceLowSettings)
      setOption SimulatorControls      $Medium
      setOption SoundQuality           $Medium
      setOption AnimationSamplingLevel $Medium
   else
      setOption SimulatorControls      $Low
      setOption SoundQuality           $Low
      setOption AnimationSamplingLevel $Low
   endif

   # turn off fullscreen fade
   setOption FullscreenFadeEffect    $Off
   if ($textureMemory >= 128)
      setOption FullscreenFadeEffect $On
   endif

   # screen res defaults
   if (($maxVertexProgramVersionHWMajor >= 1) and ($textureMemory >= 128) and not $forceLowResolution)
      setOption ScreenModeResolution $High
   elseif ($textureMemory >= 64 and not $forceLowResolution)
      setOption ScreenModeResolution $Medium
   else
      setOption ScreenModeResolution $Low
   endif

   # special event cameras for cinematics
   if (($maxPixelProgramVersionMajor >= 1) and ($supportsSpecialEventCamera))
      setOption SpecialEventCamera $On
   else
      setOption SpecialEventCamera $Off
   endif

   # set dirty rect mode
   if ($supportsDirtyRect)
      setOption DirtyRect $Medium
   else
      setOption DirtyRect $High
   endif
   
   # set turbo mode
   if ($supportsTurboRect)
      setOption Turbo $Medium
   else
      setOption Turbo $Low
   endif
   
endif


#############################################################################
# save the selected configuration for this device.

boolProp activeDeviceUseSoftwareRasterizer $useSoftwareRasterizer

#############################################################################
# tell the caller that we completely finished parsing the script
intProp configParserErrorCode 0

log $logGroup $logLevelInfo "Finished Config File"
