<?xml version="1.0" encoding="UTF-8"?>
<!--
###################################################################################
##                                                                               ##
## Improved redout/blackout system for Flightgear                                ##
##                                                                               ##
## Author: Nikolai V. Chr. (property rules), Thorsten Renk (shaders)             ##
##                                                                               ##
##                                                                               ##
##                                                                               ##
##                                                                               ##
## Version 2.05            License: GPL 2.0                                      ##
##                                                                               ##
###################################################################################
-->
<PropertyList>

    <params>
        <onset-b>sim/rendering/redout/parameters/blackout-onset-g</onset-b>
        <fast-b>sim/rendering/redout/parameters/blackout-complete-g</fast-b>
        <onset-r>sim/rendering/redout/parameters/redout-onset-g</onset-r>
        <fast-r>sim/rendering/redout/parameters/redout-complete-g</fast-r>
        <onset-t-b>sim/rendering/redout/parameters/onset-blackout-sec</onset-t-b>
        <fast-t-b>sim/rendering/redout/parameters/fast-blackout-sec</fast-t-b>
        <onset-t-r>sim/rendering/redout/parameters/onset-redout-sec</onset-t-r>
        <fast-t-r>sim/rendering/redout/parameters/fast-redout-sec</fast-t-r>
        <rec-f>sim/rendering/redout/parameters/recover-fast-sec</rec-f>
        <rec-s>sim/rendering/redout/parameters/recover-slow-sec</rec-s>
        <log>
            <g-force>sim/rendering/redout/internal/log/g-force</g-force>
            <onset-b>sim/rendering/redout/internal/log/onset-b</onset-b>
            <fast-b>sim/rendering/redout/internal/log/fast-b</fast-b>
            <g-force-r>sim/rendering/redout/internal/log/g-force-r</g-force-r>
            <onset-r>sim/rendering/redout/internal/log/onset-r</onset-r>
            <fast-r>sim/rendering/redout/internal/log/fast-r</fast-r>
        </log>
        <g-damp>accelerations/pilot-gdamped</g-damp>
        <g-force-inv>sim/rendering/redout/internal/g-force-inv</g-force-inv>
        <blackout>sim/rendering/redout/internal/blackout</blackout>
        <redout>sim/rendering/redout/internal/redout</redout>
        <jsb>sim/rendering/redout/internal/jsb</jsb>
        <alpha>sim/rendering/redout/alpha</alpha>
        <red>sim/rendering/redout/red</red>
        <inside>sim/current-view/internal</inside>
        <sum>sim/rendering/redout/internal/sum</sum>
        <enable>sim/rendering/redout/enabled</enable>
        <g-force>accelerations/pilot-g[0]</g-force>
        <greyout>sim/rendering/als-filters/grey-factor</greyout>
        <tunnel>sim/rendering/als-filters/black-factor</tunnel>
        <als>sim/rendering/shaders/skydome</als>
        <filters>sim/rendering/als-filters/use-filtering</filters>
    </params>

    <filter><!-- check for NaN -->
        <type>gain</type>
        <gain>0</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
                <not>
                    <less-than>
                        <property alias="/params/blackout"/>
                        <value>1000.0</value>
                    </less-than>
                    <greater-than>
                        <property alias="/params/blackout"/>
                        <value>-1000.0</value>
                    </greater-than>
                </not>
            </condition>
        </enable>
        <input>
            <value>0</value>
        </input>
        <output alias="/params/blackout"/>
    </filter>

    <filter><!-- check for NaN -->
        <type>gain</type>
        <gain>0</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
                <not>
                    <less-than>
                        <property alias="/params/redout"/>
                        <value>1000.0</value>
                    </less-than>
                    <greater-than>
                        <property alias="/params/redout"/>
                        <value>-1000.0</value>
                    </greater-than>
                </not>
            </condition>
        </enable>
        <input>
            <value>0</value>
        </input>
        <output alias="/params/redout"/>
    </filter>

    <filter>
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <!-- check if blackout onset has sane value. -->
            <condition>
                <or>
                    <less-than>
                        <property alias="/params/onset-b"/>
                        <value>1.25</value>
                    </less-than>
                </or>
            </condition>
        </enable>
        <input>
            <expression>
                <max>
                    <property alias="/params/fast-b"/>
                    <value>1.25</value>
                </max>
            </expression>
        </input>
        <output alias="/params/onset-b"/>
    </filter>

    <filter>
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <!-- check if blackout complete has sane value. -->
            <condition>
                <or>
                    <greater-than>
                        <property alias="/params/onset-b"/>
                        <property alias="/params/fast-b"/>                    
                    </greater-than>
                    <less-than>
                        <property alias="/params/fast-b"/>
                        <value>1.5</value>
                    </less-than>
                </or>
            </condition>
        </enable>
        <input>
            <expression>
                <max>
                    <sum>
                        <property alias="/params/onset-b"/>
                        <value>0.50</value>
                    </sum>
                    <value>1.5</value>
                </max>
            </expression>
        </input>
        <output alias="/params/fast-b"/>
    </filter>

    <filter>
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <!-- check if redout onset has sane value. -->
            <condition>
                <or>
                    <greater-than>
                        <property alias="/params/onset-r"/>
                        <value>-1.00</value>
                    </greater-than>
                </or>
            </condition>
        </enable>
        <input>
            <expression>
                <min>
                    <property alias="/params/fast-r"/>
                    <value>-1.00</value>
                </min>
            </expression>
        </input>
        <output alias="/params/onset-r"/>
    </filter>

    <filter>
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <!-- check if redout complete has sane value. -->
            <condition>
                <or>
                    <greater-than>
                        <property alias="/params/fast-r"/>
                        <property alias="/params/onset-r"/>                                        
                    </greater-than>
                    <greater-than>
                        <property alias="/params/fast-r"/>
                        <value>-1.25</value>
                    </greater-than>
                </or>
            </condition>
        </enable>
        <input>
            <expression>
                <min>
                    <sum>
                        <property alias="/params/onset-r"/>
                        <value>-0.50</value>
                    </sum>
                    <value>-1.25</value>
                </min>
            </expression>
        </input>
        <output alias="/params/fast-r"/>
    </filter>    

    <filter>
        <type>gain</type>
        <gain>-1</gain>
        <enable>
            <!-- check if JSBSim is the FDM. -->
            <condition>
                <equals>
                    <property alias="/params/jsb"/>
                    <value>1</value>
                </equals>
            </condition>
        </enable>
        <input>
            <expression>
                <div>
                    <property>accelerations/pilot/z-accel-fps_sec</property>
                    <value>32.174</value>
                </div>
            </expression>
        </input>
        <output alias="/params/g-force"/>
    </filter>

    <filter>
        <!-- for backwards compatibility for planes that use gdamped property -->
        <type>exponential</type>
        <filter-time>0.2</filter-time>
        <input alias="/params/g-force"/>
        <output alias="/params/g-damp"/>
    </filter>

    <filter>
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
            </condition>
        </enable>
        <input>
            <expression>
                <log10>
                    <property alias="/params/fast-b"/>
                </log10>
            </expression>
        </input>
        <output alias="/params/log/fast-b"/>
    </filter>

    <filter>
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
            </condition>
        </enable>
        <input>
            <expression>
                <log10>
                    <property alias="/params/onset-b"/>
                </log10>
            </expression>
        </input>
        <output alias="/params/log/onset-b"/>
    </filter>

    <filter>
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
                <less-than-equals>
                    <property alias="/params/g-force"/>
                    <value>1</value>
                </less-than-equals>
            </condition>
        </enable>
        <input>
            <value>0</value>
        </input>
        <output alias="/params/log/g-force"/>
    </filter>

    <filter>
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
                <greater-than>
                    <property alias="/params/g-force"/>
                    <value>1</value>
                </greater-than>
            </condition>
        </enable>
        <input>
            <expression>
                <log10>
                    <property alias="/params/g-force"/>
                </log10>
            </expression>
        </input>
        <output alias="/params/log/g-force"/>
    </filter>

    <filter><!-- reduce blackout -->
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
                <less-than>
                    <property alias="/params/g-force"/>
                    <property alias="/params/onset-b"/>
                </less-than>
            </condition>
        </enable>
        <input>
            <expression>
                <min>
                    <value>1</value>
                    <max>
                        <value>0</value>
                        <dif>
                            <property alias="/params/blackout"/>
                            <product>
                                <property>sim/time/delta-sec</property>
                                <div>
                                    <value>1</value>
                                    <max>
                                        <value>0</value>
                                        <sum>
                                            <property alias="/params/rec-f"/>
                                            <product>
                                                <div>
                                                    <dif>
                                                        <property alias="/params/log/g-force"/>
                                                        <log10>
                                                            <value>1</value>
                                                        </log10>
                                                    </dif>
                                                    <dif>
                                                        <property alias="/params/log/onset-b"/>
                                                        <log10>
                                                            <value>1</value>
                                                        </log10>
                                                    </dif>
                                                </div>
                                                <dif>
                                                    <property alias="/params/rec-s"/>
                                                    <property alias="/params/rec-f"/>
                                                </dif>
                                            </product>
                                        </sum>
                                    </max>
                                </div>
                            </product>
                        </dif>
                    </max>
                </min>
            </expression>
        </input>
        <output alias="/params/blackout"/>
    </filter>    

    <filter><!-- increase blackout -->
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
                <greater-than-equals>
                    <property alias="/params/g-force"/>
                    <property alias="/params/onset-b"/>
                </greater-than-equals>
            </condition>
        </enable>
        <input>
            <expression>
                <min>
                    <value>1</value>
                    <max>
                        <value>0</value>
                        <sum>
                            <property alias="/params/blackout"/>
                            <product>
                                <property>sim/time/delta-sec</property>
                                <div>
                                    <value>1</value>
                                    <max>
                                        <value>0</value>
                                        <pow>
                                            <value>10</value>
                                            <sum>
                                                <log10>
                                                    <property alias="/params/onset-t-b"/>
                                                </log10>
                                                <product>
                                                    <div>
                                                        <dif>
                                                            <property alias="/params/log/g-force"/>
                                                            <property alias="/params/log/onset-b"/>
                                                        </dif>
                                                        <dif>
                                                            <property alias="/params/log/fast-b"/>
                                                            <property alias="/params/log/onset-b"/>
                                                        </dif>
                                                    </div>
                                                    <dif>
                                                        <log10>
                                                            <property alias="/params/fast-t-b"/>
                                                        </log10>
                                                        <log10>
                                                            <property alias="/params/onset-t-b"/>
                                                        </log10>
                                                    </dif>
                                                </product>
                                            </sum>
                                        </pow>
                                    </max>
                                </div>
                            </product>
                        </sum>
                    </max>
                </min>
            </expression>
        </input>
        <output alias="/params/blackout"/>
    </filter>

    <filter>
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
            </condition>
        </enable>
        <input>
            <expression>
                <log10>
                    <product>
                        <value>-1</value>
                        <dif>
                            <property alias="/params/fast-r"/>
                            <value>2</value>
                        </dif>
                    </product>
                </log10>
            </expression>
        </input>
        <output alias="/params/log/fast-r"/>
    </filter>

    <filter>
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
            </condition>
        </enable>
        <input>
            <expression>
                <log10>
                    <product>
                        <value>-1</value>
                        <dif>
                            <property alias="/params/onset-r"/>
                            <value>2</value>
                        </dif>
                    </product>
                </log10>
            </expression>
        </input>
        <output alias="/params/log/onset-r"/>
    </filter>

    <filter>
        <type>gain</type>
        <gain>-1</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
            </condition>
        </enable>
        <input>
            <expression>
                <dif>
                    <property alias="/params/g-force"/>
                    <value>2</value>
                </dif>
            </expression>
        </input>
        <output alias="/params/g-force-inv"/>
    </filter>

    <filter>
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
                <less-than-equals>
                    <property alias="/params/g-force-inv"/>
                    <value>1</value>
                </less-than-equals>
            </condition>
        </enable>
        <input>
            <value>0</value>
        </input>
        <output alias="/params/log/g-force-r"/>
    </filter>

    <filter>
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
                <greater-than>
                    <property alias="/params/g-force-inv"/>
                    <value>1</value>
                </greater-than>
            </condition>
        </enable>
        <input>
            <expression>
                <log10>
                    <property alias="/params/g-force-inv"/>
                </log10>
            </expression>
        </input>
        <output alias="/params/log/g-force-r"/>
    </filter>

    <filter><!-- reduce redout -->
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
                <greater-than>
                    <property alias="/params/g-force"/>
                    <property alias="/params/onset-r"/>
                </greater-than>
            </condition>
        </enable>
        <input>
            <expression>
                <min>
                    <value>1</value>
                    <max>
                        <value>0</value>
                        <dif>
                            <property alias="/params/redout"/>
                            <product>
                                <property>sim/time/delta-sec</property>
                                <div>
                                    <value>1</value>
                                    <max>
                                        <value>0</value>
                                        <sum>
                                            <property alias="/params/rec-f"/>
                                            <product>
                                                <div>
                                                    <dif>
                                                        <property alias="/params/log/g-force-r"/>
                                                        <log10>
                                                            <value>1</value>
                                                        </log10>
                                                    </dif>
                                                    <dif>
                                                        <property alias="/params/log/onset-r"/>
                                                        <log10>
                                                            <value>1</value>
                                                        </log10>
                                                    </dif>
                                                </div>
                                                <dif>
                                                    <property alias="/params/rec-s"/>
                                                    <property alias="/params/rec-f"/>
                                                </dif>
                                            </product>
                                        </sum>
                                    </max>
                                </div>
                            </product>
                        </dif>
                    </max>
                </min>
            </expression>
        </input>
        <output alias="/params/redout"/>
    </filter>    

    <filter><!-- increase redout -->
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
                <less-than-equals>
                    <property alias="/params/g-force"/>
                    <property alias="/params/onset-r"/>
                </less-than-equals>
            </condition>
        </enable>
        <input>
            <expression>
                <min>
                    <value>1</value>
                    <max>
                        <value>0</value>
                        <sum>
                            <property alias="/params/redout"/>
                            <product>
                                <property>sim/time/delta-sec</property>
                                <div>
                                    <value>1</value>
                                    <max>
                                        <value>0</value>
                                        <pow>
                                            <value>10</value>
                                            <sum>
                                                <log10>
                                                    <property alias="/params/onset-t-r"/>
                                                </log10>
                                                <product>
                                                    <div>
                                                        <dif>
                                                            <property alias="/params/log/g-force-r"/>
                                                            <property alias="/params/log/onset-r"/>
                                                        </dif>
                                                        <dif>
                                                            <property alias="/params/log/fast-r"/>
                                                            <property alias="/params/log/onset-r"/>
                                                        </dif>
                                                    </div>
                                                    <dif>
                                                        <log10>
                                                            <property alias="/params/fast-t-r"/>
                                                        </log10>
                                                        <log10>
                                                            <property alias="/params/onset-t-r"/>
                                                        </log10>
                                                    </dif>
                                                </product>
                                            </sum>
                                        </pow>
                                    </max>
                                </div>
                            </product>
                        </sum>
                    </max>
                </min>
            </expression>
        </input>
        <output alias="/params/redout"/>
    </filter>

    <filter>
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
            </condition>
        </enable>
        <input>
            <expression>
                <dif>
                    <property alias="/params/blackout"/>
                    <property alias="/params/redout"/>
                </dif>
            </expression>
        </input>
        <output alias="/params/sum"/>
    </filter>

    <filter>
        <!-- When system is on, in pilot view and negative G effect, set fading color to red -->
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
                <equals>
                    <property alias="/params/inside"/>
                    <value>1</value>
                </equals>
                <less-than>
                    <property alias="/params/sum"/>
                    <value>0</value>
                </less-than>
            </condition>
        </enable>
        <input>
            <value>1</value>
        </input>
        <output alias="/params/red"/>
    </filter>

    <filter>
        <!-- When system is on, in pilot view and negative G effect, set fading amount -->
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
                <equals>
                    <property alias="/params/inside"/>
                    <value>1</value>
                </equals>
                <less-than>
                    <property alias="/params/sum"/>
                    <value>0</value>
                </less-than>
            </condition>
        </enable>
        <input>
            <expression>
                <product>
                    <property alias="/params/sum"/>
                    <value>-1</value>
                </product>
            </expression>
        </input>
        <output alias="/params/alpha"/>
    </filter>

    <filter>
        <!-- When system is on, in pilot view and positive G effect, set fading color to black -->
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
                <equals>
                    <property alias="/params/inside"/>
                    <value>1</value>
                </equals>
                <greater-than-equals>
                    <property alias="/params/sum"/>
                    <value>0</value>
                </greater-than-equals>
            </condition>
        </enable>
        <input>
            <value>0</value>
        </input>
        <output alias="/params/red"/>
    </filter>

    <filter>
        <!-- When system is on, in pilot view and effect should be black, set fading amount -->
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
                <equals>
                    <property alias="/params/inside"/>
                    <value>1</value>
                </equals>
                <greater-than-equals>
                    <property alias="/params/sum"/>
                    <value>0</value>
                </greater-than-equals>
            </condition>
        </enable>
        <input>
            <expression>
                <min>
                    <value>1</value>
                    <max>
                        <value>0</value>
                        <sum>
                            <product>
                                <property alias="/params/als"/>
                                <property alias="/params/filters"/>
                                <!-- this will be used when ALS filters are in effect -->
                                <div>
                                    <dif>
                                        <property alias="/params/sum"/>
                                        <value>0.66</value><!-- onset of blackout -->
                                    </dif>
                                    <value>0.34</value><!-- (full-onset) of blackout -->
                                </div>
                            </product>
                            <product>
                                <dif>
                                    <value>1</value>
                                    <product>
                                        <property alias="/params/als"/>
                                        <property alias="/params/filters"/>
                                    </product>
                                </dif>                                
                                <!-- this will be used when ALS filters are off -->
                                <property alias="/params/sum"/>
                            </product>
                        </sum>
                    </max>
                </min>
            </expression>
        </input>
        <output alias="/params/alpha"/>
    </filter>

    <filter>
        <!-- When system is off or not in pilot view, set fading to off -->
        <type>gain</type>
        <gain>0</gain>
        <enable>
            <condition>
                <or>
                    <not>
                        <property alias="/params/enable"/>
                    </not>
                    <equals>
                        <property alias="/params/inside"/>
                        <value>0</value>
                    </equals>
                </or>
            </condition>
        </enable>
        <input>
            <value>0</value>
        </input>
        <output alias="/params/alpha"/>
    </filter>

    <filter>
        <!-- When system disabled, set blackout amount to 0 -->
        <type>gain</type>
        <gain>0</gain>
        <enable>
            <condition>
                <not>
                    <property alias="/params/enable"/>
                </not>
            </condition>
        </enable>
        <input>
            <value>0</value>
        </input>
        <output alias="/params/blackout"/>
    </filter>

    <filter>
        <!-- When system is disabled, set redout amount to 0 -->
        <type>gain</type>
        <gain>0</gain>
        <enable>
            <condition>
                <not>
                    <property alias="/params/enable"/>
                </not>
            </condition>
        </enable>
        <input>
            <value>0</value>
        </input>
        <output alias="/params/redout"/>
    </filter>

    <filter>
        <!-- When system is off or not in pilot view or negative G effect, set greyout to off -->
        <type>gain</type>
        <gain>0</gain>
        <enable>
            <condition>
                <or>
                    <not>
                        <property alias="/params/enable"/>
                    </not>
                    <equals>
                        <property alias="/params/inside"/>
                        <value>0</value>
                    </equals>
                    <less-than>
                        <property alias="/params/sum"/>
                        <value>0</value>
                    </less-than>
                </or>
            </condition>
        </enable>
        <input>
            <value>0</value>
        </input>
        <output alias="/params/greyout"/>
    </filter>

    <filter>
        <!-- When system is off or not in pilot view or negative G effect, set tunnel-vision to off -->
        <type>gain</type>
        <gain>0</gain>
        <enable>
            <condition>
                <or>
                    <not>
                        <property alias="/params/enable"/>
                    </not>
                    <equals>
                        <property alias="/params/inside"/>
                        <value>0</value>
                    </equals>
                    <less-than>
                        <property alias="/params/sum"/>
                        <value>0</value>
                    </less-than>
                </or>
            </condition>
        </enable>
        <input>
            <value>0</value>
        </input>
        <output alias="/params/tunnel"/>
    </filter>

    <filter>
        <!-- When system is on, in pilot view and positive G effect, set tunnel-vision amount -->
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
                <equals>
                    <property alias="/params/inside"/>
                    <value>1</value>
                </equals>
                <greater-than-equals>
                    <property alias="/params/sum"/>
                    <value>0</value>
                </greater-than-equals>
            </condition>
        </enable>
        <input>
            <expression>
                <min>
                    <value>1</value>
                    <max>
                        <value>0</value>
                        <div>
                            <dif>
                                <property alias="/params/sum"/>
                                <value>0.35</value><!-- onset of tunnel -->
                            </dif>
                            <value>0.65</value><!-- (full-onset) of tunnel -->
                        </div>
                    </max>
                </min>
            </expression>
        </input>
        <output alias="/params/tunnel"/>
    </filter>

    <filter>
        <!-- When system is on, in pilot view and positive G effect, set greyout amount -->
        <type>gain</type>
        <gain>1</gain>
        <enable>
            <condition>
                <property alias="/params/enable"/>
                <equals>
                    <property alias="/params/inside"/>
                    <value>1</value>
                </equals>
                <greater-than-equals>
                    <property alias="/params/sum"/>
                    <value>0</value>
                </greater-than-equals>
            </condition>
        </enable>
        <input>
            <expression>
                <min>
                    <value>1</value>
                    <max>
                        <value>0</value>
                        <div>
                            <dif>
                                <property alias="/params/sum"/>
                                <value>0.15</value><!-- onset of greyout -->
                            </dif>
                            <value>0.65</value><!-- (full-onset) of greyout -->
                        </div>
                    </max>
                </min>
            </expression>
        </input>
        <output alias="/params/greyout"/>
    </filter>

</PropertyList>