﻿
##################################################
# Effects related to Board Games
##################################################

##################################################
# SETUP & CLEANUP EFFECTS

configure_start_board_game_effect = {
	# DOCUMENTATION
	## scope:bg_initiator
		### Who started the board game? This is who the output/invalidation events will fire for.
	## scope:bg_attacker
		### The hostile party - in the event of a tie, the match is decided against them.
	## scope:bg_defender
		### The defending party - in the event of a tie, the match is decided in their favour.
	## scope:bg_system = flag:XYZ
		### These control both the relevant skills, counters, & some flavour adjacent to the game.
		### chess
		### pachisi
		### hnefatafl
		### tabula
		### go
		### fidchell
	## scope:locale = flag:XYZ
		### These just trigger the event background of the same name. New ones can be added as needed, but should be added across all three events (round, victory, loss/board_games.0001, board_games.0031, board_games.0041), as we can't add a scripted list.
		### terrain_scope
		### wilderness_scope
		### battlefield
		### temple
		### council_chamber
		### courtyard
		### dungeon
		### docks
		### feast
		### gallows
		### garden
		### market
		### sitting_room
		### bedchamber
		### study
		### physicians_study
		### tavern
		### throne_room
		### army_camp
		### random_nice_day
		#### Selects either garden or sitting_room, depending on season.
	## output event ID
		### Which event fires after the game's completion?

	# Sort parties.
	$BG_INITIATOR$ = { save_scope_as = bg_initiator }
	$BG_DEFENDER$ = { save_scope_as = bg_defender }
	$BG_ATTACKER$ = { save_scope_as = bg_attacker }
	# Make sure they're clean for setup.
	hidden_effect = { remove_board_game_variables_effect = yes}
	# Set them both to busy for the single combat.
	scope:bg_defender = {
		set_variable = {
			name = engaged_in_board_game
			value = yes
		}
	}
	scope:bg_attacker = {
		set_variable = {
			name = engaged_in_board_game
			value = yes
		}
	}
	## Sadly, we have no good justification for ripping your shirt off whilst playing board games.
	# Set rules & system.
	save_scope_value_as = {
		name = bg_system
		value = flag:$SYSTEM$
	}
	## Chess
	### In chess, our counter order is Martial -> Learning -> Intrigue.
	if = {
		limit = { scope:bg_system = flag:chess }
		save_scope_value_as = {
			name = bg_skill_a
			value = flag:martial
		}
		save_scope_value_as = {
			name = bg_skill_b
			value = flag:learning
		}
		save_scope_value_as = {
			name = bg_skill_c
			value = flag:intrigue
		}
	}
	## Pachisi
	### For pachisi, our counter order is Diplomacy -> Intrigue -> Stewardship.
	else_if = {
		limit = { scope:bg_system = flag:pachisi }
		save_scope_value_as = {
			name = bg_skill_a
			value = flag:diplomacy
		}
		save_scope_value_as = {
			name = bg_skill_b
			value = flag:intrigue
		}
		save_scope_value_as = {
			name = bg_skill_c
			value = flag:stewardship
		}
	}
	## Hnefatafl
	### For hnefatafl, our counter order is Intrigue -> Martial -> Prowess.
	else_if = {
		limit = { scope:bg_system = flag:hnefatafl }
		save_scope_value_as = {
			name = bg_skill_a
			value = flag:intrigue
		}
		save_scope_value_as = {
			name = bg_skill_b
			value = flag:martial
		}
		save_scope_value_as = {
			name = bg_skill_c
			value = flag:prowess
		}
	}
	## Tabula
	### For tabula, our counter order is Intrigue -> Learning -> Stewardship.
	else_if = {
		limit = { scope:bg_system = flag:tabula }
		save_scope_value_as = {
			name = bg_skill_a
			value = flag:intrigue
		}
		save_scope_value_as = {
			name = bg_skill_b
			value = flag:learning
		}
		save_scope_value_as = {
			name = bg_skill_c
			value = flag:stewardship
		}
	}
	## Go
	### For go, our counter order is Learning -> Martial -> Intrigue.
	### Additional game design credit: Alexander Windahl.
	else_if = {
		limit = { scope:bg_system = flag:go }
		save_scope_value_as = {
			name = bg_skill_a
			value = flag:learning
		}
		save_scope_value_as = {
			name = bg_skill_b
			value = flag:martial
		}
		save_scope_value_as = {
			name = bg_skill_c
			value = flag:intrigue
		}
	}
	## Fidchell
	### For fidchell, our counter order is Learning -> Prowess -> Diplomacy.
	else_if = {
		limit = { scope:bg_system = flag:fidchell }
		save_scope_value_as = {
			name = bg_skill_a
			value = flag:learning
		}
		save_scope_value_as = {
			name = bg_skill_b
			value = flag:prowess
		}
		save_scope_value_as = {
			name = bg_skill_c
			value = flag:diplomacy
		}
	}
	# Note where the fight is taking place, if we're using a specific location.
	save_scope_value_as = {
		name = bg_locale
		value = flag:$LOCALE$
	}
	## Grab _scope backgrounds appropriately..
	if = {
		limit = { scope:bg_locale = flag:terrain_scope }
		scope:bg_defender.location = { save_scope_as = background_terrain_scope }
	}
	else_if = {
		limit = { scope:bg_locale = flag:wilderness_scope }
		scope:bg_defender.location = { save_scope_as = background_wilderness_scope }
	}
	## Sort any random_ backgrounds if necessary.
	else_if = {
		limit = { scope:bg_locale = flag:random_nice_day }
		random_list = {
			# Indoors.
			50 = {
				trigger = {
					exists = scope:bg_defender.location
					is_nice_season_to_be_inside_trigger = { LOCATION = scope:bg_defender.location }
				}
				save_scope_value_as = {
					name = bg_locale
					value = flag:sitting_room
				}
			}
			# Outdoors.
			50 = {
				trigger = {
					exists = scope:bg_defender.location
					is_nice_season_to_be_outside_trigger = { LOCATION = scope:bg_defender.location }
				}
				save_scope_value_as = {
					name = bg_locale
					value = flag:garden
				}
			}
			# Fallback: occasionally characters fall into a bloody void between space and time, I guess, and when they do we need to stop trying to calculate the weather for non-Euclidian space.
			50 = {
				trigger = {
					NOT = { exists = scope:bg_defender.location }
				}
				save_scope_value_as = {
					name = bg_locale
					# Because black holes are from space and space lives inside books.
					value = flag:study
				}
			}
		}
	}
	# Arrange aftermath.
	save_scope_value_as = {
		name = follow_up_event
		value = event_id:$OUTPUT_EVENT$
	}
	save_scope_value_as = {
		name = invalidation_event
		value = event_id:$INVALIDATION_EVENT$
	}
	# Configure starting scopes'n'variables.
	scope:bg_defender = {
		# Set our current round.
		set_variable = {
			name = bg_current_round
			value = 1
		}
		# Set up our initial scores.
		set_variable = {
			name = bg_current_score
			value = 0
		} 
	}
	scope:bg_attacker = {
		# Set up our initial scores.
		set_variable = {
			name = bg_current_score
			value = 0
		}
	}
	# Finally, send the first event.
	scope:bg_defender = { trigger_event = board_games.0001 }
}

