﻿#EK NOTE: new traditions affecting provinces, edits due to new EK terrain types

# root: The province in which the building was completed
# scope:character: The character that paid for the construction, if available
on_building_completed = {
	effect = {
		#save the county holder as the character in case the construction wasn't started through GUI or by the AI 
		if = {
			limit = {
				NOT = { exists = scope:character }
			}
			root.county.holder = {
				save_scope_as = character
			}
		}
		root = {
			#EK ADDITION: added flag for playing tall inspiration
			scope:character = {
				if = {
					limit = { is_alive = yes }
					add_character_flag = {
						flag = recent_building_built
						years = 5
					}
				}
			}

            #EK EDIT: Land dredge tradition
            if = {
                limit = {
                    culture = {
                        has_cultural_parameter = development_gain_on_building_complete_in_swamp
                    }
                    OR = {
                        terrain = black_marsh
                        terrain = wetlands
                    }
                }
                county = {
					change_development_progress_with_overflow = 25
					set_variable = {
						name = dev_gain_cooldown
						value = yes
						days = 365
					}
				}
            }
            #EK ADDITION END
            
			#Hard working Tradition
			if = {
				limit = {
					culture = { has_cultural_parameter = development_gain_on_building_complete }
					NOT = { county = { has_variable = dev_gain_cooldown } }
				}
				county = {
					change_development_progress_with_overflow = 25
					set_variable = {
						name = dev_gain_cooldown
						value = yes
						days = 365
					}
				}
			}

			# Metal crafting Tradition
			if = {
				limit = {
					culture = { has_cultural_parameter = prestige_gain_on_building_complete_in_mountains }
					OR = {
						terrain = mountains
						terrain = desert_mountains
					}
				}
				scope:character = {
					add_prestige = medium_prestige_value
				}
			}
			
			# Sacred Mountains Tradition
			if = {
				limit = {
					scope:character.culture = { has_cultural_parameter = piety_gain_on_building_complete_in_mountains }
					OR = {
						terrain = mountains
						terrain = desert_mountains
					}
				}
				scope:character = {
					add_piety = minor_piety_value
				}
			}
			
			# Sacred Groves Tradition
			if = {
				limit = {
					scope:character.culture = { has_cultural_parameter = piety_gain_on_building_complete_in_forest_taiga }
					OR = {
						terrain = forest
						terrain = taiga
						terrain = jungle
					}
				}
				scope:character = {
					add_piety = minor_piety_value
				}
			}
			
			#EK ADDITION
			#Sacred Marshes Tradition #EK EDIT: new tradition
			if = {
				limit = {
					scope:character.culture = {
						has_cultural_parameter = piety_gain_on_building_complete_in_swamp
					}
					OR = {
						terrain = black_marsh
						terrain = wetlands
					}
				}
				county.holder = {
					add_piety = minor_piety_value
				}
			}
			#EK ADDITION END
			
			# FP2 Dynasty perk
			if = {
				limit = {
					has_holding_type = city_holding
					exists = scope:character.dynasty
					scope:character.dynasty = {
						has_dynasty_perk = fp2_urbanism_legacy_4
					}
				}
				county = {
					holder = {
						send_interface_toast = {
							type = event_toast_effect_good
							title = prestige_gained_from_building_completion
							left_icon = root.barony
							add_prestige = medium_prestige_gain
						}
					}
					#### Chance to add super guest!
					# Set variables
					random = {
						chance = 10
						modifier = {
							add = county.development_level
						}
						set_variable = {
							name = number_prestigious_new_guests
							value = 1
						}
						if = {
							limit = { county.development_level >= max_development_level  }

							set_variable = {
								name = holding_quality
								value = 3
							}
						}
						else_if = {
							limit = { county.development_level >= great_development_level  }
							set_variable = {
								name = holding_quality
								value = 3
							}
						}
						else_if = {
							limit = { county.development_level >= good_development_level  }
							set_variable = {
								name = holding_quality
								value = 2
							}
						}
						else_if = {
							limit = { county.development_level >= medium_development_level  }
							set_variable = {
								name = holding_quality
								value = 2
							}
						}
						else_if = {
							limit = { county.development_level >= bad_development_level  }
							set_variable = {
								name = holding_quality
								value = 1
							}
						}
						else = {
							set_variable = {
								name = holding_quality
								value = 1
							}
						}
					}

					holder = {
						# Trigger the hidden event
						trigger_event = fp2_other_decisions.0001
					}
				}
			}

			# Struggle parameters
			if = {
				limit = {
					has_holding_type = castle_holding
					scope:character = {
						any_character_struggle = {
							involvement = involved
							has_struggle_phase_parameter = completing_building_in_castle_gives_development
						}
					}
				}
				county = { change_development_progress_with_overflow = 10 }
			}

			if = {
				limit = {
					has_holding_type = church_holding
					scope:character = {
						any_character_struggle = {
							involvement = involved
							has_struggle_phase_parameter = completing_building_in_temple_gives_piety
						}
					}
				}
				scope:character = { add_piety = medium_piety_gain }
			}

			# Struggle catalysts
			if = {
				limit = {
					has_holding_type = castle_holding
					scope:character = {
						any_character_struggle = {
							involvement = involved
							phase_has_catalyst = catalyst_new_building_in_castle
						}
					}
				}
				scope:character = {
					every_character_struggle = {
						involvement = involved
						activate_struggle_catalyst = {
							catalyst = catalyst_new_building_in_castle
							character = scope:character
						}
					}
				}
			}
			else_if = {
				limit = {
					has_holding_type = city_holding
					scope:character = {
						any_character_struggle = {
							involvement = involved
							phase_has_catalyst = catalyst_new_building_in_city
						}
					}
				}
				scope:character = {
					every_character_struggle = {
						involvement = involved
						activate_struggle_catalyst = {
							catalyst = catalyst_new_building_in_city
							character = scope:character
						}
					}
				}
			}
			else_if = {
				limit = {
					has_holding_type = church_holding
					scope:character = {
						any_character_struggle = {
							involvement = involved
							phase_has_catalyst = catalyst_new_building_in_temple
						}
					}
				}
				scope:character = {
					every_character_struggle = {
						involvement = involved
						activate_struggle_catalyst = {
							catalyst = catalyst_new_building_in_temple
							character = scope:character
						}
					}
				}
			}
			#EK DISABLED
			#if = {
			#	limit = {
			#		has_holding_type = church_holding
			#		county.faith.religion = religion:islam_religion
			#		county.holder = {
			#			any_character_struggle = {
			#				involvement = involved
			#				phase_has_catalyst = catalyst_new_islamic_temple_construction
			#			}
			#		}
			#	}
			#	county.holder = {
			#		every_character_struggle = {
			#			involvement = involved
			#			activate_struggle_catalyst = {
			#				catalyst = catalyst_new_islamic_temple_construction
			#				character = root.county.holder
			#			}
			#		}
			#	}
			#}
			#
			if = {
				limit = {
					scope:character = {
						any_character_struggle = {
							involvement = involved
							phase_has_catalyst = catalyst_new_building_in_holdings
						}
					}
				}
				scope:character = {
					every_character_struggle = {
						involvement = involved
						activate_struggle_catalyst = {
							catalyst = catalyst_new_building_in_holdings
							character = scope:character
						}
					}
				}
			}
			
			# Points of Interest
			# Apply them here directly so they are more promptly added than monthly via the POI update
			if = {
				limit = {
					has_special_building = no
				}
			}
			else_if = {
				limit = {
					OR = {
						has_building_with_flag = { flag = travel_point_of_interest_martial }
						has_building_with_flag = { flag = travel_point_of_interest_martial_minor }
					}
					NOT = { has_travel_point_of_interest = poi_special_buildings_martial }
				}
				add_travel_point_of_interest = poi_special_buildings_martial
			}
			else_if = {
				limit = {
					has_building_with_flag = { flag = travel_point_of_interest_learning }
					NOT = { has_travel_point_of_interest = poi_special_buildings_learning }
				}
				add_travel_point_of_interest = poi_special_buildings_learning
			}
			else_if = {
				limit = {
					has_building_with_flag = { flag = travel_point_of_interest_religious }
					NOT = { has_travel_point_of_interest = poi_special_buildings_religious }
				}
				add_travel_point_of_interest = poi_special_buildings_religious
			}
			else_if = {
				limit = {
					has_building_with_flag = { flag = travel_point_of_interest_diplomatic }
					NOT = { has_travel_point_of_interest = poi_special_buildings_diplomatic }
				}
				add_travel_point_of_interest = poi_special_buildings_diplomatic
			}
			else_if = {
				limit = {
					has_building_with_flag = { flag = travel_point_of_interest_wonder }
					NOT = { has_travel_point_of_interest = poi_special_buildings_wonder }
				}
				add_travel_point_of_interest = poi_special_buildings_wonder
			}
			else_if = {
				limit = {
					has_building_with_flag = { flag = travel_point_of_interest_economic }
					NOT = { has_travel_point_of_interest = poi_special_buildings_economic }
				}
				add_travel_point_of_interest = poi_special_buildings_economic
			}
			# EK ADDITION: added intrigue, magic & dark arts POIs
			else_if = {
				limit = {
					has_building_with_flag = { flag = travel_point_of_interest_intrigue }
					NOT = { has_travel_point_of_interest = poi_special_buildings_intrigue }
				}
				add_travel_point_of_interest = poi_special_buildings_intrigue
			}
			else_if = {
				limit = {
					has_building_with_flag = { flag = travel_point_of_interest_magic }
					NOT = { has_travel_point_of_interest = poi_special_buildings_magic }
				}
				add_travel_point_of_interest = poi_special_buildings_magic
			}
			else_if = {
				limit = {
					has_building_with_flag = { flag = travel_point_of_interest_dark_arts }
					NOT = { has_travel_point_of_interest = poi_special_buildings_dark_arts }
				}
				add_travel_point_of_interest = poi_special_buildings_dark_arts
			}
			# EK ADDITION END

			# The Builder event chain
			if = {
				limit = { 
					has_variable = the_builder_holding
					NOT = { has_variable = the_builder_finished }
				}
				var:the_builder_holding = { save_scope_as = holding_initiator }
				#Did the Builder actually finish the holding, or was it cancelled?
				scope:holding_initiator = {
					if = {
						limit = {
							is_alive = yes
							is_landed_or_landless_administrative = yes
							is_available = yes
						}
						#Then remove the safeguard event that would clean up the Builder
						if = {
							limit = { has_variable = the_builder_holding_cancelled_safeguard }
							remove_variable = the_builder_holding_cancelled_safeguard
						}
						#And trigger reward event
						trigger_event = bp2_yearly.1071
					}
				}
				set_variable = { name = the_builder_finished }
			}
			# Every time the Builder builds in their holding...
			if = {
				limit = {
					scope:character ?= { 
						OR = {
							has_character_modifier = the_city_builder_modifier
							has_character_modifier = the_castle_builder_modifier
							has_character_modifier = the_temple_builder_modifier
						}
					}
				}
				county = { change_development_progress = minor_development_progress_gain }
				if = {
					limit = {
						scope:character ?= { has_character_modifier = the_city_builder_modifier }
					}
					scope:character = { add_gold = minor_gold_value }
				}
				if = {
					limit = {
						scope:character ?= { has_character_modifier = the_castle_builder_modifier }
					}
					scope:character = { add_prestige = minor_prestige_gain }
				}
				if = {
					limit = {
						scope:character ?= { has_character_modifier = the_temple_builder_modifier }
					}
					scope:character = { add_piety = minor_piety_gain }
				}
			}
			#end any landless adventurer's construction task contract
			if = {
				limit = {
					any_player = {
						any_character_active_contract = {
							has_task_contract_type = laamp_construction_contract
							task_contract_employer = scope:character
							save_temporary_scope_as = construction_contract
						}
					}
				}
				scope:construction_contract = {
					if = {
						limit = {
							task_contract_taker.capital_province.county = root.county
						}
						complete_task_contract = success_critical
					}
					else = {
						complete_task_contract = failure_standard
					}
				}
			}

			# Inspection - Has Holding Modifier 
			if = {
				limit = {
					has_province_modifier = inspection_empty_lands_domain_modifier
				}
				remove_province_modifier = inspection_empty_lands_domain_modifier
				add_random_economic_building_effect = yes
			}
			else_if = {
				limit = {
					has_province_modifier = inspection_empty_lands_border_modifier
				}
				remove_province_modifier = inspection_empty_lands_border_modifier
				add_random_fortification_building_effect = yes
			}
			
			# Admin - Governors get rewards for constructing buildings in governed counties
			if = {
				limit = {
					scope:character = { is_governor = yes }
				}
				scope:character = {
					send_interface_message = {
						type = msg_governor_duties
						title = msg_governor_duties_t
						desc = admin_construction_completed_desc
						right_icon = root.barony
						
						change_influence = estate_construction_influence_gain_value
						increase_governance_effect = { VALUE = 4 }
					}
				}
			}
			#EK DISABLED: no Harrying
			# Harrying of the North - Pacification Gain
			#if = {
			#	limit = {
			#		scope:character.culture = culture:norman
			#		has_global_variable = harrying_of_the_north
			#		exists = global_var:harrying_of_the_north.story_owner.var:pacification
			#	}
			#	global_var:harrying_of_the_north.story_owner = {
			#		change_variable = {
			#			name = pacification
			#			add = 2
			#		}
			#	}
			#}
			
			# Brewery counter
			brewery_counter_effect = yes
		}
	}
}

