﻿#commission_legend_artifact_decision
#clear_ground_for_legendary_building

more_legends_commission_legend_artifact_decision = {
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_smith.dds"
	}
	
	cooldown = { days = standard_commission_artifact_cooldown_time }
	sort_order = 100
	decision_group_type = major

	is_shown = {
		AND = {
			is_landed = yes
			highest_held_title_tier >= tier_county
			exists = capital_province
			has_dlc_feature = legends
			OR = {
				has_variable = finished_legend_var
				has_variable = finished_famed_legend_var
				has_variable = finished_illustrious_legend_var
				has_variable = finished_mythical_legend_var
			}
			OR = {
				has_variable = finished_terrorizing_legend_var
				has_variable = finished_cultural_legend_var
				has_variable = finished_enriching_legend_var
			}
		}

	}

	is_valid_showing_failures_only = {
		# Only valid for characters with an antiquaria OR chronicler
		custom_tooltip = {
			text = commision_legend_artifact_decision_court_positions_tt
			OR = {
				employs_court_position = antiquarian_court_position
				employs_court_position = chronicler_court_position
			}
		}
		custom_tooltip = {
			text = commision_legend_artifact_decision_disappearing_tt
			any_realm_province = {
				any_province_legend = {
					has_variable = my_legend_var
				}
			}
		}
	}

	cost = {
		gold = {
			if = {
				limit = {
					has_variable = finished_mythical_legend_var
				}
				value = 800
			}
			else_if = {
				limit = {
					has_variable = finished_illustrious_legend_var
				}
				value = 600
			}
			else = {
				value = 400
			}
			if = {
				limit = { # Estate discount bonus
					domicile ?= { has_domicile_parameter = estate_reduce_commission_artifact_cost }
				}
				multiply = estate_reduce_commission_artifact_cost_value
			}
		}
	}

	effect = {
		if = {
			limit = {
				any_court_position_holder = {
					type = chronicler_court_position
				}
			}
			random_court_position_holder = {
				type = chronicler_court_position
				save_scope_as = chronicler
			}
		}
		if = {
			limit = {
				any_court_position_holder = {
					type = antiquarian_court_position
				}
			}
			random_court_position_holder = {
				type = antiquarian_court_position
				save_scope_as = chronicler
			}
		}
		# Explanatory Tooltip
		custom_tooltip = commission_legend_artifact_decision_effect

		hidden_effect = {
			trigger_event = ce1_decision_events.0001
		}
	}

	ai_check_interval = 36

	ai_potential = {
		is_at_war = no
		ai_greed < medium_positive_ai_value
		short_term_gold > ai_war_chest_desired_gold_value
		war_chest_gold >= halved_ai_war_chest_gold_maximum
	}

	ai_will_do = {
		base = 100 #AI should always create the book
	}
}

more_legends_clear_ground_for_legendary_building = {
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_legend.dds"
	}
	decision_group_type = major
	sort_order = 100

	is_shown = {
		exists = var:more_legends_legendary_reward_location
		has_dlc_feature = legends
		is_landed = yes
	}

	is_valid_showing_failures_only = {
		custom_tooltip = {
			text = more_legends_clear_ground_for_legendary_building_no_barony_tt
			any_held_title = {
				tier = tier_barony
				title_province = { has_special_building_slot = no }
				is_capital_barony = yes
				holder = root
				is_leased_out = no
			}
		}
	}

	widget = {
		gui = "decision_view_widget_option_list_generic"
		controller = decision_option_list_controller
		decision_to_second_step_button = "CHOOSE_LEGENDARY_BUILDING"
		show_from_start = yes

		item = { # Tax center
			value = clear_ground_tax_center
			is_valid = {
				custom_tooltip = { # The legend you finished is valid for a legendary shrine
					text = ml_clear_ground_tax_center_tt
					has_character_flag = tax_center_flag
				}
			}
			current_description = clear_ground_tax_center_desc
			localization = clear_ground_tax_center
			icon = "gfx/interface/icons/building_types/icon_building_tax_assessor.dds"
			flat = yes
			ai_chance = { value = 10 }
		}
		item = { # Haunted Palace
			value = clear_ground_haunted_palace
			is_valid = {
				custom_tooltip = { # The legend you finished is valid for a haunted palace
					text = ml_clear_ground_haunted_palace_tt
					has_character_flag = haunted_palace_flag
				}
			}
			current_description = clear_ground_haunted_palace_desc
			localization = clear_ground_haunted_palace
			icon = "gfx/interface/icons/building_types/icon_building_legendary_palace.dds"
			flat = yes
			ai_chance = { value = 10 }
		}
		item = { # Grand library
			value = clear_ground_grand_library
			is_valid = {
				custom_tooltip = { # The legend you finished is valid for a grand library
					text = ml_clear_ground_grand_library_tt
					has_character_flag = grand_library_flag
				}
			}
			current_description = clear_ground_grand_library_desc
			localization = clear_ground_grand_library
			icon = "gfx/interface/icons/building_types/icon_building_university.dds"
			flat = yes
			ai_chance = { value = 10 }
		}
		item = { # Watchtower
			value = clear_ground_legendary_watchtower
			is_valid = {
				custom_tooltip = { # The legend you finished is valid for a legendary watchtower
					text = clear_ground_legendary_watchtower_tt
					has_character_flag = legendary_watchtower_flag
				}
			}
			current_description = clear_ground_legendary_watchtower_desc
			localization = clear_ground_legendary_watchtower
			icon = "gfx/interface/icons/building_types/icon_building_legendary_watchtower.dds"
			flat = yes
			ai_chance = { value = 10 }
		}
		item = { # Hunting lodge
			value = clear_ground_legendary_hunting_lodge
			is_valid = {
				custom_tooltip = { # The legend you finished is valid for a legendary hunting_lodge
					text = clear_ground_legendary_hunting_lodge_tt
					has_character_flag = legendary_hunting_lodge_flag
				}
			}
			current_description = clear_ground_legendary_hunting_lodge_desc
			localization = clear_ground_legendary_hunting_lodge
			icon = "gfx/interface/icons/building_types/icon_building_legendary_hunting_grounds.dds"
			flat = yes
			ai_chance = { value = 10 }
		}
	}

	effect = {
		custom_tooltip = more_legends_clear_ground_for_legendary_building_custom_tt
        switch = {
            trigger = yes
            scope:clear_ground_haunted_palace = {
                set_variable = {
                    name = clear_ground
                    value = flag:haunted_palace
                }
            }
            scope:clear_ground_grand_library = {
                set_variable = {
                    name = clear_ground
                    value = flag:grand_library
                }
            }
            scope:clear_ground_tax_center = {
                set_variable = {
                    name = clear_ground
                    value = flag:tax_center
                }
            }
            scope:clear_ground_legendary_watchtower = {
                set_variable = {
                    name = clear_ground
                    value = flag:legendary_watchtower
                }
            }
            scope:clear_ground_legendary_hunting_lodge = {
                set_variable = {
                    name = clear_ground
                    value = flag:legendary_hunting_lodge
                }
            }
        }
        open_view_data = {
            view = decision_detail
            data = decision:more_legends_pick_legendary_barony_decision
            player = root
        }
    }
    ai_check_interval = 36
    ai_potential = {
        is_at_war = no
        ai_greed < medium_positive_ai_value
        short_term_gold > ai_war_chest_desired_gold_value
        war_chest_gold >= halved_ai_war_chest_gold_maximum
    }
    ai_will_do = {
        base = 100 #AI should always reap rewards
    }
}

