﻿##SkelSa's DYNAMIC PEACE SYSTEM.
##PLAYER IS THE WINNER

add_namespace = swp_winning

##tag = PLAYER
##this event determines the player's enemy
##All ways lead to swp_winning.0
country_event = {

    id = swp_winning.0  
	title = "Debug event for whitepeace.0"
	desc = "The creator of this mod forgot something! Here the mod checks what kind of enemy you are against."
	immediate = { log = "[GetDateText]: [Root.GetName]: swp_winning.0 -> Debug Event" }
	
	hidden = yes
	is_triggered_only = yes

	
	option = {
	name = "The events happen here!"
	

		every_country = {

			#NO Faction 
			if = {				
				limit = {

					is_ai = yes
					has_country_flag = dw_ai_war
					is_in_faction = no

					
					##manpower and economy laws
					if = {
						limit = {
							NOT = { has_global_flag = swp_easier_decisions_settings }
						}
					
						has_country_flag = swp_laws							
					}
				
					##player is winning; AI has an enemy which is the player and has the flag 
					any_enemy_country = {
						is_ai = no
						has_country_flag = dw_player_war
						
					}
				
				}
				
			country_event = { id = swp_winning.1 } 
				
			}

				
			##Faction member
			if = {			
				limit = {
					
					is_ai = yes
					is_in_faction = yes
					is_faction_leader = yes
					has_country_flag = dw_ai_war
		
					#tag = AI, player is winning!
					any_enemy_country = {
						is_ai = no
						has_country_flag = dw_player_war
					}

					##Either FL or FM has stricter laws
					if = {
						limit = {
							NOT = { has_global_flag = swp_easier_decisions_settings }
						}
					
						OR = {

							has_country_flag = swp_laws

							any_allied_country = {									
								has_country_flag = swp_laws
								has_country_flag = dw_ai_war
							}
					
						}					
					}
					
					
				}
					
				country_event = { id = swp_winning.2 }
	
			}	
		}	
	}
}

#################CHECKS ARE OVER############################################

#################EVENTS TRIGGERED ONLY BY SLOT USE#########################


##tag = AI COUNTRY; NO FACTION
country_event = {

    id = swp_winning.1                          
    title =  swp_dynamic.1.t
    desc = swp_dynamic.1.d              
	picture = GFX_report_event_soviet_tanks_snow   						
	immediate = { log = "[GetDateText]: [Root.GetName]: swp_winning.1" }
	
	##CHECK EVENT
	is_triggered_only = yes


	##Send proposal to PLAYER 
	option = {
	name = swp_dynamic.1.a 
	ai_chance = { factor = 100 }

		every_country = {
		
			limit = {
				has_war_with = ROOT
				is_ai = no
				is_subject = no
			}
		
		country_event = { days = 1 id = swp_winning.4}
		
		}
	}
	
	##AI COUNTRY declines this generous offer!
	option = {
	name = swp_dynamic.1.b

		set_country_flag = peace_declined
		
		hidden_effect = {
			country_event = { days = 200 id = dw_flags.7 }	
		}
		
	}	
}


##TAG = FACTION LEADER (AI); is asked directly
##MEMBERS are asked later
country_event = {

    id = swp_winning.2                          
    title = swp_dynamic.2.t                  
    desc = swp_dynamic.2.d                 
	picture = GFX_report_event_soviet_tanks_snow 
	immediate = { log = "[GetDateText]: [Root.GetName]: swp_winning.2" }

	##CHECK EVENTS
	is_triggered_only = yes
	
	immediate = {
		#add_named_threat = { threat = 0.5 name = "Faction Leader resigns" } 
	}
	
	
	#Send peace proposal to PLAYER
	option = { 								
	name = swp_dynamic.2.a
	ai_chance = { factor = 100 }
	
		every_country = {					##This section might give problems in multiplayer!
		
			limit = {
				has_war_with = ROOT
				is_ai = no
				#is_subject = no
			}
		
		country_event = { days = 1 id = swp_winning.5}

		}

	}
	
	
	#Fight shall continue, abort the event chain
	option = { 	
	name = swp_dynamic.2.b
	ai_chance = { factor = 0 }
	
		set_country_flag = peace_declined
		
		hidden_effect = {
			country_event = { days = 200 id = dw_flags.7 }	
		}
	
	}
	
}