# root: The province in which the building was started
# scope:character: The character that paid for the construction, if available
on_building_started = {
	effect = {
		#save the county holder as the character in case the construction wasn't started through GUI or by the AI 
		if = {
			limit = {
				NOT = { exists = scope:character }
			}
			root.county.holder = {
				save_scope_as = character
			}
		}
		if = {
			limit = {
				OR = {
	  				has_construction_with_flag = temple
					has_construction_with_flag = castle
					has_construction_with_flag = city
	  			}
				any_player = {
					is_landless_adventurer = yes
					in_diplomatic_range = scope:character					
					can_create_task_contract = {
						type_name = laamp_construction_contract
						employer = scope:character						
					}
					save_temporary_scope_as = player_laamp
				}
			}
			scope:player_laamp = {
				create_task_contract = {
					task_contract_employer = scope:character
					task_contract_type = laamp_construction_contract
					task_contract_tier = scope:character.task_contract_tier_value
					location = scope:character.capital_province
					save_scope_as = new_contract
				}
				scope:new_contract = {
					set_variable = {
						name = task_contract_employer
						value = scope:character
					}
					set_variable = {
						name = task_contract_destination
						value = root
					}
				}
			}
		}
		if = {
			limit = {
				has_province_modifier = travel_extra_building_resources_modifier
			}
			if = {
				limit = {
					has_variable = extra_building_resources 
					var:extra_building_resources > 1
				}
				change_variable = {
					name = extra_building_resources
					subtract = 1
				}
				remove_province_modifier = travel_extra_building_resources_modifier
				add_province_modifier = travel_extra_building_resources_modifier
			}
			else = {
				remove_province_modifier = travel_extra_building_resources_modifier
				remove_variable = extra_building_resources
			}
		}
	}
}