# Clean up all variables resulting from playing a board game.
remove_board_game_variables_effect = {
	# Player-side variables.
	remove_variable = bg_current_round
	remove_variable = bg_current_score
	remove_variable = bg_move_result
	# If there are any wagering counties, clear the blockers on their being wagered.
	if = {
		limit = { exists = scope:bg_stake_land_recipient }
		scope:bg_stake_land_recipient = { remove_variable = wagered_county }
	}
	if = {
		limit = { exists = scope:bg_stake_land_actor }
		scope:bg_stake_land_actor = { remove_variable = wagered_county }
	}
	# Remove any debug variables.
	remove_variable = bg_gib_only_losing_locs
	remove_variable = bg_gib_only_winning_locs
	# Note, we don't remove var:engaged_in_board_game here, as we run this effect at the start of games for clean-up also.
}

remove_engaged_in_board_game_variables_effect = {
	scope:bg_attacker = {
		if = {
			# Restrict to live characters to prevent errors.
			limit = { is_alive = yes }
			remove_variable = engaged_in_board_game
		}
	}
	scope:bg_defender = {
		if = {
			# Restrict to live characters to prevent errors.
			limit = { is_alive = yes }
			remove_variable = engaged_in_board_game
		}
	}
}

# Clean all consequences of a single combat.
remove_board_game_info_effect = {
	scope:bg_defender = {
		# Only remove variables if they're alive, as otherwise we get errors.
		if = {
			limit = { is_alive = yes }
			remove_board_game_variables_effect = yes
		}
	}
	scope:bg_attacker = {
		# Only remove variables if they're alive, as otherwise we get errors.
		if = {
			limit = { is_alive = yes }
			remove_board_game_variables_effect = yes
		}
	}
	remove_engaged_in_board_game_variables_effect = yes
}

