﻿# 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 = {
			# 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 = {
						agot_is_mountains_terrain = yes
						agot_is_desert_mountains_terrain = yes
					}
				}
				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 = {
						agot_is_mountains_terrain = yes
						agot_is_desert_mountains_terrain = yes
					}
				}
				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 = {
						agot_is_forest_terrain = yes
						agot_is_taiga_terrain = yes
						agot_is_jungle_terrain = yes
					}
				}
				scope:character = {
					add_piety = minor_piety_value
				}
			}

			# 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
						}
					}
				}
			}

			if = {
				limit = {
					has_holding_type = church_holding
					county.faith.religion = religion:islam_religion
					scope:character = {
						any_character_struggle = {
							involvement = involved
							phase_has_catalyst = catalyst_new_islamic_temple_construction
						}
					}
				}
				scope:character = {
					every_character_struggle = {
						involvement = involved
						activate_struggle_catalyst = {
							catalyst = catalyst_new_islamic_temple_construction
							character = scope:character
						}
					}
				}
			}

			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
			}

			# 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
					}
				}
			}
			
			# 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 }
					}
				}
			}

			# Harrying of the North - Pacification Gain
			if = {
				limit = {
					scope:character.culture = culture:norman
					has_global_variable = harrying_of_the_north
				}
				global_var:harrying_of_the_north.story_owner = {
					change_variable = {
						name = pacification
						add = 2
					}
				}
			}			
		}
	}
}

# 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
					}
				}
			}
		}
	}
}

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