# Handles adding & removing the dynamic modifiers as necessary
CFR_se_handle_dyn_modifier = {
    if = {
        limit = {
            OR = {
                check_variable = { CFR_steel_usage    > 0 }
                check_variable = { CFR_chromium_usage > 0 }
            }
            NOT = {
                has_dynamic_modifier = {
                    modifier = CFR_resource_modifier
                }
            }
        }
        add_dynamic_modifier = {
            modifier = CFR_resource_modifier
        }
    }
    else_if = {
        limit = {
            check_variable = { CFR_steel_usage    = 0 }
            check_variable = { CFR_chromium_usage = 0 }
            has_dynamic_modifier = {
                modifier = CFR_resource_modifier
            }
        }
        remove_dynamic_modifier = {
            modifier = CFR_resource_modifier
        }
    }
    
    if = {
        limit = {
            check_variable = { CFR_factory_output_reduction < 0 }
            NOT = {
                has_dynamic_modifier = {
                    modifier = CFR_factory_output_modifier
                }
            }
        }
        add_dynamic_modifier = {
            modifier = CFR_factory_output_modifier
        }
    }
    else_if = {
        limit = {
            check_variable = { CFR_factory_output_reduction = 0 }
            has_dynamic_modifier = {
                modifier = CFR_factory_output_modifier
            }
        }
        remove_dynamic_modifier = {
            modifier = CFR_factory_output_modifier
        }
    }
}

# Determine if factories are slowed down, and if so, by how much
# Outputs the following values to the listed variables:
#   CFR_factory_output_reduction: Decimal expression of % slowdown; range is [-1.00, 0]
#   CFR_steel_deficit:    Qty. of steel demand not met; always 0 if there is no shortage; positive number
#   CFR_chromium_deficit: Qty. of chromium demand not met; always 0 if no shortage; positive number
CFR_se_calculate_factory_output_reduction = {
    # Set/reset variables
    set_variable = {
        var   = CFR_factory_output_reduction
        value = 0
    }
    set_variable = {
        var   = CFR_steel_deficit
        value = resource@steel
    }
    set_variable = {
        var   = CFR_chromium_deficit
        value = resource@chromium
    }
    
    # Clamp
    clamp_variable = {
        var = CFR_steel_deficit
        max = 0
    }
    clamp_variable = {
        var = CFR_chromium_deficit
        max = 0
    }
    
    # Flip sign (since these values are used for user-facing text)
    multiply_variable = {
        var   = CFR_steel_deficit
        value = -1
    }
    multiply_variable = {
        var   = CFR_chromium_deficit
        value = -1
    }
    
    # Determine shortage impact
    # First calculates what % of a shortage there is, i.e. the ratio between the shortage qty. and the qty. demanded
    # Uses that % to scale the magnitude of the impact modifier
    if = {
        limit = {
            OR = {
                check_variable = { CFR_steel_usage > 0 }
                check_variable = { CFR_chromium_usage > 0 }
            }
        }
        set_temp_variable = {
            var   = steel_shortage
            value = 0
        }
        set_temp_variable = {
            var   = chromium_shortage
            value = 0
        }
        
        if = {
            limit = {
                check_variable = { CFR_steel_usage > 0 }
            }
            set_temp_variable = {
                var   = steel_shortage
                value = var:CFR_steel_deficit
            }
            divide_temp_variable = {
                var   = steel_shortage
                value = var:CFR_steel_usage
            }
        }
        if = {
            limit = {
                check_variable = { CFR_chromium_usage > 0 }
            }
            set_temp_variable = {
                var   = chromium_shortage
                value = var:CFR_chromium_deficit
            }
            divide_temp_variable = {
                var   = chromium_shortage
                value = var:CFR_chromium_usage
            }
        }
        
        add_to_temp_variable = { # variable reuse :)
            var   = steel_shortage
            value = temp_var:chromium_shortage
        }
        multiply_temp_variable = {
            var   = steel_shortage
            value = 0.5
        }
        clamp_temp_variable = {
            var = steel_shortage
            min = 0
            max = 1
        }
        multiply_temp_variable = {
            var   = steel_shortage
            value = -0.75 # Max size of shortage penalty; must be negative here to flip the sign for the modifier
        }
        
        set_variable = {
            var   = CFR_debug_1
            value = temp_var:steel_shortage
        }
        
        set_variable = {
            var   = CFR_factory_output_reduction
            value = temp_var:steel_shortage
        }
    }
}

