from header_common import *
from header_operations import *
from header_parties import *
from header_items import *
from header_skills import *
from header_triggers import *
from header_troops import *

from module_constants import *

####################################################################################################################
#  Each trigger contains the following fields:
# 1) Check interval: How frequently this trigger will be checked
# 2) Delay interval: Time to wait before applying the consequences of the trigger
#    After its conditions have been evaluated as true.
# 3) Re-arm interval. How much time must pass after applying the consequences of the trigger for the trigger to become active again.
#    You can put the constant ti_once here to make sure that the trigger never becomes active again after it fires once.
# 4) Conditions block (list). This must be a valid operation block. See header_operations.py for reference.
#    Every time the trigger is checked, the conditions block will be executed.
#    If the conditions block returns true, the consequences block will be executed.
#    If the conditions block is empty, it is assumed that it always evaluates to true.
# 5) Consequences block (list). This must be a valid operation block. See header_operations.py for reference. 
####################################################################################################################

# Some constants for use below
merchant_inventory_space = 44
num_merchandise_goods = 36



triggers = [
# Tutorial:
  (0.1, 0, ti_once, [(map_free,0)], [(dialog_box,"str_tutorial_map1")]),

#  (1.0, 0, ti_once, [(map_free,0)], [(start_map_conversation, "trp_guide", -1)]),

# Refresh Merchants
  (0.0, 0, 24.0, [], [
                      (reset_item_probabilities,100),
                      (set_merchandise_modifier_quality,150),


                      (reset_item_probabilities,100),(set_item_probability_in_merchandise,"itm_salt",700),
                      (troop_add_merchandise,"trp_salt_mine_merchant",itp_type_goods,num_merchandise_goods),

                      # Add trade goods to merchant inventories
#                      (store_sub, ":item_to_production_slot", slot_town_trade_good_productions_begin, trade_goods_begin),
                      (store_sub, ":item_to_price_slot", slot_town_trade_good_prices_begin, trade_goods_begin),

                      (try_for_range,":cur_center",towns_begin,towns_end),
                        (party_get_slot,":cur_merchant",":cur_center",slot_town_merchant),
                        (reset_item_probabilities,100),
                        (try_for_range, ":cur_goods", trade_goods_begin, trade_goods_end),
#                          (store_add, ":cur_production_slot", ":cur_goods", ":item_to_production_slot"),
                          (store_add, ":cur_price_slot", ":cur_goods", ":item_to_price_slot"),
#                          (party_get_slot, ":cur_production", ":cur_center", ":cur_production_slot"),
                          (party_get_slot, ":cur_price", ":cur_center", ":cur_price_slot"),
                      
#                          (assign, ":cur_probability", 100),
#                          (store_add, ":cur_production", 100, ":cur_production"),
#                          (val_mul, ":cur_probability", ":cur_production"),
#                          (val_div, ":cur_probability", 100),
#                          (try_begin),
#                            (gt, ":cur_probability", 100),
#                            (store_sub, ":temp_dif", ":cur_probability", 100),
#                            (val_mul, ":temp_dif", 4),
#                            (val_add, ":cur_probability", ":temp_dif"),
#                          (try_end),
#                          (store_sub, ":temp_dif", average_price_factor, ":cur_price"),
#                          (val_div, ":temp_dif", 2),
#                          (val_add, ":cur_price", ":temp_dif"),

						  (call_script, "script_center_get_production", ":cur_center", ":cur_goods"),
						  (assign, ":cur_probability", reg0),
						  (call_script, "script_center_get_consumption", ":cur_center", ":cur_goods"),
						  (val_add, ":cur_probability", reg0),
						  
						  (val_mul, ":cur_probability", 4),


                          (val_mul, ":cur_probability", average_price_factor),
                          (val_div, ":cur_probability", ":cur_price"),
                          (val_mul, ":cur_probability", average_price_factor),
                          (val_div, ":cur_probability", ":cur_price"),
                          (val_mul, ":cur_probability", average_price_factor),
                          (val_div, ":cur_probability", ":cur_price"),
#                          (val_mul, ":cur_probability", average_price_factor),
#                          (val_div, ":cur_probability", ":cur_price"),
                          (set_item_probability_in_merchandise,":cur_goods",":cur_probability"),
                        (try_end),
                        (troop_add_merchandise,":cur_merchant",itp_type_goods,num_merchandise_goods),

						# Ne building
						## Bookstore stuff - Jinnai
                        (try_begin),
                          (party_slot_eq, ":cur_center", slot_center_has_bookstore, 1),
                          (troop_add_merchandise,":cur_merchant",itp_type_book,4),
                        (try_end),
						# ne end building 
						
                        (troop_ensure_inventory_space,":cur_merchant",merchant_inventory_space),
                        (troop_sort_inventory, ":cur_merchant"),
                        (store_troop_gold, reg(6),":cur_merchant"),
						# NE building
                       # v585 Josef add more money to merchants                       
					   #(lt,":cur_gold",1500),
                        (store_character_level, ":player_level", "trp_player"),
                        (store_mul, ":min_gold", ":player_level", 500),
                        (val_add, ":min_gold", 5500),
						(lt,reg(6), ":min_gold"),
                        (store_mul, ":min_gold", ":player_level", 100),
                        (val_add, ":min_gold", 1500),
                        (store_mul, ":max_gold", ":player_level", 250),
                        (val_add, ":max_gold", 2500),
                        (store_random_in_range,":new_gold",":min_gold",":max_gold"),
                        # (store_random_in_range,":new_gold",500,1000),
						 # NE end building
                        (call_script, "script_troop_add_gold", ":cur_merchant", ":new_gold"),
                      (try_end),
                     ]),

# Refresh Armor sellers
  (0.0, 0, 24.0, [], [
                      (reset_item_probabilities,100),
                      (set_merchandise_modifier_quality,150),
                      (try_for_range,reg(2),armor_merchants_begin,armor_merchants_end),
                        (store_sub, ":cur_town", reg2, armor_merchants_begin),
                        (val_add, ":cur_town", towns_begin),
                        (party_get_slot, ":cur_faction", ":cur_town", slot_center_original_faction),

                        # Lav's modifications
                        # Remove items from merchant's inventory with 15% probability
                        (troop_get_inventory_capacity, ":capacity", reg2),
                        (try_for_range, ":slot_id", num_equipment_kinds, ":capacity"),
                            (troop_get_inventory_slot, ":item_id", reg2, ":slot_id"),
                            (ge, ":item_id", 0),
                            (item_get_type, ":item_class", ":item_id"),
                            (store_random_in_range, ":chance", 0, 100),
                            (this_or_next|lt, ":chance", 15),
                            (neg|is_between, ":item_class", itp_type_head_armor, itp_type_hand_armor+1),
                            (troop_set_inventory_slot, reg2, ":slot_id", -1),
                        (try_end),
                        # Now if merchant has free space, add it with random stuff
                        (store_free_inventory_capacity, ":free_capacity", reg2),
                        (try_begin),
                            (gt, ":free_capacity", merchant_inventory_space),
                            (try_for_range, reg5, 0, ":free_capacity"),
                                (store_random_in_range, ":item_class", 0, 11),
                                (try_begin),
                                    (eq, ":item_class", 0), # 0
                                    (troop_add_merchandise_with_faction,reg(2), ":cur_faction",itp_type_hand_armor,1),
                                (else_try),
                                    (lt, ":item_class", 3), # 1-2
                                    (troop_add_merchandise_with_faction,reg(2), ":cur_faction",itp_type_foot_armor,1),
                                (else_try),
                                    (lt, ":item_class", 7), # 3-6
                                    (troop_add_merchandise_with_faction,reg(2), ":cur_faction",itp_type_head_armor,1),
                                (else_try),
                                    (troop_add_merchandise_with_faction,reg(2), ":cur_faction",itp_type_body_armor,1),
                                (try_end),
                            (try_end),
                            (troop_ensure_inventory_space,reg(2),merchant_inventory_space),
                        (try_end),
                        (troop_sort_inventory, reg(2)),

                        # Finally, check merchant gold and buff it if necessary
                        (store_troop_gold, reg(6),reg(2)),
						# NE building
						 # v585 Josef add more money to armour merchants    
                        #(lt,reg(6),900),
                        (store_character_level, ":player_level", "trp_player"),
                        (store_mul, ":min_gold", ":player_level", 150),
                        (val_add, ":min_gold", 2000),
						(lt,reg(6), ":min_gold"),
                        (store_mul, ":min_gold", ":player_level", 10),
                        (val_add, ":min_gold", 200),
                        (store_mul, ":max_gold", ":player_level", 40),
                        (val_add, ":max_gold", 300),
                        (store_random_in_range,":new_gold",":min_gold",":max_gold"),
                        (call_script, "script_troop_add_gold", reg(2), ":new_gold"),
                      (end_try,0),
                     ]),


# Refresh Weapon sellers
  (0.0, 0, 24.0, [], [
                      (reset_item_probabilities,100),
                      (set_merchandise_modifier_quality,150),
                      (try_for_range,reg(2),weapon_merchants_begin,weapon_merchants_end),
                        (store_sub, ":cur_town", reg2, weapon_merchants_begin),
                        (val_add, ":cur_town", towns_begin),
                        (party_get_slot, ":cur_faction", ":cur_town", slot_center_original_faction),

                        # Lav's modifications
                        # Remove items from merchant's inventory with 15% probability
                        (troop_get_inventory_capacity, ":capacity", reg2),
                        (try_for_range, ":slot_id", num_equipment_kinds, ":capacity"),
                            (troop_get_inventory_slot, ":item_id", reg2, ":slot_id"),
                            (ge, ":item_id", 0),
                            (item_get_type, ":item_class", ":item_id"),
                            (store_random_in_range, ":chance", 0, 100),
                            (this_or_next|lt, ":chance", 15),
                            (neg|is_between, ":item_class", itp_type_one_handed_wpn, itp_type_thrown+1),
                            (troop_set_inventory_slot, reg2, ":slot_id", -1),
                        (try_end),
                        # Now if merchant has free space, add it with random stuff
                        (store_free_inventory_capacity, ":free_capacity", reg2),
                        (try_begin),
                            (gt, ":free_capacity", merchant_inventory_space),
                            (try_for_range, reg5, 0, ":free_capacity"),
                                (store_random_in_range, ":item_class", 0, 13),
                                (try_begin),
                                    (lt, ":item_class", 2),
                                    (troop_add_merchandise_with_faction,reg(2), ":cur_faction",itp_type_one_handed_wpn,1),
                                (else_try),
                                    (lt, ":item_class", 4),
                                    (troop_add_merchandise_with_faction,reg(2), ":cur_faction",itp_type_two_handed_wpn,1),
                                (else_try),
                                    (lt, ":item_class", 6),
                                    (troop_add_merchandise_with_faction,reg(2), ":cur_faction",itp_type_polearm,1),
                                (else_try),
                                    (lt, ":item_class", 8),
                                    (troop_add_merchandise_with_faction,reg(2), ":cur_faction",itp_type_shield,1),
                                (else_try),
                                    (eq, ":item_class", 8),
                                    (troop_add_merchandise_with_faction,reg(2), ":cur_faction",itp_type_bow,1),
                                (else_try),
                                    (eq, ":item_class", 9),
                                    (troop_add_merchandise_with_faction,reg(2), ":cur_faction",itp_type_crossbow,1),
                                (else_try),
                                    (eq, ":item_class", 10),
                                    (troop_add_merchandise_with_faction,reg(2), ":cur_faction",itp_type_thrown,1),
                                (else_try),
                                    (eq, ":item_class", 11),
                                    (troop_add_merchandise_with_faction,reg(2), ":cur_faction",itp_type_arrows,1),
                                (else_try),
                                    (troop_add_merchandise_with_faction,reg(2), ":cur_faction",itp_type_bolts,1),
                                (try_end),
                            (try_end),
                            (troop_ensure_inventory_space,reg(2),merchant_inventory_space),
                        (try_end),
                        (troop_sort_inventory, reg(2)),

                        (store_troop_gold, reg(6),reg(2)),
						# NE building 
						 # v585 Josef add more money to weapon merchants    
                        #(lt,reg(6),900),
                        (store_character_level, ":player_level", "trp_player"),
                        (store_mul, ":min_gold", ":player_level", 125),
                        (val_add, ":min_gold", 2000),
						(lt,reg(6), ":min_gold"),
                        (store_mul, ":min_gold", ":player_level", 10),
                        (val_add, ":min_gold", 200),
                        (store_mul, ":max_gold", ":player_level", 40),
                        (val_add, ":max_gold", 300),
                        (store_random_in_range,":new_gold",":min_gold",":max_gold"),
                      (call_script, "script_troop_add_gold", reg(2), ":new_gold"),
                      (end_try,0),
                     ]),

# Refresh Horse sellers
  (0.0, 0, 24.0, [], [
                      (reset_item_probabilities,100),
                      (set_merchandise_modifier_quality,150),
                      (try_for_range,":cur_merchant",horse_merchants_begin,horse_merchants_end),
                        (store_sub, ":cur_town", ":cur_merchant", horse_merchants_begin),
                        (val_add, ":cur_town", towns_begin),
                        (party_get_slot, ":cur_faction", ":cur_town", slot_center_original_faction),
                        (troop_add_merchandise_with_faction,":cur_merchant", ":cur_faction",itp_type_horse,5),
                        (troop_ensure_inventory_space,":cur_merchant",65),
                        (troop_sort_inventory, ":cur_merchant"),
                        (store_troop_gold, reg6,":cur_merchant"),
						# NE building
						# v585 Josef add more money to horse merchants   
                        #(lt,":cur_gold",600),
                        (store_character_level, ":player_level", "trp_player"),
                        (store_mul, ":min_gold", ":player_level", 125),
                        (val_add, ":min_gold", 1750),
						(lt,reg(6), ":min_gold"),
                        (store_mul, ":min_gold", ":player_level", 10),
                        (val_add, ":min_gold", 200),
                        (store_mul, ":max_gold", ":player_level", 40),
                        (val_add, ":max_gold", 300),
                        (store_random_in_range,":new_gold",":min_gold",":max_gold"),
                        (call_script, "script_troop_add_gold", ":cur_merchant", ":new_gold"),
                      (try_end),
                     ]),
  
# NE assassination
# v586 Josef - every 24 hours, increment these variables
  (0.0, 0, 24.0, [], [
                      # this controls the days between assassin attacks
                      (val_add, "$days", 1),
					  (val_add, "$nemesis_days", 1), 
					  
					  # this controls the minstrel songs (only 3 at one time)
					     (try_begin),
						 (ge, "$played_songs", 1), 
						 (val_sub, "$played_songs", 1), 
						 (try_end), 
					  
					     (try_begin), 
						 (eq, "$nemesis_days", 67), 
						 (assign, "$nemesis_days", 0),
						 (neq, "$spymaster", 0),
                         (set_spawn_radius, 3),
                         (spawn_around_party,"p_town_6","pt_nemesis"),
                         (assign, ":new_party", reg0),
                         (party_set_ai_behavior,":new_party",ai_bhvr_attack_party),
                         (party_set_ai_object,":new_party","p_main_party"),
                         (party_set_flags, ":new_party", pf_default_behavior, 0),
	                     (call_script, "script_add_notification_menu", "mnu_notification_of_nemesis", "$spymaster", 0),
	                     (try_end), 
					  
					  # if Formations has been turned off during an elite quest, turn it back on
					     (try_begin),
                         (eq, "$switch_formations", 1), 
                         (assign, "$setting_troops_form", 1),
						 (assign, "$switch_formations", 0), 
                         (try_end),
						 
					  # Lord targetting
					  # send the mercs back to Praven if they survive
					  # there needs to be two checks here - the merc_retreat variable only gets set
					  # if the mercs were the first to battle - if they joined a battle already in progress, it doesn't get set
					  (try_begin), 
					  (eq, "$merc_retreat", 1), 
					  (party_set_ai_behavior,"$merc_party",ai_bhvr_travel_to_party),
                      (party_set_ai_object,"$merc_party","p_town_6"),
                      (party_set_flags, "$merc_party", pf_default_behavior, 0),
					  #reinitialise variables
					  (assign, "$merc_retreat", 0), 
					  (assign, "$merc_party", 0), 
					  (assign, "$ass_target_lord", "p_main_party"), 
					  (else_try), 
					  ##so we also check whether the Lord has disappeared from the map. If this also fails, then 
					  ##the mercs will wait for him to reassemble his troops and attack him again
					  (neq, "$hero_requested_to_learn_target", 0), 
                      (call_script, "script_get_information_about_troops_position", "$hero_requested_to_learn_target", 0),                             
                      (eq, reg0, 0),
					  (party_set_ai_behavior,"$merc_party",ai_bhvr_travel_to_party),
                      (party_set_ai_object,"$merc_party","p_town_6"),
                      (party_set_flags, "$merc_party", pf_default_behavior, 0),
					  ## reinitialise variables
					  (assign, "$merc_retreat", 0), 
					  (assign, "$hero_requested_to_learn_target", 0), 	
					  (assign, "$ass_target_lord", "p_main_party"), 			  
					  (try_end),
			  
					  # assassination attempt 
					  (ge, "$ass_days", 0),
					  (val_sub, "$ass_days", 1),
					  (eq, "$ass_days", 0),
					  (assign, "$ass_target_lord", "p_main_party"), 
					  (store_random_in_range, ":ass_success", 0, 100),
					  (try_begin),
					  (is_between, ":ass_success", 0, 50),  
					  # 50% chance of the Lord escaping unharmed
					  (call_script, "script_add_notification_menu", "mnu_assassination_failed", "$hero_requested_to_learn_assassinate", 0),
					  (else_try), 
					  (is_between, ":ass_success", 49, 70),
					  # 20% chance of the Lord's troops fighting off the assassins. Many of the lord's troops are
					  # killed (his honor guard, not his garrison)
					  (call_script, "script_add_notification_menu", "mnu_assassination_fought_off", "$hero_requested_to_learn_assassinate", 0),				  
					  (else_try), 
					  (is_between, ":ass_success", 69, 90),
					  # 20% chance of the Lord being badly injured. His followers are spooked by the attack and his fiefs experience a sharp 
					  #drop in prosperity. Like a stock market crash, kinda.			  
					  (call_script, "script_add_notification_menu", "mnu_assassination_prosperity", "$hero_requested_to_learn_assassinate", 0),
					  (else_try), 
					  (is_between, ":ass_success", 89, 101), 					  
					  # 10% chance of the Lord's king losing faith in him and relieving him of his holdings altogether(!!). 
					  #Basically his castle or town is taken off him and given to a more deserving lord. 
					  (call_script, "script_add_notification_menu", "mnu_assassination_fiefed", "$hero_requested_to_learn_assassinate", 0),					  
					  (try_end), 
					  

	 		   
                    ]),
# NE end assassination
#############

#Captivity:

#  (1.0, 0, 0.0, [],
#   [
#       (ge,"$captivity_mode",1),
#       (store_current_hours,reg(1)),
#       (val_sub,reg(1),"$captivity_end_time"),
#       (ge,reg(1),0),
#       (display_message,"str_nobleman_reached_destination"),
#       (jump_to_menu,"$captivity_end_menu"),
#    ]),


  (5.7, 0, 0.0, 
  [
    (store_num_parties_of_template, reg2, "pt_manhunters"),    
    (lt, reg2, 4)
  ],
  [
    (set_spawn_radius, 1),
    (store_add, ":p_town_22_plus_one", "p_town_22", 1),
    (store_random_in_range, ":selected_town", "p_town_1", ":p_town_22_plus_one"),
    (spawn_around_party, ":selected_town", "pt_manhunters"),
  ]),



  (1.0, 0.0, 0.0, [
  (check_quest_active, "qst_track_down_bandits"),
  (neg|check_quest_failed, "qst_track_down_bandits"),
  (neg|check_quest_succeeded, "qst_track_down_bandits"),
  
  ],
   [
    (quest_get_slot, ":bandit_party", "qst_track_down_bandits", slot_quest_target_party),
	(try_begin),
		(party_is_active, ":bandit_party"),
		(store_faction_of_party, ":bandit_party_faction", ":bandit_party"),
		(neg|is_between, ":bandit_party_faction", kingdoms_begin, kingdoms_end), #ie, the party has not respawned as a non-bandit
		
		
		(assign, ":spot_range", 8),
		(try_begin),
			(is_currently_night),
			(assign, ":spot_range", 5),
		(try_end),
		
		(try_for_parties, ":party"),
			(gt, ":party", "p_spawn_points_end"),
			
			(store_faction_of_party, ":faction", ":party"),
			(is_between, ":faction", kingdoms_begin, kingdoms_end),
			
			
			(store_distance_to_party_from_party, ":distance", ":party", ":bandit_party"),
			(lt, ":distance", ":spot_range"),
			(try_begin),
				(eq, "$cheat_mode", 1),
				(str_store_party_name, s4, ":party"),
				(display_message, "@{!}DEBUG -- Wanted bandits spotted by {s4}"),
			(try_end),
			
			(call_script, "script_get_closest_center", ":bandit_party"),
			(assign, ":nearest_center", reg0),
#			(try_begin),
#				(get_party_ai_current_behavior, ":behavior", ":party"),
#				(eq, ":behavior", ai_bhvr_attack_party),
#				(call_script, "script_add_log_entry",  logent_party_chases_wanted_bandits, ":party",  ":nearest_center", ":bandit_party", -1),
#			(else_try),
#				(eq, ":behavior", ai_bhvr_avoid_party),
#				(call_script, "script_add_log_entry",  logent_party_runs_from_wanted_bandits, ":party",  ":nearest_center", ":bandit_party", -1),
#			(else_try),
			(call_script, "script_add_log_entry",  logent_party_spots_wanted_bandits, ":party",  ":nearest_center", ":bandit_party", -1),
#			(try_end),
		(try_end),
	(else_try), #Party not found
		(display_message, "str_bandits_eliminated_by_another"),
        (call_script, "script_abort_quest", "qst_track_down_bandits", 0),
	(try_end),
   ]),


#Tax Collectors
# Prisoner Trains
#  (4.1, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_swadians"),
#                         (assign, "$pin_party_template", "pt_swadian_prisoner_train"),
#                         (assign, "$pin_limit", peak_prisoner_trains),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                         (party_set_ai_behavior,"$pout_party",ai_bhvr_travel_to_party),
#                         (party_set_ai_object,"$pout_party","$pout_town"),
#                    ]),
#
#  (4.1, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_vaegirs"),
#                         (assign, "$pin_party_template", "pt_vaegir_prisoner_train"),
#                         (assign, "$pin_limit", peak_prisoner_trains),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                         (party_set_ai_behavior,"$pout_party",ai_bhvr_travel_to_party),
#                         (party_set_ai_object,"$pout_party","$pout_town"),
#                    ]),

  (2.0, 0, 0, [(store_random_party_of_template, reg(2), "pt_prisoner_train_party"),
               (party_is_in_any_town,reg(2)),
               ],
              [(store_faction_of_party, ":faction_no", reg(2)),
               (call_script,"script_cf_select_random_walled_center_with_faction", ":faction_no", -1),
               (party_set_ai_behavior,reg(2),ai_bhvr_travel_to_party),
               (party_set_ai_object,reg(2),reg0),
               (party_set_flags, reg(2), pf_default_behavior, 0),
            ]),

##Caravans
#  (4.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_swadians"),
#                         (assign, "$pin_party_template", "pt_swadian_caravan"),
#                         (assign, "$pin_limit", peak_kingdom_caravans),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                         (party_set_ai_behavior,"$pout_party",ai_bhvr_travel_to_party),
#                         (party_set_ai_object,"$pout_party","$pout_town"),
#                    ]),

#  (4.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_vaegirs"),
#                         (assign, "$pin_party_template", "pt_vaegir_caravan"),
#                         (assign, "$pin_limit", peak_kingdom_caravans),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                         (party_set_ai_behavior,"$pout_party",ai_bhvr_travel_to_party),
#                         (party_set_ai_object,"$pout_party","$pout_town"),
#                    ]),

##  (2.0, 0, 0, [(store_random_party_of_template, reg(2), "pt_kingdom_caravan_party"),
##               (party_is_in_any_town,reg(2)),
##               ],
##              [(store_faction_of_party, ":faction_no", reg(2)),
##               (call_script,"script_cf_select_random_town_with_faction", ":faction_no"),
##               (party_set_ai_behavior,reg(2),ai_bhvr_travel_to_party),
##               (party_set_ai_object,reg(2),reg0),
##               (party_set_flags, reg(2), pf_default_behavior, 0),
##            ]),
  
  (4.0, 0, 0.0,
   [
     (eq, "$caravan_escort_state", 1), #cancel caravan_escort_state if caravan leaves the destination
     (assign, ":continue", 0),
     (try_begin),
       (neg|party_is_active, "$caravan_escort_party_id"),
       (assign, ":continue", 1),
     (else_try),
       (get_party_ai_object, ":ai_object", "$caravan_escort_party_id"),
       (neq, ":ai_object", "$caravan_escort_destination_town"),
       (assign, ":continue", 1),
     (try_end),
     (eq, ":continue", 1),
     ],
   [
     (assign, "$caravan_escort_state", 0),
     ]),

#Messengers
#  (4.2, 0, 0.0, [],
#   [(assign, "$pin_faction", "fac_swadians"),
#    (assign, "$pin_party_template", "pt_swadian_messenger"),
#    (assign, "$pin_limit", peak_kingdom_messengers),
#    (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#    (party_set_ai_behavior,"$pout_party",ai_bhvr_travel_to_party),
#    (party_set_ai_object,"$pout_party","$pout_town"),
#    ]),

#  (4.2, 0, 0.0, [],
#   [(assign, "$pin_faction", "fac_vaegirs"),
#    (assign, "$pin_party_template", "pt_vaegir_messenger"),
#    (assign, "$pin_limit", peak_kingdom_caravans),
#    (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#    (party_set_ai_behavior,"$pout_party",ai_bhvr_travel_to_party),
#    (party_set_ai_object,"$pout_party","$pout_town"),
#    ]),

  (1.5, 0, 0, [(store_random_party_of_template, reg(2), "pt_messenger_party"),
               (party_is_in_any_town,reg(2)),
               ],
   [(store_faction_of_party, ":faction_no", reg(2)),
    (call_script,"script_cf_select_random_walled_center_with_faction", ":faction_no", -1),
    (party_set_ai_behavior,reg(2),ai_bhvr_travel_to_party),
    (party_set_ai_object,reg(2),reg0),
    (party_set_flags, reg(2), pf_default_behavior, 0),
    ]),
  
  

#Deserters

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_swadians"),
#                         (assign, "$pin_party_template", "pt_swadian_deserters"),
#                         (assign, "$pin_limit", 4),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),
  
#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_vaegirs"),
#                         (assign, "$pin_party_template", "pt_vaegir_deserters"),
#                         (assign, "$pin_limit", 4),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),

#Kingdom Parties
  (1.0, 0, 0.0, [],
   [(try_for_range, ":cur_kingdom", kingdoms_begin, kingdoms_end),
      (faction_slot_eq, ":cur_kingdom", slot_faction_state, sfs_active),
##      (neq, ":cur_kingdom", "fac_player_supporters_faction"),
##      (try_begin),
##        (store_random_in_range, ":random_no", 0, 100),
##        (lt, ":random_no", 10),
##        (call_script, "script_create_kingdom_party_if_below_limit", ":cur_kingdom", spt_forager),
##      (try_end),
##      (try_begin),
##        (store_random_in_range, ":random_no", 0, 100),
##        (lt, ":random_no", 10),
##        (call_script, "script_create_kingdom_party_if_below_limit", ":cur_kingdom", spt_scout),
##      (try_end),
##      (try_begin),
##        (store_random_in_range, ":random_no", 0, 100),
##        (lt, ":random_no", 10),
##        (call_script, "script_create_kingdom_party_if_below_limit", ":cur_kingdom", spt_patrol),
##      (try_end),
##      (try_begin),
##        (store_random_in_range, ":random_no", 0, 100),
##        (lt, ":random_no", 10),
##        (call_script, "script_create_kingdom_party_if_below_limit", ":cur_kingdom", spt_messenger),
##      (try_end),
      (try_begin),
        (store_random_in_range, ":random_no", 0, 100),
        (lt, ":random_no", 10),
        (call_script, "script_create_kingdom_party_if_below_limit", ":cur_kingdom", spt_kingdom_caravan),
      (try_end),
##      (try_begin),
##        (store_random_in_range, ":random_no", 0, 100),
##        (lt, ":random_no", 10),
##        (call_script, "script_create_kingdom_party_if_below_limit", ":cur_kingdom", spt_prisoner_train),
##      (try_end),
    (try_end),
    ]),


#Swadians
#  (0.0, 0.0, ti_once, [], [(assign,"$peak_swadian_foragers",4)]),

# NE scouts
 (0.0, 0.0, ti_once, [], [(assign,"$peak_swadian_scouts",4)]),
 (0.0, 0.0, ti_once, [], [(assign,"$peak_swadian_harassers",3)]),

#  (0.0, 0.0, ti_once, [], [(assign,"$peak_swadian_war_parties",2)]),
## Player kingdom patrol spawning - Jinnai
  (10.2, 0, 0.0, [],
                     [
                         (faction_slot_eq, "fac_player_supporters_faction", slot_faction_logistics_adviser, 1),
                         (assign, "$pin_faction", "fac_player_supporters_faction"),
                         (faction_get_slot,":capital","fac_player_supporters_faction", slot_faction_capital),
                         (try_begin),
                           (gt,":capital",0),
                           (party_get_slot,":faction",":capital",slot_center_original_faction),
                           (try_begin),
                             (eq,":faction","fac_kingdom_1"),
                             (assign, "$pin_party_template", "pt_swadian_patrol"),
                           (else_try),
                             (eq,":faction","fac_kingdom_2"),
                             (assign, "$pin_party_template", "pt_vaegir_patrol"),
                           (else_try),
                             (eq,":faction","fac_kingdom_3"),
                             (assign, "$pin_party_template", "pt_khergit_patrol"),
                           (else_try),
                             (eq,":faction","fac_kingdom_4"),
                             (assign, "$pin_party_template", "pt_nord_patrol"),
                           (else_try),
						     (eq,":faction","fac_kingdom_5"),
                             (assign, "$pin_party_template", "pt_rhodok_patrol"),
						   (else_try),			   
                           (assign, "$pin_party_template", "pt_sarranid_patrol"),
                           (try_end),
                         (else_try),
                           (store_random_in_range,":rand",1,7),
                           (try_begin),
                             (eq,":rand",1),
                             (assign, "$pin_party_template", "pt_swadian_patrol"),
                           (else_try),
                             (eq,":rand",2),
                             (assign, "$pin_party_template", "pt_vaegir_patrol"),
                           (else_try),
                             (eq,":rand",3),
                             (assign, "$pin_party_template", "pt_khergit_patrol"),
                           (else_try),
                             (eq,":rand",4),
                             (assign, "$pin_party_template", "pt_nord_patrol"),
                           (else_try),
						     (eq,":rand",5),
                             (assign, "$pin_party_template", "pt_rhodok_patrol"),
                           (else_try),
                             (assign, "$pin_party_template", "pt_sarranid_patrol"),							 						 
                           (try_end),
                         (try_end),
                         (assign,":towns",0),
                         (try_for_range,":town_no",towns_begin,towns_end), # Let the player's kingdom generate 2 patrols for every 1 town they own
                           (store_faction_of_party, ":faction", ":town_no"),
                           (eq,":faction","fac_player_supporters_faction"),
                           (val_add,":towns",2),
                         (try_end),
                         (assign, "$pin_limit", ":towns"),
                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
                    ]),

  (10.2, 0, 0.0, [],
                    [
                         (assign, "$pin_faction", "fac_kingdom_1"),
                         (assign, "$pin_party_template", "pt_swadian_scouts"),
                         (assign, "$pin_limit", "$peak_swadian_scouts"),
                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
                    ]),

  (10.2, 0, 0.0, [],
                     [
                         (assign, "$pin_faction", "fac_kingdom_1"),
                         (assign, "$pin_party_template", "pt_swadian_patrol"),
                         (assign, "$pin_limit", "$peak_swadian_harassers"),
                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
                    ]),

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_swadians"),
#                         (assign, "$pin_party_template", "pt_swadian_war_party"),
#                         (assign, "$pin_limit", "$peak_swadian_war_parties"),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),
#Vaegirs
#  (0.0, 0.0, ti_once, [], [(assign,"$peak_vaegir_foragers",4)]),
  (0.0, 0.0, ti_once, [], [(assign,"$peak_vaegir_scouts",5)]),
  (0.0, 0.0, ti_once, [], [(assign,"$peak_vaegir_harassers",4)]),
#  (0.0, 0.0, ti_once, [], [(assign,"$peak_vaegir_war_parties",2)]),
  

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_vaegirs"),
#                         (assign, "$pin_party_template", "pt_vaegir_foragers"),
#                         (assign, "$pin_limit", "$peak_vaegir_foragers"),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),

  (10.2, 0, 0.0, [],
                     [
                         (assign, "$pin_faction", "fac_kingdom_2"),
                         (assign, "$pin_party_template", "pt_vaegir_scouts"),
                         (assign, "$pin_limit", "$peak_vaegir_scouts"),
                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
                    ]),

  (10.2, 0, 0.0, [],
                     [
                         (assign, "$pin_faction", "fac_kingdom_2"),
                         (assign, "$pin_party_template", "pt_vaegir_patrol"),
                         (assign, "$pin_limit", "$peak_vaegir_harassers"),
                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
                    ]),

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_vaegirs"),
#                         (assign, "$pin_party_template", "pt_vaegir_war_party"),
#                         (assign, "$pin_limit", "$peak_vaegir_war_parties"),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),

#Villains etc.
#  (14.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_sea_raiders"),
#                         (assign, "$pin_party_template", "pt_sea_raiders"),
#                         (assign, "$pin_limit", 5),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),

#Khergits
  (0.0, 0.0, ti_once, [], [(assign,"$peak_khergit_scouts",7)]),
  (0.0, 0.0, ti_once, [], [(assign,"$peak_khergit_harassers",4)]),
  
  (10.2, 0, 0.0, [],
                     [
                         (assign, "$pin_faction", "fac_kingdom_3"),
                         (assign, "$pin_party_template", "pt_khergit_scouts"),
                         (assign, "$pin_limit", "$peak_khergit_scouts"),
                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
                    ]),

  (10.2, 0, 0.0, [],
                     [
                         (assign, "$pin_faction", "fac_kingdom_3"),
                         (assign, "$pin_party_template", "pt_khergit_patrol"),
                         (assign, "$pin_limit", "$peak_khergit_harassers"),
                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
                    ]),

#Nords
  (0.0, 0.0, ti_once, [], [(assign,"$peak_nord_scouts",4)]),
  (0.0, 0.0, ti_once, [], [(assign,"$peak_nord_harassers",7)]),
  
  (10.2, 0, 0.0, [],
                     [
                         (assign, "$pin_faction", "fac_kingdom_4"),
                         (assign, "$pin_party_template", "pt_nord_scouts"),
                         (assign, "$pin_limit", "$peak_nord_scouts"),
                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
                    ]),

  (10.2, 0, 0.0, [],
                     [
                         (assign, "$pin_faction", "fac_kingdom_4"),
                         (assign, "$pin_party_template", "pt_nord_patrol"),
                         (assign, "$pin_limit", "$peak_nord_harassers"),
                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
                    ]),
					
#Rhodoks
  (0.0, 0.0, ti_once, [], [(assign,"$peak_rhodok_scouts",9)]),
  (0.0, 0.0, ti_once, [], [(assign,"$peak_rhodok_harassers",4)]),

# v585 - Josef - changed to have Rhodok patrols in kingdom_5 rather than kingdom_3 (typo)  
  (10.2, 0, 0.0, [],
                     [
                         #(assign, "$pin_faction", "fac_kingdom_3"),
						 (assign, "$pin_faction", "fac_kingdom_5"),
                         (assign, "$pin_party_template", "pt_rhodok_scouts"),
                         (assign, "$pin_limit", "$peak_rhodok_scouts"),
                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
                    ]),

  (10.2, 0, 0.0, [],
                     [
                         #(assign, "$pin_faction", "fac_kingdom_3"),
						 (assign, "$pin_faction", "fac_kingdom_5"),
                         (assign, "$pin_party_template", "pt_rhodok_patrol"),
                         (assign, "$pin_limit", "$peak_rhodok_harassers"),
                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
                    ]),

# v585 -Josef - end					

# Sarranids
#Rhodoks
  (0.0, 0.0, ti_once, [], [(assign,"$peak_sarranid_scouts",9)]),
  (0.0, 0.0, ti_once, [], [(assign,"$peak_sarranid_harassers",4)]),


  (10.2, 0, 0.0, [],
                     [
						 (assign, "$pin_faction", "fac_kingdom_6"),
                         (assign, "$pin_party_template", "pt_sarranid_scouts"),
                         (assign, "$pin_limit", "$peak_sarranid_scouts"),
                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
                    ]),

  (10.2, 0, 0.0, [],
                     [
						 (assign, "$pin_faction", "fac_kingdom_6"),
                         (assign, "$pin_party_template", "pt_sarranid_patrol"),
                         (assign, "$pin_limit", "$peak_sarranid_harassers"),
                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
                    ]),

#
 (10.1, 0, 0.0, [],
                     [
						 (assign, "$pin_faction", "fac_kingdom_1"),
                         (assign, "$pin_party_template", "pt_crusaders"),
                         (assign, "$pin_limit", 2),
                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
                    ]),
 (10.1, 0, 0.0, [],
                     [
						 (assign, "$pin_faction", "fac_kingdom_2"),
                         (assign, "$pin_party_template", "pt_ivory_guards"),
                         (assign, "$pin_limit", 2),
                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
                    ]),
 (10.1, 0, 0.0, [],
                     [
						 (assign, "$pin_faction", "fac_kingdom_3"),
                         (assign, "$pin_party_template", "pt_red_horde"),
                         (assign, "$pin_limit", 1),
                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
                    ]),
 (10.1, 0, 0.0, [],
                     [
						 (assign, "$pin_faction", "fac_kingdom_3"),
                         (assign, "$pin_party_template", "pt_blue_horde"),
                         (assign, "$pin_limit", 1),
                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
                    ]),
 (10.1, 0, 0.0, [],
                     [
						 (assign, "$pin_faction", "fac_kingdom_4"),
                         (assign, "$pin_party_template", "pt_raiders"),
                         (assign, "$pin_limit", 2),
                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
                    ]),
 (10.1, 0, 0.0, [],
                     [
						 (assign, "$pin_faction", "fac_kingdom_6"),
                         (assign, "$pin_party_template", "pt_mamlukes"),
                         (assign, "$pin_limit", 2),
                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
                    ]),
					
 (10.1, 0, 0.0, [],
                     [
						 (assign, "$pin_faction", "fac_kingdom_5"),
                         (assign, "$pin_party_template", "pt_councilmen"),
                         (assign, "$pin_limit", 2),
                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
                    ]),

# NE End scouts

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_swadians"),
#                         (assign, "$pin_party_template", "pt_swadian_foragers"),
#                         (assign, "$pin_limit", "$peak_swadian_foragers"),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_swadians"),
#                         (assign, "$pin_party_template", "pt_swadian_scouts"),
#                         (assign, "$pin_limit", "$peak_swadian_scouts"),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_swadians"),
#                         (assign, "$pin_party_template", "pt_swadian_patrol"),
#                         (assign, "$pin_limit", "$peak_swadian_harassers"),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_swadians"),
#                         (assign, "$pin_party_template", "pt_swadian_war_party"),
#                         (assign, "$pin_limit", "$peak_swadian_war_parties"),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),
#Vaegirs
#  (0.0, 0.0, ti_once, [], [(assign,"$peak_vaegir_foragers",4)]),
#  (0.0, 0.0, ti_once, [], [(assign,"$peak_vaegir_scouts",4)]),
#  (0.0, 0.0, ti_once, [], [(assign,"$peak_vaegir_harassers",3)]),
#  (0.0, 0.0, ti_once, [], [(assign,"$peak_vaegir_war_parties",2)]),
  

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_vaegirs"),
#                         (assign, "$pin_party_template", "pt_vaegir_foragers"),
#                         (assign, "$pin_limit", "$peak_vaegir_foragers"),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_vaegirs"),
#                         (assign, "$pin_party_template", "pt_vaegir_scouts"),
#                         (assign, "$pin_limit", "$peak_vaegir_scouts"),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_vaegirs"),
#                         (assign, "$pin_party_template", "pt_vaegir_patrol"),
#                         (assign, "$pin_limit", "$peak_vaegir_harassers"),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),

#  (10.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_vaegirs"),
#                         (assign, "$pin_party_template", "pt_vaegir_war_party"),
#                         (assign, "$pin_limit", "$peak_vaegir_war_parties"),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),

#Villains etc.
#  (14.2, 0, 0.0, [],
#                     [
#                         (assign, "$pin_faction", "fac_sea_raiders"),
#                         (assign, "$pin_party_template", "pt_sea_raiders"),
#                         (assign, "$pin_limit", 5),
#                         (call_script,"script_cf_spawn_party_at_faction_town_if_below_limit"),
#                    ]),


#
##  (10.1, 0, 0.0, [],
##                     [
##                         (assign, "$pin_party_template", "pt_refugees"),
##                         (assign, "$pin_limit", 5),
##                         (call_script,"script_cf_spawn_party_at_random_town_if_below_limit"),
##                    ]),
##
##  (10.1, 0, 0.0, [],
##                     [
##                         (assign, "$pin_party_template", "pt_farmers"),
##                         (assign, "$pin_limit", 6),
##                         (call_script,"script_cf_spawn_party_at_random_town_if_below_limit"),
##                    ]),

#  [1.0, 96.0, ti_once, [], [[assign,"$peak_dark_hunters",3]]],
  
##  (10.1, 0, 0.0, [],
##                     [
##                         (assign, "$pin_party_template", "pt_dark_hunters"),
##                         (assign, "$pin_limit", "$peak_dark_hunters"),
##                         (call_script,"script_cf_spawn_party_at_random_town_if_below_limit"),
##                    ]),

#Companion quests

##  (0, 0, ti_once,
##   [
##       (entering_town,"p_town_1"),
##       (main_party_has_troop,"trp_borcha"),
##       (eq,"$borcha_freed",0)
##    ],
##   
##   [
##       (assign,"$borcha_arrive_sargoth_as_prisoner", 1),
##       (start_map_conversation, "trp_borcha", -1)
##    ]
##   ),
##
##  (1, 0, ti_once,
##   [
##      (map_free,0),
##      (eq,"$borcha_asked_for_freedom",0),
##      (main_party_has_troop,"trp_borcha")
##    ],
##   [
##       (start_map_conversation, "trp_borcha", -1)
##    ]
##   ),
##  
##  (2, 0, ti_once,
##   [
##      (map_free, 0),
##      (neq,"$borcha_asked_for_freedom",0),
##      (eq,"$borcha_freed",0),
##      (main_party_has_troop,"trp_borcha")
##    ],
##   [
##       (start_map_conversation, "trp_borcha", -1),
##    ]
##   ),

##### TODO: QUESTS COMMENT OUT BEGIN

###########################################################################
### Random Governer Quest triggers
###########################################################################

# Incriminate Loyal Advisor quest
  (0.2, 0.0, 0.0,
   [
       (check_quest_active, "qst_incriminate_loyal_commander"),
       (neg|check_quest_concluded, "qst_incriminate_loyal_commander"),
       (quest_slot_eq, "qst_incriminate_loyal_commander", slot_quest_current_state, 2),
       (quest_get_slot, ":quest_target_center", "qst_incriminate_loyal_commander", slot_quest_target_center),
       (quest_get_slot, ":quest_target_party", "qst_incriminate_loyal_commander", slot_quest_target_party),
       (try_begin),
         (neg|party_is_active, ":quest_target_party"),
         (quest_set_slot, "qst_incriminate_loyal_commander", slot_quest_current_state, 3),
         (call_script, "script_fail_quest", "qst_incriminate_loyal_commander"),
       (else_try),
         (party_is_in_town, ":quest_target_party", ":quest_target_center"),
         (remove_party, ":quest_target_party"),
         (quest_set_slot, "qst_incriminate_loyal_commander", slot_quest_current_state, 3),
         (quest_get_slot, ":quest_object_troop", "qst_incriminate_loyal_commander", slot_quest_object_troop),
         (assign, ":num_available_factions", 0),
         (try_for_range, ":faction_no", kingdoms_begin, kingdoms_end),
           (faction_slot_eq, ":faction_no", slot_faction_state, sfs_active),
           (neq, ":faction_no", "fac_player_supporters_faction"),
           (neg|quest_slot_eq, "qst_incriminate_loyal_commander", slot_quest_target_faction, ":faction_no"),
           (val_add, ":num_available_factions", 1),
         (try_end),
         (try_begin),
           (gt, ":num_available_factions", 0),
           (store_random_in_range, ":random_faction", 0, ":num_available_factions"),
           (assign, ":target_faction", -1),
           (try_for_range, ":faction_no", kingdoms_begin, kingdoms_end),
             (eq, ":target_faction", -1),
             (faction_slot_eq, ":faction_no", slot_faction_state, sfs_active),
             (neq, ":faction_no", "fac_player_supporters_faction"),
             (neg|quest_slot_eq, "qst_incriminate_loyal_commander", slot_quest_target_faction, ":faction_no"),
             (val_sub, ":random_faction", 1),
             (lt, ":random_faction", 0),
             (assign, ":target_faction", ":faction_no"),
           (try_end),
         (try_end),
         (try_begin),
           (gt, ":target_faction", 0),
           (call_script, "script_change_troop_faction", ":quest_object_troop", ":target_faction"),
         (else_try),
           (call_script, "script_change_troop_faction", ":quest_object_troop", "fac_robber_knights"),
         (try_end),
         (call_script, "script_succeed_quest", "qst_incriminate_loyal_commander"),
       (try_end),
    ],
   []
   ),
# Runaway Peasants quest
  (0.2, 0.0, 0.0,
   [
       (check_quest_active, "qst_bring_back_runaway_serfs"),
       (neg|check_quest_concluded, "qst_bring_back_runaway_serfs"),
       (quest_get_slot, ":quest_object_center", "qst_bring_back_runaway_serfs", slot_quest_object_center),
       (quest_get_slot, ":quest_target_center", "qst_bring_back_runaway_serfs", slot_quest_target_center),
       (try_begin),
         (party_is_active, "$qst_bring_back_runaway_serfs_party_1"),
         (try_begin),
           (party_is_in_town, "$qst_bring_back_runaway_serfs_party_1", ":quest_target_center"),
           (remove_party, "$qst_bring_back_runaway_serfs_party_1"),
           (val_add, "$qst_bring_back_runaway_serfs_num_parties_fleed", 1),
         (else_try),
           (party_is_in_town, "$qst_bring_back_runaway_serfs_party_1", ":quest_object_center"),
           (remove_party, "$qst_bring_back_runaway_serfs_party_1"),
           (val_add, "$qst_bring_back_runaway_serfs_num_parties_returned", 1),
         (else_try),
           (store_distance_to_party_from_party, ":cur_distance", "p_main_party", "$qst_bring_back_runaway_serfs_party_1"),
           (gt, ":cur_distance", 3),
           (party_set_ai_object, "$qst_bring_back_runaway_serfs_party_1", ":quest_target_center"),
         (try_end),
       (try_end),
       (try_begin),
         (party_is_active, "$qst_bring_back_runaway_serfs_party_2"),
         (try_begin),
           (party_is_in_town, "$qst_bring_back_runaway_serfs_party_2", ":quest_target_center"),
           (remove_party, "$qst_bring_back_runaway_serfs_party_2"),
           (val_add, "$qst_bring_back_runaway_serfs_num_parties_fleed", 1),
         (else_try),
           (party_is_in_town, "$qst_bring_back_runaway_serfs_party_2", ":quest_object_center"),
           (remove_party, "$qst_bring_back_runaway_serfs_party_2"),
           (val_add, "$qst_bring_back_runaway_serfs_num_parties_returned", 1),
         (else_try),
           (store_distance_to_party_from_party, ":cur_distance", "p_main_party", "$qst_bring_back_runaway_serfs_party_2"),
           (gt, ":cur_distance", 3),
           (party_set_ai_object, "$qst_bring_back_runaway_serfs_party_2", ":quest_target_center"),
         (try_end),
       (try_end),
       (try_begin),
         (party_is_active, "$qst_bring_back_runaway_serfs_party_3"),
         (try_begin),
           (party_is_in_town, "$qst_bring_back_runaway_serfs_party_3", ":quest_target_center"),
           (remove_party, "$qst_bring_back_runaway_serfs_party_3"),
           (val_add, "$qst_bring_back_runaway_serfs_num_parties_fleed", 1),
         (else_try),
           (party_is_in_town, "$qst_bring_back_runaway_serfs_party_3", ":quest_object_center"),
           (remove_party, "$qst_bring_back_runaway_serfs_party_3"),
           (val_add, "$qst_bring_back_runaway_serfs_num_parties_returned", 1),
         (else_try),
           (store_distance_to_party_from_party, ":cur_distance", "p_main_party", "$qst_bring_back_runaway_serfs_party_3"),
           (gt, ":cur_distance", 3),
           (party_set_ai_object, "$qst_bring_back_runaway_serfs_party_3", ":quest_target_center"),
         (try_end),
       (try_end),
       (assign, ":sum_removed", "$qst_bring_back_runaway_serfs_num_parties_returned"),
       (val_add, ":sum_removed", "$qst_bring_back_runaway_serfs_num_parties_fleed"),
       (ge, ":sum_removed", 3),
       (try_begin),
         (ge, "$qst_bring_back_runaway_serfs_num_parties_returned", 3),
         (call_script, "script_succeed_quest", "qst_bring_back_runaway_serfs"),
       (else_try),
         (eq, "$qst_bring_back_runaway_serfs_num_parties_returned", 0),
         (call_script, "script_fail_quest", "qst_bring_back_runaway_serfs"),
       (else_try),
         (call_script, "script_conclude_quest", "qst_bring_back_runaway_serfs"),
       (try_end),
    ],
   []
   ),
   
   # NE quests
  
#v585 Josef Elites			  

				   
  (0.3, 0.0, 1.1, [(check_quest_active, "qst_kill_elites"),
                   (neg|check_quest_failed, "qst_kill_elites"),
				   (try_begin),
				   (eq, "$elite_faction", "fac_kingdom_1"),
				   (store_num_parties_destroyed, ":cur_eliminated", "pt_elites_fac1"),
				   (lt, "$qst_elites_eliminated", ":cur_eliminated"),
                   (store_num_parties_destroyed_by_player, ":cur_eliminated_by_player", "pt_elites_fac1"),
                   (eq, ":cur_eliminated_by_player", "$qst_elites_eliminated_by_player"),	
				   (display_message, "str_elites_eliminated_by_another"),
                   (call_script, "script_abort_quest", "qst_kill_elites", 2),
				   (remove_party, "pt_elites_fac1"),
				   (assign, "$elite_faction", -1), 				   
				   (else_try),
				   (eq, "$elite_faction", "fac_kingdom_2"),
				   
                   (store_num_parties_destroyed, ":cur_eliminated", "pt_elites_fac2"),
                   (lt, "$qst_elites_eliminated", ":cur_eliminated"),
                   (store_num_parties_destroyed_by_player, ":cur_eliminated_by_player", "pt_elites_fac2"),
                   (eq, ":cur_eliminated_by_player", "$qst_elites_eliminated_by_player"),	
				  (display_message, "str_elites_eliminated_by_another"),
                   (call_script, "script_abort_quest", "qst_kill_elites", 2),
				   (remove_party, "pt_elites_fac2"),
				   (assign, "$elite_faction", -1), 				   
				   (else_try),				   
				   (eq, "$elite_faction", "fac_kingdom_3"),
				   
                   (store_num_parties_destroyed, ":cur_eliminated", "pt_elites_fac3"),
                   (lt, "$qst_elites_eliminated", ":cur_eliminated"),
                   (store_num_parties_destroyed_by_player, ":cur_eliminated_by_player", "pt_elites_fac3"),
                   (eq, ":cur_eliminated_by_player", "$qst_elites_eliminated_by_player"),
				  (display_message, "str_elites_eliminated_by_another"),
                   (call_script, "script_abort_quest", "qst_kill_elites", 2),
				   (remove_party, "pt_elites_fac3"),
				   (assign, "$elite_faction", -1), 				   
				   (else_try),		
				   (eq, "$elite_faction", "fac_kingdom_4"),
				   
                   (store_num_parties_destroyed, ":cur_eliminated", "pt_elites_fac4"),
                   (lt, "$qst_elites_eliminated", ":cur_eliminated"),
                   (store_num_parties_destroyed_by_player, ":cur_eliminated_by_player", "pt_elites_fac4"),
                   (eq, ":cur_eliminated_by_player", "$qst_elites_eliminated_by_player"),	
				  (display_message, "str_elites_eliminated_by_another"),
                   (call_script, "script_abort_quest", "qst_kill_elites", 2),
				   (remove_party, "pt_elites_fac4"),
				   (assign, "$elite_faction", -1), 				   
				   (else_try),		
				   (eq, "$elite_faction", "fac_kingdom_5"),
				   
                   (store_num_parties_destroyed, ":cur_eliminated", "pt_elites_fac5"),
                   (lt, "$qst_elites_eliminated", ":cur_eliminated"),
                   (store_num_parties_destroyed_by_player, ":cur_eliminated_by_player", "pt_elites_fac5"),
                   (eq, ":cur_eliminated_by_player", "$qst_elites_eliminated_by_player"),	
				   (display_message, "str_elites_eliminated_by_another"),
                   (call_script, "script_abort_quest", "qst_kill_elites", 2),
				   (remove_party, "pt_elites_fac5"),
				   (assign, "$elite_faction", -1), 	
				   (else_try),		
				   (eq, "$elite_faction", "fac_kingdom_6"),
				   
                   (store_num_parties_destroyed, ":cur_eliminated", "pt_elites_fac6"),
                   (lt, "$qst_elites_eliminated", ":cur_eliminated"),
                   (store_num_parties_destroyed_by_player, ":cur_eliminated_by_player", "pt_elites_fac6"),
                   (eq, ":cur_eliminated_by_player", "$qst_elites_eliminated_by_player"),	
				   (display_message, "str_elites_eliminated_by_another"),
                   (call_script, "script_abort_quest", "qst_kill_elites", 2),
				   (remove_party, "pt_elites_fac6"),
				   (assign, "$elite_faction", -1), 					   
				   (try_end),						   				   

                   ],
                  [
				  # (display_message, "str_elites_eliminated_by_another"),
                   # (call_script, "script_abort_quest", "qst_kill_elites", 2),
				   # (assign, "$elite_faction", -1), 
                   ]),

  (0.3, 0.0, 1.1, [(check_quest_active, "qst_kill_elites"),
                   (neg|check_quest_succeeded, "qst_kill_elites"),
				   (try_begin),
				   (eq, "$elite_faction", "fac_kingdom_1"),
                   (store_num_parties_destroyed, ":cur_eliminated", "pt_elites_fac1"),
                   (lt, "$qst_elites_eliminated", ":cur_eliminated"),
                   (store_num_parties_destroyed_by_player, ":cur_eliminated_by_player", "pt_elites_fac1"),
                   (neq, ":cur_eliminated_by_player", "$qst_elites_eliminated_by_player"),			
				  (call_script, "script_succeed_quest", "qst_kill_elites"),
				    (assign, "$elite_faction", -1), 				   
				   (else_try),
				   (eq, "$elite_faction", "fac_kingdom_2"),
                   (store_num_parties_destroyed, ":cur_eliminated", "pt_elites_fac2"),
                   (lt, "$qst_elites_eliminated", ":cur_eliminated"),
                   (store_num_parties_destroyed_by_player, ":cur_eliminated_by_player", "pt_elites_fac2"),
                   (neq, ":cur_eliminated_by_player", "$qst_elites_eliminated_by_player"),	
				   (call_script, "script_succeed_quest", "qst_kill_elites"),
				    (assign, "$elite_faction", -1), 				   
				   (else_try),				   
				   (eq, "$elite_faction", "fac_kingdom_3"),
                   (store_num_parties_destroyed, ":cur_eliminated", "pt_elites_fac3"),
                   (lt, "$qst_elites_eliminated", ":cur_eliminated"),
                   (store_num_parties_destroyed_by_player, ":cur_eliminated_by_player", "pt_elites_fac3"),
                   (neq, ":cur_eliminated_by_player", "$qst_elites_eliminated_by_player"),	
				   (call_script, "script_succeed_quest", "qst_kill_elites"),
				    (assign, "$elite_faction", -1), 				   
				   (else_try),		
				   (eq, "$elite_faction", "fac_kingdom_4"),
                   (store_num_parties_destroyed, ":cur_eliminated", "pt_elites_fac4"),
                   (lt, "$qst_elites_eliminated", ":cur_eliminated"),
                   (store_num_parties_destroyed_by_player, ":cur_eliminated_by_player", "pt_elites_fac4"),
                   (neq, ":cur_eliminated_by_player", "$qst_elites_eliminated_by_player"),		
				   (call_script, "script_succeed_quest", "qst_kill_elites"),
				    (assign, "$elite_faction", -1), 				   
				   (else_try),		
				   (eq, "$elite_faction", "fac_kingdom_5"),
                   (store_num_parties_destroyed, ":cur_eliminated", "pt_elites_fac5"),
                   (lt, "$qst_elites_eliminated", ":cur_eliminated"),
                   (store_num_parties_destroyed_by_player, ":cur_eliminated_by_player", "pt_elites_fac5"),
                   (neq, ":cur_eliminated_by_player", "$qst_elites_eliminated_by_player"),		
				   (call_script, "script_succeed_quest", "qst_kill_elites"),
				    (assign, "$elite_faction", -1), 		
				   (else_try),		
				   (eq, "$elite_faction", "fac_kingdom_6"),
                   (store_num_parties_destroyed, ":cur_eliminated", "pt_elites_fac6"),
                   (lt, "$qst_elites_eliminated", ":cur_eliminated"),
                   (store_num_parties_destroyed_by_player, ":cur_eliminated_by_player", "pt_elites_fac6"),
                   (neq, ":cur_eliminated_by_player", "$qst_elites_eliminated_by_player"),		
				   (call_script, "script_succeed_quest", "qst_kill_elites"),
				    (assign, "$elite_faction", -1), 						
				   (try_end),	
                   ],
                  [
				  # (call_script, "script_succeed_quest", "qst_kill_elites"),
				    # (assign, "$elite_faction", -1), 
					]),
	
# v585 Josef smugglers

  (0.2, 0.0, 0.0,
   [
       (check_quest_active, "qst_hunt_down_smugglers"),
       (neg|check_quest_concluded, "qst_hunt_down_smugglers"),
       (quest_get_slot, ":quest_object_center", "qst_hunt_down_smugglers", slot_quest_object_center),
       (quest_get_slot, ":quest_target_center", "qst_hunt_down_smugglers", slot_quest_target_center),
       (try_begin),
         (party_is_active, "$qst_hunt_down_smugglers_party_1"),
         (try_begin),
           (party_is_in_town, "$qst_hunt_down_smugglers_party_1", ":quest_target_center"),
           (remove_party, "$qst_hunt_down_smugglers_party_1"),
           (val_add, "$qst_hunt_down_smugglers_num_parties_fleed", 1),
         (else_try),
           (party_is_in_town, "$qst_hunt_down_smugglers_party_1", ":quest_object_center"),
           (remove_party, "$qst_hunt_down_smugglers_party_1"),
           (val_add, "$qst_hunt_down_smugglers_num_parties_returned", 1),
         (else_try),
           (store_distance_to_party_from_party, ":cur_distance", "p_main_party", "$qst_hunt_down_smugglers_party_1"),
           (gt, ":cur_distance", 3),
           (party_set_ai_object, "$qst_hunt_down_smugglers_party_1", ":quest_target_center"),
         (try_end),
       (try_end),
       (try_begin),
         (party_is_active, "$qst_hunt_down_smugglers_party_2"),
         (try_begin),
           (party_is_in_town, "$qst_hunt_down_smugglers_party_2", ":quest_target_center"),
           (remove_party, "$qst_hunt_down_smugglers_party_2"),
           (val_add, "$qst_hunt_down_smugglers_num_parties_fleed", 1),
         (else_try),
           (party_is_in_town, "$qst_hunt_down_smugglers_party_2", ":quest_object_center"),
           (remove_party, "$qst_hunt_down_smugglers_party_2"),
           (val_add, "$qst_hunt_down_smugglers_num_parties_returned", 1),
         (else_try),
           (store_distance_to_party_from_party, ":cur_distance", "p_main_party", "$qst_hunt_down_smugglers_party_2"),
           (gt, ":cur_distance", 3),
           (party_set_ai_object, "$qst_hunt_down_smugglers_party_2", ":quest_target_center"),
         (try_end),
       (try_end),
       (try_begin),
         (party_is_active, "$qst_hunt_down_smugglers_party_3"),
         (try_begin),
           (party_is_in_town, "$qst_hunt_down_smugglers_party_3", ":quest_target_center"),
           (remove_party, "$qst_hunt_down_smugglers_party_3"),
           (val_add, "$qst_hunt_down_smugglers_num_parties_fleed", 1),
         (else_try),
           (party_is_in_town, "$qst_hunt_down_smugglers_party_3", ":quest_object_center"),
           (remove_party, "$qst_hunt_down_smugglers_party_3"),
           (val_add, "$qst_hunt_down_smugglers_num_parties_returned", 1),
         (else_try),
           (store_distance_to_party_from_party, ":cur_distance", "p_main_party", "$qst_hunt_down_smugglers_party_3"),
           (gt, ":cur_distance", 3),
           (party_set_ai_object, "$qst_hunt_down_smugglers_party_3", ":quest_target_center"),
         (try_end),
       (try_end),
       (assign, ":sum_removed", "$qst_hunt_down_smugglers_num_parties_returned"),
       (val_add, ":sum_removed", "$qst_hunt_down_smugglers_num_parties_fleed"),
       (ge, ":sum_removed", 3),
       (try_begin),
         (ge, "$qst_hunt_down_smugglers_num_parties_returned", 3),
         (call_script, "script_succeed_quest", "qst_hunt_down_smugglers"),
       (else_try),
         (eq, "$qst_hunt_down_smugglers_num_parties_returned", 0),
         (call_script, "script_fail_quest", "qst_hunt_down_smugglers"),
       (else_try),
         (call_script, "script_conclude_quest", "qst_hunt_down_smugglers"),
       (try_end),
    ],
   []
   ),	   
   
   # NE end quests
### Defend Nobles Against Peasants quest
##  (0.2, 0.0, 0.0,
##   [
##       (check_quest_active, "qst_defend_nobles_against_peasants"),
##       (neg|check_quest_succeeded, "qst_defend_nobles_against_peasants"),
##       (neg|check_quest_failed, "qst_defend_nobles_against_peasants"),
##       (quest_get_slot, ":quest_target_center", "qst_defend_nobles_against_peasants", slot_quest_target_center),
##       (assign, ":num_active_parties", 0),
##       (try_begin),
##         (gt, "$qst_defend_nobles_against_peasants_noble_party_1", 0),
##         (party_is_active, "$qst_defend_nobles_against_peasants_noble_party_1"),
##         (val_add, ":num_active_parties", 1),
##         (party_is_in_town, "$qst_defend_nobles_against_peasants_noble_party_1", ":quest_target_center"),
##         (remove_party, "$qst_defend_nobles_against_peasants_noble_party_1"),
##         (party_get_num_companions, ":num_companions", "$qst_defend_nobles_against_peasants_noble_party_1"),
##         (val_add, "$qst_defend_nobles_against_peasants_num_nobles_saved", ":num_companions"),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_defend_nobles_against_peasants_noble_party_2", 0),
##         (party_is_active, "$qst_defend_nobles_against_peasants_noble_party_2"),
##         (val_add, ":num_active_parties", 1),
##         (party_is_in_town, "$qst_defend_nobles_against_peasants_noble_party_2", ":quest_target_center"),
##         (remove_party, "$qst_defend_nobles_against_peasants_noble_party_2"),
##         (party_get_num_companions, ":num_companions", "$qst_defend_nobles_against_peasants_noble_party_2"),
##         (val_add, "$qst_defend_nobles_against_peasants_num_nobles_saved", ":num_companions"),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_defend_nobles_against_peasants_noble_party_3", 0),
##         (party_is_active, "$qst_defend_nobles_against_peasants_noble_party_3"),
##         (val_add, ":num_active_parties", 1),
##         (party_is_in_town, "$qst_defend_nobles_against_peasants_noble_party_3", ":quest_target_center"),
##         (remove_party, "$qst_defend_nobles_against_peasants_noble_party_3"),
##         (party_get_num_companions, ":num_companions", "$qst_defend_nobles_against_peasants_noble_party_3"),
##         (val_add, "$qst_defend_nobles_against_peasants_num_nobles_saved", ":num_companions"),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_defend_nobles_against_peasants_noble_party_4", 0),
##         (party_is_active, "$qst_defend_nobles_against_peasants_noble_party_4"),
##         (val_add, ":num_active_parties", 1),
##         (party_is_in_town, "$qst_defend_nobles_against_peasants_noble_party_4", ":quest_target_center"),
##         (remove_party, "$qst_defend_nobles_against_peasants_noble_party_4"),
##         (party_get_num_companions, ":num_companions", "$qst_defend_nobles_against_peasants_noble_party_4"),
##         (val_add, "$qst_defend_nobles_against_peasants_num_nobles_saved", ":num_companions"),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_defend_nobles_against_peasants_noble_party_5", 0),
##         (party_is_active, "$qst_defend_nobles_against_peasants_noble_party_5"),
##         (val_add, ":num_active_parties", 1),
##         (party_is_in_town, "$qst_defend_nobles_against_peasants_noble_party_5", ":quest_target_center"),
##         (remove_party, "$qst_defend_nobles_against_peasants_noble_party_5"),
##         (party_get_num_companions, ":num_companions", "$qst_defend_nobles_against_peasants_noble_party_5"),
##         (val_add, "$qst_defend_nobles_against_peasants_num_nobles_saved", ":num_companions"),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_defend_nobles_against_peasants_noble_party_6", 0),
##         (party_is_active, "$qst_defend_nobles_against_peasants_noble_party_6"),
##         (val_add, ":num_active_parties", 1),
##         (party_is_in_town, "$qst_defend_nobles_against_peasants_noble_party_6", ":quest_target_center"),
##         (remove_party, "$qst_defend_nobles_against_peasants_noble_party_6"),
##         (party_get_num_companions, ":num_companions", "$qst_defend_nobles_against_peasants_noble_party_6"),
##         (val_add, "$qst_defend_nobles_against_peasants_num_nobles_saved", ":num_companions"),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_defend_nobles_against_peasants_noble_party_7", 0),
##         (party_is_active, "$qst_defend_nobles_against_peasants_noble_party_7"),
##         (val_add, ":num_active_parties", 1),
##         (party_is_in_town, "$qst_defend_nobles_against_peasants_noble_party_7", ":quest_target_center"),
##         (remove_party, "$qst_defend_nobles_against_peasants_noble_party_7"),
##         (party_get_num_companions, ":num_companions", "$qst_defend_nobles_against_peasants_noble_party_7"),
##         (val_add, "$qst_defend_nobles_against_peasants_num_nobles_saved", ":num_companions"),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_defend_nobles_against_peasants_noble_party_8", 0),
##         (party_is_active, "$qst_defend_nobles_against_peasants_noble_party_8"),
##         (val_add, ":num_active_parties", 1),
##         (party_is_in_town, "$qst_defend_nobles_against_peasants_noble_party_8", ":quest_target_center"),
##         (remove_party, "$qst_defend_nobles_against_peasants_noble_party_8"),
##         (party_get_num_companions, ":num_companions", "$qst_defend_nobles_against_peasants_noble_party_8"),
##         (val_add, "$qst_defend_nobles_against_peasants_num_nobles_saved", ":num_companions"),
##       (try_end),
##       (eq, ":num_active_parties", 0),
##       (try_begin),
##         (store_div, ":limit", "$qst_defend_nobles_against_peasants_num_nobles_to_save", 2),
##         (ge, "$qst_defend_nobles_against_peasants_num_nobles_saved", ":limit"),
##         (call_script, "script_succeed_quest", "qst_defend_nobles_against_peasants"),
##       (else_try),
##         (call_script, "script_fail_quest", "qst_defend_nobles_against_peasants"),
##       (try_end),
##    ],
##   []
##   ),
### Capture Conspirators quest
##  (0.15, 0.0, 0.0,
##   [
##       (check_quest_active, "qst_capture_conspirators"),
##       (neg|check_quest_succeeded, "qst_capture_conspirators"),
##       (neg|check_quest_failed, "qst_capture_conspirators"),
##       (quest_get_slot, ":quest_target_center", "qst_capture_conspirators", slot_quest_target_center),
##       (quest_get_slot, ":faction_no", "qst_capture_conspirators", slot_quest_target_faction),
##       (try_begin),
##         (gt, "$qst_capture_conspirators_num_parties_to_spawn", "$qst_capture_conspirators_num_parties_spawned"),
##         (store_random_in_range, ":random_no", 0, 100),
##         (lt, ":random_no", 20),
##         (set_spawn_radius, 3),
##         (spawn_around_party,":quest_target_center","pt_conspirator"),
##         (val_add, "$qst_capture_conspirators_num_parties_spawned", 1),
##         (party_get_num_companions, ":num_companions", reg0),
##         (val_add, "$qst_capture_conspirators_num_troops_to_capture", ":num_companions"),
##         (party_set_ai_behavior, reg0, ai_bhvr_travel_to_party),
##         (party_set_ai_object, reg0, "$qst_capture_conspirators_party_1"),
##         (party_set_flags, reg0, pf_default_behavior, 0),
##         (try_begin),
##           (le, "$qst_capture_conspirators_party_2", 0),
##           (assign, "$qst_capture_conspirators_party_2", reg0),
##         (else_try),
##           (le, "$qst_capture_conspirators_party_3", 0),
##           (assign, "$qst_capture_conspirators_party_3", reg0),
##         (else_try),
##           (le, "$qst_capture_conspirators_party_4", 0),
##           (assign, "$qst_capture_conspirators_party_4", reg0),
##         (else_try),
##           (le, "$qst_capture_conspirators_party_5", 0),
##           (assign, "$qst_capture_conspirators_party_5", reg0),
##         (else_try),
##           (le, "$qst_capture_conspirators_party_6", 0),
##           (assign, "$qst_capture_conspirators_party_6", reg0),
##         (else_try),
##           (le, "$qst_capture_conspirators_party_7", 0),
##           (assign, "$qst_capture_conspirators_party_7", reg0),
##         (try_end),
##       (try_end),
##
##       (assign, ":num_active_parties", 0),
##
##       (try_begin),
##         (gt, "$qst_capture_conspirators_party_1", 0),
##         (party_is_active, "$qst_capture_conspirators_party_1"),
##         (val_add, ":num_active_parties", 1),
##         (try_begin),
##           (party_is_in_any_town, "$qst_capture_conspirators_party_1"),
##           (remove_party, "$qst_capture_conspirators_party_1"),
##         (else_try),
##           (party_get_num_attached_parties, ":num_attachments", "$qst_capture_conspirators_party_1"),
##           (gt, ":num_attachments", 0),
##           (assign, ":leave_meeting", 0),
##           (try_begin),
##             (store_sub, ":required_attachments", "$qst_capture_conspirators_num_parties_to_spawn", 1),
##             (eq, ":num_attachments", ":required_attachments"),
##             (val_add, "$qst_capture_conspirators_leave_meeting_counter", 1),
##             (ge, "$qst_capture_conspirators_leave_meeting_counter", 15),
##             (assign, ":leave_meeting", 1),
##           (try_end),
##           (try_begin),
##             (eq, "$qst_capture_conspirators_num_parties_to_spawn", "$qst_capture_conspirators_num_parties_spawned"),
##             (store_distance_to_party_from_party, ":cur_distance", "p_main_party", "$qst_capture_conspirators_party_1"),
##             (assign, ":min_distance", 3),
##             (try_begin),
##               (is_currently_night),
##               (assign, ":min_distance", 2),
##             (try_end),
##             (lt, ":cur_distance", ":min_distance"),
##             (assign, "$qst_capture_conspirators_leave_meeting_counter", 15),
##             (assign, ":leave_meeting", 1),
##           (try_end),
##           (eq, ":leave_meeting", 1),
##           (party_set_ai_behavior, "$qst_capture_conspirators_party_1", ai_bhvr_travel_to_point),
##           (party_set_flags, "$qst_capture_conspirators_party_1", pf_default_behavior, 0),
##           (party_get_position, pos1, "$qst_capture_conspirators_party_1"),
##           (call_script, "script_map_get_random_position_around_position_within_range", 15, 17),
##           (party_set_ai_target_position, "$qst_capture_conspirators_party_1", pos2),
##           (try_begin),
##             (gt, "$qst_capture_conspirators_party_2", 0),
##             (party_detach, "$qst_capture_conspirators_party_2"),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_2", ai_bhvr_travel_to_point),
##             (party_set_flags, "$qst_capture_conspirators_party_2", pf_default_behavior, 0),
##             (call_script, "script_map_get_random_position_around_position_within_range", 15, 17),
##             (party_set_ai_target_position, "$qst_capture_conspirators_party_2", pos2),
##           (try_end),
##           (try_begin),
##             (gt, "$qst_capture_conspirators_party_3", 0),
##             (party_detach, "$qst_capture_conspirators_party_3"),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_3", ai_bhvr_travel_to_point),
##             (party_set_flags, "$qst_capture_conspirators_party_3", pf_default_behavior, 0),
##             (call_script, "script_map_get_random_position_around_position_within_range", 15, 17),
##             (party_set_ai_target_position, "$qst_capture_conspirators_party_3", pos2),
##           (try_end),
##           (try_begin),
##             (gt, "$qst_capture_conspirators_party_4", 0),
##             (party_detach, "$qst_capture_conspirators_party_4"),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_4", ai_bhvr_travel_to_point),
##             (party_set_flags, "$qst_capture_conspirators_party_4", pf_default_behavior, 0),
##             (call_script, "script_map_get_random_position_around_position_within_range", 15, 17),
##             (party_set_ai_target_position, "$qst_capture_conspirators_party_4", pos2),
##           (try_end),
##           (try_begin),
##             (gt, "$qst_capture_conspirators_party_5", 0),
##             (party_detach, "$qst_capture_conspirators_party_5"),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_5", ai_bhvr_travel_to_point),
##             (party_set_flags, "$qst_capture_conspirators_party_5", pf_default_behavior, 0),
##             (call_script, "script_map_get_random_position_around_position_within_range", 15, 17),
##             (party_set_ai_target_position, "$qst_capture_conspirators_party_5", pos2),
##           (try_end),
##           (try_begin),
##             (gt, "$qst_capture_conspirators_party_6", 0),
##             (party_detach, "$qst_capture_conspirators_party_6"),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_6", ai_bhvr_travel_to_point),
##             (party_set_flags, "$qst_capture_conspirators_party_6", pf_default_behavior, 0),
##             (call_script, "script_map_get_random_position_around_position_within_range", 15, 17),
##             (party_set_ai_target_position, "$qst_capture_conspirators_party_6", pos2),
##           (try_end),
##           (try_begin),
##             (gt, "$qst_capture_conspirators_party_7", 0),
##             (party_detach, "$qst_capture_conspirators_party_7"),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_7", ai_bhvr_travel_to_point),
##             (party_set_flags, "$qst_capture_conspirators_party_7", pf_default_behavior, 0),
##             (call_script, "script_map_get_random_position_around_position_within_range", 15, 17),
##             (party_set_ai_target_position, "$qst_capture_conspirators_party_7", pos2),
##           (try_end),
##         (try_end),
##         (try_begin),
##           (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_1"),
##           (eq, ":ai_behavior", ai_bhvr_travel_to_point),
##           (party_get_ai_target_position, pos2, "$qst_capture_conspirators_party_1"),
##           (party_get_position, pos1, "$qst_capture_conspirators_party_1"),
##           (get_distance_between_positions, ":distance", pos2, pos1),
##           (lt, ":distance", 200),
##           (call_script, "script_get_closest_walled_center_of_faction", "$qst_capture_conspirators_party_1", ":faction_no"),#Can fail
##           (ge, reg0, 0),
##           (party_set_ai_object, "$qst_capture_conspirators_party_1", reg0),
##           (party_set_ai_behavior, "$qst_capture_conspirators_party_1", ai_bhvr_travel_to_party),
##           (party_set_flags, "$qst_capture_conspirators_party_1", pf_default_behavior, 0),
##         (try_end),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_capture_conspirators_party_2", 0),
##         (party_is_active, "$qst_capture_conspirators_party_2"),
##         (val_add, ":num_active_parties", 1),
##         (try_begin),
##           (party_is_in_any_town, "$qst_capture_conspirators_party_2"),
##           (try_begin),
##             (neg|party_is_in_town, "$qst_capture_conspirators_party_2", "$qst_capture_conspirators_party_1"),
##             (remove_party, "$qst_capture_conspirators_party_2"),
##           (else_try),
##             (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_2"),
##             (neq, ":ai_behavior", ai_bhvr_hold),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_2", ai_bhvr_hold),
##             (party_attach_to_party, "$qst_capture_conspirators_party_2", "$qst_capture_conspirators_party_1"),
##             (party_set_flags, "$qst_capture_conspirators_party_2", pf_default_behavior, 0),
##           (try_end),
##         (try_end),
##         (try_begin),
##           (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_2"),
##           (eq, ":ai_behavior", ai_bhvr_travel_to_point),
##           (party_get_ai_target_position, pos2, "$qst_capture_conspirators_party_2"),
##           (party_get_position, pos1, "$qst_capture_conspirators_party_2"),
##           (get_distance_between_positions, ":distance", pos2, pos1),
##           (lt, ":distance", 200),
##           (call_script, "script_get_closest_walled_center_of_faction", "$qst_capture_conspirators_party_2", ":faction_no"),#Can fail
##           (ge, reg0, 0),
##           (party_set_ai_object, "$qst_capture_conspirators_party_2", reg0),
##           (party_set_ai_behavior, "$qst_capture_conspirators_party_2", ai_bhvr_travel_to_party),
##           (party_set_flags, "$qst_capture_conspirators_party_2", pf_default_behavior, 0),
##         (try_end),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_capture_conspirators_party_3", 0),
##         (party_is_active, "$qst_capture_conspirators_party_3"),
##         (val_add, ":num_active_parties", 1),
##         (try_begin),
##           (party_is_in_any_town, "$qst_capture_conspirators_party_3"),
##           (try_begin),
##             (neg|party_is_in_town, "$qst_capture_conspirators_party_3", "$qst_capture_conspirators_party_1"),
##             (remove_party, "$qst_capture_conspirators_party_3"),
##           (else_try),
##             (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_3"),
##             (neq, ":ai_behavior", ai_bhvr_hold),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_3", ai_bhvr_hold),
##             (party_attach_to_party, "$qst_capture_conspirators_party_3", "$qst_capture_conspirators_party_1"),
##             (party_set_flags, "$qst_capture_conspirators_party_3", pf_default_behavior, 0),
##           (try_end),
##         (try_end),
##         (try_begin),
##           (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_3"),
##           (eq, ":ai_behavior", ai_bhvr_travel_to_point),
##           (party_get_ai_target_position, pos2, "$qst_capture_conspirators_party_3"),
##           (party_get_position, pos1, "$qst_capture_conspirators_party_3"),
##           (get_distance_between_positions, ":distance", pos2, pos1),
##           (lt, ":distance", 200),
##           (call_script, "script_get_closest_walled_center_of_faction", "$qst_capture_conspirators_party_3", ":faction_no"),#Can fail
##           (ge, reg0, 0),
##           (party_set_ai_object, "$qst_capture_conspirators_party_3", reg0),
##           (party_set_ai_behavior, "$qst_capture_conspirators_party_3", ai_bhvr_travel_to_party),
##           (party_set_flags, "$qst_capture_conspirators_party_3", pf_default_behavior, 0),
##         (try_end),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_capture_conspirators_party_4", 0),
##         (party_is_active, "$qst_capture_conspirators_party_4"),
##         (val_add, ":num_active_parties", 1),
##         (try_begin),
##           (party_is_in_any_town, "$qst_capture_conspirators_party_4"),
##           (try_begin),
##             (neg|party_is_in_town, "$qst_capture_conspirators_party_4", "$qst_capture_conspirators_party_1"),
##             (remove_party, "$qst_capture_conspirators_party_4"),
##           (else_try),
##             (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_4"),
##             (neq, ":ai_behavior", ai_bhvr_hold),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_4", ai_bhvr_hold),
##             (party_set_flags, "$qst_capture_conspirators_party_4", pf_default_behavior, 0),
##             (party_attach_to_party, "$qst_capture_conspirators_party_4", "$qst_capture_conspirators_party_1"),
##           (try_end),
##         (try_end),
##         (try_begin),
##           (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_4"),
##           (eq, ":ai_behavior", ai_bhvr_travel_to_point),
##           (party_get_ai_target_position, pos2, "$qst_capture_conspirators_party_4"),
##           (party_get_position, pos1, "$qst_capture_conspirators_party_4"),
##           (get_distance_between_positions, ":distance", pos2, pos1),
##           (lt, ":distance", 200),
##           (call_script, "script_get_closest_walled_center_of_faction", "$qst_capture_conspirators_party_4", ":faction_no"),#Can fail
##           (ge, reg0, 0),
##           (party_set_ai_object, "$qst_capture_conspirators_party_4", reg0),
##           (party_set_ai_behavior, "$qst_capture_conspirators_party_4", ai_bhvr_travel_to_party),
##           (party_set_flags, "$qst_capture_conspirators_party_4", pf_default_behavior, 0),
##         (try_end),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_capture_conspirators_party_5", 0),
##         (party_is_active, "$qst_capture_conspirators_party_5"),
##         (val_add, ":num_active_parties", 1),
##         (try_begin),
##           (party_is_in_any_town, "$qst_capture_conspirators_party_5"),
##           (try_begin),
##             (neg|party_is_in_town, "$qst_capture_conspirators_party_5", "$qst_capture_conspirators_party_1"),
##             (remove_party, "$qst_capture_conspirators_party_5"),
##           (else_try),
##             (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_5"),
##             (neq, ":ai_behavior", ai_bhvr_hold),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_5", ai_bhvr_hold),
##             (party_set_flags, "$qst_capture_conspirators_party_5", pf_default_behavior, 0),
##             (party_attach_to_party, "$qst_capture_conspirators_party_5", "$qst_capture_conspirators_party_1"),
##           (try_end),
##         (try_end),
##         (try_begin),
##           (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_5"),
##           (eq, ":ai_behavior", ai_bhvr_travel_to_point),
##           (party_get_ai_target_position, pos2, "$qst_capture_conspirators_party_5"),
##           (party_get_position, pos1, "$qst_capture_conspirators_party_5"),
##           (get_distance_between_positions, ":distance", pos2, pos1),
##           (lt, ":distance", 200),
##           (call_script, "script_get_closest_walled_center_of_faction", "$qst_capture_conspirators_party_5", ":faction_no"),#Can fail
##           (ge, reg0, 0),
##           (party_set_ai_object, "$qst_capture_conspirators_party_5", reg0),
##           (party_set_ai_behavior, "$qst_capture_conspirators_party_5", ai_bhvr_travel_to_party),
##           (party_set_flags, "$qst_capture_conspirators_party_5", pf_default_behavior, 0),
##         (try_end),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_capture_conspirators_party_6", 0),
##         (party_is_active, "$qst_capture_conspirators_party_6"),
##         (val_add, ":num_active_parties", 1),
##         (try_begin),
##           (party_is_in_any_town, "$qst_capture_conspirators_party_6"),
##           (try_begin),
##             (neg|party_is_in_town, "$qst_capture_conspirators_party_6", "$qst_capture_conspirators_party_1"),
##             (remove_party, "$qst_capture_conspirators_party_6"),
##           (else_try),
##             (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_6"),
##             (neq, ":ai_behavior", ai_bhvr_hold),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_6", ai_bhvr_hold),
##             (party_set_flags, "$qst_capture_conspirators_party_6", pf_default_behavior, 0),
##             (party_attach_to_party, "$qst_capture_conspirators_party_6", "$qst_capture_conspirators_party_1"),
##           (try_end),
##         (try_end),
##         (try_begin),
##           (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_6"),
##           (eq, ":ai_behavior", ai_bhvr_travel_to_point),
##           (party_get_ai_target_position, pos2, "$qst_capture_conspirators_party_6"),
##           (party_get_position, pos1, "$qst_capture_conspirators_party_6"),
##           (get_distance_between_positions, ":distance", pos2, pos1),
##           (lt, ":distance", 200),
##           (call_script, "script_get_closest_walled_center_of_faction", "$qst_capture_conspirators_party_6", ":faction_no"),#Can fail
##           (ge, reg0, 0),
##           (party_set_ai_object, "$qst_capture_conspirators_party_6", reg0),
##           (party_set_ai_behavior, "$qst_capture_conspirators_party_6", ai_bhvr_travel_to_party),
##           (party_set_flags, "$qst_capture_conspirators_party_6", pf_default_behavior, 0),
##         (try_end),
##       (try_end),
##       (try_begin),
##         (gt, "$qst_capture_conspirators_party_7", 0),
##         (party_is_active, "$qst_capture_conspirators_party_7"),
##         (val_add, ":num_active_parties", 1),
##         (try_begin),
##           (party_is_in_any_town, "$qst_capture_conspirators_party_7"),
##           (try_begin),
##             (neg|party_is_in_town, "$qst_capture_conspirators_party_7", "$qst_capture_conspirators_party_1"),
##             (remove_party, "$qst_capture_conspirators_party_7"),
##           (else_try),
##             (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_7"),
##             (neq, ":ai_behavior", ai_bhvr_hold),
##             (party_set_ai_behavior, "$qst_capture_conspirators_party_7", ai_bhvr_hold),
##             (party_set_flags, "$qst_capture_conspirators_party_7", pf_default_behavior, 0),
##             (party_attach_to_party, "$qst_capture_conspirators_party_7", "$qst_capture_conspirators_party_1"),
##           (try_end),
##         (try_end),
##         (try_begin),
##           (get_party_ai_behavior, ":ai_behavior", "$qst_capture_conspirators_party_7"),
##           (eq, ":ai_behavior", ai_bhvr_travel_to_point),
##           (party_get_ai_target_position, pos2, "$qst_capture_conspirators_party_7"),
##           (party_get_position, pos1, "$qst_capture_conspirators_party_7"),
##           (get_distance_between_positions, ":distance", pos2, pos1),
##           (lt, ":distance", 200),
##           (call_script, "script_get_closest_walled_center_of_faction", "$qst_capture_conspirators_party_7", ":faction_no"),#Can fail
##           (ge, reg0, 0),
##           (party_set_ai_object, "$qst_capture_conspirators_party_7", reg0),
##           (party_set_ai_behavior, "$qst_capture_conspirators_party_7", ai_bhvr_travel_to_party),
##           (party_set_flags, "$qst_capture_conspirators_party_7", pf_default_behavior, 0),
##         (try_end),
##       (try_end),
##
##       (eq, ":num_active_parties", 0),
##       (party_count_prisoners_of_type, ":count_captured_conspirators", "p_main_party", "trp_conspirator"),
##       (party_count_prisoners_of_type, ":count_captured_conspirator_leaders", "p_main_party", "trp_conspirator_leader"),
##       (val_add, ":count_captured_conspirators", ":count_captured_conspirator_leaders"),
##       (try_begin),
##         (store_div, ":limit", "$qst_capture_conspirators_num_troops_to_capture", 2),
##         (gt, ":count_captured_conspirators", ":limit"),
##         (call_script, "script_succeed_quest", "qst_capture_conspirators"),
##       (else_try),
##         (call_script, "script_fail_quest", "qst_capture_conspirators"),
##       (try_end),
##    ],
##   []
##   ),
# Follow Spy quest
  (0.5, 0.0, 0.0,
   [
       (check_quest_active, "qst_follow_spy"),
       (eq, "$qst_follow_spy_no_active_parties", 0),
       (quest_get_slot, ":quest_giver_center", "qst_follow_spy", slot_quest_giver_center),
       (quest_get_slot, ":quest_object_center", "qst_follow_spy", slot_quest_object_center),
       (assign, ":abort_meeting", 0),
       (try_begin),
         (this_or_next|ge, "$qst_follow_spy_run_away", 2),
         (this_or_next|neg|party_is_active, "$qst_follow_spy_spy_party"),
         (neg|party_is_active, "$qst_follow_spy_spy_partners_party"),
       (else_try),
         (eq, "$qst_follow_spy_meeting_state", 0),
         (store_distance_to_party_from_party, ":cur_distance", "p_main_party", "$qst_follow_spy_spy_party"),
         (try_begin),
           (assign, ":min_distance", 3),
           (try_begin),
             (is_currently_night),
             (assign, ":min_distance", 1),
           (try_end),
           (le, ":cur_distance", ":min_distance"),
           (store_distance_to_party_from_party, ":player_distance_to_quest_giver_center", "p_main_party", ":quest_giver_center"),
           (gt, ":player_distance_to_quest_giver_center", 1),
           (val_add, "$qst_follow_spy_run_away", 1),
           (try_begin),
             (eq, "$qst_follow_spy_run_away", 2),
             (assign, ":abort_meeting", 1),
             (display_message, "str_qst_follow_spy_noticed_you"),
           (try_end),
         (else_try),
           (store_distance_to_party_from_party, ":cur_distance", "$qst_follow_spy_spy_partners_party", "$qst_follow_spy_spy_party"),
           (le, ":cur_distance", 1),
           (party_attach_to_party, "$qst_follow_spy_spy_party", "$qst_follow_spy_spy_partners_party"),
           (assign, "$qst_follow_spy_meeting_state", 1),
           (assign, "$qst_follow_spy_meeting_counter", 0),
         (try_end),
       (else_try),
         (eq, "$qst_follow_spy_meeting_state", 1),
         (store_distance_to_party_from_party, ":cur_distance", "p_main_party", "$qst_follow_spy_spy_partners_party"),
         (try_begin),
           (le, ":cur_distance", 1),
           (party_detach, "$qst_follow_spy_spy_party"),
           (val_add, "$qst_follow_spy_run_away", 1),
           (try_begin),
             (eq, "$qst_follow_spy_run_away", 2),
             (assign, ":abort_meeting", 1),
             (display_message, "str_qst_follow_spy_noticed_you"),
           (try_end),
         (else_try),
           (val_add, "$qst_follow_spy_meeting_counter", 1),
           (gt, "$qst_follow_spy_meeting_counter", 4),
           (party_detach, "$qst_follow_spy_spy_party"),
           (assign, ":abort_meeting", 1),
           (assign, "$qst_follow_spy_meeting_state", 2),
         (try_end),
       (try_end),
       (try_begin),
         (eq, ":abort_meeting", 1),
         (party_set_ai_object, "$qst_follow_spy_spy_party", ":quest_giver_center"),
         
         (party_set_ai_object, "$qst_follow_spy_spy_partners_party", ":quest_object_center"),
         
         (party_set_ai_behavior, "$qst_follow_spy_spy_party", ai_bhvr_travel_to_party),
         (party_set_ai_behavior, "$qst_follow_spy_spy_partners_party", ai_bhvr_travel_to_party),
         (party_set_flags, "$qst_follow_spy_spy_party", pf_default_behavior, 0),
         (party_set_flags, "$qst_follow_spy_spy_partners_party", pf_default_behavior, 0),
       (try_end),
       (assign, ":num_active", 0),
       (try_begin),
         (party_is_active, "$qst_follow_spy_spy_party"),
         (val_add, ":num_active", 1),
         (party_is_in_town, "$qst_follow_spy_spy_party", ":quest_giver_center"),
         (remove_party, "$qst_follow_spy_spy_party"),
         (assign, "$qst_follow_spy_spy_back_in_town", 1),
         (val_sub, ":num_active", 1),
       (try_end),
       (try_begin),
         (party_is_active, "$qst_follow_spy_spy_partners_party"),
         (val_add, ":num_active", 1),
         (party_is_in_town, "$qst_follow_spy_spy_partners_party", ":quest_object_center"),
         (remove_party, "$qst_follow_spy_spy_partners_party"),
         (assign, "$qst_follow_spy_partner_back_in_town", 1),
         (val_sub, ":num_active", 1),
       (try_end),
       (try_begin),
         (eq, "$qst_follow_spy_partner_back_in_town",1),
         (eq, "$qst_follow_spy_spy_back_in_town",1),
         (call_script, "script_fail_quest", "qst_follow_spy"),
       (try_end),
       (try_begin),
         (eq, ":num_active", 0),
         (assign, "$qst_follow_spy_no_active_parties", 1),
         (party_count_prisoners_of_type, ":num_spies", "p_main_party", "trp_spy"),
         (party_count_prisoners_of_type, ":num_spy_partners", "p_main_party", "trp_spy_partner"),
         (gt, ":num_spies", 0),
         (gt, ":num_spy_partners", 0),
         (call_script, "script_succeed_quest", "qst_follow_spy"),
       (try_end),
    ],
   []
   ),
### Raiders quest
##  (0.95, 0.0, 0.2,
##   [
##       (check_quest_active, "qst_hunt_down_raiders"),
##       (neg|check_quest_succeeded, "qst_hunt_down_raiders"),
##       (neg|check_quest_failed, "qst_hunt_down_raiders"),
##    ],
##   [
##       (quest_get_slot, ":quest_target_party", "qst_hunt_down_raiders", slot_quest_target_party),
##       (party_set_ai_behavior, ":quest_target_party", ai_bhvr_hold),
##       (party_set_flags, ":quest_target_party", pf_default_behavior, 0),
##    ]
##   ),
##
##  (0.7, 0, 0.2,
##   [
##       (check_quest_active, "qst_hunt_down_raiders"),
##       (neg|check_quest_succeeded, "qst_hunt_down_raiders"),
##       (neg|check_quest_failed, "qst_hunt_down_raiders"),
##    ],
##   [
##       (quest_get_slot, ":quest_target_party", "qst_hunt_down_raiders", slot_quest_target_party),
##       (party_set_ai_behavior,":quest_target_party",ai_bhvr_travel_to_party),
##       (party_set_flags, ":quest_target_party", pf_default_behavior, 0),
##    ]
##   ),
##  
##  (0.1, 0.0, 0.0,
##   [
##       (check_quest_active, "qst_hunt_down_raiders"),
##       (neg|check_quest_succeeded, "qst_hunt_down_raiders"),
##       (neg|check_quest_failed, "qst_hunt_down_raiders"),
##       (quest_get_slot, ":quest_target_party", "qst_hunt_down_raiders", slot_quest_target_party),
##       (neg|party_is_active, ":quest_target_party")
##    ],
##   [
##       (call_script, "script_succeed_quest", "qst_hunt_down_raiders"),
##    ]
##   ),
##  
##  (1.3, 0, 0.0,
##   [
##       (check_quest_active, "qst_hunt_down_raiders"),
##       (neg|check_quest_succeeded, "qst_hunt_down_raiders"),
##       (neg|check_quest_failed, "qst_hunt_down_raiders"),
##       (quest_get_slot, ":quest_target_party", "qst_hunt_down_raiders", slot_quest_target_party),
##       (quest_get_slot, ":quest_target_center", "qst_hunt_down_raiders", slot_quest_target_center),
##       (party_is_in_town,":quest_target_party",":quest_target_center")
##    ],
##   [
##       (call_script, "script_fail_quest", "qst_hunt_down_raiders"),
##       (display_message, "str_raiders_reached_base"),
##       (quest_get_slot, ":quest_target_party", "qst_hunt_down_raiders", slot_quest_target_party),
##       (remove_party, ":quest_target_party"),
##    ]
##   ),

##### TODO: QUESTS COMMENT OUT END

#########################################################################
# Random MERCHANT quest triggers
####################################  
 # Apply interest to merchants guild debt  1% per week
  (24.0 * 7, 0.0, 0.0,
   [],
   [
       (val_mul,"$debt_to_merchants_guild",101),
       (val_div,"$debt_to_merchants_guild",100)
    ]
   ),
# Escort merchant caravan:
  (0.1, 0.0, 0.1, [(check_quest_active, "qst_escort_merchant_caravan"),
                   (eq, "$escort_merchant_caravan_mode", 1)
                   ],
                  [(quest_get_slot, ":quest_target_party", "qst_escort_merchant_caravan", slot_quest_target_party),
                   (try_begin),
                     (party_is_active, ":quest_target_party"),
                     (party_set_ai_behavior, ":quest_target_party", ai_bhvr_hold),
                     (party_set_flags, ":quest_target_party", pf_default_behavior, 0),
                   (try_end),
                   ]),
  (0.1, 0.0, 0.1, [(check_quest_active, "qst_escort_merchant_caravan"),
                    (eq, "$escort_merchant_caravan_mode", 0),
                    ],
                   [(quest_get_slot, ":quest_target_party", "qst_escort_merchant_caravan", slot_quest_target_party),
                    (try_begin),
                      (party_is_active, ":quest_target_party"),
                      (party_set_ai_behavior, ":quest_target_party", ai_bhvr_escort_party),
                      (party_set_flags, ":quest_target_party", pf_default_behavior, 0),
                      (party_set_ai_object, ":quest_target_party", "p_main_party"),
                    (try_end),
                    ]),

  (0.1, 0, 0.0, [(check_quest_active, "qst_escort_merchant_caravan"),
                 (quest_get_slot, ":quest_target_party", "qst_escort_merchant_caravan", slot_quest_target_party),
                 (neg|party_is_active,":quest_target_party"),
                 ],
                [(call_script, "script_abort_quest", "qst_escort_merchant_caravan", 2),
                 ]),

# Troublesome bandits
  (0.3, 0.0, 1.1, [(check_quest_active, "qst_troublesome_bandits"),
                   (neg|check_quest_failed, "qst_troublesome_bandits"),
                   (store_num_parties_destroyed, ":cur_eliminated", "pt_troublesome_bandits"),
                   (lt, "$qst_troublesome_bandits_eliminated", ":cur_eliminated"),
                   (store_num_parties_destroyed_by_player, ":cur_eliminated_by_player", "pt_troublesome_bandits"),
                   (eq, ":cur_eliminated_by_player", "$qst_troublesome_bandits_eliminated_by_player"),
                   ],
                  [(display_message, "str_bandits_eliminated_by_another"),
                   (call_script, "script_abort_quest", "qst_troublesome_bandits", 2),
                   ]),

  (0.3, 0.0, 1.1, [(check_quest_active, "qst_troublesome_bandits"),
                   (neg|check_quest_succeeded, "qst_troublesome_bandits"),
                   (store_num_parties_destroyed, ":cur_eliminated", "pt_troublesome_bandits"),
                   (lt, "$qst_troublesome_bandits_eliminated", ":cur_eliminated"),
                   (store_num_parties_destroyed_by_player, ":cur_eliminated_by_player", "pt_troublesome_bandits"),
                   (neq, ":cur_eliminated_by_player", "$qst_troublesome_bandits_eliminated_by_player"),
                   ],
                  [(call_script, "script_succeed_quest", "qst_troublesome_bandits"),]),
	
# NE quests 

# v585 Josef Cheese Rustlers				  

  (0.3, 0.0, 1.1, [(check_quest_active, "qst_cheese_rustlers"),
                   (neg|check_quest_failed, "qst_cheese_rustlers"),
                   (store_num_parties_destroyed, ":cur_eliminated", "pt_cheese_rustlers"),
                   (lt, "$qst_cheese_rustlers_eliminated", ":cur_eliminated"),
                   (store_num_parties_destroyed_by_player, ":cur_eliminated_by_player", "pt_cheese_rustlers"),
                   (eq, ":cur_eliminated_by_player", "$qst_cheese_rustlers_eliminated_by_player"),
                   ],
                  [(display_message, "str_cheese_rustlers_eliminated_by_another"),
                   (call_script, "script_abort_quest", "qst_cheese_rustlers", 2),
                   ]),

  (0.3, 0.0, 1.1, [(check_quest_active, "qst_cheese_rustlers"),
                   (neg|check_quest_succeeded, "qst_cheese_rustlers"),
                   (store_num_parties_destroyed, ":cur_eliminated", "pt_cheese_rustlers"),
                   (lt, "$qst_cheese_rustlers_eliminated", ":cur_eliminated"),
                   (store_num_parties_destroyed_by_player, ":cur_eliminated_by_player", "pt_cheese_rustlers"),
                   (neq, ":cur_eliminated_by_player", "$qst_cheese_rustlers_eliminated_by_player"),
                   ],
                  [(call_script, "script_succeed_quest", "qst_cheese_rustlers"),]),

# v585 Josef Cheese paladins
				  
  (0.3, 0.0, 1.1, [(check_quest_active, "qst_cheese_paladins"),
                   (neg|check_quest_failed, "qst_cheese_paladins"),
                   (store_num_parties_destroyed, ":cur_eliminated", "pt_cheese_paladins"),
                   (lt, "$qst_cheese_paladins_eliminated", ":cur_eliminated"),
                   (store_num_parties_destroyed_by_player, ":cur_eliminated_by_player", "pt_cheese_paladins"),
                   (eq, ":cur_eliminated_by_player", "$qst_cheese_paladins_eliminated_by_player"),
                   ],
                  [(display_message, "str_cheese_rustlers_eliminated_by_another"),
                   (call_script, "script_abort_quest", "qst_cheese_paladins", 2),
                   ]),

  (0.3, 0.0, 1.1, [(check_quest_active, "qst_cheese_paladins"),
                   (neg|check_quest_succeeded, "qst_cheese_paladins"),
                   (store_num_parties_destroyed, ":cur_eliminated", "pt_cheese_paladins"),
                   (lt, "$qst_cheese_paladins_eliminated", ":cur_eliminated"),
                   (store_num_parties_destroyed_by_player, ":cur_eliminated_by_player", "pt_cheese_paladins"),
                   (neq, ":cur_eliminated_by_player", "$qst_cheese_paladins_eliminated_by_player"),
                   ],
                  [(call_script, "script_succeed_quest", "qst_cheese_paladins"),]),

# v585 Josef escort scholars 

  (0.1, 0.0, 0.1, [(check_quest_active, "qst_escort_scholars"),
                   (eq, "$escort_scholars_mode", 1)
                   ],
                  [(quest_get_slot, ":quest_target_party", "qst_escort_scholars", slot_quest_target_party),
                   (try_begin),
                     (party_is_active, ":quest_target_party"),
                     (party_set_ai_behavior, ":quest_target_party", ai_bhvr_hold),
                     (party_set_flags, ":quest_target_party", pf_default_behavior, 0),
                   (try_end),
                   ]),
  (0.1, 0.0, 0.1, [(check_quest_active, "qst_escort_scholars"),
                    (eq, "$escort_scholars_mode", 0),
                    ],
                   [(quest_get_slot, ":quest_target_party", "qst_escort_scholars", slot_quest_target_party),
                    (try_begin),
                      (party_is_active, ":quest_target_party"),
                      (party_set_ai_behavior, ":quest_target_party", ai_bhvr_escort_party),
                      (party_set_flags, ":quest_target_party", pf_default_behavior, 0),
                      (party_set_ai_object, ":quest_target_party", "p_main_party"),
                    (try_end),
                    ]),

  (0.1, 0, 0.0, [(check_quest_active, "qst_escort_scholars"),
                 (quest_get_slot, ":quest_target_party", "qst_escort_scholars", slot_quest_target_party),
                 (neg|party_is_active,":quest_target_party"),
                 ],
                [(call_script, "script_abort_quest", "qst_escort_scholars", 2),
                 ]),

# v585 Josef kidnapped wife
   (1, 0, 0,
   [(check_quest_active, "qst_kidnapped_wife"),
    (quest_get_slot, ":quest_target_party", "qst_kidnapped_wife", slot_quest_target_party),
    (party_is_active, ":quest_target_party"),
    (party_is_in_any_town, ":quest_target_party"),
    (remove_party, ":quest_target_party"),
    ],
   []
   ),
				  
# v585 Josef end

# NE end quests 	
# Kidnapped girl:
   (1, 0, 0,
   [(check_quest_active, "qst_kidnapped_girl"),
    (quest_get_slot, ":quest_target_party", "qst_kidnapped_girl", slot_quest_target_party),
    (party_is_active, ":quest_target_party"),
    (party_is_in_any_town, ":quest_target_party"),
    (remove_party, ":quest_target_party"),
    ],
   []
   ),


#Rebellion changes begin
#move 

  (0, 0, 24 * 14,
   [
        (try_for_range, ":pretender", pretenders_begin, pretenders_end),
          (troop_set_slot, ":pretender", slot_troop_cur_center, 0),
          (neq, ":pretender", "$supported_pretender"),
          (troop_get_slot, ":target_faction", ":pretender", slot_troop_original_faction),
          (faction_slot_eq, ":target_faction", slot_faction_state, sfs_active),
          (faction_slot_eq, ":target_faction", slot_faction_has_rebellion_chance, 1),
          (neg|troop_slot_eq, ":pretender", slot_troop_occupation, slto_kingdom_hero),

          (try_for_range, ":unused", 0, 30),
            (troop_slot_eq, ":pretender", slot_troop_cur_center, 0),
            (store_random_in_range, ":town", towns_begin, towns_end),
            (store_faction_of_party, ":town_faction", ":town"),
            (store_relation, ":relation", ":town_faction", ":target_faction"),
            (le, ":relation", 0), #fail if nothing qualifies
           
            (troop_set_slot, ":pretender", slot_troop_cur_center, ":town"),
            (try_begin),
              (eq, "$cheat_mode", 1),
              (str_store_troop_name, 4, ":pretender"),
              (str_store_party_name, 5, ":town"),
              (display_message, "@{!}{s4} is in {s5}"),
            (try_end),
          (try_end),

#        (try_for_range, ":rebel_faction", rebel_factions_begin, rebel_factions_end),
#            (faction_get_slot, ":rebellion_status", ":rebel_faction", slot_faction_state),
#            (eq, ":rebellion_status", sfs_inactive_rebellion),
#            (faction_get_slot, ":pretender", ":rebel_faction", slot_faction_leader),
#            (faction_get_slot, ":target_faction", ":rebel_faction", slot_faction_rebellion_target),#

#            (store_random_in_range, ":town", towns_begin, towns_end),
#            (store_faction_of_party, ":town_faction", ":town"),
#            (store_relation, ":relation", ":town_faction", ":target_faction"),
#            (le, ":relation", 0), #fail if nothing qualifies

 #           (faction_set_slot, ":rebel_faction", slot_faction_inactive_leader_location, ":town"),
        (try_end), 
       ],
[]
),
#Rebellion changes end

#NPC system changes begin
#Move unemployed NPCs around taverns
   (24 * 15 , 0, 0, 
   [
    (call_script, "script_update_companion_candidates_in_taverns"),
    ],
   []
   ),

#Process morale and determine personality clashes
  (0, 0, 24,
   [],
[

#Count NPCs in party and get the "grievance divisor", which determines how fast grievances go away
#Set their relation to the player
        (assign, ":npcs_in_party", 0),
        (assign, ":grievance_divisor", 100),
        (try_for_range, ":npc1", companions_begin, companions_end),
            (main_party_has_troop, ":npc1"),
            (val_add, ":npcs_in_party", 1),
        (try_end),
        (val_sub, ":grievance_divisor", ":npcs_in_party"),
        (store_skill_level, ":persuasion_level", "skl_persuasion", "trp_player"),
        (val_add, ":grievance_divisor", ":persuasion_level"),
        (assign, reg7, ":grievance_divisor"),

#        (display_message, "@{!}Process NPC changes. GD: {reg7}"),



##Activate personality clash from 24 hours ago
        (try_begin), #scheduled personality clashes require at least 24hrs together
             (gt, "$personality_clash_after_24_hrs", 0),
             (eq, "$disable_npc_complaints", 0),
             (try_begin),
                  (troop_get_slot, ":other_npc", "$personality_clash_after_24_hrs", slot_troop_personalityclash_object),
                  (main_party_has_troop, "$personality_clash_after_24_hrs"),
                  (main_party_has_troop, ":other_npc"),
                  (assign, "$npc_with_personality_clash", "$personality_clash_after_24_hrs"),
             (try_end),
             (assign, "$personality_clash_after_24_hrs", 0),
        (try_end),
#

         
        (try_for_range, ":npc", companions_begin, companions_end),
###Reset meeting variables
            (troop_set_slot, ":npc", slot_troop_turned_down_twice, 0),
            (try_begin),
                (troop_slot_eq, ":npc", slot_troop_met, 1),
                (troop_set_slot, ":npc", slot_troop_met_previously, 1),
            (try_end),

###Check for coming out of retirement
            (troop_get_slot, ":occupation", ":npc", slot_troop_occupation),
            (try_begin),
                (eq, ":occupation", slto_retirement),
                (troop_get_slot, ":renown_min", ":npc", slot_troop_return_renown),

                (str_store_troop_name, s31, ":npc"),
                (troop_get_slot, ":player_renown", "trp_player", slot_troop_renown),
                (assign, reg4, ":player_renown"),
                (assign, reg5, ":renown_min"),
#                (display_message, "@{!}Test {s31}  for retirement return {reg4}, {reg5}."),

                (gt, ":player_renown", ":renown_min"),
                (troop_set_slot, ":npc", slot_troop_personalityclash_penalties, 0),
                (troop_set_slot, ":npc", slot_troop_morality_penalties, 0),
                (troop_set_slot, ":npc", slot_troop_occupation, 0),
            (try_end),


#Check for political issues
			(try_begin), #does npc's opponent pipe up?
				(troop_slot_ge, ":npc", slot_troop_days_on_mission, 5),
				(troop_slot_eq, ":npc", slot_troop_current_mission, npc_mission_kingsupport),

				(troop_get_slot, ":other_npc", ":npc", slot_troop_kingsupport_opponent),
				(troop_slot_eq, ":other_npc", slot_troop_kingsupport_objection_state, 0),
				
				(troop_set_slot, ":other_npc", slot_troop_kingsupport_objection_state, 1),
				
				(str_store_troop_name, s3, ":npc"),
				(str_store_troop_name, s4, ":other_npc"),

				(try_begin),
					(eq, "$cheat_mode", 1),
					(display_message, "str_s4_ready_to_voice_objection_to_s3s_mission_if_in_party"),
				(try_end),
			(try_end),

			#Check for quitting
            (try_begin),
                (main_party_has_troop, ":npc"),
				
                (call_script, "script_npc_morale", ":npc"),
                (assign, ":npc_morale", reg0),

                (try_begin),
                    (lt, ":npc_morale", 20),
                    (store_random_in_range, ":random", 0, 100),
                    (val_add, ":npc_morale", ":random"),
                    (lt, ":npc_morale", 20),
                    (assign, "$npc_is_quitting", ":npc"),
                (try_end),

				#Reduce grievance over time (or augment, if party is overcrowded
                (troop_get_slot, ":grievance", ":npc", slot_troop_personalityclash_penalties),
                (val_mul, ":grievance", 90),
                (val_div, ":grievance", ":grievance_divisor"),
                (troop_set_slot, ":npc", slot_troop_personalityclash_penalties, ":grievance"),

                (troop_get_slot, ":grievance", ":npc", slot_troop_morality_penalties),
                (val_mul, ":grievance", 90),
                (val_div, ":grievance", ":grievance_divisor"),
                (troop_set_slot, ":npc", slot_troop_morality_penalties, ":grievance"),


				#Change personality grievance levels
                (try_begin),
                    (this_or_next|troop_slot_ge, ":npc", slot_troop_personalityclash_state, 1),
                        (eq, "$disable_npc_complaints", 1),
                    (troop_get_slot, ":object", ":npc", slot_troop_personalityclash_object),
                    (main_party_has_troop, ":object"),
                    (call_script, "script_reduce_companion_morale_for_clash", ":npc", ":object", slot_troop_personalityclash_state),
                (try_end),
                (try_begin),
                    (this_or_next|troop_slot_ge, ":npc", slot_troop_personalityclash2_state, 1),
                        (eq, "$disable_npc_complaints", 1),
                    (troop_get_slot, ":object", ":npc", slot_troop_personalityclash2_object),
                    (main_party_has_troop, ":object"),
                    (call_script, "script_reduce_companion_morale_for_clash", ":npc", ":object", slot_troop_personalityclash2_state),
                (try_end),
                (try_begin),
                    (this_or_next|troop_slot_ge, ":npc", slot_troop_personalitymatch_state, 1),
                        (eq, "$disable_npc_complaints", 1),
                    (troop_get_slot, ":object", ":npc", slot_troop_personalitymatch_object),
                    (main_party_has_troop, ":object"),
                    (troop_get_slot, ":grievance", ":npc", slot_troop_personalityclash_penalties),
                    (val_mul, ":grievance", 9),
                    (val_div, ":grievance", 10),
                    (troop_set_slot, ":npc", slot_troop_personalityclash_penalties, ":grievance"),
                (try_end),


				
#Check for new personality clashes

				#Active personality clash 1 if at least 24 hours have passed
                (try_begin),
                    (eq, "$disable_npc_complaints", 0),
                    (eq, "$npc_with_personality_clash", 0),
                    (eq, "$npc_with_personality_clash_2", 0),
                    (eq, "$personality_clash_after_24_hrs", 0),
                    (troop_slot_eq, ":npc", slot_troop_personalityclash_state, 0),
                    (troop_get_slot, ":other_npc", ":npc", slot_troop_personalityclash_object),
                    (main_party_has_troop, ":other_npc"),
                    (assign, "$personality_clash_after_24_hrs", ":npc"),
                (try_end),

				#Personality clash 2 and personality match is triggered by battles
				(try_begin),
					(eq, "$npc_with_political_grievance", 0),
				
					(troop_slot_eq, ":npc", slot_troop_kingsupport_objection_state, 1),
					(assign, "$npc_with_political_grievance", ":npc"),
				(try_end),

			#main party does not have troop, and the troop is a companion
			(else_try), 
				(neg|main_party_has_troop, ":npc"),
				(eq, ":occupation", slto_player_companion),

				
				(troop_get_slot, ":days_on_mission", ":npc", slot_troop_days_on_mission),
				(try_begin),
					(gt, ":days_on_mission", 0),
					(val_sub, ":days_on_mission", 1),
					(troop_set_slot, ":npc", slot_troop_days_on_mission, ":days_on_mission"),
				(else_try), 
					(troop_slot_ge, ":npc", slot_troop_current_mission, 1),
					
					#If the hero can join
					(this_or_next|neg|troop_slot_eq, ":npc", slot_troop_current_mission, npc_mission_rejoin_when_possible),
						(hero_can_join, "p_main_party"),
						
					(assign, "$npc_to_rejoin_party", ":npc"),
				(try_end),
            (try_end),
        (try_end),
    ]),




#NPC system changes end




    # PATCH TRIGGERS

    (0, 0, ti_once, [], [
        (try_begin),
            (neq, "$mod_version_initialized", 1),
            (assign, "$mod_version_initialized", 1),
            (assign, "$mod_version", 608 * 256 + 6),
            (try_begin),
                (store_item_kind_count, ":dresses", "itm_sarranid_lady_dress", "trp_kingdom_6_pretender"),
                (eq, ":dresses", 0),
                (troop_add_item, "trp_kingdom_6_pretender", "itm_sarranid_lady_dress"),
                (store_random_in_range, ":renown", 0, 1000),
                (val_add, ":renown", 1500),
                (troop_set_slot, "trp_dark_knight_lord", slot_troop_renown, ":renown"),
                (call_script, "script_update_troop_notes", "trp_dark_knight_lord"),
                (try_for_range, ":dk", "trp_dark_knight_lord1", "trp_kingdom_1_pretender"),
                    (store_random_in_range, ":renown", 0, 500),
                    (val_add, ":renown", 1000),
                    (troop_set_slot, ":dk", slot_troop_renown, ":renown"),
                    (call_script, "script_update_troop_notes", ":dk"),
                (try_end),
                #(troop_get_slot, ":town", "trp_kingdom_6_pretender", slot_troop_cur_center),
                #(str_store_party_name, s6, ":town"),
                #(str_store_troop_name, s5, "trp_kingdom_6_pretender"),
                #(display_message, "@Mod version updated. {s5} is now dressed and in {s6}."),
            (try_end),
        (try_end),
    ]),

    (0, 0, ti_once, [
        (ge, "$mod_version", 608 * 256 + 6), # Run only if version is at least 0.608b6
    ], [
        (try_begin),
            (eq, "$mod_version", 608 * 256 + 6),  # Execute anything only if version is exactly 0.608b6
            (try_for_range, ":cur_troop", active_npcs_begin, active_npcs_end),
                (store_troop_faction, ":faction_no", ":cur_troop"),
                (call_script, "script_troop_set_title_according_to_faction", ":cur_troop", ":faction_no"),
                (call_script, "script_update_troop_notes", ":cur_troop"),
            (try_end),
            (assign, "$mod_version", 608 * 256 + 7),  # Increase module version
        (try_end),
    ]),

]