# root: The province in which the building was cancelled
# scope:character: The character that paid for the construction, if available
on_building_cancelled = {
	effect = {}
}

# root: The holder of the province doing the razing
# province: The province in which the holding exists
on_holding_razed = {
	effect = {
		scope:province.county = { set_variable = was_razed }
		send_interface_toast = {
			title = province_razed_toast_title
			left_icon = scope:province.barony
			add_gold = {
				value = minor_gold_value
				add = scope:province.county.development_level
			}
			scope:province = {
				if = {
					limit = {
						this = county.title_province
						county.development_level >= 1
					}
					county = {
						change_development_progress_with_overflow = {
							value = scope:province.county.development_level
							multiply = 10
							multiply = -1
						}
					}
				}
				if = {
					limit = {
						this = county.title_province
						county = {
							uses_county_fertility = yes
						}
					}
					county = {
						change_county_fertility = medium_county_fertility_level_gain
					}
				}
				if = {
					limit = {
						has_university_building_trigger = yes
					}
					if = {
						limit = {
							has_building_or_higher = generic_university
						}
						remove_building = generic_university
					}
					#else_if = {
					#	limit = {
					#		has_building_or_higher = al_azhar_university
					#	}
					#	remove_building = al_azhar_university
					#}
					#else_if = {
					#	limit = {
					#		has_building_or_higher = sankore_university
					#	}
					#	remove_building = sankore_university
					#}
					#else_if = {
					#	limit = {
					#		has_building_or_higher = siena_university
					#	}
					#	remove_building = siena_university
					#}
					#else_if = {
					#	limit = {
					#		has_building_or_higher = nalanda_university
					#	}
					#	remove_building = nalanda_university
					#}
					#else_if = {
					#	limit = {
					#		has_building_or_higher = house_of_wisdom_01
					#	}
					#	remove_building = house_of_wisdom_01
					#}
					#else_if = {
					#	limit = {
					#		has_building_or_higher = al_qarawiyyin_university_01
					#	}
					#	remove_building = al_qarawiyyin_university_01
					#}
					#else_if = {
					#	limit = {
					#		has_building_or_higher = somapura_university_01
					#	}
					#	remove_building = somapura_university_01
					#}
					#else_if = {
					#	limit = {
					#		has_building_or_higher = cluny_abbey_01
					#	}
					#	remove_building = cluny_abbey_01
					#}
				}
			}
		}
	}
}