more_legends_pick_legendary_barony_decision = {
    picture = {
        reference = "gfx/interface/illustrations/decisions/decision_legend.dds"
    }
	decision_group_type = major
    ai_goal = yes
    is_invisible = yes
    widget = {
        controller = create_holy_order
        barony_valid = {
            trigger_if = {
                limit = { exists = this }
				title_province = { has_special_building_slot = no }
				is_capital_barony = yes
				holder = scope:ruler
				is_leased_out = no
            }
        }
    }
    is_valid_showing_failures_only = { # Error suppressing
		has_variable = clear_ground
    }
    cost = {
    }
    effect = {
        send_interface_toast = {
            type = event_toast_effect_good
            title = more_legends_clear_ground_for_legendary_building.toast
            switch = {
                trigger = root.var:clear_ground
                flag:haunted_palace = {
                    scope:barony.title_province ?= {
                        add_special_building_slot = haunted_palace
                    }
                }
                flag:grand_library = {
                    scope:barony.title_province ?= {
                        add_special_building_slot = grand_library
                    }
                }
                flag:tax_center = {
                    scope:barony.title_province ?= {
                        add_special_building_slot = tax_center
                    }
                }
                flag:legendary_watchtower = {
                    scope:barony.title_province ?= {
                        add_special_building_slot = legendary_watchtower
                    }
                }
                flag:legendary_hunting_lodge = {
                    scope:barony.title_province ?= {
                        add_special_building_slot = legendary_hunting_lodge
                    }
                }
            }
        }
        hidden_effect = {
            remove_variable = clear_ground
            # Shift the legend protagonist to the province
            if = {
                limit = {
                    exists = var:legendary_figure
                }
                scope:barony.title_province ?= {
                    set_variable = {
                        name = legendary_figure
                        value =  root.var:legendary_figure
                    }
                }
                remove_variable = legendary_figure
            }
            # Cleanup
            if = {
                limit = {
                    has_character_flag = grand_library_flag
                }
                remove_character_flag = grand_library_flag
            }
            if = {
                limit = {
                    has_character_flag = tax_center_flag
                }
                remove_character_flag = tax_center_flag
            }
            if = {
                limit = {
                    has_character_flag = haunted_palace_flag
                }
                remove_character_flag = haunted_palace_flag
            }
            if = {
                limit = {
                    has_character_flag = legendary_watchtower_flag
                }
                remove_character_flag = legendary_watchtower_flag
            }
            if = {
                limit = {
                    has_character_flag = legendary_hunting_lodge_flag
                }
                remove_character_flag = legendary_hunting_lodge_flag
            }
            if = {
                limit = {
                    exists = var:more_legends_legendary_reward_location
                }
                remove_variable = more_legends_legendary_reward_location
            }
        }
    }
    
    ai_potential = {
        is_playable_character = yes
    }
    ai_will_do = {
        base = 200
    }
}