bg_fire_clean_up_and_invalidate_effect = {
	remove_engaged_in_board_game_variables_effect = yes
	# We try to wing this at scope:bg_defender...
	if = {
		limit = {
			scope:bg_defender = { is_alive = yes }
		}
		scope:bg_defender = {
			trigger_event = { saved_event_id = scope:invalidation_event }
		}
	}
	# ... then scope:bg_attacker...
	else_if = {
		limit = {
			scope:bg_attacker = { is_alive = yes }
		}
		scope:bg_attacker = {
			trigger_event = { saved_event_id = scope:invalidation_event }
		}
	}
	# ... then scope:bg_initiator...
	else_if = {
		limit = {
			scope:bg_initiator = { is_alive = yes }
		}
		scope:bg_initiator = {
			trigger_event = { saved_event_id = scope:invalidation_event }
		}
	}
	# ... then we give up, as somehow *everyone* involved in the board game has died simultaneously.
}

##################################################
# INTERACTION-SPECIFIC EFFECTS

challenge_to_2p_chess_interaction_lock_in_actor_stake_land_effect = {
	scope:actor = {
		save_temporary_scope_as = ordering_char_temp
		ordered_held_title = {
			limit = {
				bg_stake_land_valid_neighbouring_county_to_trade_trigger = { NEIGHBOUR = scope:recipient }
			}
			order_by = bg_stake_land_evaluate_appropriate_county_ordering_value
			save_scope_as = bg_stake_land_actor
			set_variable = wagered_county
		}
	}
}

challenge_to_2p_chess_interaction_lock_in_game_type_effect = {
	$GAME_TYPE_SETTER$ = {
		# Landless characters use their location.
		if = {
			limit = { is_playable_character = no }
			location = {
				# Pachisi - favoured in the eastern parts of the map.
				if = {
					limit = { bg_game_type_region_pachisi_trigger = yes }
					save_scope_value_as = {
						name = bg_interaction_game_type
						value = flag:pachisi
					}
				}
				# Hnefatafl - favoured in the northern parts of the map.
				else_if = {
					limit = { bg_game_type_region_hnefatafl_trigger = yes }
					save_scope_value_as = {
						name = bg_interaction_game_type
						value = flag:hnefatafl
					}
				}
				# Tabula - favoured around the Med.
				else_if = {
					limit = { bg_game_type_region_tabula_trigger = yes }
					save_scope_value_as = {
						name = bg_interaction_game_type
						value = flag:tabula
					}
				}
				# Go - favoured around Tibet.
				else_if = {
					limit = { bg_game_type_region_go_trigger = yes }
					save_scope_value_as = {
						name = bg_interaction_game_type
						value = flag:go
					}
				}
				# Fidchell - favoured around the Celtic rim.
				else_if = {
					limit = { bg_game_type_region_fidchell_trigger = yes }
					save_scope_value_as = {
						name = bg_interaction_game_type
						value = flag:fidchell
					}
				}
				# Chess - fallback.
				else = {
					save_scope_value_as = {
						name = bg_interaction_game_type
						value = flag:chess
					}
				}
			}
		}
		# Landed characters use their capital.
		else = {
			capital_province = {
				# Pachisi - favoured in the eastern parts of the map.
				if = {
					limit = { bg_game_type_region_pachisi_trigger = yes }
					save_scope_value_as = {
						name = bg_interaction_game_type
						value = flag:pachisi
					}
				}
				# Hnefatafl - favoured in the northern parts of the map.
				else_if = {
					limit = { bg_game_type_region_hnefatafl_trigger = yes }
					save_scope_value_as = {
						name = bg_interaction_game_type
						value = flag:hnefatafl
					}
				}
				# Tabula - favoured around the Med.
				else_if = {
					limit = { bg_game_type_region_tabula_trigger = yes }
					save_scope_value_as = {
						name = bg_interaction_game_type
						value = flag:tabula
					}
				}
				# Go - favoured around Tibet.
				else_if = {
					limit = { bg_game_type_region_go_trigger = yes }
					save_scope_value_as = {
						name = bg_interaction_game_type
						value = flag:go
					}
				}
				# Fidchell - favoured around the Celtic rim.
				else_if = {
					limit = { bg_game_type_region_fidchell_trigger = yes }
					save_scope_value_as = {
						name = bg_interaction_game_type
						value = flag:fidchell
					}
				}
				# Chess - fallback.
				else = {
					save_scope_value_as = {
						name = bg_interaction_game_type
						value = flag:chess
					}
				}
			}
		}
	}
}

