﻿qing_htool_AI_slot_add_decision = {
    ai_check_interval = 3
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_misc.dds"
	}
    title = "AI增加建筑槽"
	desc = "AI增加建筑槽"

	#selection_tooltip = abandon_celibacy_decision_tooltip


	is_shown = {
		is_ai = yes
        has_game_rule = qing_htool_ai_main_switch_building_slot_on
	}

    is_valid = {
        is_ai = yes
    }

	effect = {
        random_directly_owned_province = {
            limit = {
                has_ongoing_construction = no
                has_free_building_slot = no
                NOR = {
                    has_holding_type = nomad_holding
                    has_holding_type = herder_holding
                }
                trigger_if = {
                    limit = { qing_htool_ai_building_slot_add_cooldown_value > 0 }
                    NOT = { has_province_modifier = qing_htool_building_slot_add_cooldown_modifier }
                }
                trigger_if = {
                    limit = { qing_htool_ai_building_slot_add_max_value > 0 }
                    NOT = { var:qing_htool_building_slot_information_count >= qing_htool_ai_building_slot_add_max_value }
                }
            }
            save_scope_as = gui_holding
        }
        
        scope:gui_holding ?= {
            if = {
                limit = {
                    NOT = { has_variable = qing_htool_building_slot_information_count }
                }
                set_variable = {
                    name = qing_htool_building_slot_information_count
                    value = 0
                }
            }
            if = {
                limit = {
                    trigger_if = {
                        limit = { qing_htool_ai_building_slot_cost_total > 0 }
                        trigger_if = {
                            limit = { has_game_rule = qing_htool_main_pay_method_gold }
                            root.gold >= qing_htool_ai_building_slot_cost_total
                        }
                        trigger_else_if = {
                            limit = { has_game_rule = qing_htool_main_pay_method_treasury }
                            trigger_if = {
                                limit = { root = { has_treasury = yes } }
                                root.treasury >= qing_htool_ai_building_slot_cost_total
                            }
                            trigger_else = {
                                root.gold >= qing_htool_ai_building_slot_cost_total
                            }
                        }
                        trigger_else_if = {
                            limit = { has_game_rule = qing_htool_main_pay_method_gold_first }
                            OR = {
                                root.gold >= qing_htool_ai_building_slot_cost_total
                                AND = {
                                    root = { has_treasury = yes }
                                    root.treasury >= qing_htool_ai_building_slot_cost_total
                                }
                            }
                        }
                        trigger_else_if = {
                            limit = { has_game_rule = qing_htool_main_pay_method_treasury_first }
                            OR = {
                                AND = {
                                    root = { has_treasury = yes }
                                    root.treasury >= qing_htool_ai_building_slot_cost_total
                                }
                                root.gold >= qing_htool_ai_building_slot_cost_total
                            }
                        }
                    }
                }
                # 冷却修正
                if = {
                    limit = { qing_htool_ai_building_slot_add_cooldown_value > 0 }
                    add_province_modifier = {
                        modifier = qing_htool_building_slot_add_cooldown_modifier
                        months = qing_htool_ai_building_slot_add_cooldown_value
                    }
                }
                # 新增建筑槽
                add_province_modifier = qing_htool_building_slot_add_modifier
                change_variable = {
                    name = qing_htool_building_slot_information_count
                    add = 1
                }
                # 花费
                if = {
                    limit = { qing_htool_ai_building_slot_cost_total > 0 }
                    if = {
                        limit = { has_game_rule = qing_htool_main_pay_method_gold }
                        root = { remove_long_term_gold = qing_htool_ai_building_slot_cost_total }
                    }
                    else_if = {
                        limit = { has_game_rule = qing_htool_main_pay_method_treasury }
                        if = {
                            limit = { root = { has_treasury = yes } }
                            root = { remove_treasury = qing_htool_ai_building_slot_cost_total }
                        }
                        else = {
                            root = { remove_long_term_gold = qing_htool_ai_building_slot_cost_total }
                        }
                    }
                    else_if = {
                        limit = { has_game_rule = qing_htool_main_pay_method_gold_first }
                        if = {
                            limit = { root.gold >= qing_htool_ai_building_slot_cost_total }
                            root = { remove_long_term_gold = qing_htool_ai_building_slot_cost_total }
                        }
                        else_if = {
                            limit = {
                                root = { has_treasury = yes }
                                root.treasury >= qing_htool_ai_building_slot_cost_total
                            }
                            root = { remove_treasury = qing_htool_ai_building_slot_cost_total }
                        }
                        else = {
                            root = { remove_long_term_gold = qing_htool_ai_building_slot_cost_total }
                        }
                    }
                    else_if = {
                        limit = { has_game_rule = qing_htool_main_pay_method_treasury_first }
                        if = {
                            limit = {
                                root = { has_treasury = yes }
                                root.treasury >= qing_htool_ai_building_slot_cost_total
                            }
                            root = { remove_treasury = qing_htool_ai_building_slot_cost_total }
                        }
                        else = {
                            root = { remove_long_term_gold = qing_htool_ai_building_slot_cost_total }
                        }
                    }
                }
            }
        }
	}
	
	
	ai_will_do = {
		base = 60
	}
}