###########################################
##TRIGGER EVENTS OVER######################
##########################################


##########################################
##CHAIN GOES ON############################
##########################################


##Comes from Loser without faction
##tag = PLAYER, is notified about the proposal; usually first event player sees
country_event = {

    id = swp_winning.4                         
    title = swp_test.1.t                 
    desc = swp_test.1.d                    
	picture = GFX_report_event_soviet_tanks_snow  
	immediate = { log = "[GetDateText]: [Root.GetName]: swp_winning.4" }

	is_triggered_only = yes 
	
	
	#Peace treaty will happen
	option = { 
		name = swp_test.1.a 
		ai_chance = {factor = 100}
		
		##Add Opinion Modifier
		reverse_add_opinion_modifier = { target = FROM modifier = swp_signed_peace_treaty }
		
		##Every winner nation gets swp_winner		
		every_country = {								##This section might give problems in multi-player!
			limit = {
			
				has_war_with = FROM
				OR = {
					is_in_faction_with = ROOT
					has_guaranteed = ROOT
					is_subject_of = ROOT
					tag = ROOT
					
				}
			
			}
			
			set_country_flag = swp_winner
		}	
		
		
		##Every loser nation gets swp_loser; FROM is loser
		every_country = {
			limit = {
			
				has_war_with = ROOT
					any_state = {
						is_owned_by = PREV
						any_country = {
							OR = {
								is_in_faction_with = ROOT
								has_guaranteed = ROOT
								is_subject_of = ROOT
								tag = ROOT
							}
							controls_state = PREV
						}
						
					}
					
				OR = {
					is_in_faction_with = FROM
					has_guaranteed = FROM
					is_subject_of = FROM
					tag = FROM					
				}
			}
			
			set_country_flag = swp_loser
	
		}

		FROM = {		
			country_event = { days = 1 id = swp_final.1}			#First instance of final.1	
		}
		
		every_country = {
		
			if = {
				limit = {
					has_country_flag = dw_ai_war
					OR = {
						has_guaranteed = FROM
						is_guaranteed_by = FROM
					}
					
				}
				
			set_country_flag = swp_loser			
			country_event = { days = 1 id = swp_final.1}          #Second instance of final.1
			}
		}
				
		set_global_flag = peace_in_process

	}
			
		
	#Fuck you AI, I will crush you!
	option = { 	
		name = swp_test.1.b
		ai_chance = {factor = 0}
	
	    #Send notification event to AI
		FROM = {	
			country_event = { days = 2 id = swp_final.2 }	
		}
		
		set_country_flag = peace_declined
		
		hidden_effect = {
			country_event = { days = 200 id = dw_flags.7 }	
		}			

		
	}		
}



##Comes from enemy FL 
##tag = PLAYER, reacts to peace proposal
country_event = {

    id = swp_winning.5                       
    title = swp_winning.5.t                  
    desc = swp_winning.5.d                    
	picture = GFX_report_event_soviet_tanks_snow  
	immediate = { log = "[GetDateText]: [Root.GetName]: swp_winning.5" }	

	is_triggered_only = yes  

	##Peace treaty will happen
	option = { 								
		name = swp_winning.5.a 

			##Every enemy faction member, who controls player territory, also gets an event
				every_enemy_country = {
				
					limit = {
						OR = {			
							is_in_faction_with = FROM
							has_guaranteed = FROM
							is_subject_of = FROM
						}
						
						NOT = { 						
							tag = FROM 
							has_global_flag = swp_vanilla_conference_settings						
						}

						any_state = {
							is_owned_by = PREV
							is_controlled_by = ROOT								
						}
					}	
				
				country_event = { days = 1 id = swp_winning.6}

				}

				
			##event for faction leader	
			FROM = { country_event = { days = 1 id = swp_winning.12} }	
			
			##Peace is happening right now; blocking any other peace treaties at the moment
			set_global_flag = peace_in_process
		
	}
		
		
	#Fuck you AI, I will crush you and your whole family!
	option = { 	
	name = swp_winning.5.b
		
		#NEGOTIATIONS FAILED!
		FROM = {country_event = { days = 1 id = swp_final.2} }


	}
	
}