fp2_bg_stake_friendly_effect = {
	$VICTOR$ = {
		if = {
			# Obscure opinion adds from players.
			limit = { is_ai = yes }
			add_opinion = {
				target = $LOSER$
				modifier = pleased_opinion
				opinion = 25
			}
		}
		add_stress = minor_stress_loss
	}
	$LOSER$ = {
		if = {
			# Obscure opinion adds from players.
			limit = { is_ai = yes }
			add_opinion = {
				target = $VICTOR$
				modifier = pleased_opinion
				opinion = 10
			}
		}
		add_stress = minor_stress_gain
	}	
}

fp2_bg_stake_fame_effect = {
	$VICTOR$ = { add_prestige = medium_prestige_gain }
	$LOSER$ = { add_prestige = minor_prestige_loss }
}

fp2_bg_stake_fortune_effect = {
	# A nice, simple, transfer of funds.
	$LOSER$ = {
		pay_short_term_gold = {
			target = $VICTOR$
			gold = scope:bg_stake_gold
		}
	}
}

fp2_bg_stake_titles_effect = {
	create_title_and_vassal_change = {
		type = granted
		save_scope_as = change
	}
	if = {
		limit = { $VICTOR$ = scope:actor }
		scope:bg_stake_land_recipient = {
			change_title_holder = {
				holder = scope:actor
				change = scope:change
			}
		}
	}
	else_if = {
		limit = { $VICTOR$ = scope:recipient }
		scope:bg_stake_land_actor = {
			change_title_holder = {
				holder = scope:recipient
				change = scope:change
			}
		}
	}
	resolve_title_and_vassal_change = scope:change
}

##################################################
# RESULT EFFECTS

perk_interaction_0122_apply_effects_effect = {
	# Stakes.
	## Friendlies bet opinion & stress.
	if = {
		limit = { always = scope:chess_friendly }
		fp2_bg_stake_friendly_effect = {
			VICTOR = scope:bg_victor
			LOSER = scope:bg_loser
		}
	}
	## Fame bets prestige.
	if = {
		limit = { always = scope:chess_prestige }
		fp2_bg_stake_fame_effect = {
			VICTOR = scope:bg_victor
			LOSER = scope:bg_loser
		}
	}
	## Fortune bets gold.
	if = {
		limit = { always = scope:chess_gold }
		fp2_bg_stake_fortune_effect = {
			VICTOR = scope:bg_victor
			LOSER = scope:bg_loser
		}
	}
	## Land bets... land.
	if = {
		limit = { always = scope:chess_land }
		fp2_bg_stake_titles_effect = { VICTOR = scope:bg_victor }
	}
}