# Determine steel & chromium usage rate
# Outputs the following values to the listed variables:
#   CFR_steel_usage:    Total qty. steel demanded
#   CFR_chromium_usage: Total qty. chromium demanded
#   CFR_heavy_industry: Qty. of civs used for construction & repair (i.e., usable by the player)
#   CFR_light_industry: Qty. of civs used for trade, consumer goods, & projects (i.e., not usable by the player)
CFR_se_calculate_resource_usage = {
    # Set/reset variables
    set_variable = {
        var   = CFR_steel_usage
        value = 0
    }
    set_variable = {
        var   = CFR_chromium_usage
        value = 0
    }
    set_variable = { # Factories used for construction & repair
        var   = CFR_heavy_industry
        value = num_of_civilian_factories_available_for_projects
    }
    set_variable = { # Factories used for consumer goods & projects
        var   = CFR_light_industry
        value = num_of_civilian_factories
    }
    subtract_from_variable = {
        var   = CFR_light_industry
        value = var:CFR_heavy_industry
    }
    
    # Declaring some working variables
    set_temp_variable = {
        var   = total_civs
        value = num_of_civilian_factories
    }
    set_temp_variable = {
        var   = civs_in_bracket
        value = 0
    }
    set_temp_variable = {
        var   = temp_steel
        value = 0
    }
    set_temp_variable = {
        var   = temp_chromium
        value = 0
    }
    
    # Calculate total base usage
    # Base rates are calculated using the following brackets:
    # [  0,  15] => 0.00 S, 0.00 C (Steel, Chromium)
    # ( 15,  30] => 0.25 S, 0.10 C
    # ( 30,  45] => 0.50 S, 0.25 C
    # ( 45,  60] => 0.75 S, 0.50 C
    # ( 60,  75] => 1.00 S, 1.00 C
    # ( 75, 105] => 1.25 S, 1.50 C
    # (105, 210] => 1.75 S, 2.50 C
    # (210, inf) => 2.50 S, 4.00 C
    # Visualized: https://www.desmos.com/calculator/ihvtrjqhwo
    # Please pardon my implementation btw :(
    if = {
        limit = {
            check_variable = { total_civs > 210 }
        }
        set_temp_variable = {
            var   = civs_in_bracket
            value = temp_var:total_civs
        }
        subtract_from_temp_variable = {
            var   = civs_in_bracket
            value = 210
        }
        set_temp_variable = {
            var   = temp_steel
            value = 2.5
        }
        set_temp_variable = {
            var   = temp_chromium
            value = 4
        }
        multiply_temp_variable = {
            var   = temp_steel
            value = temp_var:civs_in_bracket
        }
        multiply_temp_variable = {
            var   = temp_chromium
            value = temp_var:civs_in_bracket
        }
        add_to_variable = {
            var   = CFR_steel_usage
            value = temp_var:temp_steel
        }
        add_to_variable = {
            var   = CFR_chromium_usage
            value = temp_var:temp_chromium
        }
        subtract_from_temp_variable = {
            var   = total_civs
            value = temp_var:civs_in_bracket
        }
    }
    if = {
        limit = {
            check_variable = { total_civs > 105 }
        }
        set_temp_variable = {
            var   = civs_in_bracket
            value = temp_var:total_civs
        }
        subtract_from_temp_variable = {
            var   = civs_in_bracket
            value = 105
        }
        set_temp_variable = {
            var   = temp_steel
            value = 1.75
        }
        set_temp_variable = {
            var   = temp_chromium
            value = 2.5
        }
        multiply_temp_variable = {
            var   = temp_steel
            value = temp_var:civs_in_bracket
        }
        multiply_temp_variable = {
            var   = temp_chromium
            value = temp_var:civs_in_bracket
        }
        add_to_variable = {
            var   = CFR_steel_usage
            value = temp_var:temp_steel
        }
        add_to_variable = {
            var   = CFR_chromium_usage
            value = temp_var:temp_chromium
        }
        subtract_from_temp_variable = {
            var   = total_civs
            value = temp_var:civs_in_bracket
        }
    }
    if = {
        limit = {
            check_variable = { total_civs > 75 }
        }
        set_temp_variable = {
            var   = civs_in_bracket
            value = temp_var:total_civs
        }
        subtract_from_temp_variable = {
            var   = civs_in_bracket
            value = 75
        }
        set_temp_variable = {
            var   = temp_steel
            value = 1.25
        }
        set_temp_variable = {
            var   = temp_chromium
            value = 1.5
        }
        multiply_temp_variable = {
            var   = temp_steel
            value = temp_var:civs_in_bracket
        }
        multiply_temp_variable = {
            var   = temp_chromium
            value = temp_var:civs_in_bracket
        }
        add_to_variable = {
            var   = CFR_steel_usage
            value = temp_var:temp_steel
        }
        add_to_variable = {
            var   = CFR_chromium_usage
            value = temp_var:temp_chromium
        }
        subtract_from_temp_variable = {
            var   = total_civs
            value = temp_var:civs_in_bracket
        }
    }
    if = {
        limit = {
            check_variable = { total_civs > 60 }
        }
        set_temp_variable = {
            var   = civs_in_bracket
            value = temp_var:total_civs
        }
        subtract_from_temp_variable = {
            var   = civs_in_bracket
            value = 60
        }
        set_temp_variable = {
            var   = temp_steel
            value = 1
        }
        set_temp_variable = {
            var   = temp_chromium
            value = 1
        }
        multiply_temp_variable = {
            var   = temp_steel
            value = temp_var:civs_in_bracket
        }
        multiply_temp_variable = {
            var   = temp_chromium
            value = temp_var:civs_in_bracket
        }
        add_to_variable = {
            var   = CFR_steel_usage
            value = temp_var:temp_steel
        }
        add_to_variable = {
            var   = CFR_chromium_usage
            value = temp_var:temp_chromium
        }
        subtract_from_temp_variable = {
            var   = total_civs
            value = temp_var:civs_in_bracket
        }
    }
    if = {
        limit = {
            check_variable = { total_civs > 45 }
        }
        set_temp_variable = {
            var   = civs_in_bracket
            value = temp_var:total_civs
        }
        subtract_from_temp_variable = {
            var   = civs_in_bracket
            value = 45
        }
        set_temp_variable = {
            var   = temp_steel
            value = 0.75
        }
        set_temp_variable = {
            var   = temp_chromium
            value = 0.50
        }
        multiply_temp_variable = {
            var   = temp_steel
            value = temp_var:civs_in_bracket
        }
        multiply_temp_variable = {
            var   = temp_chromium
            value = temp_var:civs_in_bracket
        }
        add_to_variable = {
            var   = CFR_steel_usage
            value = temp_var:temp_steel
        }
        add_to_variable = {
            var   = CFR_chromium_usage
            value = temp_var:temp_chromium
        }
        subtract_from_temp_variable = {
            var   = total_civs
            value = temp_var:civs_in_bracket
        }
    }
    if = {
        limit = {
            check_variable = { total_civs > 30 }
        }
        set_temp_variable = {
            var   = civs_in_bracket
            value = temp_var:total_civs
        }
        subtract_from_temp_variable = {
            var   = civs_in_bracket
            value = 30
        }
        set_temp_variable = {
            var   = temp_steel
            value = 0.5
        }
        set_temp_variable = {
            var   = temp_chromium
            value = 0.25
        }
        multiply_temp_variable = {
            var   = temp_steel
            value = temp_var:civs_in_bracket
        }
        multiply_temp_variable = {
            var   = temp_chromium
            value = temp_var:civs_in_bracket
        }
        add_to_variable = {
            var   = CFR_steel_usage
            value = temp_var:temp_steel
        }
        add_to_variable = {
            var   = CFR_chromium_usage
            value = temp_var:temp_chromium
        }
        subtract_from_temp_variable = {
            var   = total_civs
            value = temp_var:civs_in_bracket
        }
    }
    if = {
        limit = {
            check_variable = { total_civs > 5 }
        }
        set_temp_variable = {
            var   = civs_in_bracket
            value = temp_var:total_civs
        }
        subtract_from_temp_variable = {
            var   = civs_in_bracket
            value = 5
        }
        set_temp_variable = {
            var   = temp_steel
            value = 0.25
        }
        set_temp_variable = {
            var   = temp_chromium
            value = 0.10
        }
        multiply_temp_variable = {
            var   = temp_steel
            value = temp_var:civs_in_bracket
        }
        multiply_temp_variable = {
            var   = temp_chromium
            value = temp_var:civs_in_bracket
        }
        add_to_variable = {
            var   = CFR_steel_usage
            value = temp_var:temp_steel
        }
        add_to_variable = {
            var   = CFR_chromium_usage
            value = temp_var:temp_chromium
        }
        subtract_from_temp_variable = {
            var   = total_civs
            value = temp_var:civs_in_bracket
        }
    }
    
    # Add tech discounts (additive, as is Clausewitz tradition)
    set_temp_variable = {
        var   = discount
        value = 1
    }
    if = {
        limit = {
            has_tech = construction2
        }
        subtract_from_temp_variable = {
            var   = discount
            value = 0.05
        }
    }
    if = {
        limit = {
            has_tech = construction3
        }
        subtract_from_temp_variable = {
            var   = discount
            value = 0.05
        }
    }
    if = {
        limit = {
            has_tech = construction4
        }
        subtract_from_temp_variable = {
            var   = discount
            value = 0.05
        }
    }
    if = {
        limit = {
            has_tech = construction5
        }
        subtract_from_temp_variable = {
            var   = discount
            value = 0.05
        }
    }
    
    # Heavy vs light industry weighting
    # Light industry has 50% reduced resource demand
    if = {
        limit = {
            num_of_civilian_factories > 0
        }
        set_temp_variable = {
            var   = weight
            value = var:CFR_light_industry
        }
        multiply_temp_variable = {
            var   = weight
            value = 0.50
        }
        add_to_temp_variable = {
            var   = weight
            value = var:CFR_heavy_industry
        }
        divide_temp_variable = {
            var   = weight
            value = num_of_civilian_factories
        }
    }
    else = {
        set_temp_variable = {
            var   = weight
            value = 0
        }
    }
    
    # Apply discounts & weights
    multiply_temp_variable = {
        var   = discount
        value = temp_var:weight
    }
    multiply_variable = {
        var   = CFR_steel_usage
        value = temp_var:discount
    }
    multiply_variable = {
        var   = CFR_chromium_usage
        value = temp_var:discount
    }
    
    # Round off
    round_variable = CFR_steel_usage
    round_variable = CFR_chromium_usage
}
